2017-08-07 16:49:41 +02:00
|
|
|
#!/usr/bin/env python3
|
2019-04-07 00:38:51 +02:00
|
|
|
# Copyright (c) 2017-2019 The Bitcoin Core developers
|
2017-08-07 16:49:41 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2019-03-22 19:04:15 +01:00
|
|
|
"""Test that the wallet resends transactions periodically."""
|
|
|
|
from collections import defaultdict
|
|
|
|
import time
|
2017-08-07 16:49:41 +02:00
|
|
|
|
2019-03-22 19:04:15 +01:00
|
|
|
from test_framework.blocktools import create_block, create_coinbase
|
|
|
|
from test_framework.messages import ToHex
|
|
|
|
from test_framework.mininode import P2PInterface, mininode_lock
|
2017-08-07 16:49:41 +02:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2019-03-22 19:04:15 +01:00
|
|
|
from test_framework.util import assert_equal, wait_until
|
|
|
|
|
|
|
|
class P2PStoreTxInvs(P2PInterface):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.tx_invs_received = defaultdict(int)
|
|
|
|
|
|
|
|
def on_inv(self, message):
|
|
|
|
# Store how many times invs have been received for each tx.
|
|
|
|
for i in message.inv:
|
|
|
|
if i.type == 1:
|
|
|
|
# save txid
|
|
|
|
self.tx_invs_received[i.hash] += 1
|
2017-08-07 16:49:41 +02:00
|
|
|
|
|
|
|
class ResendWalletTransactionsTest(BitcoinTestFramework):
|
2017-06-10 00:21:21 +02:00
|
|
|
def set_test_params(self):
|
2017-08-07 16:49:41 +02:00
|
|
|
self.num_nodes = 1
|
2018-09-09 19:32:37 +02:00
|
|
|
|
|
|
|
def skip_test_if_missing_module(self):
|
|
|
|
self.skip_if_no_wallet()
|
2017-08-07 16:49:41 +02:00
|
|
|
|
2018-09-09 19:32:37 +02:00
|
|
|
def skip_test_if_missing_module(self):
|
|
|
|
self.skip_if_no_wallet()
|
|
|
|
|
2017-08-07 16:49:41 +02:00
|
|
|
def run_test(self):
|
2019-03-22 19:04:15 +01:00
|
|
|
node = self.nodes[0] # alias
|
|
|
|
|
|
|
|
node.add_p2p_connection(P2PStoreTxInvs())
|
|
|
|
|
|
|
|
self.log.info("Create a new transaction and wait until it's broadcast")
|
|
|
|
txid = int(node.sendtoaddress(node.getnewaddress(), 1), 16)
|
|
|
|
|
2019-03-20 22:46:38 +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)
|
|
|
|
|
2019-03-22 19:04:15 +01:00
|
|
|
# Can take a few seconds due to transaction trickling
|
|
|
|
wait_until(lambda: node.p2p.tx_invs_received[txid] >= 1, lock=mininode_lock)
|
|
|
|
|
|
|
|
# Add a second peer since txs aren't rebroadcast to the same peer (see filterInventoryKnown)
|
|
|
|
node.add_p2p_connection(P2PStoreTxInvs())
|
|
|
|
|
|
|
|
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 = int(time.time()) + 6 * 60
|
|
|
|
node.setmocktime(block_time)
|
2019-07-02 18:42:29 +02:00
|
|
|
block = create_block(int(node.getbestblockhash(), 16), create_coinbase(node.getblockcount() + 1), block_time)
|
2019-03-22 19:04:15 +01:00
|
|
|
block.rehash()
|
|
|
|
block.solve()
|
|
|
|
node.submitblock(ToHex(block))
|
2017-08-07 16:49:41 +02:00
|
|
|
|
2019-03-22 19:04:15 +01:00
|
|
|
# Transaction should not be rebroadcast
|
|
|
|
node.p2ps[1].sync_with_ping()
|
|
|
|
assert_equal(node.p2ps[1].tx_invs_received[txid], 0)
|
2017-08-07 16:49:41 +02:00
|
|
|
|
2019-03-22 19:04:15 +01:00
|
|
|
self.log.info("Transaction should be rebroadcast after 30 minutes")
|
|
|
|
# Use mocktime and give an extra 5 minutes to be sure.
|
|
|
|
rebroadcast_time = int(time.time()) + 41 * 60
|
|
|
|
node.setmocktime(rebroadcast_time)
|
|
|
|
wait_until(lambda: node.p2ps[1].tx_invs_received[txid] >= 1, lock=mininode_lock)
|
2017-08-07 16:49:41 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
ResendWalletTransactionsTest().main()
|