2021-09-21 15:02:48 +00:00
|
|
|
package ssz_test
|
2020-06-18 02:15:13 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/crypto/hash"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/encoding/ssz"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/assert"
|
2020-06-18 02:15:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetDepth(t *testing.T) {
|
|
|
|
trieSize := uint64(896745231)
|
|
|
|
expected := uint8(30)
|
|
|
|
|
2021-09-21 15:02:48 +00:00
|
|
|
result := ssz.Depth(trieSize)
|
2020-07-19 21:08:29 +00:00
|
|
|
assert.Equal(t, expected, result)
|
2020-06-18 02:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMerkleizeCountGreaterThanLimit(t *testing.T) {
|
2021-09-21 15:02:48 +00:00
|
|
|
hashFn := ssz.NewHasherFunc(hash.CustomSHA256Hasher())
|
2020-06-18 02:15:13 +00:00
|
|
|
count := uint64(2)
|
|
|
|
limit := uint64(1)
|
|
|
|
chunks := [][]byte{{}}
|
|
|
|
leafIndexer := func(i uint64) []byte {
|
|
|
|
return chunks[i]
|
|
|
|
}
|
|
|
|
// Error if no panic
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r == nil {
|
|
|
|
t.Errorf("The code did not panic.")
|
|
|
|
}
|
|
|
|
}()
|
2021-09-21 15:02:48 +00:00
|
|
|
ssz.Merkleize(hashFn, count, limit, leafIndexer)
|
2020-06-18 02:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMerkleizeLimitAndCountAreZero(t *testing.T) {
|
2021-09-21 15:02:48 +00:00
|
|
|
hashFn := ssz.NewHasherFunc(hash.CustomSHA256Hasher())
|
2020-06-18 02:15:13 +00:00
|
|
|
count := uint64(0)
|
|
|
|
limit := uint64(0)
|
|
|
|
chunks := [][]byte{{}}
|
|
|
|
leafIndexer := func(i uint64) []byte {
|
|
|
|
return chunks[i]
|
|
|
|
}
|
|
|
|
expected := [32]byte{}
|
2021-09-21 15:02:48 +00:00
|
|
|
result := ssz.Merkleize(hashFn, count, limit, leafIndexer)
|
2020-07-19 21:08:29 +00:00
|
|
|
assert.Equal(t, expected, result)
|
2020-06-18 02:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMerkleizeNormalPath(t *testing.T) {
|
2021-09-21 15:02:48 +00:00
|
|
|
hashFn := ssz.NewHasherFunc(hash.CustomSHA256Hasher())
|
2020-06-18 02:15:13 +00:00
|
|
|
count := uint64(2)
|
|
|
|
limit := uint64(3)
|
|
|
|
chunks := [][]byte{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}
|
|
|
|
leafIndexer := func(i uint64) []byte {
|
|
|
|
return chunks[i]
|
|
|
|
}
|
|
|
|
expected := [32]byte{95, 27, 253, 237, 215, 58, 147, 198, 175, 194, 180, 231, 154, 130, 205, 68, 146, 112, 225, 86, 6, 103, 186, 82, 7, 142, 33, 189, 174, 56, 199, 173}
|
2021-09-21 15:02:48 +00:00
|
|
|
result := ssz.Merkleize(hashFn, count, limit, leafIndexer)
|
2020-07-19 21:08:29 +00:00
|
|
|
assert.Equal(t, expected, result)
|
2020-06-18 02:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConstructProofCountGreaterThanLimit(t *testing.T) {
|
2021-09-21 15:02:48 +00:00
|
|
|
hashFn := ssz.NewHasherFunc(hash.CustomSHA256Hasher())
|
2020-06-18 02:15:13 +00:00
|
|
|
count := uint64(2)
|
|
|
|
limit := uint64(1)
|
|
|
|
chunks := [][]byte{{}}
|
|
|
|
leafIndexer := func(i uint64) []byte {
|
|
|
|
return chunks[i]
|
|
|
|
}
|
|
|
|
index := uint64(0)
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r == nil {
|
|
|
|
t.Errorf("The code did not panic.")
|
|
|
|
}
|
|
|
|
}()
|
2021-09-21 15:02:48 +00:00
|
|
|
ssz.ConstructProof(hashFn, count, limit, leafIndexer, index)
|
2020-06-18 02:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConstructProofIndexGreaterThanEqualToLimit(t *testing.T) {
|
2021-09-21 15:02:48 +00:00
|
|
|
hashFn := ssz.NewHasherFunc(hash.CustomSHA256Hasher())
|
2020-06-18 02:15:13 +00:00
|
|
|
count := uint64(1)
|
|
|
|
limit := uint64(1)
|
|
|
|
chunks := [][]byte{{}}
|
|
|
|
leafIndexer := func(i uint64) []byte {
|
|
|
|
return chunks[i]
|
|
|
|
}
|
|
|
|
index := uint64(1)
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r == nil {
|
|
|
|
t.Errorf("The code did not panic.")
|
|
|
|
}
|
|
|
|
}()
|
2021-09-21 15:02:48 +00:00
|
|
|
ssz.ConstructProof(hashFn, count, limit, leafIndexer, index)
|
2020-06-18 02:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConstructProofNormalPath(t *testing.T) {
|
2021-09-21 15:02:48 +00:00
|
|
|
hashFn := ssz.NewHasherFunc(hash.CustomSHA256Hasher())
|
2020-06-18 02:15:13 +00:00
|
|
|
count := uint64(2)
|
|
|
|
limit := uint64(3)
|
|
|
|
chunks := [][]byte{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}
|
|
|
|
leafIndexer := func(i uint64) []byte {
|
|
|
|
return chunks[i]
|
|
|
|
}
|
|
|
|
index := uint64(1)
|
|
|
|
expected := [][32]byte{
|
|
|
|
{1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
{245, 165, 253, 66, 209, 106, 32, 48, 39, 152, 239, 110, 211, 9, 151, 155, 67, 0, 61, 35, 32, 217, 240, 232, 234, 152, 49, 169, 39, 89, 251, 75},
|
|
|
|
}
|
2021-09-21 15:02:48 +00:00
|
|
|
result := ssz.ConstructProof(hashFn, count, limit, leafIndexer, index)
|
2020-07-19 21:08:29 +00:00
|
|
|
assert.Equal(t, len(expected), len(result))
|
2020-06-18 02:15:13 +00:00
|
|
|
for i, v := range expected {
|
2020-07-19 21:08:29 +00:00
|
|
|
assert.DeepEqual(t, result[i], v)
|
2020-06-18 02:15:13 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-23 12:44:04 +00:00
|
|
|
|
|
|
|
func TestDepthOfOne(t *testing.T) {
|
|
|
|
assert.Equal(t, uint8(0), ssz.Depth(1))
|
|
|
|
}
|