@ -70,6 +70,7 @@ type syncer struct {
avgChunkTime int64
avgChunkTime int64
lastSyncedSnapshotHeight int64
lastSyncedSnapshotHeight int64
processingSnapshot * snapshot
processingSnapshot * snapshot
closeCh <- chan struct { }
}
}
// newSyncer creates a new syncer.
// newSyncer creates a new syncer.
@ -79,7 +80,9 @@ func newSyncer(
conn proxy . AppConnSnapshot ,
conn proxy . AppConnSnapshot ,
connQuery proxy . AppConnQuery ,
connQuery proxy . AppConnQuery ,
stateProvider StateProvider ,
stateProvider StateProvider ,
snapshotCh , chunkCh chan <- p2p . Envelope ,
snapshotCh chan <- p2p . Envelope ,
chunkCh chan <- p2p . Envelope ,
closeCh <- chan struct { } ,
tempDir string ,
tempDir string ,
metrics * Metrics ,
metrics * Metrics ,
) * syncer {
) * syncer {
@ -95,6 +98,7 @@ func newSyncer(
fetchers : cfg . Fetchers ,
fetchers : cfg . Fetchers ,
retryTimeout : cfg . ChunkRequestTimeout ,
retryTimeout : cfg . ChunkRequestTimeout ,
metrics : metrics ,
metrics : metrics ,
closeCh : closeCh ,
}
}
}
}
@ -139,10 +143,16 @@ func (s *syncer) AddSnapshot(peerID types.NodeID, snapshot *snapshot) (bool, err
// single request to discover snapshots, later we may want to do retries and stuff.
// single request to discover snapshots, later we may want to do retries and stuff.
func ( s * syncer ) AddPeer ( peerID types . NodeID ) {
func ( s * syncer ) AddPeer ( peerID types . NodeID ) {
s . logger . Debug ( "Requesting snapshots from peer" , "peer" , peerID )
s . logger . Debug ( "Requesting snapshots from peer" , "peer" , peerID )
s . snapshotCh <- p2p . Envelope {
msg := p2p . Envelope {
To : peerID ,
To : peerID ,
Message : & ssproto . SnapshotsRequest { } ,
Message : & ssproto . SnapshotsRequest { } ,
}
}
select {
case <- s . closeCh :
case s . snapshotCh <- msg :
}
}
}
// RemovePeer removes a peer from the pool.
// RemovePeer removes a peer from the pool.
@ -473,6 +483,8 @@ func (s *syncer) fetchChunks(ctx context.Context, snapshot *snapshot, chunks *ch
select {
select {
case <- ctx . Done ( ) :
case <- ctx . Done ( ) :
return
return
case <- s . closeCh :
return
case <- time . After ( 2 * time . Second ) :
case <- time . After ( 2 * time . Second ) :
continue
continue
}
}
@ -499,6 +511,8 @@ func (s *syncer) fetchChunks(ctx context.Context, snapshot *snapshot, chunks *ch
case <- ctx . Done ( ) :
case <- ctx . Done ( ) :
return
return
case <- s . closeCh :
return
}
}
ticker . Stop ( )
ticker . Stop ( )
@ -522,7 +536,7 @@ func (s *syncer) requestChunk(snapshot *snapshot, chunk uint32) {
"peer" , peer ,
"peer" , peer ,
)
)
s . chunkCh <- p2p . Envelope {
msg := p2p . Envelope {
To : peer ,
To : peer ,
Message : & ssproto . ChunkRequest {
Message : & ssproto . ChunkRequest {
Height : snapshot . Height ,
Height : snapshot . Height ,
@ -530,6 +544,11 @@ func (s *syncer) requestChunk(snapshot *snapshot, chunk uint32) {
Index : chunk ,
Index : chunk ,
} ,
} ,
}
}
select {
case s . chunkCh <- msg :
case <- s . closeCh :
}
}
}
// verifyApp verifies the sync, checking the app hash and last block height. It returns the
// verifyApp verifies the sync, checking the app hash and last block height. It returns the