mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 04:22:55 +01:00
9590929900
2ef5294a5bb68ceb3797d2638567a172cc21699f rpc: add RPCTypeCheck for getblockfrompeer inputs (Jon Atack) 734b9669ff7b2f5e2820993443a6f868f6b0b20a test: add getblockfrompeer coverage of invalid inputs (Jon Atack) Pull request description: The new getblockfrompeer RPC lacks test coverage for invalid arguments, and its error messages are not harmonized with the existing RPCs. Fix all issues. ACKs for top commit: brunoerg: ACK 2ef5294a5bb68ceb3797d2638567a172cc21699f Tree-SHA512: 454782cf6a44fd0e05483bb152153667ef5c8021358385ddcf89724fbbbd35e187362bdff757e00c99319527bc4c0b20c7187f67241d4585d767a29787142f25
76 lines
3.1 KiB
Python
Executable File
76 lines
3.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# Copyright (c) 2020 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 the getblockfrompeer RPC."""
|
|
|
|
from test_framework.authproxy import JSONRPCException
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
from test_framework.util import (
|
|
assert_equal,
|
|
assert_raises_rpc_error,
|
|
)
|
|
|
|
class GetBlockFromPeerTest(BitcoinTestFramework):
|
|
def set_test_params(self):
|
|
self.num_nodes = 2
|
|
|
|
def setup_network(self):
|
|
self.setup_nodes()
|
|
|
|
def check_for_block(self, hash):
|
|
try:
|
|
self.nodes[0].getblock(hash)
|
|
return True
|
|
except JSONRPCException:
|
|
return False
|
|
|
|
def run_test(self):
|
|
self.log.info("Mine 4 blocks on Node 0")
|
|
self.nodes[0].generate(4)
|
|
assert_equal(self.nodes[0].getblockcount(), 204)
|
|
|
|
self.log.info("Mine competing 3 blocks on Node 1")
|
|
self.nodes[1].generate(3)
|
|
assert_equal(self.nodes[1].getblockcount(), 203)
|
|
short_tip = self.nodes[1].getbestblockhash()
|
|
|
|
self.log.info("Connect nodes to sync headers")
|
|
self.connect_nodes(0, 1)
|
|
self.sync_blocks()
|
|
|
|
self.log.info("Node 0 should only have the header for node 1's block 3")
|
|
x = next(filter(lambda x: x['hash'] == short_tip, self.nodes[0].getchaintips()))
|
|
assert_equal(x['status'], "headers-only")
|
|
assert_raises_rpc_error(-1, "Block not found on disk", self.nodes[0].getblock, short_tip)
|
|
|
|
self.log.info("Fetch block from node 1")
|
|
peers = self.nodes[0].getpeerinfo()
|
|
assert_equal(len(peers), 1)
|
|
peer_0_peer_1_id = peers[0]["id"]
|
|
|
|
self.log.info("Arguments must be valid")
|
|
assert_raises_rpc_error(-8, "hash must be of length 64 (not 4, for '1234')", self.nodes[0].getblockfrompeer, "1234", peer_0_peer_1_id)
|
|
assert_raises_rpc_error(-3, "Expected type string, got number", self.nodes[0].getblockfrompeer, 1234, peer_0_peer_1_id)
|
|
assert_raises_rpc_error(-3, "Expected type number, got string", self.nodes[0].getblockfrompeer, short_tip, "0")
|
|
|
|
self.log.info("We must already have the header")
|
|
assert_raises_rpc_error(-1, "Block header missing", self.nodes[0].getblockfrompeer, "00" * 32, 0)
|
|
|
|
self.log.info("Non-existent peer generates error")
|
|
for peer_id in [-1, peer_0_peer_1_id + 1]:
|
|
assert_raises_rpc_error(-1, "Peer does not exist", self.nodes[0].getblockfrompeer, short_tip, peer_id)
|
|
|
|
self.log.info("Successful fetch")
|
|
result = self.nodes[0].getblockfrompeer(short_tip, peer_0_peer_1_id)
|
|
self.wait_until(lambda: self.check_for_block(short_tip), timeout=1)
|
|
assert(not "warnings" in result)
|
|
|
|
self.log.info("Don't fetch blocks we already have")
|
|
result = self.nodes[0].getblockfrompeer(short_tip, peer_0_peer_1_id)
|
|
assert("warnings" in result)
|
|
assert_equal(result["warnings"], "Block already downloaded")
|
|
|
|
if __name__ == '__main__':
|
|
GetBlockFromPeerTest().main()
|