-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
354812c
commit dc26e41
Showing
13 changed files
with
287 additions
and
43 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
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
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,102 @@ | ||
package hashesCollector | ||
|
||
import ( | ||
"strconv" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/multiversx/mx-chain-core-go/core/check" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewDataTrieHashesCollector(t *testing.T) { | ||
t.Parallel() | ||
|
||
hc := NewDataTrieHashesCollector() | ||
assert.False(t, check.IfNil(hc)) | ||
assert.NotNil(t, hc.oldHashes) | ||
assert.NotNil(t, hc.newHashes) | ||
} | ||
|
||
func TestDataTrieHashesCollector_AddDirtyHash(t *testing.T) { | ||
t.Parallel() | ||
|
||
dthc := NewDataTrieHashesCollector() | ||
numHashes := 1000 | ||
wg := &sync.WaitGroup{} | ||
wg.Add(numHashes) | ||
for i := 0; i < numHashes; i++ { | ||
go func(index int) { | ||
dthc.AddDirtyHash([]byte(strconv.Itoa(index))) | ||
wg.Done() | ||
}(i) | ||
} | ||
wg.Wait() | ||
|
||
for i := 0; i < numHashes; i++ { | ||
_, ok := dthc.newHashes[strconv.Itoa(i)] | ||
assert.True(t, ok) | ||
} | ||
} | ||
|
||
func TestDataTrieHashesCollector_GetDirtyHashes(t *testing.T) { | ||
t.Parallel() | ||
|
||
dthc := NewDataTrieHashesCollector() | ||
numHashes := 1000 | ||
for i := 0; i < numHashes; i++ { | ||
dthc.AddDirtyHash([]byte(strconv.Itoa(i))) | ||
} | ||
|
||
dirtyHashes := dthc.GetDirtyHashes() | ||
assert.Equal(t, numHashes, len(dirtyHashes)) | ||
for i := 0; i < numHashes; i++ { | ||
_, ok := dirtyHashes[strconv.Itoa(i)] | ||
assert.True(t, ok) | ||
} | ||
} | ||
|
||
func TestDataTrieHashesCollector_AddObsoleteHashes(t *testing.T) { | ||
t.Parallel() | ||
|
||
dthc := NewDataTrieHashesCollector() | ||
numHashes := 1000 | ||
dirtyHashes := make([][]byte, numHashes) | ||
|
||
for i := 0; i < numHashes; i++ { | ||
dirtyHashes[i] = []byte(strconv.Itoa(i)) | ||
} | ||
|
||
dthc.AddObsoleteHashes(nil, dirtyHashes) | ||
|
||
assert.Equal(t, numHashes, len(dthc.oldHashes)) | ||
for i := 0; i < numHashes; i++ { | ||
_, ok := dthc.oldHashes[strconv.Itoa(i)] | ||
assert.True(t, ok) | ||
} | ||
} | ||
|
||
func TestDataTriehashesCollector_GetCollectedData(t *testing.T) { | ||
t.Parallel() | ||
|
||
dthc := NewDataTrieHashesCollector() | ||
numHashes := 1000 | ||
dirtyHashes := make([][]byte, numHashes) | ||
|
||
for i := 0; i < numHashes; i++ { | ||
dirtyHashes[i] = []byte(strconv.Itoa(i)) | ||
dthc.AddDirtyHash(dirtyHashes[i]) | ||
} | ||
dthc.AddObsoleteHashes(nil, dirtyHashes) | ||
|
||
oldRootHash, oldHashes, newHashes := dthc.GetCollectedData() | ||
assert.Nil(t, oldRootHash) | ||
assert.Equal(t, numHashes, len(oldHashes)) | ||
assert.Equal(t, numHashes, len(newHashes)) | ||
for i := 0; i < numHashes; i++ { | ||
_, ok := oldHashes[strconv.Itoa(i)] | ||
assert.True(t, ok) | ||
_, ok = newHashes[strconv.Itoa(i)] | ||
assert.True(t, ok) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package hashesCollector | ||
|
||
// GetOldRootHash - | ||
func (hc *hashesCollector) GetOldRootHash() []byte { | ||
return hc.oldRootHash | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package hashesCollector_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/multiversx/mx-chain-core-go/core/check" | ||
"github.com/multiversx/mx-chain-go/common" | ||
"github.com/multiversx/mx-chain-go/state/hashesCollector" | ||
"github.com/multiversx/mx-chain-go/testscommon/trie" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewHashesCollector(t *testing.T) { | ||
t.Parallel() | ||
|
||
hc, err := hashesCollector.NewHashesCollector(nil) | ||
assert.True(t, check.IfNil(hc)) | ||
assert.Equal(t, hashesCollector.ErrNilTrieHashesCollector, err) | ||
|
||
hc, err = hashesCollector.NewHashesCollector(&trie.TrieHashesCollectorStub{}) | ||
assert.False(t, check.IfNil(hc)) | ||
assert.Nil(t, err) | ||
assert.Nil(t, hc.GetOldRootHash()) | ||
} | ||
|
||
func TestHashesCollector_AddObsoleteHashes(t *testing.T) { | ||
t.Parallel() | ||
|
||
addObsoleteHashesCalled := false | ||
oldRootHash := []byte("oldRootHash") | ||
oldHashes := [][]byte{[]byte("oldHash1"), []byte("oldHash2")} | ||
hc := &trie.TrieHashesCollectorStub{ | ||
AddObsoleteHashesCalled: func(oldRootHash []byte, oldHashes [][]byte) { | ||
assert.Equal(t, oldRootHash, oldRootHash) | ||
assert.Equal(t, oldHashes, oldHashes) | ||
addObsoleteHashesCalled = true | ||
}, | ||
} | ||
wrappedHc, _ := hashesCollector.NewHashesCollector(hc) | ||
|
||
wrappedHc.AddObsoleteHashes(oldRootHash, oldHashes) | ||
assert.True(t, addObsoleteHashesCalled) | ||
assert.Equal(t, oldRootHash, wrappedHc.GetOldRootHash()) | ||
} | ||
|
||
func TestHashesCollector_GetCollectedData(t *testing.T) { | ||
t.Parallel() | ||
|
||
getCollectedDataCalled := false | ||
oldHashes := common.ModifiedHashes{"oldHash1": {}, "oldHash2": {}} | ||
newHashes := common.ModifiedHashes{"newHash1": {}, "newHash2": {}} | ||
hc := &trie.TrieHashesCollectorStub{ | ||
GetCollectedDataCalled: func() ([]byte, common.ModifiedHashes, common.ModifiedHashes) { | ||
getCollectedDataCalled = true | ||
return []byte("oldRootHash"), oldHashes, newHashes | ||
}, | ||
} | ||
wrappedHc, _ := hashesCollector.NewHashesCollector(hc) | ||
|
||
oldRootHash, collectedOldHashes, collectedNewHashes := wrappedHc.GetCollectedData() | ||
assert.True(t, getCollectedDataCalled) | ||
assert.Nil(t, oldRootHash) | ||
assert.Equal(t, oldHashes, collectedOldHashes) | ||
assert.Equal(t, newHashes, collectedNewHashes) | ||
|
||
wrappedHc.AddObsoleteHashes([]byte("oldRootHash1"), [][]byte{[]byte("oldHash1"), []byte("oldHash2")}) | ||
oldRootHash, collectedOldHashes, collectedNewHashes = wrappedHc.GetCollectedData() | ||
assert.Equal(t, []byte("oldRootHash1"), oldRootHash) | ||
assert.Equal(t, oldHashes, collectedOldHashes) | ||
assert.Equal(t, newHashes, collectedNewHashes) | ||
} |
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,46 @@ | ||
package trie | ||
|
||
import "github.com/multiversx/mx-chain-go/common" | ||
|
||
// TrieHashesCollectorStub is a stub for the TrieHashesCollector interface. | ||
type TrieHashesCollectorStub struct { | ||
AddDirtyHashCalled func(hash []byte) | ||
GetDirtyHashesCalled func() common.ModifiedHashes | ||
AddObsoleteHashesCalled func(oldRootHash []byte, oldHashes [][]byte) | ||
GetCollectedDataCalled func() ([]byte, common.ModifiedHashes, common.ModifiedHashes) | ||
} | ||
|
||
// AddDirtyHash - | ||
func (h *TrieHashesCollectorStub) AddDirtyHash(hash []byte) { | ||
if h.AddDirtyHashCalled != nil { | ||
h.AddDirtyHashCalled(hash) | ||
} | ||
} | ||
|
||
// GetDirtyHashes - | ||
func (h *TrieHashesCollectorStub) GetDirtyHashes() common.ModifiedHashes { | ||
if h.GetDirtyHashesCalled != nil { | ||
return h.GetDirtyHashesCalled() | ||
} | ||
return nil | ||
} | ||
|
||
// AddObsoleteHashes - | ||
func (h *TrieHashesCollectorStub) AddObsoleteHashes(oldRootHash []byte, oldHashes [][]byte) { | ||
if h.AddObsoleteHashesCalled != nil { | ||
h.AddObsoleteHashesCalled(oldRootHash, oldHashes) | ||
} | ||
} | ||
|
||
// GetCollectedData - | ||
func (h *TrieHashesCollectorStub) GetCollectedData() ([]byte, common.ModifiedHashes, common.ModifiedHashes) { | ||
if h.GetCollectedDataCalled != nil { | ||
return h.GetCollectedDataCalled() | ||
} | ||
return nil, nil, nil | ||
} | ||
|
||
// IsInterfaceNil - | ||
func (h *TrieHashesCollectorStub) IsInterfaceNil() bool { | ||
return h == nil | ||
} |
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
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.