Skip to content

Commit

Permalink
Fix Weird Pre-Genesis Timestamp (#6031)
Browse files Browse the repository at this point in the history
* ensure test passes

* Apply suggestions from code review

* comment

Co-authored-by: Ivan Martinez <ivanthegreatdev@gmail.com>
  • Loading branch information
rauljordan and 0xKiwi committed May 29, 2020
1 parent 3fe47c0 commit 88aaee3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
13 changes: 11 additions & 2 deletions beacon-chain/rpc/node/server.go
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
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

0 comments on commit 88aaee3

Please sign in to comment.