prysm-pulse/endtoend/validator.go
Ivan Martinez 68edad13bc End To End Tests for Demo and Minimal config (#3932)
* Begin working on end to end tests using geth dev chain

* Start on beacon node set up

* More progress on bnode setup

* Complete flow until chainstart, begin work on evaluators

* More progress on evaluators

* Start changing bazel run to direct binary

* Move endtoend to inside beacon-chain

* use bazel provided geth, use bazel test

* tempdir

* use fork rules_go

* Change to use UUID dir and bazel binaries

* Truncate UUID a bit

* Get full run from chainstart to evaluating

* Rewrite to react to logs rather than arbitrarily wait

* Fix export

* Move evaluators to evaluators.go

* Add peer check test

* Add more comments

* Remove unneeded exports

* Check all nodes have the correct amount of peers

* Change name to onGenesisEpoch

* Remove extra wait times where not needed

* Cleanup

* Add log for beacon start

* Fix deposit amount

* Make room for eth1follow distnce

* Cleanup and fix minimal test

* Goimports

* Fix imports

* gazelle and minimal

* manual

* Fix for comments

* Make timing rely on reading logs, and cleanup

* Fix for comments

* Fix workspace

* Cleanup

* Fix visibility

* Cleanup and some comments

* Address comments

* Fix for v0.9

* Modify for v0.9

* Move to own package outside of beacon-chain

* Gazelle

* Polishing, logging

* Fix filenames

* Add more logs

* Add flag logging

* Cover for page not having libp2p info

* Improve multiAddr detection

* Add more logs

* Add missing flags

* Add log printing to defer

* Get multiAddr from logs

* Fix logging and detection

* Change evaluators to rely on EpochTimer

* Add evaluator for ValidatorParticipation

* Fix validator participation evaluator

* Cleanup, comments and fix participation calculation

* Cleanup

* Let the file searcher search for longer

* Change participation to check for full

* Log out file contents if text isnt found

* Split into different files

* Disable IPC and use RPC instead, change tmp dir to bazel dir

* Change visibility

* Gazelle

* Add e2e tag

* new line
2019-11-15 13:56:26 -05:00

117 lines
3.2 KiB
Go

package endtoend
import (
"bytes"
"fmt"
"io/ioutil"
"math/big"
"os"
"os/exec"
"path"
"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"
contracts "github.com/prysmaticlabs/prysm/contracts/deposit-contract"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
)
type validatorClientInfo struct {
processID int
monitorPort uint64
}
// initializeValidators sends the deposits to the eth1 chain and starts the validator clients.
func initializeValidators(
t *testing.T,
config *end2EndConfig,
keystorePath string,
beaconNodes []*beaconNodeInfo,
) []*validatorClientInfo {
binaryPath, found := bazel.FindBinary("validator", "validator")
if !found {
t.Fatal("validator binary not found")
}
tmpPath := config.tmpPath
validatorNum := config.numValidators
beaconNodeNum := config.numBeaconNodes
if validatorNum%beaconNodeNum != 0 {
t.Fatal("Validator count is not easily divisible by beacon node count.")
}
valClients := make([]*validatorClientInfo, beaconNodeNum)
validatorsPerNode := validatorNum / beaconNodeNum
for n := uint64(0); n < beaconNodeNum; n++ {
file, err := os.Create(path.Join(tmpPath, fmt.Sprintf("vals-%d.log", n)))
if err != nil {
t.Fatal(err)
}
args := []string{
fmt.Sprintf("--interop-num-validators=%d", validatorsPerNode),
fmt.Sprintf("--interop-start-index=%d", validatorsPerNode*n),
fmt.Sprintf("--monitoring-port=%d", 9080+n),
fmt.Sprintf("--beacon-rpc-provider=localhost:%d", 4000+n),
}
cmd := exec.Command(binaryPath, args...)
cmd.Stdout = file
cmd.Stderr = file
t.Logf("Starting validator client with flags %s", strings.Join(args, " "))
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
valClients[n] = &validatorClientInfo{
processID: cmd.Process.Pid,
monitorPort: 9080 + n,
}
}
client, err := rpc.DialHTTP("http://127.0.0.1:8545")
if err != nil {
t.Fatal(err)
}
web3 := ethclient.NewClient(client)
jsonBytes, err := ioutil.ReadFile(keystorePath)
if err != nil {
t.Fatal(err)
}
txOps, err := bind.NewTransactor(bytes.NewReader(jsonBytes), "" /*password*/)
if err != nil {
t.Fatal(err)
}
depositInGwei := big.NewInt(int64(params.BeaconConfig().MaxEffectiveBalance))
txOps.Value = depositInGwei.Mul(depositInGwei, big.NewInt(int64(params.BeaconConfig().GweiPerEth)))
txOps.GasLimit = 4000000
contract, err := contracts.NewDepositContract(config.contractAddr, web3)
if err != nil {
t.Fatal(err)
}
deposits, roots, _ := testutil.SetupInitialDeposits(t, validatorNum)
for index, dd := range deposits {
_, err = contract.Deposit(txOps, dd.Data.PublicKey, dd.Data.WithdrawalCredentials, dd.Data.Signature, roots[index])
if err != nil {
t.Error("unable to send transaction to contract")
}
}
keystore, err := keystore.DecryptKey(jsonBytes, "" /*password*/)
if err != nil {
t.Fatal(err)
}
// Picked 20 for this as a "safe" number of blocks to mine so the deposits
// are detected.
if err := mineBlocks(web3, keystore, 20); err != nil {
t.Fatal(err)
}
return valClients
}