From 17d0160a17f0e45f211fb2d0ddf4f2a97d296ab4 Mon Sep 17 00:00:00 2001 From: Michael Toutonghi Date: Fri, 20 Jul 2018 19:57:46 -0700 Subject: [PATCH] Cleanup nonce changes --- src/Makefile.am | 2 ++ src/crypto/verus_hash.h | 3 ++- src/hash.h | 2 +- src/komodo_bitcoind.h | 5 +++++ src/miner.cpp | 2 +- src/primitives/block.h | 19 +++++++++++-------- src/primitives/nonce.cpp | 21 ++++++++++++++------- src/primitives/nonce.h | 21 ++++++++++----------- src/wallet/wallet.cpp | 7 +++++-- src/wallet/wallet.h | 4 ++-- 10 files changed, 53 insertions(+), 33 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index a8d7b1bbc..e22e37137 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -271,6 +271,7 @@ libbitcoin_server_a_SOURCES = \ crypto/haraka.h \ crypto/haraka_portable.h \ crypto/verus_hash.h \ + crypto/verus_hash.cpp \ deprecation.cpp \ httprpc.cpp \ httpserver.cpp \ @@ -410,6 +411,7 @@ libbitcoin_common_a_SOURCES = \ crypto/haraka.h \ crypto/haraka_portable.h \ crypto/verus_hash.h \ + crypto/verus_hash.cpp \ hash.cpp \ key.cpp \ keystore.cpp \ diff --git a/src/crypto/verus_hash.h b/src/crypto/verus_hash.h index 33fc0ce5d..07c76d0f5 100644 --- a/src/crypto/verus_hash.h +++ b/src/crypto/verus_hash.h @@ -27,7 +27,7 @@ class CVerusHash static void init(); - CVerusHash() {} + CVerusHash() { } CVerusHash &Write(const unsigned char *data, size_t len); @@ -37,6 +37,7 @@ class CVerusHash result = buf2; curPos = 0; std::fill(buf1, buf1 + sizeof(buf1), 0); + return *this; } int64_t *ExtraI64Ptr() { return (int64_t *)(curBuf + 32); } diff --git a/src/hash.h b/src/hash.h index 1a129b720..537ba3dcb 100644 --- a/src/hash.h +++ b/src/hash.h @@ -203,7 +203,7 @@ public: int nType; int nVersion; - CVerusHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn), state() {} + CVerusHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn), state() { } CVerusHashWriter& write(const char *pch, size_t size) { state.Write((const unsigned char*)pch, size); diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index 87c4875fb..7af71828d 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -1271,7 +1271,12 @@ bool verusCheckPOSBlock(int32_t slowflag, CBlock *pblock, int32_t height) CTransaction tx; if (!pblock->IsVerusPOSBlock()) + { + printf("%s, height %d not POS block\n", pblock->nNonce.GetHex().c_str(), height); + pblock->nNonce.SetPOSTarget(pblock->nNonce.GetPOSTarget()); + printf("%s after setting POS target\n", pblock->nNonce.GetHex().c_str()); return false; + } char voutaddr[64], destaddr[64], cbaddr[64]; diff --git a/src/miner.cpp b/src/miner.cpp index 2f71e0564..86235aa4b 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -462,9 +462,9 @@ CBlockTemplate* CreateNewBlock(const CScript& _scriptPubKeyIn, bool isStake) { uint32_t nBitsPOS; arith_uint256 posHash; + siglen = verus_staked(pblock, key, txStaked, nBitsPOS, posHash, utxosig); blocktime = GetAdjustedTime(); - pblock->SetVerusPOSTarget(nBitsPOS); // change the scriptPubKeyIn to the same output script exactly as the staking transaction if (siglen > 0) diff --git a/src/primitives/block.h b/src/primitives/block.h index 06e26f4c5..12bffbeab 100644 --- a/src/primitives/block.h +++ b/src/primitives/block.h @@ -95,7 +95,7 @@ public: return (int64_t)nTime; } - int32_t GetVerusPOSTarget() const + uint32_t GetVerusPOSTarget() const { uint32_t nBits = 0; @@ -112,19 +112,22 @@ public: return nNonce.IsPOSNonce(); } - void SetVerusPOSTarget(int32_t nBits) + void SetVerusPOSTarget(uint32_t nBits) { CVerusHashWriter hashWriter = CVerusHashWriter(SER_GETHASH, PROTOCOL_VERSION); - uint256 hash; - arith_uint256 tmpNonce; arith_uint256 arNonce = UintToArith256(nNonce); - arNonce = ((arNonce >> 32) << 32) | nBits; - tmpNonce = ((arNonce << 128) >> 128); - hashWriter << ArithToUint256(tmpNonce); + // printf("before svpt: %s\n", ArithToUint256(arNonce).GetHex().c_str()); - nNonce = ArithToUint256(UintToArith256(hashWriter.GetHash()) << 128 | tmpNonce); + arNonce = (arNonce & CPOSNonce::entropyMask) | nBits; + + // printf("after clear: %s\n", ArithToUint256(arNonce).GetHex().c_str()); + + hashWriter << ArithToUint256(arNonce); + nNonce = CPOSNonce(ArithToUint256(UintToArith256(hashWriter.GetHash()) << 128 | arNonce)); + + // printf(" after svpt: %s\n", nNonce.GetHex().c_str()); } }; diff --git a/src/primitives/nonce.cpp b/src/primitives/nonce.cpp index e7ad618ee..58970a80f 100644 --- a/src/primitives/nonce.cpp +++ b/src/primitives/nonce.cpp @@ -8,6 +8,9 @@ extern char ASSETCHAINS_SYMBOL[65]; +arith_uint256 CPOSNonce::entropyMask = UintToArith256(uint256S("00000000000000000000000000000000ffffffffffffffffffffffff00000000")); +arith_uint256 CPOSNonce::posDiffMask = UintToArith256(uint256S("00000000000000000000000000000000000000000000000000000000ffffffff")); + bool CPOSNonce::NewPOSActive(int32_t height) { if ((strcmp(ASSETCHAINS_SYMBOL, "VRSC") == 0) && (height < (96480 + 100))) @@ -38,13 +41,17 @@ void CPOSNonce::SetPOSEntropy(const uint256 &pastHash, uint256 txid, int32_t vou hashWriter << txid; hashWriter << voutNum; - arith_uint256 arNonce = (UintToArith256(*this) & 0xffffffff) | - ((UintToArith256(hashWriter.GetHash()) & UintToArith256(uint256S("0000000000000000000000000000000000000000ffffffffffffffffffffffff"))) << 32); + arith_uint256 arNonce = (UintToArith256(*this) & posDiffMask) | + (UintToArith256(hashWriter.GetHash()) & entropyMask); - hashWriter = CVerusHashWriter(SER_GETHASH, PROTOCOL_VERSION); - hashWriter << ArithToUint256(arNonce); + // printf("before %s\n", ArithToUint256(arNonce).GetHex().c_str()); - *this = CPOSNonce(ArithToUint256(UintToArith256(hashWriter.GetHash()) << 128 | arNonce)); + CVerusHashWriter newWriter = CVerusHashWriter(SER_GETHASH, PROTOCOL_VERSION); + newWriter << ArithToUint256(arNonce); + + *this = CPOSNonce(ArithToUint256((UintToArith256(newWriter.GetHash()) << 128) | arNonce)); + + // printf("after %s\n", this->GetHex().c_str()); } bool CPOSNonce::CheckPOSEntropy(const uint256 &pastHash, uint256 txid, int32_t voutNum) @@ -57,8 +64,8 @@ bool CPOSNonce::CheckPOSEntropy(const uint256 &pastHash, uint256 txid, int32_t v hashWriter << txid; hashWriter << voutNum; - arith_uint256 arNonce = (UintToArith256(*this) & 0xffffffff) | - ((UintToArith256(hashWriter.GetHash()) & UintToArith256(uint256S("0000000000000000000000000000000000000000ffffffffffffffffffffffff"))) << 32); + arith_uint256 arNonce = (UintToArith256(*this) & posDiffMask) | + (UintToArith256(hashWriter.GetHash()) & entropyMask); hashWriter = CVerusHashWriter(SER_GETHASH, PROTOCOL_VERSION); hashWriter << ArithToUint256(arNonce); diff --git a/src/primitives/nonce.h b/src/primitives/nonce.h index 97863e528..a3da4b6b5 100644 --- a/src/primitives/nonce.h +++ b/src/primitives/nonce.h @@ -19,9 +19,12 @@ public: static bool NewPOSActive(int32_t height); static bool NewNonceActive(int32_t height); - CPOSNonce() {} - CPOSNonce(const base_blob<256> &b) : uint256(b) {} - CPOSNonce(const std::vector &vch) : uint256(vch) {} + static arith_uint256 entropyMask; + static arith_uint256 posDiffMask; + + CPOSNonce() : uint256() { } + CPOSNonce(const base_blob<256> &b) : uint256(b) { } + CPOSNonce(const std::vector &vch) : uint256(vch) { } int32_t GetPOSTarget() const { @@ -44,18 +47,14 @@ public: return (*this == ArithToUint256(UintToArith256(hashWriter.GetHash()) << 128 | tmpNonce)); } - void SetPOSTarget(int32_t nBits) + void SetPOSTarget(uint32_t nBits) { CVerusHashWriter hashWriter = CVerusHashWriter(SER_GETHASH, PROTOCOL_VERSION); - arith_uint256 tmpNonce; - arith_uint256 arNonce = UintToArith256(*this); - arNonce = ((arNonce >> 32) << 32) | nBits; + arith_uint256 arNonce = (UintToArith256(*this) & entropyMask) | nBits; + hashWriter << ArithToUint256(arNonce); - tmpNonce = ((arNonce << 128) >> 128); - hashWriter << ArithToUint256(tmpNonce); - - (uint256 &)(*this) = ArithToUint256(UintToArith256(hashWriter.GetHash()) << 128 | tmpNonce); + (uint256 &)(*this) = ArithToUint256(UintToArith256(hashWriter.GetHash()) << 128 | arNonce); } void SetPOSEntropy(const uint256 &pastHash, uint256 txid, int32_t voutNum); diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 275da8258..8c6b1eb50 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -998,7 +998,7 @@ CWallet::TxItems CWallet::OrderedTxItems(std::list& acentries, // UTXO with the smallest coin age if there is more than one, as larger coin age will win more often and is worth saving // each attempt consists of taking a VerusHash of the following values: // ASSETCHAINS_MAGIC, nHeight, txid, voutNum -bool CWallet::VerusSelectStakeOutput(CBlock *pBlock, arith_uint256 &hashResult, CTransaction &stakeSource, int32_t &voutNum, int32_t nHeight, uint32_t bnTarget) const +bool CWallet::VerusSelectStakeOutput(CBlock *pBlock, arith_uint256 &hashResult, CTransaction &stakeSource, int32_t &voutNum, int32_t nHeight, uint32_t &bnTarget) const { arith_uint256 target; arith_uint256 curHash; @@ -1017,6 +1017,7 @@ bool CWallet::VerusSelectStakeOutput(CBlock *pBlock, arith_uint256 &hashResult, { CBlockHeader bh = pastBlockIndex->GetBlockHeader(); uint256 pastHash = bh.GetVerusEntropyHash(nHeight); + CPOSNonce curNonce; BOOST_FOREACH(COutput &txout, vecOutputs) { @@ -1026,19 +1027,21 @@ bool CWallet::VerusSelectStakeOutput(CBlock *pBlock, arith_uint256 &hashResult, if (Solver(txout.tx->vout[txout.i].scriptPubKey, whichType, vSolutions) && (whichType == TX_PUBKEY || whichType == TX_PUBKEYHASH) && (!pwinner || pwinner->tx->vout[pwinner->i].nValue > txout.tx->vout[txout.i].nValue)) pwinner = &txout; + curNonce = pBlock->nNonce; } } if (pwinner) { stakeSource = *(pwinner->tx); voutNum = pwinner->i; + pBlock->nNonce = curNonce; return true; } } return false; } -int32_t CWallet::VerusStakeTransaction(CBlock *pBlock, CMutableTransaction &txNew, uint32_t bnTarget, arith_uint256 &hashResult, uint8_t *utxosig) const +int32_t CWallet::VerusStakeTransaction(CBlock *pBlock, CMutableTransaction &txNew, uint32_t &bnTarget, arith_uint256 &hashResult, uint8_t *utxosig) const { CTransaction stakeSource; int32_t voutNum, siglen = 0; diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 8568bf9c2..d42d3343c 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -1149,8 +1149,8 @@ public: bool ignoreUnspendable=true); // staking functions - bool VerusSelectStakeOutput(CBlock *pBlock, arith_uint256 &hashResult, CTransaction &stakeSource, int32_t &voutNum, int32_t nHeight, uint32_t bnTarget) const; - int32_t VerusStakeTransaction(CBlock *pBlock, CMutableTransaction &txNew, uint32_t bnTarget, arith_uint256 &hashResult, uint8_t *utxosig) const; + bool VerusSelectStakeOutput(CBlock *pBlock, arith_uint256 &hashResult, CTransaction &stakeSource, int32_t &voutNum, int32_t nHeight, uint32_t &bnTarget) const; + int32_t VerusStakeTransaction(CBlock *pBlock, CMutableTransaction &txNew, uint32_t &bnTarget, arith_uint256 &hashResult, uint8_t *utxosig) const; }; /** A key allocated from the key pool. */