Skip to content

Commit 8b3d2bb

Browse files
w0xlttheStack
andcommitted
test: add tests for datacarrier and datacarriersize options
Co-authored-by: Sebastian Falbesoner <[email protected]>
1 parent 93999a5 commit 8b3d2bb

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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()

test/functional/test_framework/messages.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@
6868
DEFAULT_ANCESTOR_LIMIT = 25 # default max number of in-mempool ancestors
6969
DEFAULT_DESCENDANT_LIMIT = 25 # default max number of in-mempool descendants
7070

71+
# Default setting for -datacarriersize. 80 bytes of data, +1 for OP_RETURN, +2 for the pushdata opcodes.
72+
MAX_OP_RETURN_RELAY = 83
73+
7174

7275
def sha256(s):
7376
return hashlib.sha256(s).digest()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@
320320
'feature_unsupported_utxo_db.py',
321321
'feature_logging.py',
322322
'feature_anchors.py',
323+
'mempool_datacarrier.py',
323324
'feature_coinstatsindex.py',
324325
'wallet_orphanedreward.py',
325326
'wallet_timelock.py',

0 commit comments

Comments
 (0)