Skip to content

Commit

Permalink
refactor(checkpoint_iterator): improve sleep duration calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
samcm committed Sep 13, 2023
1 parent 8b5b865 commit a0c9d8a
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions pkg/cannon/iterator/checkpoint_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (c *CheckpointIterator) Next(ctx context.Context) (*xatu.CannonLocation, er
// If location is empty we haven't started yet, start at the network default for the type. If the network default
// is empty, we'll start at epoch 0.
if location == nil {
location, err = c.createLocationFromEpochNumber(phase0.Epoch(GetDefaultSlotLocationForNetworkAndType(c.networkName, c.cannonType) * 32))
location, err = c.createLocationFromEpochNumber(phase0.Epoch(GetDefaultSlotLocationForNetworkAndType(c.networkName, c.cannonType) / 32))
if err != nil {
return nil, errors.Wrap(err, "failed to create location from slot number 0")
}
Expand All @@ -86,7 +86,15 @@ func (c *CheckpointIterator) Next(ctx context.Context) (*xatu.CannonLocation, er
// Sleep until the next epoch
epoch := c.wallclock.Epochs().Current()

time.Sleep(time.Until(epoch.TimeWindow().End()))
sleepFor := time.Until(epoch.TimeWindow().End())

c.log.WithFields(logrus.Fields{
"current_epoch": epoch.Number(),
"sleep_for": sleepFor.String(),
"checkpoint_epoch": checkpoint.Epoch,
}).Trace("Sleeping until next epoch")

time.Sleep(sleepFor)

// Sleep for an additional 5 seconds to give the beacon node time to do epoch processing.
time.Sleep(5 * time.Second)
Expand All @@ -108,7 +116,7 @@ func (c *CheckpointIterator) Next(ctx context.Context) (*xatu.CannonLocation, er
}

func (c *CheckpointIterator) fetchLatestEpoch(ctx context.Context) (*phase0.Checkpoint, error) {
finality, err := c.beaconNode.Node().FetchFinality(ctx, "head")
finality, err := c.beaconNode.Node().Finality()
if err != nil {
return nil, errors.Wrap(err, "failed to fetch finality")
}
Expand Down

0 comments on commit a0c9d8a

Please sign in to comment.