prysm-pulse/beacon-chain/powchain/deposit_test.go
Radosław Kapka b742511193
Numeric conversion cleanup (#7465)
* 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>
2020-10-13 12:43:49 +00:00

266 lines
8.5 KiB
Go

package powchain
import (
"context"
"fmt"
"testing"
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{
HTTPEndPoint: 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{
HTTPEndPoint: 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{
HTTPEndPoint: 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{
HTTPEndPoint: 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{
HTTPEndPoint: 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{
HTTPEndPoint: endpoint,
BeaconDB: beaconDB,
})
require.NoError(t, err, "unable to setup web3 ETH1.0 chain service")
web3Service = setDefaultMocks(web3Service)
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),
},
}
sk := bls.RandKey()
deposit.Data.PublicKey = sk.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 := sk.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{
HTTPEndPoint: 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(uint64(i))
require.NoError(t, err)
assert.Equal(t, params.BeaconConfig().MaxEffectiveBalance, val.EffectiveBalance)
}
}