@@ -109,17 +109,25 @@ func (pm *ProtocolManager) fetcher() {
109
109
// If any explicit fetches were replied to, import them
110
110
if count := len (explicit ); count > 0 {
111
111
glog .V (logger .Debug ).Infof ("Importing %d explicitly fetched blocks" , count )
112
+
113
+ // Create a closure with the retrieved blocks and origin peers
114
+ peers := make ([]* peer , 0 , count )
115
+ blocks := make ([]* types.Block , 0 , count )
116
+ for _ , block := range explicit {
117
+ hash := block .Hash ()
118
+ if announce := pending [hash ]; announce != nil {
119
+ peers = append (peers , announce .peer )
120
+ blocks = append (blocks , block )
121
+
122
+ delete (pending , hash )
123
+ }
124
+ }
125
+ // Run the importer on a new thread
112
126
go func () {
113
- for _ , block := range explicit {
114
- hash := block .Hash ()
115
-
116
- // Make sure there's still something pending to import
117
- if announce := pending [hash ]; announce != nil {
118
- delete (pending , hash )
119
- if err := pm .importBlock (announce .peer , block , nil ); err != nil {
120
- glog .V (logger .Detail ).Infof ("Failed to import explicitly fetched block: %v" , err )
121
- return
122
- }
127
+ for i := 0 ; i < len (blocks ); i ++ {
128
+ if err := pm .importBlock (peers [i ], blocks [i ], nil ); err != nil {
129
+ glog .V (logger .Detail ).Infof ("Failed to import explicitly fetched block: %v" , err )
130
+ return
123
131
}
124
132
}
125
133
}()
@@ -208,20 +216,21 @@ func (pm *ProtocolManager) synchronise(peer *peer) {
208
216
return
209
217
}
210
218
// Make sure the peer's TD is higher than our own. If not drop.
211
- if peer .td .Cmp (pm .chainman .Td ()) <= 0 {
219
+ if peer .Td () .Cmp (pm .chainman .Td ()) <= 0 {
212
220
return
213
221
}
214
222
// FIXME if we have the hash in our chain and the TD of the peer is
215
223
// much higher than ours, something is wrong with us or the peer.
216
224
// Check if the hash is on our own chain
217
- if pm .chainman .HasBlock (peer .recentHash ) {
225
+ head := peer .Head ()
226
+ if pm .chainman .HasBlock (head ) {
218
227
glog .V (logger .Debug ).Infoln ("Synchronisation canceled: head already known" )
219
228
return
220
229
}
221
230
// Get the hashes from the peer (synchronously)
222
- glog .V (logger .Detail ).Infof ("Attempting synchronisation: %v, 0x%x" , peer .id , peer . recentHash )
231
+ glog .V (logger .Detail ).Infof ("Attempting synchronisation: %v, 0x%x" , peer .id , head )
223
232
224
- err := pm .downloader .Synchronise (peer .id , peer . recentHash )
233
+ err := pm .downloader .Synchronise (peer .id , head )
225
234
switch err {
226
235
case nil :
227
236
glog .V (logger .Detail ).Infof ("Synchronisation completed" )
0 commit comments