Skip to content

Commit

Permalink
revise CachedNodeValue variants
Browse files Browse the repository at this point in the history
  • Loading branch information
timwu20 committed Oct 11, 2024
1 parent 5a16de9 commit 6895e2e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
26 changes: 13 additions & 13 deletions pkg/trie/triedb/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (l *TrieLookup[H, Hasher, QueryItem]) lookupWithCache(
}

var lookupData = func() ([]byte, error) {
data, err := lookupWithCacheInternal[H, Hasher](l, fullKey, nibbleKey, l.cache, loadValueOwned[H])
data, err := lookupWithCacheInternal[H, Hasher](l, fullKey, nibbleKey, l.cache, loadCachedNodeValue[H])
if err != nil {
return nil, err
}
Expand All @@ -125,10 +125,10 @@ func (l *TrieLookup[H, Hasher, QueryItem]) lookupWithCache(
case NonExistingCachedValue[H]:
res = nil
case ExistingHashCachedValue[H]:
data, err := loadValueOwned[H](
data, err := loadCachedNodeValue[H](
// If we only have the hash cached, this can only be a value node.
// For inline nodes we cache them directly as [ExistingCachedValue].
ValueOwnedNode[H](cachedVal),
NodeCachedNodeValue[H](cachedVal),
nibbleKey.OriginalDataPrefix(),
fullKey,
l.cache,
Expand Down Expand Up @@ -185,8 +185,8 @@ func (l *TrieLookup[H, Hasher, QueryItem]) lookupWithCache(
return nil, nil
}

type loadValueOwnedFunc[H hash.Hash, R any] func(
v ValueOwned[H],
type loadCachedNodeValueFunc[H hash.Hash, R any] func(
v CachedNodeValue[H],
prefix nibbles.Prefix,
fullKey []byte,
cache TrieCache[H],
Expand All @@ -201,7 +201,7 @@ func lookupWithCacheInternal[H hash.Hash, Hasher hash.Hasher[H], R, QueryItem an
fullKey []byte,
nibbleKey nibbles.Nibbles,
cache TrieCache[H],
loadValue loadValueOwnedFunc[H, R],
loadValue loadCachedNodeValueFunc[H, R],
) (*R, error) {
partial := nibbleKey
hash := l.hash
Expand Down Expand Up @@ -447,21 +447,21 @@ func (vh *valueHash[H]) CachedValue() CachedValue[H] {
// into the given cache as [ValueCachedNode].
//
// Returns the bytes representing the value and its hash.
func loadValueOwned[H hash.Hash](
v ValueOwned[H],
func loadCachedNodeValue[H hash.Hash](
v CachedNodeValue[H],
prefix nibbles.Prefix,
fullKey []byte,
cache TrieCache[H],
db db.DBGetter,
recorder TrieRecorder,
) (valueHash[H], error) {
switch v := v.(type) {
case ValueOwnedInline[H]:
case InlineCachedNodeValue[H]:
if recorder != nil {
recorder.Record(InlineValueAccess{fullKey})
}
return valueHash[H](v), nil
case ValueOwnedNode[H]:
case NodeCachedNodeValue[H]:
node, err := cache.GetOrInsertNode(v.Hash, func() (CachedNode[H], error) {
prefixedKey := append(prefix.JoinedBytes(), v.Hash.Bytes()...)
val, err := db.Get(prefixedKey)
Expand Down Expand Up @@ -613,22 +613,22 @@ func (l *TrieLookup[H, Hasher, QueryItem]) lookupHashWithCache(
}

vh, err := lookupWithCacheInternal(l, fullKey, nibbleKey, l.cache, func(
value ValueOwned[H],
value CachedNodeValue[H],
_ nibbles.Prefix,
fullKey []byte,
_ TrieCache[H],
_ db.DBGetter,
recorder TrieRecorder,
) (valueHash[H], error) {
switch value := value.(type) {
case ValueOwnedInline[H]:
case InlineCachedNodeValue[H]:
if recorder != nil {
// We can record this as [InlineValueAccess], even we are just returning
// the hash. This is done to prevent requiring to re-record this key.
recorder.Record(InlineValueAccess{FullKey: fullKey})
}
return valueHash[H](value), nil
case ValueOwnedNode[H]:
case NodeCachedNodeValue[H]:
if recorder != nil {
recorder.Record(HashAccess{FullKey: fullKey})
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/trie/triedb/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ func NewValueFromEncoded[H hash.Hash](encodedValue codec.EncodedValue) nodeValue
return nil
}

func newValueFromValueOwned[H hash.Hash](val ValueOwned[H]) nodeValue {
func newValueFromCachedNodeValue[H hash.Hash](val CachedNodeValue[H]) nodeValue {
switch val := val.(type) {
case ValueOwnedInline[H]:
case InlineCachedNodeValue[H]:
return inline(val.Value)
case ValueOwnedNode[H]:
case NodeCachedNodeValue[H]:
return valueRef[H]{val.Hash}
default:
panic("unreachable")
Expand Down Expand Up @@ -246,7 +246,7 @@ func newNodeFromCachedNode[H hash.Hash](
leaf := nodeOwned
return Leaf[H]{
partialKey: leaf.PartialKey.NodeKey(),
value: newValueFromValueOwned[H](leaf.Value),
value: newValueFromCachedNodeValue[H](leaf.Value),
}
case BranchCachedNode[H]:
k := nodeOwned.PartialKey
Expand All @@ -267,7 +267,7 @@ func newNodeFromCachedNode[H hash.Hash](
}
var value nodeValue
if val != nil {
value = newValueFromValueOwned(val)
value = newValueFromCachedNodeValue(val)
}
return Branch[H]{
partialKey: k.NodeKey(),
Expand Down
40 changes: 21 additions & 19 deletions pkg/trie/triedb/node_owned.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,48 @@ import (
)

// Value representation used in [CachedNode] interface constraint
type ValueOwnedTypes[H hash.Hash] interface {
ValueOwnedInline[H] | ValueOwnedNode[H]
ValueOwned[H]
type CachedNodeValueTypes[H hash.Hash] interface {
InlineCachedNodeValue[H] | NodeCachedNodeValue[H]
CachedNodeValue[H]
}

// Value representation used in [CachedNode]
type ValueOwned[H any] interface {
type CachedNodeValue[H any] interface {
data() []byte // nil means there is no data
dataHash() *H
EncodedValue() codec.EncodedValue
}

type (
// Value bytes as stored in a trie node and its hash.
ValueOwnedInline[H hash.Hash] struct {
InlineCachedNodeValue[H hash.Hash] struct {
Value []byte
Hash H
}
// Hash stored in a trie node.
ValueOwnedNode[H hash.Hash] struct {
NodeCachedNodeValue[H hash.Hash] struct {
Hash H
}
)

func (vo ValueOwnedInline[H]) data() []byte { return vo.Value } //nolint:unused
func (vo ValueOwnedNode[H]) data() []byte { return nil } //nolint:unused
func (vo ValueOwnedInline[H]) dataHash() *H { return &vo.Hash } //nolint:unused
func (vo ValueOwnedNode[H]) dataHash() *H { return &vo.Hash } //nolint:unused
func (vo ValueOwnedInline[H]) EncodedValue() codec.EncodedValue { return codec.InlineValue(vo.Value) }
func (vo ValueOwnedNode[H]) EncodedValue() codec.EncodedValue { return codec.HashedValue[H](vo) }
func (vo InlineCachedNodeValue[H]) data() []byte { return vo.Value } //nolint:unused
func (vo NodeCachedNodeValue[H]) data() []byte { return nil } //nolint:unused
func (vo InlineCachedNodeValue[H]) dataHash() *H { return &vo.Hash } //nolint:unused
func (vo NodeCachedNodeValue[H]) dataHash() *H { return &vo.Hash } //nolint:unused
func (vo InlineCachedNodeValue[H]) EncodedValue() codec.EncodedValue {
return codec.InlineValue(vo.Value)
}
func (vo NodeCachedNodeValue[H]) EncodedValue() codec.EncodedValue { return codec.HashedValue[H](vo) }

func newValueOwnedFromEncodedValue[H hash.Hash, Hasher hash.Hasher[H]](encVal codec.EncodedValue) ValueOwned[H] {
func newCachedNodeValueFromEncodedValue[H hash.Hash, Hasher hash.Hasher[H]](encVal codec.EncodedValue) CachedNodeValue[H] {
switch encVal := encVal.(type) {
case codec.InlineValue:
return ValueOwnedInline[H]{
return InlineCachedNodeValue[H]{
Value: encVal,
Hash: (*(new(Hasher))).Hash(encVal),
}
case codec.HashedValue[H]:
return ValueOwnedNode[H](encVal)
return NodeCachedNodeValue[H](encVal)
case nil:
return nil
default:
Expand Down Expand Up @@ -139,14 +141,14 @@ type (
// Leaf node; has key slice and value. Value may not be empty.
LeafCachedNode[H any] struct {
PartialKey nibbles.NibbleSlice
Value ValueOwned[H]
Value CachedNodeValue[H]
}
// Branch node; has slice of child nodes (each possibly null)
// and an optional value.
BranchCachedNode[H any] struct {
PartialKey nibbles.NibbleSlice
Children [codec.ChildrenCapacity]NodeHandleOwned // can be nil to represent no child
Value ValueOwned[H]
Value CachedNodeValue[H]
}
// Node that represents a value.
//
Expand Down Expand Up @@ -242,7 +244,7 @@ func newCachedNodeFromNode[H hash.Hash, Hasher hash.Hasher[H]](n codec.EncodedNo
case codec.Leaf:
return LeafCachedNode[H]{
PartialKey: nibbles.NewNibbleSliceFromNibbles(n.PartialKey),
Value: newValueOwnedFromEncodedValue[H, Hasher](n.Value),
Value: newCachedNodeValueFromEncodedValue[H, Hasher](n.Value),
}, nil
case codec.Branch:
var childrenOwned [codec.ChildrenCapacity]NodeHandleOwned
Expand All @@ -259,7 +261,7 @@ func newCachedNodeFromNode[H hash.Hash, Hasher hash.Hasher[H]](n codec.EncodedNo
return BranchCachedNode[H]{
PartialKey: nibbles.NewNibbleSliceFromNibbles(n.PartialKey),
Children: childrenOwned,
Value: newValueOwnedFromEncodedValue[H, Hasher](n.Value),
Value: newCachedNodeValueFromEncodedValue[H, Hasher](n.Value),
}, nil
default:
panic("unreachable")
Expand Down

0 comments on commit 6895e2e

Please sign in to comment.