Skip to content

Commit bd7f27d

Browse files
committed
refactor: feature_csv_activation.py: move tx helper functions to methods
This allows to get rid of the global miniwallet variable and to specify the used node self.nodes[0] at only one place, instead of passing it to every tx creation/send method again and again. Can be reviewed with --ignore-all-space --color-moved=dimmed-zebra
1 parent 2eca46b commit bd7f27d

File tree

1 file changed

+80
-81
lines changed

1 file changed

+80
-81
lines changed

test/functional/feature_csv_activation.py

Lines changed: 80 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -83,61 +83,6 @@ def relative_locktime(sdf, srhb, stf, srlb):
8383
def all_rlt_txs(txs):
8484
return [tx['tx'] for tx in txs]
8585

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']
89-
return tx
90-
91-
def create_bip112special(node, input, txversion):
92-
tx = create_self_transfer_from_utxo(node, input)
93-
tx.nVersion = txversion
94-
tx.vin[0].scriptSig = CScript([-1, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(tx.vin[0].scriptSig)))
95-
return tx
96-
97-
def create_bip112emptystack(node, input, txversion):
98-
tx = create_self_transfer_from_utxo(node, input)
99-
tx.nVersion = txversion
100-
tx.vin[0].scriptSig = CScript([OP_CHECKSEQUENCEVERIFY] + list(CScript(tx.vin[0].scriptSig)))
101-
return tx
102-
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']
107-
108-
def create_bip68txs(node, bip68inputs, txversion, locktime_delta=0):
109-
"""Returns a list of bip68 transactions with different bits set."""
110-
txs = []
111-
assert len(bip68inputs) >= 16
112-
for i, (sdf, srhb, stf, srlb) in enumerate(product(*[[True, False]] * 4)):
113-
locktime = relative_locktime(sdf, srhb, stf, srlb)
114-
tx = create_self_transfer_from_utxo(node, bip68inputs[i])
115-
tx.nVersion = txversion
116-
tx.vin[0].nSequence = locktime + locktime_delta
117-
tx.rehash()
118-
txs.append({'tx': tx, 'sdf': sdf, 'stf': stf})
119-
120-
return txs
121-
122-
def create_bip112txs(node, bip112inputs, varyOP_CSV, txversion, locktime_delta=0):
123-
"""Returns a list of bip68 transactions with different bits set."""
124-
txs = []
125-
assert len(bip112inputs) >= 16
126-
for i, (sdf, srhb, stf, srlb) in enumerate(product(*[[True, False]] * 4)):
127-
locktime = relative_locktime(sdf, srhb, stf, srlb)
128-
tx = create_self_transfer_from_utxo(node, bip112inputs[i])
129-
if (varyOP_CSV): # if varying OP_CSV, nSequence is fixed
130-
tx.vin[0].nSequence = BASE_RELATIVE_LOCKTIME + locktime_delta
131-
else: # vary nSequence instead, OP_CSV is fixed
132-
tx.vin[0].nSequence = locktime + locktime_delta
133-
tx.nVersion = txversion
134-
if (varyOP_CSV):
135-
tx.vin[0].scriptSig = CScript([locktime, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(tx.vin[0].scriptSig)))
136-
else:
137-
tx.vin[0].scriptSig = CScript([BASE_RELATIVE_LOCKTIME, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(tx.vin[0].scriptSig)))
138-
tx.rehash()
139-
txs.append({'tx': tx, 'sdf': sdf, 'stf': stf})
140-
return txs
14186

14287
class BIP68_112_113Test(BitcoinTestFramework):
14388
def set_test_params(self):
@@ -150,6 +95,62 @@ def set_test_params(self):
15095
]]
15196
self.supports_cli = False
15297

98+
def create_self_transfer_from_utxo(self, input_tx):
99+
utxo = self.miniwallet.get_utxo(txid=input_tx.rehash(), mark_as_spent=False)
100+
tx = self.miniwallet.create_self_transfer(from_node=self.nodes[0], utxo_to_spend=utxo)['tx']
101+
return tx
102+
103+
def create_bip112special(self, input, txversion):
104+
tx = self.create_self_transfer_from_utxo(input)
105+
tx.nVersion = txversion
106+
tx.vin[0].scriptSig = CScript([-1, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(tx.vin[0].scriptSig)))
107+
return tx
108+
109+
def create_bip112emptystack(self, input, txversion):
110+
tx = self.create_self_transfer_from_utxo(input)
111+
tx.nVersion = txversion
112+
tx.vin[0].scriptSig = CScript([OP_CHECKSEQUENCEVERIFY] + list(CScript(tx.vin[0].scriptSig)))
113+
return tx
114+
115+
def send_generic_input_tx(self, coinbases):
116+
input_txid = self.nodes[0].getblock(coinbases.pop(), 2)['tx'][0]['txid']
117+
utxo_to_spend = self.miniwallet.get_utxo(txid=input_txid)
118+
return self.miniwallet.send_self_transfer(from_node=self.nodes[0], utxo_to_spend=utxo_to_spend)['tx']
119+
120+
def create_bip68txs(self, bip68inputs, txversion, locktime_delta=0):
121+
"""Returns a list of bip68 transactions with different bits set."""
122+
txs = []
123+
assert len(bip68inputs) >= 16
124+
for i, (sdf, srhb, stf, srlb) in enumerate(product(*[[True, False]] * 4)):
125+
locktime = relative_locktime(sdf, srhb, stf, srlb)
126+
tx = self.create_self_transfer_from_utxo(bip68inputs[i])
127+
tx.nVersion = txversion
128+
tx.vin[0].nSequence = locktime + locktime_delta
129+
tx.rehash()
130+
txs.append({'tx': tx, 'sdf': sdf, 'stf': stf})
131+
132+
return txs
133+
134+
def create_bip112txs(self, bip112inputs, varyOP_CSV, txversion, locktime_delta=0):
135+
"""Returns a list of bip68 transactions with different bits set."""
136+
txs = []
137+
assert len(bip112inputs) >= 16
138+
for i, (sdf, srhb, stf, srlb) in enumerate(product(*[[True, False]] * 4)):
139+
locktime = relative_locktime(sdf, srhb, stf, srlb)
140+
tx = self.create_self_transfer_from_utxo(bip112inputs[i])
141+
if (varyOP_CSV): # if varying OP_CSV, nSequence is fixed
142+
tx.vin[0].nSequence = BASE_RELATIVE_LOCKTIME + locktime_delta
143+
else: # vary nSequence instead, OP_CSV is fixed
144+
tx.vin[0].nSequence = locktime + locktime_delta
145+
tx.nVersion = txversion
146+
if (varyOP_CSV):
147+
tx.vin[0].scriptSig = CScript([locktime, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(tx.vin[0].scriptSig)))
148+
else:
149+
tx.vin[0].scriptSig = CScript([BASE_RELATIVE_LOCKTIME, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(tx.vin[0].scriptSig)))
150+
tx.rehash()
151+
txs.append({'tx': tx, 'sdf': sdf, 'stf': stf})
152+
return txs
153+
153154
def generate_blocks(self, number):
154155
test_blocks = []
155156
for _ in range(number):
@@ -177,14 +178,12 @@ def send_blocks(self, blocks, success=True, reject_reason=None):
177178

178179
def run_test(self):
179180
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)
181+
self.miniwallet = MiniWallet(self.nodes[0], raw_script=True)
183182

184183
self.log.info("Generate blocks in the past for coinbase outputs.")
185184
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
186185
self.nodes[0].setmocktime(long_past_time - 100) # enough so that the generated blocks will still all be before long_past_time
187-
self.coinbase_blocks = miniwallet.generate(COINBASE_BLOCK_COUNT) # blocks generated for inputs
186+
self.coinbase_blocks = self.miniwallet.generate(COINBASE_BLOCK_COUNT) # blocks generated for inputs
188187
self.nodes[0].setmocktime(0) # set time back to present so yielded blocks aren't in the future as we advance last_block_time
189188
self.tipheight = COINBASE_BLOCK_COUNT # height of the next block to build
190189
self.last_block_time = long_past_time
@@ -203,31 +202,31 @@ def run_test(self):
203202
# 16 normal inputs
204203
bip68inputs = []
205204
for _ in range(16):
206-
bip68inputs.append(send_generic_input_tx(self.nodes[0], self.coinbase_blocks))
205+
bip68inputs.append(self.send_generic_input_tx(self.coinbase_blocks))
207206

208207
# 2 sets of 16 inputs with 10 OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
209208
bip112basicinputs = []
210209
for _ in range(2):
211210
inputs = []
212211
for _ in range(16):
213-
inputs.append(send_generic_input_tx(self.nodes[0], self.coinbase_blocks))
212+
inputs.append(self.send_generic_input_tx(self.coinbase_blocks))
214213
bip112basicinputs.append(inputs)
215214

216215
# 2 sets of 16 varied inputs with (relative_lock_time) OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
217216
bip112diverseinputs = []
218217
for _ in range(2):
219218
inputs = []
220219
for _ in range(16):
221-
inputs.append(send_generic_input_tx(self.nodes[0], self.coinbase_blocks))
220+
inputs.append(self.send_generic_input_tx(self.coinbase_blocks))
222221
bip112diverseinputs.append(inputs)
223222

224223
# 1 special input with -1 OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
225-
bip112specialinput = send_generic_input_tx(self.nodes[0], self.coinbase_blocks)
224+
bip112specialinput = self.send_generic_input_tx(self.coinbase_blocks)
226225
# 1 special input with (empty stack) OP_CSV (actually will be prepended to spending scriptSig)
227-
bip112emptystackinput = send_generic_input_tx(self.nodes[0], self.coinbase_blocks)
226+
bip112emptystackinput = self.send_generic_input_tx(self.coinbase_blocks)
228227

229228
# 1 normal input
230-
bip113input = send_generic_input_tx(self.nodes[0], self.coinbase_blocks)
229+
bip113input = self.send_generic_input_tx(self.coinbase_blocks)
231230

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

248247
# Test both version 1 and version 2 transactions for all tests
249248
# BIP113 test transaction will be modified before each use to put in appropriate block time
250-
bip113tx_v1 = create_self_transfer_from_utxo(self.nodes[0], bip113input)
249+
bip113tx_v1 = self.create_self_transfer_from_utxo(bip113input)
251250
bip113tx_v1.vin[0].nSequence = 0xFFFFFFFE
252251
bip113tx_v1.nVersion = 1
253-
bip113tx_v2 = create_self_transfer_from_utxo(self.nodes[0], bip113input)
252+
bip113tx_v2 = self.create_self_transfer_from_utxo(bip113input)
254253
bip113tx_v2.vin[0].nSequence = 0xFFFFFFFE
255254
bip113tx_v2.nVersion = 2
256255

257256
# For BIP68 test all 16 relative sequence locktimes
258-
bip68txs_v1 = create_bip68txs(self.nodes[0], bip68inputs, 1)
259-
bip68txs_v2 = create_bip68txs(self.nodes[0], bip68inputs, 2)
257+
bip68txs_v1 = self.create_bip68txs(bip68inputs, 1)
258+
bip68txs_v2 = self.create_bip68txs(bip68inputs, 2)
260259

261260
# For BIP112 test:
262261
# 16 relative sequence locktimes of 10 against 10 OP_CSV OP_DROP inputs
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)
262+
bip112txs_vary_nSequence_v1 = self.create_bip112txs(bip112basicinputs[0], False, 1)
263+
bip112txs_vary_nSequence_v2 = self.create_bip112txs(bip112basicinputs[0], False, 2)
265264
# 16 relative sequence locktimes of 9 against 10 OP_CSV OP_DROP inputs
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)
265+
bip112txs_vary_nSequence_9_v1 = self.create_bip112txs(bip112basicinputs[1], False, 1, -1)
266+
bip112txs_vary_nSequence_9_v2 = self.create_bip112txs(bip112basicinputs[1], False, 2, -1)
268267
# sequence lock time of 10 against 16 (relative_lock_time) OP_CSV OP_DROP inputs
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)
268+
bip112txs_vary_OP_CSV_v1 = self.create_bip112txs(bip112diverseinputs[0], True, 1)
269+
bip112txs_vary_OP_CSV_v2 = self.create_bip112txs(bip112diverseinputs[0], True, 2)
271270
# sequence lock time of 9 against 16 (relative_lock_time) OP_CSV OP_DROP inputs
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)
271+
bip112txs_vary_OP_CSV_9_v1 = self.create_bip112txs(bip112diverseinputs[1], True, 1, -1)
272+
bip112txs_vary_OP_CSV_9_v2 = self.create_bip112txs(bip112diverseinputs[1], True, 2, -1)
274273
# -1 OP_CSV OP_DROP input
275-
bip112tx_special_v1 = create_bip112special(self.nodes[0], bip112specialinput, 1)
276-
bip112tx_special_v2 = create_bip112special(self.nodes[0], bip112specialinput, 2)
274+
bip112tx_special_v1 = self.create_bip112special(bip112specialinput, 1)
275+
bip112tx_special_v2 = self.create_bip112special(bip112specialinput, 2)
277276
# (empty stack) OP_CSV input
278-
bip112tx_emptystack_v1 = create_bip112emptystack(self.nodes[0], bip112emptystackinput, 1)
279-
bip112tx_emptystack_v2 = create_bip112emptystack(self.nodes[0], bip112emptystackinput, 2)
277+
bip112tx_emptystack_v1 = self.create_bip112emptystack(bip112emptystackinput, 1)
278+
bip112tx_emptystack_v2 = self.create_bip112emptystack(bip112emptystackinput, 2)
280279

281280
self.log.info("TESTING")
282281

0 commit comments

Comments
 (0)