Skip to content

Commit fa085b4

Browse files
author
MarcoFalke
committed
test: Create MiniWallet.create_self_transfer
1 parent fa1bedb commit fa085b4

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

test/functional/mempool_spend_coinbase.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ def run_test(self):
4040
spend_101_id = wallet.send_self_transfer(from_node=self.nodes[0], utxo_to_spend=utxo_101)["txid"]
4141

4242
# coinbase at height 102 should be too immature to spend
43+
immature_tx = wallet.create_self_transfer(from_node=self.nodes[0], utxo_to_spend=utxo_102, mempool_valid=False)
4344
assert_raises_rpc_error(-26,
4445
"bad-txns-premature-spend-of-coinbase",
45-
lambda: wallet.send_self_transfer(from_node=self.nodes[0], utxo_to_spend=utxo_102))
46+
lambda: self.nodes[0].sendrawtransaction(immature_tx['hex']))
4647

4748
# mempool should have just spend_101:
4849
assert_equal(self.nodes[0].getrawmempool(), [spend_101_id])
@@ -52,7 +53,7 @@ def run_test(self):
5253
assert_equal(set(self.nodes[0].getrawmempool()), set())
5354

5455
# ... and now height 102 can be spent:
55-
spend_102_id = wallet.send_self_transfer(from_node=self.nodes[0], utxo_to_spend=utxo_102)["txid"]
56+
spend_102_id = self.nodes[0].sendrawtransaction(immature_tx['hex'])
5657
assert_equal(self.nodes[0].getrawmempool(), [spend_102_id])
5758

5859

test/functional/test_framework/wallet.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ def get_utxo(self, *, txid=''):
7373

7474
def send_self_transfer(self, *, fee_rate=Decimal("0.003"), from_node, utxo_to_spend=None):
7575
"""Create and send a tx with the specified fee_rate. Fee may be exact or at most one satoshi higher than needed."""
76+
tx = self.create_self_transfer(fee_rate=fee_rate, from_node=from_node, utxo_to_spend=utxo_to_spend)
77+
self.sendrawtransaction(from_node=from_node, tx_hex=tx['hex'])
78+
return tx
79+
80+
def create_self_transfer(self, *, fee_rate=Decimal("0.003"), from_node, utxo_to_spend=None, mempool_valid=True):
81+
"""Create and return a tx with the specified fee_rate. Fee may be exact or at most one satoshi higher than needed."""
7682
self._utxos = sorted(self._utxos, key=lambda k: k['value'])
7783
utxo_to_spend = utxo_to_spend or self._utxos.pop() # Pick the largest utxo (if none provided) and hope it covers the fee
7884
vsize = Decimal(96)
@@ -88,9 +94,10 @@ def send_self_transfer(self, *, fee_rate=Decimal("0.003"), from_node, utxo_to_sp
8894
tx_hex = tx.serialize().hex()
8995

9096
tx_info = from_node.testmempoolaccept([tx_hex])[0]
91-
self.sendrawtransaction(from_node=from_node, tx_hex=tx_hex)
92-
assert_equal(tx_info['vsize'], vsize)
93-
assert_equal(tx_info['fees']['base'], fee)
97+
assert_equal(mempool_valid, tx_info['allowed'])
98+
if mempool_valid:
99+
assert_equal(tx_info['vsize'], vsize)
100+
assert_equal(tx_info['fees']['base'], fee)
94101
return {'txid': tx_info['txid'], 'wtxid': tx_info['wtxid'], 'hex': tx_hex}
95102

96103
def sendrawtransaction(self, *, from_node, tx_hex):

0 commit comments

Comments
 (0)