Skip to content

Commit db7ffb9

Browse files
committed
[tests] Move utility functions in feature_csv_activation.py out of class.
1 parent 0842edf commit db7ffb9

File tree

1 file changed

+90
-90
lines changed

1 file changed

+90
-90
lines changed

test/functional/feature_csv_activation.py

Lines changed: 90 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,70 @@ def relative_locktime(sdf, srhb, stf, srlb):
8585
def all_rlt_txs(txs):
8686
return [tx['tx'] for tx in txs]
8787

88+
def create_transaction(node, txid, to_address, amount):
89+
inputs = [{"txid": txid, "vout": 0}]
90+
outputs = {to_address: amount}
91+
rawtx = node.createrawtransaction(inputs, outputs)
92+
tx = CTransaction()
93+
f = BytesIO(hex_str_to_bytes(rawtx))
94+
tx.deserialize(f)
95+
return tx
96+
97+
def sign_transaction(node, unsignedtx):
98+
rawtx = ToHex(unsignedtx)
99+
signresult = node.signrawtransactionwithwallet(rawtx)
100+
tx = CTransaction()
101+
f = BytesIO(hex_str_to_bytes(signresult['hex']))
102+
tx.deserialize(f)
103+
return tx
104+
105+
def create_bip112special(node, input, txversion, address):
106+
tx = create_transaction(node, input, address, Decimal("49.98"))
107+
tx.nVersion = txversion
108+
signtx = sign_transaction(node, tx)
109+
signtx.vin[0].scriptSig = CScript([-1, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(signtx.vin[0].scriptSig)))
110+
return signtx
111+
112+
def send_generic_input_tx(node, coinbases, address):
113+
amount = Decimal("49.99")
114+
return node.sendrawtransaction(ToHex(sign_transaction(node, create_transaction(node, node.getblock(coinbases.pop())['tx'][0], address, amount))))
115+
116+
def create_bip68txs(node, bip68inputs, txversion, address, locktime_delta=0):
117+
"""Returns a list of bip68 transactions with different bits set."""
118+
txs = []
119+
assert(len(bip68inputs) >= 16)
120+
for i, (sdf, srhb, stf, srlb) in enumerate(product(*[[True, False]] * 4)):
121+
locktime = relative_locktime(sdf, srhb, stf, srlb)
122+
tx = create_transaction(node, bip68inputs[i], address, Decimal("49.98"))
123+
tx.nVersion = txversion
124+
tx.vin[0].nSequence = locktime + locktime_delta
125+
tx = sign_transaction(node, tx)
126+
tx.rehash()
127+
txs.append({'tx': tx, 'sdf': sdf, 'stf': stf})
128+
129+
return txs
130+
131+
def create_bip112txs(node, bip112inputs, varyOP_CSV, txversion, address, locktime_delta=0):
132+
"""Returns a list of bip68 transactions with different bits set."""
133+
txs = []
134+
assert(len(bip112inputs) >= 16)
135+
for i, (sdf, srhb, stf, srlb) in enumerate(product(*[[True, False]] * 4)):
136+
locktime = relative_locktime(sdf, srhb, stf, srlb)
137+
tx = create_transaction(node, bip112inputs[i], address, Decimal("49.98"))
138+
if (varyOP_CSV): # if varying OP_CSV, nSequence is fixed
139+
tx.vin[0].nSequence = BASE_RELATIVE_LOCKTIME + locktime_delta
140+
else: # vary nSequence instead, OP_CSV is fixed
141+
tx.vin[0].nSequence = locktime + locktime_delta
142+
tx.nVersion = txversion
143+
signtx = sign_transaction(node, tx)
144+
if (varyOP_CSV):
145+
signtx.vin[0].scriptSig = CScript([locktime, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(signtx.vin[0].scriptSig)))
146+
else:
147+
signtx.vin[0].scriptSig = CScript([BASE_RELATIVE_LOCKTIME, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(signtx.vin[0].scriptSig)))
148+
tx.rehash()
149+
txs.append({'tx': signtx, 'sdf': sdf, 'stf': stf})
150+
return txs
151+
88152
class BIP68_112_113Test(ComparisonTestFramework):
89153
def set_test_params(self):
90154
self.num_nodes = 1
@@ -97,27 +161,6 @@ def run_test(self):
97161
network_thread_start()
98162
test.run()
99163

100-
def send_generic_input_tx(self, node, coinbases):
101-
amount = Decimal("49.99")
102-
return node.sendrawtransaction(ToHex(self.sign_transaction(node, self.create_transaction(node, node.getblock(coinbases.pop())['tx'][0], self.nodeaddress, amount))))
103-
104-
def create_transaction(self, node, txid, to_address, amount):
105-
inputs = [{"txid": txid, "vout": 0}]
106-
outputs = {to_address: amount}
107-
rawtx = node.createrawtransaction(inputs, outputs)
108-
tx = CTransaction()
109-
f = BytesIO(hex_str_to_bytes(rawtx))
110-
tx.deserialize(f)
111-
return tx
112-
113-
def sign_transaction(self, node, unsignedtx):
114-
rawtx = ToHex(unsignedtx)
115-
signresult = node.signrawtransactionwithwallet(rawtx)
116-
tx = CTransaction()
117-
f = BytesIO(hex_str_to_bytes(signresult['hex']))
118-
tx.deserialize(f)
119-
return tx
120-
121164
def generate_blocks(self, number, version, test_blocks=[]):
122165
for i in range(number):
123166
block = self.create_test_block([], version)
@@ -136,49 +179,6 @@ def create_test_block(self, txs, version=536870912):
136179
block.solve()
137180
return block
138181

139-
def create_bip68txs(self, bip68inputs, txversion, locktime_delta=0):
140-
"""Returns a list of bip68 transactions with different bits set."""
141-
txs = []
142-
assert(len(bip68inputs) >= 16)
143-
for i, (sdf, srhb, stf, srlb) in enumerate(product(*[[True, False]] * 4)):
144-
locktime = relative_locktime(sdf, srhb, stf, srlb)
145-
tx = self.create_transaction(self.nodes[0], bip68inputs[i], self.nodeaddress, Decimal("49.98"))
146-
tx.nVersion = txversion
147-
tx.vin[0].nSequence = locktime + locktime_delta
148-
tx = self.sign_transaction(self.nodes[0], tx)
149-
tx.rehash()
150-
txs.append({'tx': tx, 'sdf': sdf, 'stf': stf})
151-
152-
return txs
153-
154-
def create_bip112special(self, input, txversion):
155-
tx = self.create_transaction(self.nodes[0], input, self.nodeaddress, Decimal("49.98"))
156-
tx.nVersion = txversion
157-
signtx = self.sign_transaction(self.nodes[0], tx)
158-
signtx.vin[0].scriptSig = CScript([-1, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(signtx.vin[0].scriptSig)))
159-
return signtx
160-
161-
def create_bip112txs(self, bip112inputs, varyOP_CSV, txversion, locktime_delta=0):
162-
"""Returns a list of bip68 transactions with different bits set."""
163-
txs = []
164-
assert(len(bip112inputs) >= 16)
165-
for i, (sdf, srhb, stf, srlb) in enumerate(product(*[[True, False]] * 4)):
166-
locktime = relative_locktime(sdf, srhb, stf, srlb)
167-
tx = self.create_transaction(self.nodes[0], bip112inputs[i], self.nodeaddress, Decimal("49.98"))
168-
if (varyOP_CSV): # if varying OP_CSV, nSequence is fixed
169-
tx.vin[0].nSequence = BASE_RELATIVE_LOCKTIME + locktime_delta
170-
else: # vary nSequence instead, OP_CSV is fixed
171-
tx.vin[0].nSequence = locktime + locktime_delta
172-
tx.nVersion = txversion
173-
signtx = self.sign_transaction(self.nodes[0], tx)
174-
if (varyOP_CSV):
175-
signtx.vin[0].scriptSig = CScript([locktime, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(signtx.vin[0].scriptSig)))
176-
else:
177-
signtx.vin[0].scriptSig = CScript([BASE_RELATIVE_LOCKTIME, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(signtx.vin[0].scriptSig)))
178-
tx.rehash()
179-
txs.append({'tx': signtx, 'sdf': sdf, 'stf': stf})
180-
return txs
181-
182182
def get_tests(self):
183183
self.log.info("Generate blocks in the past for coinbase outputs.")
184184
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
@@ -233,29 +233,29 @@ def get_tests(self):
233233
# 16 normal inputs
234234
bip68inputs = []
235235
for i in range(16):
236-
bip68inputs.append(self.send_generic_input_tx(self.nodes[0], self.coinbase_blocks))
236+
bip68inputs.append(send_generic_input_tx(self.nodes[0], self.coinbase_blocks, self.nodeaddress))
237237

238238
# 2 sets of 16 inputs with 10 OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
239239
bip112basicinputs = []
240240
for j in range(2):
241241
inputs = []
242242
for i in range(16):
243-
inputs.append(self.send_generic_input_tx(self.nodes[0], self.coinbase_blocks))
243+
inputs.append(send_generic_input_tx(self.nodes[0], self.coinbase_blocks, self.nodeaddress))
244244
bip112basicinputs.append(inputs)
245245

246246
# 2 sets of 16 varied inputs with (relative_lock_time) OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
247247
bip112diverseinputs = []
248248
for j in range(2):
249249
inputs = []
250250
for i in range(16):
251-
inputs.append(self.send_generic_input_tx(self.nodes[0], self.coinbase_blocks))
251+
inputs.append(send_generic_input_tx(self.nodes[0], self.coinbase_blocks, self.nodeaddress))
252252
bip112diverseinputs.append(inputs)
253253

254254
# 1 special input with -1 OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
255-
bip112specialinput = self.send_generic_input_tx(self.nodes[0], self.coinbase_blocks)
255+
bip112specialinput = send_generic_input_tx(self.nodes[0], self.coinbase_blocks, self.nodeaddress)
256256

257257
# 1 normal input
258-
bip113input = self.send_generic_input_tx(self.nodes[0], self.coinbase_blocks)
258+
bip113input = send_generic_input_tx(self.nodes[0], self.coinbase_blocks, self.nodeaddress)
259259

260260
self.nodes[0].setmocktime(self.last_block_time + 600)
261261
inputblockhash = self.nodes[0].generate(1)[0] # 1 block generated for inputs to be in chain at height 572
@@ -274,33 +274,33 @@ def get_tests(self):
274274

275275
# Test both version 1 and version 2 transactions for all tests
276276
# BIP113 test transaction will be modified before each use to put in appropriate block time
277-
bip113tx_v1 = self.create_transaction(self.nodes[0], bip113input, self.nodeaddress, Decimal("49.98"))
277+
bip113tx_v1 = create_transaction(self.nodes[0], bip113input, self.nodeaddress, Decimal("49.98"))
278278
bip113tx_v1.vin[0].nSequence = 0xFFFFFFFE
279279
bip113tx_v1.nVersion = 1
280-
bip113tx_v2 = self.create_transaction(self.nodes[0], bip113input, self.nodeaddress, Decimal("49.98"))
280+
bip113tx_v2 = create_transaction(self.nodes[0], bip113input, self.nodeaddress, Decimal("49.98"))
281281
bip113tx_v2.vin[0].nSequence = 0xFFFFFFFE
282282
bip113tx_v2.nVersion = 2
283283

284284
# For BIP68 test all 16 relative sequence locktimes
285-
bip68txs_v1 = self.create_bip68txs(bip68inputs, 1)
286-
bip68txs_v2 = self.create_bip68txs(bip68inputs, 2)
285+
bip68txs_v1 = create_bip68txs(self.nodes[0], bip68inputs, 1, self.nodeaddress)
286+
bip68txs_v2 = create_bip68txs(self.nodes[0], bip68inputs, 2, self.nodeaddress)
287287

288288
# For BIP112 test:
289289
# 16 relative sequence locktimes of 10 against 10 OP_CSV OP_DROP inputs
290-
bip112txs_vary_nSequence_v1 = self.create_bip112txs(bip112basicinputs[0], False, 1)
291-
bip112txs_vary_nSequence_v2 = self.create_bip112txs(bip112basicinputs[0], False, 2)
290+
bip112txs_vary_nSequence_v1 = create_bip112txs(self.nodes[0], bip112basicinputs[0], False, 1, self.nodeaddress)
291+
bip112txs_vary_nSequence_v2 = create_bip112txs(self.nodes[0], bip112basicinputs[0], False, 2, self.nodeaddress)
292292
# 16 relative sequence locktimes of 9 against 10 OP_CSV OP_DROP inputs
293-
bip112txs_vary_nSequence_9_v1 = self.create_bip112txs(bip112basicinputs[1], False, 1, -1)
294-
bip112txs_vary_nSequence_9_v2 = self.create_bip112txs(bip112basicinputs[1], False, 2, -1)
293+
bip112txs_vary_nSequence_9_v1 = create_bip112txs(self.nodes[0], bip112basicinputs[1], False, 1, self.nodeaddress, -1)
294+
bip112txs_vary_nSequence_9_v2 = create_bip112txs(self.nodes[0], bip112basicinputs[1], False, 2, self.nodeaddress, -1)
295295
# sequence lock time of 10 against 16 (relative_lock_time) OP_CSV OP_DROP inputs
296-
bip112txs_vary_OP_CSV_v1 = self.create_bip112txs(bip112diverseinputs[0], True, 1)
297-
bip112txs_vary_OP_CSV_v2 = self.create_bip112txs(bip112diverseinputs[0], True, 2)
296+
bip112txs_vary_OP_CSV_v1 = create_bip112txs(self.nodes[0], bip112diverseinputs[0], True, 1, self.nodeaddress)
297+
bip112txs_vary_OP_CSV_v2 = create_bip112txs(self.nodes[0], bip112diverseinputs[0], True, 2, self.nodeaddress)
298298
# sequence lock time of 9 against 16 (relative_lock_time) OP_CSV OP_DROP inputs
299-
bip112txs_vary_OP_CSV_9_v1 = self.create_bip112txs(bip112diverseinputs[1], True, 1, -1)
300-
bip112txs_vary_OP_CSV_9_v2 = self.create_bip112txs(bip112diverseinputs[1], True, 2, -1)
299+
bip112txs_vary_OP_CSV_9_v1 = create_bip112txs(self.nodes[0], bip112diverseinputs[1], True, 1, self.nodeaddress, -1)
300+
bip112txs_vary_OP_CSV_9_v2 = create_bip112txs(self.nodes[0], bip112diverseinputs[1], True, 2, self.nodeaddress, -1)
301301
# -1 OP_CSV OP_DROP input
302-
bip112tx_special_v1 = self.create_bip112special(bip112specialinput, 1)
303-
bip112tx_special_v2 = self.create_bip112special(bip112specialinput, 2)
302+
bip112tx_special_v1 = create_bip112special(self.nodes[0], bip112specialinput, 1, self.nodeaddress)
303+
bip112tx_special_v2 = create_bip112special(self.nodes[0], bip112specialinput, 2, self.nodeaddress)
304304

305305
self.log.info("TESTING")
306306

@@ -310,7 +310,7 @@ def get_tests(self):
310310
success_txs = []
311311
# add BIP113 tx and -1 CSV tx
312312
bip113tx_v1.nLockTime = self.last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
313-
bip113signed1 = self.sign_transaction(self.nodes[0], bip113tx_v1)
313+
bip113signed1 = sign_transaction(self.nodes[0], bip113tx_v1)
314314
success_txs.append(bip113signed1)
315315
success_txs.append(bip112tx_special_v1)
316316
# add BIP 68 txs
@@ -329,7 +329,7 @@ def get_tests(self):
329329
success_txs = []
330330
# add BIP113 tx and -1 CSV tx
331331
bip113tx_v2.nLockTime = self.last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
332-
bip113signed2 = self.sign_transaction(self.nodes[0], bip113tx_v2)
332+
bip113signed2 = sign_transaction(self.nodes[0], bip113tx_v2)
333333
success_txs.append(bip113signed2)
334334
success_txs.append(bip112tx_special_v2)
335335
# add BIP 68 txs
@@ -353,16 +353,16 @@ def get_tests(self):
353353
self.log.info("BIP 113 tests")
354354
# BIP 113 tests should now fail regardless of version number if nLockTime isn't satisfied by new rules
355355
bip113tx_v1.nLockTime = self.last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
356-
bip113signed1 = self.sign_transaction(self.nodes[0], bip113tx_v1)
356+
bip113signed1 = sign_transaction(self.nodes[0], bip113tx_v1)
357357
bip113tx_v2.nLockTime = self.last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
358-
bip113signed2 = self.sign_transaction(self.nodes[0], bip113tx_v2)
358+
bip113signed2 = sign_transaction(self.nodes[0], bip113tx_v2)
359359
for bip113tx in [bip113signed1, bip113signed2]:
360360
yield TestInstance([[self.create_test_block([bip113tx]), False]])
361361
# BIP 113 tests should now pass if the locktime is < MTP
362362
bip113tx_v1.nLockTime = self.last_block_time - 600 * 5 - 1 # < MTP of prior block
363-
bip113signed1 = self.sign_transaction(self.nodes[0], bip113tx_v1)
363+
bip113signed1 = sign_transaction(self.nodes[0], bip113tx_v1)
364364
bip113tx_v2.nLockTime = self.last_block_time - 600 * 5 - 1 # < MTP of prior block
365-
bip113signed2 = self.sign_transaction(self.nodes[0], bip113tx_v2)
365+
bip113signed2 = sign_transaction(self.nodes[0], bip113tx_v2)
366366
for bip113tx in [bip113signed1, bip113signed2]:
367367
yield TestInstance([[self.create_test_block([bip113tx]), True]])
368368
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
@@ -476,7 +476,7 @@ def get_tests(self):
476476
time_txs = []
477477
for tx in [tx['tx'] for tx in bip112txs_vary_OP_CSV_v2 if not tx['sdf'] and tx['stf']]:
478478
tx.vin[0].nSequence = BASE_RELATIVE_LOCKTIME | SEQ_TYPE_FLAG
479-
signtx = self.sign_transaction(self.nodes[0], tx)
479+
signtx = sign_transaction(self.nodes[0], tx)
480480
time_txs.append(signtx)
481481

482482
yield TestInstance([[self.create_test_block(time_txs), True]])

0 commit comments

Comments
 (0)