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

Listens For State Initialized Event #7112

Merged
merged 4 commits into from
Aug 26, 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: 13 additions & 0 deletions beacon-chain/rpc/validator/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,19 @@ func (vs *Server) WaitForChainStart(req *ptypes.Empty, stream ethpb.BeaconNodeVa
}
return stream.Send(res)
}
// Handle race condition in the event the blockchain
// service isn't initialized in time and the saved head state is nil.
if event.Type == statefeed.Initialized {
data, ok := event.Data.(*statefeed.InitializedData)
if !ok {
return errors.New("event data is not type *statefeed.InitializedData")
}
res := &ethpb.ChainStartResponse{
Started: true,
GenesisTime: uint64(data.StartTime.Unix()),
}
return stream.Send(res)
}
case <-stateSub.Err():
return status.Error(codes.Aborted, "Subscriber closed, exiting goroutine")
case <-vs.Ctx.Done():
Expand Down
39 changes: 39 additions & 0 deletions beacon-chain/rpc/validator/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package validator

import (
"context"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -337,6 +338,44 @@ func TestWaitForChainStart_AlreadyStarted(t *testing.T) {
assert.NoError(t, Server.WaitForChainStart(&ptypes.Empty{}, mockStream), "Could not call RPC method")
}

func TestWaitForChainStart_HeadStateDoesNotExist(t *testing.T) {
db, _ := dbutil.SetupDB(t)
genesisValidatorRoot := [32]byte{0x01, 0x02}

// Set head state to nil
chainService := &mockChain.ChainService{State: nil}
notifier := chainService.StateNotifier()
Server := &Server{
Ctx: context.Background(),
ChainStartFetcher: &mockPOW.POWChain{
ChainFeed: new(event.Feed),
},
BeaconDB: db,
StateNotifier: chainService.StateNotifier(),
HeadFetcher: chainService,
}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockStream := mock.NewMockBeaconNodeValidator_WaitForChainStartServer(ctrl)

wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
assert.NoError(t, Server.WaitForChainStart(&ptypes.Empty{}, mockStream), "Could not call RPC method")
wg.Done()
}()
// Simulate a late state initialization event, so that
// method is able to handle race condition here.
notifier.StateFeed().Send(&feed.Event{
Type: statefeed.Initialized,
Data: &statefeed.InitializedData{
StartTime: time.Unix(0, 0),
GenesisValidatorsRoot: genesisValidatorRoot[:],
},
})
testutil.WaitTimeout(wg, time.Second)
}

func TestWaitForChainStart_NotStartedThenLogFired(t *testing.T) {
db, _ := dbutil.SetupDB(t)

Expand Down