Skip to content

Commit

Permalink
minor edits
Browse files Browse the repository at this point in the history
  • Loading branch information
srene committed Oct 8, 2024
1 parent 53a5c55 commit debc18c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
14 changes: 7 additions & 7 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ type Manager struct {
// prune anything that might be submitted in the future. Therefore, it must be atomic.
LastSubmittedHeight atomic.Uint64

// The last height which was finalized after dispute period, obtained from the Hub
LastFinalizedHeight atomic.Uint64

/*
Retrieval
*/
Expand Down Expand Up @@ -307,16 +304,19 @@ func (m *Manager) syncFromSettlement() error {
m.LastSubmittedHeight.Store(res.EndHeight)
m.UpdateTargetHeight(res.EndHeight)

m.triggerStateUpdateSyncing()
m.triggerStateUpdateValidation()

res, err = m.SLClient.GetLatestFinalizedBatch()
if errors.Is(err, gerrc.ErrNotFound) {
// The SL hasn't got any batches for this chain yet.
m.logger.Info("No finalized batches for chain found in SL.")
return nil
}
m.LastFinalizedHeight.Store(res.EndHeight)
// update validation height with latest finalized height (it will be updated only of finalized height is higher)
m.State.SetLastValidatedHeight(res.EndHeight)

// try to sync to last state update submitted on startup
m.triggerStateUpdateSyncing()
// try to validate all pending state updates on startup
m.triggerStateUpdateValidation()

return nil
}
Expand Down
14 changes: 8 additions & 6 deletions block/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ func (m *Manager) SyncLoop(ctx context.Context) error {

}

m.triggerStateUpdateValidation()

m.logger.Info("Synced.", "current height", m.State.Height(), "last submitted height", m.LastSubmittedHeight.Load())

m.synced.Nudge()
Expand All @@ -89,20 +91,20 @@ func (m *Manager) waitForSyncing() {
}
}

func (m *Manager) triggerStateUpdateValidation() {
func (m *Manager) triggerStateUpdateSyncing() {
// Trigger state update validation.
select {
case m.validateC <- struct{}{}:
case m.syncingC <- struct{}{}:
default:
m.logger.Debug("disregarding new state update, node is still validating")
m.logger.Debug("disregarding new state update, node is still syncing")
}
}

func (m *Manager) triggerStateUpdateSyncing() {
func (m *Manager) triggerStateUpdateValidation() {
// Trigger state update validation.
select {
case m.syncingC <- struct{}{}:
case m.validateC <- struct{}{}:
default:
m.logger.Debug("disregarding new state update, node is still syncing")
m.logger.Debug("disregarding new state update, node is still validating")
}
}
15 changes: 13 additions & 2 deletions block/validate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package block

import "context"
import (
"context"
"fmt"
)

// SyncTargetLoop listens for syncing events (from new state update or from initial syncing) and syncs to the last submitted height.
// In case the node is already synced, it validate
Expand All @@ -14,7 +17,7 @@ func (m *Manager) ValidateLoop(ctx context.Context) error {

m.logger.Info("validating state updates to target height", "targetHeight", m.LastSubmittedHeight.Load())

for currH := m.State.NextValidationHeight(); currH < m.NextHeightToSubmit(); currH = m.State.NextValidationHeight() {
for currH := m.State.NextValidationHeight(); currH <= m.LastSubmittedHeight.Load(); currH = m.State.NextValidationHeight() {

// get next batch that needs to be validated from SL
batch, err := m.SLClient.GetBatchAtHeight(currH)
Expand All @@ -32,8 +35,16 @@ func (m *Manager) ValidateLoop(ctx context.Context) error {
if currH == m.State.NextValidationHeight() {
panic("validation not progressing")
}

// update state with new validation height
_, err = m.Store.SaveState(m.State, nil)
if err != nil {
return fmt.Errorf("save state: %w", err)
}

m.logger.Debug("state info validated", "batch end height", batch.EndHeight, "lastValidatedHeight", m.State.GetLastValidatedHeight())
}

}
}
}
2 changes: 1 addition & 1 deletion types/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (s *State) Height() uint64 {
// SetHeight sets the height saved in the Store if it is higher than the existing height
// returns OK if the value was updated successfully or did not need to be updated
func (s *State) SetLastValidatedHeight(height uint64) {
s.LastValidatedHeight.Store(height)
s.LastValidatedHeight.Store(max(s.GetLastValidatedHeight(), height))
}

// Height returns height of the highest block saved in the Store.
Expand Down

0 comments on commit debc18c

Please sign in to comment.