Skip to content

Commit c5ee0cc

Browse files
author
MarcoFalke
committed
Merge bitcoin#21989: test: Use COINBASE_MATURITY in functional tests
bfa9309 Use COINBASE_MATURITY constant in functional tests. (Kiminuo) 525448d Move COINBASE_MATURITY from `feature_nulldummy` test to `blocktools`. (Kiminuo) Pull request description: `COINBASE_MATURITY` constant was added to `feature_nulldummy` test in bitcoin#21373. This PR moves the constant to `blocktools.py` file and uses the constant in more tests as suggested [here](bitcoin#21373 (comment)). Edit: Goal of this PR is to replace integer constants with `COINBASE_MATURITY` but not necessarily in *all* cases because that would mean to read and fully understand all tests. That's out of my time constraints. Any reports where `COINBASE_MATURITY` should be used are welcome though! ACKs for top commit: theStack: ACK bfa9309 🌇 Tree-SHA512: 01f04645f05a39028681f355cf3d42dd63ea3303f76d93c430e0fdce441934358a2d847a54e6068d61932f1b75e1d406f51859b057b3e4b569f7083915cb317f
2 parents d7a6bba + bfa9309 commit c5ee0cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+135
-68
lines changed

test/functional/feature_assumevalid.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"""
3232

3333
from test_framework.blocktools import (
34+
COINBASE_MATURITY,
3435
create_block,
3536
create_coinbase,
3637
)
@@ -161,8 +162,8 @@ def run_test(self):
161162

162163
# Send blocks to node0. Block 102 will be rejected.
163164
self.send_blocks_until_disconnected(p2p0)
164-
self.wait_until(lambda: self.nodes[0].getblockcount() >= 101)
165-
assert_equal(self.nodes[0].getblockcount(), 101)
165+
self.wait_until(lambda: self.nodes[0].getblockcount() >= COINBASE_MATURITY + 1)
166+
assert_equal(self.nodes[0].getblockcount(), COINBASE_MATURITY + 1)
166167

167168
# Send all blocks to node1. All blocks will be accepted.
168169
for i in range(2202):
@@ -173,8 +174,8 @@ def run_test(self):
173174

174175
# Send blocks to node2. Block 102 will be rejected.
175176
self.send_blocks_until_disconnected(p2p2)
176-
self.wait_until(lambda: self.nodes[2].getblockcount() >= 101)
177-
assert_equal(self.nodes[2].getblockcount(), 101)
177+
self.wait_until(lambda: self.nodes[2].getblockcount() >= COINBASE_MATURITY + 1)
178+
assert_equal(self.nodes[2].getblockcount(), COINBASE_MATURITY + 1)
178179

179180

180181
if __name__ == '__main__':

test/functional/feature_backwards_compatibility.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import os
2323
import shutil
2424

25+
from test_framework.blocktools import COINBASE_MATURITY
2526
from test_framework.test_framework import BitcoinTestFramework
2627
from test_framework.descriptors import descsum_create
2728

@@ -64,13 +65,13 @@ def setup_nodes(self):
6465
self.import_deterministic_coinbase_privkeys()
6566

6667
def run_test(self):
67-
self.nodes[0].generatetoaddress(101, self.nodes[0].getnewaddress())
68+
self.nodes[0].generatetoaddress(COINBASE_MATURITY + 1, self.nodes[0].getnewaddress())
6869

6970
self.sync_blocks()
7071

7172
# Sanity check the test framework:
7273
res = self.nodes[self.num_nodes - 1].getblockchaininfo()
73-
assert_equal(res['blocks'], 101)
74+
assert_equal(res['blocks'], COINBASE_MATURITY + 1)
7475

7576
node_master = self.nodes[self.num_nodes - 5]
7677
node_v19 = self.nodes[self.num_nodes - 4]

test/functional/feature_coinstatsindex.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from decimal import Decimal
1313

1414
from test_framework.blocktools import (
15+
COINBASE_MATURITY,
1516
create_block,
1617
create_coinbase,
1718
)
@@ -68,7 +69,7 @@ def _test_coin_stats_index(self):
6869
index_hash_options = ['none', 'muhash']
6970

7071
# Generate a normal transaction and mine it
71-
node.generate(101)
72+
node.generate(COINBASE_MATURITY + 1)
7273
address = self.nodes[0].get_deterministic_priv_key().address
7374
node.sendtoaddress(address=address, amount=10, subtractfeefromamount=True)
7475
node.generate(1)

test/functional/feature_loadblock.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import tempfile
1717
import urllib
1818

19+
from test_framework.blocktools import COINBASE_MATURITY
1920
from test_framework.test_framework import BitcoinTestFramework
2021
from test_framework.util import assert_equal
2122

@@ -28,7 +29,7 @@ def set_test_params(self):
2829

2930
def run_test(self):
3031
self.nodes[1].setnetworkactive(state=False)
31-
self.nodes[0].generate(100)
32+
self.nodes[0].generate(COINBASE_MATURITY)
3233

3334
# Parsing the url of our node to get settings for config file
3435
data_dir = self.nodes[0].datadir

test/functional/feature_nulldummy.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@
1414
"""
1515
import time
1616

17-
from test_framework.blocktools import NORMAL_GBT_REQUEST_PARAMS, create_block, create_transaction, add_witness_commitment
17+
from test_framework.blocktools import (
18+
COINBASE_MATURITY,
19+
NORMAL_GBT_REQUEST_PARAMS,
20+
add_witness_commitment,
21+
create_block,
22+
create_transaction,
23+
)
1824
from test_framework.messages import CTransaction
1925
from test_framework.script import CScript
2026
from test_framework.test_framework import BitcoinTestFramework
2127
from test_framework.util import assert_equal, assert_raises_rpc_error
2228

23-
COINBASE_MATURITY = 100
2429
NULLDUMMY_ERROR = "non-mandatory-script-verify-flag (Dummy CHECKMULTISIG argument must be zero)"
2530

2631
def trueDummy(tx):

test/functional/feature_rbf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from decimal import Decimal
88

9+
from test_framework.blocktools import COINBASE_MATURITY
910
from test_framework.messages import COIN, COutPoint, CTransaction, CTxIn, CTxOut
1011
from test_framework.script import CScript, OP_DROP
1112
from test_framework.test_framework import BitcoinTestFramework
@@ -27,7 +28,7 @@ def make_utxo(node, amount, confirmed=True, scriptPubKey=DUMMY_P2WPKH_SCRIPT):
2728
"""
2829
fee = 1*COIN
2930
while node.getbalance() < satoshi_round((amount + fee)/COIN):
30-
node.generate(100)
31+
node.generate(COINBASE_MATURITY)
3132

3233
new_addr = node.getnewaddress()
3334
txid = node.sendtoaddress(new_addr, satoshi_round((amount+fee)/COIN))

test/functional/feature_taproot.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Test Taproot softfork (BIPs 340-342)
66

77
from test_framework.blocktools import (
8+
COINBASE_MATURITY,
89
create_coinbase,
910
create_block,
1011
add_witness_commitment,
@@ -1440,7 +1441,7 @@ def test_spenders(self, node, spenders, input_counts):
14401441
def run_test(self):
14411442
# Post-taproot activation tests go first (pre-taproot tests' blocks are invalid post-taproot).
14421443
self.log.info("Post-activation tests...")
1443-
self.nodes[1].generate(101)
1444+
self.nodes[1].generate(COINBASE_MATURITY + 1)
14441445
self.test_spenders(self.nodes[1], spenders_taproot_active(), input_counts=[1, 2, 2, 2, 2, 3])
14451446

14461447
# Re-connect nodes in case they have been disconnected

test/functional/interface_bitcoin_cli.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"""Test bitcoin-cli"""
66

77
from decimal import Decimal
8+
9+
from test_framework.blocktools import COINBASE_MATURITY
810
from test_framework.test_framework import BitcoinTestFramework
911
from test_framework.util import (
1012
assert_equal,
@@ -16,7 +18,7 @@
1618
# The block reward of coinbaseoutput.nValue (50) BTC/block matures after
1719
# COINBASE_MATURITY (100) blocks. Therefore, after mining 101 blocks we expect
1820
# node 0 to have a balance of (BLOCKS - COINBASE_MATURITY) * 50 BTC/block.
19-
BLOCKS = 101
21+
BLOCKS = COINBASE_MATURITY + 1
2022
BALANCE = (BLOCKS - 100) * 50
2123

2224
JSON_PARSING_ERROR = 'error: Error parsing JSON: foo'

test/functional/mempool_compatibility.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import os
1717

18+
from test_framework.blocktools import COINBASE_MATURITY
1819
from test_framework.test_framework import BitcoinTestFramework
1920
from test_framework.wallet import MiniWallet
2021

@@ -41,7 +42,7 @@ def run_test(self):
4142
old_node, new_node = self.nodes
4243
new_wallet = MiniWallet(new_node)
4344
new_wallet.generate(1)
44-
new_node.generate(100)
45+
new_node.generate(COINBASE_MATURITY)
4546
# Sync the nodes to ensure old_node has the block that contains the coinbase that new_wallet will spend.
4647
# Otherwise, because coinbases are only valid in a block and not as loose txns, if the nodes aren't synced
4748
# unbroadcasted_tx won't pass old_node's `MemPoolAccept::PreChecks`.

test/functional/mempool_expiry.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from datetime import timedelta
1414

15+
from test_framework.blocktools import COINBASE_MATURITY
1516
from test_framework.test_framework import BitcoinTestFramework
1617
from test_framework.util import (
1718
assert_equal,
@@ -36,7 +37,7 @@ def test_transaction_expiry(self, timeout):
3637

3738
# Add enough mature utxos to the wallet so that all txs spend confirmed coins.
3839
self.wallet.generate(4)
39-
node.generate(100)
40+
node.generate(COINBASE_MATURITY)
4041

4142
# Send a parent transaction that will expire.
4243
parent_txid = self.wallet.send_self_transfer(from_node=node)['txid']

test/functional/mempool_package_onemore.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from decimal import Decimal
1111

12+
from test_framework.blocktools import COINBASE_MATURITY
1213
from test_framework.test_framework import BitcoinTestFramework
1314
from test_framework.util import assert_equal, assert_raises_rpc_error, satoshi_round
1415

@@ -42,7 +43,7 @@ def chain_transaction(self, node, parent_txids, vouts, value, fee, num_outputs):
4243

4344
def run_test(self):
4445
# Mine some blocks and have them mature.
45-
self.nodes[0].generate(101)
46+
self.nodes[0].generate(COINBASE_MATURITY + 1)
4647
utxo = self.nodes[0].listunspent(10)
4748
txid = utxo[0]['txid']
4849
vout = utxo[0]['vout']

test/functional/mempool_packages.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from decimal import Decimal
88

9+
from test_framework.blocktools import COINBASE_MATURITY
910
from test_framework.messages import COIN
1011
from test_framework.p2p import P2PTxInvStore
1112
from test_framework.test_framework import BitcoinTestFramework
@@ -59,7 +60,7 @@ def chain_transaction(self, node, parent_txid, vout, value, fee, num_outputs):
5960
def run_test(self):
6061
# Mine some blocks and have them mature.
6162
peer_inv_store = self.nodes[0].add_p2p_connection(P2PTxInvStore()) # keep track of invs
62-
self.nodes[0].generate(101)
63+
self.nodes[0].generate(COINBASE_MATURITY + 1)
6364
utxo = self.nodes[0].listunspent(10)
6465
txid = utxo[0]['txid']
6566
vout = utxo[0]['vout']

test/functional/mempool_reorg.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
that spend (directly or indirectly) coinbase transactions.
99
"""
1010

11-
from test_framework.blocktools import create_raw_transaction
11+
from test_framework.blocktools import (
12+
COINBASE_MATURITY,
13+
create_raw_transaction,
14+
)
1215
from test_framework.test_framework import BitcoinTestFramework
1316
from test_framework.util import assert_equal, assert_raises_rpc_error
1417

@@ -44,7 +47,7 @@ def run_test(self):
4447
# 3. Indirect (coinbase and child both in chain) : spend_103 and spend_103_1
4548
# Use invalidatblock to make all of the above coinbase spends invalid (immature coinbase),
4649
# and make sure the mempool code behaves correctly.
47-
b = [self.nodes[0].getblockhash(n) for n in range(101, 105)]
50+
b = [self.nodes[0].getblockhash(n) for n in range(COINBASE_MATURITY + 1, COINBASE_MATURITY + 5)]
4851
coinbase_txids = [self.nodes[0].getblock(h)['tx'][0] for h in b]
4952
spend_101_raw = create_raw_transaction(self.nodes[0], coinbase_txids[1], node1_address, amount=49.99)
5053
spend_102_raw = create_raw_transaction(self.nodes[0], coinbase_txids[2], node0_address, amount=49.99)

test/functional/mempool_resurrect.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test resurrection of mined transactions when the blockchain is re-organized."""
66

7+
from test_framework.blocktools import COINBASE_MATURITY
78
from test_framework.test_framework import BitcoinTestFramework
89
from test_framework.util import assert_equal
910
from test_framework.wallet import MiniWallet
@@ -20,7 +21,7 @@ def run_test(self):
2021

2122
# Add enough mature utxos to the wallet so that all txs spend confirmed coins
2223
wallet.generate(3)
23-
node.generate(100)
24+
node.generate(COINBASE_MATURITY)
2425

2526
# Spend block 1/2/3's coinbase transactions
2627
# Mine a block

test/functional/mining_getblocktemplate_longpoll.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import random
99
import threading
1010

11+
from test_framework.blocktools import COINBASE_MATURITY
1112
from test_framework.test_framework import BitcoinTestFramework
1213
from test_framework.util import get_rpc_proxy
1314
from test_framework.wallet import MiniWallet
@@ -62,7 +63,7 @@ def run_test(self):
6263
assert not thr.is_alive()
6364

6465
# Add enough mature utxos to the wallets, so that all txs spend confirmed coins
65-
self.nodes[0].generate(100)
66+
self.nodes[0].generate(COINBASE_MATURITY)
6667
self.sync_blocks()
6768

6869
self.log.info("Test that introducing a new transaction into the mempool will terminate the longpoll")

test/functional/p2p_blocksonly.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import time
88

9+
from test_framework.blocktools import COINBASE_MATURITY
910
from test_framework.messages import msg_tx
1011
from test_framework.p2p import P2PInterface, P2PTxInvStore
1112
from test_framework.test_framework import BitcoinTestFramework
@@ -23,7 +24,7 @@ def run_test(self):
2324
self.miniwallet = MiniWallet(self.nodes[0])
2425
# Add enough mature utxos to the wallet, so that all txs spend confirmed coins
2526
self.miniwallet.generate(2)
26-
self.nodes[0].generate(100)
27+
self.nodes[0].generate(COINBASE_MATURITY)
2728

2829
self.blocksonly_mode_tests()
2930
self.blocks_relay_conn_tests()

test/functional/p2p_compactblocks.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
"""
1010
import random
1111

12-
from test_framework.blocktools import create_block, NORMAL_GBT_REQUEST_PARAMS, add_witness_commitment
12+
from test_framework.blocktools import (
13+
COINBASE_MATURITY,
14+
NORMAL_GBT_REQUEST_PARAMS,
15+
add_witness_commitment,
16+
create_block,
17+
)
1318
from test_framework.messages import BlockTransactions, BlockTransactionsRequest, calculate_shortid, CBlock, CBlockHeader, CInv, COutPoint, CTransaction, CTxIn, CTxInWitness, CTxOut, FromHex, HeaderAndShortIDs, msg_no_witness_block, msg_no_witness_blocktxn, msg_cmpctblock, msg_getblocktxn, msg_getdata, msg_getheaders, msg_headers, msg_inv, msg_sendcmpct, msg_sendheaders, msg_tx, msg_block, msg_blocktxn, MSG_BLOCK, MSG_CMPCT_BLOCK, MSG_WITNESS_FLAG, NODE_NETWORK, P2PHeaderAndShortIDs, PrefilledTransaction, ser_uint256, ToHex
1419
from test_framework.p2p import p2p_lock, P2PInterface
1520
from test_framework.script import CScript, OP_TRUE, OP_DROP
@@ -115,7 +120,7 @@ def make_utxos(self):
115120
block = self.build_block_on_tip(self.nodes[0])
116121
self.segwit_node.send_and_ping(msg_no_witness_block(block))
117122
assert int(self.nodes[0].getbestblockhash(), 16) == block.sha256
118-
self.nodes[0].generatetoaddress(100, self.nodes[0].getnewaddress(address_type="bech32"))
123+
self.nodes[0].generatetoaddress(COINBASE_MATURITY, self.nodes[0].getnewaddress(address_type="bech32"))
119124

120125
total_value = block.vtx[0].vout[0].nValue
121126
out_value = total_value // 10
@@ -226,7 +231,7 @@ def check_announcement_of_new_block(node, peer, predicate):
226231

227232
# This test actually causes bitcoind to (reasonably!) disconnect us, so do this last.
228233
def test_invalid_cmpctblock_message(self):
229-
self.nodes[0].generate(101)
234+
self.nodes[0].generate(COINBASE_MATURITY + 1)
230235
block = self.build_block_on_tip(self.nodes[0])
231236

232237
cmpct_block = P2PHeaderAndShortIDs()
@@ -244,7 +249,7 @@ def test_compactblock_construction(self, test_node, use_witness_address=True):
244249
version = test_node.cmpct_version
245250
node = self.nodes[0]
246251
# Generate a bunch of transactions.
247-
node.generate(101)
252+
node.generate(COINBASE_MATURITY + 1)
248253
num_transactions = 25
249254
address = node.getnewaddress()
250255

test/functional/p2p_eviction.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515

1616
import time
1717

18-
from test_framework.blocktools import create_block, create_coinbase
18+
from test_framework.blocktools import (
19+
COINBASE_MATURITY,
20+
create_block,
21+
create_coinbase,
22+
)
1923
from test_framework.messages import CTransaction, FromHex, msg_pong, msg_tx
2024
from test_framework.p2p import P2PDataStore, P2PInterface
2125
from test_framework.test_framework import BitcoinTestFramework
@@ -45,7 +49,7 @@ def run_test(self):
4549
protected_peers = set() # peers that we expect to be protected from eviction
4650
current_peer = -1
4751
node = self.nodes[0]
48-
node.generatetoaddress(101, node.get_deterministic_priv_key().address)
52+
node.generatetoaddress(COINBASE_MATURITY + 1, node.get_deterministic_priv_key().address)
4953

5054
self.log.info("Create 4 peers and protect them from eviction by sending us a block")
5155
for _ in range(4):

test/functional/p2p_feefilter.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from decimal import Decimal
88

9+
from test_framework.blocktools import COINBASE_MATURITY
910
from test_framework.messages import MSG_TX, MSG_WTX, msg_feefilter
1011
from test_framework.p2p import P2PInterface, p2p_lock
1112
from test_framework.test_framework import BitcoinTestFramework
@@ -81,7 +82,7 @@ def test_feefilter(self):
8182
miniwallet = MiniWallet(node1)
8283
# Add enough mature utxos to the wallet, so that all txs spend confirmed coins
8384
miniwallet.generate(5)
84-
node1.generate(100)
85+
node1.generate(COINBASE_MATURITY)
8586

8687
conn = self.nodes[0].add_p2p_connection(TestP2PConn())
8788

test/functional/p2p_leak_tx.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test that we don't leak txs to inbound peers that we haven't yet announced to"""
66

7+
from test_framework.blocktools import COINBASE_MATURITY
78
from test_framework.messages import msg_getdata, CInv, MSG_TX
89
from test_framework.p2p import p2p_lock, P2PDataStore
910
from test_framework.test_framework import BitcoinTestFramework
@@ -27,7 +28,7 @@ def run_test(self):
2728
miniwallet = MiniWallet(gen_node)
2829
# Add enough mature utxos to the wallet, so that all txs spend confirmed coins
2930
miniwallet.generate(1)
30-
gen_node.generate(100)
31+
gen_node.generate(COINBASE_MATURITY)
3132

3233
inbound_peer = self.nodes[0].add_p2p_connection(P2PNode()) # An "attacking" inbound peer
3334

0 commit comments

Comments
 (0)