@@ -37,9 +37,13 @@ def scan_blocks(self, *, start=1, num):
3737 for i in range (start , start + num ):
3838 block = self ._test_node .getblock (blockhash = self ._test_node .getblockhash (i ), verbosity = 2 )
3939 for tx in block ['tx' ]:
40- for out in tx ['vout' ]:
41- if out ['scriptPubKey' ]['hex' ] == self ._scriptPubKey .hex ():
42- self ._utxos .append ({'txid' : tx ['txid' ], 'vout' : out ['n' ], 'value' : out ['value' ]})
40+ self .scan_tx (tx )
41+
42+ def scan_tx (self , tx ):
43+ """Scan the tx for self._scriptPubKey outputs and add them to self._utxos"""
44+ for out in tx ['vout' ]:
45+ if out ['scriptPubKey' ]['hex' ] == self ._scriptPubKey .hex ():
46+ self ._utxos .append ({'txid' : tx ['txid' ], 'vout' : out ['n' ], 'value' : out ['value' ]})
4347
4448 def generate (self , num_blocks ):
4549 """Generate blocks with coinbase outputs to the internal address, and append the outputs to the internal list"""
@@ -69,6 +73,12 @@ def get_utxo(self, *, txid: Optional[str]=''):
6973
7074 def send_self_transfer (self , * , fee_rate = Decimal ("0.003" ), from_node , utxo_to_spend = None ):
7175 """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."""
7282 self ._utxos = sorted (self ._utxos , key = lambda k : k ['value' ])
7383 utxo_to_spend = utxo_to_spend or self ._utxos .pop () # Pick the largest utxo (if none provided) and hope it covers the fee
7484 vsize = Decimal (85 )
@@ -83,8 +93,12 @@ def send_self_transfer(self, *, fee_rate=Decimal("0.003"), from_node, utxo_to_sp
8393 tx_hex = tx .serialize ().hex ()
8494
8595 tx_info = from_node .testmempoolaccept ([tx_hex ])[0 ]
86- self . _utxos . append ({ 'txid' : tx_info ['txid' ], 'vout' : 0 , 'value' : send_value } )
87- from_node . sendrawtransaction ( tx_hex )
88- assert_equal (len (tx_hex ) // 2 , vsize )
89- assert_equal (tx_info ['fees' ]['base' ], fee )
96+ assert_equal ( mempool_valid , tx_info ['allowed' ] )
97+ if mempool_valid :
98+ assert_equal (len (tx_hex ) // 2 , vsize )
99+ assert_equal (tx_info ['fees' ]['base' ], fee )
90100 return {'txid' : tx_info ['txid' ], 'hex' : tx_hex }
101+
102+ def sendrawtransaction (self , * , from_node , tx_hex ):
103+ from_node .sendrawtransaction (tx_hex )
104+ self .scan_tx (from_node .decoderawtransaction (tx_hex ))
0 commit comments