Repository: bitcoin/bitcoin
File: test/functional/test_framework/bdb.py (Python)

Code before:
    page_info['entries'] = []

    if pg_type == BTREE_INTERNAL:
        # Skip internal pages. These are the internal nodes of the btree and don't contain anything relevant to us
        return None

    assert pg_type == BTREE_LEAF, 'A non-btree leaf page has been encountered while dumping leaves'

    for i in range(0, entries):
        offset = page_info['entry_offsets'][i]
        entry = {'offset': offset}
        page_data_header = data[offset:offset + 3]
        e_len, pg_type = struct.unpack('HB', page_data_header)
        entry['len'] = e_len
        entry['pg_type'] = pg_type
        entry['data'] = data[offset + 3:offset + 3 + e_len]
        page_info['entries'].append(entry)

    return page_info

# Deserializes a btree metadata page into a dict.
# Does a simple sanity check on the magic value, type, and version
def dump_meta_page(page):
    # metadata page
    # general metadata
    metadata = {}
    meta_page = page[0:72]
    _, pgno, magic, version, pagesize, encrypt_alg, pg_type, metaflags, _, free, last_pgno, nparts, key_count, record_count, flags, uid = struct.unpack('QIIIIBBBBIIIIII20s', meta_page)
    metadata['pgno'] = pgno
    metadata['magic'] = magic
    metadata['version'] = version
    metadata['pagesize'] = pagesize
    metadata['encrypt_alg'] = encrypt_alg
    metadata['pg_type'] = pg_type
    metadata['metaflags'] = metaflags
    metadata['free'] = free
    metadata['last_pgno'] = last_pgno
    metadata['nparts'] = nparts
    metadata['key_count'] = key_count
    metadata['record_count'] = record_count
    metadata['flags'] = flags
    metadata['uid'] = uid.hex().encode()

    assert magic == BTREE_MAGIC, 'bdb magic does not match bdb btree magic'
    assert pg_type == BTREE_META, 'Metadata page is not a btree metadata page'
    assert version == DB_VERSION, 'Database too new'

    # btree metadata
    btree_meta_page = page[72:512]
    _, minkey, re_len, re_pad, root, _, crypto_magic, _, iv, chksum = struct.unpack('IIIII368sI12s16s20s', btree_meta_page)
    metadata['minkey'] = minkey

Review comment (suggestion):
There's a separate effort from @maflcko regarding removing the usage of `struct.pack` from all places in test. Though here it's `struct.unpack` but I guess the reasoning for the removal of `struct.pack` is still applicable.
https://github.com/bitcoin/bitcoin/pull/29401

Code after:
def dump_page(data):
    page_info = {}
    page_header = data[0:26]
    _, pgno, prev_pgno, next_pgno, entries, hf_offset, level, pg_type = struct.unpack('QIIIHHBB', page_header)
    page_info['pgno'] = pgno
    page_info['prev_pgno'] = prev_pgno
    page_info['next_pgno'] = next_pgno
    page_info['hf_offset'] = hf_offset
    page_info['level'] = level
    page_info['pg_type'] = pg_type
    page_info['entry_offsets'] = struct.unpack('{}H'.format(entries), data[26:26 + entries * 2])
    page_info['entries'] = []

    assert pg_type in (BTREE_INTERNAL, BTREE_LEAF, OVERFLOW_DATA)

    if pg_type == OVERFLOW_DATA:
        assert entries == 1
        page_info['entries'].append({'data': data[26:26 + hf_offset]})
        return page_info

    for i in range(0, entries):
        entry = {}
        offset = page_info['entry_offsets'][i]
        record_header = data[offset:offset + 3]
        offset += 3
        e_len, record_type = struct.unpack('HB', record_header)

        if pg_type == BTREE_INTERNAL:
            assert record_type == RECORD_KEYDATA
            internal_record_data = data[offset:offset + 9]
            _, page_num, _ = struct.unpack('=BII', internal_record_data)
            entry['page_num'] = page_num
        elif pg_type == BTREE_LEAF:
            assert record_type in (RECORD_KEYDATA, RECORD_OVERFLOW_DATA)
            entry['record_type'] = record_type
            if record_type == RECORD_KEYDATA:
                entry['data'] = data[offset:offset + e_len]
            elif record_type == RECORD_OVERFLOW_DATA:
                overflow_record_data = data[offset:offset + 9]
                _, page_num, _ = struct.unpack('=BII', overflow_record_data)
                entry['page_num'] = page_num

        page_info['entries'].append(entry)

    return page_info

# Deserializes a btree metadata page into a dict.
# Does a simple sanity check on the magic value, type, and version
def dump_meta_page(page):
    # metadata page
    # general metadata