Changeset View
Changeset View
Standalone View
Standalone View
test/functional/mining_basic.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2014-2016 The Bitcoin Core developers | # Copyright (c) 2014-2019 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 mining RPCs | """Test mining RPCs | ||||
- getmininginfo | - getmininginfo | ||||
- getblocktemplate proposal mode | - getblocktemplate proposal mode | ||||
- submitblock""" | - submitblock""" | ||||
import copy | import copy | ||||
from decimal import Decimal | from decimal import Decimal | ||||
from test_framework.blocktools import create_coinbase | from test_framework.blocktools import ( | ||||
create_coinbase, | |||||
TIME_GENESIS_BLOCK, | |||||
) | |||||
from test_framework.messages import ( | from test_framework.messages import ( | ||||
CBlock, | CBlock, | ||||
CBlockHeader, | CBlockHeader, | ||||
) | ) | ||||
from test_framework.mininode import ( | from test_framework.mininode import ( | ||||
P2PDataStore, | P2PDataStore, | ||||
) | ) | ||||
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_rpc_error, | assert_raises_rpc_error, | ||||
connect_nodes_bi, | |||||
) | ) | ||||
def assert_template(node, block, expect, rehash=True): | def assert_template(node, block, expect, rehash=True): | ||||
if rehash: | if rehash: | ||||
block.hashMerkleRoot = block.calc_merkle_root() | block.hashMerkleRoot = block.calc_merkle_root() | ||||
rsp = node.getblocktemplate( | rsp = node.getblocktemplate( | ||||
template_request={ | template_request={ | ||||
'data': block.serialize().hex(), | 'data': block.serialize().hex(), | ||||
'mode': 'proposal'}) | 'mode': 'proposal'}) | ||||
assert_equal(rsp, expect) | assert_equal(rsp, expect) | ||||
class MiningTest(BitcoinTestFramework): | class MiningTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.num_nodes = 2 | self.num_nodes = 2 | ||||
self.setup_clean_chain = False | self.setup_clean_chain = True | ||||
def mine_chain(self): | |||||
self.log.info('Create some old blocks') | |||||
node = self.nodes[0] | |||||
address = node.get_deterministic_priv_key().address | |||||
for t in range(TIME_GENESIS_BLOCK, | |||||
TIME_GENESIS_BLOCK + 200 * 600, 600): | |||||
node.setmocktime(t) | |||||
node.generatetoaddress(1, address) | |||||
mining_info = node.getmininginfo() | |||||
assert_equal(mining_info['blocks'], 200) | |||||
assert_equal(mining_info['currentblocktx'], 0) | |||||
assert_equal(mining_info['currentblocksize'], 1000) | |||||
self.restart_node(0) | |||||
connect_nodes_bi(self.nodes[0], self.nodes[1]) | |||||
def run_test(self): | def run_test(self): | ||||
self.mine_chain() | |||||
node = self.nodes[0] | node = self.nodes[0] | ||||
def assert_submitblock(block, result_str_1, result_str_2=None): | def assert_submitblock(block, result_str_1, result_str_2=None): | ||||
block.solve() | block.solve() | ||||
result_str_2 = result_str_2 or 'duplicate-invalid' | result_str_2 = result_str_2 or 'duplicate-invalid' | ||||
assert_equal(result_str_1, node.submitblock( | assert_equal(result_str_1, node.submitblock( | ||||
hexdata=block.serialize().hex())) | hexdata=block.serialize().hex())) | ||||
assert_equal(result_str_2, node.submitblock( | assert_equal(result_str_2, node.submitblock( | ||||
hexdata=block.serialize().hex())) | hexdata=block.serialize().hex())) | ||||
self.log.info('getmininginfo') | self.log.info('getmininginfo') | ||||
mining_info = node.getmininginfo() | mining_info = node.getmininginfo() | ||||
assert_equal(mining_info['blocks'], 200) | assert_equal(mining_info['blocks'], 200) | ||||
assert_equal(mining_info['chain'], 'regtest') | assert_equal(mining_info['chain'], 'regtest') | ||||
assert_equal(mining_info['currentblocksize'], 0) | assert 'currentblocktx' not in mining_info | ||||
assert_equal(mining_info['currentblocktx'], 0) | assert 'currentblocksize' not in mining_info | ||||
assert_equal(mining_info['difficulty'], | assert_equal(mining_info['difficulty'], | ||||
Decimal('4.656542373906925E-10')) | Decimal('4.656542373906925E-10')) | ||||
assert_equal(mining_info['networkhashps'], | assert_equal(mining_info['networkhashps'], | ||||
Decimal('0.003333333333333334')) | Decimal('0.003333333333333334')) | ||||
assert_equal(mining_info['pooledtx'], 0) | assert_equal(mining_info['pooledtx'], 0) | ||||
# Mine a block to leave initial block download | # Mine a block to leave initial block download | ||||
node.generatetoaddress(1, node.get_deterministic_priv_key().address) | node.generatetoaddress(1, node.get_deterministic_priv_key().address) | ||||
▲ Show 20 Lines • Show All 189 Lines • Show Last 20 Lines |