def setup_headers(tmpdir, headers_file): with open(os.path.join(data_dir, headers_file), 'rb') as f: raw_headers = f.read() last_height, = unpack_le_uint32(raw_headers[-84:-80]) last_raw = raw_headers[-80:] checkpoint = CheckPoint(last_raw, last_height, 0) headers = create_headers(tmpdir, checkpoint) for offset in range(0, len(raw_headers), 84): height, = unpack_le_uint32(raw_headers[offset:offset + 4]) raw_header = raw_headers[offset + 4:offset + 84] headers.set_one(height, raw_header) return headers
def setup_gzipped_headers(tmpdir, headers_file, network): import gzip with gzip.open(os.path.join(data_dir, headers_file), 'rb') as f: first_height = unpack_le_uint32(f.read(4))[0] header_count = unpack_le_uint32(f.read(4))[0] raw_header = bytearray(80) raw_header[0] = 1 checkpoint = CheckPoint(raw_header, first_height + header_count - 1, 0) headers = create_headers(tmpdir, checkpoint, network=network) for height in range(first_height, header_count): headers.set_one(height, f.read(80)) return headers
def setup_compressed_headers(tmpdir, headers_file, ts_offset, network): with open(os.path.join(data_dir, headers_file), 'rb') as f: raw_data = f.read() all_times = array.array('I') all_bits = array.array('I') read = io.BytesIO(raw_data).read first_height, = unpack_le_uint32(read(4)) header_count, = unpack_le_uint32(read(4)) # Timestamps first_time, = unpack_le_uint32(read(4)) all_times.append(first_time) for n in range(1, header_count): diff, = unpack_le_uint16(read(2)) all_times.append(all_times[-1] + diff - ts_offset) # Bits while True: raw = read(4) if not raw: break bits, = unpack_le_uint32(raw) if bits < 2016 * 2: count = bits bits, = unpack_le_uint32(read(4)) all_bits.extend(array.array('I', [bits]) * count) else: all_bits.append(bits) assert len(all_times) == header_count assert len(all_bits) == header_count raw_header = bytearray(80) raw_header[0] = 1 checkpoint = CheckPoint(raw_header, first_height + header_count - 1, 0) headers = create_headers(tmpdir, checkpoint, network=network) for height, (bits, timestamp) in enumerate(zip(all_bits, all_times), start=first_height): raw_header[68:72] = pack_le_uint32(timestamp) raw_header[72:76] = pack_le_uint32(bits) headers.set_one(height, raw_header) return headers