Skip to content

Commit

Permalink
revise NodeHandleOwned
Browse files Browse the repository at this point in the history
  • Loading branch information
timwu20 committed Oct 11, 2024
1 parent 6895e2e commit bd25322
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
6 changes: 3 additions & 3 deletions pkg/trie/triedb/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func lookupWithCacheInternal[H hash.Hash, Hasher hash.Hasher[H], R, QueryItem an
// this loop iterates through all inline children (usually max 1)
// without incrementing the depth.
for {
var nextNode NodeHandleOwned
var nextNode CachedNodeHandle
switch node := node.(type) {
case LeafCachedNode[H]:
if partial.EqualNibbleSlice(node.PartialKey) {
Expand Down Expand Up @@ -288,10 +288,10 @@ func lookupWithCacheInternal[H hash.Hash, Hasher hash.Hasher[H], R, QueryItem an

// check if new node data is inline or hash.
switch nextNode := nextNode.(type) {
case NodeHandleOwnedHash[H]:
case HashCachedNodeHandle[H]:
hash = nextNode.Hash
break inlineLoop
case NodeHandleOwnedInline[H]:
case InlineCachedNodeHandle[H]:
node = nextNode.CachedNode
default:
panic("unreachable")
Expand Down
2 changes: 1 addition & 1 deletion pkg/trie/triedb/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func newNodeFromCachedNode[H hash.Hash](

child := func(i uint) NodeHandle {
if encodedChildren[i] != nil {
newChild := newNodeHandleFromNodeHandleOwned(encodedChildren[i], storage)
newChild := newNodeHandleFromCachedNodeHandle(encodedChildren[i], storage)
return newChild
}
return nil
Expand Down
36 changes: 18 additions & 18 deletions pkg/trie/triedb/node_owned.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,29 @@ func newCachedNodeValueFromEncodedValue[H hash.Hash, Hasher hash.Hasher[H]](encV
}

// Cached version of [codec.MerkleValue] interface constraint.
type NodeHandleOwnedTypes[H hash.Hash] interface {
NodeHandleOwnedHash[H] | NodeHandleOwnedInline[H]
type CachedNodeHandleTypes[H hash.Hash] interface {
HashCachedNodeHandle[H] | InlineCachedNodeHandle[H]
}

// Cached version of [codec.MerkleValue].
type NodeHandleOwned interface {
/// Returns [NodeHandleOwned] as a [ChildReference].
type CachedNodeHandle interface {
/// Returns [CachedNodeHandle] as a [ChildReference].
ChildReference() ChildReference
}

type (
NodeHandleOwnedHash[H hash.Hash] struct {
HashCachedNodeHandle[H hash.Hash] struct {
Hash H
}
NodeHandleOwnedInline[H hash.Hash] struct {
InlineCachedNodeHandle[H hash.Hash] struct {
CachedNode[H]
}
)

func (nho NodeHandleOwnedHash[H]) ChildReference() ChildReference {
func (nho HashCachedNodeHandle[H]) ChildReference() ChildReference {
return HashChildReference[H](nho)
}
func (nho NodeHandleOwnedInline[H]) ChildReference() ChildReference {
func (nho InlineCachedNodeHandle[H]) ChildReference() ChildReference {
encoded := nho.CachedNode.encoded()
store := (*new(H))
if len(encoded) > store.Length() {
Expand All @@ -93,12 +93,12 @@ func (nho NodeHandleOwnedInline[H]) ChildReference() ChildReference {
return InlineChildReference(encoded)
}

func newNodeHandleOwnedFromMerkleValue[H hash.Hash, Hasher hash.Hasher[H]](
func newCachedNodeHandleFromMerkleValue[H hash.Hash, Hasher hash.Hasher[H]](
mv codec.MerkleValue,
) (NodeHandleOwned, error) {
) (CachedNodeHandle, error) {
switch mv := mv.(type) {
case codec.HashedNode[H]:
return NodeHandleOwnedHash[H](mv), nil
return HashCachedNodeHandle[H](mv), nil
case codec.InlineNode:
buf := bytes.NewBuffer(mv)
node, err := codec.Decode[H](buf)
Expand All @@ -109,15 +109,15 @@ func newNodeHandleOwnedFromMerkleValue[H hash.Hash, Hasher hash.Hasher[H]](
if err != nil {
return nil, err
}
return NodeHandleOwnedInline[H]{nodeOwned}, nil
return InlineCachedNodeHandle[H]{nodeOwned}, nil
default:
panic("unreachable")
}
}

type child[H any] struct {
nibble *uint8
NodeHandleOwned
CachedNodeHandle
}

// Cached nodes interface constraint.
Expand Down Expand Up @@ -147,7 +147,7 @@ type (
// and an optional value.
BranchCachedNode[H any] struct {
PartialKey nibbles.NibbleSlice
Children [codec.ChildrenCapacity]NodeHandleOwned // can be nil to represent no child
Children [codec.ChildrenCapacity]CachedNodeHandle // can be nil to represent no child
Value CachedNodeValue[H]
}
// Node that represents a value.
Expand Down Expand Up @@ -188,8 +188,8 @@ func (no BranchCachedNode[H]) children() []child[H] { //nolint:unused
if ch != nil {
nibble := uint8(i) //nolint:gosec
r = append(r, child[H]{
nibble: &nibble,
NodeHandleOwned: ch,
nibble: &nibble,
CachedNodeHandle: ch,
})
}
}
Expand Down Expand Up @@ -247,13 +247,13 @@ func newCachedNodeFromNode[H hash.Hash, Hasher hash.Hasher[H]](n codec.EncodedNo
Value: newCachedNodeValueFromEncodedValue[H, Hasher](n.Value),
}, nil
case codec.Branch:
var childrenOwned [codec.ChildrenCapacity]NodeHandleOwned
var childrenOwned [codec.ChildrenCapacity]CachedNodeHandle
for i, child := range n.Children {
if child == nil {
continue
}
var err error
childrenOwned[i], err = newNodeHandleOwnedFromMerkleValue[H, Hasher](child)
childrenOwned[i], err = newCachedNodeHandleFromMerkleValue[H, Hasher](child)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/trie/triedb/node_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ func newNodeHandleFromMerkleValue[H hash.Hash](
}
}

func newNodeHandleFromNodeHandleOwned[H hash.Hash](
child NodeHandleOwned,
func newNodeHandleFromCachedNodeHandle[H hash.Hash](
child CachedNodeHandle,
storage *nodeStorage[H],
) NodeHandle {
switch child := child.(type) {
case NodeHandleOwnedHash[H]:
case HashCachedNodeHandle[H]:
return persisted[H]{child.Hash}
case NodeHandleOwnedInline[H]:
case InlineCachedNodeHandle[H]:
ch := newNodeFromCachedNode(child.CachedNode, storage)
return inMemory(storage.alloc(NewStoredNode{node: ch}))
default:
Expand Down
4 changes: 2 additions & 2 deletions pkg/trie/triedb/triedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -1053,8 +1053,8 @@ func cacheChildValues[H hash.Hash](
fullKey nibbles.NibbleSlice,
) {
for _, child := range node.children() {
switch nho := child.NodeHandleOwned.(type) {
case NodeHandleOwnedInline[H]:
switch nho := child.CachedNodeHandle.(type) {
case InlineCachedNodeHandle[H]:
n := child.nibble
c := nho.CachedNode
key := fullKey.Clone()
Expand Down

0 comments on commit bd25322

Please sign in to comment.