@@ -26,6 +26,7 @@ type Syncer struct {
26
26
stableDistance int64 // stable block distance
27
27
blockIdxs * BlockIdxs
28
28
scheduler * gocron.Scheduler
29
+ peers []string
29
30
}
30
31
31
32
func New (startHeight int64 , filterParams FilterParams , arNode string , conNum int , stableDistance int64 ) * Syncer {
@@ -44,6 +45,11 @@ func New(startHeight int64, filterParams FilterParams, arNode string, conNum int
44
45
}
45
46
fmt .Println ("Init arweave block indep hash_list finished..." )
46
47
48
+ peers , err := arCli .GetPeers ()
49
+ if err != nil {
50
+ panic (err )
51
+ }
52
+
47
53
return & Syncer {
48
54
curHeight : startHeight ,
49
55
FilterParams : filterParams ,
@@ -56,6 +62,7 @@ func New(startHeight int64, filterParams FilterParams, arNode string, conNum int
56
62
stableDistance : stableDistance ,
57
63
blockIdxs : idxs ,
58
64
scheduler : gocron .NewScheduler (time .UTC ),
65
+ peers : peers ,
59
66
}
60
67
}
61
68
@@ -104,7 +111,7 @@ func (s *Syncer) pollingBlock() {
104
111
if end > stableHeight {
105
112
end = stableHeight
106
113
}
107
- blocks := mustGetBlocks (start , end , s .arClient , s .blockIdxs , int (s .conNum ))
114
+ blocks := mustGetBlocks (start , end , s .arClient , s .blockIdxs , int (s .conNum ), s . peers )
108
115
log .Info ("get blocks success" , "start" , start , "end" , end )
109
116
110
117
s .curHeight = end + 1
@@ -135,7 +142,7 @@ func (s *Syncer) pollingTx() {
135
142
}
136
143
137
144
func (s * Syncer ) getTxs (b types.Block ) {
138
- txs := mustGetTxs (b .Height , b .Txs , s .arClient , int (s .conNum ))
145
+ txs := mustGetTxs (b .Height , b .Txs , s .arClient , int (s .conNum ), s . peers )
139
146
140
147
// subscribe txs
141
148
for {
@@ -180,7 +187,7 @@ func (s *Syncer) filterTx() {
180
187
}
181
188
}
182
189
183
- func mustGetTxs (blockHeight int64 , blockTxs []string , arClient * goar.Client , conNum int ) (txs []types.Transaction ) {
190
+ func mustGetTxs (blockHeight int64 , blockTxs []string , arClient * goar.Client , conNum int , peers [] string ) (txs []types.Transaction ) {
184
191
if len (blockTxs ) == 0 {
185
192
return
186
193
}
@@ -199,7 +206,7 @@ func mustGetTxs(blockHeight int64, blockTxs []string, arClient *goar.Client, con
199
206
200
207
p , _ := ants .NewPoolWithFunc (conNum , func (i interface {}) {
201
208
txId := i .(string )
202
- tx , err := getTxByIdRetry (blockHeight , arClient , txId )
209
+ tx , err := getTxByIdRetry (blockHeight , arClient , txId , peers )
203
210
if err != nil {
204
211
log .Error ("get tx by id error" , "txId" , txId , "err" , err )
205
212
// notice: must return fetch failed tx
@@ -225,7 +232,7 @@ func mustGetTxs(blockHeight int64, blockTxs []string, arClient *goar.Client, con
225
232
return
226
233
}
227
234
228
- func mustGetBlocks (start , end int64 , arClient * goar.Client , blockIdxs * BlockIdxs , conNum int ) (blocks []* types.Block ) {
235
+ func mustGetBlocks (start , end int64 , arClient * goar.Client , blockIdxs * BlockIdxs , conNum int , peers [] string ) (blocks []* types.Block ) {
229
236
if start > end {
230
237
return
231
238
}
@@ -238,7 +245,7 @@ func mustGetBlocks(start, end int64, arClient *goar.Client, blockIdxs *BlockIdxs
238
245
239
246
p , _ := ants .NewPoolWithFunc (conNum , func (i interface {}) {
240
247
height := i .(int64 )
241
- b , err := getBlockByHeightRetry (arClient , height , blockIdxs )
248
+ b , err := getBlockByHeightRetry (arClient , height , blockIdxs , peers )
242
249
if err != nil {
243
250
log .Error ("get block by height error" , "height" , height , "err" , err )
244
251
panic (err )
@@ -264,48 +271,58 @@ func mustGetBlocks(start, end int64, arClient *goar.Client, blockIdxs *BlockIdxs
264
271
return
265
272
}
266
273
267
- func getTxByIdRetry (blockHeight int64 , arCli * goar.Client , txId string ) (types.Transaction , error ) {
274
+ func getTxByIdRetry (blockHeight int64 , arCli * goar.Client , txId string , peers [] string ) (types.Transaction , error ) {
268
275
count := 0
269
276
for {
270
277
// get from trust node
271
278
tx , err := arCli .GetTransactionByID (txId )
272
- if err != nil {
273
- // get from non-trust nodes
274
- tx , err = arCli .GetTxFromPeers (txId )
275
- }
276
-
277
279
if err == nil {
278
280
// verify tx, ignore genesis block txs
279
281
if blockHeight != 0 {
280
282
err = utils .VerifyTransaction (* tx )
281
283
}
282
284
}
283
285
286
+ if err != nil {
287
+ // get from non-trust nodes
288
+ tx , err = arCli .GetTxFromPeers (txId , peers ... )
289
+ if err == nil {
290
+ // verify tx, ignore genesis block txs
291
+ if blockHeight != 0 {
292
+ err = utils .VerifyTransaction (* tx )
293
+ }
294
+ }
295
+ }
296
+
284
297
if err == nil {
285
298
return * tx , nil
286
299
}
287
300
288
- if count == 2 {
301
+ if count == 5 {
289
302
return types.Transaction {}, err
290
303
}
291
304
count ++
292
305
time .Sleep (2 * time .Second )
293
306
}
294
307
}
295
308
296
- func getBlockByHeightRetry (arCli * goar.Client , height int64 , blockIdxs * BlockIdxs ) (* types.Block , error ) {
309
+ func getBlockByHeightRetry (arCli * goar.Client , height int64 , blockIdxs * BlockIdxs , peers [] string ) (* types.Block , error ) {
297
310
count := 0
298
311
for {
299
312
b , err := arCli .GetBlockByHeight (height )
300
- if err != nil {
301
- b , err = arCli .GetBlockFromPeers (height )
302
- }
303
-
304
313
if err == nil {
305
314
// verify block
306
315
err = blockIdxs .VerifyBlock (* b )
307
316
}
308
317
318
+ if err != nil {
319
+ b , err = arCli .GetBlockFromPeers (height , peers ... )
320
+ if err == nil {
321
+ // verify block
322
+ err = blockIdxs .VerifyBlock (* b )
323
+ }
324
+ }
325
+
309
326
if err == nil {
310
327
return b , nil
311
328
}
0 commit comments