Skip to content

Commit

Permalink
Update current period in autopilot within for-loop (#1216)
Browse files Browse the repository at this point in the history
Fixes #1215
  • Loading branch information
ChrisSchinnerl committed May 7, 2024
2 parents 08bc0c7 + 59de660 commit b7e54a0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
11 changes: 10 additions & 1 deletion autopilot/autopilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ func (ap *Autopilot) buildState(ctx context.Context) (*contractor.MaintenanceSta
return nil, err
}
ap.logger.Infof("initialised current period to %d", autopilot.CurrentPeriod)
} else if nextPeriod := autopilot.CurrentPeriod + autopilot.Config.Contracts.Period; cs.BlockHeight >= nextPeriod {
} else if nextPeriod := computeNextPeriod(cs.BlockHeight, autopilot.CurrentPeriod, autopilot.Config.Contracts.Period); nextPeriod != autopilot.CurrentPeriod {
prevPeriod := autopilot.CurrentPeriod
autopilot.CurrentPeriod = nextPeriod
err := ap.bus.UpdateAutopilot(ctx, autopilot)
Expand Down Expand Up @@ -950,3 +950,12 @@ func compatV105UsabilityFilterModeCheck(usabilityMode string) error {
}
return nil
}

func computeNextPeriod(bh, currentPeriod, period uint64) uint64 {
prevPeriod := currentPeriod
nextPeriod := prevPeriod
for bh >= nextPeriod+period {
nextPeriod += period
}
return nextPeriod
}
35 changes: 35 additions & 0 deletions autopilot/autopilot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package autopilot

import "testing"

func TestComputeNextPeriod(t *testing.T) {
currentPeriod := uint64(100)
period := uint64(100)
tests := []struct {
blockHeight uint64
nextPeriod uint64
}{
{
blockHeight: 100,
nextPeriod: 100,
},
{
blockHeight: 150,
nextPeriod: 100,
},
{
blockHeight: 200,
nextPeriod: 200,
},
{
blockHeight: 400,
nextPeriod: 400,
},
}
for _, test := range tests {
nextPeriod := computeNextPeriod(test.blockHeight, currentPeriod, period)
if nextPeriod != test.nextPeriod {
t.Fatalf("expected next period to be %d, got %d", test.nextPeriod, nextPeriod)
}
}
}

0 comments on commit b7e54a0

Please sign in to comment.