user121910: This is not related to bitcoin but it uses most of the bitcoin codebase. I would like to translate some c++ code into python but struggle really hard to get the hashes right.
This is the function I try go get. Hash is doing a double sha256
uint256 CalcKernelHash(const uint256& stakeModifier, int64_t height, int64_t coinstakeTime, const uint256& masternodeID, const Consensus::Params& params) {
        // Calculate hash
        CDataStream ss(SER_GETHASH, 0);
        ss << stakeModifier << coinstakeTime << GetMnCollateralAmount(int(height)) << masternodeID;
        std::cout << "\n--------------------------------\n";
        std::cout << "DEBUG[" << coinstakeTime << "] collateral: " << GetMnCollateralAmount(int(height)) << "\n";
        std::cout << "DEBUG[" << coinstakeTime << "] ss: " << stakeModifier.ToString() << coinstakeTime << GetMnCollateralAmount(int(height)) << masternodeID.ToString() << "\n";
        std::cout << "DEBUG[" << coinstakeTime << "] HASH: " << Hash(ss.begin(), ss.end()).ToString() << "\n";
        return Hash(ss.begin(), ss.end());
    }

And here it is called
const auto hashProofOfStake = UintToArith256(CalcKernelHash(stakeModifier, height, coinstakeTime, masternodeID, params));
    
std::cout << "DEBUG[" << coinstakeTime << "] coinstakeTime: " << coinstakeTime << "\n";
std::cout << "DEBUG[" << coinstakeTime << "] nbits: " << nBits << "\n";
std::cout << "DEBUG[" << coinstakeTime << "] hashProofOfStake: " << hashProofOfStake.ToString() << "\n";
std::cout << "DEBUG[" << coinstakeTime << "] stakeModifier: " << stakeModifier.ToString() << "\n";
std::cout << "DEBUG[" << coinstakeTime << "] masternodeId: " << masternodeID.ToString() << "\n";

I have added some debug statement to check the output and have a reference
--------------------------------
DEBUG[1622528110] collateral: 2000000000000
DEBUG[1622528110] ss: f541f372005beeb8952f964ea638527341c3b3a6eebb0c0309e4f092bd6c1b0e16225281102000000000000e2cfe3f576da4999ea2268224f34efdd3b0d567841d561b41b3ab67976c977d1
DEBUG[1622528110] HASH: e4b0d1b01a99d00e44d41eeb37c81d19940bf71270994fff53ea5caa1c79f663
DEBUG[1622528110] coinstakeTime: 1622528110
DEBUG[1622528110] nbits: 419654358
DEBUG[1622528110] hashProofOfStake: e4b0d1b01a99d00e44d41eeb37c81d19940bf71270994fff53ea5caa1c79f663
DEBUG[1622528110] stakeModifier: f541f372005beeb8952f964ea638527341c3b3a6eebb0c0309e4f092bd6c1b0e
DEBUG[1622528110] masternodeId: e2cfe3f576da4999ea2268224f34efdd3b0d567841d561b41b3ab67976c977d1

This is my trial in python
import binascii
import hashlib

def rotate(x):
    s2 = bytearray.fromhex(x)
    s2.reverse()
    return binascii.hexlify(s2).decode()

def padhexa(s):
    return s[2:].zfill(16)

def CalcKernelHash(stakeModifier, coinstakeTime, masternodeId):
    stakeModifier = binascii.unhexlify(stakeModifier)
    coinstakeTime = binascii.unhexlify("%x" % coinstakeTime)
    collateral = binascii.unhexlify("0%x" % 2000000000000)
    masternodeId = binascii.unhexlify(masternodeId)
    
    bin_s = stakeModifier[::-1] + coinstakeTime[::-1] + collateral[::-1] + masternodeId[::1]
    print(bin_s)
    return rotate(hashlib.sha256(hashlib.sha256(bin_s).digest()).hexdigest())



CalcKernelHash('f541f372005beeb8952f964ea638527341c3b3a6eebb0c0309e4f092bd6c1b0e', 1622528110, 'e2cfe3f576da4999ea2268224f34efdd3b0d567841d561b41b3ab67976c977d1')

When I execute with the parameter from the debug output I get
CalcKernelHash('f541f372005beeb8952f964ea638527341c3b3a6eebb0c0309e4f092bd6c1b0e', 1622528110, 'e2cfe3f576da4999ea2268224f34efdd3b0d567841d561b41b3ab67976c977d1')

'f01ba34ac7a897a1e37d61bac3a8ea6f0126c2ae883a2d75dab1f407d40aad0b'

Where I should get hashProofOfStake e4b0d1b01a99d00e44d41eeb37c81d19940bf71270994fff53ea5caa1c79f663
EDIT:
bin_s returns b'\x0e\x1bl\xbd\x92\xf0\xe4\t\x03\x0c\xbb\xee\xa6\xb3\xc3AsR8\xa6N\x96/\x95\xb8\xee[\x00r\xf3A\xf5n\xd0\xb5`\x00 J\xa9\xd1\x01\xe2\xcf\xe3\xf5v\xdaI\x99\xea"h"O4\xef\xdd;\rVxA\xd5a\xb4\x1b:\xb6yv\xc9w\xd1'
EDIT2:
Hash definition
/** Compute the 256-bit hash of an object. */
template<typename T1>
inline uint256 Hash(const T1 pbegin, const T1 pend)
{
    static const unsigned char pblank[1] = {};
    uint256 result;
    CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
              .Finalize((unsigned char*)&result);
    return result;
}


user38618: def CalcKernelHash(stakeModifier, coinstakeTime, masternodeId):
    stakeModifier = binascii.unhexlify(stakeModifier)
    coinstakeTime = binascii.unhexlify(hex(coinstakeTime)[2:])
    collateral = binascii.unhexlify("0000000000204AA9D1010000")
    masternodeId = binascii.unhexlify(masternodeId)
    
    bin_s = stakeModifier[::-1] + coinstakeTime[::-1] + collateral[::1] + masternodeId[::-1]
    print(binascii.hexlify(bin_s))
    return rotate(hashlib.sha256(hashlib.sha256(bin_s).digest()).hexdigest())


121910: <p>This is not related to bitcoin but it uses most of the bitcoin codebase. I would like to translate some c++ code into python but struggle really hard to get the hashes right.</p>
<p>This is the function I try go get. Hash is doing a double sha256</p>
<pre><code>uint256 CalcKernelHash(const uint256&amp; stakeModifier, int64_t height, int64_t coinstakeTime, const uint256&amp; masternodeID, const Consensus::Params&amp; params) {
        // Calculate hash
        CDataStream ss(SER_GETHASH, 0);
        ss &lt;&lt; stakeModifier &lt;&lt; coinstakeTime &lt;&lt; GetMnCollateralAmount(int(height)) &lt;&lt; masternodeID;
        std::cout &lt;&lt; &quot;\n--------------------------------\n&quot;;
        std::cout &lt;&lt; &quot;DEBUG[&quot; &lt;&lt; coinstakeTime &lt;&lt; &quot;] collateral: &quot; &lt;&lt; GetMnCollateralAmount(int(height)) &lt;&lt; &quot;\n&quot;;
        std::cout &lt;&lt; &quot;DEBUG[&quot; &lt;&lt; coinstakeTime &lt;&lt; &quot;] ss: &quot; &lt;&lt; stakeModifier.ToString() &lt;&lt; coinstakeTime &lt;&lt; GetMnCollateralAmount(int(height)) &lt;&lt; masternodeID.ToString() &lt;&lt; &quot;\n&quot;;
        std::cout &lt;&lt; &quot;DEBUG[&quot; &lt;&lt; coinstakeTime &lt;&lt; &quot;] HASH: &quot; &lt;&lt; Hash(ss.begin(), ss.end()).ToString() &lt;&lt; &quot;\n&quot;;
        return Hash(ss.begin(), ss.end());
    }
</code></pre>
<p>And here it is called</p>
<pre><code>const auto hashProofOfStake = UintToArith256(CalcKernelHash(stakeModifier, height, coinstakeTime, masternodeID, params));
    
std::cout &lt;&lt; &quot;DEBUG[&quot; &lt;&lt; coinstakeTime &lt;&lt; &quot;] coinstakeTime: &quot; &lt;&lt; coinstakeTime &lt;&lt; &quot;\n&quot;;
std::cout &lt;&lt; &quot;DEBUG[&quot; &lt;&lt; coinstakeTime &lt;&lt; &quot;] nbits: &quot; &lt;&lt; nBits &lt;&lt; &quot;\n&quot;;
std::cout &lt;&lt; &quot;DEBUG[&quot; &lt;&lt; coinstakeTime &lt;&lt; &quot;] hashProofOfStake: &quot; &lt;&lt; hashProofOfStake.ToString() &lt;&lt; &quot;\n&quot;;
std::cout &lt;&lt; &quot;DEBUG[&quot; &lt;&lt; coinstakeTime &lt;&lt; &quot;] stakeModifier: &quot; &lt;&lt; stakeModifier.ToString() &lt;&lt; &quot;\n&quot;;
std::cout &lt;&lt; &quot;DEBUG[&quot; &lt;&lt; coinstakeTime &lt;&lt; &quot;] masternodeId: &quot; &lt;&lt; masternodeID.ToString() &lt;&lt; &quot;\n&quot;;
</code></pre>
<p>I have added some debug statement to check the output and have a reference</p>
<pre><code>--------------------------------
DEBUG[1622528110] collateral: 2000000000000
DEBUG[1622528110] ss: f541f372005beeb8952f964ea638527341c3b3a6eebb0c0309e4f092bd6c1b0e16225281102000000000000e2cfe3f576da4999ea2268224f34efdd3b0d567841d561b41b3ab67976c977d1
DEBUG[1622528110] HASH: e4b0d1b01a99d00e44d41eeb37c81d19940bf71270994fff53ea5caa1c79f663
DEBUG[1622528110] coinstakeTime: 1622528110
DEBUG[1622528110] nbits: 419654358
DEBUG[1622528110] hashProofOfStake: e4b0d1b01a99d00e44d41eeb37c81d19940bf71270994fff53ea5caa1c79f663
DEBUG[1622528110] stakeModifier: f541f372005beeb8952f964ea638527341c3b3a6eebb0c0309e4f092bd6c1b0e
DEBUG[1622528110] masternodeId: e2cfe3f576da4999ea2268224f34efdd3b0d567841d561b41b3ab67976c977d1
</code></pre>
<p>This is my trial in python</p>
<pre><code>import binascii
import hashlib

def rotate(x):
    s2 = bytearray.fromhex(x)
    s2.reverse()
    return binascii.hexlify(s2).decode()

def padhexa(s):
    return s[2:].zfill(16)

def CalcKernelHash(stakeModifier, coinstakeTime, masternodeId):
    stakeModifier = binascii.unhexlify(stakeModifier)
    coinstakeTime = binascii.unhexlify(&quot;%x&quot; % coinstakeTime)
    collateral = binascii.unhexlify(&quot;0%x&quot; % 2000000000000)
    masternodeId = binascii.unhexlify(masternodeId)
    
    bin_s = stakeModifier[::-1] + coinstakeTime[::-1] + collateral[::-1] + masternodeId[::1]
    print(bin_s)
    return rotate(hashlib.sha256(hashlib.sha256(bin_s).digest()).hexdigest())



CalcKernelHash('f541f372005beeb8952f964ea638527341c3b3a6eebb0c0309e4f092bd6c1b0e', 1622528110, 'e2cfe3f576da4999ea2268224f34efdd3b0d567841d561b41b3ab67976c977d1')
</code></pre>
<p>When I execute with the parameter from the debug output I get</p>
<pre><code>CalcKernelHash('f541f372005beeb8952f964ea638527341c3b3a6eebb0c0309e4f092bd6c1b0e', 1622528110, 'e2cfe3f576da4999ea2268224f34efdd3b0d567841d561b41b3ab67976c977d1')

'f01ba34ac7a897a1e37d61bac3a8ea6f0126c2ae883a2d75dab1f407d40aad0b'
</code></pre>
<p>Where I should get <code>hashProofOfStake e4b0d1b01a99d00e44d41eeb37c81d19940bf71270994fff53ea5caa1c79f663</code></p>
<p>EDIT:</p>
<p>bin_s returns <code>b'\x0e\x1bl\xbd\x92\xf0\xe4\t\x03\x0c\xbb\xee\xa6\xb3\xc3AsR8\xa6N\x96/\x95\xb8\xee[\x00r\xf3A\xf5n\xd0\xb5`\x00 J\xa9\xd1\x01\xe2\xcf\xe3\xf5v\xdaI\x99\xea&quot;h&quot;O4\xef\xdd;\rVxA\xd5a\xb4\x1b:\xb6yv\xc9w\xd1'</code></p>
<p>EDIT2:</p>
<p>Hash definition</p>
<pre><code>/** Compute the 256-bit hash of an object. */
template&lt;typename T1&gt;
inline uint256 Hash(const T1 pbegin, const T1 pend)
{
    static const unsigned char pblank[1] = {};
    uint256 result;
    CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&amp;pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
              .Finalize((unsigned char*)&amp;result);
    return result;
}
</code></pre>
 38618: <pre><code>def CalcKernelHash(stakeModifier, coinstakeTime, masternodeId):
    stakeModifier = binascii.unhexlify(stakeModifier)
    coinstakeTime = binascii.unhexlify(hex(coinstakeTime)[2:])
    collateral = binascii.unhexlify(&quot;0000000000204AA9D1010000&quot;)
    masternodeId = binascii.unhexlify(masternodeId)
    
    bin_s = stakeModifier[::-1] + coinstakeTime[::-1] + collateral[::1] + masternodeId[::-1]
    print(binascii.hexlify(bin_s))
    return rotate(hashlib.sha256(hashlib.sha256(bin_s).digest()).hexdigest())
</code></pre>
