Skip to content

Commit

Permalink
Caplin: fix occassional loss of sync during ForwardSync (#13495)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giulio2002 authored Jan 19, 2025
1 parent 457a1dc commit 9d18363
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 36 deletions.
7 changes: 5 additions & 2 deletions cl/cltypes/eth1_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ func NewEth1Data() *Eth1Data {
}

func (e *Eth1Data) Copy() *Eth1Data {
copied := *e
return &copied
return &Eth1Data{
Root: e.Root,
DepositCount: e.DepositCount,
BlockHash: e.BlockHash,
}
}

func (e *Eth1Data) Equal(b *Eth1Data) bool {
Expand Down
3 changes: 3 additions & 0 deletions cl/phase1/forkchoice/fork_graph/fork_graph_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@ func (f *forkGraphDisk) getState(blockRoot libcommon.Hash, alwaysCopy bool, addC
// Traverse the blocks from top to bottom.
for i := len(blocksInTheWay) - 1; i >= 0; i-- {
if err := transition.TransitionState(copyReferencedState, blocksInTheWay[i], nil, false); err != nil {
if addChainSegment {
f.currentState = nil // reset the state if it fails here.
}
return nil, err
}
}
Expand Down
42 changes: 17 additions & 25 deletions cl/phase1/forkchoice/fork_graph/fork_graph_disk_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,7 @@ func (f *forkGraphDisk) DumpBeaconStateOnDisk(blockRoot libcommon.Hash, bs *stat
return
}
f.stateDumpLock.Lock()
unlockOnDefer := true
defer func() {
if unlockOnDefer {
f.stateDumpLock.Unlock()
}
}()
defer f.stateDumpLock.Unlock()
// Truncate and then grow the buffer to the size of the state.
f.sszBuffer, err = bs.EncodeSSZ(f.sszBuffer[:0])
if err != nil {
Expand Down Expand Up @@ -170,25 +165,22 @@ func (f *forkGraphDisk) DumpBeaconStateOnDisk(blockRoot libcommon.Hash, bs *stat
log.Error("failed to sync dumped file", "err", err)
return
}
unlockOnDefer = false
go func() {
cacheFile, err := f.fs.OpenFile(getBeaconStateCacheFilename(blockRoot), os.O_TRUNC|os.O_CREATE|os.O_RDWR, 0o755)
if err != nil {
log.Error("failed to open cache file", "err", err)
return
}
defer cacheFile.Close()
defer f.stateDumpLock.Unlock()

if _, err = cacheFile.Write(b.Bytes()); err != nil {
log.Error("failed to write cache file", "err", err)
return
}
if err = cacheFile.Sync(); err != nil {
log.Error("failed to sync cache file", "err", err)
return
}
}()

cacheFile, err := f.fs.OpenFile(getBeaconStateCacheFilename(blockRoot), os.O_TRUNC|os.O_CREATE|os.O_RDWR, 0o755)
if err != nil {
log.Error("failed to open cache file", "err", err)
return
}
defer cacheFile.Close()

if _, err = cacheFile.Write(b.Bytes()); err != nil {
log.Error("failed to write cache file", "err", err)
return
}
if err = cacheFile.Sync(); err != nil {
log.Error("failed to sync cache file", "err", err)
return
}

return
}
11 changes: 2 additions & 9 deletions cl/transition/machine/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ func ProcessBlock(impl BlockProcessor, s abstract.BeaconState, block cltypes.Gen

// ProcessOperations is called by ProcessBlock and processes the block body operations
func ProcessOperations(impl BlockOperationProcessor, s abstract.BeaconState, blockBody cltypes.GenericBeaconBody) (signatures [][]byte, messages [][]byte, publicKeys [][]byte, err error) {
maxDepositsAllowed := int(min(s.BeaconConfig().MaxDeposits, s.Eth1Data().DepositCount-s.Eth1DepositIndex()))
if s.Version() <= clparams.DenebVersion {
if blockBody.GetDeposits().Len() != int(maximumDeposits(s)) {
if blockBody.GetDeposits().Len() != maxDepositsAllowed {
return nil, nil, nil, errors.New("outstanding deposits do not match maximum deposits")
}
} else if s.Version() >= clparams.ElectraVersion {
Expand Down Expand Up @@ -345,11 +346,3 @@ func processBlsToExecutionChanges(impl BlockOperationProcessor, s abstract.Beaco

return
}

func maximumDeposits(s abstract.BeaconState) (maxDeposits uint64) {
maxDeposits = s.Eth1Data().DepositCount - s.Eth1DepositIndex()
if maxDeposits > s.BeaconConfig().MaxDeposits {
maxDeposits = s.BeaconConfig().MaxDeposits
}
return
}

0 comments on commit 9d18363

Please sign in to comment.