aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rwxr-xr-xtest/functional/node_network_limited.py88
-rwxr-xr-xtest/functional/rawtransactions.py65
-rw-r--r--test/functional/test_framework/messages.py3
3 files changed, 99 insertions, 57 deletions
diff --git a/test/functional/node_network_limited.py b/test/functional/node_network_limited.py
index 6d1bf7ced2..70415e0168 100755
--- a/test/functional/node_network_limited.py
+++ b/test/functional/node_network_limited.py
@@ -2,15 +2,26 @@
# 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.
+"""Tests NODE_NETWORK_LIMITED.
+
+Tests that a node configured with -prune=550 signals NODE_NETWORK_LIMITED correctly
+and that it responds to getdata requests for blocks correctly:
+ - send a block within 288 + 2 of the tip
+ - disconnect peers who request blocks older than that."""
+from test_framework.messages import CInv, msg_getdata
+from test_framework.mininode import NODE_BLOOM, NODE_NETWORK_LIMITED, NODE_WITNESS, NetworkThread, P2PInterface
from test_framework.test_framework import BitcoinTestFramework
-from test_framework.util import *
-from test_framework.mininode import *
+from test_framework.util import assert_equal
-class BaseNode(P2PInterface):
- nServices = 0
- firstAddrnServices = 0
- def on_version(self, message):
- self.nServices = message.nServices
+class P2PIgnoreInv(P2PInterface):
+ def on_inv(self, message):
+ # The node will send us invs for other blocks. Ignore them.
+ pass
+
+ def send_getdata_for_block(self, blockhash):
+ getdata_request = msg_getdata()
+ getdata_request.inv.append(CInv(2, int(blockhash, 16)))
+ self.send_message(getdata_request)
class NodeNetworkLimitedTest(BitcoinTestFramework):
def set_test_params(self):
@@ -18,64 +29,29 @@ class NodeNetworkLimitedTest(BitcoinTestFramework):
self.num_nodes = 1
self.extra_args = [['-prune=550']]
- def getSignaledServiceFlags(self):
- node = self.nodes[0].add_p2p_connection(BaseNode())
+ def run_test(self):
+ node = self.nodes[0].add_p2p_connection(P2PIgnoreInv())
NetworkThread().start()
node.wait_for_verack()
- services = node.nServices
- self.nodes[0].disconnect_p2ps()
- node.wait_for_disconnect()
- return services
- def tryGetBlockViaGetData(self, blockhash, must_disconnect):
- node = self.nodes[0].add_p2p_connection(BaseNode())
- NetworkThread().start()
- node.wait_for_verack()
- node.send_message(msg_verack())
- getdata_request = msg_getdata()
- getdata_request.inv.append(CInv(2, int(blockhash, 16)))
- node.send_message(getdata_request)
+ expected_services = NODE_BLOOM | NODE_WITNESS | NODE_NETWORK_LIMITED
- if (must_disconnect):
- #ensure we get disconnected
- node.wait_for_disconnect(5)
- else:
- # check if the peer sends us the requested block
- node.wait_for_block(int(blockhash, 16), 3)
- self.nodes[0].disconnect_p2ps()
- node.wait_for_disconnect()
+ self.log.info("Check that node has signalled expected services.")
+ assert_equal(node.nServices, expected_services)
- def run_test(self):
- #NODE_BLOOM & NODE_WITNESS & NODE_NETWORK_LIMITED must now be signaled
- assert_equal(self.getSignaledServiceFlags(), 1036) #1036 == 0x40C == 0100 0000 1100
-# | ||
-# | |^--- NODE_BLOOM
-# | ^---- NODE_WITNESS
-# ^-- NODE_NETWORK_LIMITED
+ self.log.info("Check that the localservices is as expected.")
+ assert_equal(int(self.nodes[0].getnetworkinfo()['localservices'], 16), expected_services)
- #now mine some blocks over the NODE_NETWORK_LIMITED + 2(racy buffer ext.) target
- firstblock = self.nodes[0].generate(1)[0]
+ self.log.info("Mine enough blocks to reach the NODE_NETWORK_LIMITED range.")
blocks = self.nodes[0].generate(292)
- blockWithinLimitedRange = blocks[-1]
-
- #make sure we can max retrive block at tip-288
- #requesting block at height 2 (tip-289) must fail (ignored)
- self.tryGetBlockViaGetData(firstblock, True) #first block must lead to disconnect
- self.tryGetBlockViaGetData(blocks[1], False) #last block in valid range
- self.tryGetBlockViaGetData(blocks[0], True) #first block outside of the 288+2 limit
-
- #NODE_NETWORK_LIMITED must still be signaled after restart
- self.restart_node(0)
- assert_equal(self.getSignaledServiceFlags(), 1036)
-
- #test the RPC service flags
- assert_equal(self.nodes[0].getnetworkinfo()['localservices'], "000000000000040c")
- # getdata a block above the NODE_NETWORK_LIMITED threshold must be possible
- self.tryGetBlockViaGetData(blockWithinLimitedRange, False)
+ self.log.info("Make sure we can max retrive block at tip-288.")
+ node.send_getdata_for_block(blocks[1]) # last block in valid range
+ node.wait_for_block(int(blocks[1], 16), timeout=3)
- # getdata a block below the NODE_NETWORK_LIMITED threshold must be ignored
- self.tryGetBlockViaGetData(firstblock, True)
+ self.log.info("Requesting block at height 2 (tip-289) must fail (ignored).")
+ node.send_getdata_for_block(blocks[0]) # first block outside of the 288+2 limit
+ node.wait_for_disconnect(5)
if __name__ == '__main__':
NodeNetworkLimitedTest().main()
diff --git a/test/functional/rawtransactions.py b/test/functional/rawtransactions.py
index 79f2a2834e..aa519eb8e8 100755
--- a/test/functional/rawtransactions.py
+++ b/test/functional/rawtransactions.py
@@ -15,6 +15,25 @@ Test the following RPCs:
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
+
+class multidict(dict):
+ """Dictionary that allows duplicate keys.
+
+ Constructed with a list of (key, value) tuples. When dumped by the json module,
+ will output invalid json with repeated keys, eg:
+ >>> json.dumps(multidict([(1,2),(1,2)])
+ '{"1": 2, "1": 2}'
+
+ Used to test calls to rpc methods with repeated keys in the json object."""
+
+ def __init__(self, x):
+ dict.__init__(self, x)
+ self.x = x
+
+ def items(self):
+ return self.x
+
+
# Create one-input, one-output, no-fee transaction:
class RawTransactionsTest(BitcoinTestFramework):
def set_test_params(self):
@@ -39,6 +58,41 @@ class RawTransactionsTest(BitcoinTestFramework):
self.nodes[0].generate(5)
self.sync_all()
+ # Test `createrawtransaction` required parameters
+ assert_raises_rpc_error(-1, "createrawtransaction", self.nodes[0].createrawtransaction)
+ assert_raises_rpc_error(-1, "createrawtransaction", self.nodes[0].createrawtransaction, [])
+
+ # Test `createrawtransaction` invalid extra parameters
+ assert_raises_rpc_error(-1, "createrawtransaction", self.nodes[0].createrawtransaction, [], {}, 0, False, 'foo')
+
+ # Test `createrawtransaction` invalid `inputs`
+ txid = '1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000'
+ assert_raises_rpc_error(-3, "Expected type array", self.nodes[0].createrawtransaction, 'foo', {})
+ assert_raises_rpc_error(-1, "JSON value is not an object as expected", self.nodes[0].createrawtransaction, ['foo'], {})
+ assert_raises_rpc_error(-8, "txid must be hexadecimal string", self.nodes[0].createrawtransaction, [{}], {})
+ assert_raises_rpc_error(-8, "txid must be hexadecimal string", self.nodes[0].createrawtransaction, [{'txid': 'foo'}], {})
+ assert_raises_rpc_error(-8, "Invalid parameter, missing vout key", self.nodes[0].createrawtransaction, [{'txid': txid}], {})
+ assert_raises_rpc_error(-8, "Invalid parameter, missing vout key", self.nodes[0].createrawtransaction, [{'txid': txid, 'vout': 'foo'}], {})
+ assert_raises_rpc_error(-8, "Invalid parameter, vout must be positive", self.nodes[0].createrawtransaction, [{'txid': txid, 'vout': -1}], {})
+ assert_raises_rpc_error(-8, "Invalid parameter, sequence number is out of range", self.nodes[0].createrawtransaction, [{'txid': txid, 'vout': 0, 'sequence': -1}], {})
+
+ # Test `createrawtransaction` invalid `outputs`
+ address = self.nodes[0].getnewaddress()
+ assert_raises_rpc_error(-3, "Expected type object", self.nodes[0].createrawtransaction, [], 'foo')
+ assert_raises_rpc_error(-8, "Data must be hexadecimal string", self.nodes[0].createrawtransaction, [], {'data': 'foo'})
+ assert_raises_rpc_error(-5, "Invalid Bitcoin address", self.nodes[0].createrawtransaction, [], {'foo': 0})
+ assert_raises_rpc_error(-3, "Invalid amount", self.nodes[0].createrawtransaction, [], {address: 'foo'})
+ assert_raises_rpc_error(-3, "Amount out of range", self.nodes[0].createrawtransaction, [], {address: -1})
+ assert_raises_rpc_error(-8, "Invalid parameter, duplicated address: %s" % address, self.nodes[0].createrawtransaction, [], multidict([(address, 1), (address, 1)]))
+
+ # Test `createrawtransaction` invalid `locktime`
+ assert_raises_rpc_error(-3, "Expected type number", self.nodes[0].createrawtransaction, [], {}, 'foo')
+ assert_raises_rpc_error(-8, "Invalid parameter, locktime out of range", self.nodes[0].createrawtransaction, [], {}, -1)
+ assert_raises_rpc_error(-8, "Invalid parameter, locktime out of range", self.nodes[0].createrawtransaction, [], {}, 4294967296)
+
+ # Test `createrawtransaction` invalid `replaceable`
+ assert_raises_rpc_error(-3, "Expected type bool", self.nodes[0].createrawtransaction, [], {}, 0, 'foo')
+
#########################################
# sendrawtransaction with missing input #
#########################################
@@ -199,6 +253,17 @@ class RawTransactionsTest(BitcoinTestFramework):
self.sync_all()
assert_equal(self.nodes[0].getbalance(), bal+Decimal('50.00000000')+Decimal('2.19000000')) #block reward + tx
+ # decoderawtransaction tests
+ # witness transaction
+ encrawtx = "010000000001010000000000000072c1a6a246ae63f74f931e8365e15a089c68d61900000000000000000000ffffffff0100e1f50500000000000000000000"
+ decrawtx = self.nodes[0].decoderawtransaction(encrawtx, True) # decode as witness transaction
+ assert_equal(decrawtx['vout'][0]['value'], Decimal('1.00000000'))
+ assert_raises_rpc_error(-22, 'TX decode failed', self.nodes[0].decoderawtransaction, encrawtx, False) # force decode as non-witness transaction
+ # non-witness transaction
+ encrawtx = "01000000010000000000000072c1a6a246ae63f74f931e8365e15a089c68d61900000000000000000000ffffffff0100e1f505000000000000000000"
+ decrawtx = self.nodes[0].decoderawtransaction(encrawtx, False) # decode as non-witness transaction
+ assert_equal(decrawtx['vout'][0]['value'], Decimal('1.00000000'))
+
# getrawtransaction tests
# 1. valid parameters - only supply txid
txHash = rawTx["hash"]
diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py
index 2ab1bdac0f..d8032e4430 100644
--- a/test/functional/test_framework/messages.py
+++ b/test/functional/test_framework/messages.py
@@ -38,10 +38,11 @@ COIN = 100000000 # 1 btc in satoshis
NODE_NETWORK = (1 << 0)
# NODE_GETUTXO = (1 << 1)
-# NODE_BLOOM = (1 << 2)
+NODE_BLOOM = (1 << 2)
NODE_WITNESS = (1 << 3)
NODE_UNSUPPORTED_SERVICE_BIT_5 = (1 << 5)
NODE_UNSUPPORTED_SERVICE_BIT_7 = (1 << 7)
+NODE_NETWORK_LIMITED = (1 << 10)
# Serialization/deserialization tools
def sha256(s):