def writeIncorrectGenesisBlockToFile(datadir, genesis):
    with open(os.path.join(datadir, "genesis.dat"), 'w', encoding='utf8') as f:
        f.write(bytes_to_hex_str(genesis.serialize()))
 def calcsnapshothash(inputs, outputs, *prev_hash):
     sm = bytes_to_hex_str(ser_uint256(0))
     cw = bytes_to_hex_str(ser_uint256(2))
     return self.nodes[0].calcsnapshothash(ser_utxos(inputs),
                                           ser_utxos(outputs), sm, cw,
                                           *prev_hash)
Exemple #3
0
    def run_test(self):
        node = self.nodes[0]
        node.add_p2p_connection(P2PDataStore())
        node.p2p.wait_for_getheaders(timeout=5)

        self.tip = node.getbestblockhash()
        best_block = node.getblock(self.tip)
        block_time = best_block["time"] + 1

        self.log.info("Incorrect xfield with xfieldType = 0")
        block = create_block(int(self.tip, 16), create_coinbase(1), block_time)
        block.xfieldType = 0
        block.xfield = b'1'
        self.createblockproof(block, self.signblockprivkey)

        node.p2p.send_blocks_and_test([block],
                                      node,
                                      success=False,
                                      request_block=False)

        self.log.info("Incorrect xfield with xfieldType = 1")
        block = create_block(int(self.tip, 16), create_coinbase(1), block_time)
        block.xfieldType = 1
        block.xfield = b'1'
        self.createblockproof(block, self.signblockprivkey)

        node.p2p.send_blocks_and_test([block],
                                      node,
                                      success=False,
                                      request_block=False,
                                      reject_code=16,
                                      reject_reason="bad-xfieldType-xfield")

        blockchaininfo = node.getblockchaininfo()
        assert_equal(
            blockchaininfo['warnings'],
            "This is a pre-release test build - use at your own risk - do not use for mining or merchant applications"
        )

        self.log.info("Incorrect xfield with xfieldType = 2")
        block = create_block(int(self.tip, 16), create_coinbase(1), block_time)
        block.xfieldType = 2
        block.xfield = b'1'
        self.createblockproof(block, self.signblockprivkey)

        assert_raises_rpc_error(
            -22, "", node.submitblock,
            bytes_to_hex_str(self.serializeblock_err(block)))

        blockchaininfo = node.getblockchaininfo()
        assert_equal(
            blockchaininfo['warnings'],
            "This is a pre-release test build - use at your own risk - do not use for mining or merchant applications"
        )

        self.log.info("Valid xfield with xfieldType = 2")
        node.p2p.send_blocks_and_test([block], node, True, True)
        assert_equal(block.hash, node.getbestblockhash())

        blockchaininfo = node.getblockchaininfo()
        assert_equal(
            blockchaininfo['warnings'],
            "Warning: Unknown xfieldType [%2x] was accepted in block [%s]" %
            (block.xfieldType, block.hash))

        self.log.info("Valid xfield with xfieldType = 0xFF")
        self.tip = node.getbestblockhash()
        block_time += 1
        block = create_block(int(self.tip, 16), create_coinbase(2), block_time)
        block.xfieldType = 0xFF
        block.xfield = b'1'
        self.createblockproof(block, self.signblockprivkey)

        node.p2p.send_blocks_and_test([block], node, True, True)
        assert_equal(block.hash, node.getbestblockhash())

        self.log.info(
            "xfieldType = 3 in invalid block - insufficient tx input")
        self.tip = node.getbestblockhash()
        block_time += 1
        block = create_block(int(self.tip, 16), create_coinbase(3), block_time)
        block.xfieldType = 3
        block.xfield = b''
        spendHash = node.getblock(self.tip)['tx'][0]
        block.vtx += [
            create_transaction(node,
                               spendHash,
                               node.getnewaddress(),
                               amount=100.0)
        ]
        block.hashMerkleRoot = block.calc_merkle_root()
        block.hashImMerkleRoot = block.calc_immutable_merkle_root()
        self.createblockproof(block, self.signblockprivkey)
        oldblockhash = block.hash

        node.p2p.send_blocks_and_test([block],
                                      node,
                                      success=False,
                                      request_block=True,
                                      reject_code=16,
                                      reject_reason="bad-txns-in-belowout")
        assert_equal(self.tip, node.getbestblockhash())

        blockchaininfo = node.getblockchaininfo()
        assert_equal(
            blockchaininfo['warnings'],
            "Warning: Unknown xfieldType [%2x] was accepted in block [%s]" %
            (block.xfieldType, block.hash))

        self.log.info("xfieldType = 4 in invalid block - height in coinbase")
        block_time += 1
        block = create_block(int(self.tip, 16), create_coinbase(1), block_time)
        block.xfieldType = 4
        block.xfield = b''
        self.createblockproof(block, self.signblockprivkey)

        node.p2p.send_blocks_and_test([block],
                                      node,
                                      success=False,
                                      request_block=True,
                                      reject_code=16,
                                      reject_reason="bad-cb-invalid")
        assert_equal(self.tip, node.getbestblockhash())

        blockchaininfo = node.getblockchaininfo()
        assert_equal(
            blockchaininfo['warnings'],
            "Warning: Unknown xfieldType [%2x] was accepted in block [%s]" %
            (3, oldblockhash))

        self.log.info("xfieldType = 5 in invalid block - no coinbase")
        block_time += 1
        block = create_block(int(self.tip, 16), create_coinbase(4), block_time)
        block.xfieldType = 5
        block.xfield = b''
        block.vtx = []
        self.createblockproof(block, self.signblockprivkey)

        node.p2p.send_blocks_and_test([block],
                                      node,
                                      success=False,
                                      request_block=True,
                                      reject_code=16,
                                      reject_reason="bad-cb-missing")
        assert_equal(self.tip, node.getbestblockhash())

        blockchaininfo = node.getblockchaininfo()
        assert_equal(
            blockchaininfo['warnings'],
            "Warning: Unknown xfieldType [%2x] was accepted in block [%s]" %
            (3, oldblockhash))
 def ser_utxos(utxos):
     return bytes_to_hex_str(ser_vector(utxos))