Skip to content

Commit

Permalink
Fixes race condition at genesis (#5016)
Browse files Browse the repository at this point in the history
* fixes race condition at genesis
  • Loading branch information
farazdagi committed Mar 5, 2020
1 parent 6158a64 commit b4aaa61
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 2 additions & 1 deletion beacon-chain/blockchain/receive_attestation.go
Expand Up @@ -73,10 +73,11 @@ func (s *Service) IsValidAttestation(ctx context.Context, att *ethpb.Attestation
}

// This processes attestations from the attestation pool to account for validator votes and fork choice.
func (s *Service) processAttestation() {
func (s *Service) processAttestation(subscribedToStateEvents chan struct{}) {
// Wait for state to be initialized.
stateChannel := make(chan *feed.Event, 1)
stateSub := s.stateNotifier.StateFeed().Subscribe(stateChannel)
subscribedToStateEvents <- struct{}{}
<-stateChannel
stateSub.Unsubscribe()

Expand Down
6 changes: 5 additions & 1 deletion beacon-chain/blockchain/service.go
Expand Up @@ -137,6 +137,9 @@ func (s *Service) Start() {
}
}

// Make sure that attestation processor is subscribed and ready for state initializing event.
attestationProcessorSubscribed := make(chan struct{}, 1)

// If the chain has already been initialized, simply start the block processing routine.
if beaconState != nil {
log.Info("Blockchain data already exists in DB, initializing...")
Expand Down Expand Up @@ -183,6 +186,7 @@ func (s *Service) Start() {
stateChannel := make(chan *feed.Event, 1)
stateSub := s.stateNotifier.StateFeed().Subscribe(stateChannel)
defer stateSub.Unsubscribe()
<-attestationProcessorSubscribed
for {
select {
case event := <-stateChannel:
Expand All @@ -203,7 +207,7 @@ func (s *Service) Start() {
}()
}

go s.processAttestation()
go s.processAttestation(attestationProcessorSubscribed)
}

// processChainStartTime initializes a series of deposits from the ChainStart deposits in the eth1
Expand Down

0 comments on commit b4aaa61

Please sign in to comment.