2016-03-19 20:58:06 +01:00
|
|
|
#!/usr/bin/env python3
|
2019-02-11 17:59:34 +01:00
|
|
|
# Copyright (c) 2014-2019 The Bitcoin Core developers
|
2014-11-17 19:47:40 +01:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-04-11 22:47:10 +02:00
|
|
|
"""Test mining RPCs
|
|
|
|
|
2017-08-26 00:20:33 +02:00
|
|
|
- getmininginfo
|
2017-04-11 22:47:10 +02:00
|
|
|
- getblocktemplate proposal mode
|
|
|
|
- submitblock"""
|
2014-11-17 19:47:40 +01:00
|
|
|
|
2017-04-11 22:42:12 +02:00
|
|
|
import copy
|
2017-08-26 00:20:33 +02:00
|
|
|
from decimal import Decimal
|
2014-11-17 19:47:40 +01:00
|
|
|
|
2019-02-11 17:59:34 +01:00
|
|
|
from test_framework.blocktools import (
|
|
|
|
create_coinbase,
|
|
|
|
TIME_GENESIS_BLOCK,
|
|
|
|
)
|
2018-06-05 02:27:08 +02:00
|
|
|
from test_framework.messages import (
|
|
|
|
CBlock,
|
|
|
|
CBlockHeader,
|
2019-01-23 16:44:13 +01:00
|
|
|
BLOCK_HEADER_SIZE
|
2018-06-05 02:27:08 +02:00
|
|
|
)
|
|
|
|
from test_framework.mininode import (
|
|
|
|
P2PDataStore,
|
|
|
|
)
|
2017-08-26 00:20:33 +02:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2018-06-05 02:27:08 +02:00
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal,
|
|
|
|
assert_raises_rpc_error,
|
2019-08-27 21:13:56 +02:00
|
|
|
connect_nodes,
|
2018-06-05 02:27:08 +02:00
|
|
|
)
|
2018-11-27 16:06:19 +01:00
|
|
|
from test_framework.script import CScriptNum
|
2014-11-17 19:47:40 +01:00
|
|
|
|
2019-02-11 17:59:34 +01:00
|
|
|
|
2017-04-11 22:42:12 +02:00
|
|
|
def assert_template(node, block, expect, rehash=True):
|
|
|
|
if rehash:
|
|
|
|
block.hashMerkleRoot = block.calc_merkle_root()
|
2019-02-18 16:35:48 +01:00
|
|
|
rsp = node.getblocktemplate(template_request={'data': block.serialize().hex(), 'mode': 'proposal', 'rules': ['segwit']})
|
2017-04-11 21:13:09 +02:00
|
|
|
assert_equal(rsp, expect)
|
2014-11-17 19:47:40 +01:00
|
|
|
|
2018-11-13 19:42:36 +01:00
|
|
|
|
2017-04-11 22:47:10 +02:00
|
|
|
class MiningTest(BitcoinTestFramework):
|
2017-06-10 00:21:21 +02:00
|
|
|
def set_test_params(self):
|
2016-05-14 13:01:31 +02:00
|
|
|
self.num_nodes = 2
|
2019-02-11 17:59:34 +01:00
|
|
|
self.setup_clean_chain = True
|
|
|
|
|
|
|
|
def mine_chain(self):
|
|
|
|
self.log.info('Create some old blocks')
|
|
|
|
for t in range(TIME_GENESIS_BLOCK, TIME_GENESIS_BLOCK + 200 * 600, 600):
|
|
|
|
self.nodes[0].setmocktime(t)
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
mining_info = self.nodes[0].getmininginfo()
|
|
|
|
assert_equal(mining_info['blocks'], 200)
|
|
|
|
assert_equal(mining_info['currentblocktx'], 0)
|
|
|
|
assert_equal(mining_info['currentblockweight'], 4000)
|
|
|
|
self.restart_node(0)
|
2019-08-27 21:13:56 +02:00
|
|
|
connect_nodes(self.nodes[0], 1)
|
2016-05-14 13:01:31 +02:00
|
|
|
|
2018-09-09 19:32:37 +02:00
|
|
|
def skip_test_if_missing_module(self):
|
|
|
|
self.skip_if_no_wallet()
|
|
|
|
|
2014-11-17 19:47:40 +01:00
|
|
|
def run_test(self):
|
2019-02-11 17:59:34 +01:00
|
|
|
self.mine_chain()
|
2014-11-17 19:47:40 +01:00
|
|
|
node = self.nodes[0]
|
2017-08-26 00:20:33 +02:00
|
|
|
|
2018-08-15 19:54:36 +02:00
|
|
|
def assert_submitblock(block, result_str_1, result_str_2=None):
|
|
|
|
block.solve()
|
|
|
|
result_str_2 = result_str_2 or 'duplicate-invalid'
|
2019-02-18 16:35:48 +01:00
|
|
|
assert_equal(result_str_1, node.submitblock(hexdata=block.serialize().hex()))
|
|
|
|
assert_equal(result_str_2, node.submitblock(hexdata=block.serialize().hex()))
|
2018-08-15 19:54:36 +02:00
|
|
|
|
2017-08-26 00:20:33 +02:00
|
|
|
self.log.info('getmininginfo')
|
|
|
|
mining_info = node.getmininginfo()
|
|
|
|
assert_equal(mining_info['blocks'], 200)
|
2019-09-19 16:31:07 +02:00
|
|
|
assert_equal(mining_info['chain'], 'lbrycrdreg')
|
2019-02-11 17:59:34 +01:00
|
|
|
assert 'currentblocktx' not in mining_info
|
|
|
|
assert 'currentblockweight' not in mining_info
|
2019-09-19 16:31:07 +02:00
|
|
|
assert_equal(mining_info['difficulty'], Decimal('0.00003051711610163642'))
|
2017-08-26 00:20:33 +02:00
|
|
|
assert_equal(mining_info['networkhashps'], Decimal('0.003333333333333334'))
|
|
|
|
assert_equal(mining_info['pooledtx'], 0)
|
|
|
|
|
2017-04-11 19:05:41 +02:00
|
|
|
# Mine a block to leave initial block download
|
2018-09-15 11:32:12 +02:00
|
|
|
node.generatetoaddress(1, node.get_deterministic_priv_key().address)
|
2018-11-26 17:17:38 +01:00
|
|
|
tmpl = node.getblocktemplate({'rules': ['segwit']})
|
2017-04-11 22:42:12 +02:00
|
|
|
self.log.info("getblocktemplate: Test capability advertised")
|
|
|
|
assert 'proposal' in tmpl['capabilities']
|
2017-04-11 21:09:19 +02:00
|
|
|
assert 'coinbasetxn' not in tmpl
|
|
|
|
|
2018-11-27 16:06:19 +01:00
|
|
|
next_height = int(tmpl["height"])
|
|
|
|
coinbase_tx = create_coinbase(height=next_height)
|
2017-04-11 21:09:19 +02:00
|
|
|
# sequence numbers must not be max for nLockTime to have effect
|
|
|
|
coinbase_tx.vin[0].nSequence = 2 ** 32 - 2
|
2017-04-11 22:42:12 +02:00
|
|
|
coinbase_tx.rehash()
|
2014-11-17 19:47:40 +01:00
|
|
|
|
2018-11-27 16:06:19 +01:00
|
|
|
# round-trip the encoded bip34 block height commitment
|
|
|
|
assert_equal(CScriptNum.decode(coinbase_tx.vin[0].scriptSig), next_height)
|
|
|
|
# round-trip negative and multi-byte CScriptNums to catch python regression
|
|
|
|
assert_equal(CScriptNum.decode(CScriptNum.encode(CScriptNum(1500))), 1500)
|
|
|
|
assert_equal(CScriptNum.decode(CScriptNum.encode(CScriptNum(-1500))), -1500)
|
|
|
|
assert_equal(CScriptNum.decode(CScriptNum.encode(CScriptNum(-1))), -1)
|
|
|
|
|
2017-04-11 22:42:12 +02:00
|
|
|
block = CBlock()
|
|
|
|
block.nVersion = tmpl["version"]
|
|
|
|
block.hashPrevBlock = int(tmpl["previousblockhash"], 16)
|
|
|
|
block.nTime = tmpl["curtime"]
|
|
|
|
block.nBits = int(tmpl["bits"], 16)
|
|
|
|
block.nNonce = 0
|
|
|
|
block.vtx = [coinbase_tx]
|
2014-11-17 19:47:40 +01:00
|
|
|
|
2018-11-26 17:17:38 +01:00
|
|
|
self.log.info("getblocktemplate: segwit rule must be set")
|
|
|
|
assert_raises_rpc_error(-8, "getblocktemplate must be called with the segwit rule set", node.getblocktemplate)
|
|
|
|
|
2017-04-11 22:39:37 +02:00
|
|
|
self.log.info("getblocktemplate: Test valid block")
|
|
|
|
assert_template(node, block, None)
|
|
|
|
|
2017-04-11 22:42:35 +02:00
|
|
|
self.log.info("submitblock: Test block decode failure")
|
2019-02-18 16:35:48 +01:00
|
|
|
assert_raises_rpc_error(-22, "Block decode failed", node.submitblock, block.serialize()[:-15].hex())
|
2017-04-11 22:42:35 +02:00
|
|
|
|
2017-04-11 19:10:20 +02:00
|
|
|
self.log.info("getblocktemplate: Test bad input hash for coinbase transaction")
|
2017-04-11 22:42:12 +02:00
|
|
|
bad_block = copy.deepcopy(block)
|
|
|
|
bad_block.vtx[0].vin[0].prevout.hash += 1
|
|
|
|
bad_block.vtx[0].rehash()
|
|
|
|
assert_template(node, bad_block, 'bad-cb-missing')
|
|
|
|
|
2017-04-11 22:42:35 +02:00
|
|
|
self.log.info("submitblock: Test invalid coinbase transaction")
|
2019-02-18 16:35:48 +01:00
|
|
|
assert_raises_rpc_error(-22, "Block does not start with a coinbase", node.submitblock, bad_block.serialize().hex())
|
2014-11-17 19:47:40 +01:00
|
|
|
|
2017-04-11 19:10:20 +02:00
|
|
|
self.log.info("getblocktemplate: Test truncated final transaction")
|
2019-02-18 16:35:48 +01:00
|
|
|
assert_raises_rpc_error(-22, "Block decode failed", node.getblocktemplate, {'data': block.serialize()[:-1].hex(), 'mode': 'proposal', 'rules': ['segwit']})
|
2014-11-17 19:47:40 +01:00
|
|
|
|
2017-04-11 19:10:20 +02:00
|
|
|
self.log.info("getblocktemplate: Test duplicate transaction")
|
2017-04-11 22:42:12 +02:00
|
|
|
bad_block = copy.deepcopy(block)
|
|
|
|
bad_block.vtx.append(bad_block.vtx[0])
|
|
|
|
assert_template(node, bad_block, 'bad-txns-duplicate')
|
2018-08-15 19:54:36 +02:00
|
|
|
assert_submitblock(bad_block, 'bad-txns-duplicate', 'bad-txns-duplicate')
|
2014-11-17 19:47:40 +01:00
|
|
|
|
2017-04-11 19:10:20 +02:00
|
|
|
self.log.info("getblocktemplate: Test invalid transaction")
|
2017-04-11 22:42:12 +02:00
|
|
|
bad_block = copy.deepcopy(block)
|
|
|
|
bad_tx = copy.deepcopy(bad_block.vtx[0])
|
|
|
|
bad_tx.vin[0].prevout.hash = 255
|
|
|
|
bad_tx.rehash()
|
|
|
|
bad_block.vtx.append(bad_tx)
|
|
|
|
assert_template(node, bad_block, 'bad-txns-inputs-missingorspent')
|
2018-08-15 19:54:36 +02:00
|
|
|
assert_submitblock(bad_block, 'bad-txns-inputs-missingorspent')
|
2014-11-17 19:47:40 +01:00
|
|
|
|
2017-04-11 19:10:20 +02:00
|
|
|
self.log.info("getblocktemplate: Test nonfinal transaction")
|
2017-04-11 22:42:12 +02:00
|
|
|
bad_block = copy.deepcopy(block)
|
|
|
|
bad_block.vtx[0].nLockTime = 2 ** 32 - 1
|
|
|
|
bad_block.vtx[0].rehash()
|
|
|
|
assert_template(node, bad_block, 'bad-txns-nonfinal')
|
2018-08-15 19:54:36 +02:00
|
|
|
assert_submitblock(bad_block, 'bad-txns-nonfinal')
|
2014-11-17 19:47:40 +01:00
|
|
|
|
2017-04-11 19:10:20 +02:00
|
|
|
self.log.info("getblocktemplate: Test bad tx count")
|
2017-04-11 22:42:12 +02:00
|
|
|
# The tx count is immediately after the block header
|
|
|
|
bad_block_sn = bytearray(block.serialize())
|
2019-01-23 16:44:13 +01:00
|
|
|
assert_equal(bad_block_sn[BLOCK_HEADER_SIZE], 1)
|
|
|
|
bad_block_sn[BLOCK_HEADER_SIZE] += 1
|
2019-02-18 16:35:48 +01:00
|
|
|
assert_raises_rpc_error(-22, "Block decode failed", node.getblocktemplate, {'data': bad_block_sn.hex(), 'mode': 'proposal', 'rules': ['segwit']})
|
2014-11-17 19:47:40 +01:00
|
|
|
|
2017-04-11 19:10:20 +02:00
|
|
|
self.log.info("getblocktemplate: Test bad bits")
|
2017-04-11 22:42:12 +02:00
|
|
|
bad_block = copy.deepcopy(block)
|
|
|
|
bad_block.nBits = 469762303 # impossible in the real world
|
|
|
|
assert_template(node, bad_block, 'bad-diffbits')
|
2014-11-17 19:47:40 +01:00
|
|
|
|
2017-04-11 19:10:20 +02:00
|
|
|
self.log.info("getblocktemplate: Test bad merkle root")
|
2017-04-11 22:42:12 +02:00
|
|
|
bad_block = copy.deepcopy(block)
|
|
|
|
bad_block.hashMerkleRoot += 1
|
|
|
|
assert_template(node, bad_block, 'bad-txnmrklroot', False)
|
2018-08-15 19:54:36 +02:00
|
|
|
assert_submitblock(bad_block, 'bad-txnmrklroot', 'bad-txnmrklroot')
|
2014-11-17 19:47:40 +01:00
|
|
|
|
2017-04-11 19:10:20 +02:00
|
|
|
self.log.info("getblocktemplate: Test bad timestamps")
|
2017-04-11 22:42:12 +02:00
|
|
|
bad_block = copy.deepcopy(block)
|
|
|
|
bad_block.nTime = 2 ** 31 - 1
|
|
|
|
assert_template(node, bad_block, 'time-too-new')
|
2018-08-15 19:54:36 +02:00
|
|
|
assert_submitblock(bad_block, 'time-too-new', 'time-too-new')
|
2017-04-11 22:42:12 +02:00
|
|
|
bad_block.nTime = 0
|
|
|
|
assert_template(node, bad_block, 'time-too-old')
|
2018-08-15 19:54:36 +02:00
|
|
|
assert_submitblock(bad_block, 'time-too-old', 'time-too-old')
|
2014-11-17 19:47:40 +01:00
|
|
|
|
2017-04-11 19:10:20 +02:00
|
|
|
self.log.info("getblocktemplate: Test not best block")
|
2017-04-11 22:42:12 +02:00
|
|
|
bad_block = copy.deepcopy(block)
|
|
|
|
bad_block.hashPrevBlock = 123
|
|
|
|
assert_template(node, bad_block, 'inconclusive-not-best-prevblk')
|
2018-08-15 19:54:36 +02:00
|
|
|
assert_submitblock(bad_block, 'prev-blk-not-found', 'prev-blk-not-found')
|
2014-11-17 19:47:40 +01:00
|
|
|
|
2018-06-05 02:27:08 +02:00
|
|
|
self.log.info('submitheader tests')
|
2019-01-23 16:44:13 +01:00
|
|
|
assert_raises_rpc_error(-22, 'Block header decode failed', lambda: node.submitheader(hexdata='xx' * BLOCK_HEADER_SIZE))
|
|
|
|
assert_raises_rpc_error(-22, 'Block header decode failed', lambda: node.submitheader(hexdata='ff' * (BLOCK_HEADER_SIZE-2)))
|
2019-02-18 16:35:48 +01:00
|
|
|
assert_raises_rpc_error(-25, 'Must submit previous header', lambda: node.submitheader(hexdata=super(CBlock, bad_block).serialize().hex()))
|
2018-06-05 02:27:08 +02:00
|
|
|
|
2018-08-15 19:54:36 +02:00
|
|
|
block.nTime += 1
|
2018-06-05 02:27:08 +02:00
|
|
|
block.solve()
|
|
|
|
|
|
|
|
def chain_tip(b_hash, *, status='headers-only', branchlen=1):
|
|
|
|
return {'hash': b_hash, 'height': 202, 'branchlen': branchlen, 'status': status}
|
|
|
|
|
|
|
|
assert chain_tip(block.hash) not in node.getchaintips()
|
2019-02-18 16:35:48 +01:00
|
|
|
node.submitheader(hexdata=block.serialize().hex())
|
2018-06-05 02:27:08 +02:00
|
|
|
assert chain_tip(block.hash) in node.getchaintips()
|
2019-02-18 16:35:48 +01:00
|
|
|
node.submitheader(hexdata=CBlockHeader(block).serialize().hex()) # Noop
|
2018-06-05 02:27:08 +02:00
|
|
|
assert chain_tip(block.hash) in node.getchaintips()
|
|
|
|
|
|
|
|
bad_block_root = copy.deepcopy(block)
|
|
|
|
bad_block_root.hashMerkleRoot += 2
|
|
|
|
bad_block_root.solve()
|
|
|
|
assert chain_tip(bad_block_root.hash) not in node.getchaintips()
|
2019-02-18 16:35:48 +01:00
|
|
|
node.submitheader(hexdata=CBlockHeader(bad_block_root).serialize().hex())
|
2018-06-05 02:27:08 +02:00
|
|
|
assert chain_tip(bad_block_root.hash) in node.getchaintips()
|
|
|
|
# Should still reject invalid blocks, even if we have the header:
|
2019-02-18 16:35:48 +01:00
|
|
|
assert_equal(node.submitblock(hexdata=bad_block_root.serialize().hex()), 'bad-txnmrklroot')
|
|
|
|
assert_equal(node.submitblock(hexdata=bad_block_root.serialize().hex()), 'bad-txnmrklroot')
|
2018-06-05 02:27:08 +02:00
|
|
|
assert chain_tip(bad_block_root.hash) in node.getchaintips()
|
|
|
|
# We know the header for this invalid block, so should just return early without error:
|
2019-02-18 16:35:48 +01:00
|
|
|
node.submitheader(hexdata=CBlockHeader(bad_block_root).serialize().hex())
|
2018-06-05 02:27:08 +02:00
|
|
|
assert chain_tip(bad_block_root.hash) in node.getchaintips()
|
|
|
|
|
|
|
|
bad_block_lock = copy.deepcopy(block)
|
|
|
|
bad_block_lock.vtx[0].nLockTime = 2**32 - 1
|
|
|
|
bad_block_lock.vtx[0].rehash()
|
|
|
|
bad_block_lock.hashMerkleRoot = bad_block_lock.calc_merkle_root()
|
|
|
|
bad_block_lock.solve()
|
2019-02-18 16:35:48 +01:00
|
|
|
assert_equal(node.submitblock(hexdata=bad_block_lock.serialize().hex()), 'bad-txns-nonfinal')
|
|
|
|
assert_equal(node.submitblock(hexdata=bad_block_lock.serialize().hex()), 'duplicate-invalid')
|
2018-06-05 02:27:08 +02:00
|
|
|
# Build a "good" block on top of the submitted bad block
|
|
|
|
bad_block2 = copy.deepcopy(block)
|
|
|
|
bad_block2.hashPrevBlock = bad_block_lock.sha256
|
|
|
|
bad_block2.solve()
|
2019-02-18 16:35:48 +01:00
|
|
|
assert_raises_rpc_error(-25, 'bad-prevblk', lambda: node.submitheader(hexdata=CBlockHeader(bad_block2).serialize().hex()))
|
2018-06-05 02:27:08 +02:00
|
|
|
|
|
|
|
# Should reject invalid header right away
|
|
|
|
bad_block_time = copy.deepcopy(block)
|
|
|
|
bad_block_time.nTime = 1
|
|
|
|
bad_block_time.solve()
|
2019-02-18 16:35:48 +01:00
|
|
|
assert_raises_rpc_error(-25, 'time-too-old', lambda: node.submitheader(hexdata=CBlockHeader(bad_block_time).serialize().hex()))
|
2018-06-05 02:27:08 +02:00
|
|
|
|
|
|
|
# Should ask for the block from a p2p node, if they announce the header as well:
|
|
|
|
node.add_p2p_connection(P2PDataStore())
|
|
|
|
node.p2p.wait_for_getheaders(timeout=5) # Drop the first getheaders
|
2018-08-29 18:42:46 +02:00
|
|
|
node.p2p.send_blocks_and_test(blocks=[block], node=node)
|
2018-06-05 02:27:08 +02:00
|
|
|
# Must be active now:
|
|
|
|
assert chain_tip(block.hash, status='active', branchlen=0) in node.getchaintips()
|
|
|
|
|
|
|
|
# Building a few blocks should give the same results
|
2018-09-15 11:32:12 +02:00
|
|
|
node.generatetoaddress(10, node.get_deterministic_priv_key().address)
|
2019-02-18 16:35:48 +01:00
|
|
|
assert_raises_rpc_error(-25, 'time-too-old', lambda: node.submitheader(hexdata=CBlockHeader(bad_block_time).serialize().hex()))
|
|
|
|
assert_raises_rpc_error(-25, 'bad-prevblk', lambda: node.submitheader(hexdata=CBlockHeader(bad_block2).serialize().hex()))
|
|
|
|
node.submitheader(hexdata=CBlockHeader(block).serialize().hex())
|
|
|
|
node.submitheader(hexdata=CBlockHeader(bad_block_root).serialize().hex())
|
|
|
|
assert_equal(node.submitblock(hexdata=block.serialize().hex()), 'duplicate') # valid
|
2018-06-05 02:27:08 +02:00
|
|
|
|
|
|
|
|
2014-11-17 19:47:40 +01:00
|
|
|
if __name__ == '__main__':
|
2017-04-11 22:47:10 +02:00
|
|
|
MiningTest().main()
|