Create node's Powchain registration file (#8754)

* extract powchain and remove unused function

* improve "no web3 provider" error log

* add second error log

* remove redundant code from registerPOWChainService

* remove deposit contract log from registerPOWChainService

* gzl

Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
This commit is contained in:
Radosław Kapka
2021-04-14 12:54:50 +02:00
committed by GitHub
parent 902e3f4f53
commit a9a0ecd76d
8 changed files with 169 additions and 77 deletions

View File

@@ -51,7 +51,6 @@ go_library(
"@com_github_prysmaticlabs_eth2_types//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
"@in_gopkg_yaml_v2//:go_default_library",
],
)
@@ -63,7 +62,6 @@ go_test(
deps = [
"//beacon-chain/core/feed/state:go_default_library",
"//shared/cmd:go_default_library",
"//shared/testutil/assert:go_default_library",
"//shared/testutil/require:go_default_library",
"@com_github_sirupsen_logrus//hooks/test:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",

View File

@@ -7,7 +7,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"os/signal"
"path/filepath"
@@ -50,7 +49,6 @@ import (
"github.com/prysmaticlabs/prysm/shared/version"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"
)
const testSkipPowFlag = "test-skip-pow"
@@ -110,7 +108,11 @@ func New(cliCtx *cli.Context) (*BeaconNode, error) {
slashingsPool: slashings.NewPool(),
}
if err := beacon.startDB(cliCtx); err != nil {
depositAddress, err := registration.DepositContractAddress()
if err != nil {
return nil, err
}
if err := beacon.startDB(cliCtx, depositAddress); err != nil {
return nil, err
}
@@ -231,7 +233,7 @@ func (b *BeaconNode) startForkChoice() {
b.forkChoiceStore = f
}
func (b *BeaconNode) startDB(cliCtx *cli.Context) error {
func (b *BeaconNode) startDB(cliCtx *cli.Context, depositAddress string) error {
baseDir := cliCtx.String(cmd.DataDirFlag.Name)
dbPath := filepath.Join(baseDir, kv.BeaconNodeDbDirName)
clearDB := cliCtx.Bool(cmd.ClearDB.Name)
@@ -304,26 +306,35 @@ func (b *BeaconNode) startDB(cliCtx *cli.Context) error {
}
}
return b.db.EnsureEmbeddedGenesis(b.ctx)
if err := b.db.EnsureEmbeddedGenesis(b.ctx); err != nil {
return err
}
knownContract, err := b.db.DepositContractAddress(b.ctx)
if err != nil {
return err
}
addr := common.HexToAddress(depositAddress)
if len(knownContract) == 0 {
if err := b.db.SaveDepositContractAddress(b.ctx, addr); err != nil {
return errors.Wrap(err, "could not save deposit contract")
}
}
if len(knownContract) > 0 && !bytes.Equal(addr.Bytes(), knownContract) {
return fmt.Errorf("database contract is %#x but tried to run with %#x. This likely means "+
"you are trying to run on a different network than what the database contains. You can run once with "+
"'--clear-db' to wipe the old database or use an alternative data directory with '--datadir'",
knownContract, addr.Bytes())
}
log.Infof("Deposit contract: %#x", addr.Bytes())
return nil
}
func (b *BeaconNode) startStateGen() {
b.stateGen = stategen.New(b.db)
}
func readbootNodes(fileName string) ([]string, error) {
fileContent, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
listNodes := make([]string, 0)
err = yaml.Unmarshal(fileContent, &listNodes)
if err != nil {
return nil, err
}
return listNodes, nil
}
func (b *BeaconNode) registerP2P(cliCtx *cli.Context) error {
bootstrapNodeAddrs, dataDir, err := registration.P2PPreregistration(cliCtx)
if err != nil {
@@ -418,26 +429,12 @@ func (b *BeaconNode) registerPOWChainService() error {
if b.cliCtx.Bool(testSkipPowFlag) {
return b.services.RegisterService(&powchain.Service{})
}
depAddress := params.BeaconConfig().DepositContractAddress
if depAddress == "" {
log.Fatal("Valid deposit contract is required")
}
if !common.IsHexAddress(depAddress) {
log.Fatalf("Invalid deposit contract address given: %s", depAddress)
depAddress, endpoints, err := registration.PowchainPreregistration(b.cliCtx)
if err != nil {
return err
}
if b.cliCtx.String(flags.HTTPWeb3ProviderFlag.Name) == "" {
log.Error(
"No ETH1 node specified to run with the beacon node. Please consider running your own ETH1 node for better uptime, security, and decentralization of ETH2. Visit https://docs.prylabs.network/docs/prysm-usage/setup-eth1 for more information.",
)
log.Error(
"You will need to specify --http-web3provider to attach an eth1 node to the prysm node. Without an eth1 node block proposals for your validator will be affected and the beacon node will not be able to initialize the genesis state.",
)
}
endpoints := []string{b.cliCtx.String(flags.HTTPWeb3ProviderFlag.Name)}
endpoints = append(endpoints, b.cliCtx.StringSlice(flags.FallbackWeb3ProviderFlag.Name)...)
cfg := &powchain.Web3ServiceConfig{
HTTPEndpoints: endpoints,
DepositContract: common.HexToAddress(depAddress),
@@ -451,23 +448,7 @@ func (b *BeaconNode) registerPOWChainService() error {
if err != nil {
return errors.Wrap(err, "could not register proof-of-work chain web3Service")
}
knownContract, err := b.db.DepositContractAddress(b.ctx)
if err != nil {
return err
}
if len(knownContract) == 0 {
if err := b.db.SaveDepositContractAddress(b.ctx, cfg.DepositContract); err != nil {
return errors.Wrap(err, "could not save deposit contract")
}
}
if len(knownContract) > 0 && !bytes.Equal(cfg.DepositContract.Bytes(), knownContract) {
return fmt.Errorf("database contract is %#x but tried to run with %#x. This likely means "+
"you are trying to run on a different network than what the database contains. You can run once with "+
"'--clear-db' to wipe the old database or use an alternative data directory with '--datadir'",
knownContract, cfg.DepositContract.Bytes())
}
log.Infof("Deposit contract: %#x", cfg.DepositContract.Bytes())
return b.services.RegisterService(web3Service)
}

View File

@@ -3,14 +3,12 @@ package node
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
"github.com/prysmaticlabs/prysm/shared/cmd"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
logTest "github.com/sirupsen/logrus/hooks/test"
"github.com/urfave/cli/v2"
@@ -44,25 +42,6 @@ func TestNodeClose_OK(t *testing.T) {
require.NoError(t, os.RemoveAll(tmp))
}
func TestBootStrapNodeFile(t *testing.T) {
file, err := ioutil.TempFile(t.TempDir(), "bootstrapFile")
require.NoError(t, err)
sampleNode0 := "- enr:-Ku4QMKVC_MowDsmEa20d5uGjrChI0h8_KsKXDmgVQbIbngZV0i" +
"dV6_RL7fEtZGo-kTNZ5o7_EJI_vCPJ6scrhwX0Z4Bh2F0dG5ldHOIAAAAAAAAAACEZXRoMpD" +
"1pf1CAAAAAP__________gmlkgnY0gmlwhBLf22SJc2VjcDI1NmsxoQJxCnE6v_x2ekgY_uo" +
"E1rtwzvGy40mq9eD66XfHPBWgIIN1ZHCCD6A"
sampleNode1 := "- enr:-TESTNODE2"
sampleNode2 := "- enr:-TESTNODE3"
err = ioutil.WriteFile(file.Name(), []byte(sampleNode0+"\n"+sampleNode1+"\n"+sampleNode2), 0644)
require.NoError(t, err, "Error in WriteFile call")
nodeList, err := readbootNodes(file.Name())
require.NoError(t, err, "Error in readbootNodes call")
assert.Equal(t, sampleNode0[2:], nodeList[0], "Unexpected nodes")
assert.Equal(t, sampleNode1[2:], nodeList[1], "Unexpected nodes")
assert.Equal(t, sampleNode2[2:], nodeList[2], "Unexpected nodes")
}
// TestClearDB tests clearing the database
func TestClearDB(t *testing.T) {
hook := logTest.NewGlobal()

View File

@@ -3,12 +3,19 @@ load("@prysm//tools/go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["p2p.go"],
srcs = [
"log.go",
"p2p.go",
"powchain.go",
],
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/node/registration",
visibility = ["//beacon-chain/node:__subpackages__"],
deps = [
"//cmd/beacon-chain/flags:go_default_library",
"//shared/cmd:go_default_library",
"//shared/params:go_default_library",
"@com_github_ethereum_go_ethereum//common:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
"@in_gopkg_yaml_v2//:go_default_library",
],
@@ -16,13 +23,18 @@ go_library(
go_test(
name = "go_default_test",
srcs = ["p2p_test.go"],
srcs = [
"p2p_test.go",
"powchain_test.go",
],
embed = [":go_default_library"],
deps = [
"//cmd/beacon-chain/flags:go_default_library",
"//shared/cmd:go_default_library",
"//shared/params:go_default_library",
"//shared/testutil/assert:go_default_library",
"//shared/testutil/require:go_default_library",
"@com_github_sirupsen_logrus//hooks/test:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
],
)

View File

@@ -0,0 +1,5 @@
package registration
import "github.com/sirupsen/logrus"
var log = logrus.WithField("prefix", "registration")

View File

@@ -2,7 +2,6 @@ package registration
import (
"io/ioutil"
"log"
"path/filepath"
"github.com/prysmaticlabs/prysm/shared/cmd"

View File

@@ -0,0 +1,47 @@
package registration
import (
"errors"
"github.com/ethereum/go-ethereum/common"
"github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/urfave/cli/v2"
)
// PowchainPreregistration prepares data for powchain.Service's registration.
func PowchainPreregistration(cliCtx *cli.Context) (depositContractAddress string, endpoints []string, err error) {
depositContractAddress, err = DepositContractAddress()
if err != nil {
return "", nil, err
}
if cliCtx.String(flags.HTTPWeb3ProviderFlag.Name) == "" && len(cliCtx.StringSlice(flags.FallbackWeb3ProviderFlag.Name)) == 0 {
log.Error(
cliCtx.Context,
"No ETH1 node specified to run with the beacon node. Please consider running your own ETH1 node for better uptime, security, and decentralization of ETH2. Visit https://docs.prylabs.network/docs/prysm-usage/setup-eth1 for more information.",
)
log.Error(
cliCtx.Context,
"You will need to specify --http-web3provider and/or --fallback-web3provider to attach an eth1 node to the prysm node. Without an eth1 node block proposals for your validator will be affected and the beacon node will not be able to initialize the genesis state.",
)
}
endpoints = []string{cliCtx.String(flags.HTTPWeb3ProviderFlag.Name)}
endpoints = append(endpoints, cliCtx.StringSlice(flags.FallbackWeb3ProviderFlag.Name)...)
return
}
// DepositContractAddress returns the address of the deposit contract.
func DepositContractAddress() (string, error) {
address := params.BeaconConfig().DepositContractAddress
if address == "" {
return "", errors.New("valid deposit contract is required")
}
if !common.IsHexAddress(address) {
return "", errors.New("invalid deposit contract address given: " + address)
}
return address, nil
}

View File

@@ -0,0 +1,71 @@
package registration
import (
"flag"
"testing"
"github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
logTest "github.com/sirupsen/logrus/hooks/test"
"github.com/urfave/cli/v2"
)
func TestPowchainPreregistration(t *testing.T) {
app := cli.App{}
set := flag.NewFlagSet("test", 0)
set.String(flags.HTTPWeb3ProviderFlag.Name, "primary", "")
fallback := cli.StringSlice{}
err := fallback.Set("fallback1")
require.NoError(t, err)
err = fallback.Set("fallback2")
require.NoError(t, err)
set.Var(&fallback, flags.FallbackWeb3ProviderFlag.Name, "")
ctx := cli.NewContext(&app, set, nil)
address, endpoints, err := PowchainPreregistration(ctx)
require.NoError(t, err)
assert.Equal(t, params.BeaconConfig().DepositContractAddress, address)
assert.DeepEqual(t, []string{"primary", "fallback1", "fallback2"}, endpoints)
}
func TestPowchainPreregistration_EmptyWeb3Provider(t *testing.T) {
hook := logTest.NewGlobal()
app := cli.App{}
set := flag.NewFlagSet("test", 0)
set.String(flags.HTTPWeb3ProviderFlag.Name, "", "")
fallback := cli.StringSlice{}
set.Var(&fallback, flags.FallbackWeb3ProviderFlag.Name, "")
ctx := cli.NewContext(&app, set, nil)
_, _, err := PowchainPreregistration(ctx)
require.NoError(t, err)
assert.LogsContain(t, hook, "No ETH1 node specified to run with the beacon node")
}
func TestDepositContractAddress_Ok(t *testing.T) {
address, err := DepositContractAddress()
require.NoError(t, err)
assert.Equal(t, params.BeaconConfig().DepositContractAddress, address)
}
func TestDepositContractAddress_EmptyAddress(t *testing.T) {
params.SetupTestConfigCleanup(t)
config := params.BeaconConfig()
config.DepositContractAddress = ""
params.OverrideBeaconConfig(config)
_, err := DepositContractAddress()
assert.ErrorContains(t, "valid deposit contract is required", err)
}
func TestDepositContractAddress_NotHexAddress(t *testing.T) {
params.SetupTestConfigCleanup(t)
config := params.BeaconConfig()
config.DepositContractAddress = "abc?!"
params.OverrideBeaconConfig(config)
_, err := DepositContractAddress()
assert.ErrorContains(t, "invalid deposit contract address given", err)
}