Skip to content

Commit

Permalink
Tests for beacon-chain/node/config.go (#8769)
Browse files Browse the repository at this point in the history
* Tests for `beacon-chain/node/config.go`

* gzl

* change tested value of NetworkID
  • Loading branch information
rkapka authored Apr 15, 2021
1 parent aba2ec9 commit 2c6549b
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
9 changes: 8 additions & 1 deletion beacon-chain/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,19 @@ go_library(
go_test(
name = "go_default_test",
size = "small",
srcs = ["node_test.go"],
srcs = [
"config_test.go",
"node_test.go",
],
embed = [":go_default_library"],
deps = [
"//beacon-chain/core/feed/state:go_default_library",
"//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_prysmaticlabs_eth2_types//:go_default_library",
"@com_github_sirupsen_logrus//hooks/test:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
],
Expand Down
90 changes: 90 additions & 0 deletions beacon-chain/node/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package node

import (
"flag"
"fmt"
"strconv"
"testing"

types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags"
"github.com/prysmaticlabs/prysm/shared/cmd"
"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 TestConfigureHistoricalSlasher(t *testing.T) {
params.SetupTestConfigCleanup(t)
hook := logTest.NewGlobal()

app := cli.App{}
set := flag.NewFlagSet("test", 0)
set.Bool(flags.HistoricalSlasherNode.Name, true, "")
cliCtx := cli.NewContext(&app, set, nil)

configureHistoricalSlasher(cliCtx)

assert.Equal(t, params.BeaconConfig().SlotsPerEpoch*4, params.BeaconConfig().SlotsPerArchivedPoint)
assert.LogsContain(t, hook,
fmt.Sprintf(
"Setting %d slots per archive point and %d max RPC page size for historical slasher usage",
params.BeaconConfig().SlotsPerArchivedPoint,
int(params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().MaxAttestations))),
)
}

func TestConfigureSlotsPerArchivedPoint(t *testing.T) {
params.SetupTestConfigCleanup(t)

app := cli.App{}
set := flag.NewFlagSet("test", 0)
set.Int(flags.SlotsPerArchivedPoint.Name, 0, "")
require.NoError(t, set.Set(flags.SlotsPerArchivedPoint.Name, strconv.Itoa(100)))
cliCtx := cli.NewContext(&app, set, nil)

configureSlotsPerArchivedPoint(cliCtx)

assert.Equal(t, types.Slot(100), params.BeaconConfig().SlotsPerArchivedPoint)
}

func TestConfigureProofOfWork(t *testing.T) {
params.SetupTestConfigCleanup(t)

app := cli.App{}
set := flag.NewFlagSet("test", 0)
set.Uint64(flags.ChainID.Name, 0, "")
set.Uint64(flags.NetworkID.Name, 0, "")
set.String(flags.DepositContractFlag.Name, "", "")
require.NoError(t, set.Set(flags.ChainID.Name, strconv.Itoa(100)))
require.NoError(t, set.Set(flags.NetworkID.Name, strconv.Itoa(200)))
require.NoError(t, set.Set(flags.DepositContractFlag.Name, "deposit"))
cliCtx := cli.NewContext(&app, set, nil)

configureProofOfWork(cliCtx)

assert.Equal(t, uint64(100), params.BeaconConfig().DepositChainID)
assert.Equal(t, uint64(200), params.BeaconConfig().DepositNetworkID)
assert.Equal(t, "deposit", params.BeaconConfig().DepositContractAddress)
}

func TestConfigureNetwork(t *testing.T) {
params.SetupTestConfigCleanup(t)

app := cli.App{}
set := flag.NewFlagSet("test", 0)
bootstrapNodes := cli.StringSlice{}
set.Var(&bootstrapNodes, cmd.BootstrapNode.Name, "")
set.Int(flags.ContractDeploymentBlock.Name, 0, "")
require.NoError(t, set.Set(cmd.BootstrapNode.Name, "node1"))
require.NoError(t, set.Set(cmd.BootstrapNode.Name, "node2"))
require.NoError(t, set.Set(flags.ContractDeploymentBlock.Name, strconv.Itoa(100)))
cliCtx := cli.NewContext(&app, set, nil)

configureNetwork(cliCtx)

assert.DeepEqual(t, []string{"node1", "node2"}, params.BeaconNetworkConfig().BootstrapNodes)
assert.Equal(t, uint64(100), params.BeaconNetworkConfig().ContractDeploymentBlock)
}

0 comments on commit 2c6549b

Please sign in to comment.