Skip to content

Commit 2eca46b

Browse files
committed
test: use MiniWallet for feature_csv_activation.py
This test can now be run even with the Bitcoin Core wallet disabled.
1 parent 9313c4e commit 2eca46b

File tree

2 files changed

+72
-74
lines changed

2 files changed

+72
-74
lines changed

test/functional/feature_csv_activation.py

Lines changed: 67 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@
3737
bip112tx_special - test negative argument to OP_CSV
3838
bip112tx_emptystack - test empty stack (= no argument) OP_CSV
3939
"""
40-
from decimal import Decimal
4140
from itertools import product
42-
from io import BytesIO
4341
import time
4442

45-
from test_framework.blocktools import create_coinbase, create_block, create_transaction
46-
from test_framework.messages import ToHex, CTransaction
43+
from test_framework.blocktools import (
44+
create_block,
45+
create_coinbase,
46+
)
4747
from test_framework.p2p import P2PDataStore
4848
from test_framework.script import (
4949
CScript,
@@ -53,9 +53,9 @@
5353
from test_framework.test_framework import BitcoinTestFramework
5454
from test_framework.util import (
5555
assert_equal,
56-
hex_str_to_bytes,
5756
softfork_active,
5857
)
58+
from test_framework.wallet import MiniWallet
5959

6060
TESTING_TX_COUNT = 83 # Number of testing transactions: 1 BIP113 tx, 16 BIP68 txs, 66 BIP112 txs (see comments above)
6161
COINBASE_BLOCK_COUNT = TESTING_TX_COUNT # Number of coinbase blocks we need to generate as inputs for our txs
@@ -83,65 +83,60 @@ def relative_locktime(sdf, srhb, stf, srlb):
8383
def all_rlt_txs(txs):
8484
return [tx['tx'] for tx in txs]
8585

86-
def sign_transaction(node, unsignedtx):
87-
rawtx = ToHex(unsignedtx)
88-
signresult = node.signrawtransactionwithwallet(rawtx)
89-
tx = CTransaction()
90-
f = BytesIO(hex_str_to_bytes(signresult['hex']))
91-
tx.deserialize(f)
86+
def create_self_transfer_from_utxo(node, input_tx):
87+
utxo = miniwallet.get_utxo(txid=input_tx.rehash(), mark_as_spent=False)
88+
tx = miniwallet.create_self_transfer(from_node=node, utxo_to_spend=utxo)['tx']
9289
return tx
9390

94-
def create_bip112special(node, input, txversion, address):
95-
tx = create_transaction(node, input, address, amount=Decimal("49.98"))
91+
def create_bip112special(node, input, txversion):
92+
tx = create_self_transfer_from_utxo(node, input)
9693
tx.nVersion = txversion
97-
signtx = sign_transaction(node, tx)
98-
signtx.vin[0].scriptSig = CScript([-1, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(signtx.vin[0].scriptSig)))
99-
return signtx
94+
tx.vin[0].scriptSig = CScript([-1, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(tx.vin[0].scriptSig)))
95+
return tx
10096

101-
def create_bip112emptystack(node, input, txversion, address):
102-
tx = create_transaction(node, input, address, amount=Decimal("49.98"))
97+
def create_bip112emptystack(node, input, txversion):
98+
tx = create_self_transfer_from_utxo(node, input)
10399
tx.nVersion = txversion
104-
signtx = sign_transaction(node, tx)
105-
signtx.vin[0].scriptSig = CScript([OP_CHECKSEQUENCEVERIFY] + list(CScript(signtx.vin[0].scriptSig)))
106-
return signtx
100+
tx.vin[0].scriptSig = CScript([OP_CHECKSEQUENCEVERIFY] + list(CScript(tx.vin[0].scriptSig)))
101+
return tx
107102

108-
def send_generic_input_tx(node, coinbases, address):
109-
return node.sendrawtransaction(ToHex(sign_transaction(node, create_transaction(node, node.getblock(coinbases.pop())['tx'][0], address, amount=Decimal("49.99")))))
103+
def send_generic_input_tx(node, coinbases):
104+
input_txid = node.getblock(coinbases.pop(), 2)['tx'][0]['txid']
105+
utxo_to_spend = miniwallet.get_utxo(txid=input_txid)
106+
return miniwallet.send_self_transfer(from_node=node, utxo_to_spend=utxo_to_spend)['tx']
110107

111-
def create_bip68txs(node, bip68inputs, txversion, address, locktime_delta=0):
108+
def create_bip68txs(node, bip68inputs, txversion, locktime_delta=0):
112109
"""Returns a list of bip68 transactions with different bits set."""
113110
txs = []
114111
assert len(bip68inputs) >= 16
115112
for i, (sdf, srhb, stf, srlb) in enumerate(product(*[[True, False]] * 4)):
116113
locktime = relative_locktime(sdf, srhb, stf, srlb)
117-
tx = create_transaction(node, bip68inputs[i], address, amount=Decimal("49.98"))
114+
tx = create_self_transfer_from_utxo(node, bip68inputs[i])
118115
tx.nVersion = txversion
119116
tx.vin[0].nSequence = locktime + locktime_delta
120-
tx = sign_transaction(node, tx)
121117
tx.rehash()
122118
txs.append({'tx': tx, 'sdf': sdf, 'stf': stf})
123119

124120
return txs
125121

126-
def create_bip112txs(node, bip112inputs, varyOP_CSV, txversion, address, locktime_delta=0):
122+
def create_bip112txs(node, bip112inputs, varyOP_CSV, txversion, locktime_delta=0):
127123
"""Returns a list of bip68 transactions with different bits set."""
128124
txs = []
129125
assert len(bip112inputs) >= 16
130126
for i, (sdf, srhb, stf, srlb) in enumerate(product(*[[True, False]] * 4)):
131127
locktime = relative_locktime(sdf, srhb, stf, srlb)
132-
tx = create_transaction(node, bip112inputs[i], address, amount=Decimal("49.98"))
128+
tx = create_self_transfer_from_utxo(node, bip112inputs[i])
133129
if (varyOP_CSV): # if varying OP_CSV, nSequence is fixed
134130
tx.vin[0].nSequence = BASE_RELATIVE_LOCKTIME + locktime_delta
135131
else: # vary nSequence instead, OP_CSV is fixed
136132
tx.vin[0].nSequence = locktime + locktime_delta
137133
tx.nVersion = txversion
138-
signtx = sign_transaction(node, tx)
139134
if (varyOP_CSV):
140-
signtx.vin[0].scriptSig = CScript([locktime, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(signtx.vin[0].scriptSig)))
135+
tx.vin[0].scriptSig = CScript([locktime, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(tx.vin[0].scriptSig)))
141136
else:
142-
signtx.vin[0].scriptSig = CScript([BASE_RELATIVE_LOCKTIME, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(signtx.vin[0].scriptSig)))
137+
tx.vin[0].scriptSig = CScript([BASE_RELATIVE_LOCKTIME, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(tx.vin[0].scriptSig)))
143138
tx.rehash()
144-
txs.append({'tx': signtx, 'sdf': sdf, 'stf': stf})
139+
txs.append({'tx': tx, 'sdf': sdf, 'stf': stf})
145140
return txs
146141

147142
class BIP68_112_113Test(BitcoinTestFramework):
@@ -150,14 +145,11 @@ def set_test_params(self):
150145
self.setup_clean_chain = True
151146
self.extra_args = [[
152147
153-
'-addresstype=legacy',
148+
'-acceptnonstdtxn=1',
154149
'-par=1', # Use only one script thread to get the exact reject reason for testing
155150
]]
156151
self.supports_cli = False
157152

158-
def skip_test_if_missing_module(self):
159-
self.skip_if_no_wallet()
160-
161153
def generate_blocks(self, number):
162154
test_blocks = []
163155
for _ in range(number):
@@ -185,16 +177,18 @@ def send_blocks(self, blocks, success=True, reject_reason=None):
185177

186178
def run_test(self):
187179
self.helper_peer = self.nodes[0].add_p2p_connection(P2PDataStore())
180+
# TODO: store as class member to get rid of global variable
181+
global miniwallet
182+
miniwallet = MiniWallet(self.nodes[0], raw_script=True)
188183

189184
self.log.info("Generate blocks in the past for coinbase outputs.")
190185
long_past_time = int(time.time()) - 600 * 1000 # enough to build up to 1000 blocks 10 minutes apart without worrying about getting into the future
191186
self.nodes[0].setmocktime(long_past_time - 100) # enough so that the generated blocks will still all be before long_past_time
192-
self.coinbase_blocks = self.nodes[0].generate(COINBASE_BLOCK_COUNT) # blocks generated for inputs
187+
self.coinbase_blocks = miniwallet.generate(COINBASE_BLOCK_COUNT) # blocks generated for inputs
193188
self.nodes[0].setmocktime(0) # set time back to present so yielded blocks aren't in the future as we advance last_block_time
194189
self.tipheight = COINBASE_BLOCK_COUNT # height of the next block to build
195190
self.last_block_time = long_past_time
196191
self.tip = int(self.nodes[0].getbestblockhash(), 16)
197-
self.nodeaddress = self.nodes[0].getnewaddress()
198192

199193
# Activation height is hardcoded
200194
# We advance to block height five below BIP112 activation for the following tests
@@ -209,31 +203,31 @@ def run_test(self):
209203
# 16 normal inputs
210204
bip68inputs = []
211205
for _ in range(16):
212-
bip68inputs.append(send_generic_input_tx(self.nodes[0], self.coinbase_blocks, self.nodeaddress))
206+
bip68inputs.append(send_generic_input_tx(self.nodes[0], self.coinbase_blocks))
213207

214208
# 2 sets of 16 inputs with 10 OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
215209
bip112basicinputs = []
216210
for _ in range(2):
217211
inputs = []
218212
for _ in range(16):
219-
inputs.append(send_generic_input_tx(self.nodes[0], self.coinbase_blocks, self.nodeaddress))
213+
inputs.append(send_generic_input_tx(self.nodes[0], self.coinbase_blocks))
220214
bip112basicinputs.append(inputs)
221215

222216
# 2 sets of 16 varied inputs with (relative_lock_time) OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
223217
bip112diverseinputs = []
224218
for _ in range(2):
225219
inputs = []
226220
for _ in range(16):
227-
inputs.append(send_generic_input_tx(self.nodes[0], self.coinbase_blocks, self.nodeaddress))
221+
inputs.append(send_generic_input_tx(self.nodes[0], self.coinbase_blocks))
228222
bip112diverseinputs.append(inputs)
229223

230224
# 1 special input with -1 OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
231-
bip112specialinput = send_generic_input_tx(self.nodes[0], self.coinbase_blocks, self.nodeaddress)
225+
bip112specialinput = send_generic_input_tx(self.nodes[0], self.coinbase_blocks)
232226
# 1 special input with (empty stack) OP_CSV (actually will be prepended to spending scriptSig)
233-
bip112emptystackinput = send_generic_input_tx(self.nodes[0],self.coinbase_blocks, self.nodeaddress)
227+
bip112emptystackinput = send_generic_input_tx(self.nodes[0], self.coinbase_blocks)
234228

235229
# 1 normal input
236-
bip113input = send_generic_input_tx(self.nodes[0], self.coinbase_blocks, self.nodeaddress)
230+
bip113input = send_generic_input_tx(self.nodes[0], self.coinbase_blocks)
237231

238232
self.nodes[0].setmocktime(self.last_block_time + 600)
239233
inputblockhash = self.nodes[0].generate(1)[0] # 1 block generated for inputs to be in chain at height 431
@@ -253,36 +247,36 @@ def run_test(self):
253247

254248
# Test both version 1 and version 2 transactions for all tests
255249
# BIP113 test transaction will be modified before each use to put in appropriate block time
256-
bip113tx_v1 = create_transaction(self.nodes[0], bip113input, self.nodeaddress, amount=Decimal("49.98"))
250+
bip113tx_v1 = create_self_transfer_from_utxo(self.nodes[0], bip113input)
257251
bip113tx_v1.vin[0].nSequence = 0xFFFFFFFE
258252
bip113tx_v1.nVersion = 1
259-
bip113tx_v2 = create_transaction(self.nodes[0], bip113input, self.nodeaddress, amount=Decimal("49.98"))
253+
bip113tx_v2 = create_self_transfer_from_utxo(self.nodes[0], bip113input)
260254
bip113tx_v2.vin[0].nSequence = 0xFFFFFFFE
261255
bip113tx_v2.nVersion = 2
262256

263257
# For BIP68 test all 16 relative sequence locktimes
264-
bip68txs_v1 = create_bip68txs(self.nodes[0], bip68inputs, 1, self.nodeaddress)
265-
bip68txs_v2 = create_bip68txs(self.nodes[0], bip68inputs, 2, self.nodeaddress)
258+
bip68txs_v1 = create_bip68txs(self.nodes[0], bip68inputs, 1)
259+
bip68txs_v2 = create_bip68txs(self.nodes[0], bip68inputs, 2)
266260

267261
# For BIP112 test:
268262
# 16 relative sequence locktimes of 10 against 10 OP_CSV OP_DROP inputs
269-
bip112txs_vary_nSequence_v1 = create_bip112txs(self.nodes[0], bip112basicinputs[0], False, 1, self.nodeaddress)
270-
bip112txs_vary_nSequence_v2 = create_bip112txs(self.nodes[0], bip112basicinputs[0], False, 2, self.nodeaddress)
263+
bip112txs_vary_nSequence_v1 = create_bip112txs(self.nodes[0], bip112basicinputs[0], False, 1)
264+
bip112txs_vary_nSequence_v2 = create_bip112txs(self.nodes[0], bip112basicinputs[0], False, 2)
271265
# 16 relative sequence locktimes of 9 against 10 OP_CSV OP_DROP inputs
272-
bip112txs_vary_nSequence_9_v1 = create_bip112txs(self.nodes[0], bip112basicinputs[1], False, 1, self.nodeaddress, -1)
273-
bip112txs_vary_nSequence_9_v2 = create_bip112txs(self.nodes[0], bip112basicinputs[1], False, 2, self.nodeaddress, -1)
266+
bip112txs_vary_nSequence_9_v1 = create_bip112txs(self.nodes[0], bip112basicinputs[1], False, 1, -1)
267+
bip112txs_vary_nSequence_9_v2 = create_bip112txs(self.nodes[0], bip112basicinputs[1], False, 2, -1)
274268
# sequence lock time of 10 against 16 (relative_lock_time) OP_CSV OP_DROP inputs
275-
bip112txs_vary_OP_CSV_v1 = create_bip112txs(self.nodes[0], bip112diverseinputs[0], True, 1, self.nodeaddress)
276-
bip112txs_vary_OP_CSV_v2 = create_bip112txs(self.nodes[0], bip112diverseinputs[0], True, 2, self.nodeaddress)
269+
bip112txs_vary_OP_CSV_v1 = create_bip112txs(self.nodes[0], bip112diverseinputs[0], True, 1)
270+
bip112txs_vary_OP_CSV_v2 = create_bip112txs(self.nodes[0], bip112diverseinputs[0], True, 2)
277271
# sequence lock time of 9 against 16 (relative_lock_time) OP_CSV OP_DROP inputs
278-
bip112txs_vary_OP_CSV_9_v1 = create_bip112txs(self.nodes[0], bip112diverseinputs[1], True, 1, self.nodeaddress, -1)
279-
bip112txs_vary_OP_CSV_9_v2 = create_bip112txs(self.nodes[0], bip112diverseinputs[1], True, 2, self.nodeaddress, -1)
272+
bip112txs_vary_OP_CSV_9_v1 = create_bip112txs(self.nodes[0], bip112diverseinputs[1], True, 1, -1)
273+
bip112txs_vary_OP_CSV_9_v2 = create_bip112txs(self.nodes[0], bip112diverseinputs[1], True, 2, -1)
280274
# -1 OP_CSV OP_DROP input
281-
bip112tx_special_v1 = create_bip112special(self.nodes[0], bip112specialinput, 1, self.nodeaddress)
282-
bip112tx_special_v2 = create_bip112special(self.nodes[0], bip112specialinput, 2, self.nodeaddress)
275+
bip112tx_special_v1 = create_bip112special(self.nodes[0], bip112specialinput, 1)
276+
bip112tx_special_v2 = create_bip112special(self.nodes[0], bip112specialinput, 2)
283277
# (empty stack) OP_CSV input
284-
bip112tx_emptystack_v1 = create_bip112emptystack(self.nodes[0], bip112emptystackinput, 1, self.nodeaddress)
285-
bip112tx_emptystack_v2 = create_bip112emptystack(self.nodes[0], bip112emptystackinput, 2, self.nodeaddress)
278+
bip112tx_emptystack_v1 = create_bip112emptystack(self.nodes[0], bip112emptystackinput, 1)
279+
bip112tx_emptystack_v2 = create_bip112emptystack(self.nodes[0], bip112emptystackinput, 2)
286280

287281
self.log.info("TESTING")
288282

@@ -292,8 +286,8 @@ def run_test(self):
292286
success_txs = []
293287
# BIP113 tx, -1 CSV tx and empty stack CSV tx should succeed
294288
bip113tx_v1.nLockTime = self.last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
295-
bip113signed1 = sign_transaction(self.nodes[0], bip113tx_v1)
296-
success_txs.append(bip113signed1)
289+
bip113tx_v1.rehash()
290+
success_txs.append(bip113tx_v1)
297291
success_txs.append(bip112tx_special_v1)
298292
success_txs.append(bip112tx_emptystack_v1)
299293
# add BIP 68 txs
@@ -312,8 +306,8 @@ def run_test(self):
312306
success_txs = []
313307
# BIP113 tx, -1 CSV tx and empty stack CSV tx should succeed
314308
bip113tx_v2.nLockTime = self.last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
315-
bip113signed2 = sign_transaction(self.nodes[0], bip113tx_v2)
316-
success_txs.append(bip113signed2)
309+
bip113tx_v2.rehash()
310+
success_txs.append(bip113tx_v2)
317311
success_txs.append(bip112tx_special_v2)
318312
success_txs.append(bip112tx_emptystack_v2)
319313
# add BIP 68 txs
@@ -338,17 +332,18 @@ def run_test(self):
338332
self.log.info("BIP 113 tests")
339333
# BIP 113 tests should now fail regardless of version number if nLockTime isn't satisfied by new rules
340334
bip113tx_v1.nLockTime = self.last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
341-
bip113signed1 = sign_transaction(self.nodes[0], bip113tx_v1)
335+
bip113tx_v1.rehash()
342336
bip113tx_v2.nLockTime = self.last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
343-
bip113signed2 = sign_transaction(self.nodes[0], bip113tx_v2)
344-
for bip113tx in [bip113signed1, bip113signed2]:
337+
bip113tx_v2.rehash()
338+
for bip113tx in [bip113tx_v1, bip113tx_v2]:
345339
self.send_blocks([self.create_test_block([bip113tx])], success=False, reject_reason='bad-txns-nonfinal')
340+
346341
# BIP 113 tests should now pass if the locktime is < MTP
347342
bip113tx_v1.nLockTime = self.last_block_time - 600 * 5 - 1 # < MTP of prior block
348-
bip113signed1 = sign_transaction(self.nodes[0], bip113tx_v1)
343+
bip113tx_v1.rehash()
349344
bip113tx_v2.nLockTime = self.last_block_time - 600 * 5 - 1 # < MTP of prior block
350-
bip113signed2 = sign_transaction(self.nodes[0], bip113tx_v2)
351-
for bip113tx in [bip113signed1, bip113signed2]:
345+
bip113tx_v2.rehash()
346+
for bip113tx in [bip113tx_v1, bip113tx_v2]:
352347
self.send_blocks([self.create_test_block([bip113tx])])
353348
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
354349

@@ -471,8 +466,8 @@ def run_test(self):
471466
time_txs = []
472467
for tx in [tx['tx'] for tx in bip112txs_vary_OP_CSV_v2 if not tx['sdf'] and tx['stf']]:
473468
tx.vin[0].nSequence = BASE_RELATIVE_LOCKTIME | SEQ_TYPE_FLAG
474-
signtx = sign_transaction(self.nodes[0], tx)
475-
time_txs.append(signtx)
469+
tx.rehash()
470+
time_txs.append(tx)
476471

477472
self.send_blocks([self.create_test_block(time_txs)])
478473
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())

test/functional/test_framework/wallet.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def generate(self, num_blocks):
6161
def get_address(self):
6262
return self._address
6363

64-
def get_utxo(self, *, txid=''):
64+
def get_utxo(self, *, txid='', mark_as_spent=True):
6565
"""
6666
Returns a utxo and marks it as spent (pops it from the internal list)
6767
@@ -74,7 +74,10 @@ def get_utxo(self, *, txid=''):
7474
if txid:
7575
utxo = next(filter(lambda utxo: txid == utxo['txid'], self._utxos))
7676
index = self._utxos.index(utxo)
77-
return self._utxos.pop(index)
77+
if mark_as_spent:
78+
return self._utxos.pop(index)
79+
else:
80+
return self._utxos[index]
7881

7982
def send_self_transfer(self, *, fee_rate=Decimal("0.003"), from_node, utxo_to_spend=None):
8083
"""Create and send a tx with the specified fee_rate. Fee may be exact or at most one satoshi higher than needed."""

0 commit comments

Comments
 (0)