From 67a2c966862dec59685d1c62d0a82c404df4f9fe Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Mon, 29 Sep 2025 13:49:16 -0400 Subject: [PATCH 01/23] chainparams: encapsulate deployment configuration logic This encapsulates the soft fork configuration logic as set by the `-testactivationheight` (for buried deployments) and `-vbparams` (for version bits deployments) options which for the moment are regtest-only, in order to make them available on other networks as well in the next commit. Can be reviewed using git's --color-moved option. --- src/chainparams.cpp | 54 ++++++++++++++++++---------------- src/kernel/chainparams.cpp | 59 +++++++++++++++++++++----------------- src/kernel/chainparams.h | 26 ++++++++++------- 3 files changed, 77 insertions(+), 62 deletions(-) diff --git a/src/chainparams.cpp b/src/chainparams.cpp index 4e640969aade..a6114838acac 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -42,30 +42,8 @@ static void HandleRenounceArgs(const ArgsManager& args, CChainParams::RenouncePa } } -void ReadSigNetArgs(const ArgsManager& args, CChainParams::SigNetOptions& options) -{ - if (!args.GetArgs("-signetseednode").empty()) { - options.seeds.emplace(args.GetArgs("-signetseednode")); - } - if (!args.GetArgs("-signetchallenge").empty()) { - const auto signet_challenge = args.GetArgs("-signetchallenge"); - if (signet_challenge.size() != 1) { - throw std::runtime_error("-signetchallenge cannot be multiple values."); - } - const auto val{TryParseHex(signet_challenge[0])}; - if (!val) { - throw std::runtime_error(strprintf("-signetchallenge must be hex, not '%s'.", signet_challenge[0])); - } - options.challenge.emplace(*val); - } - HandleRenounceArgs(args, options.renounce); -} - -void ReadRegTestArgs(const ArgsManager& args, CChainParams::RegTestOptions& options) +static void HandleDeploymentArgs(const ArgsManager& args, CChainParams::DeploymentOptions& options) { - if (auto value = args.GetBoolArg("-fastprune")) options.fastprune = *value; - if (HasTestOption(args, "bip94")) options.enforce_bip94 = true; - for (const std::string& arg : args.GetArgs("-testactivationheight")) { const auto found{arg.find('@')}; if (found == std::string::npos) { @@ -86,8 +64,6 @@ void ReadRegTestArgs(const ArgsManager& args, CChainParams::RegTestOptions& opti } } - HandleRenounceArgs(args, options.renounce); - for (const std::string& strDeployment : args.GetArgs("-vbparams")) { std::vector vDeploymentParams = SplitString(strDeployment, ':'); if (vDeploymentParams.size() != 3) { @@ -115,6 +91,34 @@ void ReadRegTestArgs(const ArgsManager& args, CChainParams::RegTestOptions& opti } } +void ReadSigNetArgs(const ArgsManager& args, CChainParams::SigNetOptions& options) +{ + if (!args.GetArgs("-signetseednode").empty()) { + options.seeds.emplace(args.GetArgs("-signetseednode")); + } + if (!args.GetArgs("-signetchallenge").empty()) { + const auto signet_challenge = args.GetArgs("-signetchallenge"); + if (signet_challenge.size() != 1) { + throw std::runtime_error("-signetchallenge cannot be multiple values."); + } + const auto val{TryParseHex(signet_challenge[0])}; + if (!val) { + throw std::runtime_error(strprintf("-signetchallenge must be hex, not '%s'.", signet_challenge[0])); + } + options.challenge.emplace(*val); + } + HandleRenounceArgs(args, options.renounce); +} + +void ReadRegTestArgs(const ArgsManager& args, CChainParams::RegTestOptions& options) +{ + if (auto value = args.GetBoolArg("-fastprune")) options.fastprune = *value; + if (HasTestOption(args, "bip94")) options.enforce_bip94 = true; + + HandleDeploymentArgs(args, options.dep_opts); + HandleRenounceArgs(args, options.renounce); +} + static std::unique_ptr globalChainParams; const CChainParams &Params() { diff --git a/src/kernel/chainparams.cpp b/src/kernel/chainparams.cpp index 090a49ef8480..8290678ac298 100644 --- a/src/kernel/chainparams.cpp +++ b/src/kernel/chainparams.cpp @@ -116,6 +116,37 @@ struct SetupDeployment }; } +void CChainParams::ApplyDeploymentOptions(const DeploymentOptions& opts) +{ + for (const auto& [dep, height] : opts.activation_heights) { + switch (dep) { + case Consensus::BuriedDeployment::DEPLOYMENT_TAPROOT: + consensus.TaprootHeight = int{height}; + break; + case Consensus::BuriedDeployment::DEPLOYMENT_SEGWIT: + consensus.SegwitHeight = int{height}; + break; + case Consensus::BuriedDeployment::DEPLOYMENT_HEIGHTINCB: + consensus.BIP34Height = int{height}; + break; + case Consensus::BuriedDeployment::DEPLOYMENT_DERSIG: + consensus.BIP66Height = int{height}; + break; + case Consensus::BuriedDeployment::DEPLOYMENT_CLTV: + consensus.BIP65Height = int{height}; + break; + case Consensus::BuriedDeployment::DEPLOYMENT_CSV: + consensus.CSVHeight = int{height}; + break; + } + } + + for (const auto& [deployment_pos, version_bits_params] : opts.version_bits_parameters) { + consensus.vDeployments[deployment_pos].nStartTime = version_bits_params.start_time; + consensus.vDeployments[deployment_pos].nTimeout = version_bits_params.timeout; + } +} + /** * Main network on which people trade goods and services. */ @@ -577,33 +608,7 @@ class CRegTestParams : public CChainParams m_assumed_blockchain_size = 0; m_assumed_chain_state_size = 0; - for (const auto& [dep, height] : opts.activation_heights) { - switch (dep) { - case Consensus::BuriedDeployment::DEPLOYMENT_TAPROOT: - consensus.TaprootHeight = int{height}; - break; - case Consensus::BuriedDeployment::DEPLOYMENT_SEGWIT: - consensus.SegwitHeight = int{height}; - break; - case Consensus::BuriedDeployment::DEPLOYMENT_HEIGHTINCB: - consensus.BIP34Height = int{height}; - break; - case Consensus::BuriedDeployment::DEPLOYMENT_DERSIG: - consensus.BIP66Height = int{height}; - break; - case Consensus::BuriedDeployment::DEPLOYMENT_CLTV: - consensus.BIP65Height = int{height}; - break; - case Consensus::BuriedDeployment::DEPLOYMENT_CSV: - consensus.CSVHeight = int{height}; - break; - } - } - - for (const auto& [deployment_pos, version_bits_params] : opts.version_bits_parameters) { - consensus.vDeployments[deployment_pos].nStartTime = version_bits_params.start_time; - consensus.vDeployments[deployment_pos].nTimeout = version_bits_params.timeout; - } + ApplyDeploymentOptions(opts.dep_opts); RenounceDeployments(opts.renounce, consensus.vDeployments); diff --git a/src/kernel/chainparams.h b/src/kernel/chainparams.h index 5bb223ba858c..d6f8667d8d07 100644 --- a/src/kernel/chainparams.h +++ b/src/kernel/chainparams.h @@ -138,6 +138,19 @@ class CChainParams using RenounceParameters = std::vector; + /** + * VersionBitsParameters holds activation parameters + */ + struct VersionBitsParameters { + int64_t start_time; + int64_t timeout; + }; + + struct DeploymentOptions { + std::unordered_map version_bits_parameters{}; + std::unordered_map activation_heights{}; + }; + /** * SigNetOptions holds configurations for creating a signet CChainParams. */ @@ -147,20 +160,11 @@ class CChainParams RenounceParameters renounce{}; }; - /** - * VersionBitsParameters holds activation parameters - */ - struct VersionBitsParameters { - int64_t start_time; - int64_t timeout; - }; - /** * RegTestOptions holds configurations for creating a regtest CChainParams. */ struct RegTestOptions { - std::unordered_map version_bits_parameters{}; - std::unordered_map activation_heights{}; + DeploymentOptions dep_opts{}; RenounceParameters renounce{}; bool fastprune{false}; bool enforce_bip94{false}; @@ -192,6 +196,8 @@ class CChainParams CCheckpointData checkpointData; std::vector m_assumeutxo_data; ChainTxData chainTxData; + + void ApplyDeploymentOptions(const DeploymentOptions& opts); }; std::optional GetNetworkForMagic(const MessageStartChars& pchMessageStart); From 6665296b5f85e971f682842368022a1ca528a908 Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Mon, 29 Sep 2025 13:56:32 -0400 Subject: [PATCH 02/23] chainparams: make deployment configuration options always available in unit tests This allows unit tests to set `-testactivationheight` and `-vbparams` on all networks instead of exclusively on regtest. Those are kept test-network-only when used as startup parameters. --- src/chainparams.cpp | 31 +++++++++++++++++++++++++------ src/chainparamsbase.cpp | 4 ++-- src/init.cpp | 10 ++++++++++ src/kernel/chainparams.cpp | 26 +++++++++++++++++--------- src/kernel/chainparams.h | 18 +++++++++++++++--- 5 files changed, 69 insertions(+), 20 deletions(-) diff --git a/src/chainparams.cpp b/src/chainparams.cpp index a6114838acac..1eda11779aa5 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -91,6 +91,16 @@ static void HandleDeploymentArgs(const ArgsManager& args, CChainParams::Deployme } } +void ReadMainNetArgs(const ArgsManager& args, CChainParams::MainNetOptions& options) +{ + HandleDeploymentArgs(args, options.dep_opts); +} + +void ReadTestNetArgs(const ArgsManager& args, CChainParams::TestNetOptions& options) +{ + HandleDeploymentArgs(args, options.dep_opts); +} + void ReadSigNetArgs(const ArgsManager& args, CChainParams::SigNetOptions& options) { if (!args.GetArgs("-signetseednode").empty()) { @@ -129,12 +139,21 @@ const CChainParams &Params() { std::unique_ptr CreateChainParams(const ArgsManager& args, const ChainType chain) { switch (chain) { - case ChainType::MAIN: - return CChainParams::Main(); - case ChainType::TESTNET: - return CChainParams::TestNet(); - case ChainType::TESTNET4: - return CChainParams::TestNet4(); + case ChainType::MAIN: { + auto opts = CChainParams::MainNetOptions{}; + ReadMainNetArgs(args, opts); + return CChainParams::Main(opts); + } + case ChainType::TESTNET: { + auto opts = CChainParams::TestNetOptions{}; + ReadTestNetArgs(args, opts); + return CChainParams::TestNet(opts); + } + case ChainType::TESTNET4: { + auto opts = CChainParams::TestNetOptions{}; + ReadTestNetArgs(args, opts); + return CChainParams::TestNet4(opts); + } case ChainType::SIGNET: { auto opts = CChainParams::SigNetOptions{}; ReadSigNetArgs(args, opts); diff --git a/src/chainparamsbase.cpp b/src/chainparamsbase.cpp index aa03a3e20ccd..7ff623f1dcb4 100644 --- a/src/chainparamsbase.cpp +++ b/src/chainparamsbase.cpp @@ -16,10 +16,10 @@ void SetupChainParamsBaseOptions(ArgsManager& argsman) argsman.AddArg("-chain=", "Use the chain (default: main). Allowed values: " LIST_CHAIN_NAMES, ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS); argsman.AddArg("-regtest", "Enter regression test mode, which uses a special chain in which blocks can be solved instantly. " "This is intended for regression testing tools and app development. Equivalent to -chain=regtest.", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::CHAINPARAMS); - argsman.AddArg("-testactivationheight=name@height.", "Set the activation height of 'name' (segwit, bip34, dersig, cltv, csv). (regtest-only)", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST); + argsman.AddArg("-testactivationheight=name@height.", "Set the activation height of 'name' (segwit, bip34, dersig, cltv, csv). (test-only)", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST); argsman.AddArg("-testnet", "Use the testnet3 chain. Equivalent to -chain=test. Support for testnet3 is deprecated and will be removed in an upcoming release. Consider moving to testnet4 now by using -testnet4.", ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS); argsman.AddArg("-testnet4", "Use the testnet4 chain. Equivalent to -chain=testnet4.", ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS); - argsman.AddArg("-vbparams=deployment:start:end[:min_activation_height]", "Use given start/end times and min_activation_height for specified version bits deployment (regtest-only)", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::CHAINPARAMS); + argsman.AddArg("-vbparams=deployment:start:end[:min_activation_height]", "Use given start/end times and min_activation_height for specified version bits deployment (test-only)", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::CHAINPARAMS); argsman.AddArg("-renounce=deployment", "Unconditionally disable an heretical deployment attempt", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::CHAINPARAMS); argsman.AddArg("-signet", "Use the signet chain. Equivalent to -chain=signet. Note that the network is defined by the -signetchallenge parameter", ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS); argsman.AddArg("-signetchallenge", "Blocks must satisfy the given script to be considered valid (only for signet networks; defaults to the global default signet test network challenge)", ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_NEGATION, OptionsCategory::CHAINPARAMS); diff --git a/src/init.cpp b/src/init.cpp index e0093b424bcb..780c4839d7f8 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1343,6 +1343,16 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) const ArgsManager& args = *Assert(node.args); const CChainParams& chainparams = Params(); + // Prevent setting deployment parameters on mainnet. + if (chainparams.GetChainType() == ChainType::MAIN) { + if (args.IsArgSet("-testactivationheight")) { + return InitError(_("The -testactivationheight option may not be used on mainnet.")); + } + if (args.IsArgSet("-vbparams")) { + return InitError(_("The -vbparams option may not be used on mainnet.")); + } + } + // Disallow mainnet/testnet operation if (Params().GetChainType() == ChainType::MAIN || Params().GetChainType() == ChainType::TESTNET) { return InitError(Untranslated(strprintf("Selected network '%s' is unsupported for this client, select -regtest or -signet instead.\n", Params().GetChainTypeString()))); diff --git a/src/kernel/chainparams.cpp b/src/kernel/chainparams.cpp index 8290678ac298..235ceed40454 100644 --- a/src/kernel/chainparams.cpp +++ b/src/kernel/chainparams.cpp @@ -152,7 +152,7 @@ void CChainParams::ApplyDeploymentOptions(const DeploymentOptions& opts) */ class CMainParams : public CChainParams { public: - CMainParams() { + CMainParams(const MainNetOptions& opts) { m_chain_type = ChainType::MAIN; consensus.signet_blocks = false; consensus.signet_challenge.clear(); @@ -180,6 +180,8 @@ class CMainParams : public CChainParams { dep = SetupDeployment{.activate = 0x30000000, .abandon = 0, .never = true, .period = 2016}; } + ApplyDeploymentOptions(opts.dep_opts); + consensus.nMinimumChainWork = uint256{"0000000000000000000000000000000000000000b1f3b93b65b16d035a82be84"}; consensus.defaultAssumeValid = uint256{"00000000000000000001b658dd1120e82e66d2790811f89ede9742ada3ed6d77"}; // 886157 @@ -277,7 +279,7 @@ class CMainParams : public CChainParams { */ class CTestNetParams : public CChainParams { public: - CTestNetParams() { + CTestNetParams(const TestNetOptions& opts) { m_chain_type = ChainType::TESTNET; consensus.signet_blocks = false; consensus.signet_challenge.clear(); @@ -304,6 +306,8 @@ class CTestNetParams : public CChainParams { dep = SetupDeployment{.activate = 0x30000000, .abandon = 0, .never = true, .period = 2016}; } + ApplyDeploymentOptions(opts.dep_opts); + consensus.nMinimumChainWork = uint256{"0000000000000000000000000000000000000000000015f5e0c9f13455b0eb17"}; consensus.defaultAssumeValid = uint256{"00000000000003fc7967410ba2d0a8a8d50daedc318d43e8baf1a9782c236a57"}; // 3974606 @@ -372,7 +376,7 @@ class CTestNetParams : public CChainParams { */ class CTestNet4Params : public CChainParams { public: - CTestNet4Params() { + CTestNet4Params(const TestNetOptions& opts) { m_chain_type = ChainType::TESTNET4; consensus.signet_blocks = false; consensus.signet_challenge.clear(); @@ -396,6 +400,8 @@ class CTestNet4Params : public CChainParams { dep = SetupDeployment{.activate = 0x30000000, .abandon = 0, .never = true, .period = 2016}; } + ApplyDeploymentOptions(opts.dep_opts); + consensus.nMinimumChainWork = uint256{"0000000000000000000000000000000000000000000001d6dce8651b6094e4c1"}; consensus.defaultAssumeValid = uint256{"0000000000003ed4f08dbdf6f7d6b271a6bcffce25675cb40aa9fa43179a89f3"}; // 72600 @@ -526,6 +532,8 @@ class SigNetParams : public CChainParams { consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY] = SetupDeployment{.activate = 0x30000000, .abandon = 0, .never = true, .period = 432}; INQ_DEPLOYMENTS_SIGNET + ApplyDeploymentOptions(options.dep_opts); + RenounceDeployments(options.renounce, consensus.vDeployments); // message start is defined as the first 4 bytes of the sha256d of the block script @@ -688,19 +696,19 @@ std::unique_ptr CChainParams::RegTest(const RegTestOptions& return std::make_unique(options); } -std::unique_ptr CChainParams::Main() +std::unique_ptr CChainParams::Main(const MainNetOptions& options) { - return std::make_unique(); + return std::make_unique(options); } -std::unique_ptr CChainParams::TestNet() +std::unique_ptr CChainParams::TestNet(const TestNetOptions& options) { - return std::make_unique(); + return std::make_unique(options); } -std::unique_ptr CChainParams::TestNet4() +std::unique_ptr CChainParams::TestNet4(const TestNetOptions& options) { - return std::make_unique(); + return std::make_unique(options); } std::vector CChainParams::GetAvailableSnapshotHeights() const diff --git a/src/kernel/chainparams.h b/src/kernel/chainparams.h index d6f8667d8d07..6d22fc4dd1b6 100644 --- a/src/kernel/chainparams.h +++ b/src/kernel/chainparams.h @@ -155,6 +155,7 @@ class CChainParams * SigNetOptions holds configurations for creating a signet CChainParams. */ struct SigNetOptions { + DeploymentOptions dep_opts{}; std::optional> challenge{}; std::optional> seeds{}; RenounceParameters renounce{}; @@ -170,11 +171,22 @@ class CChainParams bool enforce_bip94{false}; }; + struct MainNetOptions { + DeploymentOptions dep_opts{}; + }; + + struct TestNetOptions { + DeploymentOptions dep_opts{}; + }; + static std::unique_ptr RegTest(const RegTestOptions& options); static std::unique_ptr SigNet(const SigNetOptions& options); - static std::unique_ptr Main(); - static std::unique_ptr TestNet(); - static std::unique_ptr TestNet4(); + static std::unique_ptr Main(const MainNetOptions& options); + static std::unique_ptr Main() { const MainNetOptions opts{}; return Main(opts); } + static std::unique_ptr TestNet(const TestNetOptions& options); + static std::unique_ptr TestNet() { const TestNetOptions opts{}; return TestNet(opts); } + static std::unique_ptr TestNet4(const TestNetOptions& options); + static std::unique_ptr TestNet4() { const TestNetOptions opts{}; return TestNet4(opts); } protected: CChainParams() = default; From 832bf232bf771d7e81c1e59764cd3f00941530ac Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Tue, 14 Oct 2025 16:22:42 -0400 Subject: [PATCH 03/23] ======= Consensus Cleanup BEGINS HERE ======= Prior commits are preparatory work. Following commits is the implementation of BIP54. From 337b5b71e966a4a5cf554d4d4bf52fb9267bef4f Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Thu, 4 Sep 2025 15:59:46 -0400 Subject: [PATCH 04/23] binana: add Consensus Cleanup --- src/binana/consensuscleanup.json | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/binana/consensuscleanup.json diff --git a/src/binana/consensuscleanup.json b/src/binana/consensuscleanup.json new file mode 100644 index 000000000000..24d10b9aaf31 --- /dev/null +++ b/src/binana/consensuscleanup.json @@ -0,0 +1,7 @@ +{ + "binana": [2025, 1, 0], + "deployment": "CONSENSUSCLEANUP", + "scriptverify": false, + "scriptverify_discourage": false, + "opcodes": {} +} From bba7033c7f2dfda36cfb778e41588b27cf0d29b6 Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Tue, 20 Jan 2026 16:38:25 -0500 Subject: [PATCH 05/23] scripted-diff: rename MAX_TX_LEGACY_SIGOPS to MAX_TX_BIP54_SIGOPS BIP54 counts sigops differently from existing sigops-based checks. Since we are overloading the sigops term, make clear the constant refers to BIP54-sigops, not other kinds of pre-existing sigops. -BEGIN VERIFY SCRIPT- sed -i 's/MAX_TX_LEGACY_SIGOPS/MAX_TX_BIP54_SIGOPS/g' $(git grep -l MAX_TX_LEGACY_SIGOPS src/) -END VERIFY SCRIPT- --- src/policy/policy.cpp | 2 +- src/policy/policy.h | 2 +- src/test/transaction_tests.cpp | 10 +++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/policy/policy.cpp b/src/policy/policy.cpp index 994e67978622..f36496676373 100644 --- a/src/policy/policy.cpp +++ b/src/policy/policy.cpp @@ -184,7 +184,7 @@ static bool CheckSigopsBIP54(const CTransaction& tx, const CCoinsViewCache& inpu sigops += txin.scriptSig.GetSigOpCount(/*fAccurate=*/true); sigops += prev_txo.scriptPubKey.GetSigOpCount(txin.scriptSig); - if (sigops > MAX_TX_LEGACY_SIGOPS) { + if (sigops > MAX_TX_BIP54_SIGOPS) { return false; } } diff --git a/src/policy/policy.h b/src/policy/policy.h index b93137e54aaa..e7163b484bd9 100644 --- a/src/policy/policy.h +++ b/src/policy/policy.h @@ -40,7 +40,7 @@ static constexpr unsigned int MAX_P2SH_SIGOPS{15}; /** The maximum number of sigops we're willing to relay/mine in a single tx */ static constexpr unsigned int MAX_STANDARD_TX_SIGOPS_COST{MAX_BLOCK_SIGOPS_COST/5}; /** The maximum number of potentially executed legacy signature operations in a single standard tx */ -static constexpr unsigned int MAX_TX_LEGACY_SIGOPS{2'500}; +static constexpr unsigned int MAX_TX_BIP54_SIGOPS{2'500}; /** Default for -incrementalrelayfee, which sets the minimum feerate increase for mempool limiting or replacement **/ static constexpr unsigned int DEFAULT_INCREMENTAL_RELAY_FEE{100}; /** Default for -bytespersigop */ diff --git a/src/test/transaction_tests.cpp b/src/test/transaction_tests.cpp index 5f4de9b141d1..21242f55165c 100644 --- a/src/test/transaction_tests.cpp +++ b/src/test/transaction_tests.cpp @@ -1035,7 +1035,7 @@ BOOST_AUTO_TEST_CASE(max_standard_legacy_sigops) // Create a transaction fanning out as many such P2SH outputs as is standard to spend in a // single transaction, and a transaction spending them. CMutableTransaction tx_create, tx_max_sigops; - const unsigned p2sh_inputs_count{MAX_TX_LEGACY_SIGOPS / MAX_P2SH_SIGOPS}; + const unsigned p2sh_inputs_count{MAX_TX_BIP54_SIGOPS / MAX_P2SH_SIGOPS}; tx_create.vout.reserve(p2sh_inputs_count); for (unsigned i{0}; i < p2sh_inputs_count; ++i) { tx_create.vout.emplace_back(424242 + i, max_sigops_p2sh); @@ -1047,7 +1047,7 @@ BOOST_AUTO_TEST_CASE(max_standard_legacy_sigops) } // p2sh_inputs_count is truncated to 166 (from 166.6666..) - BOOST_CHECK_LT(p2sh_inputs_count * MAX_P2SH_SIGOPS, MAX_TX_LEGACY_SIGOPS); + BOOST_CHECK_LT(p2sh_inputs_count * MAX_P2SH_SIGOPS, MAX_TX_BIP54_SIGOPS); AddCoins(coins, CTransaction(tx_create), 0, false); // 2490 sigops is below the limit. @@ -1062,7 +1062,7 @@ BOOST_AUTO_TEST_CASE(max_standard_legacy_sigops) } tx_max_sigops.vin.emplace_back(prev_txid, p2sh_inputs_count, CScript() << ToByteVector(max_sigops_redeem_script)); AddCoins(coins, CTransaction(tx_create), 0, false); - BOOST_CHECK_GT((p2sh_inputs_count + 1) * MAX_P2SH_SIGOPS, MAX_TX_LEGACY_SIGOPS); + BOOST_CHECK_GT((p2sh_inputs_count + 1) * MAX_P2SH_SIGOPS, MAX_TX_BIP54_SIGOPS); BOOST_CHECK_EQUAL(GetP2SHSigOpCount(CTransaction(tx_max_sigops), coins), 2505); BOOST_CHECK(!::AreInputsStandard(CTransaction(tx_max_sigops), coins)); @@ -1082,7 +1082,7 @@ BOOST_AUTO_TEST_CASE(max_standard_legacy_sigops) AddCoins(coins, CTransaction(tx_create_p2pk), 0, false); // The transaction now contains exactly 2500 sigops, the check should pass. - BOOST_CHECK_EQUAL(p2sh_inputs_count * MAX_P2SH_SIGOPS + p2pk_inputs_count * 1, MAX_TX_LEGACY_SIGOPS); + BOOST_CHECK_EQUAL(p2sh_inputs_count * MAX_P2SH_SIGOPS + p2pk_inputs_count * 1, MAX_TX_BIP54_SIGOPS); BOOST_CHECK(::AreInputsStandard(CTransaction(tx_max_sigops), coins)); // Now, add some Segwit inputs. We add one for each defined Segwit output type. The limit @@ -1110,7 +1110,7 @@ BOOST_AUTO_TEST_CASE(max_standard_legacy_sigops) tx_max_sigops.vin.emplace_back(prev_txid, i); } AddCoins(coins, CTransaction(tx_create_p2pk), 0, false); - BOOST_CHECK_GT(p2sh_inputs_count * MAX_P2SH_SIGOPS + p2pk_inputs_count * 1, MAX_TX_LEGACY_SIGOPS); + BOOST_CHECK_GT(p2sh_inputs_count * MAX_P2SH_SIGOPS + p2pk_inputs_count * 1, MAX_TX_BIP54_SIGOPS); BOOST_CHECK(!::AreInputsStandard(CTransaction(tx_max_sigops), coins)); } From 9a93ad6f1fb8dd768aca3b1cdd4a54693fe42b96 Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Thu, 4 Sep 2025 16:14:42 -0400 Subject: [PATCH 06/23] moveonly: move CheckSigopsBIP54 from policy to consensus Move the function that checks whether a transaction respects the BIP54 sigops rule to the consensus folder (along with the accompanying constant), as it will be made consensus-critical in the next commit. Can be reviewed with git's --color-moved option. --- src/CMakeLists.txt | 2 +- src/consensus/consensus.h | 3 +++ src/consensus/tx_verify.cpp | 26 ++++++++++++++++++++++++++ src/consensus/tx_verify.h | 5 +++++ src/policy/policy.cpp | 32 ++------------------------------ src/policy/policy.h | 2 -- 6 files changed, 37 insertions(+), 33 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fefc1b9dde41..865167bbced0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -158,6 +158,7 @@ add_library(bitcoin_common STATIC EXCLUDE_FROM_ALL common/system.cpp common/url.cpp compressor.cpp + consensus/tx_verify.cpp core_read.cpp core_write.cpp deploymentinfo.cpp @@ -233,7 +234,6 @@ add_library(bitcoin_node STATIC EXCLUDE_FROM_ALL bip324.cpp blockencodings.cpp blockfilter.cpp - consensus/tx_verify.cpp dbwrapper.cpp deploymentstatus.cpp flatfile.cpp diff --git a/src/consensus/consensus.h b/src/consensus/consensus.h index cffe9cdafd79..bb047cbfc52b 100644 --- a/src/consensus/consensus.h +++ b/src/consensus/consensus.h @@ -34,4 +34,7 @@ static constexpr unsigned int LOCKTIME_VERIFY_SEQUENCE = (1 << 0); */ static constexpr int64_t MAX_TIMEWARP = 600; +/** The maximum number of potentially executed legacy signature operations in a single tx */ +static constexpr unsigned int MAX_TX_BIP54_SIGOPS{2'500}; + #endif // BITCOIN_CONSENSUS_CONSENSUS_H diff --git a/src/consensus/tx_verify.cpp b/src/consensus/tx_verify.cpp index 9d09872597a2..c7744c12edac 100644 --- a/src/consensus/tx_verify.cpp +++ b/src/consensus/tx_verify.cpp @@ -161,6 +161,32 @@ int64_t GetTransactionSigOpCost(const CTransaction& tx, const CCoinsViewCache& i return nSigOps; } +bool Consensus::CheckSigopsBIP54(const CTransaction& tx, const CCoinsViewCache& inputs) +{ + Assert(!tx.IsCoinBase()); + + unsigned int sigops{0}; + for (const auto& txin: tx.vin) { + const auto& prev_txo{inputs.AccessCoin(txin.prevout).out}; + + // Unlike the existing block wide sigop limit which counts sigops present in the block + // itself (including the scriptPubKey which is not executed until spending later), BIP54 + // counts sigops in the block where they are potentially executed (only). + // This means sigops in the spent scriptPubKey count toward the limit. + // `fAccurate` means correctly accounting sigops for CHECKMULTISIGs(VERIFY) with 16 pubkeys + // or fewer. This method of accounting was introduced by BIP16, and BIP54 reuses it. + // The GetSigOpCount call on the previous scriptPubKey counts both bare and P2SH sigops. + sigops += txin.scriptSig.GetSigOpCount(/*fAccurate=*/true); + sigops += prev_txo.scriptPubKey.GetSigOpCount(txin.scriptSig); + + if (sigops > MAX_TX_BIP54_SIGOPS) { + return false; + } + } + + return true; +} + bool Consensus::CheckTxInputs(const CTransaction& tx, TxValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight, CAmount& txfee) { // are the actual inputs available? diff --git a/src/consensus/tx_verify.h b/src/consensus/tx_verify.h index 710d14dca13a..f387fed7fd6d 100644 --- a/src/consensus/tx_verify.h +++ b/src/consensus/tx_verify.h @@ -19,6 +19,11 @@ class TxValidationState; /** Transaction validation functions */ namespace Consensus { +/** + * Check the total number of non-witness sigops across the whole transaction, as per BIP54. + */ +bool CheckSigopsBIP54(const CTransaction& tx, const CCoinsViewCache& inputs); + /** * Check whether all inputs of this transaction are valid (no double spends and amounts) * This does not modify the UTXO set. This does not check scripts and sigs. diff --git a/src/policy/policy.cpp b/src/policy/policy.cpp index f36496676373..35c9f9cddf2d 100644 --- a/src/policy/policy.cpp +++ b/src/policy/policy.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -163,35 +164,6 @@ bool IsStandardTx(const CTransaction& tx, const std::optional& max_dat return true; } -/** - * Check the total number of non-witness sigops across the whole transaction, as per BIP54. - */ -static bool CheckSigopsBIP54(const CTransaction& tx, const CCoinsViewCache& inputs) -{ - Assert(!tx.IsCoinBase()); - - unsigned int sigops{0}; - for (const auto& txin: tx.vin) { - const auto& prev_txo{inputs.AccessCoin(txin.prevout).out}; - - // Unlike the existing block wide sigop limit which counts sigops present in the block - // itself (including the scriptPubKey which is not executed until spending later), BIP54 - // counts sigops in the block where they are potentially executed (only). - // This means sigops in the spent scriptPubKey count toward the limit. - // `fAccurate` means correctly accounting sigops for CHECKMULTISIGs(VERIFY) with 16 pubkeys - // or fewer. This method of accounting was introduced by BIP16, and BIP54 reuses it. - // The GetSigOpCount call on the previous scriptPubKey counts both bare and P2SH sigops. - sigops += txin.scriptSig.GetSigOpCount(/*fAccurate=*/true); - sigops += prev_txo.scriptPubKey.GetSigOpCount(txin.scriptSig); - - if (sigops > MAX_TX_BIP54_SIGOPS) { - return false; - } - } - - return true; -} - /** * Check transaction inputs to mitigate two * potential denial-of-service attacks: @@ -218,7 +190,7 @@ bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs) return true; // Coinbases don't use vin normally } - if (!CheckSigopsBIP54(tx, mapInputs)) { + if (!Consensus::CheckSigopsBIP54(tx, mapInputs)) { return false; } diff --git a/src/policy/policy.h b/src/policy/policy.h index e7163b484bd9..ff4a004d47b9 100644 --- a/src/policy/policy.h +++ b/src/policy/policy.h @@ -39,8 +39,6 @@ static constexpr unsigned int MIN_STANDARD_TX_NONWITNESS_SIZE{65}; static constexpr unsigned int MAX_P2SH_SIGOPS{15}; /** The maximum number of sigops we're willing to relay/mine in a single tx */ static constexpr unsigned int MAX_STANDARD_TX_SIGOPS_COST{MAX_BLOCK_SIGOPS_COST/5}; -/** The maximum number of potentially executed legacy signature operations in a single standard tx */ -static constexpr unsigned int MAX_TX_BIP54_SIGOPS{2'500}; /** Default for -incrementalrelayfee, which sets the minimum feerate increase for mempool limiting or replacement **/ static constexpr unsigned int DEFAULT_INCREMENTAL_RELAY_FEE{100}; /** Default for -bytespersigop */ From 0c95b4e325e20811515ee6c442f34f459f19090d Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Tue, 14 Oct 2025 16:20:07 -0400 Subject: [PATCH 07/23] validation: make BIP54 sigops check consensus-critical When BIP54 is active, make sure transaction in blocks do not violate the BIP54 limit on the number of potentially-executed legacy sigops. --- src/consensus/tx_verify.cpp | 6 +++++- src/consensus/tx_verify.h | 3 ++- src/policy/policy.cpp | 6 ------ src/test/fuzz/coins_view.cpp | 7 ++++++- src/test/transaction_tests.cpp | 19 ++++++++++++++----- src/txmempool.cpp | 2 +- src/validation.cpp | 5 +++-- test/functional/mempool_sigoplimit.py | 12 ++++++------ 8 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/consensus/tx_verify.cpp b/src/consensus/tx_verify.cpp index c7744c12edac..e4111a74e3ff 100644 --- a/src/consensus/tx_verify.cpp +++ b/src/consensus/tx_verify.cpp @@ -187,7 +187,7 @@ bool Consensus::CheckSigopsBIP54(const CTransaction& tx, const CCoinsViewCache& return true; } -bool Consensus::CheckTxInputs(const CTransaction& tx, TxValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight, CAmount& txfee) +bool Consensus::CheckTxInputs(const CTransaction& tx, TxValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight, CAmount& txfee, bool enforce_bip54) { // are the actual inputs available? if (!inputs.HaveInputs(tx)) { @@ -195,6 +195,10 @@ bool Consensus::CheckTxInputs(const CTransaction& tx, TxValidationState& state, strprintf("%s: inputs missing/spent", __func__)); } + if (enforce_bip54 && !Consensus::CheckSigopsBIP54(tx, inputs)) { + return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-legacy-sigops", "too many legacy sigops (BIP54)"); + } + CAmount nValueIn = 0; for (unsigned int i = 0; i < tx.vin.size(); ++i) { const COutPoint &prevout = tx.vin[i].prevout; diff --git a/src/consensus/tx_verify.h b/src/consensus/tx_verify.h index f387fed7fd6d..c568fcf43abc 100644 --- a/src/consensus/tx_verify.h +++ b/src/consensus/tx_verify.h @@ -28,9 +28,10 @@ bool CheckSigopsBIP54(const CTransaction& tx, const CCoinsViewCache& inputs); * Check whether all inputs of this transaction are valid (no double spends and amounts) * This does not modify the UTXO set. This does not check scripts and sigs. * @param[out] txfee Set to the transaction fee if successful. + * @param[in] enforce_bip54 Whether to perform the BIP54 sigops check. * Preconditions: tx.IsCoinBase() is false. */ -[[nodiscard]] bool CheckTxInputs(const CTransaction& tx, TxValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight, CAmount& txfee); +[[nodiscard]] bool CheckTxInputs(const CTransaction& tx, TxValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight, CAmount& txfee, bool enforce_bip54); } // namespace Consensus /** Auxiliary functions for transaction validation (ideally should not be exposed) */ diff --git a/src/policy/policy.cpp b/src/policy/policy.cpp index 35c9f9cddf2d..38a5c1c393c1 100644 --- a/src/policy/policy.cpp +++ b/src/policy/policy.cpp @@ -181,8 +181,6 @@ bool IsStandardTx(const CTransaction& tx, const std::optional& max_dat * DUP CHECKSIG DROP ... repeated 100 times... OP_1 * * Note that only the non-witness portion of the transaction is checked here. - * - * We also check the total number of non-witness sigops across the whole transaction, as per BIP54. */ bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs) { @@ -190,10 +188,6 @@ bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs) return true; // Coinbases don't use vin normally } - if (!Consensus::CheckSigopsBIP54(tx, mapInputs)) { - return false; - } - for (unsigned int i = 0; i < tx.vin.size(); i++) { const CTxOut& prev = mapInputs.AccessCoin(tx.vin[i].prevout).out; diff --git a/src/test/fuzz/coins_view.cpp b/src/test/fuzz/coins_view.cpp index c435ed91ba79..6ddc3fbf697e 100644 --- a/src/test/fuzz/coins_view.cpp +++ b/src/test/fuzz/coins_view.cpp @@ -255,7 +255,12 @@ FUZZ_TARGET(coins_view, .init = initialize_coins_view) // It is not allowed to call CheckTxInputs if CheckTransaction failed return; } - if (Consensus::CheckTxInputs(transaction, state, coins_view_cache, fuzzed_data_provider.ConsumeIntegralInRange(0, std::numeric_limits::max()), tx_fee_out)) { + if (transaction.IsCoinBase()) { + // It is not allowed to call CheckTxInputs on a coinbase transaction. + return; + } + const bool enforce_bip54{fuzzed_data_provider.ConsumeBool()}; + if (Consensus::CheckTxInputs(transaction, state, coins_view_cache, fuzzed_data_provider.ConsumeIntegralInRange(0, std::numeric_limits::max()), tx_fee_out, enforce_bip54)) { assert(MoneyRange(tx_fee_out)); } }, diff --git a/src/test/transaction_tests.cpp b/src/test/transaction_tests.cpp index 21242f55165c..7ce34b131607 100644 --- a/src/test/transaction_tests.cpp +++ b/src/test/transaction_tests.cpp @@ -1021,6 +1021,9 @@ BOOST_AUTO_TEST_CASE(test_IsStandard) BOOST_AUTO_TEST_CASE(max_standard_legacy_sigops) { + TxValidationState state; + const int dummy_height{0}; + CAmount dummy_fee; CCoinsView coins_dummy; CCoinsViewCache coins(&coins_dummy); CKey key; @@ -1052,7 +1055,8 @@ BOOST_AUTO_TEST_CASE(max_standard_legacy_sigops) // 2490 sigops is below the limit. BOOST_CHECK_EQUAL(GetP2SHSigOpCount(CTransaction(tx_max_sigops), coins), 2490); - BOOST_CHECK(::AreInputsStandard(CTransaction(tx_max_sigops), coins)); + BOOST_CHECK(Consensus::CheckTxInputs(CTransaction(tx_max_sigops), state, coins, dummy_height, dummy_fee, /*enforce_bip54=*/true)); + BOOST_CHECK(state.IsValid()); // Adding one more input will bump this to 2505, hitting the limit. tx_create.vout.emplace_back(424242, max_sigops_p2sh); @@ -1064,7 +1068,9 @@ BOOST_AUTO_TEST_CASE(max_standard_legacy_sigops) AddCoins(coins, CTransaction(tx_create), 0, false); BOOST_CHECK_GT((p2sh_inputs_count + 1) * MAX_P2SH_SIGOPS, MAX_TX_BIP54_SIGOPS); BOOST_CHECK_EQUAL(GetP2SHSigOpCount(CTransaction(tx_max_sigops), coins), 2505); - BOOST_CHECK(!::AreInputsStandard(CTransaction(tx_max_sigops), coins)); + BOOST_CHECK(!Consensus::CheckTxInputs(CTransaction(tx_max_sigops), state, coins, dummy_height, dummy_fee, /*enforce_bip54=*/true)); + BOOST_CHECK(state.IsInvalid()); + state = TxValidationState{}; // Now, check the limit can be reached with regular P2PK outputs too. Use a separate // preparation transaction, to demonstrate spending coins from a single tx is irrelevant. @@ -1083,7 +1089,8 @@ BOOST_AUTO_TEST_CASE(max_standard_legacy_sigops) // The transaction now contains exactly 2500 sigops, the check should pass. BOOST_CHECK_EQUAL(p2sh_inputs_count * MAX_P2SH_SIGOPS + p2pk_inputs_count * 1, MAX_TX_BIP54_SIGOPS); - BOOST_CHECK(::AreInputsStandard(CTransaction(tx_max_sigops), coins)); + BOOST_CHECK(Consensus::CheckTxInputs(CTransaction(tx_max_sigops), state, coins, dummy_height, dummy_fee, /*enforce_bip54=*/true)); + BOOST_CHECK(state.IsValid()); // Now, add some Segwit inputs. We add one for each defined Segwit output type. The limit // is exclusively on non-witness sigops and therefore those should not be counted. @@ -1099,7 +1106,8 @@ BOOST_AUTO_TEST_CASE(max_standard_legacy_sigops) // The transaction now still contains exactly 2500 sigops, the check should pass. AddCoins(coins, CTransaction(tx_create_segwit), 0, false); - BOOST_REQUIRE(::AreInputsStandard(CTransaction(tx_max_sigops), coins)); + BOOST_CHECK(Consensus::CheckTxInputs(CTransaction(tx_max_sigops), state, coins, dummy_height, dummy_fee, /*enforce_bip54=*/true)); + BOOST_CHECK(state.IsValid()); // Add one more P2PK input. We'll reach the limit. tx_create_p2pk.vout.emplace_back(212121, p2pk_script); @@ -1111,7 +1119,8 @@ BOOST_AUTO_TEST_CASE(max_standard_legacy_sigops) } AddCoins(coins, CTransaction(tx_create_p2pk), 0, false); BOOST_CHECK_GT(p2sh_inputs_count * MAX_P2SH_SIGOPS + p2pk_inputs_count * 1, MAX_TX_BIP54_SIGOPS); - BOOST_CHECK(!::AreInputsStandard(CTransaction(tx_max_sigops), coins)); + BOOST_CHECK(!Consensus::CheckTxInputs(CTransaction(tx_max_sigops), state, coins, dummy_height, dummy_fee, /*enforce_bip54=*/true)); + BOOST_CHECK(state.IsInvalid()); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/txmempool.cpp b/src/txmempool.cpp index 3a5a3fb306d3..59c4a7005b74 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -777,7 +777,7 @@ void CTxMemPool::check(const CCoinsViewCache& active_coins_tip, int64_t spendhei TxValidationState dummy_state; // Not used. CheckTxInputs() should always pass CAmount txfee = 0; assert(!tx.IsCoinBase()); - assert(Consensus::CheckTxInputs(tx, dummy_state, mempoolDuplicate, spendheight, txfee)); + assert(Consensus::CheckTxInputs(tx, dummy_state, mempoolDuplicate, spendheight, txfee, /*enforce_bip54=*/true)); for (const auto& input: tx.vin) mempoolDuplicate.SpendCoin(input.prevout); AddCoins(mempoolDuplicate, tx, std::numeric_limits::max()); } diff --git a/src/validation.cpp b/src/validation.cpp index f40096066d91..ddf6fd0e9af4 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -872,7 +872,7 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws) } // The mempool holds txs for the next block, so pass height+1 to CheckTxInputs - if (!Consensus::CheckTxInputs(tx, state, m_view, m_active_chainstate.m_chain.Height() + 1, ws.m_base_fees)) { + if (!Consensus::CheckTxInputs(tx, state, m_view, m_active_chainstate.m_chain.Height() + 1, ws.m_base_fees, /*enforce_bip54=*/true)) { return false; // state filled in by CheckTxInputs } @@ -2625,6 +2625,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state, CCheckQueueControl control(fScriptChecks && parallel_script_checks ? &m_chainman.GetCheckQueue() : nullptr); std::vector txsdata(block.vtx.size()); + const bool enforce_bip54{DeploymentActiveAt(*pindex, m_chainman, Consensus::DEPLOYMENT_CONSENSUSCLEANUP)}; std::vector prevheights; CAmount nFees = 0; int nInputs = 0; @@ -2641,7 +2642,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state, { CAmount txfee = 0; TxValidationState tx_state; - if (!Consensus::CheckTxInputs(tx, tx_state, view, pindex->nHeight, txfee)) { + if (!Consensus::CheckTxInputs(tx, tx_state, view, pindex->nHeight, txfee, /*enforce_bip54=*/enforce_bip54)) { // Any transaction validation failure in ConnectBlock is a block consensus failure state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, tx_state.GetRejectReason(), diff --git a/test/functional/mempool_sigoplimit.py b/test/functional/mempool_sigoplimit.py index 98e825f1a29d..b98440f0a9a1 100755 --- a/test/functional/mempool_sigoplimit.py +++ b/test/functional/mempool_sigoplimit.py @@ -183,7 +183,7 @@ def create_bare_multisig_tx(utxo_to_spend=None): assert_greater_than(2000, tx_parent.get_weight() + tx_child.get_weight()) def test_legacy_sigops_stdness(self): - self.log.info("Test a transaction with too many legacy sigops in its inputs is non-standard.") + self.log.info("Test a transaction with too many legacy sigops in its inputs is invalid.") # Restart with the default settings self.restart_node(0) @@ -204,19 +204,19 @@ def test_legacy_sigops_stdness(self): outpoints.append(COutPoint(txid, res["sent_vout"])) self.generate(self.nodes[0], 1) - # Spending all these outputs at once accounts for 2505 legacy sigops and is non-standard. + # Spending all these outputs at once accounts for 2505 legacy sigops and is invalid. nonstd_tx = CTransaction() nonstd_tx.vin = [CTxIn(op, CScript([b"", packed_redeem_script])) for op in outpoints] nonstd_tx.vout = [CTxOut(0, CScript([OP_RETURN, b""]))] - assert_raises_rpc_error(-26, "bad-txns-nonstandard-inputs", self.nodes[0].sendrawtransaction, nonstd_tx.serialize().hex()) + assert_raises_rpc_error(-26, "bad-txns-legacy-sigops", self.nodes[0].sendrawtransaction, nonstd_tx.serialize().hex()) - # Spending one less accounts for 2490 legacy sigops and is standard. + # Spending one less accounts for 2490 legacy sigops and is valid and standard. std_tx = deepcopy(nonstd_tx) std_tx.vin.pop() self.nodes[0].sendrawtransaction(std_tx.serialize().hex()) - # Make sure the original, non-standard, transaction can be mined. - self.generateblock(self.nodes[0], output="raw(42)", transactions=[nonstd_tx.serialize().hex()]) + # The invalid transaction also cannot appear in a block. + assert_raises_rpc_error(-25, "bad-txns-legacy-sigops", self.generateblock, self.nodes[0], "raw(42)", [nonstd_tx.serialize().hex()]) def run_test(self): self.wallet = MiniWallet(self.nodes[0]) From e042fb3a7e1042571d1d0a223a7ea26555ea573a Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Tue, 16 Sep 2025 13:09:04 -0400 Subject: [PATCH 08/23] qa: add to utilities a version of SignSignature for Taproot inputs In Taproot the signature commits to the list of spent outputs. --- src/test/util/transaction_utils.cpp | 14 ++++++++++++++ src/test/util/transaction_utils.h | 3 +++ 2 files changed, 17 insertions(+) diff --git a/src/test/util/transaction_utils.cpp b/src/test/util/transaction_utils.cpp index a588e6194410..71a3d4b73622 100644 --- a/src/test/util/transaction_utils.cpp +++ b/src/test/util/transaction_utils.cpp @@ -91,6 +91,20 @@ void BulkTransaction(CMutableTransaction& tx, int32_t target_weight) assert(GetTransactionWeight(CTransaction(tx)) <= target_weight + 3); } +bool SignSignature(const SigningProvider &provider, const CScript& fromPubKey, CMutableTransaction& txTo, unsigned int nIn, + const CAmount& amount, std::vector&& spent_outputs, int nHashType, SignatureData& sig_data) +{ + assert(nIn < txTo.vin.size()); + + PrecomputedTransactionData txdata; + txdata.Init(txTo, std::forward>(spent_outputs), /*force=*/true); + MutableTransactionSignatureCreator creator(txTo, nIn, amount, &txdata, nHashType); + + bool ret = ProduceSignature(provider, creator, fromPubKey, sig_data); + UpdateInput(txTo.vin.at(nIn), sig_data); + return ret; +} + bool SignSignature(const SigningProvider &provider, const CScript& fromPubKey, CMutableTransaction& txTo, unsigned int nIn, const CAmount& amount, int nHashType, SignatureData& sig_data) { assert(nIn < txTo.vin.size()); diff --git a/src/test/util/transaction_utils.h b/src/test/util/transaction_utils.h index 4a18ab6ab49d..ddc5350852be 100644 --- a/src/test/util/transaction_utils.h +++ b/src/test/util/transaction_utils.h @@ -49,5 +49,8 @@ bool SignSignature(const SigningProvider &provider, const CScript& fromPubKey, C unsigned int nIn, const CAmount& amount, int nHashType, SignatureData& sig_data); bool SignSignature(const SigningProvider &provider, const CTransaction& txFrom, CMutableTransaction& txTo, unsigned int nIn, int nHashType, SignatureData& sig_data); +bool SignSignature(const SigningProvider &provider, const CScript& fromPubKey, CMutableTransaction& txTo, + unsigned int nIn, const CAmount& amount, std::vector&& spent_outputs, int nHashType, + SignatureData& sig_data); #endif // BITCOIN_TEST_UTIL_TRANSACTION_UTILS_H From 7cf3bcd5a1cd1e542a764d27d4e432350dda9bdf Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Mon, 22 Sep 2025 13:53:14 -0400 Subject: [PATCH 09/23] qa: extensive unit tests for BIP54 legacy sigops limit Test the newly introduced limit with various combinations of inputs and outputs types, historical transactions, and exercise some implementation-specific edge cases. Record each test case and optionally write them to disk as JSON to generate the BIP test vectors. --- src/test/CMakeLists.txt | 1 + src/test/bip54_tests.cpp | 1321 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 1322 insertions(+) create mode 100644 src/test/bip54_tests.cpp diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 3ec1a8a39b4b..0936eacc1097 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -18,6 +18,7 @@ add_executable(test_bitcoin bech32_tests.cpp bip32_tests.cpp bip324_tests.cpp + bip54_tests.cpp blockchain_tests.cpp blockencodings_tests.cpp blockfilter_index_tests.cpp diff --git a/src/test/bip54_tests.cpp b/src/test/bip54_tests.cpp new file mode 100644 index 000000000000..bbc41b44f93b --- /dev/null +++ b/src/test/bip54_tests.cpp @@ -0,0 +1,1321 @@ +// Copyright (c) The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +#include +#include +#include +#include +#include +#include +#include +#include