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.
1#!/usr/bin/env python32# Copyright (c) 2016-present The Bitcoin Core developers3# Distributed under the MIT software license, see the accompanying4# file COPYING or http://www.opensource.org/licenses/mit-license.php.5"""Test NULLDUMMY softfork.7Connect to a single node.8Generate 2 blocks (save the coinbases for later).9Generate COINBASE_MATURITY (CB) more blocks to ensure the coinbases are mature.10[Policy/Consensus] Check that NULLDUMMY compliant transactions are accepted in block CB + 3.11[Policy] Check that non-NULLDUMMY transactions are rejected before activation.12[Consensus] Check that the new NULLDUMMY rules are not enforced on block CB + 4.13[Policy/Consensus] Check that the new NULLDUMMY rules are enforced on block CB + 5.14"""15import time17from test_framework.address import address_to_scriptpubkey18from test_framework.blocktools import (19COINBASE_MATURITY,20NORMAL_GBT_REQUEST_PARAMS,21add_witness_commitment,22create_block,23)24from test_framework.messages import (25CTransaction,26tx_from_hex,27)28from test_framework.script import (29OP_0,30OP_TRUE,31)32from test_framework.test_framework import BitcoinTestFramework33from test_framework.util import (34assert_equal,35assert_raises_rpc_error,36)37from test_framework.wallet import getnewdestination38from test_framework.wallet_util import generate_keypair40NULLDUMMY_TX_ERROR = "mempool-script-verify-flag-failed (Dummy CHECKMULTISIG argument must be zero)"41NULLDUMMY_BLK_ERROR = "block-script-verify-flag-failed (Dummy CHECKMULTISIG argument must be zero)"43def invalidate_nulldummy_tx(tx):44"""Transform a NULLDUMMY compliant tx (i.e. scriptSig starts with OP_0)45to be non-NULLDUMMY compliant by replacing the dummy with OP_TRUE"""46assert_equal(tx.vin[0].scriptSig[0], OP_0)47tx.vin[0].scriptSig = bytes([OP_TRUE]) + tx.vin[0].scriptSig[1:]50class NULLDUMMYTest(BitcoinTestFramework):51def set_test_params(self):52self.num_nodes = 153self.setup_clean_chain = True54# This script tests NULLDUMMY activation, which is part of the 'segwit' deployment, so we go through55# normal segwit activation here (and don't use the default always-on behaviour).56self.extra_args = [[57f'-testactivationheight=segwit@{COINBASE_MATURITY + 5}',58'-addresstype=legacy',59]]61def create_transaction(self, *, txid, input_details=None, addr, amount, privkey):62input = {"txid": txid, "vout": 0}63output = {addr: amount}64rawtx = self.nodes[0].createrawtransaction([input], output)65# Details only needed for scripthash or witness spends66input = None if not input_details else [{**input, **input_details}]67signedtx = self.nodes[0].signrawtransactionwithkey(rawtx, [privkey], input)68return tx_from_hex(signedtx["hex"])70def run_test(self):71self.privkey, self.pubkey = generate_keypair(wif=True)72cms = self.nodes[0].createmultisig(1, [self.pubkey.hex()])73wms = self.nodes[0].createmultisig(1, [self.pubkey.hex()], 'p2sh-segwit')74self.ms_address = cms["address"]75ms_unlock_details = {"scriptPubKey": address_to_scriptpubkey(self.ms_address).hex(),76"redeemScript": cms["redeemScript"]}77self.wit_ms_address = wms['address']79self.coinbase_blocks = self.generate(self.nodes[0], 2) # block height = 280coinbase_txid = []81for i in self.coinbase_blocks:82coinbase_txid.append(self.nodes[0].getblock(i)['tx'][0])83self.generate(self.nodes[0], COINBASE_MATURITY) # block height = COINBASE_MATURITY + 284self.lastblockhash = self.nodes[0].getbestblockhash()85self.lastblockheight = COINBASE_MATURITY + 286self.lastblocktime = int(time.time()) + self.lastblockheight88self.log.info(f"Test 1: NULLDUMMY compliant base transactions should be accepted to mempool and mined before activation [{COINBASE_MATURITY + 3}]")89test1txs = [self.create_transaction(txid=coinbase_txid[0], addr=self.ms_address, amount=49,90privkey=self.nodes[0].get_deterministic_priv_key().key)]91txid1 = self.nodes[0].sendrawtransaction(test1txs[0].serialize_with_witness().hex(), 0)92test1txs.append(self.create_transaction(txid=txid1, input_details=ms_unlock_details,93addr=self.ms_address, amount=48,94privkey=self.privkey))95txid2 = self.nodes[0].sendrawtransaction(test1txs[1].serialize_with_witness().hex(), 0)96test1txs.append(self.create_transaction(txid=coinbase_txid[1],97addr=self.wit_ms_address, amount=49,98privkey=self.nodes[0].get_deterministic_priv_key().key))99txid3 = self.nodes[0].sendrawtransaction(test1txs[2].serialize_with_witness().hex(), 0)100self.block_submit(self.nodes[0], test1txs, accept=True)102self.log.info("Test 2: Non-NULLDUMMY base multisig transaction should not be accepted to mempool before activation")103test2tx = self.create_transaction(txid=txid2, input_details=ms_unlock_details,104addr=self.ms_address, amount=47,105privkey=self.privkey)106invalidate_nulldummy_tx(test2tx)107assert_raises_rpc_error(-26, NULLDUMMY_TX_ERROR, self.nodes[0].sendrawtransaction, test2tx.serialize_with_witness().hex(), 0)109self.log.info(f"Test 3: Non-NULLDUMMY base transactions should be accepted in a block before activation [{COINBASE_MATURITY + 4}]")110self.block_submit(self.nodes[0], [test2tx], accept=True)112self.log.info("Test 4: Non-NULLDUMMY base multisig transaction is invalid after activation")113test4tx = self.create_transaction(txid=test2tx.txid_hex, input_details=ms_unlock_details,114addr=getnewdestination()[2], amount=46,115privkey=self.privkey)116test6txs = [CTransaction(test4tx)]117invalidate_nulldummy_tx(test4tx)118assert_raises_rpc_error(-26, NULLDUMMY_TX_ERROR, self.nodes[0].sendrawtransaction, test4tx.serialize_with_witness().hex(), 0)119self.block_submit(self.nodes[0], [test4tx], accept=False)121self.log.info("Test 5: Non-NULLDUMMY P2WSH multisig transaction invalid after activation")122test5tx = self.create_transaction(txid=txid3, input_details={"scriptPubKey": test1txs[2].vout[0].scriptPubKey.hex(),123"amount": 49, "witnessScript": wms["redeemScript"]},124addr=getnewdestination(address_type='p2sh-segwit')[2], amount=48,125privkey=self.privkey)126test6txs.append(CTransaction(test5tx))127test5tx.wit.vtxinwit[0].scriptWitness.stack[0] = b'\x01'128assert_raises_rpc_error(-26, NULLDUMMY_TX_ERROR, self.nodes[0].sendrawtransaction, test5tx.serialize_with_witness().hex(), 0)129self.block_submit(self.nodes[0], [test5tx], with_witness=True, accept=False)131self.log.info(f"Test 6: NULLDUMMY compliant base/witness transactions should be accepted to mempool and in block after activation [{COINBASE_MATURITY + 5}]")132for i in test6txs:133self.nodes[0].sendrawtransaction(i.serialize_with_witness().hex(), 0)134self.block_submit(self.nodes[0], test6txs, with_witness=True, accept=True)136def block_submit(self, node, txs, *, with_witness=False, accept):137tmpl = node.getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)138assert_equal(tmpl['previousblockhash'], self.lastblockhash)139assert_equal(tmpl['height'], self.lastblockheight + 1)140block = create_block(tmpl=tmpl, ntime=self.lastblocktime + 1, txlist=txs)141if with_witness:142add_witness_commitment(block)143block.solve()144assert_equal(None if accept else NULLDUMMY_BLK_ERROR, node.submitblock(block.serialize().hex()))145if accept:146assert_equal(node.getbestblockhash(), block.hash_hex)147self.lastblockhash = block.hash_hex148self.lastblocktime += 1149self.lastblockheight += 1150else:151assert_equal(node.getbestblockhash(), self.lastblockhash)154if __name__ == '__main__':155NULLDUMMYTest(__file__).main()
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.