def get_block_header(self, node, target_block_hash): target_block = node.getblock(target_block_hash, 2) block = CBlockHeader(is_part=True) block.nTime = target_block['time'] block.hashPrevBlock = int(target_block['previousblockhash'], 16) block.nVersion = target_block['version'] block.nBits = int(target_block['bits'], 16) block.hashMerkleRoot = int(target_block['merkleroot'], 16) block.hashWitnessMerkleRoot = int(target_block['witnessmerkleroot'], 16) block.calc_sha256() return block
def create_block_header(self, node, hashPrevBlock, hashMerkleRoot, target_block_hash): target_block = node.getblock(target_block_hash, 2) block = CBlockHeader(is_part=True) block.nTime = target_block['time'] block.hashPrevBlock = hashPrevBlock block.nVersion = target_block['version'] block.nBits = int(target_block['bits'], 16) # Will break after a difficulty adjustment... block.hashMerkleRoot = hashMerkleRoot block.hashWitnessMerkleRoot = 0 #block.vchBlockSig = b"x" * 1024 #block.hashMerkleRoot = block.calc_merkle_root() block.calc_sha256() return block
def update_headers_and_blocks_from(self, node): self.headers = [] prev_block_hash = self.snapshot_header.block_hash for i in range(1, node.getblockcount() + 1): blockhash = node.getblockhash(i) header = CBlockHeader() FromHex(header, node.getblockheader(blockhash, False)) header.calc_sha256() self.headers.append(header) # keep only parent blocks if prev_block_hash == header.hashPrevBlock: block = node.getblock(blockhash, False) self.parent_blocks[header.sha256] = FromHex(CBlock(), block) self.parent_blocks[header.sha256].calc_sha256() prev_block_hash = header.sha256
def _test_getblockheader(self): node = self.nodes[0] assert_raises_rpc_error( -8, "hash_or_height must be of length 64 (not 8, for 'nonsense')", node.getblockheader, "nonsense") assert_raises_rpc_error( -8, "hash_or_height must be hexadecimal string (not 'ZZZ7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844')", node.getblockheader, "ZZZ7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844") assert_raises_rpc_error( -5, "Block not found", node.getblockheader, "0cf7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844") assert_raises_rpc_error( -8, "Target block height 201 after current tip 200", node.getblockheader, 201) assert_raises_rpc_error(-8, "Target block height -10 is negative", node.getblockheader, -10) besthash = node.getbestblockhash() secondbesthash = node.getblockhash(199) header = node.getblockheader(hash_or_height=besthash) assert_equal(header['hash'], besthash) assert_equal(header['height'], 200) assert_equal(header['confirmations'], 1) assert_equal(header['previousblockhash'], secondbesthash) assert_is_hex_string(header['chainwork']) assert_equal(header['nTx'], 1) assert_is_hash_string(header['hash']) assert_is_hash_string(header['previousblockhash']) assert_is_hash_string(header['merkleroot']) assert_is_hash_string(header['bits'], length=None) assert isinstance(header['time'], int) assert isinstance(header['mediantime'], int) assert isinstance(header['nonce'], int) assert isinstance(header['version'], int) assert isinstance(int(header['versionHex'], 16), int) assert isinstance(header['difficulty'], Decimal) header_by_height = node.getblockheader(hash_or_height=200) assert_equal(header, header_by_height) # Next, check that the old alias 'blockhash' still works # and is interchangeable with hash_or_height # First, make sure errors work as expected for unknown named params self.log.info( "Testing that getblockheader(blockhashhh=\"HEX\") produces the proper error" ) assert_raises_rpc_error(-8, "Unknown named parameter blockhashhh", node.getblockheader, blockhashhh=header['hash']) # Next, actually try the old legacy blockhash="xx" style arg self.log.info( "Testing that legacy getblockheader(blockhash=\"HEX\") still works ok" ) header_by_hash2 = node.getblockheader(blockhash=header['hash']) assert_equal(header, header_by_hash2) header_by_height2 = node.getblockheader(blockhash=200) assert_equal(header, header_by_height2) # check that we actually get a hex string back from getblockheader # if verbose is set to false. header_verbose_false = node.getblockheader(200, False) assert not isinstance(header_verbose_false, dict) assert isinstance(header_verbose_false, str) assert (c in string.hexdigits for c in header_verbose_false) assert_is_hex_string(header_verbose_false) # check that header_verbose_false is the same header we get via # getblockheader(hash_or_height=besthash) just in a different "form" h = CBlockHeader() h.deserialize(BytesIO(hex_str_to_bytes(header_verbose_false))) h.calc_sha256() assert_equal(header['version'], h.nVersion) assert_equal(header['time'], h.nTime) assert_equal(header['previousblockhash'], "{:064x}".format(h.hashPrevBlock)) assert_equal(header['merkleroot'], "{:064x}".format(h.hashMerkleRoot)) assert_equal(header['hash'], h.hash) # check that we get the same header by hash and by height in # the case verbose is set to False header_verbose_false_by_hash = node.getblockheader(besthash, False) assert_equal(header_verbose_false_by_hash, header_verbose_false)
def _test_getblockheader(self): node = self.nodes[0] assert_raises_rpc_error( -8, "hash_or_height must be of length 64 (not 8, for 'nonsense')", node.getblockheader, "nonsense") assert_raises_rpc_error( -8, "hash_or_height must be hexadecimal string (not 'ZZZ7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844')", node.getblockheader, "ZZZ7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844") assert_raises_rpc_error( -5, "Block not found", node.getblockheader, "0cf7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844") assert_raises_rpc_error( -8, "Target block height 201 after current tip 200", node.getblockheader, 201) assert_raises_rpc_error(-8, "Target block height -10 is negative", node.getblockheader, -10) besthash = node.getbestblockhash() secondbesthash = node.getblockhash(199) header = node.getblockheader(hash_or_height=besthash) assert_equal(header['hash'], besthash) assert_equal(header['height'], 200) assert_equal(header['confirmations'], 1) assert_equal(header['previousblockhash'], secondbesthash) assert_is_hex_string(header['chainwork']) assert_equal(header['nTx'], 1) assert_is_hash_string(header['hash']) assert_is_hash_string(header['previousblockhash']) assert_is_hash_string(header['merkleroot']) assert_is_hash_string(header['bits'], length=None) assert isinstance(header['time'], int) assert isinstance(header['mediantime'], int) assert isinstance(header['nonce'], int) assert isinstance(header['version'], int) assert isinstance(int(header['versionHex'], 16), int) assert isinstance(header['difficulty'], Decimal) header_by_height = node.getblockheader(hash_or_height=200) assert_equal(header, header_by_height) # check that we actually get a hex string back from getblockheader # if verbose is set to false. header_verbose_false = node.getblockheader(200, False) assert not isinstance(header_verbose_false, dict) assert isinstance(header_verbose_false, str) assert (c in string.hexdigits for c in header_verbose_false) assert_is_hex_string(header_verbose_false) # check that header_verbose_false is the same header we get via # getblockheader(hash_or_height=besthash) just in a different "form" h = CBlockHeader() h.deserialize(BytesIO(hex_str_to_bytes(header_verbose_false))) h.calc_sha256() assert_equal(header['version'], h.nVersion) assert_equal(header['time'], h.nTime) assert_equal(header['previousblockhash'], "{:064x}".format(h.hashPrevBlock)) assert_equal(header['merkleroot'], "{:064x}".format(h.hashMerkleRoot)) assert_equal(header['hash'], h.hash) # check that we get the same header by hash and by height in # the case verbose is set to False header_verbose_false_by_hash = node.getblockheader(besthash, False) assert_equal(header_verbose_false_by_hash, header_verbose_false)