Changeset View
Changeset View
Standalone View
Standalone View
src/test/validation_block_tests.cpp
Show First 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | struct TestSubscriber : public CValidationInterface { | ||||
void BlockDisconnected(const std::shared_ptr<const CBlock> &block) { | void BlockDisconnected(const std::shared_ptr<const CBlock> &block) { | ||||
BOOST_CHECK_EQUAL(m_expected_tip, block->GetHash()); | BOOST_CHECK_EQUAL(m_expected_tip, block->GetHash()); | ||||
m_expected_tip = block->hashPrevBlock; | m_expected_tip = block->hashPrevBlock; | ||||
} | } | ||||
}; | }; | ||||
std::shared_ptr<CBlock> Block(const Config &config, const uint256 &prev_hash) { | std::shared_ptr<CBlock> Block(const Config &config, | ||||
const BlockHash &prev_hash) { | |||||
static int i = 0; | static int i = 0; | ||||
static uint64_t time = config.GetChainParams().GenesisBlock().nTime; | static uint64_t time = config.GetChainParams().GenesisBlock().nTime; | ||||
CScript pubKey; | CScript pubKey; | ||||
pubKey << i++ << OP_TRUE; | pubKey << i++ << OP_TRUE; | ||||
auto ptemplate = BlockAssembler(config, g_mempool).CreateNewBlock(pubKey); | auto ptemplate = BlockAssembler(config, g_mempool).CreateNewBlock(pubKey); | ||||
auto pblock = std::make_shared<CBlock>(ptemplate->block); | auto pblock = std::make_shared<CBlock>(ptemplate->block); | ||||
Show All 15 Lines | while (!CheckProofOfWork(pblock->GetHash(), pblock->nBits, params)) { | ||||
++(pblock->nNonce); | ++(pblock->nNonce); | ||||
} | } | ||||
return pblock; | return pblock; | ||||
} | } | ||||
// construct a valid block | // construct a valid block | ||||
const std::shared_ptr<const CBlock> GoodBlock(const Config &config, | const std::shared_ptr<const CBlock> GoodBlock(const Config &config, | ||||
const uint256 &prev_hash) { | const BlockHash &prev_hash) { | ||||
return FinalizeBlock(config.GetChainParams().GetConsensus(), | return FinalizeBlock(config.GetChainParams().GetConsensus(), | ||||
Block(config, prev_hash)); | Block(config, prev_hash)); | ||||
} | } | ||||
// construct an invalid block (but with a valid header) | // construct an invalid block (but with a valid header) | ||||
const std::shared_ptr<const CBlock> BadBlock(const Config &config, | const std::shared_ptr<const CBlock> BadBlock(const Config &config, | ||||
const uint256 &prev_hash) { | const BlockHash &prev_hash) { | ||||
auto pblock = Block(config, prev_hash); | auto pblock = Block(config, prev_hash); | ||||
CMutableTransaction coinbase_spend; | CMutableTransaction coinbase_spend; | ||||
coinbase_spend.vin.push_back( | coinbase_spend.vin.push_back( | ||||
CTxIn(COutPoint(pblock->vtx[0]->GetId(), 0), CScript(), 0)); | CTxIn(COutPoint(pblock->vtx[0]->GetId(), 0), CScript(), 0)); | ||||
coinbase_spend.vout.push_back(pblock->vtx[0]->vout[0]); | coinbase_spend.vout.push_back(pblock->vtx[0]->vout[0]); | ||||
CTransactionRef tx = MakeTransactionRef(coinbase_spend); | CTransactionRef tx = MakeTransactionRef(coinbase_spend); | ||||
pblock->vtx.push_back(tx); | pblock->vtx.push_back(tx); | ||||
auto ret = FinalizeBlock(config.GetChainParams().GetConsensus(), pblock); | auto ret = FinalizeBlock(config.GetChainParams().GetConsensus(), pblock); | ||||
return ret; | return ret; | ||||
} | } | ||||
void BuildChain(const Config &config, const uint256 &root, int height, | void BuildChain(const Config &config, const BlockHash &root, int height, | ||||
const unsigned int invalid_rate, const unsigned int branch_rate, | const unsigned int invalid_rate, const unsigned int branch_rate, | ||||
const unsigned int max_size, | const unsigned int max_size, | ||||
std::vector<std::shared_ptr<const CBlock>> &blocks) { | std::vector<std::shared_ptr<const CBlock>> &blocks) { | ||||
if (height <= 0 || blocks.size() >= max_size) { | if (height <= 0 || blocks.size() >= max_size) { | ||||
return; | return; | ||||
} | } | ||||
bool gen_invalid = InsecureRandRange(100) < invalid_rate; | bool gen_invalid = InsecureRandRange(100) < invalid_rate; | ||||
▲ Show 20 Lines • Show All 93 Lines • Show Last 20 Lines |