Skip to content

Commit 736f941

Browse files
committed
[Tests] Cleanup extra instances of create_transaction
1 parent 1576518 commit 736f941

File tree

9 files changed

+46
-73
lines changed

9 files changed

+46
-73
lines changed

test/functional/feature_block.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import struct
88
import time
99

10-
from test_framework.blocktools import create_block, create_coinbase, create_transaction, get_legacy_sigopcount_block
10+
from test_framework.blocktools import create_block, create_coinbase, create_tx_with_script, get_legacy_sigopcount_block
1111
from test_framework.key import CECKey
1212
from test_framework.messages import (
1313
CBlock,
@@ -1217,7 +1217,7 @@ def add_transactions_to_block(self, block, tx_list):
12171217

12181218
# this is a little handier to use than the version in blocktools.py
12191219
def create_tx(self, spend_tx, n, value, script=CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE])):
1220-
return create_transaction(spend_tx, n, b"", value, script)
1220+
return create_tx_with_script(spend_tx, n, b"", value, script)
12211221

12221222
# sign a transaction, using the key we know about
12231223
# this signs input 0 in tx, which is assumed to be spending output n in spend_tx
@@ -1253,7 +1253,7 @@ def next_block(self, number, spend=None, additional_coinbase_value=0, script=CSc
12531253
coinbase.vout[0].nValue += spend.tx.vout[spend.n].nValue - 1 # all but one satoshi to fees
12541254
coinbase.rehash()
12551255
block = create_block(base_block_hash, coinbase, block_time)
1256-
tx = create_transaction(spend.tx, spend.n, b"", 1, script) # spend 1 satoshi
1256+
tx = create_tx_with_script(spend.tx, spend.n, b"", 1, script) # spend 1 satoshi
12571257
self.sign_tx(tx, spend.tx, spend.n)
12581258
self.add_transactions_to_block(block, [tx])
12591259
block.hashMerkleRoot = block.calc_merkle_root()

test/functional/feature_cltv.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from test_framework.test_framework import BitcoinTestFramework
1212
from test_framework.util import *
1313
from test_framework.mininode import *
14-
from test_framework.blocktools import create_coinbase, create_block
14+
from test_framework.blocktools import create_coinbase, create_block, create_transaction
1515
from test_framework.script import CScript, OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP, CScriptNum
1616
from io import BytesIO
1717

@@ -49,16 +49,6 @@ def cltv_validate(node, tx, height):
4949
list(CScript(new_tx.vin[0].scriptSig)))
5050
return new_tx
5151

52-
def create_transaction(node, coinbase, to_address, amount):
53-
from_txid = node.getblock(coinbase)['tx'][0]
54-
inputs = [{ "txid" : from_txid, "vout" : 0}]
55-
outputs = { to_address : amount }
56-
rawtx = node.createrawtransaction(inputs, outputs)
57-
signresult = node.signrawtransactionwithwallet(rawtx)
58-
tx = CTransaction()
59-
tx.deserialize(BytesIO(hex_str_to_bytes(signresult['hex'])))
60-
return tx
61-
6252
class BIP65Test(BitcoinTestFramework):
6353
def set_test_params(self):
6454
self.num_nodes = 1
@@ -70,12 +60,12 @@ def run_test(self):
7060
self.nodes[0].p2p.wait_for_verack()
7161

7262
self.log.info("Mining %d blocks", CLTV_HEIGHT - 2)
73-
self.coinbase_blocks = self.nodes[0].generate(CLTV_HEIGHT - 2)
63+
self.coinbase_txids = [self.nodes[0].getblock(b)['tx'][0] for b in self.nodes[0].generate(CLTV_HEIGHT - 2)]
7464
self.nodeaddress = self.nodes[0].getnewaddress()
7565

7666
self.log.info("Test that an invalid-according-to-CLTV transaction can still appear in a block")
7767

78-
spendtx = create_transaction(self.nodes[0], self.coinbase_blocks[0],
68+
spendtx = create_transaction(self.nodes[0], self.coinbase_txids[0],
7969
self.nodeaddress, 1.0)
8070
cltv_invalidate(spendtx)
8171
spendtx.rehash()
@@ -110,7 +100,7 @@ def run_test(self):
110100
self.log.info("Test that invalid-according-to-cltv transactions cannot appear in a block")
111101
block.nVersion = 4
112102

113-
spendtx = create_transaction(self.nodes[0], self.coinbase_blocks[1],
103+
spendtx = create_transaction(self.nodes[0], self.coinbase_txids[1],
114104
self.nodeaddress, 1.0)
115105
cltv_invalidate(spendtx)
116106
spendtx.rehash()

test/functional/feature_csv_activation.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
from io import BytesIO
4848
import time
4949

50-
from test_framework.blocktools import create_coinbase, create_block
50+
from test_framework.blocktools import create_coinbase, create_block, create_transaction
5151
from test_framework.messages import ToHex, CTransaction
5252
from test_framework.mininode import P2PDataStore
5353
from test_framework.script import (
@@ -85,15 +85,6 @@ 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-
9788
def sign_transaction(node, unsignedtx):
9889
rawtx = ToHex(unsignedtx)
9990
signresult = node.signrawtransactionwithwallet(rawtx)

test/functional/feature_dersig.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
from test_framework.test_framework import BitcoinTestFramework
1111
from test_framework.util import *
1212
from test_framework.mininode import *
13-
from test_framework.blocktools import create_coinbase, create_block
13+
from test_framework.blocktools import create_coinbase, create_block, create_transaction
1414
from test_framework.script import CScript
15-
from io import BytesIO
1615

1716
DERSIG_HEIGHT = 1251
1817

@@ -37,15 +36,6 @@ def unDERify(tx):
3736
newscript.append(i)
3837
tx.vin[0].scriptSig = CScript(newscript)
3938

40-
def create_transaction(node, coinbase, to_address, amount):
41-
from_txid = node.getblock(coinbase)['tx'][0]
42-
inputs = [{ "txid" : from_txid, "vout" : 0}]
43-
outputs = { to_address : amount }
44-
rawtx = node.createrawtransaction(inputs, outputs)
45-
signresult = node.signrawtransactionwithwallet(rawtx)
46-
tx = CTransaction()
47-
tx.deserialize(BytesIO(hex_str_to_bytes(signresult['hex'])))
48-
return tx
4939

5040

5141
class BIP66Test(BitcoinTestFramework):
@@ -61,12 +51,12 @@ def run_test(self):
6151
self.nodes[0].p2p.wait_for_verack()
6252

6353
self.log.info("Mining %d blocks", DERSIG_HEIGHT - 2)
64-
self.coinbase_blocks = self.nodes[0].generate(DERSIG_HEIGHT - 2)
54+
self.coinbase_txids = [self.nodes[0].getblock(b)['tx'][0] for b in self.nodes[0].generate(DERSIG_HEIGHT - 2)]
6555
self.nodeaddress = self.nodes[0].getnewaddress()
6656

6757
self.log.info("Test that a transaction with non-DER signature can still appear in a block")
6858

69-
spendtx = create_transaction(self.nodes[0], self.coinbase_blocks[0],
59+
spendtx = create_transaction(self.nodes[0], self.coinbase_txids[0],
7060
self.nodeaddress, 1.0)
7161
unDERify(spendtx)
7262
spendtx.rehash()
@@ -103,7 +93,7 @@ def run_test(self):
10393
self.log.info("Test that transactions with non-DER signatures cannot appear in a block")
10494
block.nVersion = 3
10595

106-
spendtx = create_transaction(self.nodes[0], self.coinbase_blocks[1],
96+
spendtx = create_transaction(self.nodes[0], self.coinbase_txids[1],
10797
self.nodeaddress, 1.0)
10898
unDERify(spendtx)
10999
spendtx.rehash()
@@ -141,7 +131,7 @@ def run_test(self):
141131

142132
self.log.info("Test that a version 3 block with a DERSIG-compliant transaction is accepted")
143133
block.vtx[1] = create_transaction(self.nodes[0],
144-
self.coinbase_blocks[1], self.nodeaddress, 1.0)
134+
self.coinbase_txids[1], self.nodeaddress, 1.0)
145135
block.hashMerkleRoot = block.calc_merkle_root()
146136
block.rehash()
147137
block.solve()

test/functional/feature_nulldummy.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
from test_framework.test_framework import BitcoinTestFramework
1717
from test_framework.util import *
1818
from test_framework.messages import CTransaction
19-
from test_framework.blocktools import create_coinbase, create_block, add_witness_commitment
19+
from test_framework.blocktools import create_coinbase, create_block, create_transaction, add_witness_commitment
2020
from test_framework.script import CScript
21-
from io import BytesIO
2221
import time
2322

2423
NULLDUMMY_ERROR = "non-mandatory-script-verify-flag (Dummy CHECKMULTISIG argument must be zero) (code 64)"
@@ -61,31 +60,31 @@ def run_test(self):
6160
self.lastblocktime = int(time.time()) + 429
6261

6362
self.log.info("Test 1: NULLDUMMY compliant base transactions should be accepted to mempool and mined before activation [430]")
64-
test1txs = [self.create_transaction(self.nodes[0], coinbase_txid[0], self.ms_address, 49)]
63+
test1txs = [create_transaction(self.nodes[0], coinbase_txid[0], self.ms_address, 49)]
6564
txid1 = self.nodes[0].sendrawtransaction(bytes_to_hex_str(test1txs[0].serialize_with_witness()), True)
66-
test1txs.append(self.create_transaction(self.nodes[0], txid1, self.ms_address, 48))
65+
test1txs.append(create_transaction(self.nodes[0], txid1, self.ms_address, 48))
6766
txid2 = self.nodes[0].sendrawtransaction(bytes_to_hex_str(test1txs[1].serialize_with_witness()), True)
68-
test1txs.append(self.create_transaction(self.nodes[0], coinbase_txid[1], self.wit_ms_address, 49))
67+
test1txs.append(create_transaction(self.nodes[0], coinbase_txid[1], self.wit_ms_address, 49))
6968
txid3 = self.nodes[0].sendrawtransaction(bytes_to_hex_str(test1txs[2].serialize_with_witness()), True)
7069
self.block_submit(self.nodes[0], test1txs, False, True)
7170

7271
self.log.info("Test 2: Non-NULLDUMMY base multisig transaction should not be accepted to mempool before activation")
73-
test2tx = self.create_transaction(self.nodes[0], txid2, self.ms_address, 47)
72+
test2tx = create_transaction(self.nodes[0], txid2, self.ms_address, 47)
7473
trueDummy(test2tx)
7574
assert_raises_rpc_error(-26, NULLDUMMY_ERROR, self.nodes[0].sendrawtransaction, bytes_to_hex_str(test2tx.serialize_with_witness()), True)
7675

7776
self.log.info("Test 3: Non-NULLDUMMY base transactions should be accepted in a block before activation [431]")
7877
self.block_submit(self.nodes[0], [test2tx], False, True)
7978

8079
self.log.info("Test 4: Non-NULLDUMMY base multisig transaction is invalid after activation")
81-
test4tx = self.create_transaction(self.nodes[0], test2tx.hash, self.address, 46)
80+
test4tx = create_transaction(self.nodes[0], test2tx.hash, self.address, 46)
8281
test6txs=[CTransaction(test4tx)]
8382
trueDummy(test4tx)
8483
assert_raises_rpc_error(-26, NULLDUMMY_ERROR, self.nodes[0].sendrawtransaction, bytes_to_hex_str(test4tx.serialize_with_witness()), True)
8584
self.block_submit(self.nodes[0], [test4tx])
8685

8786
self.log.info("Test 5: Non-NULLDUMMY P2WSH multisig transaction invalid after activation")
88-
test5tx = self.create_transaction(self.nodes[0], txid3, self.wit_address, 48)
87+
test5tx = create_transaction(self.nodes[0], txid3, self.wit_address, 48)
8988
test6txs.append(CTransaction(test5tx))
9089
test5tx.wit.vtxinwit[0].scriptWitness.stack[0] = b'\x01'
9190
assert_raises_rpc_error(-26, NULLDUMMY_ERROR, self.nodes[0].sendrawtransaction, bytes_to_hex_str(test5tx.serialize_with_witness()), True)
@@ -97,17 +96,6 @@ def run_test(self):
9796
self.block_submit(self.nodes[0], test6txs, True, True)
9897

9998

100-
def create_transaction(self, node, txid, to_address, amount):
101-
inputs = [{ "txid" : txid, "vout" : 0}]
102-
outputs = { to_address : amount }
103-
rawtx = node.createrawtransaction(inputs, outputs)
104-
signresult = node.signrawtransactionwithwallet(rawtx)
105-
tx = CTransaction()
106-
f = BytesIO(hex_str_to_bytes(signresult['hex']))
107-
tx.deserialize(f)
108-
return tx
109-
110-
11199
def block_submit(self, node, txs, witness = False, accept = False):
112100
block = create_block(self.tip, create_coinbase(self.lastblockheight + 1), self.lastblocktime + 1)
113101
block.nVersion = 4

test/functional/p2p_invalid_block.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"""
1313
import copy
1414

15-
from test_framework.blocktools import create_block, create_coinbase, create_transaction
15+
from test_framework.blocktools import create_block, create_coinbase, create_tx_with_script
1616
from test_framework.messages import COIN
1717
from test_framework.mininode import P2PDataStore
1818
from test_framework.test_framework import BitcoinTestFramework
@@ -64,8 +64,8 @@ def run_test(self):
6464
block_time += 1
6565

6666
# b'0x51' is OP_TRUE
67-
tx1 = create_transaction(block1.vtx[0], 0, b'\x51', 50 * COIN)
68-
tx2 = create_transaction(tx1, 0, b'\x51', 50 * COIN)
67+
tx1 = create_tx_with_script(block1.vtx[0], 0, script_sig=b'\x51', amount=50 * COIN)
68+
tx2 = create_tx_with_script(tx1, 0, script_sig=b'\x51', amount=50 * COIN)
6969

7070
block2.vtx.extend([tx1, tx2])
7171
block2.hashMerkleRoot = block2.calc_merkle_root()

test/functional/p2p_invalid_tx.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""Test node responses to invalid transactions.
66
77
In this test we connect to one node over p2p, and test tx requests."""
8-
from test_framework.blocktools import create_block, create_coinbase, create_transaction
8+
from test_framework.blocktools import create_block, create_coinbase, create_tx_with_script
99
from test_framework.messages import (
1010
COIN,
1111
COutPoint,
@@ -68,7 +68,7 @@ def run_test(self):
6868
# Transaction will be rejected with code 16 (REJECT_INVALID)
6969
# and we get disconnected immediately
7070
self.log.info('Test a transaction that is rejected')
71-
tx1 = create_transaction(block1.vtx[0], 0, b'\x64' * 35, 50 * COIN - 12000)
71+
tx1 = create_tx_with_script(block1.vtx[0], 0, script_sig=b'\x64' * 35, amount=50 * COIN - 12000)
7272
node.p2p.send_txs_and_test([tx1], node, success=False, expect_disconnect=True)
7373

7474
# Make two p2p connections to provide the node with orphans

test/functional/p2p_unrequested_blocks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
from test_framework.test_framework import BitcoinTestFramework
5656
from test_framework.util import *
5757
import time
58-
from test_framework.blocktools import create_block, create_coinbase, create_transaction
58+
from test_framework.blocktools import create_block, create_coinbase, create_tx_with_script
5959

6060

6161
class AcceptBlockTest(BitcoinTestFramework):
@@ -244,7 +244,7 @@ def run_test(self):
244244
block_290f.solve()
245245
block_291 = create_block(block_290f.sha256, create_coinbase(291), block_290f.nTime+1)
246246
# block_291 spends a coinbase below maturity!
247-
block_291.vtx.append(create_transaction(block_290f.vtx[0], 0, b"42", 1))
247+
block_291.vtx.append(create_tx_with_script(block_290f.vtx[0], 0, script_sig=b"42", amount=1))
248248
block_291.hashMerkleRoot = block_291.calc_merkle_root()
249249
block_291.solve()
250250
block_292 = create_block(block_291.sha256, create_coinbase(292), block_291.nTime+1)

test/functional/test_framework/blocktools.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
hash160,
4040
)
4141
from .util import assert_equal
42+
from io import BytesIO
4243

4344
# From BIP141
4445
WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed"
@@ -117,17 +118,30 @@ def create_coinbase(height, pubkey=None):
117118
coinbase.calc_sha256()
118119
return coinbase
119120

120-
def create_transaction(prevtx, n, sig, value, script_pub_key=CScript()):
121-
"""Create a transaction.
121+
def create_tx_with_script(prevtx, n, script_sig=b"", amount=1, script_pub_key=CScript()):
122+
"""Return one-input, one-output transaction object
123+
spending the prevtx's n-th output with the given amount.
122124
123-
If the script_pub_key is not specified, make it anyone-can-spend."""
125+
Can optionally pass scriptPubKey and scriptSig, default is anyone-can-spend ouput.
126+
"""
124127
tx = CTransaction()
125128
assert(n < len(prevtx.vout))
126-
tx.vin.append(CTxIn(COutPoint(prevtx.sha256, n), sig, 0xffffffff))
127-
tx.vout.append(CTxOut(value, script_pub_key))
129+
tx.vin.append(CTxIn(COutPoint(prevtx.sha256, n), script_sig, 0xffffffff))
130+
tx.vout.append(CTxOut(amount, script_pub_key))
128131
tx.calc_sha256()
129132
return tx
130133

134+
def create_transaction(node, txid, to_address, amount):
135+
""" Return signed transaction spending the first output of the
136+
input txid. Note that the node must be able to sign for the
137+
output that is being spent, and the node must not be running
138+
multiple wallets.
139+
"""
140+
raw_tx = create_raw_transaction(node, txid, to_address, amount)
141+
tx = CTransaction()
142+
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx)))
143+
return tx
144+
131145
def create_raw_transaction(node, txid, to_address, amount):
132146
""" Return raw signed transaction spending the first output of the
133147
input txid. Note that the node must be able to sign for the

0 commit comments

Comments
 (0)