Updated Genesis JSON and Fixed Test (#1044)

This commit is contained in:
terence tsao 2018-12-05 15:44:07 -08:00 committed by GitHub
parent d3055531b9
commit 3e8c2b2957
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 39 deletions

View File

@ -5,6 +5,8 @@ load("@io_kubernetes_build//defs:run_in_workspace.bzl", "workspace_binary")
prefix = "github.com/prysmaticlabs/prysm" prefix = "github.com/prysmaticlabs/prysm"
exports_files(["genesis.json"])
# gazelle:prefix github.com/prysmaticlabs/prysm # gazelle:prefix github.com/prysmaticlabs/prysm
gazelle( gazelle(
name = "gazelle", name = "gazelle",

View File

@ -29,11 +29,11 @@ go_test(
"genesis_json_test.go", "genesis_json_test.go",
"shuffle_test.go", "shuffle_test.go",
], ],
data = ["//:genesis.json"],
embed = [":go_default_library"], embed = [":go_default_library"],
deps = [ deps = [
"//proto/beacon/p2p/v1:go_default_library", "//proto/beacon/p2p/v1:go_default_library",
"//shared/params:go_default_library", "//shared/params:go_default_library",
"@com_github_ethereum_go_ethereum//common:go_default_library", "@com_github_ethereum_go_ethereum//common:go_default_library",
"@com_github_gogo_protobuf//jsonpb:go_default_library",
], ],
) )

View File

@ -11,10 +11,9 @@ import (
// InitialValidatorRegistryFromJSON retrieves the validator set that is stored in // InitialValidatorRegistryFromJSON retrieves the validator set that is stored in
// genesis.json. // genesis.json.
func InitialValidatorRegistryFromJSON(genesisJSONPath string) ([]*pb.ValidatorRecord, error) { func InitialValidatorRegistryFromJSON(genesisJSONPath string) ([]*pb.ValidatorRecord, error) {
// #nosec G304
// genesisJSONPath is a user input for the path of genesis.json. // genesisJSONPath is a user input for the path of genesis.json.
// Ex: /path/to/my/genesis.json. // Ex: /path/to/my/genesis.json.
f, err := os.Open(genesisJSONPath) f, err := os.Open(genesisJSONPath) // #nosec
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -1,10 +1,11 @@
package utils package utils
import ( import (
"fmt"
"os" "os"
"reflect"
"testing" "testing"
"github.com/gogo/protobuf/jsonpb"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/params"
) )
@ -21,39 +22,31 @@ func TestInitGenesisJsonFailure(t *testing.T) {
} }
func TestInitGenesisJson(t *testing.T) { func TestInitGenesisJson(t *testing.T) {
fname := "/genesis.json" // Support running this test via bazel or go test.
pwd, _ := os.Getwd() var fNamePath string
fnamePath := pwd + fname if os.Getenv("RUNFILES_DIR") != "" {
os.Remove(fnamePath) fNamePath = fmt.Sprintf("%s/%s/%s",
os.Getenv("RUNFILES_DIR"),
os.Getenv("TEST_WORKSPACE"),
"/genesis.json",
)
} else {
fNamePath = "../../genesis.json"
}
params.UseDemoBeaconConfig() params.UseDemoBeaconConfig()
stateJSON := &pb.BeaconState{ state := &pb.BeaconState{
LastStateRecalculationSlot: 0,
JustifiedStreak: 1,
LastFinalizedSlot: 99,
ValidatorRegistry: []*pb.ValidatorRecord{ 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 { 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{} if !reflect.DeepEqual(state.ValidatorRegistry[0], validators[0]) {
err = ma.Marshal(f, stateJSON) t.Error("Validator registry mismatched")
if err != nil {
t.Fatalf("can't marshal file %v", err)
} }
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)
} }

View File

@ -1,51 +1,43 @@
{ {
"validators": [ "validatorRegistry": [
{ {
"pubkey": "QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFB", "pubkey": "QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFB",
"balance": "32000000000", "balance": "32000000000",
"exitSlot": "999999999999999999",
"status": "1" "status": "1"
}, },
{ {
"pubkey": "QkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJC", "pubkey": "QkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJC",
"balance": "32000000000", "balance": "32000000000",
"exitSlot": "999999999999999999",
"status": "1" "status": "1"
}, },
{ {
"pubkey": "Q0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0ND", "pubkey": "Q0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0ND",
"balance": "32000000000", "balance": "32000000000",
"exitSlot": "999999999999999999",
"status": "1" "status": "1"
}, },
{ {
"pubkey": "RERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERE", "pubkey": "RERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERE",
"balance": "32000000000", "balance": "32000000000",
"exitSlot": "999999999999999999",
"status": "1" "status": "1"
}, },
{ {
"pubkey": "RUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVF", "pubkey": "RUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVF",
"balance": "32000000000", "balance": "32000000000",
"exitSlot": "999999999999999999",
"status": "1" "status": "1"
}, },
{ {
"pubkey": "RkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZG", "pubkey": "RkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZG",
"balance": "32000000000", "balance": "32000000000",
"exitSlot": "999999999999999999",
"status": "1" "status": "1"
}, },
{ {
"pubkey": "R0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dH", "pubkey": "R0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dH",
"balance": "32000000000", "balance": "32000000000",
"exitSlot": "999999999999999999",
"status": "1" "status": "1"
}, },
{ {
"pubkey": "SEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhI", "pubkey": "SEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhI",
"balance": "32000000000", "balance": "32000000000",
"exitSlot": "999999999999999999",
"status": "1" "status": "1"
} }
] ]