Skip to content

Commit

Permalink
[Consensus] Fix fake-stake spent serials detection on forked chains
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed Dec 19, 2019
1 parent d2c1ef2 commit 0417f66
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
46 changes: 21 additions & 25 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4908,14 +4908,14 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex,
}
}
if(tx.IsCoinStake()) continue;
if(hasPIVInputs)
if(hasPIVInputs) {
// Check if coinstake input is double spent inside the same block
for (const CTxIn& pivIn : pivInputs){
if(pivIn.prevout == in.prevout){
for (const CTxIn& pivIn : pivInputs)
if(pivIn.prevout == in.prevout)
// double spent coinstake input inside block
return error("%s: double spent coinstake input inside block", __func__);
}
}
}

}
}
inBlockSerials.clear();
Expand Down Expand Up @@ -4944,25 +4944,21 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex,
return error("%s: forked chain longer than maximum reorg limit", __func__);
}

// Loop through every input from said block
for (const CTransaction &t : bl.vtx) {
for (const CTxIn &in: t.vin) {
// Loop through every input of the staking tx
for (const CTxIn &stakeIn : pivInputs) {
// if it's already spent

// First regular staking check
if (hasPIVInputs) {
if (stakeIn.prevout == in.prevout) {
return state.DoS(100, error("%s: input already spent on a previous block",
__func__));
}
// Loop through every tx of this block
for (const CTransaction& t : bl.vtx) {
// Loop through every input of this tx
for (const CTxIn& in: t.vin) {
// If this input is a zerocoin spend, and the coinstake has zerocoin inputs
// then store the serials for later check
if(hasZPIVInputs && in.IsZerocoinSpend())
vBlockSerials.push_back(TxInToZerocoinSpend(in).getCoinSerialNumber());

// Second, if there is zPoS staking then store the serials for later check
if(in.IsZerocoinSpend()){
vBlockSerials.push_back(TxInToZerocoinSpend(in).getCoinSerialNumber());
}
}
// Loop through every input of the staking tx
if (hasPIVInputs) {
for (const CTxIn& stakeIn : pivInputs)
// check if the tx input is double spending any coinstake input
if (stakeIn.prevout == in.prevout)
return state.DoS(100, error("%s: input already spent on a previous block", __func__));
}
}
}
Expand All @@ -4979,7 +4975,7 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex,
splitHeight = prev->nHeight;

// Now that this loop if completed. Check if we have zPIV inputs.
if(hasZPIVInputs){
if(hasZPIVInputs) {
for (const CTxIn& zPivInput : zPIVInputs) {
libzerocoin::CoinSpend spend = TxInToZerocoinSpend(zPivInput);

Expand All @@ -4993,7 +4989,7 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex,

// Now check if the serial exists before the chain split.
int nHeightTx = 0;
if (IsSerialInBlockchain(spend.getCoinSerialNumber(), nHeightTx)){
if (IsSerialInBlockchain(spend.getCoinSerialNumber(), nHeightTx)) {
// if the height is nHeightTx > chainSplit means that the spent occurred after the chain split
if(nHeightTx <= splitHeight)
return state.DoS(100, error("%s: serial double spent on main chain", __func__));
Expand Down
3 changes: 0 additions & 3 deletions test/functional/pos_fakestake.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,6 @@ def fake_stake(self,
self.log.info("Trying to send block [%s...] with height=%d" % (block.hash[:16], nHeight))
var = self.nodes[1].submitblock(bytes_to_hex_str(block.serialize()))
sleep(1)
if (isZPoS and not isMainChain and i < 2):
# !TODO: fix this last case failing (this must NOT be accepted)
fMustBeAccepted = True
if (not fMustBeAccepted and var not in [None, "rejected", "bad-txns-invalid-zpiv"]):
raise AssertionError("Error, block submitted (%s) in %s chain" % (var, chainName))
elif (fMustBeAccepted and var != "inconclusive"):
Expand Down

0 comments on commit 0417f66

Please sign in to comment.