Skip to content

Commit e559b39

Browse files
authored
Merge pull request #6476 from oasisprotocol/peternose/bugfix/first-synced-round
go/runtime/history: Always broadcast first synced round
2 parents 76002fa + 8f26e9d commit e559b39

4 files changed

Lines changed: 28 additions & 11 deletions

File tree

.changelog/6476.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
go/runtime/history: Always broadcast first synced round

go/roothash/api/history.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type BlockHistory interface {
3636
StorageSyncCheckpoint(round uint64) error
3737

3838
// LastStorageSyncedRound returns the last runtime round which was synced to storage.
39-
LastStorageSyncedRound() (uint64, error)
39+
LastStorageSyncedRound() (uint64, bool)
4040

4141
// WatchBlocks returns a channel watching block rounds as they are committed.
4242
// If node has local storage this includes waiting for the round to be synced into storage.

go/runtime/history/history.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ func (h *runtimeHistory) StorageSyncCheckpoint(round uint64) error {
138138
h.syncRoundLock.Lock()
139139
defer h.syncRoundLock.Unlock()
140140
switch {
141+
case h.lastStorageSyncedRound == roothash.RoundInvalid:
142+
// First sync, continue below.
141143
case round < h.lastStorageSyncedRound:
142144
return fmt.Errorf("runtime/history: storage sync checkpoint at lower round (current: %d wanted: %d)", h.lastStorageSyncedRound, round)
143145
case round == h.lastStorageSyncedRound:
@@ -163,10 +165,13 @@ func (h *runtimeHistory) StorageSyncCheckpoint(round uint64) error {
163165
return nil
164166
}
165167

166-
func (h *runtimeHistory) LastStorageSyncedRound() (uint64, error) {
168+
func (h *runtimeHistory) LastStorageSyncedRound() (uint64, bool) {
167169
h.syncRoundLock.RLock()
168170
defer h.syncRoundLock.RUnlock()
169-
return h.lastStorageSyncedRound, nil
171+
if h.lastStorageSyncedRound == roothash.RoundInvalid {
172+
return 0, false
173+
}
174+
return h.lastStorageSyncedRound, true
170175
}
171176

172177
func (h *runtimeHistory) WatchBlocks() (<-chan *roothash.AnnotatedBlock, pubsub.ClosableSubscription, error) {
@@ -227,16 +232,26 @@ func (h *runtimeHistory) resolveRound(round uint64, includeStorage bool) (uint64
227232
h.syncRoundLock.RLock()
228233
defer h.syncRoundLock.RUnlock()
229234
// Also take storage sync state into account.
230-
if includeStorage && h.hasLocalStorage && h.lastStorageSyncedRound < meta.LastRound {
231-
return h.lastStorageSyncedRound, nil
235+
if includeStorage && h.hasLocalStorage {
236+
if h.lastStorageSyncedRound == roothash.RoundInvalid {
237+
return 0, roothash.ErrNotFound
238+
}
239+
if h.lastStorageSyncedRound < meta.LastRound {
240+
return h.lastStorageSyncedRound, nil
241+
}
232242
}
233243
return meta.LastRound, nil
234244
default:
235245
h.syncRoundLock.RLock()
236246
defer h.syncRoundLock.RUnlock()
237247
// Ensure round exists.
238-
if includeStorage && h.hasLocalStorage && h.lastStorageSyncedRound < round {
239-
return 0, roothash.ErrNotFound
248+
if includeStorage && h.hasLocalStorage {
249+
if h.lastStorageSyncedRound == roothash.RoundInvalid {
250+
return 0, roothash.ErrNotFound
251+
}
252+
if h.lastStorageSyncedRound < round {
253+
return 0, roothash.ErrNotFound
254+
}
240255
}
241256
return round, nil
242257
}
@@ -368,6 +383,7 @@ func New(runtimeID common.Namespace, dataDir string, prunerFactory PrunerFactory
368383
ctx: ctx,
369384
cancelCtx: cancelCtx,
370385
db: db,
386+
lastStorageSyncedRound: roothash.RoundInvalid,
371387
hasLocalStorage: hasLocalStorage,
372388
syncedBlocksNotifier: pubsub.NewBroker(true),
373389
committedBlocksNotifier: pubsub.NewBroker(true),

go/runtime/history/history_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func TestHistory(t *testing.T) {
3939
require.NoError(err, "LastConsensusHeight")
4040
require.EqualValues(0, lastHeight)
4141

42-
lastRound, err := history.LastStorageSyncedRound()
43-
require.NoError(err, "LastStorageSyncedRound")
42+
lastRound, ok := history.LastStorageSyncedRound()
43+
require.False(ok, "LastStorageSyncedRound")
4444
require.EqualValues(0, lastRound)
4545

4646
_, err = history.GetBlock(ctx, 10)
@@ -95,8 +95,8 @@ func TestHistory(t *testing.T) {
9595
err = history.StorageSyncCheckpoint(5)
9696
require.Error(err, "StorageSyncCheckpoint should fail for lower height")
9797

98-
lastRound, err = history.LastStorageSyncedRound()
99-
require.NoError(err, "LastStorageSyncedRound")
98+
lastRound, ok = history.LastStorageSyncedRound()
99+
require.True(ok, "LastStorageSyncedRound")
100100
require.EqualValues(10, lastRound)
101101

102102
gotBlk, err := history.GetBlock(ctx, 10)

0 commit comments

Comments
 (0)