2016-12-12 16:18:11 +01:00
|
|
|
#!/usr/bin/env python3
|
2020-12-31 09:48:25 +01:00
|
|
|
# Copyright (c) 2014-2020 The Bitcoin Core developers
|
2016-12-12 16:18:11 +01:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-01-18 00:34:40 +01:00
|
|
|
"""Test wallet import RPCs.
|
|
|
|
|
|
|
|
Test rescan behavior of importaddress, importpubkey, importprivkey, and
|
2017-02-14 21:00:34 +01:00
|
|
|
importmulti RPCs with different types of keys and rescan options.
|
|
|
|
|
2017-02-24 18:39:33 +01:00
|
|
|
In the first part of the test, node 0 creates an address for each type of
|
|
|
|
import RPC call and sends BTC to it. Then other nodes import the addresses,
|
|
|
|
and the test makes listtransactions and getbalance calls to confirm that the
|
|
|
|
importing node either did or did not execute rescans picking up the send
|
|
|
|
transactions.
|
2017-02-14 21:00:34 +01:00
|
|
|
|
|
|
|
In the second part of the test, node 0 sends more BTC to each address, and the
|
|
|
|
test makes more listtransactions and getbalance calls to confirm that the
|
|
|
|
importing nodes pick up the new transactions regardless of whether rescans
|
|
|
|
happened previously.
|
|
|
|
"""
|
2016-12-12 16:18:11 +01:00
|
|
|
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2019-07-25 23:11:31 +02:00
|
|
|
from test_framework.address import AddressType
|
2019-04-07 00:38:51 +02:00
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal,
|
|
|
|
set_node_times,
|
|
|
|
)
|
2016-12-12 16:18:11 +01:00
|
|
|
|
|
|
|
import collections
|
2019-07-25 23:14:47 +02:00
|
|
|
from decimal import Decimal
|
2016-12-12 16:18:11 +01:00
|
|
|
import enum
|
|
|
|
import itertools
|
2019-07-25 23:14:47 +02:00
|
|
|
import random
|
2016-12-12 16:18:11 +01:00
|
|
|
|
2016-11-28 23:19:27 +01:00
|
|
|
Call = enum.Enum("Call", "single multiaddress multiscript")
|
2016-12-12 16:18:11 +01:00
|
|
|
Data = enum.Enum("Data", "address pub priv")
|
2017-02-14 23:39:26 +01:00
|
|
|
Rescan = enum.Enum("Rescan", "no yes late_timestamp")
|
2017-02-14 21:00:34 +01:00
|
|
|
|
|
|
|
|
2019-07-25 23:11:31 +02:00
|
|
|
class Variant(collections.namedtuple("Variant", "call data address_type rescan prune")):
|
2017-02-14 21:00:34 +01:00
|
|
|
"""Helper for importing one key and verifying scanned transactions."""
|
2017-02-14 23:39:26 +01:00
|
|
|
def do_import(self, timestamp):
|
2017-02-14 21:00:34 +01:00
|
|
|
"""Call one key import RPC."""
|
2018-04-24 21:50:00 +02:00
|
|
|
rescan = self.rescan == Rescan.yes
|
2017-02-14 21:00:34 +01:00
|
|
|
|
2019-07-25 23:11:31 +02:00
|
|
|
assert_equal(self.address["solvable"], True)
|
|
|
|
assert_equal(self.address["isscript"], self.address_type == AddressType.p2sh_segwit)
|
|
|
|
assert_equal(self.address["iswitness"], self.address_type == AddressType.bech32)
|
|
|
|
if self.address["isscript"]:
|
|
|
|
assert_equal(self.address["embedded"]["isscript"], False)
|
|
|
|
assert_equal(self.address["embedded"]["iswitness"], True)
|
|
|
|
|
2017-02-14 21:00:34 +01:00
|
|
|
if self.call == Call.single:
|
|
|
|
if self.data == Data.address:
|
2019-04-22 20:30:02 +02:00
|
|
|
response = self.node.importaddress(address=self.address["address"], label=self.label, rescan=rescan)
|
2017-02-14 21:00:34 +01:00
|
|
|
elif self.data == Data.pub:
|
2019-04-22 20:30:02 +02:00
|
|
|
response = self.node.importpubkey(pubkey=self.address["pubkey"], label=self.label, rescan=rescan)
|
2017-02-14 21:00:34 +01:00
|
|
|
elif self.data == Data.priv:
|
2019-04-22 20:30:02 +02:00
|
|
|
response = self.node.importprivkey(privkey=self.key, label=self.label, rescan=rescan)
|
2017-02-14 21:00:34 +01:00
|
|
|
assert_equal(response, None)
|
2017-07-12 16:29:02 +02:00
|
|
|
|
2016-11-28 23:19:27 +01:00
|
|
|
elif self.call in (Call.multiaddress, Call.multiscript):
|
2019-07-25 23:11:31 +02:00
|
|
|
request = {
|
2017-02-14 21:00:34 +01:00
|
|
|
"scriptPubKey": {
|
|
|
|
"address": self.address["address"]
|
2016-11-28 23:19:27 +01:00
|
|
|
} if self.call == Call.multiaddress else self.address["scriptPubKey"],
|
2017-03-02 18:20:34 +01:00
|
|
|
"timestamp": timestamp + TIMESTAMP_WINDOW + (1 if self.rescan == Rescan.late_timestamp else 0),
|
2017-02-14 21:00:34 +01:00
|
|
|
"pubkeys": [self.address["pubkey"]] if self.data == Data.pub else [],
|
|
|
|
"keys": [self.key] if self.data == Data.priv else [],
|
2018-10-06 06:48:23 +02:00
|
|
|
"label": self.label,
|
2017-02-14 21:00:34 +01:00
|
|
|
"watchonly": self.data != Data.priv
|
2019-07-25 23:11:31 +02:00
|
|
|
}
|
|
|
|
if self.address_type == AddressType.p2sh_segwit and self.data != Data.address:
|
|
|
|
# We need solving data when providing a pubkey or privkey as data
|
|
|
|
request.update({"redeemscript": self.address['embedded']['scriptPubKey']})
|
|
|
|
response = self.node.importmulti(
|
|
|
|
requests=[request],
|
|
|
|
options={"rescan": self.rescan in (Rescan.yes, Rescan.late_timestamp)},
|
|
|
|
)
|
2017-02-14 21:00:34 +01:00
|
|
|
assert_equal(response, [{"success": True}])
|
|
|
|
|
2019-04-24 16:08:26 +02:00
|
|
|
def check(self, txid=None, amount=None, confirmation_height=None):
|
2018-10-06 06:48:23 +02:00
|
|
|
"""Verify that listtransactions/listreceivedbyaddress return expected values."""
|
|
|
|
|
|
|
|
txs = self.node.listtransactions(label=self.label, count=10000, include_watchonly=True)
|
2019-04-24 16:08:26 +02:00
|
|
|
current_height = self.node.getblockcount()
|
2018-10-06 06:48:23 +02:00
|
|
|
assert_equal(len(txs), self.expected_txs)
|
2017-02-14 21:00:34 +01:00
|
|
|
|
2018-04-24 21:50:00 +02:00
|
|
|
addresses = self.node.listreceivedbyaddress(minconf=0, include_watchonly=True, address_filter=self.address['address'])
|
|
|
|
if self.expected_txs:
|
|
|
|
assert_equal(len(addresses[0]["txids"]), self.expected_txs)
|
2017-02-14 21:00:34 +01:00
|
|
|
|
|
|
|
if txid is not None:
|
2018-10-06 06:48:23 +02:00
|
|
|
tx, = [tx for tx in txs if tx["txid"] == txid]
|
|
|
|
assert_equal(tx["label"], self.label)
|
|
|
|
assert_equal(tx["address"], self.address["address"])
|
|
|
|
assert_equal(tx["amount"], amount)
|
|
|
|
assert_equal(tx["category"], "receive")
|
|
|
|
assert_equal(tx["label"], self.label)
|
|
|
|
assert_equal(tx["txid"], txid)
|
2019-04-24 16:08:26 +02:00
|
|
|
assert_equal(tx["confirmations"], 1 + current_height - confirmation_height)
|
2018-10-06 06:48:23 +02:00
|
|
|
assert_equal("trusted" not in tx, True)
|
|
|
|
|
2018-04-24 21:50:00 +02:00
|
|
|
address, = [ad for ad in addresses if txid in ad["txids"]]
|
|
|
|
assert_equal(address["address"], self.address["address"])
|
|
|
|
assert_equal(address["amount"], self.expected_balance)
|
2019-04-24 16:08:26 +02:00
|
|
|
assert_equal(address["confirmations"], 1 + current_height - confirmation_height)
|
2017-02-23 19:05:40 +01:00
|
|
|
# Verify the transaction is correctly marked watchonly depending on
|
|
|
|
# whether the transaction pays to an imported public key or
|
|
|
|
# imported private key. The test setup ensures that transaction
|
|
|
|
# inputs will not be from watchonly keys (important because
|
|
|
|
# involvesWatchonly will be true if either the transaction output
|
|
|
|
# or inputs are watchonly).
|
2017-02-14 21:00:34 +01:00
|
|
|
if self.data != Data.priv:
|
2018-04-24 21:50:00 +02:00
|
|
|
assert_equal(address["involvesWatchonly"], True)
|
2017-02-14 21:00:34 +01:00
|
|
|
else:
|
2018-04-24 21:50:00 +02:00
|
|
|
assert_equal("involvesWatchonly" not in address, True)
|
2017-02-14 21:00:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
# List of Variants for each way a key or address could be imported.
|
2019-07-25 23:11:31 +02:00
|
|
|
IMPORT_VARIANTS = [Variant(*variants) for variants in itertools.product(Call, Data, AddressType, Rescan, (False, True))]
|
2017-02-15 17:28:50 +01:00
|
|
|
|
|
|
|
# List of nodes to import keys to. Half the nodes will have pruning disabled,
|
|
|
|
# half will have it enabled. Different nodes will be used for imports that are
|
|
|
|
# expected to cause rescans, and imports that are not expected to cause
|
|
|
|
# rescans, in order to prevent rescans during later imports picking up
|
|
|
|
# transactions associated with earlier imports. This makes it easier to keep
|
|
|
|
# track of expected balances and transactions.
|
|
|
|
ImportNode = collections.namedtuple("ImportNode", "prune rescan")
|
|
|
|
IMPORT_NODES = [ImportNode(*fields) for fields in itertools.product((False, True), repeat=2)]
|
2016-12-12 16:18:11 +01:00
|
|
|
|
2017-02-14 23:39:26 +01:00
|
|
|
# Rescans start at the earliest block up to 2 hours before the key timestamp.
|
2017-03-02 18:20:34 +01:00
|
|
|
TIMESTAMP_WINDOW = 2 * 60 * 60
|
2017-02-14 23:39:26 +01:00
|
|
|
|
2019-07-25 23:14:47 +02:00
|
|
|
AMOUNT_DUST = 0.00000546
|
|
|
|
|
|
|
|
|
|
|
|
def get_rand_amount():
|
|
|
|
r = random.uniform(AMOUNT_DUST, 1)
|
|
|
|
return Decimal(str(round(r, 8)))
|
|
|
|
|
2016-12-12 16:18:11 +01:00
|
|
|
|
|
|
|
class ImportRescanTest(BitcoinTestFramework):
|
2017-06-10 00:21:21 +02:00
|
|
|
def set_test_params(self):
|
2017-02-23 19:05:40 +01:00
|
|
|
self.num_nodes = 2 + len(IMPORT_NODES)
|
2019-12-06 15:37:49 +01:00
|
|
|
self.supports_cli = False
|
2020-04-17 23:03:25 +02:00
|
|
|
self.rpc_timeout = 120
|
2016-12-12 16:18:11 +01:00
|
|
|
|
2018-09-09 19:32:37 +02:00
|
|
|
def skip_test_if_missing_module(self):
|
|
|
|
self.skip_if_no_wallet()
|
|
|
|
|
2016-12-12 16:18:11 +01:00
|
|
|
def setup_network(self):
|
2020-09-29 02:24:06 +02:00
|
|
|
self.extra_args = [[] for _ in range(self.num_nodes)]
|
2017-02-23 19:05:40 +01:00
|
|
|
for i, import_node in enumerate(IMPORT_NODES, 2):
|
2017-02-15 17:28:50 +01:00
|
|
|
if import_node.prune:
|
2019-07-25 23:11:31 +02:00
|
|
|
self.extra_args[i] += ["-prune=1"]
|
2017-02-15 17:28:50 +01:00
|
|
|
|
2019-07-25 23:11:31 +02:00
|
|
|
self.add_nodes(self.num_nodes, extra_args=self.extra_args)
|
2018-09-10 22:58:15 +02:00
|
|
|
|
2018-09-13 15:23:11 +02:00
|
|
|
# Import keys with pruning disabled
|
2020-09-29 02:24:06 +02:00
|
|
|
self.start_nodes(extra_args=[[]] * self.num_nodes)
|
|
|
|
self.import_deterministic_coinbase_privkeys()
|
2018-09-10 22:58:15 +02:00
|
|
|
self.stop_nodes()
|
|
|
|
|
2017-06-09 22:35:17 +02:00
|
|
|
self.start_nodes()
|
2016-12-12 16:18:11 +01:00
|
|
|
for i in range(1, self.num_nodes):
|
2020-09-17 09:46:07 +02:00
|
|
|
self.connect_nodes(i, 0)
|
2016-12-12 16:18:11 +01:00
|
|
|
|
|
|
|
def run_test(self):
|
2018-04-24 21:50:00 +02:00
|
|
|
# Create one transaction on node 0 with a unique amount for
|
2016-12-12 16:18:11 +01:00
|
|
|
# each possible type of wallet import RPC.
|
2017-02-14 21:00:34 +01:00
|
|
|
for i, variant in enumerate(IMPORT_VARIANTS):
|
2018-10-06 06:48:23 +02:00
|
|
|
variant.label = "label {} {}".format(i, variant)
|
2019-07-25 23:11:31 +02:00
|
|
|
variant.address = self.nodes[1].getaddressinfo(self.nodes[1].getnewaddress(
|
|
|
|
label=variant.label,
|
|
|
|
address_type=variant.address_type.value,
|
|
|
|
))
|
2017-02-23 19:05:40 +01:00
|
|
|
variant.key = self.nodes[1].dumpprivkey(variant.address["address"])
|
2019-07-25 23:14:47 +02:00
|
|
|
variant.initial_amount = get_rand_amount()
|
2017-02-14 21:00:34 +01:00
|
|
|
variant.initial_txid = self.nodes[0].sendtoaddress(variant.address["address"], variant.initial_amount)
|
2019-04-24 16:08:26 +02:00
|
|
|
self.nodes[0].generate(1) # Generate one block for each send
|
|
|
|
variant.confirmation_height = self.nodes[0].getblockcount()
|
|
|
|
variant.timestamp = self.nodes[0].getblockheader(self.nodes[0].getbestblockhash())["time"]
|
2020-10-18 10:13:07 +02:00
|
|
|
self.sync_all() # Conclude sync before calling setmocktime to avoid timeouts
|
2017-02-14 21:00:34 +01:00
|
|
|
|
2019-04-24 16:08:26 +02:00
|
|
|
# Generate a block further in the future (past the rescan window).
|
2016-12-12 16:18:11 +01:00
|
|
|
assert_equal(self.nodes[0].getrawmempool(), [])
|
2019-04-24 16:08:26 +02:00
|
|
|
set_node_times(
|
|
|
|
self.nodes,
|
|
|
|
self.nodes[0].getblockheader(self.nodes[0].getbestblockhash())["time"] + TIMESTAMP_WINDOW + 1,
|
|
|
|
)
|
2017-02-14 23:39:26 +01:00
|
|
|
self.nodes[0].generate(1)
|
2019-04-22 15:03:35 +02:00
|
|
|
self.sync_all()
|
2016-12-12 16:18:11 +01:00
|
|
|
|
2017-02-14 21:00:34 +01:00
|
|
|
# For each variation of wallet key import, invoke the import RPC and
|
2017-02-15 17:28:50 +01:00
|
|
|
# check the results from getbalance and listtransactions.
|
2017-02-14 21:00:34 +01:00
|
|
|
for variant in IMPORT_VARIANTS:
|
2019-04-22 15:03:35 +02:00
|
|
|
self.log.info('Run import for variant {}'.format(variant))
|
2019-04-22 20:30:02 +02:00
|
|
|
expect_rescan = variant.rescan == Rescan.yes
|
2017-02-23 19:05:40 +01:00
|
|
|
variant.node = self.nodes[2 + IMPORT_NODES.index(ImportNode(variant.prune, expect_rescan))]
|
2019-04-24 16:08:26 +02:00
|
|
|
variant.do_import(variant.timestamp)
|
2017-02-15 17:28:50 +01:00
|
|
|
if expect_rescan:
|
2017-02-14 21:00:34 +01:00
|
|
|
variant.expected_balance = variant.initial_amount
|
|
|
|
variant.expected_txs = 1
|
2019-04-24 16:08:26 +02:00
|
|
|
variant.check(variant.initial_txid, variant.initial_amount, variant.confirmation_height)
|
2017-02-14 21:00:34 +01:00
|
|
|
else:
|
|
|
|
variant.expected_balance = 0
|
|
|
|
variant.expected_txs = 0
|
|
|
|
variant.check()
|
|
|
|
|
|
|
|
# Create new transactions sending to each address.
|
|
|
|
for i, variant in enumerate(IMPORT_VARIANTS):
|
2019-07-25 23:14:47 +02:00
|
|
|
variant.sent_amount = get_rand_amount()
|
2017-02-14 21:00:34 +01:00
|
|
|
variant.sent_txid = self.nodes[0].sendtoaddress(variant.address["address"], variant.sent_amount)
|
2019-04-24 16:08:26 +02:00
|
|
|
self.nodes[0].generate(1) # Generate one block for each send
|
|
|
|
variant.confirmation_height = self.nodes[0].getblockcount()
|
2016-12-12 16:18:11 +01:00
|
|
|
|
|
|
|
assert_equal(self.nodes[0].getrawmempool(), [])
|
2019-04-22 15:03:35 +02:00
|
|
|
self.sync_all()
|
2016-12-12 16:18:11 +01:00
|
|
|
|
2017-02-14 21:00:34 +01:00
|
|
|
# Check the latest results from getbalance and listtransactions.
|
|
|
|
for variant in IMPORT_VARIANTS:
|
2019-04-22 15:03:35 +02:00
|
|
|
self.log.info('Run check for variant {}'.format(variant))
|
2019-04-22 20:30:02 +02:00
|
|
|
variant.expected_balance += variant.sent_amount
|
|
|
|
variant.expected_txs += 1
|
2019-04-24 16:08:26 +02:00
|
|
|
variant.check(variant.sent_txid, variant.sent_amount, variant.confirmation_height)
|
2017-02-15 17:28:50 +01:00
|
|
|
|
2020-04-17 23:03:25 +02:00
|
|
|
|
2016-12-12 16:18:11 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
ImportRescanTest().main()
|