prysm-pulse/endtoend/eth1.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

147 lines
4.0 KiB
Go

package endtoend
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"math/big"
"os"
"os/exec"
"path"
"strings"
"testing"
"time"
"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/common"
"github.com/ethereum/go-ethereum/core/types"
"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"
)
// startEth1 starts an eth1 local dev chain and deploys a deposit contract.
func startEth1(t *testing.T, tmpPath string) (common.Address, string, int) {
binaryPath, found := bazel.FindBinary("cmd/geth", "geth")
if !found {
t.Fatal("go-ethereum binary not found")
}
args := []string{
fmt.Sprintf("--datadir=%s", path.Join(tmpPath, "eth1data/")),
"--rpc",
"--rpcaddr=0.0.0.0",
"--rpccorsdomain=\"*\"",
"--rpcvhosts=\"*\"",
"--ws",
"--wsaddr=0.0.0.0",
"--wsorigins=\"*\"",
"--dev",
"--dev.period=0",
"--ipcdisable",
}
cmd := exec.Command(binaryPath, args...)
file, err := os.Create(path.Join(tmpPath, "eth1.log"))
if err != nil {
t.Fatal(err)
}
cmd.Stdout = file
cmd.Stderr = file
if err := cmd.Start(); err != nil {
t.Fatalf("failed to start eth1 chain: %v", err)
}
if err = waitForTextInFile(file, "Commit new mining work"); err != nil {
t.Fatal(err)
}
// Connect to the started geth dev chain.
client, err := rpc.DialHTTP("http://127.0.0.1:8545")
if err != nil {
t.Fatalf("failed to connect to ipc: %v", err)
}
web3 := ethclient.NewClient(client)
// Access the dev account keystore to deploy the contract.
fileName, err := exec.Command("ls", path.Join(tmpPath, "eth1data/keystore")).Output()
if err != nil {
t.Fatal(err)
}
keystorePath := path.Join(tmpPath, fmt.Sprintf("eth1data/keystore/%s", strings.TrimSpace(string(fileName))))
jsonBytes, err := ioutil.ReadFile(keystorePath)
if err != nil {
t.Fatal(err)
}
keystore, err := keystore.DecryptKey(jsonBytes, "" /*password*/)
if err != nil {
t.Fatal(err)
}
// Advancing the blocks eth1follow distance to prevent issues reading the chain.
if err := mineBlocks(web3, keystore, params.BeaconConfig().Eth1FollowDistance); err != nil {
t.Fatalf("unable to advance chain: %v", err)
}
txOpts, err := bind.NewTransactor(bytes.NewReader(jsonBytes), "" /*password*/)
if err != nil {
t.Fatal(err)
}
nonce, err := web3.PendingNonceAt(context.Background(), keystore.Address)
if err != nil {
t.Fatal(err)
}
txOpts.Nonce = big.NewInt(int64(nonce))
contractAddr, tx, _, err := contracts.DeployDepositContract(txOpts, web3, txOpts.From)
if err != nil {
t.Fatalf("failed to deploy deposit contract: %v", err)
}
// Wait for contract to mine.
for pending := true; pending; _, pending, err = web3.TransactionByHash(context.Background(), tx.Hash()) {
if err != nil {
t.Fatal(err)
}
time.Sleep(100 * time.Millisecond)
}
return contractAddr, keystorePath, cmd.Process.Pid
}
func mineBlocks(web3 *ethclient.Client, keystore *keystore.Key, blocksToMake uint64) error {
nonce, err := web3.PendingNonceAt(context.Background(), keystore.Address)
if err != nil {
return err
}
chainID, err := web3.NetworkID(context.Background())
if err != nil {
return err
}
block, err := web3.BlockByNumber(context.Background(), nil)
if err != nil {
return err
}
finishBlock := block.NumberU64() + blocksToMake
for block.NumberU64() <= finishBlock {
spamTX := types.NewTransaction(nonce, keystore.Address, big.NewInt(0), 21000, big.NewInt(1e6), []byte{})
signed, err := types.SignTx(spamTX, types.NewEIP155Signer(chainID), keystore.PrivateKey)
if err != nil {
return err
}
if err := web3.SendTransaction(context.Background(), signed); err != nil {
return err
}
nonce++
time.Sleep(250 * time.Microsecond)
block, err = web3.BlockByNumber(context.Background(), nil)
if err != nil {
return err
}
}
return nil
}