Skip to content

Commit

Permalink
Comments fix
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Jan 9, 2023
1 parent 5536eee commit 9087bc8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
11 changes: 2 additions & 9 deletions consensus/polybft/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,22 +443,15 @@ func (s *State) insertStateSyncEvent(event *types.StateSyncEvent) error {
}

// getStateSyncEventsForCommitment returns state sync events for commitment
// based on the getAll flag, the function will try to get all events in range, and if it fails,
// it will either return an error, or return all the events it can without returning an error
func (s *State) getStateSyncEventsForCommitment(fromIndex, toIndex uint64,
getAll bool) ([]*types.StateSyncEvent, error) {
func (s *State) getStateSyncEventsForCommitment(fromIndex, toIndex uint64) ([]*types.StateSyncEvent, error) {
var events []*types.StateSyncEvent

err := s.db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(syncStateEventsBucket)
for i := fromIndex; i <= toIndex; i++ {
v := bucket.Get(itob(i))
if v == nil {
if getAll {
return errNotEnoughStateSyncs
}

return nil
return errNotEnoughStateSyncs
}

var event *types.StateSyncEvent
Expand Down
6 changes: 3 additions & 3 deletions consensus/polybft/state_sync_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ func (s *stateSyncManager) buildProofs(commitmentMsg *CommitmentMessage) error {
"toIndex", commitmentMsg.ToIndex,
)

events, err := s.state.getStateSyncEventsForCommitment(commitmentMsg.FromIndex, commitmentMsg.ToIndex, true)
events, err := s.state.getStateSyncEventsForCommitment(commitmentMsg.FromIndex, commitmentMsg.ToIndex)
if err != nil {
return err
}
Expand Down Expand Up @@ -428,8 +428,8 @@ func (s *stateSyncManager) buildCommitment() error {
fromIndex := s.nextCommittedIndex

stateSyncEvents, err := s.state.getStateSyncEventsForCommitment(fromIndex,
fromIndex+s.config.maxCommitmentSize-1, false)
if err != nil {
fromIndex+s.config.maxCommitmentSize-1)
if err != nil && !errors.Is(err, errNotEnoughStateSyncs) {
return fmt.Errorf("failed to get state sync events for commitment. Error: %w", err)
}

Expand Down
4 changes: 2 additions & 2 deletions consensus/polybft/state_sync_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func TestStateSyncerManager_AddLog_BuildCommitments(t *testing.T) {

s.AddLog(goodLog)

stateSyncs, err = s.state.getStateSyncEventsForCommitment(0, 0, true)
stateSyncs, err = s.state.getStateSyncEventsForCommitment(0, 0)
require.NoError(t, err)
require.Len(t, stateSyncs, 1)
require.Len(t, s.pendingCommitments, 0)
Expand Down Expand Up @@ -350,7 +350,7 @@ func TestStateSyncerManager_EventTracker_Sync(t *testing.T) {

time.Sleep(2 * time.Second)

events, err := s.state.getStateSyncEventsForCommitment(0, 9, true)
events, err := s.state.getStateSyncEventsForCommitment(0, 9)
require.NoError(t, err)
require.Len(t, events, 10)
}
Expand Down
12 changes: 6 additions & 6 deletions consensus/polybft/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func TestState_getStateSyncEventsForCommitment_NotEnoughEvents(t *testing.T) {
}))
}

_, err := state.getStateSyncEventsForCommitment(0, maxCommitmentSize-1, true)
_, err := state.getStateSyncEventsForCommitment(0, maxCommitmentSize-1)
assert.ErrorIs(t, err, errNotEnoughStateSyncs)
}

Expand All @@ -302,31 +302,31 @@ func TestState_getStateSyncEventsForCommitment(t *testing.T) {
t.Run("Return all - forced. Enough events", func(t *testing.T) {
t.Parallel()

events, err := state.getStateSyncEventsForCommitment(0, maxCommitmentSize-1, true)
events, err := state.getStateSyncEventsForCommitment(0, maxCommitmentSize-1)
require.NoError(t, err)
require.Equal(t, maxCommitmentSize, len(events))
})

t.Run("Return all - forced. Not enough events", func(t *testing.T) {
t.Parallel()

_, err := state.getStateSyncEventsForCommitment(0, maxCommitmentSize+1, true)
_, err := state.getStateSyncEventsForCommitment(0, maxCommitmentSize+1)
require.ErrorIs(t, err, errNotEnoughStateSyncs)
})

t.Run("Return all you can. Enough events", func(t *testing.T) {
t.Parallel()

events, err := state.getStateSyncEventsForCommitment(0, maxCommitmentSize-1, false)
events, err := state.getStateSyncEventsForCommitment(0, maxCommitmentSize-1)
assert.NoError(t, err)
assert.Equal(t, maxCommitmentSize, len(events))
})

t.Run("Return all you can. Not enough events", func(t *testing.T) {
t.Parallel()

events, err := state.getStateSyncEventsForCommitment(0, maxCommitmentSize+1, false)
assert.NoError(t, err)
events, err := state.getStateSyncEventsForCommitment(0, maxCommitmentSize+1)
assert.ErrorIs(t, err, errNotEnoughStateSyncs)
assert.Equal(t, maxCommitmentSize, len(events))
})
}
Expand Down

0 comments on commit 9087bc8

Please sign in to comment.