Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Jan 27, 2025
1 parent cfbcc3d commit c5ff49f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions core/trie2/trienode/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/NethermindEth/juno/core/felt"
)

// Represents a raw trie node, which contains the encoded blob and the hash of the node.
type Node struct {
blob []byte
hash felt.Felt
Expand Down
12 changes: 9 additions & 3 deletions core/trie2/trienode/nodeset.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"github.com/NethermindEth/juno/core/trie2/trieutils"
)

// Contains a set of nodes, which are indexed by their path in the trie.
// It is not thread safe.
type NodeSet struct {
Owner felt.Felt
Owner felt.Felt // The owner (i.e. contract address)
Nodes map[trieutils.BitArray]*Node
updates int
deletes int
updates int // the count of updated and inserted nodes
deletes int // the count of deleted nodes
}

func NewNodeSet(owner felt.Felt) *NodeSet {
Expand All @@ -29,6 +31,7 @@ func (ns *NodeSet) Add(key trieutils.BitArray, node *Node) {
ns.Nodes[key] = node
}

// Iterates over the nodes in a sorted order and calls the callback for each node.
func (ns *NodeSet) ForEach(desc bool, callback func(key trieutils.BitArray, node *Node) error) error {
paths := make([]trieutils.BitArray, 0, len(ns.Nodes))
for key := range ns.Nodes {
Expand All @@ -54,6 +57,8 @@ func (ns *NodeSet) ForEach(desc bool, callback func(key trieutils.BitArray, node
return nil
}

// Merges the other node set into the current node set.
// The owner of both node sets must be the same.
func (ns *NodeSet) MergeSet(other *NodeSet) error {
if ns.Owner != other.Owner {
return fmt.Errorf("cannot merge node sets with different owners %x-%x", ns.Owner, other.Owner)
Expand All @@ -64,6 +69,7 @@ func (ns *NodeSet) MergeSet(other *NodeSet) error {
return nil
}

// Adds a set of nodes to the current node set.
func (ns *NodeSet) Merge(owner felt.Felt, other map[trieutils.BitArray]*Node) error {
if ns.Owner != owner {
return fmt.Errorf("cannot merge node sets with different owners %x-%x", ns.Owner, owner)
Expand Down

0 comments on commit c5ff49f

Please sign in to comment.