diff --git a/BUILD.bazel b/BUILD.bazel index f2ab814e7..0d2bc3fc1 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -5,6 +5,8 @@ load("@io_kubernetes_build//defs:run_in_workspace.bzl", "workspace_binary") prefix = "github.com/prysmaticlabs/prysm" +exports_files(["genesis.json"]) + # gazelle:prefix github.com/prysmaticlabs/prysm gazelle( name = "gazelle", diff --git a/beacon-chain/utils/BUILD.bazel b/beacon-chain/utils/BUILD.bazel index d82f04c69..fe213fea2 100644 --- a/beacon-chain/utils/BUILD.bazel +++ b/beacon-chain/utils/BUILD.bazel @@ -29,11 +29,11 @@ go_test( "genesis_json_test.go", "shuffle_test.go", ], + data = ["//:genesis.json"], embed = [":go_default_library"], deps = [ "//proto/beacon/p2p/v1:go_default_library", "//shared/params:go_default_library", "@com_github_ethereum_go_ethereum//common:go_default_library", - "@com_github_gogo_protobuf//jsonpb:go_default_library", ], ) diff --git a/beacon-chain/utils/genesis_json.go b/beacon-chain/utils/genesis_json.go index 6c517a044..8b5d296a2 100644 --- a/beacon-chain/utils/genesis_json.go +++ b/beacon-chain/utils/genesis_json.go @@ -11,10 +11,9 @@ import ( // InitialValidatorRegistryFromJSON retrieves the validator set that is stored in // genesis.json. func InitialValidatorRegistryFromJSON(genesisJSONPath string) ([]*pb.ValidatorRecord, error) { - // #nosec G304 // genesisJSONPath is a user input for the path of genesis.json. // Ex: /path/to/my/genesis.json. - f, err := os.Open(genesisJSONPath) + f, err := os.Open(genesisJSONPath) // #nosec if err != nil { return nil, err } diff --git a/beacon-chain/utils/genesis_json_test.go b/beacon-chain/utils/genesis_json_test.go index 7c0dfb077..1674f2c5c 100644 --- a/beacon-chain/utils/genesis_json_test.go +++ b/beacon-chain/utils/genesis_json_test.go @@ -1,10 +1,11 @@ package utils import ( + "fmt" "os" + "reflect" "testing" - "github.com/gogo/protobuf/jsonpb" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/params" ) @@ -21,39 +22,31 @@ func TestInitGenesisJsonFailure(t *testing.T) { } func TestInitGenesisJson(t *testing.T) { - fname := "/genesis.json" - pwd, _ := os.Getwd() - fnamePath := pwd + fname - os.Remove(fnamePath) + // Support running this test via bazel or go test. + var fNamePath string + if os.Getenv("RUNFILES_DIR") != "" { + fNamePath = fmt.Sprintf("%s/%s/%s", + os.Getenv("RUNFILES_DIR"), + os.Getenv("TEST_WORKSPACE"), + "/genesis.json", + ) + } else { + fNamePath = "../../genesis.json" + } params.UseDemoBeaconConfig() - stateJSON := &pb.BeaconState{ - LastStateRecalculationSlot: 0, - JustifiedStreak: 1, - LastFinalizedSlot: 99, + state := &pb.BeaconState{ ValidatorRegistry: []*pb.ValidatorRecord{ - {Pubkey: []byte{}, Balance: 32, Status: uint64(params.Active)}, + {Pubkey: []byte("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"), Balance: 32000000000, Status: uint64(params.Active)}, }, } - os.Create(fnamePath) - f, err := os.OpenFile(fnamePath, os.O_APPEND|os.O_WRONLY, os.ModeAppend) + + validators, err := InitialValidatorRegistryFromJSON(fNamePath) if err != nil { - t.Fatalf("can't open file %v", err) + t.Fatalf("Reading validatory registry from genesis.json failed %v", err) } - ma := jsonpb.Marshaler{} - err = ma.Marshal(f, stateJSON) - if err != nil { - t.Fatalf("can't marshal file %v", err) + if !reflect.DeepEqual(state.ValidatorRegistry[0], validators[0]) { + t.Error("Validator registry mismatched") } - - validators, err := InitialValidatorRegistryFromJSON(fnamePath) - if err != nil { - t.Fatalf("genesis.json failed %v", err) - } - - if validators[0].Status != 1 { - t.Errorf("Failed to load of genesis json") - } - os.Remove(fnamePath) } diff --git a/genesis.json b/genesis.json index a7c3f12a3..d239e038c 100644 --- a/genesis.json +++ b/genesis.json @@ -1,51 +1,43 @@ { - "validators": [ + "validatorRegistry": [ { "pubkey": "QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFB", "balance": "32000000000", - "exitSlot": "999999999999999999", "status": "1" }, { "pubkey": "QkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJC", "balance": "32000000000", - "exitSlot": "999999999999999999", "status": "1" }, { "pubkey": "Q0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0ND", "balance": "32000000000", - "exitSlot": "999999999999999999", "status": "1" }, { "pubkey": "RERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERE", "balance": "32000000000", - "exitSlot": "999999999999999999", "status": "1" }, { "pubkey": "RUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVF", "balance": "32000000000", - "exitSlot": "999999999999999999", "status": "1" }, { "pubkey": "RkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZG", "balance": "32000000000", - "exitSlot": "999999999999999999", "status": "1" }, { "pubkey": "R0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dH", "balance": "32000000000", - "exitSlot": "999999999999999999", "status": "1" }, { "pubkey": "SEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhI", "balance": "32000000000", - "exitSlot": "999999999999999999", "status": "1" } ]