Skip to content

Commit

Permalink
remove unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
timwu20 committed Oct 4, 2024
1 parent e4699f8 commit 895baf8
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 550 deletions.
164 changes: 0 additions & 164 deletions pkg/trie/triedb/codec/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package codec

import (
"bytes"
"fmt"
"io"

Expand Down Expand Up @@ -172,166 +171,3 @@ func EncodeHeader(partialKey []byte, partialKeyLength uint, kind NodeKind, write

return nil
}

type ValueOwnedTypes[H any] interface {
ValueOwnedInline[H] | ValueOwnedNode[H]
ValueOwned
}
type ValueOwned interface {
isValueOwned()
}

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

func (ValueOwnedInline[H]) isValueOwned() {}
func (ValueOwnedNode[H]) isValueOwned() {}

var (
_ ValueOwned = ValueOwnedInline[string]{}
_ ValueOwned = ValueOwnedNode[string]{}
)

func ValueOwnedFromEncodedValue[H hash.Hash, Hasher hash.Hasher[H]](encVal EncodedValue) ValueOwned {
switch encVal := encVal.(type) {
case InlineValue:
return ValueOwnedInline[H]{
Value: encVal,
Hash: (*(new(Hasher))).Hash(encVal),
}
case HashedValue[H]:
return ValueOwnedNode[H](encVal)
default:
panic("unreachable")
}
}

type NodeHandleOwnedTypes[H any] interface {
NodeHandleOwnedHash[H] | NodeHandleOwnedInline[H]
}

type NodeHandleOwned interface {
isNodeHandleOwned()
}

type (
NodeHandleOwnedHash[H any] struct {
Hash H
}
NodeHandleOwnedInline[H any] struct {
NodeOwned
}
)

func (NodeHandleOwnedHash[H]) isNodeHandleOwned() {}
func (NodeHandleOwnedInline[H]) isNodeHandleOwned() {}

var (
_ NodeHandleOwned = NodeHandleOwnedHash[string]{}
_ NodeHandleOwned = NodeHandleOwnedInline[string]{}
)

func NodeHandleOwnedFromMerkleValue[H hash.Hash, Hasher hash.Hasher[H]](mv MerkleValue) (NodeHandleOwned, error) {
switch mv := mv.(type) {
case HashedNode[H]:
return NodeHandleOwnedHash[H](mv), nil
case InlineNode:
buf := bytes.NewBuffer(mv)
node, err := Decode[H](buf)
if err != nil {
return nil, err
}
nodeOwned, err := NodeOwnedFromNode[H, Hasher](node)
if err != nil {
return nil, err
}
return NodeHandleOwnedInline[H]{nodeOwned}, nil
default:
panic("unreachable")
}
}

type NodeOwnedTypes[H any] interface {
NodeOwnedEmpty | NodeOwnedLeaf[H] | NodeOwnedBranch[H] | NodeOwnedValue[H]
NodeOwned
}
type NodeOwned interface {
isNodeOwned()
}

type (
// Null trie node; could be an empty root or an empty branch entry.
NodeOwnedEmpty struct{}
// Leaf node; has key slice and value. Value may not be empty.
NodeOwnedLeaf[H any] struct {
PartialKey nibbles.Nibbles
Value ValueOwned
}
// Branch node; has slice of child nodes (each possibly null)
// and an optional immediate node data.
NodeOwnedBranch[H any] struct {
PartialKey nibbles.Nibbles
Children [ChildrenCapacity]NodeHandleOwned // can be nil to represent None()
Value ValueOwned
}
// Node that represents a value.
//
// This variant is only constructed when working with a [`crate::TrieCache`]. It is only
// used to cache a raw value.
NodeOwnedValue[H any] struct {
Value []byte
Hash H
}
)

func (NodeOwnedEmpty) isNodeOwned() {}
func (NodeOwnedLeaf[H]) isNodeOwned() {}
func (NodeOwnedBranch[H]) isNodeOwned() {}
func (NodeOwnedValue[H]) isNodeOwned() {}

var (
_ NodeOwned = NodeOwnedEmpty{}
_ NodeOwned = NodeOwnedLeaf[string]{}
_ NodeOwned = NodeOwnedBranch[string]{}
_ NodeOwned = NodeOwnedValue[string]{}
)

func NodeOwnedFromNode[H hash.Hash, Hasher hash.Hasher[H]](n EncodedNode) (NodeOwned, error) {
switch n := n.(type) {
case Empty:
return NodeOwnedEmpty{}, nil
case Leaf:
return NodeOwnedLeaf[H]{
PartialKey: n.PartialKey,
Value: ValueOwnedFromEncodedValue[H, Hasher](n.Value),
}, nil
case Branch:
var childrenOwned [ChildrenCapacity]NodeHandleOwned
for i, child := range n.Children {
if child == nil {
continue
}
var err error
childrenOwned[i], err = NodeHandleOwnedFromMerkleValue[H, Hasher](child)
if err != nil {
return nil, err
}
}
return NodeOwnedBranch[H]{
PartialKey: n.PartialKey,
Children: childrenOwned,
Value: ValueOwnedFromEncodedValue[H, Hasher](n.Value),
}, nil
default:
panic("unreachable")
}
}
4 changes: 2 additions & 2 deletions pkg/trie/triedb/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func (ri *rawIterator[H, Hasher]) nextRawItem(fwd bool) (*rawItem[H], error) {
case statusAftExiting:
ri.trail = ri.trail[:len(ri.trail)-1]
if len(ri.trail) > 0 {
crumb := &ri.trail[len(ri.trail)-1] //nolint:govet
crumb := &ri.trail[len(ri.trail)-1]
crumb.step(fwd)
}
case statusExiting:
Expand All @@ -383,7 +383,7 @@ func (ri *rawIterator[H, Hasher]) nextRawItem(fwd bool) (*rawItem[H], error) {
default:
panic("unreachable")
}
crumb := &ri.trail[len(ri.trail)-1] //nolint:govet
crumb := &ri.trail[len(ri.trail)-1]
crumb.step(fwd)
if !fwd {
return &rawItem[H]{ri.keyNibbles, crumb.hash, crumb.node}, nil
Expand Down
Loading

0 comments on commit 895baf8

Please sign in to comment.