jam.state.merkle.node module

class jam.state.merkle.node.Node(hash_function: Hash | None = None)[source]

Bases: object

Node encoding for Merkle trees as defined in D.2.1

Nodes are fixed in size at 512 bit (64 bytes). Each node is either a branch or a leaf. The first bit discriminates between these two types.

NODE_SIZE = 64
ZERO_HASH = 0x0000000000000000000000000000000000000000000000000000000000000000
__init__(hash_function: Hash | None = None)[source]

Initialize node encoder with optional hash function

encode_branch(left_hash: ByteArray32, right_hash: ByteArray32) ByteArray64[source]

Encode a branch node (B function in D.3)

For a branch, we: 1. Clear the first bit of left_hash (AND with 0xfe) 2. Concatenate with full right_hash

encode_leaf(key: ByteArray32, value: Bytes) ByteArray64[source]

Encode a leaf node (L function in D.4)

For a leaf, the second bit discriminates between embedded-value leaves and regular leaves. For embedded values (|v| ≤ 32):

  • 6 bits store the embedded value size

  • First 31 bytes store key

  • Last 32 bytes store the value (zero-padded)

For regular leaves:
  • 6 bits are zeroed

  • First 31 bytes store key

  • Last 32 bytes store hash of value