Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update current period in autopilot within for-loop #1216

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}
Loading