zkregistry

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2024 License: GPL-3.0, Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// LeafKeyLength is the size of the key in bytes used to store a leaf node in the sparse tree.
	LeafKeyLength = storage.PrefixLength +
		ZKCertificateRegistryIndexLength +
		TreeLevelTypeLength +
		LeafIndexTypeLength

	// LeafIndexBytesSize is the size of leaf index in bytes.
	LeafIndexBytesSize = storage.PrefixLength + ZKCertificateRegistryIndexLength + LeafValueTypeLength
)
View Source
const (
	EmptyLeafSeed = "Galactica"

	TreeLevelTypeLength = 1
	LeafIndexTypeLength = 4
	LeafValueTypeLength = 32
)
View Source
const (

	// ZKCertificateRegistryIndexLength is the length of the address index in bytes.
	ZKCertificateRegistryIndexLength = 1
)

Variables

View Source
var DefaultEmptyLeafValue = new(uint256.Int).Mod(
	uint256.MustFromBig(new(big.Int).SetBytes(makeDefaultSeedForEmptyLeaf())),
	uint256.MustFromBig(ff.Modulus()),
)

Functions

This section is empty.

Types

type BatchWithLeavesBuffer

type BatchWithLeavesBuffer struct {
	db.Batch
	// contains filtered or unexported fields
}

BatchWithLeavesBuffer is a wrapper around db.Batch that allows reading leaf nodes from the buffer.

func NewBatchWithLeavesBuffer

func NewBatchWithLeavesBuffer(batch db.Batch, index RegistryIndex) *BatchWithLeavesBuffer

func (*BatchWithLeavesBuffer) GetLeaf

func (b *BatchWithLeavesBuffer) GetLeaf(_ context.Context, level TreeLevel, index TreeLeafIndex) (*uint256.Int, error)

GetLeaf reads the leaf node at the given level and index from the buffer. If the leaf node is not found in the buffer, it is returned an error ErrNotFound.

func (*BatchWithLeavesBuffer) SetLeaf

func (b *BatchWithLeavesBuffer) SetLeaf(_ context.Context, level TreeLevel, index TreeLeafIndex, value *uint256.Int) error

SetLeaf sets the leaf node at the given level and index to the given value to the buffer. The value is not written to the database until the batch is committed. Call batch.Write() or batch.WriteSync() to write the changes to the database.

type IndexStorage

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

IndexStorage serves as a storage mechanism for the ZkCertificateRegistry contract's address index. It maintains the index associated with each registry address. Structure:

ZKCertificateRegistryIndexPrefix -> address -> index
ZKCertificateRegistryCounterPrefix -> index counter

func NewIndexStorage

func NewIndexStorage(database db.DB) *IndexStorage

NewIndexStorage creates a new address index storage.

func (*IndexStorage) ApplyAddressToIndex

func (f *IndexStorage) ApplyAddressToIndex(ctx context.Context, address common.Address) (RegistryIndex, error)

ApplyAddressToIndex applies the address to the address index and returns the index.

func (*IndexStorage) FindRegistryIndex

func (f *IndexStorage) FindRegistryIndex(
	_ context.Context,
	address common.Address,
) (RegistryIndex, error)

FindRegistryIndex finds the address index for the given address.

type IndexerProgressTracker added in v0.2.3

type IndexerProgressTracker interface {
	IsOnHead() bool
	SetOnHead(onHead bool)
}

type LeafOperation

type LeafOperation struct {
	Leaf TreeLeaf
	Op   Operation
}

type LeafStorage

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

LeafStorage is a storage mechanism for the leaf nodes in the sparse tree.

func NewLeafStorage

func NewLeafStorage(database db.DB) *LeafStorage

func (*LeafStorage) DeleteLeafIndex

func (s *LeafStorage) DeleteLeafIndex(
	_ context.Context,
	batch db.Batch,
	registryIndex RegistryIndex,
	leafValue *uint256.Int,
) error

DeleteLeafIndex deletes the leaf index by leaf value.

func (*LeafStorage) GetLeaf

func (s *LeafStorage) GetLeaf(
	_ context.Context,
	registryIndex RegistryIndex,
	level TreeLevel,
	index TreeLeafIndex,
) (*uint256.Int, error)

func (*LeafStorage) GetLeafIndex

func (s *LeafStorage) GetLeafIndex(
	_ context.Context,
	registryIndex RegistryIndex,
	leafValue *uint256.Int,
) (TreeLeafIndex, error)

GetLeafIndex gets the leaf index by leaf value.

func (*LeafStorage) LeafView

func (s *LeafStorage) LeafView(registryIndex RegistryIndex) *LeafView

LeafView returns a view of the leaf storage for the given tree index.

func (*LeafStorage) NewBatch

func (s *LeafStorage) NewBatch(registryIndex RegistryIndex) *BatchWithLeavesBuffer

NewBatch TODO: do we really need this? Probably move to view or remove.

func (*LeafStorage) SetLeaf

func (s *LeafStorage) SetLeaf(
	_ context.Context,
	registryIndex RegistryIndex,
	level TreeLevel,
	index TreeLeafIndex,
	value *uint256.Int,
) error

func (*LeafStorage) SetLeafIndex

func (s *LeafStorage) SetLeafIndex(
	_ context.Context,
	batch db.Batch,
	registryIndex RegistryIndex,
	leafValue *uint256.Int,
	leafIndex TreeLeafIndex,
) error

SetLeafIndex sets the leaf index by leaf value.

type LeafView

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

LeafView provides a view of the leaf storage for a specific tree index.

func NewLeafStorageView

func NewLeafStorageView(registryIndex RegistryIndex, leafStorage *LeafStorage) *LeafView

func (*LeafView) DeleteLeafIndex

func (s *LeafView) DeleteLeafIndex(ctx context.Context, batch db.Batch, value *uint256.Int) error

func (*LeafView) GetLeaf

func (s *LeafView) GetLeaf(ctx context.Context, level TreeLevel, index TreeLeafIndex) (*uint256.Int, error)

func (*LeafView) GetLeafIndex

func (s *LeafView) GetLeafIndex(ctx context.Context, value *uint256.Int) (TreeLeafIndex, error)

func (*LeafView) SetLeaf

func (s *LeafView) SetLeaf(ctx context.Context, level TreeLevel, index TreeLeafIndex, value *uint256.Int) error

func (*LeafView) SetLeafIndex

func (s *LeafView) SetLeafIndex(ctx context.Context, batch db.Batch, value *uint256.Int, index TreeLeafIndex) error

type MerkleProof

type MerkleProof struct {
	// Leaf is the value of the Leaf node
	Leaf *uint256.Int

	// Path is a list of hashes of the branches on the side of the path
	Path []*uint256.Int

	// Index can also be interpreted as binary number. If a bit is set, it means that the path is the right
	// part of the parent node. The rightmost bit is for the Leaf.
	Index TreeLeafIndex

	// Root is the Root of the tree
	Root *uint256.Int
}

type Metadata

type Metadata struct {
	Address         common.Address
	Depth           TreeLevel
	EmptyValue      *uint256.Int
	InitBlockHeight uint64
	Description     string
	Index           RegistryIndex
}

type MetadataStorage

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

MetadataStorage is a storage for the metadata of the zk certificate registry contract.

func NewMetadataStorage

func NewMetadataStorage(database db.DB) *MetadataStorage

func (*MetadataStorage) GetMetadata

func (s *MetadataStorage) GetMetadata(_ context.Context, address common.Address) (Metadata, error)

GetMetadata gets the metadata for the zk certificate registry contract.

func (*MetadataStorage) NewBatch

func (s *MetadataStorage) NewBatch() db.Batch

func (*MetadataStorage) SetMetadata

func (s *MetadataStorage) SetMetadata(_ context.Context, batch db.Batch, metadata Metadata) error

SetMetadata sets the metadata for the zk certificate registry contract.

type Mutex

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

Mutex is a mutex for each zk certificate registry. It is used to lock the zk certificate registry address for reading or writing.

func NewMutex

func NewMutex() *Mutex

func (*Mutex) Lock

func (m *Mutex) Lock(index RegistryIndex)

Lock locks the mutex for the given address.

func (*Mutex) NewView

func (m *Mutex) NewView(registryIndex RegistryIndex) *MutexView

func (*Mutex) RLock

func (m *Mutex) RLock(index RegistryIndex)

RLock locks the mutex for reading for the given address.

func (*Mutex) RUnlock

func (m *Mutex) RUnlock(index RegistryIndex)

RUnlock unlocks the mutex for reading for the given address.

func (*Mutex) Unlock

func (m *Mutex) Unlock(index RegistryIndex)

Unlock unlocks the mutex for the given address.

type MutexView

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

MutexView is a view of the mutex for a specific zk certificate registry.

func (*MutexView) Lock

func (v *MutexView) Lock()

Lock locks the registry for the given address.

func (*MutexView) RLock

func (v *MutexView) RLock()

RLock locks the registry for the given address for reading.

func (*MutexView) RUnlock

func (v *MutexView) RUnlock()

RUnlock unlocks the registry for the given address for reading.

func (*MutexView) Unlock

func (v *MutexView) Unlock()

Unlock unlocks the registry for the given address.

type Operation

type Operation uint8
const (
	OperationAddition   Operation = 1
	OperationRevocation Operation = 2
)

type OperationsBuffer

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

func NewOperationsBuffer

func NewOperationsBuffer() *OperationsBuffer

func (*OperationsBuffer) AppendAddition

func (b *OperationsBuffer) AppendAddition(index TreeLeafIndex, value *uint256.Int) error

func (*OperationsBuffer) AppendRevocation

func (b *OperationsBuffer) AppendRevocation(index TreeLeafIndex, value *uint256.Int) error

func (*OperationsBuffer) Operations

func (b *OperationsBuffer) Operations() []LeafOperation

func (*OperationsBuffer) Reset

func (b *OperationsBuffer) Reset()

type Progress added in v0.2.3

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

func NewProgress added in v0.2.3

func NewProgress() *Progress

func (*Progress) IsOnHead added in v0.2.3

func (p *Progress) IsOnHead() bool

IsOnHead returns true if the indexer is on the head of the chain.

func (*Progress) SetOnHead added in v0.2.3

func (p *Progress) SetOnHead(onHead bool)

SetOnHead sets the flag that the indexer is on the head of the chain.

type RegistryIndex

type RegistryIndex uint8

RegistryIndex is the index of the ZkCertificateRegistry contract address in the storage.

type Service

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

Service provides methods to interact with the zk certificate registry. It caches the registry instances for faster access.

func InitializeService

func InitializeService(kvDB db.DB, ethereumClient *ethclient.Client) *Service

func NewService

func NewService(
	registryStorage *MetadataStorage,
	registryIndexStorage *IndexStorage,
	registryMutex *Mutex,
	leafStorage *LeafStorage,
	contractCaller bind.ContractCaller,
) *Service

func (*Service) InitializeRegistry

func (s *Service) InitializeRegistry(ctx context.Context, address common.Address) (*ZKCertificateRegistry, error)

InitializeRegistry fetches or creates the zk certificate registry for the given address. If the registry does not exist, it fetches the metadata from the contract, stores it, and creates a new registry instance. If the registry exists, it fetches the metadata from the storage and creates a new registry instance. Usually, this function is used at initialization time to fetch or create all registry instances

func (*Service) ZKCertificateRegistry

func (s *Service) ZKCertificateRegistry(ctx context.Context, address common.Address) (*ZKCertificateRegistry, error)

ZKCertificateRegistry returns the zk certificate registry for the given address.

type SparseMerkleTree

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

func NewSparseTree

func NewSparseTree(
	depth TreeLevel,
	emptyLeafValue *uint256.Int,
	storageLeafGetter TreeLeafGetter,
) (*SparseMerkleTree, error)

NewSparseTree creates a new SparseMerkleTree with the specified depth.

func (*SparseMerkleTree) CreateProof

func (t *SparseMerkleTree) CreateProof(ctx context.Context, index TreeLeafIndex) (MerkleProof, error)

CreateProof creates a proof for a TreeLeaf at a specified index.

func (*SparseMerkleTree) GetRandomEmptyLeafIndex

func (t *SparseMerkleTree) GetRandomEmptyLeafIndex(ctx context.Context) (TreeLeafIndex, error)

GetRandomEmptyLeafIndex returns a random index of an empty TreeLeaf.

func (*SparseMerkleTree) GetRoot

func (t *SparseMerkleTree) GetRoot(ctx context.Context) (*uint256.Int, error)

GetRoot returns the Root of the SparseMerkleTree.

func (*SparseMerkleTree) InsertLeaf

func (t *SparseMerkleTree) InsertLeaf(
	ctx context.Context,
	batch TreeLeafGetterSetter,
	index TreeLeafIndex,
	value *uint256.Int,
) error

InsertLeaf inserts a TreeLeaf into the SparseMerkleTree at a specified index.

func (*SparseMerkleTree) InsertLeaves

func (t *SparseMerkleTree) InsertLeaves(ctx context.Context, batch TreeLeafGetterSetter, leaves []TreeLeaf) error

InsertLeaves adds multiple leaves to a specific level in the SparseMerkleTree. The tree is updated from the TreeLeaf to the Root. The parent nodes' hash is computed only once, after all leaves have been inserted. This function enhances efficiency by only updating the tree branches that are impacted by the new leaves. This approach significantly reduces the number of hash computations, thereby improving performance. Function mutate the batch to insert the leaves, so internal storage not updated until batch is written.

type TreeLeaf

type TreeLeaf struct {
	Index TreeLeafIndex
	Value *uint256.Int
}

type TreeLeafGetter

type TreeLeafGetter interface {
	GetLeaf(ctx context.Context, level TreeLevel, index TreeLeafIndex) (*uint256.Int, error)
}

TreeLeafGetter is an interface that allows to retrieve TreeLeaf values.

type TreeLeafGetterSetter

type TreeLeafGetterSetter interface {
	TreeLeafGetter
	SetLeaf(ctx context.Context, level TreeLevel, index TreeLeafIndex, value *uint256.Int) error
}

TreeLeafGetterSetter is an interface that allows to retrieve and set TreeLeaf values.

type TreeLeafIndex

type TreeLeafIndex uint32

type TreeLevel

type TreeLevel uint8

type ZKCertificateRegistry

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

ZKCertificateRegistry represents a zk certificate registry for a specific address. It provides methods to interact with the merkle tree.

func NewZKCertificateRegistry

func NewZKCertificateRegistry(
	metadata Metadata,
	sparseTree *SparseMerkleTree,
	leafView *LeafView,
	mutex *MutexView,
	progressTracker IndexerProgressTracker,
) *ZKCertificateRegistry

func (*ZKCertificateRegistry) CommitOperations

func (reg *ZKCertificateRegistry) CommitOperations(ctx context.Context, batch db.Batch, operations []LeafOperation) error

CommitOperations commits the given operations to the tree.

func (*ZKCertificateRegistry) CreateProof

func (reg *ZKCertificateRegistry) CreateProof(ctx context.Context, leaf *uint256.Int) (MerkleProof, error)

CreateProof creates a proof for the given leaf value.

func (*ZKCertificateRegistry) GetRandomEmptyLeafProof

func (reg *ZKCertificateRegistry) GetRandomEmptyLeafProof(ctx context.Context) (MerkleProof, error)

GetRandomEmptyLeafProof returns a proof for a random empty leaf.

func (*ZKCertificateRegistry) LeafView

func (reg *ZKCertificateRegistry) LeafView() *LeafView

LeafView returns the leaf view of the tree.

func (*ZKCertificateRegistry) Metadata

func (reg *ZKCertificateRegistry) Metadata() Metadata

Metadata returns the metadata of the tree.

func (*ZKCertificateRegistry) Mutex

func (reg *ZKCertificateRegistry) Mutex() *MutexView

Mutex returns the mutex view of the tree.

func (*ZKCertificateRegistry) ProgressTracker added in v0.2.3

func (reg *ZKCertificateRegistry) ProgressTracker() IndexerProgressTracker

ProgressTracker returns the progress tracker of the registry.

func (*ZKCertificateRegistry) Tree

Tree returns the sparse merkle tree.

Jump to

Keyboard shortcuts

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