Changeset View
Changeset View
Standalone View
Standalone View
src/bench/duplicate_inputs.cpp
Show All 11 Lines | |||||
#include <random.h> | #include <random.h> | ||||
#include <script/scriptcache.h> | #include <script/scriptcache.h> | ||||
#include <txmempool.h> | #include <txmempool.h> | ||||
#include <validation.h> | #include <validation.h> | ||||
static void DuplicateInputs(benchmark::State &state) { | static void DuplicateInputs(benchmark::State &state) { | ||||
const CScript SCRIPT_PUB{CScript(OP_TRUE)}; | const CScript SCRIPT_PUB{CScript(OP_TRUE)}; | ||||
const CChainParams &chainparams = Params(); | const CChainParams &chainParams = Params(); | ||||
const Consensus::Params &consensusParams = chainParams.GetConsensus(); | |||||
CBlock block{}; | CBlock block{}; | ||||
CMutableTransaction coinbaseTx{}; | CMutableTransaction coinbaseTx{}; | ||||
CMutableTransaction naughtyTx{}; | CMutableTransaction naughtyTx{}; | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
CBlockIndex *pindexPrev = ::ChainActive().Tip(); | CBlockIndex *pindexPrev = ::ChainActive().Tip(); | ||||
assert(pindexPrev != nullptr); | assert(pindexPrev != nullptr); | ||||
block.nBits = | block.nBits = GetNextWorkRequired(pindexPrev, &block, chainParams); | ||||
GetNextWorkRequired(pindexPrev, &block, chainparams.GetConsensus()); | |||||
block.nNonce = 0; | block.nNonce = 0; | ||||
auto nHeight = pindexPrev->nHeight + 1; | auto nHeight = pindexPrev->nHeight + 1; | ||||
// Make a coinbase TX | // Make a coinbase TX | ||||
coinbaseTx.vin.resize(1); | coinbaseTx.vin.resize(1); | ||||
coinbaseTx.vin[0].prevout = COutPoint(); | coinbaseTx.vin[0].prevout = COutPoint(); | ||||
coinbaseTx.vout.resize(1); | coinbaseTx.vout.resize(1); | ||||
coinbaseTx.vout[0].scriptPubKey = SCRIPT_PUB; | coinbaseTx.vout[0].scriptPubKey = SCRIPT_PUB; | ||||
coinbaseTx.vout[0].nValue = | coinbaseTx.vout[0].nValue = GetBlockSubsidy(nHeight, consensusParams); | ||||
GetBlockSubsidy(nHeight, chainparams.GetConsensus()); | |||||
coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0; | coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0; | ||||
naughtyTx.vout.resize(1); | naughtyTx.vout.resize(1); | ||||
naughtyTx.vout[0].nValue = Amount::zero(); | naughtyTx.vout[0].nValue = Amount::zero(); | ||||
naughtyTx.vout[0].scriptPubKey = SCRIPT_PUB; | naughtyTx.vout[0].scriptPubKey = SCRIPT_PUB; | ||||
uint64_t n_inputs = | uint64_t n_inputs = | ||||
((MAX_TX_SIZE - CTransaction(naughtyTx).GetTotalSize()) / 41) - 100; | ((MAX_TX_SIZE - CTransaction(naughtyTx).GetTotalSize()) / 41) - 100; | ||||
for (uint64_t x = 0; x < (n_inputs - 1); ++x) { | for (uint64_t x = 0; x < (n_inputs - 1); ++x) { | ||||
naughtyTx.vin.emplace_back(TxId(GetRandHash()), 0, CScript(), 0); | naughtyTx.vin.emplace_back(TxId(GetRandHash()), 0, CScript(), 0); | ||||
} | } | ||||
naughtyTx.vin.emplace_back(naughtyTx.vin.back()); | naughtyTx.vin.emplace_back(naughtyTx.vin.back()); | ||||
block.vtx.push_back(MakeTransactionRef(std::move(coinbaseTx))); | block.vtx.push_back(MakeTransactionRef(std::move(coinbaseTx))); | ||||
block.vtx.push_back(MakeTransactionRef(std::move(naughtyTx))); | block.vtx.push_back(MakeTransactionRef(std::move(naughtyTx))); | ||||
block.hashMerkleRoot = BlockMerkleRoot(block); | block.hashMerkleRoot = BlockMerkleRoot(block); | ||||
while (state.KeepRunning()) { | while (state.KeepRunning()) { | ||||
BlockValidationState cvstate{}; | BlockValidationState cvstate{}; | ||||
assert(!CheckBlock(block, cvstate, chainparams.GetConsensus(), | assert(!CheckBlock(block, cvstate, consensusParams, | ||||
BlockValidationOptions(GetConfig()) | BlockValidationOptions(GetConfig()) | ||||
.withCheckPoW(false) | .withCheckPoW(false) | ||||
.withCheckMerkleRoot(false))); | .withCheckMerkleRoot(false))); | ||||
assert(cvstate.GetRejectReason() == "bad-txns-inputs-duplicate"); | assert(cvstate.GetRejectReason() == "bad-txns-inputs-duplicate"); | ||||
} | } | ||||
} | } | ||||
BENCHMARK(DuplicateInputs, 10); | BENCHMARK(DuplicateInputs, 10); |