2020-06-18 02:15:13 +00:00
|
|
|
package htrutils_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/htrutils"
|
2020-07-19 21:08:29 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
2020-06-18 02:15:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetDepth(t *testing.T) {
|
|
|
|
trieSize := uint64(896745231)
|
|
|
|
expected := uint8(30)
|
|
|
|
|
2021-01-25 21:27:30 +00:00
|
|
|
result := htrutils.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) {
|
|
|
|
hashFn := htrutils.NewHasherFunc(hashutil.CustomSHA256Hasher())
|
|
|
|
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.")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
htrutils.Merkleize(hashFn, count, limit, leafIndexer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMerkleizeLimitAndCountAreZero(t *testing.T) {
|
|
|
|
hashFn := htrutils.NewHasherFunc(hashutil.CustomSHA256Hasher())
|
|
|
|
count := uint64(0)
|
|
|
|
limit := uint64(0)
|
|
|
|
chunks := [][]byte{{}}
|
|
|
|
leafIndexer := func(i uint64) []byte {
|
|
|
|
return chunks[i]
|
|
|
|
}
|
|
|
|
expected := [32]byte{}
|
|
|
|
result := htrutils.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) {
|
|
|
|
hashFn := htrutils.NewHasherFunc(hashutil.CustomSHA256Hasher())
|
|
|
|
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}
|
|
|
|
result := htrutils.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) {
|
|
|
|
hashFn := htrutils.NewHasherFunc(hashutil.CustomSHA256Hasher())
|
|
|
|
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.")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
htrutils.ConstructProof(hashFn, count, limit, leafIndexer, index)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConstructProofIndexGreaterThanEqualToLimit(t *testing.T) {
|
|
|
|
hashFn := htrutils.NewHasherFunc(hashutil.CustomSHA256Hasher())
|
|
|
|
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.")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
htrutils.ConstructProof(hashFn, count, limit, leafIndexer, index)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConstructProofNormalPath(t *testing.T) {
|
|
|
|
hashFn := htrutils.NewHasherFunc(hashutil.CustomSHA256Hasher())
|
|
|
|
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},
|
|
|
|
}
|
|
|
|
result := htrutils.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
|
|
|
}
|
|
|
|
}
|