|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2020-2021 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | +"""Test datacarrier functionality""" |
| 6 | +from test_framework.messages import ( |
| 7 | + CTxOut, |
| 8 | + MAX_OP_RETURN_RELAY, |
| 9 | +) |
| 10 | +from test_framework.script import ( |
| 11 | + CScript, |
| 12 | + OP_RETURN, |
| 13 | +) |
| 14 | +from test_framework.test_framework import BitcoinTestFramework |
| 15 | +from test_framework.test_node import TestNode |
| 16 | +from test_framework.util import ( |
| 17 | + assert_raises_rpc_error, |
| 18 | + random_bytes, |
| 19 | +) |
| 20 | +from test_framework.wallet import MiniWallet |
| 21 | + |
| 22 | + |
| 23 | +class DataCarrierTest(BitcoinTestFramework): |
| 24 | + def set_test_params(self): |
| 25 | + self.num_nodes = 3 |
| 26 | + self.extra_args = [ |
| 27 | + [], |
| 28 | + ["-datacarrier=0"], |
| 29 | + ["-datacarrier=1", f"-datacarriersize={MAX_OP_RETURN_RELAY - 1}"] |
| 30 | + ] |
| 31 | + |
| 32 | + def test_null_data_transaction(self, node: TestNode, data: bytes, success: bool) -> None: |
| 33 | + tx = self.wallet.create_self_transfer(fee_rate=0)["tx"] |
| 34 | + tx.vout.append(CTxOut(nValue=0, scriptPubKey=CScript([OP_RETURN, data]))) |
| 35 | + tx.vout[0].nValue -= tx.get_vsize() # simply pay 1sat/vbyte fee |
| 36 | + |
| 37 | + tx_hex = tx.serialize().hex() |
| 38 | + |
| 39 | + if success: |
| 40 | + self.wallet.sendrawtransaction(from_node=node, tx_hex=tx_hex) |
| 41 | + assert tx.rehash() in node.getrawmempool(True), f'{tx_hex} not in mempool' |
| 42 | + else: |
| 43 | + assert_raises_rpc_error(-26, "scriptpubkey", self.wallet.sendrawtransaction, from_node=node, tx_hex=tx_hex) |
| 44 | + |
| 45 | + def run_test(self): |
| 46 | + self.wallet = MiniWallet(self.nodes[0]) |
| 47 | + self.wallet.rescan_utxos() |
| 48 | + |
| 49 | + # By default, only 80 bytes are used for data (+1 for OP_RETURN, +2 for the pushdata opcodes). |
| 50 | + default_size_data = random_bytes(MAX_OP_RETURN_RELAY - 3) |
| 51 | + too_long_data = random_bytes(MAX_OP_RETURN_RELAY - 2) |
| 52 | + small_data = random_bytes(MAX_OP_RETURN_RELAY - 4) |
| 53 | + |
| 54 | + self.log.info("Testing null data transaction with default -datacarrier and -datacarriersize values.") |
| 55 | + self.test_null_data_transaction(node=self.nodes[0], data=default_size_data, success=True) |
| 56 | + |
| 57 | + self.log.info("Testing a null data transaction larger than allowed by the default -datacarriersize value.") |
| 58 | + self.test_null_data_transaction(node=self.nodes[0], data=too_long_data, success=False) |
| 59 | + |
| 60 | + self.log.info("Testing a null data transaction with -datacarrier=false.") |
| 61 | + self.test_null_data_transaction(node=self.nodes[1], data=default_size_data, success=False) |
| 62 | + |
| 63 | + self.log.info("Testing a null data transaction with a size larger than accepted by -datacarriersize.") |
| 64 | + self.test_null_data_transaction(node=self.nodes[2], data=default_size_data, success=False) |
| 65 | + |
| 66 | + self.log.info("Testing a null data transaction with a size smaller than accepted by -datacarriersize.") |
| 67 | + self.test_null_data_transaction(node=self.nodes[2], data=small_data, success=True) |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == '__main__': |
| 71 | + DataCarrierTest().main() |
0 commit comments