Documentation
¶
Index ¶
- Constants
- Variables
- func VerifyProof(key, value, root, siblings types.HexBytes, pathBits uint64) bool
- type CensusDB
- func (c *CensusDB) CleanupWorkingCensus(censusID uuid.UUID) error
- func (c *CensusDB) Del(censusID uuid.UUID) error
- func (c *CensusDB) Exists(censusID uuid.UUID) bool
- func (c *CensusDB) ExistsByAddress(address common.Address) bool
- func (c *CensusDB) ExistsByRoot(root types.HexBytes) bool
- func (c *CensusDB) HashLen() int
- func (c *CensusDB) Import(root types.HexBytes, reader io.Reader) (*CensusRef, error)
- func (c *CensusDB) ImportAll(data []byte) (*CensusRef, error)
- func (c *CensusDB) ImportEvents(root types.HexBytes, events []census.CensusEvent) (*CensusRef, error)
- func (c *CensusDB) ImportEventsByAddress(address common.Address, expectedRoot types.HexBytes, ...) (*CensusRef, error)
- func (c *CensusDB) Load(censusID uuid.UUID) (*CensusRef, error)
- func (c *CensusDB) LoadByAddress(address common.Address) (*CensusRef, error)
- func (c *CensusDB) LoadByRoot(root types.HexBytes) (*CensusRef, error)
- func (c *CensusDB) New(censusID uuid.UUID) (*CensusRef, error)
- func (c *CensusDB) NewByAddress(address common.Address) (*CensusRef, error)
- func (c *CensusDB) NewByRoot(root types.HexBytes) (*CensusRef, error)
- func (c *CensusDB) ProofByRoot(root, leafKey types.HexBytes) (*types.CensusProof, error)
- func (c *CensusDB) PublishCensus(originCensusID uuid.UUID, destinationRef *CensusRef) error
- func (c *CensusDB) PurgeWorkingCensuses(maxAge time.Duration) (int, error)
- func (c *CensusDB) SizeByRoot(root types.HexBytes) (int, error)
- func (c *CensusDB) TrunkKey(key types.HexBytes) types.HexBytes
- func (c *CensusDB) VerifyProof(proof *types.CensusProof) bool
- type CensusRef
- func (cr *CensusRef) ApplyEvents(events []census.CensusEvent) error
- func (cr *CensusRef) FetchKeysAndValues() ([]types.HexBytes, []*types.BigInt, error)
- func (cr *CensusRef) GenProof(key types.HexBytes) (*types.CensusProof, error)
- func (cr *CensusRef) Insert(key, value types.HexBytes) error
- func (cr *CensusRef) InsertBatch(keys, values []types.HexBytes) ([]any, error)
- func (cr *CensusRef) Root() types.HexBytes
- func (cr *CensusRef) SetTree(tree *census.CensusIMT)
- func (cr *CensusRef) Size() int
- func (cr *CensusRef) Tree() *census.CensusIMT
Constants ¶
const (
// CensusKeyMaxLen is the maximum length for census keys (20 bytes for Ethereum addresses)
CensusKeyMaxLen = 20
)
Variables ¶
var ( // ErrCensusNotFound is returned when a census is not found in the database. ErrCensusNotFound = fmt.Errorf("census not found in the local database") // ErrCensusAlreadyExists is returned by New() if the census already exists. ErrCensusAlreadyExists = fmt.Errorf("census already exists in the local database") // ErrWrongAuthenticationToken is returned when the authentication token is invalid. ErrWrongAuthenticationToken = fmt.Errorf("wrong authentication token") // ErrCensusIsLocked is returned if the census does not allow write operations. ErrCensusIsLocked = fmt.Errorf("census is locked") // ErrKeyNotFound is returned when a key is not found in the Merkle tree. ErrKeyNotFound = fmt.Errorf("key not found") )
Functions ¶
Types ¶
type CensusDB ¶
type CensusDB struct {
// contains filtered or unexported fields
}
CensusDB is a safe and persistent database of census trees. It maintains an in‑memory index mapping Merkle tree roots (in hexadecimal form) to census IDs.
func NewCensusDB ¶
NewCensusDB creates a new CensusDB object. It scans the persistent database for existing census references and builds the in‑memory index.
func (*CensusDB) CleanupWorkingCensus ¶
CleanupWorkingCensus removes a working census from the database and memory. This is used to clean up temporary censuses after they have been converted to root-based ones.
func (*CensusDB) ExistsByAddress ¶
ExistsByAddress returns true if a census with the given Ethereum address exists in the local database.
func (*CensusDB) ExistsByRoot ¶
ExistsByRoot returns true if a census with the given root exists in the local database.
func (*CensusDB) Import ¶
Import imports a census from a JSON-encoded census dump read from an io.Reader. It creates a new census tree, populates it with the data from the dump, and creates a new CensusRef with the imported tree. It returns the CensusRef and any error encountered during the process, such as decoding errors or tree creation/import errors.
func (*CensusDB) ImportAll ¶
ImportAll imports a census from a JSON-encoded census dump. It decodes the dump, creates a new census tree, and populates it with the data from the dump. Then, it creates a new CensusRef with the imported tree and adds it to the database. It returns the CensusRef and any error encountered during the process, such as decoding errors or tree creation/import errors.
func (*CensusDB) ImportEvents ¶
func (c *CensusDB) ImportEvents(root types.HexBytes, events []census.CensusEvent) (*CensusRef, error)
ImportEvents imports a census from a list of census events. It creates a new census tree by the root provided, applies the events checking against the that root, and returns the CensusRef.
func (*CensusDB) ImportEventsByAddress ¶
func (c *CensusDB) ImportEventsByAddress( address common.Address, expectedRoot types.HexBytes, events []census.CensusEvent, ) (*CensusRef, error)
ImportEventsByAddress imports a census from a list of census events, identified by an Ethereum address. It creates a new census tree by the address provided, applies the events checking against the expected root, and returns the CensusRef.
func (*CensusDB) LoadByAddress ¶
LoadByAddress loads a census by its Ethereum address from memory or from the persistent KV database.
func (*CensusDB) LoadByRoot ¶
LoadByRoot loads a census by its root from memory or from the persistent KV database.
func (*CensusDB) New ¶
New creates a new working census with a UUID identifier and adds it to the database. It returns ErrCensusAlreadyExists if a census with the given UUID is already present.
func (*CensusDB) NewByAddress ¶
NewByAddress creates a new census identified by an Ethereum address. It returns ErrCensusAlreadyExists if a census with the given address is already present.
func (*CensusDB) NewByRoot ¶
NewByRoot creates a new census identified by its root. It returns ErrCensusAlreadyExists if a census with the given root is already present.
func (*CensusDB) ProofByRoot ¶
ProofByRoot generates a Merkle proof for the given leafKey in a census identified by its root.
func (*CensusDB) PublishCensus ¶
PublishCensus publishes a working census to a root-based census by moving the Pebble directory.
func (*CensusDB) PurgeWorkingCensuses ¶
PurgeWorkingCensuses removes all working censuses older than the specified duration.
func (*CensusDB) SizeByRoot ¶
SizeByRoot returns the number of leaves in the Merkle tree with the given root.
func (*CensusDB) TrunkKey ¶
TrunkKey computes the hash of a key and truncates it to the required length. For leanimt census, we use the address directly (20 bytes for Ethereum addresses).
func (*CensusDB) VerifyProof ¶
func (c *CensusDB) VerifyProof(proof *types.CensusProof) bool
VerifyProof checks the validity of a Merkle proof.
type CensusRef ¶
type CensusRef struct {
ID uuid.UUID
HashType string
LastUsed time.Time
// contains filtered or unexported fields
}
CensusRef is a reference to a census. It holds the Merkle tree. All accesses to the underlying tree (and its currentRoot) are protected by treeMu.
func (*CensusRef) ApplyEvents ¶
func (cr *CensusRef) ApplyEvents(events []census.CensusEvent) error
ApplyEvents safely applies a list of census events to the Merkle tree.
func (*CensusRef) FetchKeysAndValues ¶
FetchKeysAndValues fetches all keys and values from the Merkle tree. Returns the keys as byte arrays (20-byte addresses) and the values as BigInts (weights). Note: This is a placeholder implementation. The lean-imt census package doesn't expose a direct iteration API, so this would need to be implemented if required.
func (*CensusRef) GenProof ¶
GenProof safely generates a Merkle proof for the given leaf key. It returns the proof components (key, value, siblings, index) and an inclusion boolean. For lean-imt, key must be a 20-byte Ethereum address.
func (*CensusRef) Insert ¶
Insert safely inserts a key/value pair into the Merkle tree. It holds treeMu during the Add and Root calls. Key must be exactly 20 bytes (Ethereum address).
func (*CensusRef) InsertBatch ¶
InsertBatch safely inserts a batch of key/value pairs into the Merkle tree.