Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
501}502// - Populate a CoinsViewCache with the unspent output503CCoinsViewCache coins_cache{&CoinsViewEmpty::Get()};504for (const auto& input_transaction : input_transactions) {505AddCoins(coins_cache, *input_transaction.get(), input_height);506}507// Build Outpoint to Coin map for SignTransaction508std::map<COutPoint, Coin> input_coins;509CAmount inputs_amount{0};510for (const auto& outpoint_to_spend : inputs) {511// Use GetCoin to properly populate utxo_to_spend512auto utxo_to_spend{coins_cache.GetCoin(outpoint_to_spend).value()};513input_coins.insert({outpoint_to_spend, utxo_to_spend});514inputs_amount += utxo_to_spend.out.nValue;515}516// - Default signature hashing type517int nHashType = SIGHASH_ALL;518std::map<int, bilingual_str> input_errors;519assert(SignTransaction(mempool_txn, &keystore, input_coins, {.sighash_type = nHashType}, input_errors));520CAmount current_fee = inputs_amount - std::accumulate(outputs.begin(), outputs.end(), CAmount(0),521[](const CAmount& acc, const CTxOut& out) {522return acc + out.nValue;523});524// Deduct fees from fee_output to meet feerate if set525if (feerate.has_value()) {526assert(fee_output.has_value());527assert(fee_output.value() < mempool_txn.vout.size());528CAmount target_fee = feerate.value().GetFee(GetVirtualTransactionSize(CTransaction{mempool_txn}));529CAmount deduction = target_fee - current_fee;530if (deduction > 0) {531// Only deduct fee if there's anything to deduct. If the caller has put more fees than532// the target feerate, don't change the fee.533mempool_txn.vout[fee_output.value()].nValue -= deduction;534// Re-sign since an output has changed535input_errors.clear();536assert(SignTransaction(mempool_txn, &keystore, input_coins, {.sighash_type = nHashType}, input_errors));537current_fee = target_fee;538}539}540return {mempool_txn, current_fee};541}543CMutableTransaction TestChain100Setup::CreateValidMempoolTransaction(const std::vector<CTransactionRef>& input_transactions,544const std::vector<COutPoint>& inputs,545int input_height,546const std::vector<CKey>& input_signing_keys,547const std::vector<CTxOut>& outputs,548bool submit)549{550CMutableTransaction mempool_txn = CreateValidTransaction(input_transactions, inputs, input_height, input_signing_keys, outputs, std::nullopt, std::nullopt).first;551// If submit=true, add transaction to the mempool.552if (submit) {553LOCK(cs_main);554const MempoolAcceptResult result = m_node.chainman->ProcessTransaction(MakeTransactionRef(mempool_txn));555assert(result.m_result_type == MempoolAcceptResult::ResultType::VALID);556}557return mempool_txn;558}560CMutableTransaction TestChain100Setup::CreateValidMempoolTransaction(CTransactionRef input_transaction,561uint32_t input_vout,562int input_height,563CKey input_signing_key,564CScript output_destination,565CAmount output_amount,566bool submit)567{568COutPoint input{input_transaction->GetHash(), input_vout};569CTxOut output{output_amount, output_destination};570return CreateValidMempoolTransaction(/*input_transactions=*/{input_transaction},571/*inputs=*/{input},572/*input_height=*/input_height,573/*input_signing_keys=*/{input_signing_key},574/*outputs=*/{output},575/*submit=*/submit);576}578std::vector<CTransactionRef> TestChain100Setup::PopulateMempool(FastRandomContext& det_rand, size_t num_transactions, bool submit)579{580std::vector<CTransactionRef> mempool_transactions;581std::deque<std::pair<COutPoint, CAmount>> unspent_prevouts, undo_info;582std::transform(m_coinbase_txns.begin(), m_coinbase_txns.end(), std::back_inserter(unspent_prevouts),583[](const auto& tx){ return std::make_pair(COutPoint(tx->GetHash(), 0), tx->vout[0].nValue); });584while (num_transactions > 0 && !unspent_prevouts.empty()) {585// The number of inputs and outputs are randomly chosen, between 1-5586// and 1-25 respectively.587CMutableTransaction mtx = CMutableTransaction();588const size_t num_inputs = det_rand.randrange(5) + 1;589CAmount total_in{0};590for (size_t n{0}; n < num_inputs; ++n) {591if (unspent_prevouts.empty()) break;592const auto& [prevout, amount] = unspent_prevouts.front();593undo_info.emplace_back(prevout, amount);594mtx.vin.emplace_back(prevout, CScript());595total_in += amount;596unspent_prevouts.pop_front();597}598const size_t num_outputs = det_rand.randrange(25) + 1;599const CAmount fee = 100 * det_rand.randrange(30);600const CAmount amount_per_output = (total_in - fee) / num_outputs;601for (size_t n{0}; n < num_outputs; ++n) {602CScript spk = CScript() << CScriptNum(num_transactions + n);603mtx.vout.emplace_back(amount_per_output, spk);604}605CTransactionRef ptx = MakeTransactionRef(mtx);606bool success{true};607if (submit) {608LOCK2(cs_main, m_node.mempool->cs);609LockPoints lp;610auto changeset = m_node.mempool->GetChangeSet();611changeset->StageAddition(ptx, /*fee=*/(total_in - num_outputs * amount_per_output),612/*time=*/0, /*entry_height=*/1, /*entry_sequence=*/0,613/*spends_coinbase=*/false, /*sigops_cost=*/4, lp);614if (changeset->CheckMemPoolPolicyLimits()) {615changeset->Apply();616--num_transactions;617} else {618success = false;619// Add the inputs back to unspent prevouts620for (const auto& [prevout, amount] : undo_info) {621unspent_prevouts.emplace_back(prevout, amount);622std::swap(unspent_prevouts.back(), unspent_prevouts[det_rand.randrange(unspent_prevouts.size())]);623}624}625}626if (success) {627mempool_transactions.push_back(ptx);628if (amount_per_output > 3000) {629// If the value is high enough to fund another transaction + fees, keep track of it so630// it can be used to build a more complex transaction graph. Insert randomly into631// unspent_prevouts for extra randomness in the resulting structures.632for (size_t n{0}; n < num_outputs; ++n) {633unspent_prevouts.emplace_back(COutPoint(ptx->GetHash(), n), amount_per_output);634std::swap(unspent_prevouts.back(), unspent_prevouts[det_rand.randrange(unspent_prevouts.size())]);635}636}637}638undo_info.clear();639}640return mempool_transactions;641}643SocketTestingSetup::SocketTestingSetup()644{645// HTTPServer is not integrated into NodeContext yet and still pulls global args.646// This is the IP address DynSock claims to be from when connecting.647gArgs.ForceSetArg("-rpcallowip", "5.5.5.5");649// "back up" the current CreateSock() so we can restore it after the test650m_create_sock_orig = CreateSock;652CreateSock = [this](int, int, int) {653// This is a mock Listening Socket that a server can "bind" to and654// listen to for incoming connections. We won't need to access its I/O655// pipes because we don't read or write directly to it. It will return656// Connected Sockets from the queue via its Accept() method.657return std::make_unique<DynSock>(std::make_shared<DynSock::Pipes>(), &m_accepted_sockets);658};659};661SocketTestingSetup::~SocketTestingSetup()662{663CreateSock = m_create_sock_orig;664}666/**667* @returns a real block (0000000000013b8ab2cd513b0261a14096412195a72a0c4827d229dcc7e0f7af)668* with 9 txs.669*/670CBlock getBlock13b8a()671{672CBlock block;673DataStream stream{674"0100000090f0a9f110702f808219ebea1173056042a714bad51b916cb6800000000000005275289558f51c9966699404ae2294730c3c9f9bda53523ce50e9b95e558da2fdb261b4d4c86041b1ab1bf930901000000010000000000000000000000000000000000000000000000000000000000000000ffffffff07044c86041b0146ffffffff0100f2052a01000000434104e18f7afbe4721580e81e8414fc8c24d7cfacf254bb5c7b949450c3e997c2dc1242487a8169507b631eb3771f2b425483fb13102c4eb5d858eef260fe70fbfae0ac00000000010000000196608ccbafa16abada902780da4dc35dafd7af05fa0da08cf833575f8cf9e836000000004a493046022100dab24889213caf43ae6adc41cf1c9396c08240c199f5225acf45416330fd7dbd022100fe37900e0644bf574493a07fc5edba06dbc07c311b947520c2d514bc5725dcb401ffffffff0100f2052a010000001976a914f15d1921f52e4007b146dfa60f369ed2fc393ce288ac000000000100000001fb766c1288458c2bafcfec81e48b24d98ec706de6b8af7c4e3c29419bfacb56d000000008c493046022100f268ba165ce0ad2e6d93f089cfcd3785de5c963bb5ea6b8c1b23f1ce3e517b9f022100da7c0f21adc6c401887f2bfd1922f11d76159cbc597fbd756a23dcbb00f4d7290141042b4e8625a96127826915a5b109852636ad0da753c9e1d5606a50480cd0c40f1f8b8d898235e571fe9357d9ec842bc4bba1827daaf4de06d71844d0057707966affffffff0280969800000000001976a9146963907531db72d0ed1a0cfb471ccb63923446f388ac80d6e34c000000001976a914f0688ba1c0d1ce182c7af6741e02658c7d4dfcd388ac000000000100000002c40297f730dd7b5a99567eb8d27b78758f607507c52292d02d4031895b52f2ff010000008b483045022100f7edfd4b0aac404e5bab4fd3889e0c6c41aa8d0e6fa122316f68eddd0a65013902205b09cc8b2d56e1cd1f7f2fafd60a129ed94504c4ac7bdc67b56fe67512658b3e014104732012cb962afa90d31b25d8fb0e32c94e513ab7a17805c14ca4c3423e18b4fb5d0e676841733cb83abaf975845c9f6f2a8097b7d04f4908b18368d6fc2d68ecffffffffca5065ff9617cbcba45eb23726df6498a9b9cafed4f54cbab9d227b0035ddefb000000008a473044022068010362a13c7f9919fa832b2dee4e788f61f6f5d344a7c2a0da6ae740605658022006d1af525b9a14a35c003b78b72bd59738cd676f845d1ff3fc25049e01003614014104732012cb962afa90d31b25d8fb0e32c94e513ab7a17805c14ca4c3423e18b4fb5d0e676841733cb83abaf975845c9f6f2a8097b7d04f4908b18368d6fc2d68ecffffffff01001ec4110200000043410469ab4181eceb28985b9b4e895c13fa5e68d85761b7eee311db5addef76fa8621865134a221bd01f28ec9999ee3e021e60766e9d1f3458c115fb28650605f11c9ac000000000100000001cdaf2f758e91c514655e2dc50633d1e4c84989f8aa90a0dbc883f0d23ed5c2fa010000008b48304502207ab51be6f12a1962ba0aaaf24a20e0b69b27a94fac5adf45aa7d2d18ffd9236102210086ae728b370e5329eead9accd880d0cb070aea0c96255fae6c4f1ddcce1fd56e014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff02404b4c00000000001976a9142b6ba7c9d796b75eef7942fc9288edd37c32f5c388ac002d3101000000001976a9141befba0cdc1ad56529371864d9f6cb042faa06b588ac000000000100000001b4a47603e71b61bc3326efd90111bf02d2f549b067f4c4a8fa183b57a0f800cb010000008a4730440220177c37f9a505c3f1a1f0ce2da777c339bd8339ffa02c7cb41f0a5804f473c9230220585b25a2ee80eb59292e52b987dad92acb0c64eced92ed9ee105ad153cdb12d001410443bd44f683467e549dae7d20d1d79cbdb6df985c6e9c029c8d0c6cb46cc1a4d3cf7923c5021b27f7a0b562a … [line preview shortened]675};676stream >> TX_WITH_WITNESS(block);677return block;678}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.