Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Weird Pre-Genesis Timestamp #6031

Merged
merged 4 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions beacon-chain/rpc/node/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"sort"
"time"

ptypes "github.com/gogo/protobuf/types"
"github.com/libp2p/go-libp2p-core/network"
Expand Down Expand Up @@ -40,17 +41,25 @@ func (ns *Server) GetSyncStatus(ctx context.Context, _ *ptypes.Empty) (*ethpb.Sy
}, nil
}

// GetGenesis fetches genesis chain information of Ethereum 2.0.
// GetGenesis fetches genesis chain information of Ethereum 2.0. Returns unix timestamp 0
// if a genesis time has yet to be determined.
func (ns *Server) GetGenesis(ctx context.Context, _ *ptypes.Empty) (*ethpb.Genesis, error) {
contractAddr, err := ns.BeaconDB.DepositContractAddress(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not retrieve contract address from db: %v", err)
}
genesisTime := ns.GenesisTimeFetcher.GenesisTime()
gt, err := ptypes.TimestampProto(genesisTime)
var defaultGenesisTime time.Time
var gt *ptypes.Timestamp
if genesisTime == defaultGenesisTime {
gt, err = ptypes.TimestampProto(time.Unix(0, 0))
} else {
gt, err = ptypes.TimestampProto(genesisTime)
}
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not convert genesis time to proto: %v", err)
}

genValRoot := ns.GenesisFetcher.GenesisValidatorRoot()
return &ethpb.Genesis{
GenesisTime: gt,
Expand Down
15 changes: 14 additions & 1 deletion beacon-chain/rpc/node/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestNodeServer_GetGenesis(t *testing.T) {
genValRoot := bytesutil.ToBytes32([]byte("I am root"))
ns := &Server{
BeaconDB: db,
GenesisTimeFetcher: &mock.ChainService{Genesis: time.Unix(0, 0)},
GenesisTimeFetcher: &mock.ChainService{},
GenesisFetcher: &mock.ChainService{
State: st,
ValidatorsRoot: genValRoot,
Expand All @@ -76,6 +76,19 @@ func TestNodeServer_GetGenesis(t *testing.T) {
if !bytes.Equal(genValRoot[:], res.GenesisValidatorsRoot) {
t.Errorf("Wanted GenesisValidatorsRoot = %v, received %v", genValRoot, res.GenesisValidatorsRoot)
}

ns.GenesisTimeFetcher = &mock.ChainService{Genesis: time.Unix(10, 0)}
res, err = ns.GetGenesis(context.Background(), &ptypes.Empty{})
if err != nil {
t.Fatal(err)
}
pUnix, err = ptypes.TimestampProto(time.Unix(10, 0))
if err != nil {
t.Fatal(err)
}
if !res.GenesisTime.Equal(pUnix) {
t.Errorf("Wanted GenesisTime() = %v, received %v", pUnix, res.GenesisTime)
}
}

func TestNodeServer_GetVersion(t *testing.T) {
Expand Down