depositsnapshot

package
v5.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: GPL-3.0 Imports: 21 Imported by: 0

Documentation

Overview

Package depositsnapshot implements the EIP-4881 standard for minimal sparse Merkle tree. The format proposed by the EIP allows for the pruning of deposits that are no longer needed to participate fully in consensus. Full EIP-4881 specification can be found here: https://eips.ethereum.org/EIPS/eip-4881

Index

Constants

View Source
const (
	// DepositContractDepth is the maximum tree depth as defined by EIP-4881.
	DepositContractDepth = 32
)

Variables

View Source
var (
	// ErrInvalidSnapshotRoot occurs when the snapshot root does not match the calculated root.
	ErrInvalidSnapshotRoot = errors.New("snapshot root is invalid")
	// ErrInvalidDepositCount occurs when the value for mix in length is 0.
	ErrInvalidDepositCount = errors.New("deposit count should be greater than 0")
	// ErrInvalidIndex occurs when the index is less than the number of finalized deposits.
	ErrInvalidIndex = errors.New("index should be greater than finalizedDeposits - 1")
	// ErrTooManyDeposits occurs when the number of deposits exceeds the capacity of the tree.
	ErrTooManyDeposits = errors.New("number of deposits should not be greater than the capacity of the tree")
)
View Source
var (
	// ErrFinalizedNodeCannotPushLeaf may occur when attempting to push a leaf to a finalized node. When a node is finalized, it cannot be modified or changed.
	ErrFinalizedNodeCannotPushLeaf = errors.New("can't push a leaf to a finalized node")
	// ErrLeafNodeCannotPushLeaf may occur when attempting to push a leaf to a leaf node.
	ErrLeafNodeCannotPushLeaf = errors.New("can't push a leaf to a leaf node")
	// ErrZeroLevel occurs when the value of level is 0.
	ErrZeroLevel = errors.New("level should be greater than 0")
	// ErrZeroDepth occurs when the value of depth is 0.
	ErrZeroDepth = errors.New("depth should be greater than 0")
)

Functions

This section is empty.

Types

type Cache

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

Cache stores all in-memory deposit objects. This stores all the deposit related data that is required by the beacon-node.

func New

func New() (*Cache, error)

New instantiates a new deposit cache

func (*Cache) AllDepositContainers

func (c *Cache) AllDepositContainers(ctx context.Context) []*ethpb.DepositContainer

AllDepositContainers returns all historical deposit containers.

func (*Cache) AllDeposits

func (c *Cache) AllDeposits(ctx context.Context, untilBlk *big.Int) []*ethpb.Deposit

AllDeposits returns a list of historical deposits until the given block number (inclusive). If no block is specified then this method returns all historical deposits.

func (*Cache) DepositByPubkey

func (c *Cache) DepositByPubkey(ctx context.Context, pubKey []byte) (*ethpb.Deposit, *big.Int)

DepositByPubkey looks through historical deposits and finds one which contains a certain public key within its deposit data.

func (*Cache) DepositsNumberAndRootAtHeight

func (c *Cache) DepositsNumberAndRootAtHeight(ctx context.Context, blockHeight *big.Int) (uint64, [32]byte)

DepositsNumberAndRootAtHeight returns number of deposits made up to blockheight and the root that corresponds to the latest deposit at that blockheight.

func (*Cache) FinalizedDeposits

func (c *Cache) FinalizedDeposits(ctx context.Context) (cache.FinalizedDeposits, error)

FinalizedDeposits returns the finalized deposits trie.

func (*Cache) InsertDeposit

func (c *Cache) InsertDeposit(ctx context.Context, d *ethpb.Deposit, blockNum uint64, index int64, depositRoot [32]byte) error

InsertDeposit into the database. If deposit or block number are nil then this method does nothing.

func (*Cache) InsertDepositContainers

func (c *Cache) InsertDepositContainers(ctx context.Context, ctrs []*ethpb.DepositContainer)

InsertDepositContainers inserts a set of deposit containers into our deposit cache.

func (*Cache) InsertFinalizedDeposits

func (c *Cache) InsertFinalizedDeposits(ctx context.Context, eth1DepositIndex int64,
	executionHash common.Hash, executionNumber uint64) error

InsertFinalizedDeposits inserts deposits up to eth1DepositIndex (inclusive) into the finalized deposits cache.

func (*Cache) InsertPendingDeposit

func (c *Cache) InsertPendingDeposit(ctx context.Context, d *ethpb.Deposit, blockNum uint64, index int64, depositRoot [32]byte)

InsertPendingDeposit into the database. If deposit or block number are nil then this method does nothing.

func (*Cache) NonFinalizedDeposits

func (c *Cache) NonFinalizedDeposits(ctx context.Context, lastFinalizedIndex int64, untilBlk *big.Int) []*ethpb.Deposit

NonFinalizedDeposits returns the list of non-finalized deposits until the given block number (inclusive). If no block is specified then this method returns all non-finalized deposits.

func (*Cache) PendingContainers

func (c *Cache) PendingContainers(ctx context.Context, untilBlk *big.Int) []*ethpb.DepositContainer

PendingContainers returns a list of deposit containers until the given block number (inclusive).

func (*Cache) PendingDeposits

func (c *Cache) PendingDeposits(ctx context.Context, untilBlk *big.Int) []*ethpb.Deposit

PendingDeposits returns a list of deposits until the given block number (inclusive). If no block is specified then this method returns all pending deposits.

func (*Cache) PrunePendingDeposits

func (c *Cache) PrunePendingDeposits(ctx context.Context, merkleTreeIndex int64)

PrunePendingDeposits removes any deposit which is older than the given deposit merkle tree index.

func (*Cache) PruneProofs

func (c *Cache) PruneProofs(ctx context.Context, untilDepositIndex int64) error

PruneProofs removes proofs from all deposits whose index is equal or less than untilDepositIndex.

type DepositTree

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

DepositTree is the Merkle tree representation of deposits.

func DepositTreeFromSnapshotProto

func DepositTreeFromSnapshotProto(snapshotProto *protodb.DepositSnapshot) (*DepositTree, error)

DepositTreeFromSnapshotProto generates a deposit tree object from a provided snapshot.

func NewDepositTree

func NewDepositTree() *DepositTree

NewDepositTree creates an empty deposit tree.

func (*DepositTree) Copy

func (d *DepositTree) Copy() (*DepositTree, error)

Copy performs a deep copy of the tree.

func (*DepositTree) Finalize

func (d *DepositTree) Finalize(eth1DepositIndex int64, executionHash common.Hash, executionNumber uint64) error

Finalize marks a deposit as finalized.

func (*DepositTree) GetSnapshot

func (d *DepositTree) GetSnapshot() (DepositTreeSnapshot, error)

GetSnapshot returns a deposit tree snapshot.

func (*DepositTree) HashTreeRoot

func (d *DepositTree) HashTreeRoot() ([32]byte, error)

HashTreeRoot is defined as part of MerkleTree interface and calculates the hash tree root.

func (*DepositTree) Insert

func (d *DepositTree) Insert(item []byte, _ int) error

Insert is defined as part of MerkleTree interface and adds a new leaf to the tree.

func (*DepositTree) MerkleProof

func (d *DepositTree) MerkleProof(index int) ([][]byte, error)

MerkleProof is defined as part of MerkleTree interface and generates a merkle proof.

func (*DepositTree) NumOfItems

func (d *DepositTree) NumOfItems() int

NumOfItems is defined as part of MerkleTree interface and returns the number of deposits in the tree.

func (*DepositTree) ToProto

func (d *DepositTree) ToProto() (*protodb.DepositSnapshot, error)

ToProto returns a proto object of the deposit snapshot of the tree.

type DepositTreeSnapshot

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

DepositTreeSnapshot represents the data used to create a deposit tree given a snapshot.

func (*DepositTreeSnapshot) CalculateRoot

func (ds *DepositTreeSnapshot) CalculateRoot() ([32]byte, error)

CalculateRoot returns the root of a deposit tree snapshot.

func (*DepositTreeSnapshot) ToProto

ToProto converts the underlying trie into its corresponding proto object.

type FinalizedNode

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

FinalizedNode represents a finalized node and satisfies the MerkleTreeNode interface.

func (*FinalizedNode) Finalize

func (f *FinalizedNode) Finalize(depositsToFinalize uint64, depth uint64) (MerkleTreeNode, error)

Finalize marks deposits of the Merkle tree as finalized.

func (*FinalizedNode) GetFinalized

func (f *FinalizedNode) GetFinalized(result [][32]byte) (uint64, [][32]byte)

GetFinalized returns a list of hashes of all the finalized nodes and the number of deposits.

func (*FinalizedNode) GetRoot

func (f *FinalizedNode) GetRoot() [32]byte

GetRoot returns the root of the Merkle tree.

func (*FinalizedNode) IsFull

func (_ *FinalizedNode) IsFull() bool

IsFull returns whether there is space left for deposits. A FinalizedNode will always return true as by definition it is full and deposits can't be added to it.

func (*FinalizedNode) Left

func (_ *FinalizedNode) Left() MerkleTreeNode

Left returns nil as a finalized node can't have any children.

func (*FinalizedNode) PushLeaf

func (_ *FinalizedNode) PushLeaf(_ [32]byte, _ uint64) (MerkleTreeNode, error)

PushLeaf adds a new leaf node at the next available zero node.

func (*FinalizedNode) Right

func (_ *FinalizedNode) Right() MerkleTreeNode

Right returns nil as a finalized node can't have any children.

type InnerNode

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

InnerNode represents an inner node with two children and satisfies the MerkleTreeNode interface.

func (*InnerNode) Finalize

func (n *InnerNode) Finalize(depositsToFinalize uint64, depth uint64) (MerkleTreeNode, error)

Finalize marks deposits of the Merkle tree as finalized.

func (*InnerNode) GetFinalized

func (n *InnerNode) GetFinalized(result [][32]byte) (uint64, [][32]byte)

GetFinalized returns a list of hashes of all the finalized nodes and the number of deposits.

func (*InnerNode) GetRoot

func (n *InnerNode) GetRoot() [32]byte

GetRoot returns the root of the Merkle tree.

func (*InnerNode) IsFull

func (n *InnerNode) IsFull() bool

IsFull returns whether there is space left for deposits.

func (*InnerNode) Left

func (n *InnerNode) Left() MerkleTreeNode

Left returns the child node on the left.

func (*InnerNode) PushLeaf

func (n *InnerNode) PushLeaf(leaf [32]byte, depth uint64) (MerkleTreeNode, error)

PushLeaf adds a new leaf node at the next available zero node.

func (*InnerNode) Right

func (n *InnerNode) Right() MerkleTreeNode

Right returns the child node on the right.

type LeafNode

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

LeafNode represents a leaf node holding a deposit and satisfies the MerkleTreeNode interface.

func (*LeafNode) Finalize

func (l *LeafNode) Finalize(depositsToFinalize uint64, depth uint64) (MerkleTreeNode, error)

Finalize marks deposits of the Merkle tree as finalized.

func (*LeafNode) GetFinalized

func (_ *LeafNode) GetFinalized(result [][32]byte) (uint64, [][32]byte)

GetFinalized returns a list of hashes of all the finalized nodes and the number of deposits.

func (*LeafNode) GetRoot

func (l *LeafNode) GetRoot() [32]byte

GetRoot returns the root of the Merkle tree.

func (*LeafNode) IsFull

func (_ *LeafNode) IsFull() bool

IsFull returns whether there is space left for deposits. A LeafNode will always return true as it is the last node in the tree and therefore can't have any deposits added to it.

func (*LeafNode) Left

func (_ *LeafNode) Left() MerkleTreeNode

Left returns nil as a leaf node is the last node and can't have any children.

func (*LeafNode) PushLeaf

func (_ *LeafNode) PushLeaf(_ [32]byte, _ uint64) (MerkleTreeNode, error)

PushLeaf adds a new leaf node at the next available zero node.

func (*LeafNode) Right

func (_ *LeafNode) Right() MerkleTreeNode

Right returns nil as a leaf node is the last node and can't have any children.

type MerkleTreeNode

type MerkleTreeNode interface {
	// GetRoot returns the root of the Merkle tree.
	GetRoot() [32]byte
	// IsFull returns whether there is space left for deposits.
	IsFull() bool
	// Finalize marks deposits of the Merkle tree as finalized.
	Finalize(depositsToFinalize uint64, depth uint64) (MerkleTreeNode, error)
	// GetFinalized returns the number of deposits and a list of hashes of all the finalized nodes.
	GetFinalized(result [][32]byte) (uint64, [][32]byte)
	// PushLeaf adds a new leaf node at the next available Zero node.
	PushLeaf(leaf [32]byte, depth uint64) (MerkleTreeNode, error)

	// Right represents the right child of a node.
	Right() MerkleTreeNode
	// Left represents the left child of a node.
	Left() MerkleTreeNode
}

MerkleTreeNode is the interface for a Merkle tree.

type ZeroNode

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

ZeroNode represents an empty node without a deposit and satisfies the MerkleTreeNode interface.

func (*ZeroNode) Finalize

func (_ *ZeroNode) Finalize(depositsToFinalize uint64, depth uint64) (MerkleTreeNode, error)

Finalize marks deposits of the Merkle tree as finalized.

func (*ZeroNode) GetFinalized

func (_ *ZeroNode) GetFinalized(result [][32]byte) (uint64, [][32]byte)

GetFinalized returns a list of hashes of all the finalized nodes and the number of deposits.

func (*ZeroNode) GetRoot

func (z *ZeroNode) GetRoot() [32]byte

GetRoot returns the root of the Merkle tree.

func (*ZeroNode) IsFull

func (_ *ZeroNode) IsFull() bool

IsFull returns wh ether there is space left for deposits. A ZeroNode will always return false as a ZeroNode is an empty node that gets replaced by a deposit.

func (*ZeroNode) Left

func (_ *ZeroNode) Left() MerkleTreeNode

Left returns nil as a zero node can't have any children.

func (*ZeroNode) PushLeaf

func (_ *ZeroNode) PushLeaf(leaf [32]byte, depth uint64) (MerkleTreeNode, error)

PushLeaf adds a new leaf node at the next available zero node.

func (*ZeroNode) Right

func (_ *ZeroNode) Right() MerkleTreeNode

Right returns nil as a zero node can't have any children.

Jump to

Keyboard shortcuts

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