def test_Bitcoin(raw_header, header_hash, version, prev_hash, merkle_root, timestamp, bits, nonce): header_hash = hex_str_to_hash(header_hash) prev_hash = hex_str_to_hash(prev_hash) merkle_root = hex_str_to_hash(merkle_root) assert Bitcoin.header_hash(raw_header) == header_hash assert Bitcoin.header_prev_hash(raw_header) == prev_hash assert Bitcoin.header_work(raw_header) == bits_to_work(bits) assert Bitcoin.header_timestamp(raw_header) == timestamp header = Bitcoin.deserialized_header(raw_header, 0) assert header.version == version assert header.prev_hash == prev_hash assert header.merkle_root == merkle_root assert header.timestamp == timestamp assert header.bits == bits assert header.nonce == nonce assert header.raw == raw_header assert header.hash == header_hash assert header.height == 0 assert header.work() == Bitcoin.header_work(raw_header) assert header.target() == bits_to_target(bits) assert header.hash_value() == hash_to_value(header_hash) assert header.hex_str() == hash_to_hex_str(header_hash) assert 'height=0' in str(header)
def calculate_regtest_checkpoint(height): logging.getLogger("urllib3").setLevel(logging.WARNING) # suppress excessive logging try: hash_ = get_blockhash_by_height(height) checkpoint_raw_header = get_raw_block_header_by_hash(hash_) # RegTest nBits overflow the max allowable value so cap the nBits at max prev_work = sum((bits_to_work(MAX_BITS) for i in range(Net.MIN_CHECKPOINT_HEIGHT - 1))) checkpoint = CheckPoint(raw_header=checkpoint_raw_header, height=Net.MIN_CHECKPOINT_HEIGHT, prev_work=prev_work) verification_block_merkle_root = checkpoint_raw_header[36:68] return checkpoint, verification_block_merkle_root except requests.HTTPError as e: if e.response.json().get('error').get('code') == BLOCK_HEIGHT_OUT_OF_RANGE_ERROR: regtest_generate_nblocks(200, address=Net.REGTEST_P2PKH_ADDRESS) # retry with more blocks return calculate_regtest_checkpoint(height)