Fix Weird Pre-Genesis Timestamp (#6031)

* ensure test passes

* Apply suggestions from code review

* comment

Co-authored-by: Ivan Martinez <ivanthegreatdev@gmail.com>
This commit is contained in:
Raul Jordan 2020-05-28 20:57:03 -05:00 committed by GitHub
parent 3fe47c0043
commit 88aaee36bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View File

@ -7,6 +7,7 @@ import (
"context"
"fmt"
"sort"
"time"
ptypes "github.com/gogo/protobuf/types"
"github.com/libp2p/go-libp2p-core/network"
@ -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,

View File

@ -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,
@ -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) {