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) 2020-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 datacarrier functionality"""6from test_framework.messages import (7CTxOut,8MAX_OP_RETURN_RELAY,9)10from test_framework.script import (11CScript,12OP_RETURN,13)14from test_framework.test_framework import BitcoinTestFramework15from test_framework.test_node import TestNode16from test_framework.util import (17assert_equal,18assert_raises_rpc_error,19)20from test_framework.wallet import MiniWallet22from random import randbytes24# The historical maximum, now used to test coverage25CUSTOM_DATACARRIER_ARG = 8327class DataCarrierTest(BitcoinTestFramework):28def set_test_params(self):29self.num_nodes = 430self.extra_args = [31[], # default is uncapped32["-datacarrier=0"], # no relay of datacarrier33["-datacarrier=1", f"-datacarriersize={CUSTOM_DATACARRIER_ARG}"],34["-datacarrier=1", "-datacarriersize=2"],35]37def test_null_data_transaction(self, node: TestNode, data, success: bool) -> None:38tx = self.wallet.create_self_transfer(fee_rate=0)["tx"]39data = [] if data is None else [data]40tx.vout.append(CTxOut(nValue=0, scriptPubKey=CScript([OP_RETURN] + data)))41tx.vout[0].nValue -= tx.get_vsize() # simply pay 1sat/vbyte fee43tx_hex = tx.serialize().hex()45if success:46self.wallet.sendrawtransaction(from_node=node, tx_hex=tx_hex)47assert tx.txid_hex in node.getrawmempool(True), f'{tx_hex} not in mempool'48else:49assert_raises_rpc_error(-26, "datacarrier", self.wallet.sendrawtransaction, from_node=node, tx_hex=tx_hex)51def run_test(self):52self.wallet = MiniWallet(self.nodes[0])54# Test that bare multisig is allowed by default. Do it here rather than create a new test for it.55assert_equal(self.nodes[0].getmempoolinfo()["permitbaremultisig"], True)57assert_equal(self.nodes[0].getmempoolinfo()["maxdatacarriersize"], MAX_OP_RETURN_RELAY)58assert_equal(self.nodes[1].getmempoolinfo()["maxdatacarriersize"], 0)59assert_equal(self.nodes[2].getmempoolinfo()["maxdatacarriersize"], CUSTOM_DATACARRIER_ARG)60assert_equal(self.nodes[3].getmempoolinfo()["maxdatacarriersize"], 2)62# By default, any size is allowed.64# If it is custom set to 83, the historical value,65# only 80 bytes are used for data (+1 for OP_RETURN, +2 for the pushdata opcodes).66custom_size_data = randbytes(CUSTOM_DATACARRIER_ARG - 3)67too_long_data = randbytes(CUSTOM_DATACARRIER_ARG - 2)68extremely_long_data = randbytes(MAX_OP_RETURN_RELAY - 200)69one_byte = randbytes(1)70zero_bytes = randbytes(0)72self.log.info("Testing a null data transaction succeeds for default arg regardless of size.")73self.test_null_data_transaction(node=self.nodes[0], data=too_long_data, success=True)74self.test_null_data_transaction(node=self.nodes[0], data=extremely_long_data, success=True)76self.log.info("Testing a null data transaction with -datacarrier=false.")77self.test_null_data_transaction(node=self.nodes[1], data=custom_size_data, success=False)79self.log.info("Testing a null data transaction with a size larger than accepted by -datacarriersize.")80self.test_null_data_transaction(node=self.nodes[2], data=too_long_data, success=False)82self.log.info("Testing a null data transaction with a size equal to -datacarriersize.")83self.test_null_data_transaction(node=self.nodes[2], data=custom_size_data, success=True)85self.log.info("Testing a null data transaction with no data.")86self.test_null_data_transaction(node=self.nodes[0], data=None, success=True)87self.test_null_data_transaction(node=self.nodes[1], data=None, success=False)88self.test_null_data_transaction(node=self.nodes[2], data=None, success=True)89self.test_null_data_transaction(node=self.nodes[3], data=None, success=True)91self.log.info("Testing a null data transaction with zero bytes of data.")92self.test_null_data_transaction(node=self.nodes[0], data=zero_bytes, success=True)93self.test_null_data_transaction(node=self.nodes[1], data=zero_bytes, success=False)94self.test_null_data_transaction(node=self.nodes[2], data=zero_bytes, success=True)95self.test_null_data_transaction(node=self.nodes[3], data=zero_bytes, success=True)97self.log.info("Testing a null data transaction with one byte of data.")98self.test_null_data_transaction(node=self.nodes[0], data=one_byte, success=True)99self.test_null_data_transaction(node=self.nodes[1], data=one_byte, success=False)100self.test_null_data_transaction(node=self.nodes[2], data=one_byte, success=True)101self.test_null_data_transaction(node=self.nodes[3], data=one_byte, success=False)103if __name__ == '__main__':104DataCarrierTest(__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.