diff --git a/autopilot/autopilot.go b/autopilot/autopilot.go index 66b5792c2..fce58b637 100644 --- a/autopilot/autopilot.go +++ b/autopilot/autopilot.go @@ -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) @@ -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 +} diff --git a/autopilot/autopilot_test.go b/autopilot/autopilot_test.go new file mode 100644 index 000000000..60143f550 --- /dev/null +++ b/autopilot/autopilot_test.go @@ -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) + } + } +}