Clean up POS checks

This commit is contained in:
miketout
2018-10-12 01:12:08 -07:00
parent a8d2188651
commit 8c3a9bc7ae
4 changed files with 23 additions and 34 deletions

View File

@@ -65,14 +65,13 @@ bool CCheatList::IsCheatInList(const CTransaction &tx, CTransaction *cheatTx)
range = indexedCheatCandidates.equal_range(utxo); range = indexedCheatCandidates.equal_range(utxo);
for (auto it = range.first; it != range.second; it++) 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; CTransaction &cTx = it->second->tx;
printf("cTx::opret : %s\n", cTx.vout[1].scriptPubKey.ToString().c_str());
// need both parameters to check // need both parameters to check
if (GetStakeParams(cTx, s)) if (GetStakeParams(cTx, s))
{ {
if (p.prevHash != s.prevHash) if (p.prevHash != s.prevHash && s.blkHeight >= p.blkHeight)
{ {
cheatTx = &cTx; cheatTx = &cTx;
return true; return true;
@@ -85,7 +84,6 @@ bool CCheatList::IsCheatInList(const CTransaction &tx, CTransaction *cheatTx)
bool CCheatList::Add(CTxHolder &txh) bool CCheatList::Add(CTxHolder &txh)
{ {
CVerusHashWriter hw = CVerusHashWriter(SER_GETHASH, PROTOCOL_VERSION);
if (NetworkUpgradeActive(txh.height, Params().GetConsensus(), Consensus::UPGRADE_SAPLING)) if (NetworkUpgradeActive(txh.height, Params().GetConsensus(), Consensus::UPGRADE_SAPLING))
{ {
LOCK(cs_cheat); LOCK(cs_cheat);

View File

@@ -15,6 +15,7 @@
#include <vector> #include <vector>
#include "streams.h" #include "streams.h"
#include "primitives/transaction.h"
#include "script/script.h" #include "script/script.h"
#include "uint256.h" #include "uint256.h"
@@ -34,6 +35,13 @@ class CTxHolder
hw << tx.vin[0].prevout.n; hw << tx.vin[0].prevout.n;
utxo = hw.GetHash(); utxo = hw.GetHash();
} }
CTxHolder& operator=(const CTxHolder& txh)
{
utxo = txh.utxo;
height = txh.height;
tx = txh.tx;
}
}; };

View File

@@ -1492,31 +1492,13 @@ bool verusCheckPOSBlock(int32_t slowflag, CBlock *pblock, int32_t height)
value = pblock->vtx[txn_count-1].vout[0].nValue; value = pblock->vtx[txn_count-1].vout[0].nValue;
{ {
bool validHash = true; bool validHash = (value != 0);
bool enablePOSNonce = CPOSNonce::NewPOSActive(height); bool enablePOSNonce = CPOSNonce::NewPOSActive(height);
bool newPOSEnforcement = enablePOSNonce && (Params().GetConsensus().vUpgrades[Consensus::UPGRADE_SAPLING].nActivationHeight <= height); bool newPOSEnforcement = enablePOSNonce && (Params().GetConsensus().vUpgrades[Consensus::UPGRADE_SAPLING].nActivationHeight <= height);
uint256 rawHash; uint256 rawHash;
arith_uint256 posHash; arith_uint256 posHash;
// check without consequences if not enforced yet if (validHash && newPOSEnforcement)
{
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)
{ {
validHash = pblock->GetRawVerusPOSHash(rawHash, height); validHash = pblock->GetRawVerusPOSHash(rawHash, height);
posHash = UintToArith256(rawHash) / value; 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", 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); 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; validHash = false;
} if (ValidateMatchingStake(pblock->vtx[0], i, pblock->vtx[txn_count-1], validHash) && !validHash)
else
{
// make sure prev block hash and block height are correct
CStakeParams p;
if (validHash = GetStakeParams(pblock->vtx[txn_count-1], p))
{ {
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", 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); pblock->hashPrevBlock.GetHex().c_str(), p.prevHash.GetHex().c_str(), height, p.blkHeight);
validHash = false; validHash = false;
} }
} }
else validHash = false;
} }
} }
} }

View File

@@ -3724,7 +3724,7 @@ bool static DisconnectTip(CValidationState &state, bool fBare = false) {
ASSETCHAINS_LWMAPOS && (i == (block.vtx.size() - 1)) && ASSETCHAINS_LWMAPOS && (i == (block.vtx.size() - 1)) &&
(block.IsVerusPOSBlock())) (block.IsVerusPOSBlock()))
{ {
CTxHolder txh(block.vtx[i], pindexDelete->GetHeight()); CTxHolder txh = CTxHolder(block.vtx[i], pindexDelete->GetHeight());
cheatList.Add(txh); cheatList.Add(txh);
} }
} }