Skip to content

Commit

Permalink
Fix/prevent reverts (#408)
Browse files Browse the repository at this point in the history
* Prevent chain revert past Eunos

Signed-off-by: Anthony Fieroni <[email protected]>

* Add consesus code to reject custom tx

Signed-off-by: Anthony Fieroni <[email protected]>
  • Loading branch information
bvbfan authored May 21, 2021
1 parent 7187082 commit 74a0eb3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/consensus/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ static const unsigned char REJECT_NONSTANDARD = 0x40;
// static const unsigned char REJECT_DUST = 0x41; // part of BIP 61
static const unsigned char REJECT_INSUFFICIENTFEE = 0x42;
static const unsigned char REJECT_CHECKPOINT = 0x43;
static const unsigned char REJECT_CUSTOMTX = 0x44;

/** A "reason" why something was invalid, suitable for determining whether the
* provider of the object should be banned/ignored/disconnected/etc.
Expand Down
38 changes: 18 additions & 20 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2455,9 +2455,15 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl

const auto res = ApplyCustomTx(accountsView, view, tx, chainparams.GetConsensus(), pindex->nHeight, pindex->GetBlockTime(), i, paccountHistoryDB.get(), pburnHistoryDB.get());
if (!res.ok && (res.code & CustomTxErrCodes::Fatal)) {
// we will never fail, but skip, unless transaction mints UTXOs
return error("ConnectBlock(): ApplyCustomTx on %s failed with %s",
tx.GetHash().ToString(), res.msg);
if (pindex->nHeight >= chainparams.GetConsensus().EunosHeight) {
return state.Invalid(ValidationInvalidReason::CONSENSUS,
error("ConnectBlock(): ApplyCustomTx on %s failed with %s",
tx.GetHash().ToString(), res.msg), REJECT_CUSTOMTX, "bad-custom-tx");
} else {
// we will never fail, but skip, unless transaction mints UTXOs
return error("ConnectBlock(): ApplyCustomTx on %s failed with %s",
tx.GetHash().ToString(), res.msg);
}
}
// log
if (!fJustCheck && !res.msg.empty()) {
Expand Down Expand Up @@ -3516,28 +3522,22 @@ bool CChainState::ActivateBestChainStep(CValidationState& state, const CChainPar
// at this stage only high hash error can be in header
// so just skip that block
continue;
} else if (reason == ValidationInvalidReason::BLOCK_MUTATED) {
}
fContinue = false;
fInvalidFound = true;
InvalidChainFound(vpindexToConnect.front());
if (reason == ValidationInvalidReason::BLOCK_MUTATED) {
// prior EunosHeight we shoutdown node on mutated block
if (ShutdownRequested()) {
return false;
}
// now block cannot be part of blockchain either
// but it can be produced by outdated/malicious masternode
// so we should not shoutdown entire network, let's skip it
continue;
} else if (reason == ValidationInvalidReason::CONSENSUS) {
if (pindexConnect->nHeight == chainparams.GetConsensus().EunosHeight) {
auto strReason = state.GetRejectReason();
// we have situation when old masternode will generate a block
// that has coinbase higher than post Eunos fork
// that block is invalid, let's keep waiting for the choosen one
if (strReason.find("bad-cb-amount") != strReason.npos) {
continue;
}
}
// so we should not shoutdown entire network
}
InvalidChainFound(vpindexToConnect.front());
if (fCheckpointsEnabled && pindexConnect == pindexMostWork) {
if (fCheckpointsEnabled && pindexConnect == pindexMostWork
&& (pindexConnect->nHeight < chainparams.GetConsensus().EunosHeight
|| state.GetRejectCode() == REJECT_CUSTOMTX)) {
// NOTE: Invalidate blocks back to last checkpoint
auto &checkpoints = chainparams.Checkpoints().mapCheckpoints;
//calculate the latest suitable checkpoint block height
Expand All @@ -3561,8 +3561,6 @@ bool CChainState::ActivateBestChainStep(CValidationState& state, const CChainPar
return false;
}
}
fInvalidFound = true;
fContinue = false;
break;
} else {
// A system error occurred (disk space, database error, ...).
Expand Down

0 comments on commit 74a0eb3

Please sign in to comment.