Skip to content

Commit 6e3e139

Browse files
authored
Fix an off-by-one in the powerstore (#736)
Also fix the test.
1 parent c5b5134 commit 6e3e139

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

internal/powerstore/powerstore.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ func (ps *Store) f3PowerBase(ctx context.Context) (int64, uint64, error) {
125125
baseInstance := ps.manifest.InitialInstance
126126
if lastCert := ps.cs.Latest(); lastCert != nil {
127127
baseInstance = lastCert.GPBFTInstance + 1
128-
if lastCert.GPBFTInstance > ps.manifest.InitialInstance+ps.manifest.CommitteeLookback {
129-
baseCert, err := ps.cs.Get(ctx, lastCert.GPBFTInstance-ps.manifest.CommitteeLookback)
128+
if baseInstance >= ps.manifest.InitialInstance+ps.manifest.CommitteeLookback {
129+
baseCert, err := ps.cs.Get(ctx, baseInstance-ps.manifest.CommitteeLookback)
130130
if err != nil {
131131
return 0, 0, err
132132
}

internal/powerstore/powerstore_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package powerstore_test
22

33
import (
44
"context"
5+
"math/rand/v2"
56
"slices"
67
"testing"
78
"time"
@@ -200,12 +201,12 @@ func advanceF3(t *testing.T, m *manifest.Manifest, ps *powerstore.Store, cs *cer
200201
basePt, err := cs.GetPowerTable(ctx, instance)
201202
require.NoError(t, err)
202203
for len(gpbftChain) > 1 {
203-
count := min(len(gpbftChain), 1+epochsPerCert, gpbft.ChainMaxLen)
204+
count := min(len(gpbftChain), rand.IntN(epochsPerCert+1)+1, gpbft.ChainMaxLen)
204205
newChain := gpbftChain[:count]
205206

206207
nextPt := basePt
207-
if instance >= m.InitialInstance+m.CommitteeLookback {
208-
ptCert, err := cs.Get(ctx, instance-m.CommitteeLookback)
208+
if instance+1 >= m.InitialInstance+m.CommitteeLookback {
209+
ptCert, err := cs.Get(ctx, instance+1-m.CommitteeLookback)
209210
require.NoError(t, err)
210211
nextPt, err = ps.GetPowerTable(ctx, ptCert.ECChain.Head().Key)
211212
require.NoError(t, err)

0 commit comments

Comments
 (0)