Browse Source

statesync: improve e2e test outcomes (backport #6378) (#6380)

(cherry picked from commit d36a5905a6)

Co-authored-by: Sam Kleinman <garen@tychoish.com>
pull/6384/head
mergify[bot] 3 years ago
committed by GitHub
parent
commit
1614e12035
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 37 additions and 21 deletions
  1. +2
    -0
      CHANGELOG_PENDING.md
  2. +4
    -0
      config/config.go
  3. +9
    -4
      statesync/reactor.go
  4. +2
    -2
      statesync/snapshots.go
  5. +9
    -1
      statesync/syncer.go
  6. +7
    -7
      statesync/syncer_test.go
  7. +4
    -7
      test/e2e/app/snapshots.go

+ 2
- 0
CHANGELOG_PENDING.md View File

@ -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

+ 4
- 0
config/config.go View File

@ -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")
}


+ 9
- 4
statesync/reactor.go View File

@ -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()


+ 2
- 2
statesync/snapshots.go View File

@ -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 {


+ 9
- 1
statesync/syncer.go View File

@ -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


+ 7
- 7
statesync/syncer_test.go View File

@ -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)
}


+ 4
- 7
test/e2e/app/snapshots.go View File

@ -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
}


Loading…
Cancel
Save