diff --git a/module/block_iterator.go b/module/block_iterator.go index 5a2db6923af..7cf186500ea 100644 --- a/module/block_iterator.go +++ b/module/block_iterator.go @@ -13,23 +13,22 @@ type IterateRange struct { End uint64 // the end of the range } -// IterateProgress is an interface for reading and writing the progress of the iterator -type IterateProgress interface { - IterateProgressReader - IterateProgressWriter +// IteratorState is an interface for reading and writing the progress of the iterator +type IteratorState interface { + IteratorStateReader + IteratorStateWriter } -// IterateProgressReader reads the progress of the iterator, useful for resuming the iteration +// IteratorStateReader reads the progress of the iterator, useful for resuming the iteration // after restart -type IterateProgressReader interface { +type IteratorStateReader interface { // LoadState reads the next block to iterate - // caller must ensure the reader is created by the IterateProgressInitializer, - // otherwise LoadState would return exception. + // caller must ensure the state is initialized, otherwise LoadState would return exception. LoadState() (progress uint64, exception error) } -// IterateProgressWriter saves the progress of the iterator -type IterateProgressWriter interface { +// IteratorStateWriter saves the progress of the iterator +type IteratorStateWriter interface { // SaveState persists the next block to be iterated SaveState(uint64) (exception error) } diff --git a/module/block_iterator/creator.go b/module/block_iterator/creator.go index b192c5abb18..8227b1ddb95 100644 --- a/module/block_iterator/creator.go +++ b/module/block_iterator/creator.go @@ -14,7 +14,7 @@ import ( // a new block iterator can be created to iterate through the next range. type Creator struct { getBlockIDByIndex func(uint64) (flow.Identifier, bool, error) - progress *NextProgress + progress *PersistentIteratorState } var _ module.IteratorCreator = (*Creator)(nil) @@ -27,7 +27,7 @@ func NewCreator( latest func() (uint64, error), ) (*Creator, error) { // initialize the progress in storage, saving the root block index in storage - progress, err := NewNextProgress(progressStorage, root, latest) + progress, err := NewPersistentIteratorState(progressStorage, root, latest) if err != nil { return nil, fmt.Errorf("failed to initialize progress: %w", err) } diff --git a/module/block_iterator/iterator.go b/module/block_iterator/iterator.go index 81563dd0c78..c555ec34c10 100644 --- a/module/block_iterator/iterator.go +++ b/module/block_iterator/iterator.go @@ -15,9 +15,9 @@ import ( type IndexedBlockIterator struct { // dependencies getBlockIDByIndex func(uint64) (blockID flow.Identifier, indexed bool, exception error) - progress module.IterateProgressWriter // for saving the next index to be iterated for resuming the iteration - endIndex uint64 // the end index to iterate, this never change - nextIndex uint64 // the start index to iterate, this will be updated after each iteration + progress module.IteratorStateWriter // for saving the next index to be iterated for resuming the iteration + endIndex uint64 // the end index to iterate, this never change + nextIndex uint64 // the start index to iterate, this will be updated after each iteration } var _ module.BlockIterator = (*IndexedBlockIterator)(nil) @@ -25,7 +25,7 @@ var _ module.BlockIterator = (*IndexedBlockIterator)(nil) // caller must ensure that both iterRange.Start and iterRange.End are finalized func NewIndexedBlockIterator( getBlockIDByIndex func(uint64) (blockID flow.Identifier, indexed bool, exception error), - progress module.IterateProgressWriter, + progress module.IteratorStateWriter, iterRange module.IterateRange, ) module.BlockIterator { return &IndexedBlockIterator{ diff --git a/module/block_iterator/iterator_test.go b/module/block_iterator/iterator_test.go index e6736c7c4ee..ac25a6b5391 100644 --- a/module/block_iterator/iterator_test.go +++ b/module/block_iterator/iterator_test.go @@ -81,8 +81,8 @@ type saveNextHeight struct { savedNextHeight uint64 } -var _ module.IterateProgressWriter = (*saveNextHeight)(nil) -var _ module.IterateProgressReader = (*saveNextHeight)(nil) +var _ module.IteratorStateWriter = (*saveNextHeight)(nil) +var _ module.IteratorStateReader = (*saveNextHeight)(nil) func (s *saveNextHeight) SaveState(height uint64) error { s.savedNextHeight = height diff --git a/module/block_iterator/progress.go b/module/block_iterator/state.go similarity index 66% rename from module/block_iterator/progress.go rename to module/block_iterator/state.go index db9599534a8..262f5b74d4d 100644 --- a/module/block_iterator/progress.go +++ b/module/block_iterator/state.go @@ -8,14 +8,15 @@ import ( "github.com/onflow/flow-go/storage" ) -type NextProgress struct { +// PersistentIteratorState stores the state of the iterator in a persistent storage +type PersistentIteratorState struct { store storage.ConsumerProgress latest func() (uint64, error) } -var _ module.IterateProgress = (*NextProgress)(nil) +var _ module.IteratorState = (*PersistentIteratorState)(nil) -func NewNextProgress(store storage.ConsumerProgress, root uint64, latest func() (uint64, error)) (*NextProgress, error) { +func NewPersistentIteratorState(store storage.ConsumerProgress, root uint64, latest func() (uint64, error)) (*PersistentIteratorState, error) { _, err := store.ProcessedIndex() if errors.Is(err, storage.ErrNotFound) { next := root + 1 @@ -25,22 +26,22 @@ func NewNextProgress(store storage.ConsumerProgress, root uint64, latest func() } } - return &NextProgress{ + return &PersistentIteratorState{ store: store, latest: latest, }, nil } -func (n *NextProgress) LoadState() (uint64, error) { +func (n *PersistentIteratorState) LoadState() (uint64, error) { return n.store.ProcessedIndex() } -func (n *NextProgress) SaveState(next uint64) error { +func (n *PersistentIteratorState) SaveState(next uint64) error { return n.store.SetProcessedIndex(next) } // NextRange returns the next range of blocks to iterate over -func (n *NextProgress) NextRange() (module.IterateRange, error) { +func (n *PersistentIteratorState) NextRange() (module.IterateRange, error) { next, err := n.LoadState() if err != nil { return module.IterateRange{}, fmt.Errorf("failed to read next height: %w", err) diff --git a/module/block_iterator/progress_test.go b/module/block_iterator/state_test.go similarity index 94% rename from module/block_iterator/progress_test.go rename to module/block_iterator/state_test.go index 1911775804b..cf805d574f0 100644 --- a/module/block_iterator/progress_test.go +++ b/module/block_iterator/state_test.go @@ -21,7 +21,7 @@ func TestProgress(t *testing.T) { store := storagepebble.NewConsumerProgress(db, "test") - progress, err := NewNextProgress(store, root, getLatest) + progress, err := NewPersistentIteratorState(store, root, getLatest) require.NoError(t, err) // initial state should be the next of root