diff --git a/core/trie/node.go b/core/trie/node.go index 788567bb3d..647fce7be8 100644 --- a/core/trie/node.go +++ b/core/trie/node.go @@ -144,12 +144,13 @@ func (n *Node) String() string { return fmt.Sprintf("Node{Value: %s, Left: %s, Right: %s, LeftHash: %s, RightHash: %s}", n.Value, n.Left, n.Right, n.LeftHash, n.RightHash) } -// Merge combines the fields of two nodes if they are not nil. -// If a field is nil in the current node, it is updated with the corresponding field from the other node. -// If a field is not nil in both nodes, the fields must be equal, otherwise an error is returned. +// Update the receiver with non-nil fields from the `other` Node. +// If a field is non-nil in both Nodes, they must be equal, or an error is returned. +// +// This method modifies the receiver in-place and returns an error if any field conflicts are detected. // //nolint:gocyclo -func (n *Node) Merge(other *Node) error { +func (n *Node) Update(other *Node) error { // Compare Value if both exist if n.Value != nil && other.Value != nil { if !n.Value.Equal(other.Value) { diff --git a/core/trie/trie.go b/core/trie/trie.go index 587967707c..702fe77eb9 100644 --- a/core/trie/trie.go +++ b/core/trie/trie.go @@ -133,7 +133,7 @@ func (sn *StorageNode) String() string { return fmt.Sprintf("StorageNode{key: %s, node: %s}", sn.key, sn.node) } -func (sn *StorageNode) Merge(other *StorageNode) error { +func (sn *StorageNode) Update(other *StorageNode) error { if sn.key != nil && other.key != nil && !sn.key.Equal(NilKey) && !other.key.Equal(NilKey) { if !sn.key.Equal(other.key) { return fmt.Errorf("keys do not match: %s != %s", sn.key, other.key) @@ -141,7 +141,7 @@ func (sn *StorageNode) Merge(other *StorageNode) error { } else if other.key != nil && !other.key.Equal(NilKey) { sn.key = other.key } - return sn.node.Merge(other.node) + return sn.node.Update(other.node) } func NewStorageNode(key *Key, node *Node) *StorageNode { @@ -182,10 +182,10 @@ func (s *StorageNodeSet) Put(key Key, node *StorageNode) error { return fmt.Errorf("cannot put nil node") } - // If key exists, merge the nodes + // If key exists, update the node if existingNode, exists := s.set.Get(key); exists { - if err := existingNode.Merge(node); err != nil { - return fmt.Errorf("failed to merge nodes for key %v: %w", key, err) + if err := existingNode.Update(node); err != nil { + return fmt.Errorf("failed to update node for key %v: %w", key, err) } return nil }