2020-03-22 23:04:23 +00:00
|
|
|
package components
|
2019-11-15 18:56:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-01-06 20:50:36 +00:00
|
|
|
"context"
|
2019-11-15 18:56:26 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/big"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/bazelbuild/rules_go/go/tools/bazel"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2020-04-27 15:59:42 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-11-15 18:56:26 +00:00
|
|
|
contracts "github.com/prysmaticlabs/prysm/contracts/deposit-contract"
|
2020-03-22 23:04:23 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/endtoend/helpers"
|
|
|
|
e2e "github.com/prysmaticlabs/prysm/endtoend/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/endtoend/types"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
2019-11-15 18:56:26 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
|
|
)
|
|
|
|
|
2020-04-27 15:59:42 +00:00
|
|
|
// StartValidatorClients starts the configured amount of validators, also sending and mining their validator deposits.
|
|
|
|
// Should only be used on initialization.
|
|
|
|
func StartValidatorClients(t *testing.T, config *types.E2EConfig, keystorePath string) []int {
|
2020-03-22 23:04:23 +00:00
|
|
|
// Always using genesis count since using anything else would be difficult to test for.
|
|
|
|
validatorNum := int(params.BeaconConfig().MinGenesisActiveValidatorCount)
|
|
|
|
beaconNodeNum := e2e.TestParams.BeaconNodeCount
|
2019-11-15 18:56:26 +00:00
|
|
|
if validatorNum%beaconNodeNum != 0 {
|
|
|
|
t.Fatal("Validator count is not easily divisible by beacon node count.")
|
|
|
|
}
|
2020-03-22 23:04:23 +00:00
|
|
|
processIDs := make([]int, beaconNodeNum)
|
2019-11-15 18:56:26 +00:00
|
|
|
validatorsPerNode := validatorNum / beaconNodeNum
|
2020-04-27 15:59:42 +00:00
|
|
|
for i := 0; i < beaconNodeNum; i++ {
|
|
|
|
pID := StartNewValidatorClient(t, config, validatorsPerNode, i)
|
|
|
|
processIDs[i] = pID
|
|
|
|
}
|
2020-01-25 07:39:56 +00:00
|
|
|
|
2020-04-27 15:59:42 +00:00
|
|
|
SendAndMineDeposits(t, keystorePath, validatorNum, 0)
|
|
|
|
|
|
|
|
return processIDs
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartNewValidatorClient starts a validator client with the passed in configuration.
|
|
|
|
func StartNewValidatorClient(t *testing.T, config *types.E2EConfig, validatorNum int, index int) int {
|
|
|
|
validatorsPerClient := int(params.BeaconConfig().MinGenesisActiveValidatorCount) / e2e.TestParams.BeaconNodeCount
|
|
|
|
// Only allow validatorsPerClient count for each validator client.
|
|
|
|
if validatorNum != validatorsPerClient {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
binaryPath, found := bazel.FindBinary("validator", "validator")
|
|
|
|
if !found {
|
|
|
|
t.Fatal("validator binary not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
beaconRPCPort := e2e.TestParams.BeaconNodeRPCPort + index
|
|
|
|
if beaconRPCPort >= e2e.TestParams.BeaconNodeRPCPort+e2e.TestParams.BeaconNodeCount {
|
|
|
|
// Point any extra validator clients to a node we know is running.
|
|
|
|
beaconRPCPort = e2e.TestParams.BeaconNodeRPCPort
|
2019-11-15 18:56:26 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 15:59:42 +00:00
|
|
|
file, err := helpers.DeleteAndCreateFile(e2e.TestParams.LogPath, fmt.Sprintf(e2e.ValidatorLogFileName, index))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
args := []string{
|
|
|
|
fmt.Sprintf("--datadir=%s/eth2-val-%d", e2e.TestParams.TestPath, index),
|
|
|
|
fmt.Sprintf("--log-file=%s", file.Name()),
|
|
|
|
fmt.Sprintf("--interop-num-validators=%d", validatorNum),
|
|
|
|
fmt.Sprintf("--interop-start-index=%d", validatorNum*index),
|
|
|
|
fmt.Sprintf("--monitoring-port=%d", e2e.TestParams.ValidatorMetricsPort+index),
|
|
|
|
fmt.Sprintf("--beacon-rpc-provider=localhost:%d", beaconRPCPort),
|
|
|
|
"--grpc-headers=dummy=value,foo=bar", // Sending random headers shouldn't break anything.
|
2020-06-11 06:38:15 +00:00
|
|
|
"--verbosity=trace",
|
2020-04-27 15:59:42 +00:00
|
|
|
"--force-clear-db",
|
2020-05-26 19:04:42 +00:00
|
|
|
"--e2e-config",
|
2020-04-27 15:59:42 +00:00
|
|
|
}
|
|
|
|
args = append(args, featureconfig.E2EValidatorFlags...)
|
|
|
|
args = append(args, config.ValidatorFlags...)
|
|
|
|
|
|
|
|
cmd := exec.Command(binaryPath, args...)
|
|
|
|
t.Logf("Starting validator client %d with flags: %s", index, strings.Join(args[2:], " "))
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd.Process.Pid
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendAndMineDeposits sends the requested amount of deposits and mines the chain after to ensure the deposits are seen.
|
|
|
|
func SendAndMineDeposits(t *testing.T, keystorePath string, validatorNum int, offset int) {
|
2020-03-22 23:04:23 +00:00
|
|
|
client, err := rpc.DialHTTP(fmt.Sprintf("http://127.0.0.1:%d", e2e.TestParams.Eth1RPCPort))
|
2019-11-15 18:56:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-04-27 15:59:42 +00:00
|
|
|
defer client.Close()
|
2019-11-15 18:56:26 +00:00
|
|
|
web3 := ethclient.NewClient(client)
|
|
|
|
|
2020-04-27 15:59:42 +00:00
|
|
|
keystoreBytes, err := ioutil.ReadFile(keystorePath)
|
2019-11-15 18:56:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-04-27 15:59:42 +00:00
|
|
|
if err := SendDeposits(web3, keystoreBytes, validatorNum, offset); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
mineKey, err := keystore.DecryptKey(keystoreBytes, "" /*password*/)
|
2019-11-15 18:56:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-04-27 15:59:42 +00:00
|
|
|
if err := mineBlocks(web3, mineKey, params.BeaconConfig().Eth1FollowDistance); err != nil {
|
|
|
|
t.Fatalf("failed to mine blocks %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendDeposits uses the passed in web3 and keystore bytes to send the requested deposits.
|
|
|
|
func SendDeposits(web3 *ethclient.Client, keystoreBytes []byte, num int, offset int) error {
|
|
|
|
txOps, err := bind.NewTransactor(bytes.NewReader(keystoreBytes), "" /*password*/)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-15 18:56:26 +00:00
|
|
|
depositInGwei := big.NewInt(int64(params.BeaconConfig().MaxEffectiveBalance))
|
|
|
|
txOps.Value = depositInGwei.Mul(depositInGwei, big.NewInt(int64(params.BeaconConfig().GweiPerEth)))
|
|
|
|
txOps.GasLimit = 4000000
|
2020-01-06 20:50:36 +00:00
|
|
|
nonce, err := web3.PendingNonceAt(context.Background(), txOps.From)
|
|
|
|
if err != nil {
|
2020-04-27 15:59:42 +00:00
|
|
|
return err
|
2020-01-06 20:50:36 +00:00
|
|
|
}
|
|
|
|
txOps.Nonce = big.NewInt(int64(nonce))
|
2019-11-15 18:56:26 +00:00
|
|
|
|
2020-03-22 23:04:23 +00:00
|
|
|
contract, err := contracts.NewDepositContract(e2e.TestParams.ContractAddress, web3)
|
2019-11-15 18:56:26 +00:00
|
|
|
if err != nil {
|
2020-04-27 15:59:42 +00:00
|
|
|
return err
|
2019-11-15 18:56:26 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 15:59:42 +00:00
|
|
|
deposits, _, err := testutil.DeterministicDepositsAndKeys(uint64(num + offset))
|
2020-04-13 04:11:09 +00:00
|
|
|
if err != nil {
|
2020-04-27 15:59:42 +00:00
|
|
|
return err
|
2020-04-13 04:11:09 +00:00
|
|
|
}
|
2019-12-05 19:51:33 +00:00
|
|
|
_, roots, err := testutil.DeterministicDepositTrie(len(deposits))
|
|
|
|
if err != nil {
|
2020-04-27 15:59:42 +00:00
|
|
|
return err
|
2019-12-05 19:51:33 +00:00
|
|
|
}
|
2019-11-15 18:56:26 +00:00
|
|
|
for index, dd := range deposits {
|
2020-04-27 15:59:42 +00:00
|
|
|
if index < offset {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-15 18:56:26 +00:00
|
|
|
_, err = contract.Deposit(txOps, dd.Data.PublicKey, dd.Data.WithdrawalCredentials, dd.Data.Signature, roots[index])
|
|
|
|
if err != nil {
|
2020-04-27 15:59:42 +00:00
|
|
|
return errors.Wrap(err, "unable to send transaction to contract")
|
2019-11-15 18:56:26 +00:00
|
|
|
}
|
2020-01-06 20:50:36 +00:00
|
|
|
txOps.Nonce = txOps.Nonce.Add(txOps.Nonce, big.NewInt(1))
|
2019-11-15 18:56:26 +00:00
|
|
|
}
|
2020-04-27 15:59:42 +00:00
|
|
|
return nil
|
2019-11-15 18:56:26 +00:00
|
|
|
}
|