diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index ada4b2aa8..cbdff5427 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -22,4 +22,6 @@ Friendly reminder, we have a [bug bounty program](https://hackerone.com/tendermi ### IMPROVEMENTS +- [statesync] \#6378 Retry requests for snapshots and add a minimum discovery time (5s) for new snapshots. + ### BUG FIXES diff --git a/config/config.go b/config/config.go index 15b3ab092..1c261cf9b 100644 --- a/config/config.go +++ b/config/config.go @@ -761,6 +761,10 @@ func (cfg *StateSyncConfig) ValidateBasic() error { return errors.New("found empty rpc_servers entry") } } + if cfg.DiscoveryTime != 0 && cfg.DiscoveryTime < 5*time.Second { + return errors.New("discovery time must be 0s or greater than five seconds") + } + if cfg.TrustPeriod <= 0 { return errors.New("trusted_period is required") } diff --git a/statesync/reactor.go b/statesync/reactor.go index 62e1289d1..1a3cdbff5 100644 --- a/statesync/reactor.go +++ b/statesync/reactor.go @@ -255,11 +255,16 @@ func (r *Reactor) Sync(stateProvider StateProvider, discoveryTime time.Duration) r.syncer = newSyncer(r.Logger, r.conn, r.connQuery, stateProvider, r.tempDir) r.mtx.Unlock() - // Request snapshots from all currently connected peers - r.Logger.Debug("Requesting snapshots from known peers") - r.Switch.Broadcast(SnapshotChannel, mustEncodeMsg(&ssproto.SnapshotsRequest{})) + hook := func() { + r.Logger.Debug("Requesting snapshots from known peers") + // Request snapshots from all currently connected peers + r.Switch.Broadcast(SnapshotChannel, mustEncodeMsg(&ssproto.SnapshotsRequest{})) + } + + hook() + + state, commit, err := r.syncer.SyncAny(discoveryTime, hook) - state, commit, err := r.syncer.SyncAny(discoveryTime) r.mtx.Lock() r.syncer = nil r.mtx.Unlock() diff --git a/statesync/snapshots.go b/statesync/snapshots.go index a9ebdc9eb..067bdc1d1 100644 --- a/statesync/snapshots.go +++ b/statesync/snapshots.go @@ -173,8 +173,8 @@ func (p *snapshotPool) Ranked() []*snapshot { defer p.Unlock() candidates := make([]*snapshot, 0, len(p.snapshots)) - for _, snapshot := range p.snapshots { - candidates = append(candidates, snapshot) + for key := range p.snapshots { + candidates = append(candidates, p.snapshots[key]) } sort.Slice(candidates, func(i, j int) bool { diff --git a/statesync/syncer.go b/statesync/syncer.go index e58837bb2..f38133e3d 100644 --- a/statesync/syncer.go +++ b/statesync/syncer.go @@ -24,6 +24,9 @@ const ( chunkTimeout = 2 * time.Minute // requestTimeout is the timeout before rerequesting a chunk, possibly from a different peer. chunkRequestTimeout = 10 * time.Second + // minimumDiscoveryTime is the lowest allowable time for a + // SyncAny discovery time. + minimumDiscoveryTime = 5 * time.Second ) var ( @@ -125,7 +128,11 @@ func (s *syncer) RemovePeer(peer p2p.Peer) { // SyncAny tries to sync any of the snapshots in the snapshot pool, waiting to discover further // snapshots if none were found and discoveryTime > 0. It returns the latest state and block commit // which the caller must use to bootstrap the node. -func (s *syncer) SyncAny(discoveryTime time.Duration) (sm.State, *types.Commit, error) { +func (s *syncer) SyncAny(discoveryTime time.Duration, retryHook func()) (sm.State, *types.Commit, error) { + if discoveryTime != 0 && discoveryTime < minimumDiscoveryTime { + discoveryTime = 5 * minimumDiscoveryTime + } + if discoveryTime > 0 { s.logger.Info(fmt.Sprintf("Discovering snapshots for %v", discoveryTime)) time.Sleep(discoveryTime) @@ -148,6 +155,7 @@ func (s *syncer) SyncAny(discoveryTime time.Duration) (sm.State, *types.Commit, if discoveryTime == 0 { return sm.State{}, nil, errNoSnapshots } + retryHook() s.logger.Info(fmt.Sprintf("Discovering snapshots for %v", discoveryTime)) time.Sleep(discoveryTime) continue diff --git a/statesync/syncer_test.go b/statesync/syncer_test.go index c78509bfe..89c32f48a 100644 --- a/statesync/syncer_test.go +++ b/statesync/syncer_test.go @@ -186,7 +186,7 @@ func TestSyncer_SyncAny(t *testing.T) { LastBlockAppHash: []byte("app_hash"), }, nil) - newState, lastCommit, err := syncer.SyncAny(0) + newState, lastCommit, err := syncer.SyncAny(0, func() {}) require.NoError(t, err) time.Sleep(50 * time.Millisecond) // wait for peers to receive requests @@ -210,7 +210,7 @@ func TestSyncer_SyncAny(t *testing.T) { func TestSyncer_SyncAny_noSnapshots(t *testing.T) { syncer, _ := setupOfferSyncer(t) - _, _, err := syncer.SyncAny(0) + _, _, err := syncer.SyncAny(0, func() {}) assert.Equal(t, errNoSnapshots, err) } @@ -224,7 +224,7 @@ func TestSyncer_SyncAny_abort(t *testing.T) { Snapshot: toABCI(s), AppHash: []byte("app_hash"), }).Once().Return(&abci.ResponseOfferSnapshot{Result: abci.ResponseOfferSnapshot_ABORT}, nil) - _, _, err = syncer.SyncAny(0) + _, _, err = syncer.SyncAny(0, func() {}) assert.Equal(t, errAbort, err) connSnapshot.AssertExpectations(t) } @@ -255,7 +255,7 @@ func TestSyncer_SyncAny_reject(t *testing.T) { Snapshot: toABCI(s11), AppHash: []byte("app_hash"), }).Once().Return(&abci.ResponseOfferSnapshot{Result: abci.ResponseOfferSnapshot_REJECT}, nil) - _, _, err = syncer.SyncAny(0) + _, _, err = syncer.SyncAny(0, func() {}) assert.Equal(t, errNoSnapshots, err) connSnapshot.AssertExpectations(t) } @@ -282,7 +282,7 @@ func TestSyncer_SyncAny_reject_format(t *testing.T) { Snapshot: toABCI(s11), AppHash: []byte("app_hash"), }).Once().Return(&abci.ResponseOfferSnapshot{Result: abci.ResponseOfferSnapshot_ABORT}, nil) - _, _, err = syncer.SyncAny(0) + _, _, err = syncer.SyncAny(0, func() {}) assert.Equal(t, errAbort, err) connSnapshot.AssertExpectations(t) } @@ -320,7 +320,7 @@ func TestSyncer_SyncAny_reject_sender(t *testing.T) { Snapshot: toABCI(sa), AppHash: []byte("app_hash"), }).Once().Return(&abci.ResponseOfferSnapshot{Result: abci.ResponseOfferSnapshot_REJECT}, nil) - _, _, err = syncer.SyncAny(0) + _, _, err = syncer.SyncAny(0, func() {}) assert.Equal(t, errNoSnapshots, err) connSnapshot.AssertExpectations(t) } @@ -336,7 +336,7 @@ func TestSyncer_SyncAny_abciError(t *testing.T) { Snapshot: toABCI(s), AppHash: []byte("app_hash"), }).Once().Return(nil, errBoom) - _, _, err = syncer.SyncAny(0) + _, _, err = syncer.SyncAny(0, func() {}) assert.True(t, errors.Is(err, errBoom)) connSnapshot.AssertExpectations(t) } diff --git a/test/e2e/app/snapshots.go b/test/e2e/app/snapshots.go index 590b13cee..4ddb7ecdc 100644 --- a/test/e2e/app/snapshots.go +++ b/test/e2e/app/snapshots.go @@ -2,7 +2,6 @@ package main import ( - "crypto/sha256" "encoding/json" "errors" "fmt" @@ -88,11 +87,10 @@ func (s *SnapshotStore) Create(state *State) (abci.Snapshot, error) { if err != nil { return abci.Snapshot{}, err } - hash := sha256.Sum256(bz) snapshot := abci.Snapshot{ Height: state.Height, Format: 1, - Hash: hash[:], + Hash: hashItems(state.Values), Chunks: byteChunks(bz), } err = ioutil.WriteFile(filepath.Join(s.dir, fmt.Sprintf("%v.json", state.Height)), bz, 0644) @@ -111,10 +109,9 @@ func (s *SnapshotStore) Create(state *State) (abci.Snapshot, error) { func (s *SnapshotStore) List() ([]*abci.Snapshot, error) { s.RLock() defer s.RUnlock() - snapshots := []*abci.Snapshot{} - for _, snapshot := range s.metadata { - s := snapshot // copy to avoid pointer to range variable - snapshots = append(snapshots, &s) + snapshots := make([]*abci.Snapshot, len(s.metadata)) + for idx := range s.metadata { + snapshots[idx] = &s.metadata[idx] } return snapshots, nil }