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.
252BOOST_AUTO_TEST_CASE(tx_invalid)253{254// Read tests from test/data/tx_invalid.json255UniValue tests = read_json(json_tests::tx_invalid);257for (unsigned int idx = 0; idx < tests.size(); idx++) {258const UniValue& test = tests[idx];259std::string strTest = test.write();260if (test[0].isArray())261{262if (test.size() != 3 || !test[1].isStr() || !test[2].isStr())263{264BOOST_ERROR("Bad test: " << strTest);265continue;266}268std::map<COutPoint, CScript> mapprevOutScriptPubKeys;269std::map<COutPoint, int64_t> mapprevOutValues;270UniValue inputs = test[0].get_array();271bool fValid = true;272for (unsigned int inpIdx = 0; inpIdx < inputs.size(); inpIdx++) {273const UniValue& input = inputs[inpIdx];274if (!input.isArray()) {275fValid = false;276break;277}278const UniValue& vinput = input.get_array();279if (vinput.size() < 3 || vinput.size() > 4)280{281fValid = false;282break;283}284COutPoint outpoint{Txid::FromHex(vinput[0].get_str()).value(), uint32_t(vinput[1].getInt<int>())};285mapprevOutScriptPubKeys[outpoint] = ParseScript(vinput[2].get_str());286if (vinput.size() >= 4)287{288mapprevOutValues[outpoint] = vinput[3].getInt<int64_t>();289}290}291if (!fValid)292{293BOOST_ERROR("Bad test: " << strTest);294continue;295}297std::string transaction = test[1].get_str();298DataStream stream(ParseHex(transaction));299CTransaction tx(deserialize, TX_WITH_WITNESS, stream);301TxValidationState state;302if (!CheckTransaction(tx, state) || state.IsInvalid()) {303BOOST_CHECK_MESSAGE(test[2].get_str() == "BADTX", strTest);304continue;305}307PrecomputedTransactionData txdata(tx);308script_verify_flags verify_flags = ParseScriptFlags(test[2].get_str());310// Check that the test gives a valid combination of flags (otherwise VerifyScript will throw). Don't edit the flags.311if (verify_flags != FillFlags(verify_flags)) {312BOOST_ERROR("Bad test flags: " << strTest);313}315// Not using FillFlags() in the main test, in order to detect invalid verifyFlags combination316BOOST_CHECK_MESSAGE(CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, verify_flags, txdata, strTest, /*expect_valid=*/false),317"Tx unexpectedly passed: " << strTest);319// Backwards compatibility of script verification flags: Adding any flag(s) should not validate an invalid transaction320for (const auto& [name, flag] : mapFlagNames) {321script_verify_flags flags = FillFlags(verify_flags | flag);322// Adding individual flags323if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags, txdata, strTest, /*expect_valid=*/false)) {324BOOST_ERROR("Tx unexpectedly passed with flag " << name << " set: " << strTest);325}326// Adding random combinations of flags327flags = FillFlags(verify_flags | script_verify_flags::from_int(m_rng.randbits(MAX_SCRIPT_VERIFY_FLAGS_BITS)));328if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags, txdata, strTest, /*expect_valid=*/false)) {329BOOST_ERROR("Tx unexpectedly passed with random flags " << name << ": " << strTest);330}331}333// Check that flags are minimal: transaction should succeed if any set flags are unset.334for (auto flags_excluding_one : ExcludeIndividualFlags(verify_flags)) {335if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags_excluding_one, txdata, strTest, /*expect_valid=*/true)) {336BOOST_ERROR("Too many flags set: " << strTest);337}338}339}340}341}343BOOST_AUTO_TEST_CASE(tx_no_inputs)344{345CMutableTransaction empty;347TxValidationState state;348BOOST_CHECK_MESSAGE(!CheckTransaction(CTransaction(empty), state), "Transaction with no inputs should be invalid.");349BOOST_CHECK(state.GetRejectReason() == "bad-txns-vin-empty");350}352BOOST_AUTO_TEST_CASE(tx_oversized)353{354auto createTransaction =[](size_t payloadSize) {355CMutableTransaction tx;356tx.vin.resize(1);357tx.vout.emplace_back(1, CScript() << OP_RETURN << std::vector<unsigned char>(payloadSize));358return CTransaction(tx);359};360const auto maxTransactionSize = MAX_BLOCK_WEIGHT / WITNESS_SCALE_FACTOR;361const auto oversizedTransactionBaseSize = ::GetSerializeSize(TX_NO_WITNESS(createTransaction(maxTransactionSize))) - maxTransactionSize;363auto maxPayloadSize = maxTransactionSize - oversizedTransactionBaseSize;364{365TxValidationState state;366CheckTransaction(createTransaction(maxPayloadSize), state);367BOOST_CHECK(state.GetRejectReason() != "bad-txns-oversize");368}370maxPayloadSize += 1;371{372TxValidationState state;373BOOST_CHECK_MESSAGE(!CheckTransaction(createTransaction(maxPayloadSize), state), "Oversized transaction should be invalid");374BOOST_CHECK(state.GetRejectReason() == "bad-txns-oversize");375}376}378BOOST_AUTO_TEST_CASE(basic_transaction_tests)379{380// Random real transaction (e2769b09e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436)381unsigned char ch[] = {0x01, 0x00, 0x00, 0x00, 0x01, 0x6b, 0xff, 0x7f, 0xcd, 0x4f, 0x85, 0x65, 0xef, 0x40, 0x6d, 0xd5, 0xd6, 0x3d, 0x4f, 0xf9, 0x4f, 0x31, 0x8f, 0xe8, 0x20, 0x27, 0xfd, 0x4d, 0xc4, 0x51, 0xb0, 0x44, 0x74, 0x01, 0x9f, 0x74, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x49, 0x30, 0x46, 0x02, 0x21, 0x00, 0xda, 0x0d, 0xc6, 0xae, 0xce, 0xfe, 0x1e, 0x06, 0xef, 0xdf, 0x05, 0x77, 0x37, 0x57, 0xde, 0xb1, 0x68, 0x82, 0x09, 0x30, 0xe3, 0xb0, 0xd0, 0x3f, 0x46, 0xf5, 0xfc, 0xf1, 0x50, 0xbf, 0x99, 0x0c, 0x02, 0x21, 0x00, 0xd2, 0x5b, 0x5c, 0x87, 0x04, 0x00, 0x76, 0xe4, 0xf2, 0x53, 0xf8, 0x26, 0x2e, 0x76, 0x3e, 0x2d, 0xd5, 0x1e, 0x7f, 0xf0, 0xbe, 0x15, 0x77, 0x27, 0xc4, 0xbc, 0x42, 0x80, 0x7f, 0x17, 0xbd, 0x39, 0x01, 0x41, 0x04, 0xe6, 0xc2, 0x6e, 0xf6, 0x7d, 0xc6, 0x10, 0xd2, 0xcd, 0x19, 0x24, 0x84, 0x78, 0x9a, 0x6c, 0xf9, 0xae, 0xa9, 0x93, 0x0b, 0x94, 0x4b, 0x7e, 0x2d, 0xb5, 0x34, 0x2b, 0x9d, 0x9e, 0x5b, 0x9f, 0xf7, 0x9a, 0xff, 0x9a, 0x2e, 0xe1, 0x97, 0x8d, 0xd7, 0xfd, 0x01, 0xdf, 0xc5, 0x22, 0xee, 0x02, 0x28, 0x3d, 0x3b, 0x06, 0xa9, 0xd0, 0x3a, 0xcf, 0x80, 0x96, 0x96, 0x8d, 0x7d, 0xbb, 0x0f, 0x91, 0x78, 0xff, 0xff, 0xff, 0xff, 0x02, 0x8b, 0xa7, 0x94, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xba, 0xde, 0xec, 0xfd, 0xef, 0x05, 0x07, 0x24, 0x7f, 0xc8, 0xf7, 0x42, 0x41, 0xd7, 0x3b, 0xc0, 0x39, 0x97, 0x2d, 0x7b, 0x88, 0xac, 0x40, 0x94, 0xa8, 0x02, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xc1, 0x09, 0x32, 0x48, 0x3f, 0xec, 0x93, 0xed, 0x51, 0xf5, 0xfe, 0x95, 0xe7, 0x25, 0x59, 0xf2, 0xcc, 0x70, 0x43, 0xf9, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00};382std::vector<unsigned char> vch(ch, ch + sizeof(ch) -1);383CMutableTransaction tx;384SpanReader{vch} >> TX_WITH_WITNESS(tx);385TxValidationState state;386BOOST_CHECK_MESSAGE(CheckTransaction(CTransaction(tx), state), "Simple deserialized transaction should be valid.");387BOOST_CHECK_MESSAGE(state.IsValid(), "Simple deserialized transaction should be valid.");389// Check that duplicate txins fail390tx.vin.push_back(tx.vin[0]);391BOOST_CHECK_MESSAGE(!CheckTransaction(CTransaction(tx), state) || !state.IsValid(), "Transaction with duplicate txins should be invalid.");392}394BOOST_AUTO_TEST_CASE(test_Get)395{396FillableSigningProvider keystore;397CCoinsViewCache coins{&CoinsViewEmpty::Get()};398std::vector<CMutableTransaction> dummyTransactions =399SetupDummyInputs(keystore, coins, {11*CENT, 50*CENT, 21*CENT, 22*CENT});401CMutableTransaction t1;402t1.vin.resize(3);403t1.vin[0].prevout.hash = dummyTransactions[0].GetHash();404t1.vin[0].prevout.n = 1;405t1.vin[0].scriptSig << std::vector<unsigned char>(65, 0);406t1.vin[1].prevout.hash = dummyTransactions[1].GetHash();407t1.vin[1].prevout.n = 0;408t1.vin[1].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);409t1.vin[2].prevout.hash = dummyTransactions[1].GetHash();410t1.vin[2].prevout.n = 1;411t1.vin[2].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);412t1.vout.resize(2);413t1.vout[0].nValue = 90*CENT;414t1.vout[0].scriptPubKey << OP_1;416BOOST_CHECK(ValidateInputsStandardness(CTransaction(t1), coins).IsValid());417}419static void CreateCreditAndSpend(const FillableSigningProvider& keystore, const CScript& outscript, CTransactionRef& output, CMutableTransaction& input, bool success = true)420{421CMutableTransaction outputm;422outputm.version = 1;423outputm.vin.resize(1);424outputm.vin[0].prevout.SetNull();425outputm.vin[0].scriptSig = CScript();426outputm.vout.resize(1);427outputm.vout[0].nValue = 1;428outputm.vout[0].scriptPubKey = outscript;429DataStream ssout;430ssout << TX_WITH_WITNESS(outputm);431ssout >> TX_WITH_WITNESS(output);432assert(output->vin.size() == 1);433assert(output->vin[0] == outputm.vin[0]);434assert(output->vout.size() == 1);435assert(output->vout[0] == outputm.vout[0]);437CMutableTransaction inputm;438inputm.version = 1;439inputm.vin.resize(1);440inputm.vin[0].prevout.hash = output->GetHash();441inputm.vin[0].prevout.n = 0;442inputm.vout.resize(1);443inputm.vout[0].nValue = 1;444inputm.vout[0].scriptPubKey = CScript();445SignatureData empty;446bool ret = SignSignature(keystore, *output, inputm, 0, SIGHASH_ALL, empty);447assert(ret == success);448DataStream ssin;449ssin << TX_WITH_WITNESS(inputm);450ssin >> TX_WITH_WITNESS(input);451assert(input.vin.size() == 1);452assert(input.vin[0] == inputm.vin[0]);453assert(input.vout.size() == 1);454assert(input.vout[0] == inputm.vout[0]);455assert(input.vin[0].scriptWitness.stack == inputm.vin[0].scriptWitness.stack);456}458static void CheckWithFlag(const CTransactionRef& output, const CMutableTransaction& input, script_verify_flags flags, bool success)459{460ScriptError error;461CTransaction inputi(input);462bool ret = VerifyScript(inputi.vin[0].scriptSig, output->vout[0].scriptPubKey, &inputi.vin[0].scriptWitness, flags, TransactionSignatureChecker(&inputi, 0, output->vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &error);463assert(ret == success);464}466static CScript PushAll(const std::vector<valtype>& values)467{468CScript result;469for (const valtype& v : values) {470if (v.size() == 0) {471result << OP_0;472} else if (v.size() == 1 && v[0] >= 1 && v[0] <= 16) {473result << CScript::EncodeOP_N(v[0]);474} else if (v.size() == 1 && v[0] == 0x81) {475result << OP_1NEGATE;476} else {477result << v;478}479}480return result;481}483static void ReplaceRedeemScript(CScript& script, const CScript& redeemScript)484{485std::vector<valtype> stack;486EvalScript(stack, script, SCRIPT_VERIFY_STRICTENC, BaseSignatureChecker(), SigVersion::BASE);487assert(stack.size() > 0);488stack.back() = std::vector<unsigned char>(redeemScript.begin(), redeemScript.end());489script = PushAll(stack);490}492BOOST_AUTO_TEST_CASE(test_big_witness_transaction)493{494CMutableTransaction mtx;495mtx.version = 1;497CKey key = GenerateRandomKey(); // Need to use compressed keys in segwit or the signing will fail498FillableSigningProvider keystore;499BOOST_CHECK(keystore.AddKeyPubKey(key, key.GetPubKey()));500CKeyID hash = key.GetPubKey().GetID();
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.