Skip to content

Commit

Permalink
update all generic param H to hash.Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
timwu20 committed Oct 11, 2024
1 parent 27b7829 commit 1689aac
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 30 deletions.
24 changes: 12 additions & 12 deletions pkg/trie/triedb/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type CachedNodeValueTypes[H hash.Hash] interface {
}

// Value representation used in [CachedNode]
type CachedNodeValue[H any] interface {
type CachedNodeValue[H hash.Hash] interface {
data() []byte // nil means there is no data
dataHash() *H
EncodedValue() codec.EncodedValue
Expand Down Expand Up @@ -116,7 +116,7 @@ func newCachedNodeHandleFromMerkleValue[H hash.Hash, Hasher hash.Hasher[H]](
}
}

type child[H any] struct {
type child[H hash.Hash] struct {
nibble *uint8
CachedNodeHandle
}
Expand All @@ -128,7 +128,7 @@ type CachedNodeTypes[H hash.Hash] interface {
}

// Cached nodes.
type CachedNode[H any] interface {
type CachedNode[H hash.Hash] interface {
data() []byte // nil means there is no data
dataHash() *H
children() []child[H]
Expand All @@ -140,13 +140,13 @@ type (
// Empty trie node; could be an empty root or an empty branch entry.
EmptyCachedNode[H hash.Hash] struct{}
// Leaf node; has key slice and value. Value may not be empty.
LeafCachedNode[H any] struct {
LeafCachedNode[H hash.Hash] struct {
PartialKey nibbles.NibbleSlice
Value CachedNodeValue[H]
}
// Branch node; has slice of child nodes (each possibly null)
// and an optional value.
BranchCachedNode[H any] struct {
BranchCachedNode[H hash.Hash] struct {
PartialKey nibbles.NibbleSlice
Children [codec.ChildrenCapacity]CachedNodeHandle // can be nil to represent no child
Value CachedNodeValue[H]
Expand All @@ -155,7 +155,7 @@ type (
//
// This variant is only constructed when working with a [TrieCache]. It is only
// used to cache a raw value.
ValueCachedNode[H any] struct {
ValueCachedNode[H hash.Hash] struct {
Value []byte
Hash H
}
Expand Down Expand Up @@ -270,38 +270,38 @@ func newCachedNodeFromNode[H hash.Hash, Hasher hash.Hasher[H]](n codec.EncodedNo
}

// The values cached by [TrieCache].
type CachedValues[H any] interface {
type CachedValues[H hash.Hash] interface {
NonExistingCachedValue[H] | ExistingHashCachedValue[H] | ExistingCachedValue[H]
CachedValue[H]
}

// A value cached by [TrieCache].
type CachedValue[H any] interface {
type CachedValue[H hash.Hash] interface {
data() []byte
hash() *H
}

// Constructor for [CachedValue]
func NewCachedValue[H any, CV CachedValues[H]](cv CV) CachedValue[H] {
func NewCachedValue[H hash.Hash, CV CachedValues[H]](cv CV) CachedValue[H] {
return cv
}

// The value doesn't exist in the trie.
type NonExistingCachedValue[H any] struct{}
type NonExistingCachedValue[H hash.Hash] struct{}

func (NonExistingCachedValue[H]) data() []byte { return nil } //nolint:unused
func (NonExistingCachedValue[H]) hash() *H { return nil } //nolint:unused

// The hash is cached and not the data because it was not accessed.
type ExistingHashCachedValue[H any] struct {
type ExistingHashCachedValue[H hash.Hash] struct {
Hash H
}

func (ExistingHashCachedValue[H]) data() []byte { return nil } //nolint:unused
func (ehcv ExistingHashCachedValue[H]) hash() *H { return &ehcv.Hash } //nolint:unused

// The value exists in the trie.
type ExistingCachedValue[H any] struct {
type ExistingCachedValue[H hash.Hash] struct {
// The hash of the value.
Hash H
// The actual data of the value.
Expand Down
2 changes: 1 addition & 1 deletion pkg/trie/triedb/codec/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type (
// InlineNode contains bytes of the encoded node data
InlineNode []byte
// HashedNode contains a hash used to lookup in db for encoded node data
HashedNode[H any] struct{ Hash H }
HashedNode[H hash.Hash] struct{ Hash H }
)

func (InlineNode) IsHashed() bool { return false }
Expand Down
2 changes: 1 addition & 1 deletion pkg/trie/triedb/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type extractedKey struct {
Value codec.EncodedValue
}

type rawItem[H any] struct {
type rawItem[H hash.Hash] struct {
nibbles.NibbleSlice
hash *H
codec.EncodedNode
Expand Down
2 changes: 1 addition & 1 deletion pkg/trie/triedb/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ func lookupWithoutCache[H hash.Hash, Hasher hash.Hasher[H], QueryItem, R any](
}
}

type valueHash[H any] struct {
type valueHash[H hash.Hash] struct {
Value []byte
Hash H
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/trie/triedb/node_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type NodeHandle interface {
}

type (
inMemory storageHandle
persisted[H any] struct{ hash H }
inMemory storageHandle
persisted[H hash.Hash] struct{ hash H }
)

func (inMemory) isNodeHandle() {}
Expand Down Expand Up @@ -77,7 +77,7 @@ type (
NewStoredNode struct {
node Node
}
CachedStoredNode[H any] struct {
CachedStoredNode[H hash.Hash] struct {
node Node
hash H
}
Expand All @@ -92,12 +92,12 @@ func (n CachedStoredNode[H]) getNode() Node {

// nodeStorage is a struct that contains all the temporal nodes that are stored
// in the trieDB before being written to the backed db
type nodeStorage[H any] struct {
type nodeStorage[H hash.Hash] struct {
nodes []StoredNode
freeIndices *deque.Deque[int]
}

func newNodeStorage[H any]() nodeStorage[H] {
func newNodeStorage[H hash.Hash]() nodeStorage[H] {
return nodeStorage[H]{
nodes: make([]StoredNode, 0),
freeIndices: deque.New[int](0),
Expand Down
2 changes: 1 addition & 1 deletion pkg/trie/triedb/proof/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type nodeHandle interface {
}

type (
nodeHandleHash[H any] struct {
nodeHandleHash[H hash.Hash] struct {
hash H
}
nodeHandleInline []byte
Expand Down
16 changes: 8 additions & 8 deletions pkg/trie/triedb/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package triedb

import "github.com/ChainSafe/gossamer/pkg/trie/triedb/hash"

// Used to report the trie access to the [TrieRecorder].
//
// As the trie can use a [TrieCache], there are multiple kinds of accesses.
Expand All @@ -12,12 +14,12 @@ type TrieAccess interface {

type (
// The given [CachedNode] was accessed using its hash.
CachedNodeAccess[H any] struct {
CachedNodeAccess[H hash.Hash] struct {
Hash H
Node CachedNode[H]
}
// The given EncodedNode was accessed using its hash.
EncodedNodeAccess[H any] struct {
EncodedNodeAccess[H hash.Hash] struct {
Hash H
EncodedNode []byte
}
Expand All @@ -26,7 +28,7 @@ type (
// The given FullKey is the key to access this value in the trie.
//
// Should map to [RecordedValue] when checking the recorder.
ValueAccess[H any] struct {
ValueAccess[H hash.Hash] struct {
Hash H
Value []byte
FullKey []byte
Expand Down Expand Up @@ -108,19 +110,19 @@ const (
)

// The record of a visited node.
type Record[H any] struct {
type Record[H hash.Hash] struct {
Hash H
Data []byte
}

// Records trie nodes as they pass it.
type Recorder[H any] struct {
type Recorder[H hash.Hash] struct {
nodes []Record[H]
recordedKeys map[string]RecordedForKey
}

// Constructor for [Recorder]
func NewRecorder[H any]() *Recorder[H] {
func NewRecorder[H hash.Hash]() *Recorder[H] {
return &Recorder[H]{
nodes: []Record[H]{},
recordedKeys: make(map[string]RecordedForKey),
Expand Down Expand Up @@ -172,5 +174,3 @@ func (r *Recorder[H]) TrieNodesRecordedForKey(key []byte) RecordedForKey {
}
return rfk
}

var _ TrieRecorder = &Recorder[string]{}
2 changes: 1 addition & 1 deletion pkg/trie/triedb/triedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ func (t *TrieDB[H, Hasher]) commitChild(
}
}

type valueToCache[H any] struct {
type valueToCache[H hash.Hash] struct {
KeyBytes []byte
CachedValue[H]
}
Expand Down

0 comments on commit 1689aac

Please sign in to comment.