Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(lib/trie): lib/trie/recorder sub-package #2082

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/trie/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,27 @@ import (

"github.com/ChainSafe/gossamer/lib/trie/branch"
"github.com/ChainSafe/gossamer/lib/trie/node"
"github.com/ChainSafe/gossamer/lib/trie/record"
)

var _ recorder = (*record.Recorder)(nil)

type recorder interface {
Record(hash, rawData []byte)
}

// findAndRecord search for a desired key recording all the nodes in the path including the desired node
func findAndRecord(t *Trie, key []byte, recorder *recorder) error {
func findAndRecord(t *Trie, key []byte, recorder recorder) error {
return find(t.root, key, recorder)
}

func find(parent node.Node, key []byte, recorder *recorder) error {
func find(parent node.Node, key []byte, recorder recorder) error {
enc, hash, err := parent.EncodeAndHash()
if err != nil {
return err
}

recorder.record(hash, enc)
recorder.Record(hash, enc)

b, ok := parent.(*branch.Branch)
if !ok {
Expand Down
14 changes: 6 additions & 8 deletions lib/trie/proof.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

package trie

import (
Expand All @@ -12,6 +9,7 @@ import (
"github.com/ChainSafe/chaindb"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/trie/decode"
"github.com/ChainSafe/gossamer/lib/trie/record"
)

var (
Expand Down Expand Up @@ -43,17 +41,17 @@ func GenerateProof(root []byte, keys [][]byte, db chaindb.Database) ([][]byte, e
for _, k := range keys {
nk := decode.KeyLEToNibbles(k)

recorder := new(recorder)
recorder := record.NewRecorder()
err := findAndRecord(proofTrie, nk, recorder)
if err != nil {
return nil, err
}

for !recorder.isEmpty() {
recNode := recorder.next()
nodeHashHex := common.BytesToHex(recNode.hash)
for !recorder.IsEmpty() {
recNode := recorder.Next()
nodeHashHex := common.BytesToHex(recNode.Hash)
if _, ok := trackedProofs[nodeHashHex]; !ok {
trackedProofs[nodeHashHex] = recNode.rawData
trackedProofs[nodeHashHex] = recNode.RawData
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions lib/trie/record/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package record

// Node represents a record of a visited node
type Node struct {
RawData []byte
Hash []byte
}
38 changes: 38 additions & 0 deletions lib/trie/record/recorder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

package record

// Recorder records the list of nodes found by Lookup.Find
type Recorder struct {
nodes []Node
}

// NewRecorder creates a new recorder.
func NewRecorder() *Recorder {
return &Recorder{}
}

// Record appends a node to the list of visited nodes.
func (r *Recorder) Record(hash, rawData []byte) {
r.nodes = append(r.nodes, Node{RawData: rawData, Hash: hash})
}

// Next returns the first node in the recorded list
// and removes it (shift operation).
func (r *Recorder) Next() (node *Node) {
qdm12 marked this conversation as resolved.
Show resolved Hide resolved
if len(r.nodes) == 0 {
return nil
}

node = &r.nodes[0]
r.nodes = r.nodes[1:]

return node
}

// IsEmpty returns true if no node is in in the current
// recorded list of nodes.
func (r *Recorder) IsEmpty() (empty bool) {
return len(r.nodes) == 0
}
157 changes: 157 additions & 0 deletions lib/trie/record/recorder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package record

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_NewRecorder(t *testing.T) {
t.Parallel()

expected := &Recorder{}

recorder := NewRecorder()

assert.Equal(t, expected, recorder)
}

func Test_Recorder_Record(t *testing.T) {
testCases := map[string]struct {
recorder *Recorder
hash []byte
rawData []byte
expectedRecorder *Recorder
}{
"nil data": {
recorder: &Recorder{},
expectedRecorder: &Recorder{
nodes: []Node{
{},
},
},
},
"insert in empty recorder": {
recorder: &Recorder{},
hash: []byte{1, 2},
rawData: []byte{3, 4},
expectedRecorder: &Recorder{
nodes: []Node{
{Hash: []byte{1, 2}, RawData: []byte{3, 4}},
},
},
},
"insert in non-empty recorder": {
recorder: &Recorder{
nodes: []Node{
{Hash: []byte{5, 6}, RawData: []byte{7, 8}},
},
},
hash: []byte{1, 2},
rawData: []byte{3, 4},
expectedRecorder: &Recorder{
nodes: []Node{
{Hash: []byte{5, 6}, RawData: []byte{7, 8}},
{Hash: []byte{1, 2}, RawData: []byte{3, 4}},
},
},
},
}

for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()

testCase.recorder.Record(testCase.hash, testCase.rawData)

assert.Equal(t, testCase.expectedRecorder, testCase.recorder)
})
}
}

func Test_Recorder_Next(t *testing.T) {
testCases := map[string]struct {
recorder *Recorder
node *Node
expectedRecorder *Recorder
}{
"no node": {
recorder: &Recorder{},
expectedRecorder: &Recorder{},
},
"get single node from recorder": {
recorder: &Recorder{
nodes: []Node{
{Hash: []byte{1, 2}, RawData: []byte{3, 4}},
},
},
node: &Node{Hash: []byte{1, 2}, RawData: []byte{3, 4}},
expectedRecorder: &Recorder{
nodes: []Node{},
},
},
"get node from multiple nodes in recorder": {
recorder: &Recorder{
nodes: []Node{
{Hash: []byte{1, 2}, RawData: []byte{3, 4}},
{Hash: []byte{5, 6}, RawData: []byte{7, 8}},
{Hash: []byte{9, 6}, RawData: []byte{7, 8}},
},
},
node: &Node{Hash: []byte{1, 2}, RawData: []byte{3, 4}},
expectedRecorder: &Recorder{
nodes: []Node{
{Hash: []byte{5, 6}, RawData: []byte{7, 8}},
{Hash: []byte{9, 6}, RawData: []byte{7, 8}},
},
},
},
}

for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()

node := testCase.recorder.Next()

assert.Equal(t, testCase.node, node)
assert.Equal(t, testCase.expectedRecorder, testCase.recorder)
})
}
}

func Test_Recorder_IsEmpty(t *testing.T) {
testCases := map[string]struct {
recorder *Recorder
empty bool
}{
"nil nodes": {
recorder: &Recorder{},
empty: true,
},
"empty nodes": {
recorder: &Recorder{
nodes: []Node{},
},
empty: true,
},
"non-empty nodes": {
recorder: &Recorder{
nodes: []Node{{}},
},
},
}

for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()

empty := testCase.recorder.IsEmpty()

assert.Equal(t, testCase.empty, empty)
})
}
}
34 changes: 0 additions & 34 deletions lib/trie/recorder.go

This file was deleted.