From 8c3a9bc7ae619d98ef5fe2279e5435d9a4ee8fe6 Mon Sep 17 00:00:00 2001 From: miketout Date: Fri, 12 Oct 2018 01:12:08 -0700 Subject: [PATCH] Clean up POS checks --- src/cheatcatcher.cpp | 6 ++---- src/cheatcatcher.h | 8 ++++++++ src/komodo_bitcoind.h | 41 ++++++++++++----------------------------- src/main.cpp | 2 +- 4 files changed, 23 insertions(+), 34 deletions(-) diff --git a/src/cheatcatcher.cpp b/src/cheatcatcher.cpp index 4008c34a2..27c5e4746 100644 --- a/src/cheatcatcher.cpp +++ b/src/cheatcatcher.cpp @@ -65,14 +65,13 @@ bool CCheatList::IsCheatInList(const CTransaction &tx, CTransaction *cheatTx) range = indexedCheatCandidates.equal_range(utxo); for (auto it = range.first; it != range.second; it++) { - // we assume height is valid, as we should have pruned the list before checking. since the tx came out of a valid block, - // what matters is if the prior hash matches CTransaction &cTx = it->second->tx; + printf("cTx::opret : %s\n", cTx.vout[1].scriptPubKey.ToString().c_str()); // need both parameters to check if (GetStakeParams(cTx, s)) { - if (p.prevHash != s.prevHash) + if (p.prevHash != s.prevHash && s.blkHeight >= p.blkHeight) { cheatTx = &cTx; return true; @@ -85,7 +84,6 @@ bool CCheatList::IsCheatInList(const CTransaction &tx, CTransaction *cheatTx) bool CCheatList::Add(CTxHolder &txh) { - CVerusHashWriter hw = CVerusHashWriter(SER_GETHASH, PROTOCOL_VERSION); if (NetworkUpgradeActive(txh.height, Params().GetConsensus(), Consensus::UPGRADE_SAPLING)) { LOCK(cs_cheat); diff --git a/src/cheatcatcher.h b/src/cheatcatcher.h index 39a3a8112..cff210c85 100644 --- a/src/cheatcatcher.h +++ b/src/cheatcatcher.h @@ -15,6 +15,7 @@ #include #include "streams.h" +#include "primitives/transaction.h" #include "script/script.h" #include "uint256.h" @@ -34,6 +35,13 @@ class CTxHolder hw << tx.vin[0].prevout.n; utxo = hw.GetHash(); } + + CTxHolder& operator=(const CTxHolder& txh) + { + utxo = txh.utxo; + height = txh.height; + tx = txh.tx; + } }; diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index 4943267e7..fb767da57 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -1492,31 +1492,13 @@ bool verusCheckPOSBlock(int32_t slowflag, CBlock *pblock, int32_t height) value = pblock->vtx[txn_count-1].vout[0].nValue; { - bool validHash = true; + bool validHash = (value != 0); bool enablePOSNonce = CPOSNonce::NewPOSActive(height); bool newPOSEnforcement = enablePOSNonce && (Params().GetConsensus().vUpgrades[Consensus::UPGRADE_SAPLING].nActivationHeight <= height); uint256 rawHash; arith_uint256 posHash; - // check without consequences if not enforced yet - { - if (pblock->GetRawVerusPOSHash(rawHash, height)) - { - if (posHash > target) - { - posHash = UintToArith256(rawHash) / value; - printf("PoS block\nblkHash: %s\nnNonce: %s\nrawHash: %s\nposHash: %s\nvalue: %lu\n", - pblock->GetHash().GetHex().c_str(), pblock->nNonce.GetHex().c_str(), rawHash.GetHex().c_str(), posHash.GetHex().c_str(), value); - } - } - else - { - printf("PoS block\nblkHash: %s\nnNonce: %s\nFAILED TO GET posHash\n", - pblock->GetHash().GetHex().c_str(), pblock->nNonce.GetHex().c_str()); - } - } - - if (newPOSEnforcement) + if (validHash && newPOSEnforcement) { validHash = pblock->GetRawVerusPOSHash(rawHash, height); posHash = UintToArith256(rawHash) / value; @@ -1526,25 +1508,26 @@ bool verusCheckPOSBlock(int32_t slowflag, CBlock *pblock, int32_t height) printf("ERROR: invalid nonce value for PoS block\nnNonce: %s\nrawHash: %s\nposHash: %s\nvalue: %lu\n", pblock->nNonce.GetHex().c_str(), rawHash.GetHex().c_str(), posHash.GetHex().c_str(), value); } - for (int i = 0; validHash && i < pblock->vtx[0].vout.size(); i++) + // make sure prev block hash and block height are correct + CStakeParams p; + if (validHash && (validHash = GetStakeParams(pblock->vtx[txn_count-1], p))) { - if (!ValidateMatchingStake(pblock->vtx[0], i, pblock->vtx[txn_count-1], validHash)) + for (int i = 0; validHash && i < pblock->vtx[0].vout.size(); i++) { validHash = false; - } - else - { - // make sure prev block hash and block height are correct - CStakeParams p; - if (validHash = GetStakeParams(pblock->vtx[txn_count-1], p)) + if (ValidateMatchingStake(pblock->vtx[0], i, pblock->vtx[txn_count-1], validHash) && !validHash) { - if (p.prevHash != pblock->hashPrevBlock || p.blkHeight != height) + if (p.prevHash == pblock->hashPrevBlock && p.blkHeight == height) + { + validHash = true; + } { printf("ERROR: invalid block data for stake tx\nblkHash: %s\ntxBlkHash: %s\nblkHeight: %d\ntxBlkHeight: %d\n", pblock->hashPrevBlock.GetHex().c_str(), p.prevHash.GetHex().c_str(), height, p.blkHeight); validHash = false; } } + else validHash = false; } } } diff --git a/src/main.cpp b/src/main.cpp index de810d871..d8845319f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3724,7 +3724,7 @@ bool static DisconnectTip(CValidationState &state, bool fBare = false) { ASSETCHAINS_LWMAPOS && (i == (block.vtx.size() - 1)) && (block.IsVerusPOSBlock())) { - CTxHolder txh(block.vtx[i], pindexDelete->GetHeight()); + CTxHolder txh = CTxHolder(block.vtx[i], pindexDelete->GetHeight()); cheatList.Add(txh); } }