prysm-pulse/encoding/ssz/merkleize_test.go
Raul Jordan d077483577
Add V3 Suffix to All Prysm Packages (#11083)
* v3 import renamings

* tidy

* fmt

* rev

* Update beacon-chain/core/epoch/precompute/reward_penalty_test.go

* Update beacon-chain/core/helpers/validators_test.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/iface/BUILD.bazel

* Update beacon-chain/db/kv/kv.go

* Update beacon-chain/db/kv/state.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/sync/initial-sync/service.go

* fix deps

* fix bad replacements

* fix bad replacements

* change back

* gohashtree version

* fix deps

Co-authored-by: Nishant Das <nishdas93@gmail.com>
Co-authored-by: Potuz <potuz@prysmaticlabs.com>
2022-08-16 12:20:13 +00:00

119 lines
3.3 KiB
Go

package ssz_test
import (
"testing"
"github.com/prysmaticlabs/prysm/v3/crypto/hash"
"github.com/prysmaticlabs/prysm/v3/encoding/ssz"
"github.com/prysmaticlabs/prysm/v3/testing/assert"
)
func TestGetDepth(t *testing.T) {
trieSize := uint64(896745231)
expected := uint8(30)
result := ssz.Depth(trieSize)
assert.Equal(t, expected, result)
}
func TestMerkleizeCountGreaterThanLimit(t *testing.T) {
hashFn := ssz.NewHasherFunc(hash.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.")
}
}()
ssz.Merkleize(hashFn, count, limit, leafIndexer)
}
func TestMerkleizeLimitAndCountAreZero(t *testing.T) {
hashFn := ssz.NewHasherFunc(hash.CustomSHA256Hasher())
count := uint64(0)
limit := uint64(0)
chunks := [][]byte{{}}
leafIndexer := func(i uint64) []byte {
return chunks[i]
}
expected := [32]byte{}
result := ssz.Merkleize(hashFn, count, limit, leafIndexer)
assert.Equal(t, expected, result)
}
func TestMerkleizeNormalPath(t *testing.T) {
hashFn := ssz.NewHasherFunc(hash.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 := ssz.Merkleize(hashFn, count, limit, leafIndexer)
assert.Equal(t, expected, result)
}
func TestConstructProofCountGreaterThanLimit(t *testing.T) {
hashFn := ssz.NewHasherFunc(hash.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.")
}
}()
ssz.ConstructProof(hashFn, count, limit, leafIndexer, index)
}
func TestConstructProofIndexGreaterThanEqualToLimit(t *testing.T) {
hashFn := ssz.NewHasherFunc(hash.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.")
}
}()
ssz.ConstructProof(hashFn, count, limit, leafIndexer, index)
}
func TestConstructProofNormalPath(t *testing.T) {
hashFn := ssz.NewHasherFunc(hash.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 := ssz.ConstructProof(hashFn, count, limit, leafIndexer, index)
assert.Equal(t, len(expected), len(result))
for i, v := range expected {
assert.DeepEqual(t, result[i], v)
}
}
func TestDepthOfOne(t *testing.T) {
assert.Equal(t, uint8(0), ssz.Depth(1))
}