Skip to content

Commit

Permalink
Merge bitcoin#21989: test: Use COINBASE_MATURITY in functional tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
MarcoFalke committed May 31, 2021
2 parents d7a6bba + bfa9309 commit c5ee0cc
Show file tree
Hide file tree
Showing 47 changed files with 135 additions and 68 deletions.
9 changes: 5 additions & 4 deletions test/functional/feature_assumevalid.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"""

from test_framework.blocktools import (
COINBASE_MATURITY,
create_block,
create_coinbase,
)
Expand Down Expand Up @@ -161,8 +162,8 @@ def run_test(self):

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

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

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


if __name__ == '__main__':
Expand Down
5 changes: 3 additions & 2 deletions test/functional/feature_backwards_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import os
import shutil

from test_framework.blocktools import COINBASE_MATURITY
from test_framework.test_framework import BitcoinTestFramework
from test_framework.descriptors import descsum_create

Expand Down Expand Up @@ -64,13 +65,13 @@ def setup_nodes(self):
self.import_deterministic_coinbase_privkeys()

def run_test(self):
self.nodes[0].generatetoaddress(101, self.nodes[0].getnewaddress())
self.nodes[0].generatetoaddress(COINBASE_MATURITY + 1, self.nodes[0].getnewaddress())

self.sync_blocks()

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

node_master = self.nodes[self.num_nodes - 5]
node_v19 = self.nodes[self.num_nodes - 4]
Expand Down
3 changes: 2 additions & 1 deletion test/functional/feature_coinstatsindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from decimal import Decimal

from test_framework.blocktools import (
COINBASE_MATURITY,
create_block,
create_coinbase,
)
Expand Down Expand Up @@ -68,7 +69,7 @@ def _test_coin_stats_index(self):
index_hash_options = ['none', 'muhash']

# Generate a normal transaction and mine it
node.generate(101)
node.generate(COINBASE_MATURITY + 1)
address = self.nodes[0].get_deterministic_priv_key().address
node.sendtoaddress(address=address, amount=10, subtractfeefromamount=True)
node.generate(1)
Expand Down
3 changes: 2 additions & 1 deletion test/functional/feature_loadblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import tempfile
import urllib

from test_framework.blocktools import COINBASE_MATURITY
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal

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

def run_test(self):
self.nodes[1].setnetworkactive(state=False)
self.nodes[0].generate(100)
self.nodes[0].generate(COINBASE_MATURITY)

# Parsing the url of our node to get settings for config file
data_dir = self.nodes[0].datadir
Expand Down
9 changes: 7 additions & 2 deletions test/functional/feature_nulldummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@
"""
import time

from test_framework.blocktools import NORMAL_GBT_REQUEST_PARAMS, create_block, create_transaction, add_witness_commitment
from test_framework.blocktools import (
COINBASE_MATURITY,
NORMAL_GBT_REQUEST_PARAMS,
add_witness_commitment,
create_block,
create_transaction,
)
from test_framework.messages import CTransaction
from test_framework.script import CScript
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, assert_raises_rpc_error

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

def trueDummy(tx):
Expand Down
3 changes: 2 additions & 1 deletion test/functional/feature_rbf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from decimal import Decimal

from test_framework.blocktools import COINBASE_MATURITY
from test_framework.messages import COIN, COutPoint, CTransaction, CTxIn, CTxOut
from test_framework.script import CScript, OP_DROP
from test_framework.test_framework import BitcoinTestFramework
Expand All @@ -27,7 +28,7 @@ def make_utxo(node, amount, confirmed=True, scriptPubKey=DUMMY_P2WPKH_SCRIPT):
"""
fee = 1*COIN
while node.getbalance() < satoshi_round((amount + fee)/COIN):
node.generate(100)
node.generate(COINBASE_MATURITY)

new_addr = node.getnewaddress()
txid = node.sendtoaddress(new_addr, satoshi_round((amount+fee)/COIN))
Expand Down
3 changes: 2 additions & 1 deletion test/functional/feature_taproot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Test Taproot softfork (BIPs 340-342)

from test_framework.blocktools import (
COINBASE_MATURITY,
create_coinbase,
create_block,
add_witness_commitment,
Expand Down Expand Up @@ -1440,7 +1441,7 @@ def test_spenders(self, node, spenders, input_counts):
def run_test(self):
# Post-taproot activation tests go first (pre-taproot tests' blocks are invalid post-taproot).
self.log.info("Post-activation tests...")
self.nodes[1].generate(101)
self.nodes[1].generate(COINBASE_MATURITY + 1)
self.test_spenders(self.nodes[1], spenders_taproot_active(), input_counts=[1, 2, 2, 2, 2, 3])

# Re-connect nodes in case they have been disconnected
Expand Down
4 changes: 3 additions & 1 deletion test/functional/interface_bitcoin_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"""Test bitcoin-cli"""

from decimal import Decimal

from test_framework.blocktools import COINBASE_MATURITY
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_equal,
Expand All @@ -16,7 +18,7 @@
# The block reward of coinbaseoutput.nValue (50) BTC/block matures after
# COINBASE_MATURITY (100) blocks. Therefore, after mining 101 blocks we expect
# node 0 to have a balance of (BLOCKS - COINBASE_MATURITY) * 50 BTC/block.
BLOCKS = 101
BLOCKS = COINBASE_MATURITY + 1
BALANCE = (BLOCKS - 100) * 50

JSON_PARSING_ERROR = 'error: Error parsing JSON: foo'
Expand Down
3 changes: 2 additions & 1 deletion test/functional/mempool_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import os

from test_framework.blocktools import COINBASE_MATURITY
from test_framework.test_framework import BitcoinTestFramework
from test_framework.wallet import MiniWallet

Expand All @@ -41,7 +42,7 @@ def run_test(self):
old_node, new_node = self.nodes
new_wallet = MiniWallet(new_node)
new_wallet.generate(1)
new_node.generate(100)
new_node.generate(COINBASE_MATURITY)
# Sync the nodes to ensure old_node has the block that contains the coinbase that new_wallet will spend.
# Otherwise, because coinbases are only valid in a block and not as loose txns, if the nodes aren't synced
# unbroadcasted_tx won't pass old_node's `MemPoolAccept::PreChecks`.
Expand Down
3 changes: 2 additions & 1 deletion test/functional/mempool_expiry.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from datetime import timedelta

from test_framework.blocktools import COINBASE_MATURITY
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_equal,
Expand All @@ -36,7 +37,7 @@ def test_transaction_expiry(self, timeout):

# Add enough mature utxos to the wallet so that all txs spend confirmed coins.
self.wallet.generate(4)
node.generate(100)
node.generate(COINBASE_MATURITY)

# Send a parent transaction that will expire.
parent_txid = self.wallet.send_self_transfer(from_node=node)['txid']
Expand Down
3 changes: 2 additions & 1 deletion test/functional/mempool_package_onemore.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from decimal import Decimal

from test_framework.blocktools import COINBASE_MATURITY
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, assert_raises_rpc_error, satoshi_round

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

def run_test(self):
# Mine some blocks and have them mature.
self.nodes[0].generate(101)
self.nodes[0].generate(COINBASE_MATURITY + 1)
utxo = self.nodes[0].listunspent(10)
txid = utxo[0]['txid']
vout = utxo[0]['vout']
Expand Down
3 changes: 2 additions & 1 deletion test/functional/mempool_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from decimal import Decimal

from test_framework.blocktools import COINBASE_MATURITY
from test_framework.messages import COIN
from test_framework.p2p import P2PTxInvStore
from test_framework.test_framework import BitcoinTestFramework
Expand Down Expand Up @@ -59,7 +60,7 @@ def chain_transaction(self, node, parent_txid, vout, value, fee, num_outputs):
def run_test(self):
# Mine some blocks and have them mature.
peer_inv_store = self.nodes[0].add_p2p_connection(P2PTxInvStore()) # keep track of invs
self.nodes[0].generate(101)
self.nodes[0].generate(COINBASE_MATURITY + 1)
utxo = self.nodes[0].listunspent(10)
txid = utxo[0]['txid']
vout = utxo[0]['vout']
Expand Down
7 changes: 5 additions & 2 deletions test/functional/mempool_reorg.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
that spend (directly or indirectly) coinbase transactions.
"""

from test_framework.blocktools import create_raw_transaction
from test_framework.blocktools import (
COINBASE_MATURITY,
create_raw_transaction,
)
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, assert_raises_rpc_error

Expand Down Expand Up @@ -44,7 +47,7 @@ def run_test(self):
# 3. Indirect (coinbase and child both in chain) : spend_103 and spend_103_1
# Use invalidatblock to make all of the above coinbase spends invalid (immature coinbase),
# and make sure the mempool code behaves correctly.
b = [self.nodes[0].getblockhash(n) for n in range(101, 105)]
b = [self.nodes[0].getblockhash(n) for n in range(COINBASE_MATURITY + 1, COINBASE_MATURITY + 5)]
coinbase_txids = [self.nodes[0].getblock(h)['tx'][0] for h in b]
spend_101_raw = create_raw_transaction(self.nodes[0], coinbase_txids[1], node1_address, amount=49.99)
spend_102_raw = create_raw_transaction(self.nodes[0], coinbase_txids[2], node0_address, amount=49.99)
Expand Down
3 changes: 2 additions & 1 deletion test/functional/mempool_resurrect.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test resurrection of mined transactions when the blockchain is re-organized."""

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

# Add enough mature utxos to the wallet so that all txs spend confirmed coins
wallet.generate(3)
node.generate(100)
node.generate(COINBASE_MATURITY)

# Spend block 1/2/3's coinbase transactions
# Mine a block
Expand Down
3 changes: 2 additions & 1 deletion test/functional/mining_getblocktemplate_longpoll.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import random
import threading

from test_framework.blocktools import COINBASE_MATURITY
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import get_rpc_proxy
from test_framework.wallet import MiniWallet
Expand Down Expand Up @@ -62,7 +63,7 @@ def run_test(self):
assert not thr.is_alive()

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

self.log.info("Test that introducing a new transaction into the mempool will terminate the longpoll")
Expand Down
3 changes: 2 additions & 1 deletion test/functional/p2p_blocksonly.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import time

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

self.blocksonly_mode_tests()
self.blocks_relay_conn_tests()
Expand Down
13 changes: 9 additions & 4 deletions test/functional/p2p_compactblocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
"""
import random

from test_framework.blocktools import create_block, NORMAL_GBT_REQUEST_PARAMS, add_witness_commitment
from test_framework.blocktools import (
COINBASE_MATURITY,
NORMAL_GBT_REQUEST_PARAMS,
add_witness_commitment,
create_block,
)
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
from test_framework.p2p import p2p_lock, P2PInterface
from test_framework.script import CScript, OP_TRUE, OP_DROP
Expand Down Expand Up @@ -115,7 +120,7 @@ def make_utxos(self):
block = self.build_block_on_tip(self.nodes[0])
self.segwit_node.send_and_ping(msg_no_witness_block(block))
assert int(self.nodes[0].getbestblockhash(), 16) == block.sha256
self.nodes[0].generatetoaddress(100, self.nodes[0].getnewaddress(address_type="bech32"))
self.nodes[0].generatetoaddress(COINBASE_MATURITY, self.nodes[0].getnewaddress(address_type="bech32"))

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

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

cmpct_block = P2PHeaderAndShortIDs()
Expand All @@ -244,7 +249,7 @@ def test_compactblock_construction(self, test_node, use_witness_address=True):
version = test_node.cmpct_version
node = self.nodes[0]
# Generate a bunch of transactions.
node.generate(101)
node.generate(COINBASE_MATURITY + 1)
num_transactions = 25
address = node.getnewaddress()

Expand Down
8 changes: 6 additions & 2 deletions test/functional/p2p_eviction.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

import time

from test_framework.blocktools import create_block, create_coinbase
from test_framework.blocktools import (
COINBASE_MATURITY,
create_block,
create_coinbase,
)
from test_framework.messages import CTransaction, FromHex, msg_pong, msg_tx
from test_framework.p2p import P2PDataStore, P2PInterface
from test_framework.test_framework import BitcoinTestFramework
Expand Down Expand Up @@ -45,7 +49,7 @@ def run_test(self):
protected_peers = set() # peers that we expect to be protected from eviction
current_peer = -1
node = self.nodes[0]
node.generatetoaddress(101, node.get_deterministic_priv_key().address)
node.generatetoaddress(COINBASE_MATURITY + 1, node.get_deterministic_priv_key().address)

self.log.info("Create 4 peers and protect them from eviction by sending us a block")
for _ in range(4):
Expand Down
3 changes: 2 additions & 1 deletion test/functional/p2p_feefilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from decimal import Decimal

from test_framework.blocktools import COINBASE_MATURITY
from test_framework.messages import MSG_TX, MSG_WTX, msg_feefilter
from test_framework.p2p import P2PInterface, p2p_lock
from test_framework.test_framework import BitcoinTestFramework
Expand Down Expand Up @@ -81,7 +82,7 @@ def test_feefilter(self):
miniwallet = MiniWallet(node1)
# Add enough mature utxos to the wallet, so that all txs spend confirmed coins
miniwallet.generate(5)
node1.generate(100)
node1.generate(COINBASE_MATURITY)

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

Expand Down
3 changes: 2 additions & 1 deletion test/functional/p2p_leak_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test that we don't leak txs to inbound peers that we haven't yet announced to"""

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

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

Expand Down
Loading

0 comments on commit c5ee0cc

Please sign in to comment.