forked from okx/xlayer-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MT optimization: do not write nodes to DB until tx is commited (0xPol…
…ygonHermez#532) * Changes for new MT spec and golden poseidon * SC code hash working * Add MT node cache * Added rollback test
- Loading branch information
Showing
8 changed files
with
632 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package tree | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hermeznetwork/hermez-core/test/testutils" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMTNodeCacheGet(t *testing.T) { | ||
tcs := []struct { | ||
description string | ||
data map[string][]uint64 | ||
key [][]uint64 | ||
expected [][]uint64 | ||
expectedErr bool | ||
expectedErrMsg string | ||
}{ | ||
{ | ||
description: "single matching item", | ||
data: map[string][]uint64{ | ||
"0x0000000000000001000000000000000100000000000000010000000000000001": {15}, | ||
}, | ||
key: [][]uint64{{1, 1, 1, 1}}, | ||
expected: [][]uint64{{15}}, | ||
}, | ||
{ | ||
description: "single non-matching item", | ||
data: map[string][]uint64{ | ||
"0x0000000000000001000000000000000100000000000000010000000000000001": {15}, | ||
}, | ||
key: [][]uint64{{1, 1, 1, 0}}, | ||
expectedErr: true, | ||
expectedErrMsg: errMTNodeCacheItemNotFound.Error(), | ||
}, | ||
{ | ||
description: "multiple matching items", | ||
data: map[string][]uint64{ | ||
"0x0000000000000001000000000000000100000000000000010000000000000001": {15}, | ||
"0x0000000000000001000000000000000100000000000000010000000000000002": {16}, | ||
"0x0000000000000001000000000000000100000000000000010000000000000003": {17}, | ||
}, | ||
key: [][]uint64{{1, 1, 1, 1}, {2, 1, 1, 1}, {3, 1, 1, 1}}, | ||
expected: [][]uint64{{15}, {16}, {17}}, | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
tc := tc | ||
t.Run(tc.description, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
subject := newNodeCache() | ||
|
||
subject.data = tc.data | ||
|
||
for i := 0; i < len(tc.key); i++ { | ||
actual, err := subject.get(tc.key[i]) | ||
require.NoError(t, testutils.CheckError(err, tc.expectedErr, tc.expectedErrMsg)) | ||
|
||
if !tc.expectedErr { | ||
require.Equal(t, tc.expected[i], actual) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestMTNodeCacheSet(t *testing.T) { | ||
tcs := []struct { | ||
description string | ||
key [][]uint64 | ||
value [][]uint64 | ||
expectedData map[string][]uint64 | ||
expectedErr bool | ||
expectedErrMsg string | ||
}{ | ||
{ | ||
description: "single item set", | ||
key: [][]uint64{{1, 1, 1, 1}}, | ||
value: [][]uint64{{15}}, | ||
expectedData: map[string][]uint64{ | ||
"0x0000000000000001000000000000000100000000000000010000000000000001": {15}, | ||
}, | ||
}, | ||
{ | ||
description: "mutiple items set", | ||
key: [][]uint64{{1, 1, 1, 1}, {1, 1, 1, 2}, {1, 2, 1, 3}}, | ||
value: [][]uint64{{15}, {16}, {17}}, | ||
expectedData: map[string][]uint64{ | ||
"0x0000000000000001000000000000000100000000000000010000000000000001": {15}, | ||
"0x0000000000000002000000000000000100000000000000010000000000000001": {16}, | ||
"0x0000000000000003000000000000000100000000000000020000000000000001": {17}, | ||
}, | ||
}, | ||
{ | ||
description: "keys can be updated", | ||
key: [][]uint64{{1, 1, 1, 1}, {1, 1, 1, 2}, {1, 1, 1, 1}}, | ||
value: [][]uint64{{15}, {16}, {1500}}, | ||
expectedData: map[string][]uint64{ | ||
"0x0000000000000001000000000000000100000000000000010000000000000001": {1500}, | ||
"0x0000000000000002000000000000000100000000000000010000000000000001": {16}, | ||
}, | ||
}, | ||
{ | ||
description: "key length != 4 causes error", | ||
key: [][]uint64{{1, 1, 1}}, | ||
value: [][]uint64{{15}}, | ||
expectedErr: true, | ||
expectedErrMsg: "Invalid key length, should be 4", | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
tc := tc | ||
t.Run(tc.description, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
subject := newNodeCache() | ||
for i := 0; i < len(tc.key); i++ { | ||
err := subject.set(tc.key[i], tc.value[i]) | ||
require.NoError(t, testutils.CheckError(err, tc.expectedErr, tc.expectedErrMsg)) | ||
} | ||
if !tc.expectedErr { | ||
require.Equal(t, tc.expectedData, subject.data) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestMTNodeCacheMaxItems(t *testing.T) { | ||
subject := newNodeCache() | ||
for i := 0; i < maxMTNodeCacheEntries; i++ { | ||
err := subject.set([]uint64{1, 1, 1, uint64(i)}, []uint64{1}) | ||
require.NoError(t, err) | ||
} | ||
|
||
err := subject.set([]uint64{1, 1, 1, uint64(maxMTNodeCacheEntries + 1)}, []uint64{1}) | ||
require.Error(t, err) | ||
|
||
require.Equal(t, "MT node cache is full", err.Error()) | ||
} | ||
|
||
func TestMTNodeCacheClear(t *testing.T) { | ||
subject := newNodeCache() | ||
for i := 0; i < 5; i++ { | ||
err := subject.set([]uint64{1, 1, 1, uint64(i)}, []uint64{1}) | ||
require.NoError(t, err) | ||
} | ||
|
||
subject.clear() | ||
|
||
require.Zero(t, len(subject.data)) | ||
} |
Oops, something went wrong.