示例#1
0
def random_header(prev_hash=None, height=-1, good_bits=None):
    good_bits = good_bits or some_good_bits
    raw_header = bytearray(urandom(80))
    raw_header[72:76] = pack_le_uint32(random.choice(good_bits))
    if prev_hash:
        raw_header[4:36] = prev_hash
    return Bitcoin.deserialized_header(bytes(raw_header), height)
示例#2
0
 def test_from_checkpoint(self):
     header = Bitcoin.deserialized_header(bsv_checkpoint.raw_header, bsv_checkpoint.height)
     chain = Chain.from_checkpoint(Bitcoin, bsv_checkpoint)
     assert chain.height == bsv_checkpoint.height
     assert chain.tip == header
     assert chain.work == bsv_checkpoint.prev_work + header.work() == 0xd54c9a84f54e93d3d87015
     assert chain.parent is None
     assert chain._header_indices[-1] == bsv_checkpoint.height
示例#3
0
def test_mainnet_2016_headers(tmpdir):
    # Mainnet headers 0, 2015, 2016, 4031, 4032, ... 4249808
    headers = setup_headers(tmpdir, 'mainnet-headers-2016')

    chain = headers.chains()[0]
    for height in range(0, len(headers), 2016):
        header = headers.header_at_height(chain, height)
        assert headers.required_bits(chain, height, None) == header.bits
        assert headers.required_bits(chain, height + 1, None) == header.bits

    assert header.difficulty() == 860_221_984_436.2223

    bounded_bits = 403011440
    # Test // 4 is lower bound for the last one
    raw_header = bytearray(headers.raw_header_at_height(chain, height - 2016))
    timestamp = Bitcoin.header_timestamp(raw_header)
    # Add 8 weeks and a 14 seconds; the minimum to trigger it
    raw_header[68:72] = pack_le_uint32(timestamp + 4 * 2016 * 600 + 14)
    headers.set_one(height - 1, raw_header)
    assert headers.required_bits(chain, height, ) == bounded_bits