hamt

package
v1.9.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2026 License: Apache-2.0, MIT Imports: 11 Imported by: 0

Documentation

Overview

Package hamt is Lantern's proof-recording HAMT walker.

The Filecoin state tree maps `Address -> Actor` via a HAMT (hash-array-mapped trie) of bit-width 5, using sha256 as the hash function. Actor sub-state (e.g. the Init actor's address-resolution table) uses HAMTs too. See https://github.com/ipld/specs/blob/master/data-structures/hashmap.md for the IPLD HashMap spec; Filecoin's variant fixes `bucketSize = 3`.

We delegate the actual node decoding and walking to github.com/filecoin-project/go-hamt-ipld/v3 (pure Go, no CGo). Our value-add is two things:

  1. A BlockGetter abstraction the caller controls (cache + Bitswap + HTTP).
  2. A path-recording wrapper that captures every node CID fetched during a Lookup. That CID list is the inclusion (or exclusion) proof and is what Lantern's "verified by us" guarantee is built on: a third party can re-run VerifyProof against the same root and prove the value was present in the canonical state at the time of lookup.

Strictly local: this package never talks to the network. Network adapters live in net/bitswap and net/hsync; they implement BlockGetter.

Index

Constants

View Source
const (
	FilBitWidth   = 5
	FilBucketSize = 3
)

Defaults for the canonical Filecoin state-tree HAMT.

Variables

View Source
var ErrNotFound = fmt.Errorf("key not found in HAMT")

ErrNotFound is returned by Lookup when the key is not in the HAMT.

Functions

func CborStoreFromMem

func CborStoreFromMem(m *MemBlockStore) *cbornode.BasicIpldStore

CborStoreFromMem returns a cbornode.IpldStore backed by an in-memory MemBlockStore. Tests use it.

func FilHash

func FilHash(k []byte) []byte

FilHash implements the go-hamt-ipld HashFunc using sha256, matching what Filecoin's state tree uses. Returns the full 32-byte digest.

func Lookup

func Lookup(ctx context.Context, root cid.Cid, key []byte, bg BlockGetter, opts *LookupOptions) ([]byte, []cid.Cid, error)

Lookup walks the HAMT rooted at `root`, fetching nodes via `bg` (which must serve canonical IPLD-DAG-CBOR encoded HAMT nodes for every CID it returns). It returns the raw value bytes for the leaf, the *path of node CIDs traversed* (the inclusion proof), and an error.

If the key isn't present, Lookup returns ErrNotFound and the proof path captured up to the negative-result node (the absence proof).

func LookupCBOR

func LookupCBOR(ctx context.Context, root cid.Cid, key []byte, bg BlockGetter, out cbg.CBORUnmarshaler, opts *LookupOptions) ([]cid.Cid, error)

LookupCBOR is a convenience wrapper around Lookup that unmarshals the leaf bytes into out via cbor-gen's UnmarshalCBOR interface.

func VerifyBlockCID

func VerifyBlockCID(c cid.Cid, raw []byte) error

VerifyBlockCID recomputes the CID hash over raw and returns nil iff it matches c. Exported because both state/hamt and state/amt callers want it.

func VerifyProof

func VerifyProof(ctx context.Context, root cid.Cid, key []byte, value []byte, proof []cid.Cid, blockGet BlockGetter, opts *LookupOptions) error

VerifyProof independently re-verifies an inclusion-proof: given a root CID, a key, an expected value, and a proof path (list of node CIDs in fetch order), it loads each node from blockGet, walks the HAMT, and confirms the key resolves to value. Returns nil on success.

Crucially, blockGet here is typically a *fresh* MemBlockStore seeded only with the proof-path bytes — that's how a third party can audit a claim "Lantern claimed addr X had balance Y at state root R" using nothing but the proof bytes Lantern published.

Types

type BlockGetter

type BlockGetter interface {
	Get(ctx context.Context, c cid.Cid) ([]byte, error)
}

BlockGetter is the minimal interface Lantern's state layer needs from a block source. It is satisfied by:

  • state/cache (local Badger blockstore)
  • net/bitswap (Bitswap client)
  • net/hsync (HTTP gateway client)
  • net/combined (the cache+bitswap+http fallback chain)
  • testdata fixtures (MemBlockStore in this package)

Get returns the raw IPLD block bytes for the given CID, or an error. The caller is responsible for verifying the CID against the returned bytes; for safety, all Lantern-internal BlockGetter wrappers re-hash on the way out.

type LookupOptions

type LookupOptions struct {
	// BitWidth selects the HAMT bit-width. Default FilBitWidth (5).
	BitWidth int
	// HashAlgo selects the hashing function. Default FilHash (sha256).
	HashAlgo func([]byte) []byte
}

Options on a single Lookup call.

type MemBlockStore

type MemBlockStore struct {
	// contains filtered or unexported fields
}

MemBlockStore is an in-memory BlockGetter used for tests and fixture replay. It also implements the go-ipld-cbor IpldBlockstore interface so it can be plugged into go-hamt-ipld and go-amt-ipld directly.

func NewMemBlockStore

func NewMemBlockStore() *MemBlockStore

NewMemBlockStore returns an empty MemBlockStore.

func (*MemBlockStore) CIDs

func (m *MemBlockStore) CIDs() []cid.Cid

CIDs returns the list of stored CIDs (unordered).

func (*MemBlockStore) Get

func (m *MemBlockStore) Get(_ context.Context, c cid.Cid) ([]byte, error)

Get implements BlockGetter.

func (*MemBlockStore) GetBlock

func (m *MemBlockStore) GetBlock(ctx context.Context, c cid.Cid) (block.Block, error)

GetBlock implements the go-ipld-cbor IpldBlockstore interface.

func (*MemBlockStore) Has

func (m *MemBlockStore) Has(c cid.Cid) bool

Has reports whether a block is present.

func (*MemBlockStore) Len

func (m *MemBlockStore) Len() int

Len returns the number of stored blocks.

func (*MemBlockStore) Put

func (m *MemBlockStore) Put(c cid.Cid, raw []byte) cid.Cid

Put stores raw bytes under their CID. Returns the CID for chaining. It does not re-validate that c.Hash matches the bytes; callers wanting to import untrusted data should use PutVerify.

func (*MemBlockStore) PutBlock

func (m *MemBlockStore) PutBlock(_ context.Context, b block.Block) error

PutBlock implements the go-ipld-cbor IpldBlockstore interface.

func (*MemBlockStore) PutVerify

func (m *MemBlockStore) PutVerify(c cid.Cid, raw []byte) error

PutVerify recomputes the CID's hash over raw and inserts only if it matches; otherwise returns an error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL