Changeset View
Changeset View
Standalone View
Standalone View
test/functional/mining_basic.py
Show All 33 Lines | def assert_template(node, block, expect, rehash=True): | ||||
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 = False | ||||
def skip_test_if_missing_module(self): | |||||
self.skip_if_no_wallet() | |||||
def run_test(self): | def run_test(self): | ||||
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_equal(mining_info['currentblocksize'], 0) | ||||
assert_equal(mining_info['currentblocktx'], 0) | assert_equal(mining_info['currentblocktx'], 0) | ||||
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.generate(1) | node.generatetoaddress(1, node.get_deterministic_priv_key().address) | ||||
tmpl = node.getblocktemplate() | tmpl = node.getblocktemplate() | ||||
self.log.info("getblocktemplate: Test capability advertised") | self.log.info("getblocktemplate: Test capability advertised") | ||||
assert 'proposal' in tmpl['capabilities'] | assert 'proposal' in tmpl['capabilities'] | ||||
assert 'coinbasetxn' not in tmpl | assert 'coinbasetxn' not in tmpl | ||||
coinbase_tx = create_coinbase(height=int(tmpl["height"]) + 1) | coinbase_tx = create_coinbase(height=int(tmpl["height"]) + 1) | ||||
# sequence numbers must not be max for nLockTime to have effect | # sequence numbers must not be max for nLockTime to have effect | ||||
coinbase_tx.vin[0].nSequence = 2 ** 32 - 2 | coinbase_tx.vin[0].nSequence = 2 ** 32 - 2 | ||||
▲ Show 20 Lines • Show All 158 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
# Drop the first getheaders | # Drop the first getheaders | ||||
node.p2p.wait_for_getheaders(timeout=5) | node.p2p.wait_for_getheaders(timeout=5) | ||||
node.p2p.send_blocks_and_test(blocks=[block], node=node) | node.p2p.send_blocks_and_test(blocks=[block], node=node) | ||||
# Must be active now: | # Must be active now: | ||||
assert chain_tip(block.hash, status='active', | assert chain_tip(block.hash, status='active', | ||||
branchlen=0) in node.getchaintips() | branchlen=0) in node.getchaintips() | ||||
# Building a few blocks should give the same results | # Building a few blocks should give the same results | ||||
node.generate(10) | node.generatetoaddress(10, node.get_deterministic_priv_key().address) | ||||
assert_raises_rpc_error(-25, 'time-too-old', lambda: node.submitheader( | assert_raises_rpc_error(-25, 'time-too-old', lambda: node.submitheader( | ||||
hexdata=CBlockHeader(bad_block_time).serialize().hex())) | hexdata=CBlockHeader(bad_block_time).serialize().hex())) | ||||
assert_raises_rpc_error(-25, 'bad-prevblk', lambda: node.submitheader( | assert_raises_rpc_error(-25, 'bad-prevblk', lambda: node.submitheader( | ||||
hexdata=CBlockHeader(bad_block2).serialize().hex())) | hexdata=CBlockHeader(bad_block2).serialize().hex())) | ||||
node.submitheader(hexdata=CBlockHeader(block).serialize().hex()) | node.submitheader(hexdata=CBlockHeader(block).serialize().hex()) | ||||
node.submitheader(hexdata=CBlockHeader( | node.submitheader(hexdata=CBlockHeader( | ||||
bad_block_root).serialize().hex()) | bad_block_root).serialize().hex()) | ||||
# valid | # valid | ||||
assert_equal(node.submitblock( | assert_equal(node.submitblock( | ||||
hexdata=block.serialize().hex()), 'duplicate') | hexdata=block.serialize().hex()), 'duplicate') | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
MiningTest().main() | MiningTest().main() |