aboutsummaryrefslogtreecommitdiff
path: root/qa
diff options
context:
space:
mode:
Diffstat (limited to 'qa')
-rwxr-xr-xqa/pull-tester/rpc-tests.py2
-rwxr-xr-xqa/rpc-tests/import-rescan.py254
-rwxr-xr-xqa/rpc-tests/importmulti.py80
-rwxr-xr-xqa/rpc-tests/p2p-fullblocktest.py4
-rwxr-xr-xqa/rpc-tests/p2p-leaktests.py145
-rwxr-xr-xqa/rpc-tests/p2p-timeouts.py103
-rwxr-xr-xqa/rpc-tests/test_framework/mininode.py38
7 files changed, 497 insertions, 129 deletions
diff --git a/qa/pull-tester/rpc-tests.py b/qa/pull-tester/rpc-tests.py
index 26bc6a73df..20ab0fdd1d 100755
--- a/qa/pull-tester/rpc-tests.py
+++ b/qa/pull-tester/rpc-tests.py
@@ -154,6 +154,7 @@ testScripts = [
'bumpfee.py',
'rpcnamedargs.py',
'listsinceblock.py',
+ 'p2p-leaktests.py',
]
if ENABLE_ZMQ:
testScripts.append('zmq_test.py')
@@ -168,6 +169,7 @@ testScriptsExt = [
# vv Tests less than 2m vv
'bip68-sequence.py',
'getblocktemplate_longpoll.py',
+ 'p2p-timeouts.py',
# vv Tests less than 60s vv
'bip9-softforks.py',
'p2p-feefilter.py',
diff --git a/qa/rpc-tests/import-rescan.py b/qa/rpc-tests/import-rescan.py
index e683df26db..54cc6d2642 100755
--- a/qa/rpc-tests/import-rescan.py
+++ b/qa/rpc-tests/import-rescan.py
@@ -2,54 +2,105 @@
# Copyright (c) 2014-2016 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
-
+"""Test rescan behavior of importaddress, importpubkey, importprivkey, and
+importmulti RPCs with different types of keys and rescan options.
+
+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.
+
+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.
+"""
+
+from test_framework.authproxy import JSONRPCException
from test_framework.test_framework import BitcoinTestFramework
-from test_framework.util import (start_nodes, connect_nodes, sync_blocks, assert_equal)
+from test_framework.util import (start_nodes, connect_nodes, sync_blocks, assert_equal, set_node_times)
from decimal import Decimal
import collections
import enum
import itertools
-import functools
Call = enum.Enum("Call", "single multi")
Data = enum.Enum("Data", "address pub priv")
-ImportNode = collections.namedtuple("ImportNode", "rescan")
-
-
-def call_import_rpc(call, data, address, scriptPubKey, pubkey, key, label, node, rescan):
- """Helper that calls a wallet import RPC on a bitcoin node."""
- watchonly = data != Data.priv
- if call == Call.single:
- if data == Data.address:
- response = node.importaddress(address, label, rescan)
- elif data == Data.pub:
- response = node.importpubkey(pubkey, label, rescan)
- elif data == Data.priv:
- response = node.importprivkey(key, label, rescan)
- assert_equal(response, None)
- elif call == Call.multi:
- response = node.importmulti([{
- "scriptPubKey": {
- "address": address
- },
- "pubkeys": [pubkey] if data == Data.pub else [],
- "keys": [key] if data == Data.priv else [],
- "label": label,
- "watchonly": watchonly
- }], {"rescan": rescan})
- assert_equal(response, [{"success": True}])
- return watchonly
-
-
-# List of RPCs that import a wallet key or address in various ways.
-IMPORT_RPCS = [functools.partial(call_import_rpc, call, data) for call, data in itertools.product(Call, Data)]
-
-# List of bitcoind nodes that will import keys.
-IMPORT_NODES = [
- ImportNode(rescan=True),
- ImportNode(rescan=False),
-]
+Rescan = enum.Enum("Rescan", "no yes late_timestamp")
+
+
+class Variant(collections.namedtuple("Variant", "call data rescan prune")):
+ """Helper for importing one key and verifying scanned transactions."""
+
+ def do_import(self, timestamp):
+ """Call one key import RPC."""
+
+ if self.call == Call.single:
+ if self.data == Data.address:
+ response, error = try_rpc(self.node.importaddress, self.address["address"], self.label,
+ self.rescan == Rescan.yes)
+ elif self.data == Data.pub:
+ response, error = try_rpc(self.node.importpubkey, self.address["pubkey"], self.label,
+ self.rescan == Rescan.yes)
+ elif self.data == Data.priv:
+ response, error = try_rpc(self.node.importprivkey, self.key, self.label, self.rescan == Rescan.yes)
+ assert_equal(response, None)
+ assert_equal(error, {'message': 'Rescan is disabled in pruned mode',
+ 'code': -4} if self.expect_disabled else None)
+ elif self.call == Call.multi:
+ response = self.node.importmulti([{
+ "scriptPubKey": {
+ "address": self.address["address"]
+ },
+ "timestamp": timestamp + RESCAN_WINDOW + (1 if self.rescan == Rescan.late_timestamp else 0),
+ "pubkeys": [self.address["pubkey"]] if self.data == Data.pub else [],
+ "keys": [self.key] if self.data == Data.priv else [],
+ "label": self.label,
+ "watchonly": self.data != Data.priv
+ }], {"rescan": self.rescan in (Rescan.yes, Rescan.late_timestamp)})
+ assert_equal(response, [{"success": True}])
+
+ def check(self, txid=None, amount=None, confirmations=None):
+ """Verify that getbalance/listtransactions return expected values."""
+
+ balance = self.node.getbalance(self.label, 0, True)
+ assert_equal(balance, self.expected_balance)
+
+ txs = self.node.listtransactions(self.label, 10000, 0, True)
+ assert_equal(len(txs), self.expected_txs)
+
+ if txid is not None:
+ tx, = [tx for tx in txs if tx["txid"] == txid]
+ assert_equal(tx["account"], 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)
+ assert_equal(tx["confirmations"], confirmations)
+ assert_equal("trusted" not in tx, True)
+ if self.data != Data.priv:
+ assert_equal(tx["involvesWatchonly"], True)
+ else:
+ assert_equal("involvesWatchonly" not in tx, True)
+
+
+# List of Variants for each way a key or address could be imported.
+IMPORT_VARIANTS = [Variant(*variants) for variants in itertools.product(Call, Data, Rescan, (False, True))]
+
+# 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)]
+
+# Rescans start at the earliest block up to 2 hours before the key timestamp.
+RESCAN_WINDOW = 2 * 60 * 60
class ImportRescanTest(BitcoinTestFramework):
@@ -59,6 +110,10 @@ class ImportRescanTest(BitcoinTestFramework):
def setup_network(self):
extra_args = [["-debug=1"] for _ in range(self.num_nodes)]
+ for i, import_node in enumerate(IMPORT_NODES, 1):
+ if import_node.prune:
+ extra_args[i] += ["-prune=1"]
+
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, extra_args)
for i in range(1, self.num_nodes):
connect_nodes(self.nodes[i], 0)
@@ -66,89 +121,64 @@ class ImportRescanTest(BitcoinTestFramework):
def run_test(self):
# Create one transaction on node 0 with a unique amount and label for
# each possible type of wallet import RPC.
- import_rpc_variants = []
- for i, import_rpc in enumerate(IMPORT_RPCS):
- label = "label{}".format(i)
- addr = self.nodes[0].validateaddress(self.nodes[0].getnewaddress(label))
- key = self.nodes[0].dumpprivkey(addr["address"])
- amount = 24.9375 - i * .0625
- txid = self.nodes[0].sendtoaddress(addr["address"], amount)
- import_rpc = functools.partial(import_rpc, addr["address"], addr["scriptPubKey"], addr["pubkey"], key,
- label)
- import_rpc_variants.append((import_rpc, label, amount, txid, addr))
-
+ for i, variant in enumerate(IMPORT_VARIANTS):
+ variant.label = "label {} {}".format(i, variant)
+ variant.address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress(variant.label))
+ variant.key = self.nodes[0].dumpprivkey(variant.address["address"])
+ variant.initial_amount = 25 - (i + 1) / 4.0
+ variant.initial_txid = self.nodes[0].sendtoaddress(variant.address["address"], variant.initial_amount)
+
+ # Generate a block containing the initial transactions, then another
+ # block further in the future (past the rescan window).
self.nodes[0].generate(1)
assert_equal(self.nodes[0].getrawmempool(), [])
+ timestamp = self.nodes[0].getblockheader(self.nodes[0].getbestblockhash())["time"]
+ set_node_times(self.nodes, timestamp + RESCAN_WINDOW + 1)
+ self.nodes[0].generate(1)
sync_blocks(self.nodes)
- # For each importing node and variation of wallet import RPC, invoke
- # the RPC and check the results from getbalance and listtransactions.
- for node, import_node in zip(self.nodes[1:], IMPORT_NODES):
- for import_rpc, label, amount, txid, addr in import_rpc_variants:
- watchonly = import_rpc(node, import_node.rescan)
-
- balance = node.getbalance(label, 0, True)
- if import_node.rescan:
- assert_equal(balance, amount)
- else:
- assert_equal(balance, 0)
-
- txs = node.listtransactions(label, 10000, 0, True)
- if import_node.rescan:
- assert_equal(len(txs), 1)
- assert_equal(txs[0]["account"], label)
- assert_equal(txs[0]["address"], addr["address"])
- assert_equal(txs[0]["amount"], amount)
- assert_equal(txs[0]["category"], "receive")
- assert_equal(txs[0]["label"], label)
- assert_equal(txs[0]["txid"], txid)
- assert_equal(txs[0]["confirmations"], 1)
- assert_equal("trusted" not in txs[0], True)
- if watchonly:
- assert_equal(txs[0]["involvesWatchonly"], True)
- else:
- assert_equal("involvesWatchonly" not in txs[0], True)
- else:
- assert_equal(len(txs), 0)
-
- # Create spends for all the imported addresses.
- spend_txids = []
+ # For each variation of wallet key import, invoke the import RPC and
+ # check the results from getbalance and listtransactions.
+ for variant in IMPORT_VARIANTS:
+ variant.expect_disabled = variant.rescan == Rescan.yes and variant.prune and variant.call == Call.single
+ expect_rescan = variant.rescan == Rescan.yes and not variant.expect_disabled
+ variant.node = self.nodes[1 + IMPORT_NODES.index(ImportNode(variant.prune, expect_rescan))]
+ variant.do_import(timestamp)
+ if expect_rescan:
+ variant.expected_balance = variant.initial_amount
+ variant.expected_txs = 1
+ variant.check(variant.initial_txid, variant.initial_amount, 2)
+ else:
+ variant.expected_balance = 0
+ variant.expected_txs = 0
+ variant.check()
+
+ # Create new transactions sending to each address.
fee = self.nodes[0].getnetworkinfo()["relayfee"]
- for import_rpc, label, amount, txid, addr in import_rpc_variants:
- raw_tx = self.nodes[0].getrawtransaction(txid)
- decoded_tx = self.nodes[0].decoderawtransaction(raw_tx)
- input_vout = next(out["n"] for out in decoded_tx["vout"]
- if out["scriptPubKey"]["addresses"] == [addr["address"]])
- inputs = [{"txid": txid, "vout": input_vout}]
- outputs = {self.nodes[0].getnewaddress(): Decimal(amount) - fee}
- raw_spend_tx = self.nodes[0].createrawtransaction(inputs, outputs)
- signed_spend_tx = self.nodes[0].signrawtransaction(raw_spend_tx)
- spend_txid = self.nodes[0].sendrawtransaction(signed_spend_tx["hex"])
- spend_txids.append(spend_txid)
+ for i, variant in enumerate(IMPORT_VARIANTS):
+ variant.sent_amount = 25 - (2 * i + 1) / 8.0
+ variant.sent_txid = self.nodes[0].sendtoaddress(variant.address["address"], variant.sent_amount)
+ # Generate a block containing the new transactions.
self.nodes[0].generate(1)
assert_equal(self.nodes[0].getrawmempool(), [])
sync_blocks(self.nodes)
- # Check the results from getbalance and listtransactions after the spends.
- for node, import_node in zip(self.nodes[1:], IMPORT_NODES):
- txs = node.listtransactions("*", 10000, 0, True)
- for (import_rpc, label, amount, txid, addr), spend_txid in zip(import_rpc_variants, spend_txids):
- balance = node.getbalance(label, 0, True)
- spend_tx = [tx for tx in txs if tx["txid"] == spend_txid]
- if import_node.rescan:
- assert_equal(balance, amount)
- assert_equal(len(spend_tx), 1)
- assert_equal(spend_tx[0]["account"], "")
- assert_equal(spend_tx[0]["amount"] + spend_tx[0]["fee"], -amount)
- assert_equal(spend_tx[0]["category"], "send")
- assert_equal("label" not in spend_tx[0], True)
- assert_equal(spend_tx[0]["confirmations"], 1)
- assert_equal("trusted" not in spend_tx[0], True)
- assert_equal("involvesWatchonly" not in txs[0], True)
- else:
- assert_equal(balance, 0)
- assert_equal(spend_tx, [])
+ # Check the latest results from getbalance and listtransactions.
+ for variant in IMPORT_VARIANTS:
+ if not variant.expect_disabled:
+ variant.expected_balance += variant.sent_amount
+ variant.expected_txs += 1
+ variant.check(variant.sent_txid, variant.sent_amount, 1)
+ else:
+ variant.check()
+
+
+def try_rpc(func, *args, **kwargs):
+ try:
+ return func(*args, **kwargs), None
+ except JSONRPCException as e:
+ return None, e.error
if __name__ == "__main__":
diff --git a/qa/rpc-tests/importmulti.py b/qa/rpc-tests/importmulti.py
index e100a3af9d..1aa4ba2e18 100755
--- a/qa/rpc-tests/importmulti.py
+++ b/qa/rpc-tests/importmulti.py
@@ -20,6 +20,7 @@ class ImportMultiTest (BitcoinTestFramework):
print ("Mining blocks...")
self.nodes[0].generate(1)
self.nodes[1].generate(1)
+ timestamp = self.nodes[1].getblock(self.nodes[1].getbestblockhash())['mediantime']
# keyword definition
PRIV_KEY = 'privkey'
@@ -52,31 +53,48 @@ class ImportMultiTest (BitcoinTestFramework):
result = self.nodes[1].importmulti([{
"scriptPubKey": {
"address": address['address']
- }
+ },
+ "timestamp": "now",
}])
assert_equal(result[0]['success'], True)
address_assert = self.nodes[1].validateaddress(address['address'])
assert_equal(address_assert['iswatchonly'], True)
assert_equal(address_assert['ismine'], False)
+ assert_equal(address_assert['timestamp'], timestamp)
+ watchonly_address = address['address']
+ watchonly_timestamp = timestamp
+ print("Should not import an invalid address")
+ result = self.nodes[1].importmulti([{
+ "scriptPubKey": {
+ "address": "not valid address",
+ },
+ "timestamp": "now",
+ }])
+ assert_equal(result[0]['success'], False)
+ assert_equal(result[0]['error']['code'], -5)
+ assert_equal(result[0]['error']['message'], 'Invalid address')
# ScriptPubKey + internal
print("Should import a scriptPubKey with internal flag")
address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
result = self.nodes[1].importmulti([{
"scriptPubKey": address['scriptPubKey'],
+ "timestamp": "now",
"internal": True
}])
assert_equal(result[0]['success'], True)
address_assert = self.nodes[1].validateaddress(address['address'])
assert_equal(address_assert['iswatchonly'], True)
assert_equal(address_assert['ismine'], False)
+ assert_equal(address_assert['timestamp'], timestamp)
# ScriptPubKey + !internal
print("Should not import a scriptPubKey without internal flag")
address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
result = self.nodes[1].importmulti([{
- "scriptPubKey": address['scriptPubKey']
+ "scriptPubKey": address['scriptPubKey'],
+ "timestamp": "now",
}])
assert_equal(result[0]['success'], False)
assert_equal(result[0]['error']['code'], -8)
@@ -84,6 +102,7 @@ class ImportMultiTest (BitcoinTestFramework):
address_assert = self.nodes[1].validateaddress(address['address'])
assert_equal(address_assert['iswatchonly'], False)
assert_equal(address_assert['ismine'], False)
+ assert_equal('timestamp' in address_assert, False)
# Address + Public key + !Internal
@@ -93,12 +112,14 @@ class ImportMultiTest (BitcoinTestFramework):
"scriptPubKey": {
"address": address['address']
},
+ "timestamp": "now",
"pubkeys": [ address['pubkey'] ]
}])
assert_equal(result[0]['success'], True)
address_assert = self.nodes[1].validateaddress(address['address'])
assert_equal(address_assert['iswatchonly'], True)
assert_equal(address_assert['ismine'], False)
+ assert_equal(address_assert['timestamp'], timestamp)
# ScriptPubKey + Public key + internal
@@ -106,6 +127,7 @@ class ImportMultiTest (BitcoinTestFramework):
address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
request = [{
"scriptPubKey": address['scriptPubKey'],
+ "timestamp": "now",
"pubkeys": [ address['pubkey'] ],
"internal": True
}]
@@ -114,12 +136,14 @@ class ImportMultiTest (BitcoinTestFramework):
address_assert = self.nodes[1].validateaddress(address['address'])
assert_equal(address_assert['iswatchonly'], True)
assert_equal(address_assert['ismine'], False)
+ assert_equal(address_assert['timestamp'], timestamp)
# ScriptPubKey + Public key + !internal
print("Should not import a scriptPubKey without internal and with public key")
address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
request = [{
"scriptPubKey": address['scriptPubKey'],
+ "timestamp": "now",
"pubkeys": [ address['pubkey'] ]
}]
result = self.nodes[1].importmulti(request)
@@ -129,6 +153,7 @@ class ImportMultiTest (BitcoinTestFramework):
address_assert = self.nodes[1].validateaddress(address['address'])
assert_equal(address_assert['iswatchonly'], False)
assert_equal(address_assert['ismine'], False)
+ assert_equal('timestamp' in address_assert, False)
# Address + Private key + !watchonly
print("Should import an address with private key")
@@ -137,12 +162,14 @@ class ImportMultiTest (BitcoinTestFramework):
"scriptPubKey": {
"address": address['address']
},
+ "timestamp": "now",
"keys": [ self.nodes[0].dumpprivkey(address['address']) ]
}])
assert_equal(result[0]['success'], True)
address_assert = self.nodes[1].validateaddress(address['address'])
assert_equal(address_assert['iswatchonly'], False)
assert_equal(address_assert['ismine'], True)
+ assert_equal(address_assert['timestamp'], timestamp)
# Address + Private key + watchonly
print("Should not import an address with private key and with watchonly")
@@ -151,6 +178,7 @@ class ImportMultiTest (BitcoinTestFramework):
"scriptPubKey": {
"address": address['address']
},
+ "timestamp": "now",
"keys": [ self.nodes[0].dumpprivkey(address['address']) ],
"watchonly": True
}])
@@ -160,12 +188,14 @@ class ImportMultiTest (BitcoinTestFramework):
address_assert = self.nodes[1].validateaddress(address['address'])
assert_equal(address_assert['iswatchonly'], False)
assert_equal(address_assert['ismine'], False)
+ assert_equal('timestamp' in address_assert, False)
# ScriptPubKey + Private key + internal
print("Should import a scriptPubKey with internal and with private key")
address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
result = self.nodes[1].importmulti([{
"scriptPubKey": address['scriptPubKey'],
+ "timestamp": "now",
"keys": [ self.nodes[0].dumpprivkey(address['address']) ],
"internal": True
}])
@@ -173,12 +203,14 @@ class ImportMultiTest (BitcoinTestFramework):
address_assert = self.nodes[1].validateaddress(address['address'])
assert_equal(address_assert['iswatchonly'], False)
assert_equal(address_assert['ismine'], True)
+ assert_equal(address_assert['timestamp'], timestamp)
# ScriptPubKey + Private key + !internal
print("Should not import a scriptPubKey without internal and with private key")
address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
result = self.nodes[1].importmulti([{
"scriptPubKey": address['scriptPubKey'],
+ "timestamp": "now",
"keys": [ self.nodes[0].dumpprivkey(address['address']) ]
}])
assert_equal(result[0]['success'], False)
@@ -187,6 +219,7 @@ class ImportMultiTest (BitcoinTestFramework):
address_assert = self.nodes[1].validateaddress(address['address'])
assert_equal(address_assert['iswatchonly'], False)
assert_equal(address_assert['ismine'], False)
+ assert_equal('timestamp' in address_assert, False)
# P2SH address
@@ -197,18 +230,21 @@ class ImportMultiTest (BitcoinTestFramework):
self.nodes[1].generate(100)
transactionid = self.nodes[1].sendtoaddress(multi_sig_script['address'], 10.00)
self.nodes[1].generate(1)
+ timestamp = self.nodes[1].getblock(self.nodes[1].getbestblockhash())['mediantime']
transaction = self.nodes[1].gettransaction(transactionid)
print("Should import a p2sh")
result = self.nodes[1].importmulti([{
"scriptPubKey": {
"address": multi_sig_script['address']
- }
+ },
+ "timestamp": "now",
}])
assert_equal(result[0]['success'], True)
address_assert = self.nodes[1].validateaddress(multi_sig_script['address'])
assert_equal(address_assert['isscript'], True)
assert_equal(address_assert['iswatchonly'], True)
+ assert_equal(address_assert['timestamp'], timestamp)
p2shunspent = self.nodes[1].listunspent(0,999999, [multi_sig_script['address']])[0]
assert_equal(p2shunspent['spendable'], False)
assert_equal(p2shunspent['solvable'], False)
@@ -222,6 +258,7 @@ class ImportMultiTest (BitcoinTestFramework):
self.nodes[1].generate(100)
transactionid = self.nodes[1].sendtoaddress(multi_sig_script['address'], 10.00)
self.nodes[1].generate(1)
+ timestamp = self.nodes[1].getblock(self.nodes[1].getbestblockhash())['mediantime']
transaction = self.nodes[1].gettransaction(transactionid)
print("Should import a p2sh with respective redeem script")
@@ -229,9 +266,12 @@ class ImportMultiTest (BitcoinTestFramework):
"scriptPubKey": {
"address": multi_sig_script['address']
},
+ "timestamp": "now",
"redeemscript": multi_sig_script['redeemScript']
}])
assert_equal(result[0]['success'], True)
+ address_assert = self.nodes[1].validateaddress(multi_sig_script['address'])
+ assert_equal(address_assert['timestamp'], timestamp)
p2shunspent = self.nodes[1].listunspent(0,999999, [multi_sig_script['address']])[0]
assert_equal(p2shunspent['spendable'], False)
@@ -246,6 +286,7 @@ class ImportMultiTest (BitcoinTestFramework):
self.nodes[1].generate(100)
transactionid = self.nodes[1].sendtoaddress(multi_sig_script['address'], 10.00)
self.nodes[1].generate(1)
+ timestamp = self.nodes[1].getblock(self.nodes[1].getbestblockhash())['mediantime']
transaction = self.nodes[1].gettransaction(transactionid)
print("Should import a p2sh with respective redeem script and private keys")
@@ -253,10 +294,13 @@ class ImportMultiTest (BitcoinTestFramework):
"scriptPubKey": {
"address": multi_sig_script['address']
},
+ "timestamp": "now",
"redeemscript": multi_sig_script['redeemScript'],
"keys": [ self.nodes[0].dumpprivkey(sig_address_1['address']), self.nodes[0].dumpprivkey(sig_address_2['address'])]
}])
assert_equal(result[0]['success'], True)
+ address_assert = self.nodes[1].validateaddress(multi_sig_script['address'])
+ assert_equal(address_assert['timestamp'], timestamp)
p2shunspent = self.nodes[1].listunspent(0,999999, [multi_sig_script['address']])[0]
assert_equal(p2shunspent['spendable'], False)
@@ -277,6 +321,7 @@ class ImportMultiTest (BitcoinTestFramework):
"scriptPubKey": {
"address": multi_sig_script['address']
},
+ "timestamp": "now",
"redeemscript": multi_sig_script['redeemScript'],
"keys": [ self.nodes[0].dumpprivkey(sig_address_1['address']), self.nodes[0].dumpprivkey(sig_address_2['address'])],
"watchonly": True
@@ -294,6 +339,7 @@ class ImportMultiTest (BitcoinTestFramework):
"scriptPubKey": {
"address": address['address']
},
+ "timestamp": "now",
"pubkeys": [ address2['pubkey'] ]
}])
assert_equal(result[0]['success'], False)
@@ -302,6 +348,7 @@ class ImportMultiTest (BitcoinTestFramework):
address_assert = self.nodes[1].validateaddress(address['address'])
assert_equal(address_assert['iswatchonly'], False)
assert_equal(address_assert['ismine'], False)
+ assert_equal('timestamp' in address_assert, False)
# ScriptPubKey + Public key + internal + Wrong pubkey
@@ -310,6 +357,7 @@ class ImportMultiTest (BitcoinTestFramework):
address2 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
request = [{
"scriptPubKey": address['scriptPubKey'],
+ "timestamp": "now",
"pubkeys": [ address2['pubkey'] ],
"internal": True
}]
@@ -320,6 +368,7 @@ class ImportMultiTest (BitcoinTestFramework):
address_assert = self.nodes[1].validateaddress(address['address'])
assert_equal(address_assert['iswatchonly'], False)
assert_equal(address_assert['ismine'], False)
+ assert_equal('timestamp' in address_assert, False)
# Address + Private key + !watchonly + Wrong private key
@@ -330,6 +379,7 @@ class ImportMultiTest (BitcoinTestFramework):
"scriptPubKey": {
"address": address['address']
},
+ "timestamp": "now",
"keys": [ self.nodes[0].dumpprivkey(address2['address']) ]
}])
assert_equal(result[0]['success'], False)
@@ -338,6 +388,7 @@ class ImportMultiTest (BitcoinTestFramework):
address_assert = self.nodes[1].validateaddress(address['address'])
assert_equal(address_assert['iswatchonly'], False)
assert_equal(address_assert['ismine'], False)
+ assert_equal('timestamp' in address_assert, False)
# ScriptPubKey + Private key + internal + Wrong private key
@@ -346,6 +397,7 @@ class ImportMultiTest (BitcoinTestFramework):
address2 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
result = self.nodes[1].importmulti([{
"scriptPubKey": address['scriptPubKey'],
+ "timestamp": "now",
"keys": [ self.nodes[0].dumpprivkey(address2['address']) ],
"internal": True
}])
@@ -355,6 +407,28 @@ class ImportMultiTest (BitcoinTestFramework):
address_assert = self.nodes[1].validateaddress(address['address'])
assert_equal(address_assert['iswatchonly'], False)
assert_equal(address_assert['ismine'], False)
+ assert_equal('timestamp' in address_assert, False)
+
+ # restart nodes to check for proper serialization/deserialization of watch only address
+ stop_nodes(self.nodes)
+ self.nodes = start_nodes(2, self.options.tmpdir)
+ address_assert = self.nodes[1].validateaddress(watchonly_address)
+ assert_equal(address_assert['iswatchonly'], True)
+ assert_equal(address_assert['ismine'], False)
+ assert_equal(address_assert['timestamp'], watchonly_timestamp);
+
+ # Bad or missing timestamps
+ print("Should throw on invalid or missing timestamp values")
+ assert_raises_message(JSONRPCException, 'Missing required timestamp field for key',
+ self.nodes[1].importmulti, [{
+ "scriptPubKey": address['scriptPubKey'],
+ }])
+ assert_raises_message(JSONRPCException, 'Expected number or "now" timestamp value for key. got type string',
+ self.nodes[1].importmulti, [{
+ "scriptPubKey": address['scriptPubKey'],
+ "timestamp": "",
+ }])
+
if __name__ == '__main__':
ImportMultiTest ().main ()
diff --git a/qa/rpc-tests/p2p-fullblocktest.py b/qa/rpc-tests/p2p-fullblocktest.py
index e4b889d761..44e5f10322 100755
--- a/qa/rpc-tests/p2p-fullblocktest.py
+++ b/qa/rpc-tests/p2p-fullblocktest.py
@@ -398,7 +398,7 @@ class FullBlockTest(ComparisonTestFramework):
# Extend the b26 chain to make sure bitcoind isn't accepting b26
b27 = block(27, spend=out[7])
- yield rejected(RejectResult(16, b'bad-prevblk'))
+ yield rejected(RejectResult(0, b'bad-prevblk'))
# Now try a too-large-coinbase script
tip(15)
@@ -410,7 +410,7 @@ class FullBlockTest(ComparisonTestFramework):
# Extend the b28 chain to make sure bitcoind isn't accepting b28
b29 = block(29, spend=out[7])
- yield rejected(RejectResult(16, b'bad-prevblk'))
+ yield rejected(RejectResult(0, b'bad-prevblk'))
# b30 has a max-sized coinbase scriptSig.
tip(23)
diff --git a/qa/rpc-tests/p2p-leaktests.py b/qa/rpc-tests/p2p-leaktests.py
new file mode 100755
index 0000000000..41ca84d779
--- /dev/null
+++ b/qa/rpc-tests/p2p-leaktests.py
@@ -0,0 +1,145 @@
+#!/usr/bin/env python3
+# Copyright (c) 2017 The Bitcoin Core developers
+# Distributed under the MIT software license, see the accompanying
+# file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+from test_framework.mininode import *
+from test_framework.test_framework import BitcoinTestFramework
+from test_framework.util import *
+
+'''
+Test for message sending before handshake completion
+
+A node should never send anything other than VERSION/VERACK/REJECT until it's
+received a VERACK.
+
+This test connects to a node and sends it a few messages, trying to intice it
+into sending us something it shouldn't.
+'''
+
+banscore = 10
+
+class CLazyNode(NodeConnCB):
+ def __init__(self):
+ self.connection = None
+ self.unexpected_msg = False
+ self.connected = False
+ super().__init__()
+
+ def add_connection(self, conn):
+ self.connection = conn
+
+ def send_message(self, message):
+ self.connection.send_message(message)
+
+ def bad_message(self, message):
+ self.unexpected_msg = True
+ print("should not have received message: %s" % message.command)
+
+ def on_open(self, conn):
+ self.connected = True
+
+ def on_version(self, conn, message): self.bad_message(message)
+ def on_verack(self, conn, message): self.bad_message(message)
+ def on_reject(self, conn, message): self.bad_message(message)
+ def on_inv(self, conn, message): self.bad_message(message)
+ def on_addr(self, conn, message): self.bad_message(message)
+ def on_alert(self, conn, message): self.bad_message(message)
+ def on_getdata(self, conn, message): self.bad_message(message)
+ def on_getblocks(self, conn, message): self.bad_message(message)
+ def on_tx(self, conn, message): self.bad_message(message)
+ def on_block(self, conn, message): self.bad_message(message)
+ def on_getaddr(self, conn, message): self.bad_message(message)
+ def on_headers(self, conn, message): self.bad_message(message)
+ def on_getheaders(self, conn, message): self.bad_message(message)
+ def on_ping(self, conn, message): self.bad_message(message)
+ def on_mempool(self, conn): self.bad_message(message)
+ def on_pong(self, conn, message): self.bad_message(message)
+ def on_feefilter(self, conn, message): self.bad_message(message)
+ def on_sendheaders(self, conn, message): self.bad_message(message)
+ def on_sendcmpct(self, conn, message): self.bad_message(message)
+ def on_cmpctblock(self, conn, message): self.bad_message(message)
+ def on_getblocktxn(self, conn, message): self.bad_message(message)
+ def on_blocktxn(self, conn, message): self.bad_message(message)
+
+# Node that never sends a version. We'll use this to send a bunch of messages
+# anyway, and eventually get disconnected.
+class CNodeNoVersionBan(CLazyNode):
+ def __init__(self):
+ super().__init__()
+
+ # send a bunch of veracks without sending a message. This should get us disconnected.
+ # NOTE: implementation-specific check here. Remove if bitcoind ban behavior changes
+ def on_open(self, conn):
+ super().on_open(conn)
+ for i in range(banscore):
+ self.send_message(msg_verack())
+
+ def on_reject(self, conn, message): pass
+
+# Node that never sends a version. This one just sits idle and hopes to receive
+# any message (it shouldn't!)
+class CNodeNoVersionIdle(CLazyNode):
+ def __init__(self):
+ super().__init__()
+
+# Node that sends a version but not a verack.
+class CNodeNoVerackIdle(CLazyNode):
+ def __init__(self):
+ self.version_received = False
+ super().__init__()
+
+ def on_reject(self, conn, message): pass
+ def on_verack(self, conn, message): pass
+ # When version is received, don't reply with a verack. Instead, see if the
+ # node will give us a message that it shouldn't. This is not an exhaustive
+ # list!
+ def on_version(self, conn, message):
+ self.version_received = True
+ conn.send_message(msg_ping())
+ conn.send_message(msg_getaddr())
+
+class P2PLeakTest(BitcoinTestFramework):
+ def __init__(self):
+ super().__init__()
+ self.num_nodes = 1
+ def setup_network(self):
+ extra_args = [['-debug', '-banscore='+str(banscore)]
+ for i in range(self.num_nodes)]
+ self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, extra_args)
+
+ def run_test(self):
+ no_version_bannode = CNodeNoVersionBan()
+ no_version_idlenode = CNodeNoVersionIdle()
+ no_verack_idlenode = CNodeNoVerackIdle()
+
+ connections = []
+ connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], no_version_bannode, send_version=False))
+ connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], no_version_idlenode, send_version=False))
+ connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], no_verack_idlenode))
+ no_version_bannode.add_connection(connections[0])
+ no_version_idlenode.add_connection(connections[1])
+ no_verack_idlenode.add_connection(connections[2])
+
+ NetworkThread().start() # Start up network handling in another thread
+
+ assert(wait_until(lambda: no_version_bannode.connected and no_version_idlenode.connected and no_verack_idlenode.version_received, timeout=10))
+
+ # Mine a block and make sure that it's not sent to the connected nodes
+ self.nodes[0].generate(1)
+
+ #Give the node enough time to possibly leak out a message
+ time.sleep(5)
+
+ #This node should have been banned
+ assert(no_version_bannode.connection.state == "closed")
+
+ [conn.disconnect_node() for conn in connections]
+
+ # Make sure no unexpected messages came in
+ assert(no_version_bannode.unexpected_msg == False)
+ assert(no_version_idlenode.unexpected_msg == False)
+ assert(no_verack_idlenode.unexpected_msg == False)
+
+if __name__ == '__main__':
+ P2PLeakTest().main()
diff --git a/qa/rpc-tests/p2p-timeouts.py b/qa/rpc-tests/p2p-timeouts.py
new file mode 100755
index 0000000000..f1b190587d
--- /dev/null
+++ b/qa/rpc-tests/p2p-timeouts.py
@@ -0,0 +1,103 @@
+#!/usr/bin/env python3
+# Copyright (c) 2016 The Bitcoin Core developers
+# Distributed under the MIT software license, see the accompanying
+# file COPYING or http://www.opensource.org/licenses/mit-license.php.
+""" TimeoutsTest -- test various net timeouts (only in extended tests)
+
+- Create three bitcoind nodes:
+
+ no_verack_node - we never send a verack in response to their version
+ no_version_node - we never send a version (only a ping)
+ no_send_node - we never send any P2P message.
+
+- Start all three nodes
+- Wait 1 second
+- Assert that we're connected
+- Send a ping to no_verack_node and no_version_node
+- Wait 30 seconds
+- Assert that we're still connected
+- Send a ping to no_verack_node and no_version_node
+- Wait 31 seconds
+- Assert that we're no longer connected (timeout to receive version/verack is 60 seconds)
+"""
+
+from time import sleep
+
+from test_framework.mininode import *
+from test_framework.test_framework import BitcoinTestFramework
+from test_framework.util import *
+
+class TestNode(SingleNodeConnCB):
+ def __init__(self):
+ SingleNodeConnCB.__init__(self)
+ self.connected = False
+ self.received_version = False
+
+ def on_open(self, conn):
+ self.connected = True
+
+ def on_close(self, conn):
+ self.connected = False
+
+ def on_version(self, conn, message):
+ # Don't send a verack in response
+ self.received_version = True
+
+class TimeoutsTest(BitcoinTestFramework):
+ def __init__(self):
+ super().__init__()
+ self.setup_clean_chain = True
+ self.num_nodes = 1
+
+ def setup_network(self):
+ self.nodes = []
+
+ # Start up node0 to be a version 1, pre-segwit node.
+ self.nodes = start_nodes(self.num_nodes, self.options.tmpdir,
+ [["-debug", "-logtimemicros=1"]])
+
+ def run_test(self):
+ # Setup the p2p connections and start up the network thread.
+ self.no_verack_node = TestNode() # never send verack
+ self.no_version_node = TestNode() # never send version (just ping)
+ self.no_send_node = TestNode() # never send anything
+
+ connections = []
+ connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], self.no_verack_node))
+ connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], self.no_version_node, send_version=False))
+ connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], self.no_send_node, send_version=False))
+ self.no_verack_node.add_connection(connections[0])
+ self.no_version_node.add_connection(connections[1])
+ self.no_send_node.add_connection(connections[2])
+
+ NetworkThread().start() # Start up network handling in another thread
+
+ sleep(1)
+
+ assert(self.no_verack_node.connected)
+ assert(self.no_version_node.connected)
+ assert(self.no_send_node.connected)
+
+ ping_msg = msg_ping()
+ connections[0].send_message(ping_msg)
+ connections[1].send_message(ping_msg)
+
+ sleep(30)
+
+ assert(self.no_verack_node.received_version)
+
+ assert(self.no_verack_node.connected)
+ assert(self.no_version_node.connected)
+ assert(self.no_send_node.connected)
+
+ connections[0].send_message(ping_msg)
+ connections[1].send_message(ping_msg)
+
+ sleep(31)
+
+ assert(not self.no_verack_node.connected)
+ assert(not self.no_version_node.connected)
+ assert(not self.no_send_node.connected)
+
+if __name__ == '__main__':
+ TimeoutsTest().main()
diff --git a/qa/rpc-tests/test_framework/mininode.py b/qa/rpc-tests/test_framework/mininode.py
index 91daa4ab1f..5b563c58ae 100755
--- a/qa/rpc-tests/test_framework/mininode.py
+++ b/qa/rpc-tests/test_framework/mininode.py
@@ -1540,6 +1540,7 @@ class NodeConnCB(object):
if conn.ver_send > BIP0031_VERSION:
conn.send_message(msg_pong(message.nonce))
def on_reject(self, conn, message): pass
+ def on_open(self, conn): pass
def on_close(self, conn): pass
def on_mempool(self, conn): pass
def on_pong(self, conn, message): pass
@@ -1614,7 +1615,7 @@ class NodeConn(asyncore.dispatcher):
"regtest": b"\xfa\xbf\xb5\xda", # regtest
}
- def __init__(self, dstaddr, dstport, rpc, callback, net="regtest", services=NODE_NETWORK):
+ def __init__(self, dstaddr, dstport, rpc, callback, net="regtest", services=NODE_NETWORK, send_version=True):
asyncore.dispatcher.__init__(self, map=mininode_socket_map)
self.log = logging.getLogger("NodeConn(%s:%d)" % (dstaddr, dstport))
self.dstaddr = dstaddr
@@ -1631,14 +1632,16 @@ class NodeConn(asyncore.dispatcher):
self.disconnect = False
self.nServices = 0
- # stuff version msg into sendbuf
- vt = msg_version()
- vt.nServices = services
- vt.addrTo.ip = self.dstaddr
- vt.addrTo.port = self.dstport
- vt.addrFrom.ip = "0.0.0.0"
- vt.addrFrom.port = 0
- self.send_message(vt, True)
+ if send_version:
+ # stuff version msg into sendbuf
+ vt = msg_version()
+ vt.nServices = services
+ vt.addrTo.ip = self.dstaddr
+ vt.addrTo.port = self.dstport
+ vt.addrFrom.ip = "0.0.0.0"
+ vt.addrFrom.port = 0
+ self.send_message(vt, True)
+
print('MiniNode: Connecting to Bitcoin Node IP # ' + dstaddr + ':' \
+ str(dstport))
@@ -1652,8 +1655,10 @@ class NodeConn(asyncore.dispatcher):
self.log.debug(msg)
def handle_connect(self):
- self.show_debug_msg("MiniNode: Connected & Listening: \n")
- self.state = "connected"
+ if self.state != "connected":
+ self.show_debug_msg("MiniNode: Connected & Listening: \n")
+ self.state = "connected"
+ self.cb.on_open(self)
def handle_close(self):
self.show_debug_msg("MiniNode: Closing Connection to %s:%d... "
@@ -1681,11 +1686,20 @@ class NodeConn(asyncore.dispatcher):
def writable(self):
with mininode_lock:
+ pre_connection = self.state == "connecting"
length = len(self.sendbuf)
- return (length > 0)
+ return (length > 0 or pre_connection)
def handle_write(self):
with mininode_lock:
+ # asyncore does not expose socket connection, only the first read/write
+ # event, thus we must check connection manually here to know when we
+ # actually connect
+ if self.state == "connecting":
+ self.handle_connect()
+ if not self.writable():
+ return
+
try:
sent = self.send(self.sendbuf)
except: