Skip to content

Commit

Permalink
chore: remove deltas to tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
EclesioMeloJunior committed Feb 7, 2024
1 parent c88b51f commit 8cce14e
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 68 deletions.
50 changes: 2 additions & 48 deletions internal/trie/tracking/deltas.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,16 @@

package tracking

import (
"github.com/ChainSafe/gossamer/lib/common"
)

type DeltaEntry struct {
Key []byte
Value []byte
}
import "github.com/ChainSafe/gossamer/lib/common"

// Deltas tracks the trie deltas, for example deleted node hashes.
type Deltas struct {
entries []DeltaEntry
deletedNodeHashes map[common.Hash]struct{}
}

// New returns a new Deltas struct.
func New() *Deltas {
return &Deltas{
entries: make([]DeltaEntry, 0),
deletedNodeHashes: make(map[common.Hash]struct{}),
}
}
Expand All @@ -31,43 +22,19 @@ func (d *Deltas) RecordDeleted(nodeHash common.Hash) {
d.deletedNodeHashes[nodeHash] = struct{}{}
}

// RecordUpdated records a node hash that was created or updated.
func (d *Deltas) RecordUpdated(key, value []byte) {
newEntry := DeltaEntry{
Key: make([]byte, len(key)),
Value: make([]byte, len(value)),
}

copy(newEntry.Key[:], key[:])
copy(newEntry.Value[:], value[:])
d.entries = append(d.entries, newEntry)
}

func (d *Deltas) HasUpdated(partialKeyHash common.Hash) bool {
return false
}

// Deleted returns a set (map) of all the recorded deleted
// node hashes. Note the map returned is not deep copied for
// performance reasons and so it's not safe for mutation.
func (d *Deltas) Deleted() (nodeHashes map[common.Hash]struct{}) {
return d.deletedNodeHashes
}

func (d *Deltas) Updated() []DeltaEntry {
return d.entries
}

// MergeWith merges the deltas given as argument in the receiving
// deltas struct.
func (d *Deltas) MergeWith(deltas Getter) {
func (d *Deltas) MergeWith(deltas DeletedGetter) {
for nodeHash := range deltas.Deleted() {
d.RecordDeleted(nodeHash)
}

for _, deltaEntry := range deltas.Updated() {
d.RecordUpdated(deltaEntry.Key, deltaEntry.Value)
}
}

// DeepCopy returns a deep copy of the deltas.
Expand All @@ -85,18 +52,5 @@ func (d *Deltas) DeepCopy() (deepCopy *Deltas) {
}
}

if len(d.entries) != 0 {
deepCopy.entries = make([]DeltaEntry, len(d.entries))
for idx, deltaEntry := range d.entries {
newEntry := DeltaEntry{
Key: make([]byte, len(deltaEntry.Key)),
Value: make([]byte, len(deltaEntry.Value)),
}

copy(newEntry.Key[:], deltaEntry.Key[:])
copy(newEntry.Value[:], deltaEntry.Value[:])
deepCopy.entries[idx] = newEntry
}
}
return deepCopy
}
2 changes: 1 addition & 1 deletion internal/trie/tracking/deltas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func Test_Deltas_MergeWith(t *testing.T) {

testCases := map[string]struct {
deltas Deltas
deltasArg Getter
deltasArg DeletedGetter
expectedDeltas Deltas
}{
"merge_empty_deltas": {
Expand Down
5 changes: 2 additions & 3 deletions internal/trie/tracking/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ package tracking

import "github.com/ChainSafe/gossamer/lib/common"

// Getter gets deleted node hashes.
type Getter interface {
// DeletedGetter gets deleted node hashes.
type DeletedGetter interface {
Deleted() (nodeHashes map[common.Hash]struct{})
Updated() []DeltaEntry
}
5 changes: 2 additions & 3 deletions lib/trie/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ import (
// the last snapshot.
type Deltas interface {
DeltaMerger
tracking.Getter
tracking.DeletedGetter
DeltaRecorder
DeepCopy() (deepCopy *tracking.Deltas)
}

// DeltaMerger merges the given deltas into the current
// deltas.
type DeltaMerger interface {
MergeWith(deltas tracking.Getter)
MergeWith(deltas tracking.DeletedGetter)
}

// DeltaRecorder records deltas done in a ongoing trie operation.
type DeltaRecorder interface {
RecordDeleted(nodeHash common.Hash)
RecordUpdated(key, value []byte)
}
3 changes: 1 addition & 2 deletions lib/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (t *Trie) Snapshot() (newTrie *Trie) {

// handleTrackedDeltas sets the pending deleted node hashes in
// the trie deltas tracker if and only if success is true.
func (t *Trie) handleTrackedDeltas(success bool, pendingDeltas tracking.Getter) {
func (t *Trie) handleTrackedDeltas(success bool, pendingDeltas tracking.DeletedGetter) {

Check failure on line 104 in lib/trie/trie.go

View workflow job for this annotation

GitHub Actions / linting

`(*Trie).handleTrackedDeltas` - `success` is unused (unparam)
// if !success || t.generation == 0 {
// // Do not persist tracked deleted node hashes if the operation failed or
// // if the trie generation is zero (first block, no trie snapshot done yet).
Expand Down Expand Up @@ -360,7 +360,6 @@ func (t *Trie) Put(keyLE, value []byte) (err error) {
t.handleTrackedDeltas(success, pendingDeltas)
}()

pendingDeltas.RecordUpdated(keyLE, value)
err = t.insertKeyLE(keyLE, value, pendingDeltas)
if err != nil {
return err
Expand Down
12 changes: 1 addition & 11 deletions lib/trie/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package trie
import (
"bytes"
"encoding/hex"
"fmt"
"reflect"
"testing"

Expand Down Expand Up @@ -111,7 +110,7 @@ func Test_Trie_handleTrackedDeltas(t *testing.T) {
testCases := map[string]struct {
trie Trie
success bool
pendingDeltas tracking.Getter
pendingDeltas tracking.DeletedGetter
expectedTrie Trie
}{
"no_success_and_generation_1": {
Expand Down Expand Up @@ -4372,12 +4371,3 @@ func Benchmark_concatSlices(b *testing.B) {
}
})
}

func TestTrieDeltaRegisterUpdates(t *testing.T) {
trie := NewEmptyTrie()
trie.Put([]byte("alpha"), make([]byte, 40))
trie.Put([]byte("al"), make([]byte, 40))

fmt.Println("tries deltas updated", trie.deltas.Updated())
fmt.Println("tries deltas deleted", trie.deltas.Deleted())
}

0 comments on commit 8cce14e

Please sign in to comment.