Changeset View
Changeset View
Standalone View
Standalone View
test/functional/rpc_blockchain.py
Show All 27 Lines | from test_framework.util import ( | ||||
assert_equal, | assert_equal, | ||||
assert_greater_than, | assert_greater_than, | ||||
assert_greater_than_or_equal, | assert_greater_than_or_equal, | ||||
assert_raises, | assert_raises, | ||||
assert_raises_rpc_error, | assert_raises_rpc_error, | ||||
assert_is_hash_string, | assert_is_hash_string, | ||||
assert_is_hex_string, | assert_is_hex_string, | ||||
) | ) | ||||
from test_framework.blocktools import ( | |||||
create_block, | |||||
create_coinbase, | |||||
) | |||||
from test_framework.messages import ( | |||||
msg_block, | |||||
) | |||||
from test_framework.mininode import ( | |||||
P2PInterface, | |||||
network_thread_start, | |||||
) | |||||
class BlockchainTest(BitcoinTestFramework): | class BlockchainTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.num_nodes = 1 | self.num_nodes = 1 | ||||
self.extra_args = [['-stopatheight=207', '-prune=1']] | self.extra_args = [['-stopatheight=207', '-prune=1']] | ||||
def run_test(self): | def run_test(self): | ||||
self._test_getblockchaininfo() | self._test_getblockchaininfo() | ||||
self._test_getchaintxstats() | 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() | ||||
self._test_getblock() | self._test_getblock() | ||||
self._test_waitforblockheight() | |||||
assert self.nodes[0].verifychain(4, 0) | assert self.nodes[0].verifychain(4, 0) | ||||
def _test_getblockchaininfo(self): | def _test_getblockchaininfo(self): | ||||
self.log.info("Test getblockchaininfo") | self.log.info("Test getblockchaininfo") | ||||
keys = [ | keys = [ | ||||
'bestblockhash', | 'bestblockhash', | ||||
'blocks', | 'blocks', | ||||
▲ Show 20 Lines • Show All 227 Lines • ▼ Show 20 Lines | def _test_getblock(self): | ||||
getblockinfo['difficulty'], getblockheaderinfo['difficulty']) | getblockinfo['difficulty'], getblockheaderinfo['difficulty']) | ||||
assert_equal( | assert_equal( | ||||
getblockinfo['chainwork'], getblockheaderinfo['chainwork']) | getblockinfo['chainwork'], getblockheaderinfo['chainwork']) | ||||
assert_equal( | assert_equal( | ||||
getblockinfo['previousblockhash'], getblockheaderinfo['previousblockhash']) | getblockinfo['previousblockhash'], getblockheaderinfo['previousblockhash']) | ||||
assert_equal( | assert_equal( | ||||
getblockinfo['nextblockhash'], getblockheaderinfo['nextblockhash']) | getblockinfo['nextblockhash'], getblockheaderinfo['nextblockhash']) | ||||
def _test_waitforblockheight(self): | |||||
self.log.info("Test waitforblockheight") | |||||
node = self.nodes[0] | |||||
# Start a P2P connection since we'll need to create some blocks. | |||||
node.add_p2p_connection(P2PInterface()) | |||||
network_thread_start() | |||||
node.p2p.wait_for_verack() | |||||
current_height = node.getblock(node.getbestblockhash())['height'] | |||||
# Create a fork somewhere below our current height, invalidate the tip | |||||
# of that fork, and then ensure that waitforblockheight still | |||||
# works as expected. | |||||
# | |||||
# (Previously this was broken based on setting | |||||
# `rpc/blockchain.cpp:latestblock` incorrectly.) | |||||
# | |||||
b20hash = node.getblockhash(20) | |||||
b20 = node.getblock(b20hash) | |||||
def solve_and_send_block(prevhash, height, time): | |||||
b = create_block(prevhash, create_coinbase(height), time) | |||||
b.solve() | |||||
node.p2p.send_message(msg_block(b)) | |||||
node.p2p.sync_with_ping() | |||||
return b | |||||
b21f = solve_and_send_block(int(b20hash, 16), 21, b20['time'] + 1) | |||||
b22f = solve_and_send_block(b21f.sha256, 22, b21f.nTime + 1) | |||||
node.invalidateblock(b22f.hash) | |||||
def assert_waitforheight(height, timeout=2): | |||||
assert_equal( | |||||
node.waitforblockheight(height, timeout)['height'], | |||||
current_height) | |||||
assert_waitforheight(0) | |||||
assert_waitforheight(current_height - 1) | |||||
assert_waitforheight(current_height) | |||||
assert_waitforheight(current_height + 1) | |||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
BlockchainTest().main() | BlockchainTest().main() |