-
Notifications
You must be signed in to change notification settings - Fork 140
chore(lib/trie): add Deltas
in internal/trie/tracking
#2896
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2022 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package tracking | ||
|
||
import "github.com/ChainSafe/gossamer/lib/common" | ||
|
||
// Deltas tracks the trie deltas, for example deleted node hashes. | ||
type Deltas struct { | ||
deletedNodeHashes map[common.Hash]struct{} | ||
} | ||
|
||
// New returns a new Deltas struct. | ||
func New() *Deltas { | ||
return &Deltas{ | ||
deletedNodeHashes: make(map[common.Hash]struct{}), | ||
} | ||
} | ||
|
||
// RecordDeleted records a node hash as deleted. | ||
func (d *Deltas) RecordDeleted(nodeHash common.Hash) { | ||
d.deletedNodeHashes[nodeHash] = struct{}{} | ||
} | ||
|
||
// 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 | ||
} | ||
|
||
// MergeWith merges the deltas given as argument in the receiving | ||
// deltas struct. | ||
func (d *Deltas) MergeWith(deltas DeletedGetter) { | ||
for nodeHash := range deltas.Deleted() { | ||
d.RecordDeleted(nodeHash) | ||
} | ||
} | ||
|
||
// DeepCopy returns a deep copy of the deltas. | ||
func (d *Deltas) DeepCopy() (deepCopy *Deltas) { | ||
if d == nil { | ||
return nil | ||
} | ||
|
||
deepCopy = &Deltas{} | ||
|
||
if d.deletedNodeHashes != nil { | ||
deepCopy.deletedNodeHashes = make(map[common.Hash]struct{}, len(d.deletedNodeHashes)) | ||
for nodeHash := range d.deletedNodeHashes { | ||
deepCopy.deletedNodeHashes[nodeHash] = struct{}{} | ||
} | ||
} | ||
|
||
return deepCopy | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
// Copyright 2022 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package tracking | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ChainSafe/gossamer/lib/common" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_New(t *testing.T) { | ||
t.Parallel() | ||
|
||
deltas := New() | ||
|
||
expectedDeltas := &Deltas{ | ||
deletedNodeHashes: make(map[common.Hash]struct{}), | ||
} | ||
assert.Equal(t, expectedDeltas, deltas) | ||
} | ||
|
||
func Test_Deltas_RecordDeleted(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
deltas Deltas | ||
nodeHash common.Hash | ||
expectedDeltas Deltas | ||
}{ | ||
"set_in_empty_deltas": { | ||
deltas: Deltas{ | ||
deletedNodeHashes: map[common.Hash]struct{}{}, | ||
}, | ||
nodeHash: common.Hash{1}, | ||
expectedDeltas: Deltas{ | ||
deletedNodeHashes: map[common.Hash]struct{}{{1}: {}}, | ||
}, | ||
}, | ||
"set_in_non_empty_deltas": { | ||
deltas: Deltas{ | ||
deletedNodeHashes: map[common.Hash]struct{}{{1}: {}}, | ||
}, | ||
nodeHash: common.Hash{2}, | ||
expectedDeltas: Deltas{ | ||
deletedNodeHashes: map[common.Hash]struct{}{ | ||
{1}: {}, {2}: {}, | ||
}, | ||
}, | ||
}, | ||
"override_in_deltas": { | ||
deltas: Deltas{ | ||
deletedNodeHashes: map[common.Hash]struct{}{{1}: {}}, | ||
}, | ||
nodeHash: common.Hash{1}, | ||
expectedDeltas: Deltas{ | ||
deletedNodeHashes: map[common.Hash]struct{}{{1}: {}}, | ||
}, | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCase.deltas.RecordDeleted(testCase.nodeHash) | ||
assert.Equal(t, testCase.expectedDeltas, testCase.deltas) | ||
}) | ||
} | ||
} | ||
|
||
func Test_Deltas_Deleted(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
deltas Deltas | ||
nodeHashes map[common.Hash]struct{} | ||
}{ | ||
"empty_deltas": {}, | ||
"non_empty_deltas": { | ||
deltas: Deltas{ | ||
deletedNodeHashes: map[common.Hash]struct{}{{1}: {}}, | ||
}, | ||
nodeHashes: map[common.Hash]struct{}{{1}: {}}, | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
nodeHashes := testCase.deltas.Deleted() | ||
assert.Equal(t, testCase.nodeHashes, nodeHashes) | ||
}) | ||
} | ||
} | ||
|
||
func Test_Deltas_MergeWith(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
deltas Deltas | ||
deltasArg DeletedGetter | ||
expectedDeltas Deltas | ||
}{ | ||
"merge_empty_deltas": { | ||
deltas: Deltas{ | ||
deletedNodeHashes: map[common.Hash]struct{}{{1}: {}}, | ||
}, | ||
deltasArg: &Deltas{}, | ||
expectedDeltas: Deltas{ | ||
deletedNodeHashes: map[common.Hash]struct{}{{1}: {}}, | ||
}, | ||
}, | ||
"merge_deltas": { | ||
deltas: Deltas{ | ||
deletedNodeHashes: map[common.Hash]struct{}{{1}: {}}, | ||
}, | ||
deltasArg: &Deltas{ | ||
deletedNodeHashes: map[common.Hash]struct{}{ | ||
{1}: {}, {2}: {}, | ||
}, | ||
}, | ||
expectedDeltas: Deltas{ | ||
deletedNodeHashes: map[common.Hash]struct{}{ | ||
{1}: {}, {2}: {}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCase.deltas.MergeWith(testCase.deltasArg) | ||
assert.Equal(t, testCase.expectedDeltas, testCase.deltas) | ||
}) | ||
} | ||
} | ||
|
||
func Test_Deltas_DeepCopy(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
deltasOriginal *Deltas | ||
deltasCopy *Deltas | ||
}{ | ||
"nil_deltas": {}, | ||
"empty_deltas": { | ||
deltasOriginal: &Deltas{}, | ||
deltasCopy: &Deltas{}, | ||
}, | ||
"filled_deltas": { | ||
deltasOriginal: &Deltas{ | ||
deletedNodeHashes: map[common.Hash]struct{}{{1}: {}}, | ||
}, | ||
deltasCopy: &Deltas{ | ||
deletedNodeHashes: map[common.Hash]struct{}{{1}: {}}, | ||
}, | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
deepCopy := testCase.deltasOriginal.DeepCopy() | ||
|
||
assert.Equal(t, testCase.deltasCopy, deepCopy) | ||
assertPointersNotEqual(t, testCase.deltasOriginal, deepCopy) | ||
if testCase.deltasOriginal != nil { | ||
assertPointersNotEqual(t, testCase.deltasOriginal.deletedNodeHashes, deepCopy.deletedNodeHashes) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2022 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package tracking | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func getPointer(x interface{}) (pointer uintptr, ok bool) { | ||
func() { | ||
defer func() { | ||
ok = recover() == nil | ||
}() | ||
valueOfX := reflect.ValueOf(x) | ||
pointer = valueOfX.Pointer() | ||
}() | ||
return pointer, ok | ||
} | ||
|
||
func assertPointersNotEqual(t *testing.T, a, b interface{}) { | ||
t.Helper() | ||
pointerA, okA := getPointer(a) | ||
pointerB, okB := getPointer(b) | ||
require.Equal(t, okA, okB) | ||
|
||
switch { | ||
case pointerA == 0 && pointerB == 0: // nil and nil | ||
case okA: | ||
assert.NotEqual(t, pointerA, pointerB) | ||
default: // values like `int` | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright 2022 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package tracking | ||
|
||
import "github.com/ChainSafe/gossamer/lib/common" | ||
|
||
// DeletedGetter gets deleted node hashes. | ||
type DeletedGetter interface { | ||
Deleted() (nodeHashes map[common.Hash]struct{}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.