Changeset View
Changeset View
Standalone View
Standalone View
test/functional/blockchain.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2014-2016 The Bitcoin Core developers | # Copyright (c) 2014-2016 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Test RPCs related to blockchainstate. | """Test RPCs related to blockchainstate. | ||||
Test the following RPCs: | Test the following RPCs: | ||||
- gettxoutsetinfo | - gettxoutsetinfo | ||||
- getdifficulty | - getdifficulty | ||||
- getbestblockhash | - getbestblockhash | ||||
- getblockhash | - getblockhash | ||||
- getblockheader | - getblockheader | ||||
- getchaintxstats | |||||
- getnetworkhashps | - getnetworkhashps | ||||
- verifychain | - verifychain | ||||
Tests correspond to code in rpc/blockchain.cpp. | Tests correspond to code in rpc/blockchain.cpp. | ||||
""" | """ | ||||
from decimal import Decimal | from decimal import Decimal | ||||
import http.client | import http.client | ||||
import subprocess | import subprocess | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import ( | from test_framework.util import ( | ||||
assert_equal, | assert_equal, | ||||
assert_raises, | assert_raises, | ||||
assert_raises_jsonrpc, | assert_raises_jsonrpc, | ||||
assert_is_hex_string, | assert_is_hex_string, | ||||
assert_is_hash_string, | assert_is_hash_string, | ||||
start_node, | |||||
bitcoind_processes, | |||||
BITCOIND_PROC_WAIT_TIMEOUT, | BITCOIND_PROC_WAIT_TIMEOUT, | ||||
) | ) | ||||
class BlockchainTest(BitcoinTestFramework): | class BlockchainTest(BitcoinTestFramework): | ||||
def __init__(self): | def __init__(self): | ||||
super().__init__() | super().__init__() | ||||
self.setup_clean_chain = False | self.setup_clean_chain = False | ||||
self.num_nodes = 1 | self.num_nodes = 1 | ||||
self.extra_args = [['-stopatheight=207']] | self.extra_args = [['-stopatheight=207']] | ||||
def run_test(self): | def run_test(self): | ||||
self._test_getchaintxstats() | |||||
self._test_gettxoutsetinfo() | self._test_gettxoutsetinfo() | ||||
self._test_getblockheader() | self._test_getblockheader() | ||||
self._test_getdifficulty() | self._test_getdifficulty() | ||||
self._test_getnetworkhashps() | self._test_getnetworkhashps() | ||||
self._test_stopatheight() | self._test_stopatheight() | ||||
assert self.nodes[0].verifychain(4, 0) | assert self.nodes[0].verifychain(4, 0) | ||||
def _test_getchaintxstats(self): | |||||
chaintxstats = self.nodes[0].getchaintxstats(1) | |||||
# 200 txs plus genesis tx | |||||
assert_equal(chaintxstats['txcount'], 201) | |||||
# tx rate should be 1 per 10 minutes, or 1/600 | |||||
# we have to round because of binary math | |||||
assert_equal(round(chaintxstats['txrate'] * 600, 10), Decimal(1)) | |||||
b1 = self.nodes[0].getblock(self.nodes[0].getblockhash(1)) | |||||
b200 = self.nodes[0].getblock(self.nodes[0].getblockhash(200)) | |||||
time_diff = b200['mediantime'] - b1['mediantime'] | |||||
chaintxstats = self.nodes[0].getchaintxstats() | |||||
assert_equal(chaintxstats['time'], b200['time']) | |||||
assert_equal(chaintxstats['txcount'], 201) | |||||
assert_equal(chaintxstats['window_block_count'], 199) | |||||
assert_equal(chaintxstats['window_tx_count'], 199) | |||||
assert_equal(chaintxstats['window_interval'], time_diff) | |||||
assert_equal( | |||||
round(chaintxstats['txrate'] * time_diff, 10), Decimal(199)) | |||||
chaintxstats = self.nodes[0].getchaintxstats(blockhash=b1['hash']) | |||||
assert_equal(chaintxstats['time'], b1['time']) | |||||
assert_equal(chaintxstats['txcount'], 2) | |||||
assert_equal(chaintxstats['window_block_count'], 0) | |||||
assert('window_tx_count' not in chaintxstats) | |||||
assert('window_interval' not in chaintxstats) | |||||
assert('txrate' not in chaintxstats) | |||||
assert_raises_jsonrpc(-8, "Invalid block count: should be between 0 and the block's height - 1", | |||||
self.nodes[0].getchaintxstats, 201) | |||||
def _test_gettxoutsetinfo(self): | def _test_gettxoutsetinfo(self): | ||||
node = self.nodes[0] | node = self.nodes[0] | ||||
res = node.gettxoutsetinfo() | res = node.gettxoutsetinfo() | ||||
assert_equal(res['total_amount'], Decimal('8725.00000000')) | assert_equal(res['total_amount'], Decimal('8725.00000000')) | ||||
assert_equal(res['transactions'], 200) | assert_equal(res['transactions'], 200) | ||||
assert_equal(res['height'], 200) | assert_equal(res['height'], 200) | ||||
assert_equal(res['txouts'], 200) | assert_equal(res['txouts'], 200) | ||||
▲ Show 20 Lines • Show All 91 Lines • Show Last 20 Lines |