-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Define `Deltas` struct in `internal/trie/tracking` (with unit tests) - Replace passing of a plain Go `map[string]struct{}` by passing `pendingDeltas DeltaRecorder` in the trie code - Define trie local deltas interfaces
- Loading branch information
Showing
10 changed files
with
653 additions
and
344 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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.