prysm-pulse/beacon-chain/sync/initial-sync/sync_state.go
Raul Jordan eb900a8193
Refactor Initial Sync, Enable Catching Up to Chain (#2111)
* refactor initial sync to prevent reorg infinite loops

* lint

* fixed build

* passing tests

* tests passing

* terence suggestion

* new attempt

* clean up and refactor sync service

* complete the new initial sync logic

* revert head

* init sync working

* config for blockchain receive block

* all works

* builds

* fix a few more tests

* init sync tests pass

* revert scripts

* revert accounts changes

* lint

* lint2

* travis lint

* fix build

* fix single use argument

* any peer

* imports spacing

* imports

* ready for a rolling restart

* add todo

* fork choice in blocks when exiting sync

* readd finalized state root to requests

* successful build

* revert blockchain config

* old config reversion

* initial sync tests pass

* initial sync full test works

* lint

* use the new block processing api

* new proto defs

* init sync functions again

* remove sync polling

* tests fixed

* fixed catching up with chain

* tests pass

* spacing

* lint

* goimports

* add changes

* add lock and conditional to prevent multiple goroutines

* make reg sync synchronous

* add

* fixed the parent block issue

* fix errors in chain service

* tests pass

* check nil block

* typo

* fix nil state

* merge & conflicts

* revert synchronus reg sync

* add more spans to state db

* fix lint

* lint
2019-04-03 10:13:19 -05:00

112 lines
3.4 KiB
Go

package initialsync
import (
"context"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/p2p"
"github.com/prysmaticlabs/prysm/shared/params"
"go.opencensus.io/trace"
)
func (s *InitialSync) processState(msg p2p.Message) {
ctx, span := trace.StartSpan(msg.Ctx, "beacon-chain.sync.initial-sync.processState")
defer span.End()
data := msg.Data.(*pb.BeaconStateResponse)
finalizedState := data.FinalizedState
justifiedState := data.JustifiedState
canonicalState := data.CanonicalState
recState.Inc()
if err := s.db.SaveFinalizedState(finalizedState); err != nil {
log.Errorf("Unable to set received last finalized state in db: %v", err)
return
}
if err := s.db.SaveHistoricalState(finalizedState); err != nil {
log.Errorf("Could not save new historical state: %v", err)
return
}
if err := s.db.SaveFinalizedBlock(finalizedState.LatestBlock); err != nil {
log.Errorf("Could not save finalized block %v", err)
return
}
if err := s.db.SaveBlock(finalizedState.LatestBlock); err != nil {
log.Errorf("Could not save block %v", err)
return
}
if err := s.db.SaveJustifiedState(justifiedState); err != nil {
log.Errorf("Could not set beacon state for initial sync %v", err)
return
}
if err := s.db.SaveHistoricalState(justifiedState); err != nil {
log.Errorf("Could not save new historical state: %v", err)
return
}
if err := s.db.SaveJustifiedBlock(justifiedState.LatestBlock); err != nil {
log.Errorf("Could not save finalized block %v", err)
return
}
if err := s.db.SaveBlock(justifiedState.LatestBlock); err != nil {
log.Errorf("Could not save finalized block %v", err)
return
}
exists, blkNum, err := s.powchain.BlockExists(ctx, bytesutil.ToBytes32(finalizedState.LatestEth1Data.BlockHash32))
if err != nil {
log.Errorf("Unable to get powchain block %v", err)
}
if !exists {
log.Error("Latest ETH1 block doesn't exist in the pow chain")
return
}
s.db.PrunePendingDeposits(ctx, blkNum)
if err := s.db.SaveBlock(canonicalState.LatestBlock); err != nil {
log.Errorf("Could not save block %v", err)
return
}
if err := s.db.UpdateChainHead(ctx, finalizedState.LatestBlock, finalizedState); err != nil {
log.Errorf("Could not update chain head %v", err)
return
}
if err := s.db.SaveHistoricalState(canonicalState); err != nil {
log.Errorf("Could not save new historical state: %v", err)
return
}
// sets the current slot to the last finalized slot of the
// beacon state to begin our sync from.
s.currentSlot = finalizedState.Slot
s.stateReceived = true
s.highestObservedCanonicalState = canonicalState
s.highestObservedSlot = canonicalState.Slot
log.Debugf(
"Successfully saved beacon state with the last finalized slot: %d, canonical slot: %d",
finalizedState.Slot-params.BeaconConfig().GenesisSlot,
canonicalState.Slot-params.BeaconConfig().GenesisSlot,
)
s.requestBatchedBlocks(s.currentSlot+1, s.highestObservedSlot)
s.lastRequestedSlot = s.highestObservedSlot
}
// requestStateFromPeer requests for the canonical state, finalized state, and justified state from a peer.
func (s *InitialSync) requestStateFromPeer(ctx context.Context, lastFinalizedRoot [32]byte) error {
ctx, span := trace.StartSpan(ctx, "beacon-chain.sync.initial-sync.requestStateFromPeer")
defer span.End()
stateReq.Inc()
return s.p2p.Send(ctx, &pb.BeaconStateRequest{
FinalizedStateRootHash32S: lastFinalizedRoot[:],
}, p2p.AnyPeer)
}