mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 12:32:48 +01:00
Abstract context-dependent block checking from acceptance
This commit is contained in:
parent
ff17816abf
commit
a48f2d6ddd
106
src/main.cpp
106
src/main.cpp
@ -2334,32 +2334,15 @@ bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bo
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex** ppindex)
|
bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex * const pindexPrev)
|
||||||
{
|
{
|
||||||
AssertLockHeld(cs_main);
|
|
||||||
// Check for duplicate
|
|
||||||
uint256 hash = block.GetHash();
|
uint256 hash = block.GetHash();
|
||||||
BlockMap::iterator miSelf = mapBlockIndex.find(hash);
|
if (hash == Params().HashGenesisBlock())
|
||||||
CBlockIndex *pindex = NULL;
|
|
||||||
if (miSelf != mapBlockIndex.end()) {
|
|
||||||
// Block header is already known.
|
|
||||||
pindex = miSelf->second;
|
|
||||||
if (ppindex)
|
|
||||||
*ppindex = pindex;
|
|
||||||
if (pindex->nStatus & BLOCK_FAILED_MASK)
|
|
||||||
return state.Invalid(error("%s : block is marked invalid", __func__), 0, "duplicate");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
|
|
||||||
// Get prev block index
|
assert(pindexPrev);
|
||||||
CBlockIndex* pindexPrev = NULL;
|
|
||||||
int nHeight = 0;
|
int nHeight = pindexPrev->nHeight+1;
|
||||||
if (hash != Params().HashGenesisBlock()) {
|
|
||||||
BlockMap::iterator mi = mapBlockIndex.find(block.hashPrevBlock);
|
|
||||||
if (mi == mapBlockIndex.end())
|
|
||||||
return state.DoS(10, error("%s : prev block not found", __func__), 0, "bad-prevblk");
|
|
||||||
pindexPrev = (*mi).second;
|
|
||||||
nHeight = pindexPrev->nHeight+1;
|
|
||||||
|
|
||||||
// Check proof of work
|
// Check proof of work
|
||||||
if ((!Params().SkipProofOfWorkCheck()) &&
|
if ((!Params().SkipProofOfWorkCheck()) &&
|
||||||
@ -2389,8 +2372,64 @@ bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBloc
|
|||||||
return state.Invalid(error("%s : rejected nVersion=1 block", __func__),
|
return state.Invalid(error("%s : rejected nVersion=1 block", __func__),
|
||||||
REJECT_OBSOLETE, "bad-version");
|
REJECT_OBSOLETE, "bad-version");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIndex * const pindexPrev)
|
||||||
|
{
|
||||||
|
const int nHeight = pindexPrev == NULL ? 0 : pindexPrev->nHeight + 1;
|
||||||
|
|
||||||
|
// Check that all transactions are finalized
|
||||||
|
BOOST_FOREACH(const CTransaction& tx, block.vtx)
|
||||||
|
if (!IsFinalTx(tx, nHeight, block.GetBlockTime())) {
|
||||||
|
return state.DoS(10, error("%s : contains a non-final transaction", __func__), REJECT_INVALID, "bad-txns-nonfinal");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Enforce block.nVersion=2 rule that the coinbase starts with serialized block height
|
||||||
|
// if 750 of the last 1,000 blocks are version 2 or greater (51/100 if testnet):
|
||||||
|
if (block.nVersion >= 2 &&
|
||||||
|
CBlockIndex::IsSuperMajority(2, pindexPrev, Params().EnforceBlockUpgradeMajority()))
|
||||||
|
{
|
||||||
|
CScript expect = CScript() << nHeight;
|
||||||
|
if (block.vtx[0].vin[0].scriptSig.size() < expect.size() ||
|
||||||
|
!std::equal(expect.begin(), expect.end(), block.vtx[0].vin[0].scriptSig.begin())) {
|
||||||
|
return state.DoS(100, error("%s : block height mismatch in coinbase", __func__), REJECT_INVALID, "bad-cb-height");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex** ppindex)
|
||||||
|
{
|
||||||
|
AssertLockHeld(cs_main);
|
||||||
|
// Check for duplicate
|
||||||
|
uint256 hash = block.GetHash();
|
||||||
|
BlockMap::iterator miSelf = mapBlockIndex.find(hash);
|
||||||
|
CBlockIndex *pindex = NULL;
|
||||||
|
if (miSelf != mapBlockIndex.end()) {
|
||||||
|
// Block header is already known.
|
||||||
|
pindex = miSelf->second;
|
||||||
|
if (ppindex)
|
||||||
|
*ppindex = pindex;
|
||||||
|
if (pindex->nStatus & BLOCK_FAILED_MASK)
|
||||||
|
return state.Invalid(error("%s : block is marked invalid", __func__), 0, "duplicate");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get prev block index
|
||||||
|
CBlockIndex* pindexPrev = NULL;
|
||||||
|
if (hash != Params().HashGenesisBlock()) {
|
||||||
|
BlockMap::iterator mi = mapBlockIndex.find(block.hashPrevBlock);
|
||||||
|
if (mi == mapBlockIndex.end())
|
||||||
|
return state.DoS(10, error("%s : prev block not found", __func__), 0, "bad-prevblk");
|
||||||
|
pindexPrev = (*mi).second;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ContextualCheckBlockHeader(block, state, pindexPrev))
|
||||||
|
return false;
|
||||||
|
|
||||||
if (pindex == NULL)
|
if (pindex == NULL)
|
||||||
pindex = AddToBlockIndex(block);
|
pindex = AddToBlockIndex(block);
|
||||||
|
|
||||||
@ -2415,7 +2454,7 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!CheckBlock(block, state)) {
|
if ((!CheckBlock(block, state)) || !ContextualCheckBlock(block, state, pindex->pprev)) {
|
||||||
if (state.IsInvalid() && !state.CorruptionPossible()) {
|
if (state.IsInvalid() && !state.CorruptionPossible()) {
|
||||||
pindex->nStatus |= BLOCK_FAILED_VALID;
|
pindex->nStatus |= BLOCK_FAILED_VALID;
|
||||||
}
|
}
|
||||||
@ -2424,27 +2463,6 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex,
|
|||||||
|
|
||||||
int nHeight = pindex->nHeight;
|
int nHeight = pindex->nHeight;
|
||||||
|
|
||||||
// Check that all transactions are finalized
|
|
||||||
BOOST_FOREACH(const CTransaction& tx, block.vtx)
|
|
||||||
if (!IsFinalTx(tx, nHeight, block.GetBlockTime())) {
|
|
||||||
pindex->nStatus |= BLOCK_FAILED_VALID;
|
|
||||||
return state.DoS(10, error("AcceptBlock() : contains a non-final transaction"),
|
|
||||||
REJECT_INVALID, "bad-txns-nonfinal");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Enforce block.nVersion=2 rule that the coinbase starts with serialized block height
|
|
||||||
// if 750 of the last 1,000 blocks are version 2 or greater (51/100 if testnet):
|
|
||||||
if (block.nVersion >= 2 &&
|
|
||||||
CBlockIndex::IsSuperMajority(2, pindex->pprev, Params().EnforceBlockUpgradeMajority()))
|
|
||||||
{
|
|
||||||
CScript expect = CScript() << nHeight;
|
|
||||||
if (block.vtx[0].vin[0].scriptSig.size() < expect.size() ||
|
|
||||||
!std::equal(expect.begin(), expect.end(), block.vtx[0].vin[0].scriptSig.begin())) {
|
|
||||||
pindex->nStatus |= BLOCK_FAILED_VALID;
|
|
||||||
return state.DoS(100, error("AcceptBlock() : block height mismatch in coinbase"), REJECT_INVALID, "bad-cb-height");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write block to history file
|
// Write block to history file
|
||||||
try {
|
try {
|
||||||
unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
|
unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
|
||||||
|
@ -463,6 +463,10 @@ bool ConnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, C
|
|||||||
bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, bool fCheckPOW = true);
|
bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, bool fCheckPOW = true);
|
||||||
bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
|
bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
|
||||||
|
|
||||||
|
// Context-dependent validity checks
|
||||||
|
bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex *pindexPrev);
|
||||||
|
bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIndex *pindexPrev);
|
||||||
|
|
||||||
// Store block on disk
|
// Store block on disk
|
||||||
// if dbp is provided, the file is known to already reside on disk
|
// if dbp is provided, the file is known to already reside on disk
|
||||||
bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex **pindex, CDiskBlockPos* dbp = NULL);
|
bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex **pindex, CDiskBlockPos* dbp = NULL);
|
||||||
|
Loading…
Reference in New Issue
Block a user