prysm-pulse/endtoend/validator.go
Ivan Martinez 4ab0a91e51
Validator Slashing Protection DB (#4389)
* Begin adding DB to validator client

Begin adding ValidatorProposalHistory

Implement most of proposal history

Finish tests

Fix marking a proposal for the first time

Change proposalhistory to not using bit shifting

Add pb.go

Change after proto/slashing added

Finally fix protos

Fix most tests

Fix all tests for double proposal protection

Start initialiing DB in validator client

Add db to validator struct

Add DB to ProposeBlock

Fix test errors and begin mocking

Fix test formatting and pass test for validator protection!

Fix merge issues

Fix renames

Fix tests

* Fix tests

* Fix first startup on DB

* Fix nil check tests

* Fix E2E

* Fix e2e flag

* Fix comments

* Fix for comments

* Move proposal hepers to validator/client to keep DB clean

* Add clear-db flag to validator client

* Fix formatting

* Clear out unintended changes

* Fix build issues

* Fix build issues

* Gazelle

* Fix mock test

* Remove proposal history

* Add terminal confirmation to DB clearing

* Add interface for validatorDB, add context to DB functions

* Add force-clear-db flag

* Cleanup

* Update validator/node/node.go

Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com>

* Change db to clear file, not whole folder

* Fix db test

* Fix teardown test

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
2020-01-08 13:16:17 -05:00

133 lines
3.7 KiB
Go

package endtoend
import (
"bytes"
"context"
"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
}
var validatorLogFileName = "vals-%d.log"
// initializeValidators sends the deposits to the eth1 chain and starts the validator clients.
func initializeValidators(
t *testing.T,
config *end2EndConfig,
keystorePath string,
) []*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(validatorLogFileName, 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("--datadir=%s/eth2-val-%d", tmpPath, n),
fmt.Sprintf("--beacon-rpc-provider=localhost:%d", 4000+n),
}
if config.minimalConfig {
args = append(args, "--minimal-config")
}
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
nonce, err := web3.PendingNonceAt(context.Background(), txOps.From)
if err != nil {
t.Fatal(err)
}
txOps.Nonce = big.NewInt(int64(nonce))
contract, err := contracts.NewDepositContract(config.contractAddr, web3)
if err != nil {
t.Fatal(err)
}
deposits, _, _ := testutil.DeterministicDepositsAndKeys(validatorNum)
_, roots, err := testutil.DeterministicDepositTrie(len(deposits))
if err != nil {
t.Fatal(err)
}
for index, dd := range deposits {
_, err = contract.Deposit(txOps, dd.Data.PublicKey, dd.Data.WithdrawalCredentials, dd.Data.Signature, roots[index])
if err != nil {
t.Errorf("unable to send transaction to contract: %v", err)
}
txOps.Nonce = txOps.Nonce.Add(txOps.Nonce, big.NewInt(1))
}
keystore, err := keystore.DecryptKey(jsonBytes, "" /*password*/)
if err != nil {
t.Fatal(err)
}
// "Safe" amount of blocks to mine to make sure the deposits are seen.
if err := mineBlocks(web3, keystore, 20); err != nil {
t.Fatalf("failed to mine blocks %v", err)
}
return valClients
}