Skip to content

Commit

Permalink
Enable local snapshot restore
Browse files Browse the repository at this point in the history
  • Loading branch information
chillyvee committed Oct 12, 2022
1 parent d32df22 commit 2b5aff4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@ type StateSyncConfig struct {
DiscoveryTime time.Duration `mapstructure:"discovery_time"`
ChunkRequestTimeout time.Duration `mapstructure:"chunk_request_timeout"`
ChunkFetchers int32 `mapstructure:"chunk_fetchers"`
RestoreHeight uint64 `mapstructure:"restore_height"`
}

func (cfg *StateSyncConfig) TrustHashBytes() []byte {
Expand Down
14 changes: 13 additions & 1 deletion statesync/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,19 @@ func (r *Reactor) Sync(stateProvider StateProvider, discoveryTime time.Duration)

hook()

state, commit, err := r.syncer.SyncAny(discoveryTime, hook)
var state sm.State
var commit *types.Commit
var err error

if r.cfg.RestoreHeight > 0 {
// Restore Height for Local Snapshot Specified
// cosmos-sdk has already restored the chunks
// finish up restoring stateStore and blockStore
r.Logger.Debug("Local Statesync - Update state and commit")
state, commit, err = r.syncer.LocalSync()
} else {
state, commit, err = r.syncer.SyncAny(discoveryTime, hook)
}

r.mtx.Lock()
r.syncer = nil
Expand Down
33 changes: 33 additions & 0 deletions statesync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type syncer struct {
tempDir string
chunkFetchers int32
retryTimeout time.Duration
restoreHeight uint64

mtx tmsync.RWMutex
chunks *chunkQueue
Expand All @@ -83,6 +84,7 @@ func newSyncer(
tempDir: tempDir,
chunkFetchers: cfg.ChunkFetchers,
retryTimeout: cfg.ChunkRequestTimeout,
restoreHeight: cfg.RestoreHeight,
}
}

Expand Down Expand Up @@ -313,6 +315,37 @@ func (s *syncer) Sync(snapshot *snapshot, chunks *chunkQueue) (sm.State, *types.
return state, commit, nil
}

// LocalSync restores stateStore and blockStore state which is not included in the snapshot
// This function assumes that the implicit trusted local snapshot chunks have already been applied
// State restoration depends on heights after the restored snapshot so the RPC dependency remains
func (s *syncer) LocalSync() (sm.State, *types.Commit, error) {
pctx, pcancel := context.WithTimeout(context.TODO(), 30*time.Second)
defer pcancel()

// Optimistically build new state, so we don't discover any light client failures at the end.
state, err := s.stateProvider.State(pctx, s.restoreHeight)
if err != nil {
s.logger.Info("failed to fetch and verify tendermint state", "err", err)
if err == light.ErrNoWitnesses {
return sm.State{}, nil, err
}
return sm.State{}, nil, errRejectSnapshot
}
commit, err := s.stateProvider.Commit(pctx, s.restoreHeight)
if err != nil {
s.logger.Info("failed to fetch and verify commit", "err", err)
if err == light.ErrNoWitnesses {
return sm.State{}, nil, err
}
return sm.State{}, nil, errRejectSnapshot
}

// Done! 🎉
s.logger.Info("🎉 Local Snapshot Restored", "height", s.restoreHeight)

return state, commit, nil
}

// offerSnapshot offers a snapshot to the app. It returns various errors depending on the app's
// response, or nil if the snapshot was accepted.
func (s *syncer) offerSnapshot(snapshot *snapshot) error {
Expand Down

0 comments on commit 2b5aff4

Please sign in to comment.