2017-08-12 13:18:17 +02:00
|
|
|
#!/usr/bin/env python3
|
2023-08-16 19:27:31 +02:00
|
|
|
# Copyright (c) 2017-2020 The Bitcoin Core developers
|
2017-08-12 13:18:17 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2021-12-12 14:38:12 +01:00
|
|
|
"""Test that the wallet resends transactions periodically."""
|
|
|
|
import time
|
2017-08-12 13:18:17 +02:00
|
|
|
|
2021-12-12 14:38:12 +01:00
|
|
|
from test_framework.blocktools import create_block, create_coinbase
|
|
|
|
from test_framework.messages import ToHex
|
2020-08-27 08:21:53 +02:00
|
|
|
from test_framework.p2p import P2PTxInvStore
|
2017-08-12 13:18:17 +02:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2020-08-27 08:21:53 +02:00
|
|
|
from test_framework.util import assert_equal
|
2021-12-12 14:38:12 +01:00
|
|
|
|
2017-08-12 13:18:17 +02:00
|
|
|
class ResendWalletTransactionsTest(BitcoinTestFramework):
|
2017-09-01 18:47:13 +02:00
|
|
|
def set_test_params(self):
|
2017-08-12 13:18:17 +02:00
|
|
|
self.num_nodes = 1
|
|
|
|
|
2018-09-13 12:33:15 +02:00
|
|
|
def skip_test_if_missing_module(self):
|
|
|
|
self.skip_if_no_wallet()
|
|
|
|
|
2017-08-12 13:18:17 +02:00
|
|
|
def run_test(self):
|
2021-12-12 14:38:12 +01:00
|
|
|
node = self.nodes[0] # alias
|
|
|
|
|
2020-09-25 14:18:21 +02:00
|
|
|
peer_first = node.add_p2p_connection(P2PTxInvStore())
|
2021-12-12 14:38:12 +01:00
|
|
|
|
|
|
|
self.log.info("Create a new transaction and wait until it's broadcast")
|
2020-08-27 08:21:53 +02:00
|
|
|
txid = node.sendtoaddress(node.getnewaddress(), 1)
|
2021-12-12 14:38:12 +01:00
|
|
|
|
2021-12-12 16:57:51 +01:00
|
|
|
# Wallet rebroadcast is first scheduled 1 sec after startup (see
|
|
|
|
# nNextResend in ResendWalletTransactions()). Sleep for just over a
|
|
|
|
# second to be certain that it has been called before the first
|
|
|
|
# setmocktime call below.
|
|
|
|
time.sleep(1.1)
|
|
|
|
|
2021-12-12 14:38:12 +01:00
|
|
|
# Can take a few seconds due to transaction trickling
|
|
|
|
def wait_p2p():
|
|
|
|
self.bump_mocktime(1)
|
2020-09-25 14:18:21 +02:00
|
|
|
return peer_first.tx_invs_received[int(txid, 16)] >= 1
|
2020-08-27 08:21:53 +02:00
|
|
|
self.wait_until(wait_p2p)
|
2021-12-12 14:38:12 +01:00
|
|
|
|
|
|
|
# Add a second peer since txs aren't rebroadcast to the same peer (see filterInventoryKnown)
|
2020-09-25 14:18:21 +02:00
|
|
|
peer_second = node.add_p2p_connection(P2PTxInvStore())
|
2021-12-12 14:38:12 +01:00
|
|
|
|
|
|
|
self.log.info("Create a block")
|
|
|
|
# Create and submit a block without the transaction.
|
|
|
|
# Transactions are only rebroadcast if there has been a block at least five minutes
|
|
|
|
# after the last time we tried to broadcast. Use mocktime and give an extra minute to be sure.
|
|
|
|
block_time = self.mocktime + 6 * 60
|
|
|
|
node.setmocktime(block_time)
|
2019-07-03 14:49:07 +02:00
|
|
|
block = create_block(int(node.getbestblockhash(), 16), create_coinbase(node.getblockcount() + 1), block_time)
|
2021-12-12 14:38:12 +01:00
|
|
|
block.rehash()
|
|
|
|
block.solve()
|
|
|
|
node.submitblock(ToHex(block))
|
|
|
|
|
2020-12-06 19:12:49 +01:00
|
|
|
# Set correct m_best_block_time, which is used in ResendWalletTransactions
|
2020-03-12 14:20:42 +01:00
|
|
|
node.syncwithvalidationinterfacequeue()
|
2020-05-30 18:22:03 +02:00
|
|
|
|
|
|
|
# Transaction should not be rebroadcast within first 12 hours
|
|
|
|
# Leave 2 mins for buffer
|
|
|
|
twelve_hrs = 12 * 60 * 60
|
|
|
|
two_min = 2 * 60
|
|
|
|
node.setmocktime(self.mocktime + twelve_hrs - two_min)
|
|
|
|
self.mocktime = self.mocktime + twelve_hrs - two_min
|
|
|
|
time.sleep(2) # ensure enough time has passed for rebroadcast attempt to occur
|
2020-09-26 12:55:16 +02:00
|
|
|
assert_equal(int(txid, 16) in peer_second.get_invs(), False)
|
2017-08-12 13:18:17 +02:00
|
|
|
|
2022-05-06 06:04:50 +02:00
|
|
|
self.log.info("Bump time & check that transaction is rebroadcast")
|
2020-08-27 08:21:53 +02:00
|
|
|
# Transaction should be rebroadcast approximately 24 hours in the future,
|
|
|
|
# but can range from 12-36. So bump 36 hours to be sure.
|
2020-10-08 11:38:36 +02:00
|
|
|
node.setmocktime(self.mocktime + 36 * 60 * 60)
|
|
|
|
# Tell scheduler to call MaybeResendWalletTxn now.
|
|
|
|
node.mockscheduler(1)
|
|
|
|
# Give some time for trickle to occur
|
|
|
|
node.setmocktime(self.mocktime + 36 * 60 * 60 + 600)
|
2020-09-25 14:18:21 +02:00
|
|
|
peer_second.wait_for_broadcast([txid])
|
2017-08-12 13:18:17 +02:00
|
|
|
|
2020-03-12 14:20:42 +01:00
|
|
|
|
2017-08-12 13:18:17 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
ResendWalletTransactionsTest().main()
|