prysm-pulse/beacon-chain/powchain/deposit_test.go
Ivan Martinez 2f10b1c7b1
Change gogoproto compiler to protoc-gen-go-cast (#8697)
* Remove gogoproto compiler

* Remove more gogoproto

* Improvements

* Fix gengo

* More scripts

* Gazelle, fix deps

* Fix version and errors

* Fix gocast for arrays

* Fix ethapis

* Fixes

* Fix compile errors

* fix go.mod

* //proto/... builds

* Update for protov2

* temp fix compilation to move on

* Change everything to emptypb.empty

* Add grpc to proto/slashings

* Fix almost all build failures

* Oher build problems

* FIX THIS FUCKING THING

* gaz literally every .bazel

* Final touches

* Final final touches

* Fix proto

* Begin moving proto.Marshal to native

* Fix site_data

* Fixes

* Fix duplicate gateway

* Fix gateway target

* Fix ethapis

* Fixes from review

* Update

* Fix

* Fix status test

* Fix fuzz

* Add isprotoslice to fun

* Change DeepEqual to DeepSSZEqual for proto arrays

* Fix build

* Fix gaz

* Update go

* Fixes

* Fixes

* Add case for nil validators after copy

* Fix cast

* Fix test

* Fix imports

* Go mod

* Only use extension where needed

* Fixes

* Split gateway from gengo

* gaz

* go mod

* Add back hydrated state

* fix hydrate

* Fix proto.clone

* Fies

* Revert "Split gateway from gengo"

This reverts commit 7298bb2054d446e427d9af97e13b8fabe8695085.

* Revert "gaz"

This reverts commit ca952565701a88727e22302d6c8d60ac48d97255.

* Merge all gateway into one target

* go mod

* Gaz

* Add generate v1_gateway files

* run pb again

* goimports

* gaz

* Fix comments

* Fix protos

* Fix PR

* Fix protos

* Update grpc-gateway and ethapis

* Update ethapis and gen-go-cast

* Go tidy

* Reorder

* Fix ethapis

* fix spec tests

* Fix script

* Remove unused import

* Fix fuzz

* Fix gomod

* Update version

* Error if the cloned result is nil

* Handle optional slots

* ADd more empty checks to clone

* Undo fuzz changes

* Fix build.bazel

* Gaz

* Redo fuzz changes

* Undo some eth1data changes

* Update go.mod

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>

* Undo clone beacon state

* Remove gogo proto more and unused v1_gateway

* Add manual fix for nil vals

* Fix gaz

* tidy

* Tidy again

* Add detailed error

* Revert "Add detailed error"

This reverts commit 59bc053dcd59569a54c95b07739d5a379665ec5d.

* Undo varint changes

* Fix nil validators in deposit test

* Commit

* Undo

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
Co-authored-by: Radosław Kapka <rkapka@wp.pl>
Co-authored-by: Nishant Das <nishdas93@gmail.com>
Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
2021-05-17 18:32:04 +00:00

269 lines
8.7 KiB
Go

package powchain
import (
"context"
"fmt"
"testing"
types "github.com/prysmaticlabs/eth2-types"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
"github.com/prysmaticlabs/prysm/shared/trieutil"
logTest "github.com/sirupsen/logrus/hooks/test"
)
const pubKeyErr = "could not convert bytes to public key"
func TestProcessDeposit_OK(t *testing.T) {
beaconDB := testDB.SetupDB(t)
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
HttpEndpoints: []string{endpoint},
BeaconDB: beaconDB,
})
require.NoError(t, err, "Unable to setup web3 ETH1.0 chain service")
web3Service = setDefaultMocks(web3Service)
deposits, _, err := testutil.DeterministicDepositsAndKeys(1)
require.NoError(t, err)
eth1Data, err := testutil.DeterministicEth1Data(len(deposits))
require.NoError(t, err)
err = web3Service.processDeposit(context.Background(), eth1Data, deposits[0])
require.NoError(t, err, "could not process deposit")
valcount, err := helpers.ActiveValidatorCount(web3Service.preGenesisState, 0)
require.NoError(t, err)
require.Equal(t, 1, int(valcount), "Did not get correct active validator count")
}
func TestProcessDeposit_InvalidMerkleBranch(t *testing.T) {
beaconDB := testDB.SetupDB(t)
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
HttpEndpoints: []string{endpoint},
BeaconDB: beaconDB,
})
require.NoError(t, err, "unable to setup web3 ETH1.0 chain service")
web3Service = setDefaultMocks(web3Service)
deposits, _, err := testutil.DeterministicDepositsAndKeys(1)
require.NoError(t, err)
eth1Data, err := testutil.DeterministicEth1Data(len(deposits))
require.NoError(t, err)
deposits[0].Proof = [][]byte{{'f', 'a', 'k', 'e'}}
err = web3Service.processDeposit(context.Background(), eth1Data, deposits[0])
require.NotNil(t, err, "No errors, when an error was expected")
want := "deposit merkle branch of deposit root did not verify for root"
assert.ErrorContains(t, want, err)
}
func TestProcessDeposit_InvalidPublicKey(t *testing.T) {
hook := logTest.NewGlobal()
beaconDB := testDB.SetupDB(t)
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
HttpEndpoints: []string{endpoint},
BeaconDB: beaconDB,
})
require.NoError(t, err, "unable to setup web3 ETH1.0 chain service")
web3Service = setDefaultMocks(web3Service)
deposits, _, err := testutil.DeterministicDepositsAndKeys(1)
require.NoError(t, err)
deposits[0].Data.PublicKey = bytesutil.PadTo([]byte("junk"), 48)
leaf, err := deposits[0].Data.HashTreeRoot()
require.NoError(t, err, "Could not hash deposit")
trie, err := trieutil.GenerateTrieFromItems([][]byte{leaf[:]}, params.BeaconConfig().DepositContractTreeDepth)
require.NoError(t, err)
deposits[0].Proof, err = trie.MerkleProof(0)
require.NoError(t, err)
root := trie.Root()
eth1Data := &ethpb.Eth1Data{
DepositCount: 1,
DepositRoot: root[:],
}
err = web3Service.processDeposit(context.Background(), eth1Data, deposits[0])
require.NoError(t, err)
require.LogsContain(t, hook, pubKeyErr)
}
func TestProcessDeposit_InvalidSignature(t *testing.T) {
hook := logTest.NewGlobal()
beaconDB := testDB.SetupDB(t)
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
HttpEndpoints: []string{endpoint},
BeaconDB: beaconDB,
})
require.NoError(t, err, "unable to setup web3 ETH1.0 chain service")
web3Service = setDefaultMocks(web3Service)
deposits, _, err := testutil.DeterministicDepositsAndKeys(1)
require.NoError(t, err)
var fakeSig [96]byte
copy(fakeSig[:], []byte{'F', 'A', 'K', 'E'})
deposits[0].Data.Signature = fakeSig[:]
leaf, err := deposits[0].Data.HashTreeRoot()
require.NoError(t, err, "Could not hash deposit")
trie, err := trieutil.GenerateTrieFromItems([][]byte{leaf[:]}, params.BeaconConfig().DepositContractTreeDepth)
require.NoError(t, err)
root := trie.Root()
eth1Data := &ethpb.Eth1Data{
DepositCount: 1,
DepositRoot: root[:],
}
err = web3Service.processDeposit(context.Background(), eth1Data, deposits[0])
require.NoError(t, err)
require.LogsContain(t, hook, pubKeyErr)
}
func TestProcessDeposit_UnableToVerify(t *testing.T) {
hook := logTest.NewGlobal()
beaconDB := testDB.SetupDB(t)
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
HttpEndpoints: []string{endpoint},
BeaconDB: beaconDB,
})
require.NoError(t, err, "unable to setup web3 ETH1.0 chain service")
web3Service = setDefaultMocks(web3Service)
testutil.ResetCache()
deposits, keys, err := testutil.DeterministicDepositsAndKeys(1)
require.NoError(t, err)
sig := keys[0].Sign([]byte{'F', 'A', 'K', 'E'})
deposits[0].Data.Signature = sig.Marshal()
trie, _, err := testutil.DepositTrieFromDeposits(deposits)
require.NoError(t, err)
root := trie.Root()
eth1Data := &ethpb.Eth1Data{
DepositCount: 1,
DepositRoot: root[:],
}
proof, err := trie.MerkleProof(0)
require.NoError(t, err)
deposits[0].Proof = proof
err = web3Service.processDeposit(context.Background(), eth1Data, deposits[0])
require.NoError(t, err)
want := "signature did not verify"
require.LogsContain(t, hook, want)
}
func TestProcessDeposit_IncompleteDeposit(t *testing.T) {
beaconDB := testDB.SetupDB(t)
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
HttpEndpoints: []string{endpoint},
BeaconDB: beaconDB,
})
require.NoError(t, err, "unable to setup web3 ETH1.0 chain service")
web3Service = setDefaultMocks(web3Service)
require.NoError(t, web3Service.preGenesisState.SetValidators([]*ethpb.Validator{}))
deposit := &ethpb.Deposit{
Data: &ethpb.Deposit_Data{
Amount: params.BeaconConfig().EffectiveBalanceIncrement, // incomplete deposit
WithdrawalCredentials: bytesutil.PadTo([]byte("testing"), 32),
Signature: bytesutil.PadTo([]byte("test"), 96),
},
}
priv, err := bls.RandKey()
require.NoError(t, err)
deposit.Data.PublicKey = priv.PublicKey().Marshal()
d, err := helpers.ComputeDomain(params.BeaconConfig().DomainDeposit, nil, nil)
require.NoError(t, err)
signedRoot, err := helpers.ComputeSigningRoot(deposit.Data, d)
require.NoError(t, err)
sig := priv.Sign(signedRoot[:])
deposit.Data.Signature = sig.Marshal()
trie, err := trieutil.NewTrie(params.BeaconConfig().DepositContractTreeDepth)
require.NoError(t, err)
root := trie.Root()
eth1Data := &ethpb.Eth1Data{
DepositCount: 1,
DepositRoot: root[:],
}
proof, err := trie.MerkleProof(0)
require.NoError(t, err)
dataRoot, err := deposit.Data.HashTreeRoot()
require.NoError(t, err)
deposit.Proof = proof
factor := params.BeaconConfig().MaxEffectiveBalance / params.BeaconConfig().EffectiveBalanceIncrement
// deposit till 31e9
for i := 0; i < int(factor-1); i++ {
trie.Insert(dataRoot[:], i)
trieRoot := trie.HashTreeRoot()
eth1Data.DepositRoot = trieRoot[:]
eth1Data.DepositCount = uint64(i + 1)
deposit.Proof, err = trie.MerkleProof(i)
require.NoError(t, err)
err = web3Service.processDeposit(context.Background(), eth1Data, deposit)
require.NoError(t, err, fmt.Sprintf("Could not process deposit at %d", i))
valcount, err := helpers.ActiveValidatorCount(web3Service.preGenesisState, 0)
require.NoError(t, err)
require.Equal(t, 0, int(valcount), "Did not get correct active validator count")
}
}
func TestProcessDeposit_AllDepositedSuccessfully(t *testing.T) {
beaconDB := testDB.SetupDB(t)
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
HttpEndpoints: []string{endpoint},
BeaconDB: beaconDB,
})
require.NoError(t, err, "unable to setup web3 ETH1.0 chain service")
web3Service = setDefaultMocks(web3Service)
testutil.ResetCache()
deposits, keys, err := testutil.DeterministicDepositsAndKeys(10)
require.NoError(t, err)
eth1Data, err := testutil.DeterministicEth1Data(len(deposits))
require.NoError(t, err)
for i := range keys {
eth1Data.DepositCount = uint64(i + 1)
err = web3Service.processDeposit(context.Background(), eth1Data, deposits[i])
require.NoError(t, err, fmt.Sprintf("Could not process deposit at %d", i))
valCount, err := helpers.ActiveValidatorCount(web3Service.preGenesisState, 0)
require.NoError(t, err)
require.Equal(t, uint64(i+1), valCount, "Did not get correct active validator count")
val, err := web3Service.preGenesisState.ValidatorAtIndex(types.ValidatorIndex(i))
require.NoError(t, err)
assert.Equal(t, params.BeaconConfig().MaxEffectiveBalance, val.EffectiveBalance)
}
}