Hack Forums
[C] XMRIG C++ function >> Python 3.8 function (monero pool job) - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] XMRIG C++ function >> Python 3.8 function (monero pool job) (/showthread.php?tid=6192699)



XMRIG C++ function >> Python 3.8 function (monero pool job) - Fhack - Feb 8, 2022

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 ✌


RE: XMRIG C++ function >> Python 3.8 function (monero pool job) - Fhack - Feb 12, 2022

Nobody have the solution still stuck on it !?


RE: XMRIG C++ function >> Python 3.8 function (monero pool job) - vmpvirtualziersimp - Feb 12, 2022

rename your user title to "incompetent developer"


RE: XMRIG C++ function >> Python 3.8 function (monero pool job) - Fhack - Feb 13, 2022

(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.


RE: XMRIG C++ function >> Python 3.8 function (monero pool job) - Fhack - Mar 2, 2022

Nobody have the solution ?