mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-26 05:17:22 +00:00
b742511193
* validator count * fix build errors * fix test error * randao mixes * active indices * lastReceivedMerkleIndex * redundant conversions * eth1FollowDistance * refs * fix compile error * slasher * validator * revert changes to lastReceivedMerkleIndex * remove todo * fix references test * fix committee test * Revert "validator count" This reverts commit 19b376e39914b88b620a113215daa539e3e66758. # Conflicts: # beacon-chain/rpc/beacon/validators.go * Revert "fix build errors" This reverts commit f4acd6e9776c899a0523df10b64e55783b44c451. * Revert "fix test error" This reverts commit 2a5c9eec63e4546139220c71fe3c07ad92bed3b6. * Revert "randao mixes" This reverts commit 2863f9c24dd8cfc44ffce16321f68ef3b793e9a2. * Revert "active indices" This reverts commit 6e8385f3956a08ef9c8980b5343365a1bee48542. * Revert "refs" This reverts commit c64a153f67cd26daa58c3bf5f911da05d41cfb5d. * Revert "fix references test" This reverts commit fe773b55a6f8cc38c6c5c1f96615fdb8bbd4397d. * Revert "fix committee test" This reverts commit 7a0931c4487707e3eaf75a592415577f04253772. * fix compilation errors * Revert "slasher" This reverts commit 8b34137931cff1e6cdffeaf65e1ef07eefdea647. * trieutil * some int() simplifications * fix deepsource issues * removed redundant fmt.Sprint Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
260 lines
6.7 KiB
Go
260 lines
6.7 KiB
Go
package trieutil
|
|
|
|
import (
|
|
"strconv"
|
|
"testing"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
contracts "github.com/prysmaticlabs/prysm/contracts/deposit-contract"
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
)
|
|
|
|
func TestMarshalDepositWithProof(t *testing.T) {
|
|
items := [][]byte{
|
|
[]byte("A"),
|
|
[]byte("BB"),
|
|
[]byte("CCC"),
|
|
[]byte("DDDD"),
|
|
[]byte("EEEEE"),
|
|
[]byte("FFFFFF"),
|
|
[]byte("GGGGGGG"),
|
|
}
|
|
m, err := GenerateTrieFromItems(items, params.BeaconConfig().DepositContractTreeDepth)
|
|
require.NoError(t, err)
|
|
proof, err := m.MerkleProof(2)
|
|
require.NoError(t, err)
|
|
require.Equal(t, len(proof), int(params.BeaconConfig().DepositContractTreeDepth)+1)
|
|
someRoot := [32]byte{1, 2, 3, 4}
|
|
someSig := [96]byte{1, 2, 3, 4}
|
|
someKey := [48]byte{1, 2, 3, 4}
|
|
dep := ðpb.Deposit{
|
|
Proof: proof,
|
|
Data: ðpb.Deposit_Data{
|
|
PublicKey: someKey[:],
|
|
WithdrawalCredentials: someRoot[:],
|
|
Amount: 32,
|
|
Signature: someSig[:],
|
|
},
|
|
}
|
|
enc, err := dep.MarshalSSZ()
|
|
require.NoError(t, err)
|
|
dec := ðpb.Deposit{}
|
|
require.NoError(t, dec.UnmarshalSSZ(enc))
|
|
require.DeepEqual(t, dec, dep)
|
|
}
|
|
|
|
func TestMerkleTrie_MerkleProofOutOfRange(t *testing.T) {
|
|
h := hashutil.Hash([]byte("hi"))
|
|
m := &SparseMerkleTrie{
|
|
branches: [][][]byte{
|
|
{
|
|
h[:],
|
|
},
|
|
{
|
|
h[:],
|
|
},
|
|
{
|
|
[]byte{},
|
|
},
|
|
},
|
|
depth: 4,
|
|
}
|
|
if _, err := m.MerkleProof(6); err == nil {
|
|
t.Error("Expected out of range failure, received nil", err)
|
|
}
|
|
}
|
|
|
|
func TestMerkleTrieRoot_EmptyTrie(t *testing.T) {
|
|
trie, err := NewTrie(params.BeaconConfig().DepositContractTreeDepth)
|
|
require.NoError(t, err)
|
|
testAccount, err := contracts.Setup()
|
|
require.NoError(t, err)
|
|
|
|
depRoot, err := testAccount.Contract.GetDepositRoot(&bind.CallOpts{})
|
|
require.NoError(t, err)
|
|
require.DeepEqual(t, depRoot, trie.HashTreeRoot())
|
|
}
|
|
|
|
func TestGenerateTrieFromItems_NoItemsProvided(t *testing.T) {
|
|
if _, err := GenerateTrieFromItems(nil, params.BeaconConfig().DepositContractTreeDepth); err == nil {
|
|
t.Error("Expected error when providing nil items received nil")
|
|
}
|
|
}
|
|
|
|
func TestMerkleTrie_VerifyMerkleProof(t *testing.T) {
|
|
items := [][]byte{
|
|
[]byte("A"),
|
|
[]byte("B"),
|
|
[]byte("C"),
|
|
[]byte("D"),
|
|
[]byte("E"),
|
|
[]byte("F"),
|
|
[]byte("G"),
|
|
[]byte("H"),
|
|
}
|
|
m, err := GenerateTrieFromItems(items, params.BeaconConfig().DepositContractTreeDepth)
|
|
require.NoError(t, err)
|
|
proof, err := m.MerkleProof(0)
|
|
require.NoError(t, err)
|
|
require.Equal(t, int(params.BeaconConfig().DepositContractTreeDepth)+1, len(proof))
|
|
root := m.Root()
|
|
if ok := VerifyMerkleBranch(root[:], items[0], 0, proof, params.BeaconConfig().DepositContractTreeDepth); !ok {
|
|
t.Error("First Merkle proof did not verify")
|
|
}
|
|
proof, err = m.MerkleProof(3)
|
|
require.NoError(t, err)
|
|
require.Equal(t, true, VerifyMerkleBranch(root[:], items[3], 3, proof, params.BeaconConfig().DepositContractTreeDepth))
|
|
require.Equal(t, false, VerifyMerkleBranch(root[:], []byte("buzz"), 3, proof, params.BeaconConfig().DepositContractTreeDepth))
|
|
}
|
|
|
|
func TestMerkleTrie_VerifyMerkleProof_TrieUpdated(t *testing.T) {
|
|
items := [][]byte{
|
|
{1},
|
|
{2},
|
|
{3},
|
|
{4},
|
|
}
|
|
depth := params.BeaconConfig().DepositContractTreeDepth + 1
|
|
m, err := GenerateTrieFromItems(items, depth)
|
|
require.NoError(t, err)
|
|
proof, err := m.MerkleProof(0)
|
|
require.NoError(t, err)
|
|
root := m.Root()
|
|
require.Equal(t, true, VerifyMerkleBranch(root[:], items[0], 0, proof, depth))
|
|
|
|
// Now we update the trie.
|
|
m.Insert([]byte{5}, 3)
|
|
proof, err = m.MerkleProof(3)
|
|
require.NoError(t, err)
|
|
root = m.Root()
|
|
if ok := VerifyMerkleBranch(root[:], []byte{5}, 3, proof, depth); !ok {
|
|
t.Error("Second Merkle proof did not verify")
|
|
}
|
|
if ok := VerifyMerkleBranch(root[:], []byte{4}, 3, proof, depth); ok {
|
|
t.Error("Old item should not verify")
|
|
}
|
|
|
|
// Now we update the trie at an index larger than the number of items.
|
|
m.Insert([]byte{6}, 15)
|
|
}
|
|
|
|
func TestRoundtripProto_OK(t *testing.T) {
|
|
items := [][]byte{
|
|
{1},
|
|
{2},
|
|
{3},
|
|
{4},
|
|
}
|
|
m, err := GenerateTrieFromItems(items, params.BeaconConfig().DepositContractTreeDepth+1)
|
|
require.NoError(t, err)
|
|
|
|
protoTrie := m.ToProto()
|
|
depositRoot := m.HashTreeRoot()
|
|
|
|
newTrie := CreateTrieFromProto(protoTrie)
|
|
require.DeepEqual(t, depositRoot, newTrie.HashTreeRoot())
|
|
|
|
}
|
|
|
|
func TestCopy_OK(t *testing.T) {
|
|
items := [][]byte{
|
|
{1},
|
|
{2},
|
|
{3},
|
|
{4},
|
|
}
|
|
source, err := GenerateTrieFromItems(items, params.BeaconConfig().DepositContractTreeDepth+1)
|
|
require.NoError(t, err)
|
|
copiedTrie := source.Copy()
|
|
|
|
if copiedTrie == source {
|
|
t.Errorf("Original trie returned.")
|
|
}
|
|
copyHash := copiedTrie.HashTreeRoot()
|
|
require.DeepEqual(t, copyHash, copiedTrie.HashTreeRoot())
|
|
}
|
|
|
|
func BenchmarkGenerateTrieFromItems(b *testing.B) {
|
|
items := [][]byte{
|
|
[]byte("A"),
|
|
[]byte("BB"),
|
|
[]byte("CCC"),
|
|
[]byte("DDDD"),
|
|
[]byte("EEEEE"),
|
|
[]byte("FFFFFF"),
|
|
[]byte("GGGGGGG"),
|
|
}
|
|
for i := 0; i < b.N; i++ {
|
|
_, err := GenerateTrieFromItems(items, params.BeaconConfig().DepositContractTreeDepth)
|
|
require.NoError(b, err, "Could not generate Merkle trie from items")
|
|
}
|
|
}
|
|
|
|
func BenchmarkInsertTrie_Optimized(b *testing.B) {
|
|
b.StopTimer()
|
|
numDeposits := 16000
|
|
items := make([][]byte, numDeposits)
|
|
for i := 0; i < numDeposits; i++ {
|
|
someRoot := bytesutil.ToBytes32([]byte(strconv.Itoa(i)))
|
|
items[i] = someRoot[:]
|
|
}
|
|
tr, err := GenerateTrieFromItems(items, params.BeaconConfig().DepositContractTreeDepth)
|
|
require.NoError(b, err)
|
|
|
|
someItem := bytesutil.ToBytes32([]byte("hello-world"))
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
tr.Insert(someItem[:], i%numDeposits)
|
|
}
|
|
}
|
|
|
|
func BenchmarkGenerateProof(b *testing.B) {
|
|
b.StopTimer()
|
|
items := [][]byte{
|
|
[]byte("A"),
|
|
[]byte("BB"),
|
|
[]byte("CCC"),
|
|
[]byte("DDDD"),
|
|
[]byte("EEEEE"),
|
|
[]byte("FFFFFF"),
|
|
[]byte("GGGGGGG"),
|
|
}
|
|
normalTrie, err := GenerateTrieFromItems(items, params.BeaconConfig().DepositContractTreeDepth)
|
|
require.NoError(b, err)
|
|
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_, err := normalTrie.MerkleProof(3)
|
|
require.NoError(b, err)
|
|
}
|
|
}
|
|
|
|
func BenchmarkVerifyMerkleBranch(b *testing.B) {
|
|
b.StopTimer()
|
|
items := [][]byte{
|
|
[]byte("A"),
|
|
[]byte("BB"),
|
|
[]byte("CCC"),
|
|
[]byte("DDDD"),
|
|
[]byte("EEEEE"),
|
|
[]byte("FFFFFF"),
|
|
[]byte("GGGGGGG"),
|
|
}
|
|
m, err := GenerateTrieFromItems(items, params.BeaconConfig().DepositContractTreeDepth)
|
|
require.NoError(b, err)
|
|
proof, err := m.MerkleProof(2)
|
|
require.NoError(b, err)
|
|
|
|
root := m.Root()
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
if ok := VerifyMerkleBranch(root[:], items[2], 2, proof, params.BeaconConfig().DepositContractTreeDepth); !ok {
|
|
b.Error("Merkle proof did not verify")
|
|
}
|
|
}
|
|
}
|