prysm-pulse/shared/params/loader_test.go
Shay Zluf 1a27c21d9c
Load chain config from file (#5694)
* load chain config from file

* revert flag name change

* add dependencies to image

* Update shared/cmd/flags.go

Co-authored-by: Victor Farazdagi <simple.square@gmail.com>

* Update beacon-chain/main.go

Co-authored-by: Victor Farazdagi <simple.square@gmail.com>

* added test to load config file

* Merge branch 'yaml_chain_config' of github.com:prysmaticlabs/prysm into yaml_chain_config

# Conflicts:
#	beacon-chain/main.go

* replace hex with yaml format of fixed byte array

* fix test and check if comment

* move to node package

* gaz

* added contract address case

* fix key name issue

* add tests

* gaz

* add 1 byte handling

* change to fatal

* add config printout

* revert main changes

* revert line removal

* fix one byte handling

* fix test and one byte handling

* remove log

* Apply suggestions from code review

* change to debug

* Update beacon-chain/node/node.go

* move helper methods

Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
Co-authored-by: terence tsao <terence@prysmaticlabs.com>
2020-05-05 20:58:07 +03:00

118 lines
4.5 KiB
Go

package params
import (
"path"
"strings"
"testing"
"github.com/bazelbuild/rules_go/go/tools/bazel"
)
func TestLoadConfigFile(t *testing.T) {
mainnetConfigFile := ConfigFilePath(t, "mainnet")
LoadChainConfigFile(mainnetConfigFile)
if BeaconConfig().MaxCommitteesPerSlot != MainnetConfig().MaxCommitteesPerSlot {
t.Errorf("Expected MaxCommitteesPerSlot to be set to mainnet value: %d found: %d",
MainnetConfig().MaxCommitteesPerSlot,
BeaconConfig().MaxCommitteesPerSlot)
}
if BeaconConfig().SecondsPerSlot != MainnetConfig().SecondsPerSlot {
t.Errorf("Expected SecondsPerSlot to be set to mainnet value: %d found: %d",
MainnetConfig().SecondsPerSlot,
BeaconConfig().SecondsPerSlot)
}
minimalConfigFile := ConfigFilePath(t, "minimal")
LoadChainConfigFile(minimalConfigFile)
if BeaconConfig().MaxCommitteesPerSlot != MinimalSpecConfig().MaxCommitteesPerSlot {
t.Errorf("Expected MaxCommitteesPerSlot to be set to minimal value: %d found: %d",
MinimalSpecConfig().MaxCommitteesPerSlot,
BeaconConfig().MaxCommitteesPerSlot)
}
if BeaconConfig().SecondsPerSlot != MinimalSpecConfig().SecondsPerSlot {
t.Errorf("Expected SecondsPerSlot to be set to minimal value: %d found: %d",
MinimalSpecConfig().SecondsPerSlot,
BeaconConfig().SecondsPerSlot)
}
}
func Test_replaceHexStringWithYAMLFormat(t *testing.T) {
testLines := []struct {
line string
wanted string
}{
{
line: "ONE_BYTE: 0x41",
wanted: "ONE_BYTE: 65\n",
},
{
line: "FOUR_BYTES: 0x41414141",
wanted: "FOUR_BYTES: \n- 65\n- 65\n- 65\n- 65\n",
},
{
line: "THREE_BYTES: 0x414141",
wanted: "THREE_BYTES: \n- 65\n- 65\n- 65\n- 0\n",
},
{
line: "EIGHT_BYTES: 0x4141414141414141",
wanted: "EIGHT_BYTES: \n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n",
},
{
line: "SIXTEEN_BYTES: 0x41414141414141414141414141414141",
wanted: "SIXTEEN_BYTES: \n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
"- 65\n- 65\n- 65\n- 65\n",
},
{
line: "TWENTY_BYTES: 0x4141414141414141414141414141414141414141",
wanted: "TWENTY_BYTES: \n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n",
},
{
line: "THIRTY_TWO_BYTES: 0x4141414141414141414141414141414141414141414141414141414141414141",
wanted: "THIRTY_TWO_BYTES: \n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n",
},
{
line: "FORTY_EIGHT_BYTES: 0x41414141414141414141414141414141414141414141414141414141414141414141" +
"4141414141414141414141414141",
wanted: "FORTY_EIGHT_BYTES: \n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n",
},
{
line: "NINETY_SIX_BYTES: 0x414141414141414141414141414141414141414141414141414141414141414141414141" +
"4141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141" +
"41414141414141414141414141",
wanted: "NINETY_SIX_BYTES: \n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n",
},
}
for _, line := range testLines {
parts := replaceHexStringWithYAMLFormat(line.line)
res := strings.Join(parts, "\n")
if res != line.wanted {
t.Errorf("expected conversion to be: %v got: %v", line.wanted, res)
}
}
}
// ConfigFilePath sets the proper config and returns the relevant
// config file path from eth2-spec-tests directory.
func ConfigFilePath(t *testing.T, config string) string {
configFolderPath := path.Join("tests", config)
filepath, err := bazel.Runfile(configFolderPath)
if err != nil {
t.Fatal(err)
}
configFilePath := path.Join(filepath, "config.yaml")
return configFilePath
}