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

164 lines
4.2 KiB
Go

package endtoend
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"strings"
"testing"
"time"
"github.com/bazelbuild/rules_go/go/tools/bazel"
"github.com/ethereum/go-ethereum/common"
ev "github.com/prysmaticlabs/prysm/endtoend/evaluators"
)
type beaconNodeInfo struct {
processID int
datadir string
rpcPort uint64
monitorPort uint64
grpcPort uint64
multiAddr string
}
type end2EndConfig struct {
minimalConfig bool
tmpPath string
epochsToRun uint64
numValidators uint64
numBeaconNodes uint64
contractAddr common.Address
evaluators []ev.Evaluator
}
// startBeaconNodes starts the requested amount of beacon nodes, passing in the deposit contract given.
func startBeaconNodes(t *testing.T, config *end2EndConfig) []*beaconNodeInfo {
numNodes := config.numBeaconNodes
nodeInfo := []*beaconNodeInfo{}
for i := uint64(0); i < numNodes; i++ {
newNode := startNewBeaconNode(t, config, nodeInfo)
nodeInfo = append(nodeInfo, newNode)
}
return nodeInfo
}
func startNewBeaconNode(t *testing.T, config *end2EndConfig, beaconNodes []*beaconNodeInfo) *beaconNodeInfo {
tmpPath := config.tmpPath
index := len(beaconNodes)
binaryPath, found := bazel.FindBinary("beacon-chain", "beacon-chain")
if !found {
t.Log(binaryPath)
t.Fatal("beacon chain binary not found")
}
file, err := os.Create(path.Join(tmpPath, fmt.Sprintf("beacon-%d.log", index)))
if err != nil {
t.Fatal(err)
}
args := []string{
"--no-discovery",
"--http-web3provider=http://127.0.0.1:8545",
"--web3provider=ws://127.0.0.1:8546",
fmt.Sprintf("--datadir=%s/eth2-beacon-node-%d", tmpPath, index),
fmt.Sprintf("--deposit-contract=%s", config.contractAddr.Hex()),
fmt.Sprintf("--rpc-port=%d", 4000+index),
fmt.Sprintf("--p2p-udp-port=%d", 12000+index),
fmt.Sprintf("--p2p-tcp-port=%d", 13000+index),
fmt.Sprintf("--monitoring-port=%d", 8080+index),
fmt.Sprintf("--grpc-gateway-port=%d", 3200+index),
}
if config.minimalConfig {
args = append(args, "--minimal-config")
}
// After the first node is made, have all following nodes connect to all previously made nodes.
if index >= 1 {
for p := 0; p < index; p++ {
args = append(args, fmt.Sprintf("--peer=%s", beaconNodes[p].multiAddr))
}
}
t.Logf("Starting beacon chain with flags %s", strings.Join(args, " "))
cmd := exec.Command(binaryPath, args...)
cmd.Stderr = file
cmd.Stdout = file
if err := cmd.Start(); err != nil {
t.Fatalf("failed to start beacon node: %v", err)
}
if err = waitForTextInFile(file, "Node started p2p server"); err != nil {
t.Fatal(err)
}
multiAddr, err := getMultiAddrFromLogFile(file.Name())
if err != nil {
t.Fatal(err)
}
return &beaconNodeInfo{
processID: cmd.Process.Pid,
datadir: fmt.Sprintf("%s/eth2-beacon-node-%d", tmpPath, index),
rpcPort: 4000 + uint64(index),
monitorPort: 8080 + uint64(index),
grpcPort: 3200 + uint64(index),
multiAddr: multiAddr,
}
}
func getMultiAddrFromLogFile(name string) (string, error) {
byteContent, err := ioutil.ReadFile(name)
if err != nil {
return "", err
}
contents := string(byteContent)
searchText := "\"Node started p2p server\" multiAddr=\""
startIdx := strings.Index(contents, searchText)
if startIdx == -1 {
return "", fmt.Errorf("did not find peer text in %s", contents)
}
startIdx += len(searchText)
endIdx := strings.Index(contents[startIdx:], "\"")
if endIdx == -1 {
return "", fmt.Errorf("did not find peer text in %s", contents)
}
return contents[startIdx : startIdx+endIdx], nil
}
func waitForTextInFile(file *os.File, text string) error {
wait := 0
// Putting the wait cap at 24 seconds.
totalWait := 24
for wait < totalWait {
time.Sleep(2 * time.Second)
// Rewind the file pointer to the start of the file so we can read it again.
_, err := file.Seek(0, io.SeekStart)
if err != nil {
return fmt.Errorf("could not rewind file to start: %v", err)
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if strings.Contains(scanner.Text(), text) {
return nil
}
}
if err := scanner.Err(); err != nil {
return err
}
wait += 2
}
contents, err := ioutil.ReadFile(file.Name())
if err != nil {
return err
}
return fmt.Errorf("could not find requested text %s in logs:\n%s", text, string(contents))
}