def run_test(self):
    node = self.nodes[0]
    self.log.info('getmininginfo')
    mining_info = node.getmininginfo()
    assert_equal(mining_info['blocks'], 200)
    assert_equal(mining_info['chain'], 'regtest')
    assert_equal(mining_info['currentblocktx'], 0)
    assert_equal(mining_info['currentblockweight'], 0)
    assert_equal(mining_info['difficulty'], Decimal('4.656542373906925E-10'))
    assert_equal(mining_info['networkhashps'], Decimal('0.003333333333333334'))
    assert_equal(mining_info['pooledtx'], 0)
    node.generate(1)
    tmpl = node.getblocktemplate()
    self.log.info('getblocktemplate: Test capability advertised')
    assert ('proposal' in tmpl['capabilities'])
    assert ('coinbasetxn' not in tmpl)
    coinbase_tx = create_coinbase(height=(int(tmpl['height']) + 1))
    coinbase_tx.vin[0].nSequence = ((2 ** 32) - 2)
    coinbase_tx.rehash()
    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]
    self.log.info('getblocktemplate: Test valid block')
    assert_template(node, block, None)
    self.log.info('submitblock: Test block decode failure')
    assert_raises_rpc_error((- 22), 'Block decode failed', node.submitblock, b2x(block.serialize()[:(- 15)]))
    self.log.info('getblocktemplate: Test bad input hash for coinbase transaction')
    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')
    self.log.info('submitblock: Test invalid coinbase transaction')
    assert_raises_rpc_error((- 22), 'Block does not start with a coinbase', node.submitblock, b2x(bad_block.serialize()))
    self.log.info('getblocktemplate: Test truncated final transaction')
    assert_raises_rpc_error((- 22), 'Block decode failed', node.getblocktemplate, {'data': b2x(block.serialize()[:(- 1)]), 'mode': 'proposal'})
    self.log.info('getblocktemplate: Test duplicate transaction')
    bad_block = copy.deepcopy(block)
    bad_block.vtx.append(bad_block.vtx[0])
    assert_template(node, bad_block, 'bad-txns-duplicate')
    self.log.info('getblocktemplate: Test invalid transaction')
    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')
    self.log.info('getblocktemplate: Test nonfinal transaction')
    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')
    self.log.info('getblocktemplate: Test bad tx count')
    TX_COUNT_OFFSET = 80
    bad_block_sn = bytearray(block.serialize())
    assert_equal(bad_block_sn[TX_COUNT_OFFSET], 1)
    bad_block_sn[TX_COUNT_OFFSET] += 1
    assert_raises_rpc_error((- 22), 'Block decode failed', node.getblocktemplate, {'data': b2x(bad_block_sn), 'mode': 'proposal'})
    self.log.info('getblocktemplate: Test bad bits')
    bad_block = copy.deepcopy(block)
    bad_block.nBits = 469762303
    assert_template(node, bad_block, 'bad-diffbits')
    self.log.info('getblocktemplate: Test bad merkle root')
    bad_block = copy.deepcopy(block)
    bad_block.hashMerkleRoot += 1
    assert_template(node, bad_block, 'bad-txnmrklroot', False)
    self.log.info('getblocktemplate: Test bad timestamps')
    bad_block = copy.deepcopy(block)
    bad_block.nTime = ((2 ** 31) - 1)
    assert_template(node, bad_block, 'time-too-new')
    bad_block.nTime = 0
    assert_template(node, bad_block, 'time-too-old')
    self.log.info('getblocktemplate: Test not best block')
    bad_block = copy.deepcopy(block)
    bad_block.hashPrevBlock = 123
    assert_template(node, bad_block, 'inconclusive-not-best-prevblk')
def run_test(self):
    node = self.nodes[0]
    self.log.info('getmininginfo')
    mining_info = node.getmininginfo()
    assert_equal(mining_info['blocks'], 200)
    assert_equal(mining_info['chain'], 'regtest')
    assert_equal(mining_info['currentblocktx'], 0)
    assert_equal(mining_info['currentblockweight'], 0)
    assert_equal(mining_info['difficulty'], Decimal('4.656542373906925E-10'))
    assert_equal(mining_info['networkhashps'], Decimal('0.003333333333333334'))
    assert_equal(mining_info['pooledtx'], 0)
    node.generate(1)
    tmpl = node.getblocktemplate()
    self.log.info('getblocktemplate: Test capability advertised')
    assert ('proposal' in tmpl['capabilities'])
    assert ('coinbasetxn' not in tmpl)
    coinbase_tx = create_coinbase(height=(int(tmpl['height']) + 1))
    coinbase_tx.vin[0].nSequence = ((2 ** 32) - 2)
    coinbase_tx.rehash()
    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]
    self.log.info('getblocktemplate: Test valid block')
    assert_template(node, block, None)
    self.log.info('submitblock: Test block decode failure')
    assert_raises_rpc_error((- 22), 'Block decode failed', node.submitblock, b2x(block.serialize()[:(- 15)]))
    self.log.info('getblocktemplate: Test bad input hash for coinbase transaction')
    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')
    self.log.info('submitblock: Test invalid coinbase transaction')
    assert_raises_rpc_error((- 22), 'Block does not start with a coinbase', node.submitblock, b2x(bad_block.serialize()))
    self.log.info('getblocktemplate: Test truncated final transaction')
    assert_raises_rpc_error((- 22), 'Block decode failed', node.getblocktemplate, {'data': b2x(block.serialize()[:(- 1)]), 'mode': 'proposal'})
    self.log.info('getblocktemplate: Test duplicate transaction')
    bad_block = copy.deepcopy(block)
    bad_block.vtx.append(bad_block.vtx[0])
    assert_template(node, bad_block, 'bad-txns-duplicate')
    self.log.info('getblocktemplate: Test invalid transaction')
    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')
    self.log.info('getblocktemplate: Test nonfinal transaction')
    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')
    self.log.info('getblocktemplate: Test bad tx count')
    TX_COUNT_OFFSET = 80
    bad_block_sn = bytearray(block.serialize())
    assert_equal(bad_block_sn[TX_COUNT_OFFSET], 1)
    bad_block_sn[TX_COUNT_OFFSET] += 1
    assert_raises_rpc_error((- 22), 'Block decode failed', node.getblocktemplate, {'data': b2x(bad_block_sn), 'mode': 'proposal'})
    self.log.info('getblocktemplate: Test bad bits')
    bad_block = copy.deepcopy(block)
    bad_block.nBits = 469762303
    assert_template(node, bad_block, 'bad-diffbits')
    self.log.info('getblocktemplate: Test bad merkle root')
    bad_block = copy.deepcopy(block)
    bad_block.hashMerkleRoot += 1
    assert_template(node, bad_block, 'bad-txnmrklroot', False)
    self.log.info('getblocktemplate: Test bad timestamps')
    bad_block = copy.deepcopy(block)
    bad_block.nTime = ((2 ** 31) - 1)
    assert_template(node, bad_block, 'time-too-new')
    bad_block.nTime = 0
    assert_template(node, bad_block, 'time-too-old')
    self.log.info('getblocktemplate: Test not best block')
    bad_block = copy.deepcopy(block)
    bad_block.hashPrevBlock = 123
    assert_template(node, bad_block, 'inconclusive-not-best-prevblk')