Skip to content

Commit 548ca1d

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#22550: test: improve test_signing_with_{csv,cltv} subtests (speed, prevent timeout)
12f094e test: use constants for CSV/CLTV activation heights in rpc_signrawtransaction (Sebastian Falbesoner) 746f203 test: introduce `generate_to_height` helper, use in rpc_signrawtransaction (Sebastian Falbesoner) e3237b1 test: check that CSV/CLTV are active in rpc_signrawtransaction (Sebastian Falbesoner) Pull request description: This PR primarily aims to solve the current RPC timeout problem for test rpc_signrawtransaction.py, as described in #22542. In the course of that the test is also improved in other ways (see bitcoin/bitcoin#22542 (review)). Reviewers guideline: * In `test_signing_with_cltv()`, a comment is fixed -- it wrongly referred to CSV, it should be CLTV. * As preparation, assertions are added that ensure that CSV and CLTV have been really activated after generating blocks by checking the 'softforks' output of the getblockchaininfo() RPC. Right now in master, one could remove (or decrease, like in #22542) the generate calls and the test would still pass, when it shouldn't. * A helper `generate_to_height()` is introduced which improves the previous way of reaching a block height in two ways: - instead of blindly generating TH blocks to reach target block height >= TH, the current block height CH is taken into account, and only (TH - CH) are generated in total - to avoid potential RPC timeouts, the block generation is split up into multiple generatetoaddress RPC calls ([as suggested by laanwj](bitcoin/bitcoin#22542 (comment))); here chunks of 200 blocks have been chosen * The helper is used in the affected sub-tests, which should both speed-up the test (from ~18s to ~12s on my machine) and avoid potential timeouts * Finally, the activation constants for CSV and CLTV are used instead of using magic numbers 500 and 1500 Open questions: * Any good naming alternatives for `generate_to_height()`? Not really happy with the name, happy to hear suggestions * Where to put the CSV and CLTV activation height constants in the test_framewor folder? I guess importing constants from other tests isn't really the desired way to go ACKs for top commit: laanwj: Code review and tested ACK 12f094e rajarshimaitra: reACK bitcoin/bitcoin@12f094e Tree-SHA512: 14509f6d3e5a5a05d6a30a3145bb82cd96a29d9d8a589abf1944a8bf34291cde78ce711195f52e9426588dc822b3618ec9b455e057360021ae46152bb7613516
2 parents be175ce + 12f094e commit 548ca1d

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

test/functional/feature_cltv.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""
1010

1111
from test_framework.blocktools import (
12+
CLTV_HEIGHT,
1213
create_block,
1314
create_coinbase,
1415
)
@@ -31,8 +32,6 @@
3132
MiniWalletMode,
3233
)
3334

34-
CLTV_HEIGHT = 1351
35-
3635

3736
# Helper function to modify a transaction by
3837
# 1) prepending a given script to the scriptSig of vin 0 and

test/functional/feature_csv_activation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import time
4242

4343
from test_framework.blocktools import (
44+
CSV_ACTIVATION_HEIGHT,
4445
create_block,
4546
create_coinbase,
4647
)
@@ -63,7 +64,6 @@
6364
TESTING_TX_COUNT = 83 # Number of testing transactions: 1 BIP113 tx, 16 BIP68 txs, 66 BIP112 txs (see comments above)
6465
COINBASE_BLOCK_COUNT = TESTING_TX_COUNT # Number of coinbase blocks we need to generate as inputs for our txs
6566
BASE_RELATIVE_LOCKTIME = 10
66-
CSV_ACTIVATION_HEIGHT = 432
6767
SEQ_DISABLE_FLAG = 1 << 31
6868
SEQ_RANDOM_HIGH_BIT = 1 << 25
6969
SEQ_TYPE_FLAG = 1 << 22

test/functional/rpc_signrawtransaction.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test transaction signing using the signrawtransaction* RPCs."""
66

7-
from test_framework.blocktools import COINBASE_MATURITY
7+
from test_framework.blocktools import (
8+
CLTV_HEIGHT,
9+
COINBASE_MATURITY,
10+
CSV_ACTIVATION_HEIGHT,
11+
)
812
from test_framework.address import (
913
script_to_p2sh,
1014
script_to_p2wsh,
@@ -15,6 +19,7 @@
1519
assert_equal,
1620
assert_raises_rpc_error,
1721
find_vout_for_address,
22+
generate_to_height,
1823
hex_str_to_bytes,
1924
)
2025
from test_framework.messages import (
@@ -270,7 +275,8 @@ def test_signing_with_csv(self):
270275
getcontext().prec = 8
271276

272277
# Make sure CSV is active
273-
self.nodes[0].generate(500)
278+
generate_to_height(self.nodes[0], CSV_ACTIVATION_HEIGHT)
279+
assert self.nodes[0].getblockchaininfo()['softforks']['csv']['active']
274280

275281
# Create a P2WSH script with CSV
276282
script = CScript([1, OP_CHECKSEQUENCEVERIFY, OP_DROP])
@@ -304,8 +310,9 @@ def test_signing_with_cltv(self):
304310
self.nodes[0].walletpassphrase("password", 9999)
305311
getcontext().prec = 8
306312

307-
# Make sure CSV is active
308-
self.nodes[0].generate(1500)
313+
# Make sure CLTV is active
314+
generate_to_height(self.nodes[0], CLTV_HEIGHT)
315+
assert self.nodes[0].getblockchaininfo()['softforks']['bip65']['active']
309316

310317
# Create a P2WSH script with CLTV
311318
script = CScript([1000, OP_CHECKLOCKTIMEVERIFY, OP_DROP])

test/functional/test_framework/blocktools.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
# Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
5656
COINBASE_MATURITY = 100
5757

58+
# Soft-fork activation heights
59+
CLTV_HEIGHT = 1351
60+
CSV_ACTIVATION_HEIGHT = 432
61+
5862
# From BIP141
5963
WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed"
6064

test/functional/test_framework/util.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,17 @@ def mine_large_block(node, utxos=None):
559559
node.generate(1)
560560

561561

562+
def generate_to_height(node, target_height):
563+
"""Generates blocks until a given target block height has been reached.
564+
To prevent timeouts, only up to 200 blocks are generated per RPC call.
565+
Can be used to activate certain soft-forks (e.g. CSV, CLTV)."""
566+
current_height = node.getblockcount()
567+
while current_height < target_height:
568+
nblocks = min(200, target_height - current_height)
569+
current_height += len(node.generate(nblocks))
570+
assert_equal(node.getblockcount(), target_height)
571+
572+
562573
def find_vout_for_address(node, txid, addr):
563574
"""
564575
Locate the vout index of the given transaction sending to the

0 commit comments

Comments
 (0)