prysm-pulse/beacon-chain/powchain/deposit_test.go
Ivan Martinez 83179376d4 Cleanup testutil and change name scheme to reference deterministic (#4167)
* Clean testutil, change tool names to Deterministic
* Cleanup errors
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into clean-testutil
* Fix bug with generating deposits
* Fix a few tests
* Fix most tests
* Clean up some tests
* Remove err pt. 1
* Remove err pt. 2
* Change tests to use genesis state util
* Remove err from deposits
* Merge branch 'master' of https://github.com/prysmaticlabs/Prysm into clean-testutil
* Remove circular dependency
* Remove uncompressed signature test
* Merge branch 'master' of https://github.com/prysmaticlabs/Prysm into clean-testutil
* Merge branch 'master' into clean-testutil
* Goimports
* gazelle
* Merge branch 'master' of https://github.com/prysmaticlabs/Prysm into clean-testutil
* Add back error handling
* New attestation pool (#4185)

* New pool
* Better namings
* Fmt
* Gazelle
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into define-pool
* Raul's feedback
* Raul's feedback
* Log peer conected log for incoming connections (#4173)

* Log peer conected log for incoming connections
* Merge branch 'master' into peerconnected
* Merge branch 'master' into peerconnected
* Update handshake.go
* Update handshake.go
* Merge branch 'master' into peerconnected
* Merge branch 'master' into peerconnected
* Attestation pool to use go-cache (#4187)
* Update EthereumAPIs  (#4186)

* include new patch targeting latest ethapis master
* ensure project builds
* Merge branch 'master' into update-all-api
* fix up committees
* Merge branch 'update-all-api' of github.com:prysmaticlabs/prysm into update-all-api
* include latest eth apis
* Merge branch 'master' into update-all-api
* update block tests
* Merge branch 'update-all-api' of github.com:prysmaticlabs/prysm into update-all-api
* Merge branch 'master' into update-all-api
* add todos
* Implement GetValidator RPC Endpoint (#4188)

* include new patch targeting latest ethapis master
* ensure project builds
* Merge branch 'master' into update-all-api
* fix up committees
* Merge branch 'update-all-api' of github.com:prysmaticlabs/prysm into update-all-api
* include latest eth apis
* Merge branch 'master' into update-all-api
* update block tests
* Merge branch 'update-all-api' of github.com:prysmaticlabs/prysm into update-all-api
* Merge branch 'master' into update-all-api
* add todos
* implement get validator rpc
* add test for get validator
* table driven test
* fix up test
* fix confs
* tests for more cases
* fix up tests and add out of range
* Slasher optimization (#4172)

* size

* batching and concurrency improvements

* gaz

* merge fixes

* fix comment

* fix test

* fix test

* fix build

* ethpb

* ethpb

* fix test

* fix comment

* add benchmark

* fix benchmark
* Handle error for all testutil uses
* Fix errors
* Merge branch 'master' of https://github.com/prysmaticlabs/Prysm into clean-testutil
* Revert error handling

Revert "Fix errors"

This reverts commit db081f5486f62c854e7a34e792f5e380cfa922e7.

Revert "Handle error for all testutil uses"

This reverts commit bdabef230632dd184491c2dad63c7d3e74a3861b.

Revert "Add back error handling"

This reverts commit da7e3d2020cd906f45f452d4e441b566f2d5c8aa.
* Change genesis state func to use testing.T
* Merge branch 'master' of https://github.com/prysmaticlabs/Prysm into clean-testutil
* Merge branch 'master' of https://github.com/prysmaticlabs/Prysm into clean-testutil
* Fix conflict
* Merge branch 'master' of https://github.com/prysmaticlabs/Prysm into clean-testutil
* Merge branch 'master' into clean-testutil
* Merge branch 'master' into clean-testutil
* Captialize other logs
* Merge branch 'clean-testutil' of https://github.com/0xKiwi/Prysm into clean-testutil
* Merge branch 'master' of https://github.com/prysmaticlabs/Prysm into clean-testutil
* Merge branch 'master' into clean-testutil
2019-12-05 19:51:33 +00:00

295 lines
8.3 KiB
Go

package powchain
import (
"context"
"strings"
"testing"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/db/kv"
"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/trieutil"
)
const pubKeyErr = "could not deserialize validator public key"
func TestProcessDeposit_OK(t *testing.T) {
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
ETH1Endpoint: endpoint,
BeaconDB: &kv.Store{},
})
if err != nil {
t.Fatalf("Unable to setup web3 ETH1.0 chain service: %v", err)
}
web3Service = setDefaultMocks(web3Service)
deposits, _, _ := testutil.DeterministicDepositsAndKeys(1)
eth1Data, err := testutil.DeterministicEth1Data(len(deposits))
if err != nil {
t.Fatal(err)
}
if err := web3Service.processDeposit(eth1Data, deposits[0]); err != nil {
t.Fatalf("Could not process deposit %v", err)
}
if web3Service.activeValidatorCount != 1 {
t.Errorf("Did not get correct active validator count received %d, but wanted %d", web3Service.activeValidatorCount, 1)
}
}
func TestProcessDeposit_InvalidMerkleBranch(t *testing.T) {
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
ETH1Endpoint: endpoint,
BeaconDB: &kv.Store{},
})
if err != nil {
t.Fatalf("Unable to setup web3 ETH1.0 chain service: %v", err)
}
web3Service = setDefaultMocks(web3Service)
deposits, _, _ := testutil.DeterministicDepositsAndKeys(1)
eth1Data, err := testutil.DeterministicEth1Data(len(deposits))
if err != nil {
t.Fatal(err)
}
deposits[0].Proof = [][]byte{{'f', 'a', 'k', 'e'}}
err = web3Service.processDeposit(eth1Data, deposits[0])
if err == nil {
t.Fatal("No errors, when an error was expected")
}
want := "deposit merkle branch of deposit root did not verify for root"
if !strings.Contains(err.Error(), want) {
t.Errorf("Did not get expected error. Wanted: '%s' but got '%s'", want, err.Error())
}
}
func TestProcessDeposit_InvalidPublicKey(t *testing.T) {
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
ETH1Endpoint: endpoint,
BeaconDB: &kv.Store{},
})
if err != nil {
t.Fatalf("Unable to setup web3 ETH1.0 chain service: %v", err)
}
web3Service = setDefaultMocks(web3Service)
deposits, _, _ := testutil.DeterministicDepositsAndKeys(1)
deposits[0].Data.PublicKey = []byte("junk")
leaf, err := ssz.HashTreeRoot(deposits[0].Data)
if err != nil {
t.Fatalf("Could not hash deposit %v", err)
}
trie, err := trieutil.GenerateTrieFromItems([][]byte{leaf[:]}, int(params.BeaconConfig().DepositContractTreeDepth))
if err != nil {
log.Error(err)
}
deposits[0].Proof, err = trie.MerkleProof((0))
if err != nil {
t.Fatal(err)
}
root := trie.Root()
eth1Data := &ethpb.Eth1Data{
DepositCount: 1,
DepositRoot: root[:],
}
err = web3Service.processDeposit(eth1Data, deposits[0])
if err == nil {
t.Fatal("No errors, when an error was expected")
}
if !strings.Contains(err.Error(), pubKeyErr) {
t.Errorf("Did not get expected error. Wanted: '%s' but got '%s'", pubKeyErr, err.Error())
}
}
func TestProcessDeposit_InvalidSignature(t *testing.T) {
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
ETH1Endpoint: endpoint,
BeaconDB: &kv.Store{},
})
if err != nil {
t.Fatalf("Unable to setup web3 ETH1.0 chain service: %v", err)
}
web3Service = setDefaultMocks(web3Service)
deposits, _, _ := testutil.DeterministicDepositsAndKeys(1)
var fakeSig [96]byte
copy(fakeSig[:], []byte{'F', 'A', 'K', 'E'})
deposits[0].Data.Signature = fakeSig[:]
leaf, err := ssz.HashTreeRoot(deposits[0].Data)
if err != nil {
t.Fatalf("Could not hash deposit %v", err)
}
trie, err := trieutil.GenerateTrieFromItems([][]byte{leaf[:]}, int(params.BeaconConfig().DepositContractTreeDepth))
if err != nil {
log.Error(err)
}
root := trie.Root()
eth1Data := &ethpb.Eth1Data{
DepositCount: 1,
DepositRoot: root[:],
}
err = web3Service.processDeposit(eth1Data, deposits[0])
if err == nil {
t.Fatal("No errors, when an error was expected")
}
if !strings.Contains(err.Error(), pubKeyErr) {
t.Errorf("Did not get expected error. Wanted: '%s' but got '%s'", pubKeyErr, err.Error())
}
}
func TestProcessDeposit_UnableToVerify(t *testing.T) {
helpers.ClearAllCaches()
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
ETH1Endpoint: endpoint,
BeaconDB: &kv.Store{},
})
if err != nil {
t.Fatalf("Unable to setup web3 ETH1.0 chain service: %v", err)
}
web3Service = setDefaultMocks(web3Service)
testutil.ResetCache()
deposits, keys, _ := testutil.DeterministicDepositsAndKeys(1)
sig := keys[0].Sign([]byte{'F', 'A', 'K', 'E'}, bls.ComputeDomain(params.BeaconConfig().DomainDeposit))
deposits[0].Data.Signature = sig.Marshal()[:]
trie, _, err := testutil.DepositTrieFromDeposits(deposits)
if err != nil {
t.Fatal(err)
}
root := trie.Root()
eth1Data := &ethpb.Eth1Data{
DepositCount: 1,
DepositRoot: root[:],
}
proof, err := trie.MerkleProof(0)
if err != nil {
t.Fatal(err)
}
deposits[0].Proof = proof
err = web3Service.processDeposit(eth1Data, deposits[0])
if err == nil {
t.Fatal("No errors, when an error was expected")
}
want := "deposit signature did not verify"
if !strings.Contains(err.Error(), want) {
t.Errorf("Did not get expected error. Wanted: '%s' but got '%s'", want, err.Error())
}
}
func TestProcessDeposit_IncompleteDeposit(t *testing.T) {
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
ETH1Endpoint: endpoint,
BeaconDB: &kv.Store{},
})
if err != nil {
t.Fatalf("Unable to setup web3 ETH1.0 chain service: %v", err)
}
web3Service = setDefaultMocks(web3Service)
deposit := &ethpb.Deposit{
Data: &ethpb.Deposit_Data{
Amount: params.BeaconConfig().EffectiveBalanceIncrement, // incomplete deposit
WithdrawalCredentials: []byte("testing"),
},
}
sk := bls.RandKey()
deposit.Data.PublicKey = sk.PublicKey().Marshal()
signedRoot, err := ssz.SigningRoot(deposit.Data)
if err != nil {
t.Fatal(err)
}
sig := sk.Sign(signedRoot[:], bls.ComputeDomain(params.BeaconConfig().DomainDeposit))
deposit.Data.Signature = sig.Marshal()
trie, _, err := testutil.DepositTrieFromDeposits([]*ethpb.Deposit{deposit})
if err != nil {
t.Fatal(err)
}
root := trie.Root()
eth1Data := &ethpb.Eth1Data{
DepositCount: 1,
DepositRoot: root[:],
}
proof, err := trie.MerkleProof(0)
if err != nil {
t.Fatal(err)
}
deposit.Proof = proof
factor := params.BeaconConfig().MaxEffectiveBalance / params.BeaconConfig().EffectiveBalanceIncrement
// deposit till 31e9
for i := 0; i < int(factor-1); i++ {
if err := web3Service.processDeposit(eth1Data, deposit); err != nil {
t.Fatalf("Could not process deposit %v", err)
}
if web3Service.activeValidatorCount == 1 {
t.Errorf("Did not get correct active validator count received %d, but wanted %d", web3Service.activeValidatorCount, 0)
}
}
}
func TestProcessDeposit_AllDepositedSuccessfully(t *testing.T) {
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
ETH1Endpoint: endpoint,
BeaconDB: &kv.Store{},
})
if err != nil {
t.Fatalf("Unable to setup web3 ETH1.0 chain service: %v", err)
}
web3Service = setDefaultMocks(web3Service)
testutil.ResetCache()
deposits, keys, _ := testutil.DeterministicDepositsAndKeys(10)
eth1Data, err := testutil.DeterministicEth1Data(len(deposits))
if err != nil {
t.Fatal(err)
}
for i, k := range keys {
eth1Data.DepositCount = uint64(i + 1)
if err := web3Service.processDeposit(eth1Data, deposits[i]); err != nil {
t.Fatalf("Could not process deposit %v", err)
}
if web3Service.activeValidatorCount != uint64(i+1) {
t.Errorf("Did not get correct active validator count received %d, but wanted %d", web3Service.activeValidatorCount, uint64(i+1))
}
pubkey := bytesutil.ToBytes48(k.PublicKey().Marshal())
if web3Service.depositedPubkeys[pubkey] != params.BeaconConfig().MaxEffectiveBalance {
t.Errorf("Wanted a full deposit of %d but got %d", params.BeaconConfig().MaxEffectiveBalance, web3Service.depositedPubkeys[pubkey])
}
}
}