2023-01-07 11:25:28 +00:00
|
|
|
package merkle_tree_test
|
|
|
|
|
|
|
|
import (
|
2023-05-13 21:44:07 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/phase1/core/state/state_encoding"
|
2023-01-07 11:25:28 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-01-07 11:25:28 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/merkle_tree"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEmptyArraysRoot(t *testing.T) {
|
2023-01-13 18:12:18 +00:00
|
|
|
expected := libcommon.HexToHash("df6af5f5bbdb6be9ef8aa618e4bf8073960867171e29676f8b284dea6a08a85e")
|
2023-01-07 11:25:28 +00:00
|
|
|
root, err := merkle_tree.ArraysRoot([][32]byte{}, state_encoding.StateRootsLength)
|
|
|
|
require.NoError(t, err)
|
2023-01-13 18:12:18 +00:00
|
|
|
require.Equal(t, expected, libcommon.Hash(root))
|
2023-01-07 11:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyArraysWithLengthRoot(t *testing.T) {
|
2023-01-13 18:12:18 +00:00
|
|
|
expected := libcommon.HexToHash("0xf770287da731841c38eb035da016bd2daad53bf0bca607461c0685b0ea54c5f9")
|
2023-01-07 11:25:28 +00:00
|
|
|
roots := [][32]byte{
|
2023-01-13 18:12:18 +00:00
|
|
|
libcommon.BytesToHash([]byte{1}),
|
|
|
|
libcommon.BytesToHash([]byte{2}),
|
|
|
|
libcommon.BytesToHash([]byte{3}),
|
|
|
|
libcommon.BytesToHash([]byte{4}),
|
|
|
|
libcommon.BytesToHash([]byte{5}),
|
|
|
|
libcommon.BytesToHash([]byte{6}),
|
|
|
|
libcommon.BytesToHash([]byte{7}),
|
|
|
|
libcommon.BytesToHash([]byte{8}),
|
2023-01-07 11:25:28 +00:00
|
|
|
}
|
|
|
|
root, err := merkle_tree.ArraysRootWithLimit(roots, 8192)
|
|
|
|
require.NoError(t, err)
|
2023-01-13 18:12:18 +00:00
|
|
|
require.Equal(t, expected, libcommon.Hash(root))
|
2023-01-07 11:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUint64ListRootWithLimit(t *testing.T) {
|
2023-01-13 18:12:18 +00:00
|
|
|
expected := libcommon.HexToHash("0xfbe583f8fbcc3683d98c12ae969e93aaa5ac472e15422c14759cb7f3ef60673c")
|
2023-01-07 11:25:28 +00:00
|
|
|
nums := []uint64{1, 2, 4, 5, 2, 5, 6, 7, 1, 4, 3, 5, 100, 6, 64, 2}
|
|
|
|
root, err := merkle_tree.Uint64ListRootWithLimit(nums, 274877906944)
|
|
|
|
require.NoError(t, err)
|
2023-01-13 18:12:18 +00:00
|
|
|
require.Equal(t, expected, libcommon.Hash(root))
|
2023-01-07 11:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParticipationBitsRoot(t *testing.T) {
|
2023-01-13 18:12:18 +00:00
|
|
|
expected := libcommon.HexToHash("0x8e6653ba3656afddaf5e6c69c149e63a2e26ff91a2e361b3c40b11f08c039572")
|
2023-01-07 11:25:28 +00:00
|
|
|
bits := []byte{1, 2, 4, 5, 2, 5, 6, 7, 1, 4, 3, 5, 100, 6, 64, 2}
|
|
|
|
root, err := merkle_tree.BitlistRootWithLimitForState(bits, 1099511627776)
|
|
|
|
require.NoError(t, err)
|
2023-01-13 18:12:18 +00:00
|
|
|
require.Equal(t, expected, libcommon.Hash(root))
|
2023-01-07 11:25:28 +00:00
|
|
|
}
|