Skip to content

Commit

Permalink
test: Test that an unconfirmed not-in-mempool chain is rebroadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
achow101 committed Aug 18, 2022
1 parent 9a55656 commit e40ddf3
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions test/functional/wallet_resendwallettransactions.py
Expand Up @@ -11,8 +11,10 @@
)
from test_framework.p2p import P2PTxInvStore
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal

from test_framework.util import (
assert_equal,
assert_raises_rpc_error,
)

class ResendWalletTransactionsTest(BitcoinTestFramework):
def set_test_params(self):
Expand All @@ -27,7 +29,10 @@ def run_test(self):
peer_first = node.add_p2p_connection(P2PTxInvStore())

self.log.info("Create a new transaction and wait until it's broadcast")
txid = node.sendtoaddress(node.getnewaddress(), 1)
unspent = node.listunspent()
parent_utxo = unspent[0]
indep_utxo = unspent[1]
txid = node.send(outputs=[{node.getnewaddress(): 1}], options={"inputs": [parent_utxo]})["txid"]

# Wallet rebroadcast is first scheduled 1 sec after startup (see
# nNextResend in ResendWalletTransactions()). Tell scheduler to call
Expand Down Expand Up @@ -74,6 +79,41 @@ def run_test(self):
node.setmocktime(now + 36 * 60 * 60 + 600)
peer_second.wait_for_broadcast([txid])

self.log.info("Chain of unconfirmed not-in-mempool txs are rebroadcast")
# Ideally we would add a transaction that would spend the existing transaction and also be
# in mapWallet before it. However mapWallet is a std::unsorted_map which means that it is
# hard for us to externally construct such a transaction. So we will ignore that fact and
# rely on the fact that because there are only two items in mapWallet, 50% of the time the
# child will be placed before the parent in mapWallet. The incorrect behavior that this
# test tries to catch should thus fail intermittently at a rate of 50%.
send_res = node.send(outputs=[{node.getnewaddress(): 0.5}], options={"inputs": [{"txid":txid, "vout":0}], "add_to_wallet": False})
child_txid = send_res["txid"]
node.sendrawtransaction(send_res["hex"])
entry_time = node.getmempoolentry(child_txid)["time"]

block_time = entry_time + 6 * 60
node.setmocktime(block_time)
block = create_block(int(node.getbestblockhash(), 16), create_coinbase(node.getblockcount() + 1), block_time)
block.solve()
node.submitblock(block.serialize().hex())
node.syncwithvalidationinterfacequeue()

# Evict these txs from the mempool
evict_time = block_time + 60 * 60 * 336 + 5
node.setmocktime(evict_time)
indep_send = node.send(outputs=[{node.getnewaddress(): 1}], options={"inputs": [indep_utxo]})
node.syncwithvalidationinterfacequeue()
node.getmempoolentry(indep_send["txid"])
assert_raises_rpc_error(-5, "Transaction not in mempool", node.getmempoolentry, txid)
assert_raises_rpc_error(-5, "Transaction not in mempool", node.getmempoolentry, child_txid)

# Rebroadcast and check that parent and child are both in the mempool
with node.assert_debug_log(['ResendWalletTransactions: resubmit 2 unconfirmed transactions']):
node.setmocktime(evict_time + 36 * 60 * 60)
node.mockscheduler(1)
node.getmempoolentry(txid)
node.getmempoolentry(child_txid)


if __name__ == '__main__':
ResendWalletTransactionsTest().main()

0 comments on commit e40ddf3

Please sign in to comment.