test: Speed up mempool_spend_coinbase.py

This commit is contained in:
MarcoFalke 2021-04-23 09:47:19 +02:00
parent fa29382ab2
commit fa40eb5b6b
No known key found for this signature in database
GPG Key ID: CE2B75697E69A548

View File

@ -20,41 +20,41 @@ from test_framework.wallet import MiniWallet
class MempoolSpendCoinbaseTest(BitcoinTestFramework): class MempoolSpendCoinbaseTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):
self.num_nodes = 1 self.num_nodes = 1
self.setup_clean_chain = True
def run_test(self): def run_test(self):
wallet = MiniWallet(self.nodes[0]) wallet = MiniWallet(self.nodes[0])
wallet.generate(200) # Invalidate two blocks, so that miniwallet has access to a coin that will mature in the next block
chain_height = self.nodes[0].getblockcount() chain_height = 198
assert_equal(chain_height, 200) self.nodes[0].invalidateblock(self.nodes[0].getblockhash(chain_height + 1))
assert_equal(chain_height, self.nodes[0].getblockcount())
# Coinbase at height chain_height-100+1 ok in mempool, should # Coinbase at height chain_height-100+1 ok in mempool, should
# get mined. Coinbase at height chain_height-100+2 is # get mined. Coinbase at height chain_height-100+2 is
# too immature to spend. # too immature to spend.
b = [self.nodes[0].getblockhash(n) for n in range(101, 103)] wallet.scan_blocks(start=chain_height - 100 + 1, num=1)
coinbase_txids = [self.nodes[0].getblock(h)['tx'][0] for h in b] utxo_mature = wallet.get_utxo()
utxo_101 = wallet.get_utxo(txid=coinbase_txids[0]) wallet.scan_blocks(start=chain_height - 100 + 2, num=1)
utxo_102 = wallet.get_utxo(txid=coinbase_txids[1]) utxo_immature = wallet.get_utxo()
spend_101_id = wallet.send_self_transfer(from_node=self.nodes[0], utxo_to_spend=utxo_101)["txid"] spend_mature_id = wallet.send_self_transfer(from_node=self.nodes[0], utxo_to_spend=utxo_mature)["txid"]
# coinbase at height 102 should be too immature to spend # other coinbase should be too immature to spend
immature_tx = wallet.create_self_transfer(from_node=self.nodes[0], utxo_to_spend=utxo_102, mempool_valid=False) immature_tx = wallet.create_self_transfer(from_node=self.nodes[0], utxo_to_spend=utxo_immature, mempool_valid=False)
assert_raises_rpc_error(-26, assert_raises_rpc_error(-26,
"bad-txns-premature-spend-of-coinbase", "bad-txns-premature-spend-of-coinbase",
lambda: self.nodes[0].sendrawtransaction(immature_tx['hex'])) lambda: self.nodes[0].sendrawtransaction(immature_tx['hex']))
# mempool should have just spend_101: # mempool should have just the mature one
assert_equal(self.nodes[0].getrawmempool(), [spend_101_id]) assert_equal(self.nodes[0].getrawmempool(), [spend_mature_id])
# mine a block, spend_101 should get confirmed # mine a block, mature one should get confirmed
self.nodes[0].generate(1) self.nodes[0].generate(1)
assert_equal(set(self.nodes[0].getrawmempool()), set()) assert_equal(set(self.nodes[0].getrawmempool()), set())
# ... and now height 102 can be spent: # ... and now previously immature can be spent:
spend_102_id = self.nodes[0].sendrawtransaction(immature_tx['hex']) spend_new_id = self.nodes[0].sendrawtransaction(immature_tx['hex'])
assert_equal(self.nodes[0].getrawmempool(), [spend_102_id]) assert_equal(self.nodes[0].getrawmempool(), [spend_new_id])
if __name__ == '__main__': if __name__ == '__main__':