Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

XMRIG C++ function >> Python 3.8 function (monero pool job)

Hello i've opened a thread lately about converting a function in xmrig to a python version of it.
I rewrote a thread with better presentation including all the needed c++ code to understand how the function works and the actual python version of it i've coded.

The code just basically get a job from a monero pool and then try to extract tx count of the block from the "blob" an hexadecimal string.

Here the c++ code :

Code
uint32_t xmrig::Job::getNumTransactions() const
{
    uint32_t num_transactions = 0;

    // Monero (and some other coins) has the number of transactions encoded as varint in the end of hashing blob
    const size_t expected_tx_offset = 75;

    if ((m_size > expected_tx_offset) && (m_size <= expected_tx_offset + 4)) {
        for (size_t i = expected_tx_offset, k = 0; i < m_size; ++i, k += 7) {
            const uint8_t b = m_blob[i];
            num_transactions |= static_cast<uint32_t>(b & 0x7F) << k;
            if ((b & 0x80) == 0) {
                break;
            }
        }
    }

    return num_transactions;
}

Code
bool xmrig::Job::setBlob(const char *blob)
{
    if (!blob) {
        return false;
    }

    m_size = strlen(blob);
    if (m_size % 2 != 0) {
        return false;
    }

    m_size /= 2;

    const size_t minSize = nonceOffset() + nonceSize();
    if (m_size < minSize || m_size >= sizeof(m_blob)) {
        return false;
    }

    if (!Cvt::fromHex(m_blob, sizeof(m_blob), blob, m_size * 2)) {
        return false;
    }

    if (readUnaligned(nonce()) != 0 && !m_nicehash) {
        m_nicehash = true;
    }

#   ifdef XMRIG_PROXY_PROJECT
    memset(m_rawBlob, 0, sizeof(m_rawBlob));
    memcpy(m_rawBlob, blob, m_size * 2);
#   endif

    return true;
}

Cvt::fromHex is just sodium_hex2bin function in c++ i tought the equivalent in python is bytesarray.fromhex(self.blob)

Here the python version i've coded :

Code
def _getNumTransactions(self):
        txCount = 0

        print(self.blob)
        blob = bytearray.fromhex(self.blob)
        txOffset = 75
        k = 0

        if txOffset < len(blob) <= txOffset + 4:
            for bit in blob[txOffset:]:
                bit = bit % 2**8
                txCount |= (((bit & 0x7F) % 2**32) << k) % 2**32
                if (bit & 0x80) == 0: break
                k += 7

        return txCount

It sometimes return the right tx count with 1 more tx and sometimes a completly wrong number ! something should went wrong with type casting or hexadecimal to bytes conversion.

Looking for some help to get the exact tx count from the actual height job !

Regards ✌
Nobody have the solution still stuck on it !?
rename your user title to "incompetent developer"
(Feb 12, 2022 03:59 PM)vmpvirtualziersimp Wrote:  rename your user title to "incompetent developer"

show me the solution of your so great intelligence son of a bitch, i'm not good in math and assembly logic, that's why i'm asking for help, at least i know my weak, your a coard to say this behind a computer.
Nobody have the solution ?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 630 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[Python] by sid123Jun 20, 2022 11:31 AM 3 168 Jun 20, 2022 11:31 AM
Last Post: dinosaur770
[C] by lynakoubaFeb 25, 2022 06:38 AM 1 152 Feb 25, 2022 06:38 AM
Last Post: ❖Onyx❖