@@ -237,7 +237,7 @@ func (api *PublicBlockChainAPI) GetDatabaseInfo() (interface{}, error) {
237
237
return ret , nil
238
238
}
239
239
240
- func (api * PublicBlockChainAPI ) GetChainInfo (lastCount int ) (interface {}, error ) {
240
+ func (api * PublicBlockChainAPI ) GetChainInfo (lastCount int , start * int64 , end * int64 ) (interface {}, error ) {
241
241
if ! api .node .GetPeerServer ().IsCurrent () {
242
242
return nil , fmt .Errorf ("Busy, try again later" )
243
243
}
@@ -246,54 +246,118 @@ func (api *PublicBlockChainAPI) GetChainInfo(lastCount int) (interface{}, error)
246
246
count = lastCount
247
247
}
248
248
md := api .node .GetBlockChain ().BlockDAG ()
249
+ var startBlock meerdag.IBlock
250
+ var endBlock meerdag.IBlock
251
+ var info * json.ChainInfoResult
249
252
mainTip := md .GetMainChainTip ()
250
- var start meerdag.IBlock
251
- var blockNode * blockchain.BlockNode
252
- info := json.ChainInfoResult {Count : 0 , End : fmt .Sprintf ("%s (order:%d)" , mainTip .GetHash ().String (), mainTip .GetOrder ())}
253
253
totalTxs := 0
254
254
emptyBlocks := 0
255
- err := md .Foreach (mainTip , uint (count ), meerdag .All , func (block meerdag.IBlock ) (bool , error ) {
256
- if block .GetID () <= 0 {
257
- return true , nil
255
+ startOrder := uint (0 )
256
+ if start != nil {
257
+ startOrder = uint (* start )
258
+ }
259
+ endOrder := uint (0 )
260
+ if end != nil {
261
+ endOrder = uint (* end )
262
+ }
263
+ if startOrder > 0 {
264
+ if endOrder > 0 {
265
+ endBlock = md .GetBlockByOrder (endOrder )
266
+ if endBlock == nil {
267
+ return nil , fmt .Errorf ("No end block by order:%d" , endOrder )
268
+ }
269
+ } else {
270
+ endOrder = mainTip .GetOrder ()
258
271
}
259
- blockNode = api .node .GetBlockChain ().GetBlockNode (block )
260
- if blockNode == nil {
272
+ if startOrder >= endOrder {
273
+ return nil , fmt .Errorf ("Invalid start block by order:%d" , startOrder )
274
+ }
275
+ startBlock = md .GetBlockByOrder (startOrder )
276
+ if startBlock == nil {
277
+ return nil , fmt .Errorf ("No start block by order:%d" , startOrder )
278
+ }
279
+
280
+ info = & json.ChainInfoResult {Count : 0 }
281
+ var block meerdag.IBlock
282
+ for i := startOrder + 1 ; i <= endOrder ; i ++ {
283
+ block = md .GetBlockByOrder (i )
284
+ if block == nil {
285
+ return nil , fmt .Errorf ("No block by order:%d" , i )
286
+ }
287
+ blockNode := api .node .GetBlockChain ().GetBlockNode (block )
288
+ if blockNode == nil {
289
+ return true , nil
290
+ }
291
+ info .Count ++
292
+ totalTxs += blockNode .GetPriority ()
293
+ if blockNode .GetPriority () <= 1 {
294
+ emptyBlocks ++
295
+ }
296
+ if endBlock != nil {
297
+ continue
298
+ }
299
+ if info .Count >= uint64 (count ) {
300
+ endBlock = block
301
+ break
302
+ }
303
+ }
304
+ } else {
305
+ endBlock = mainTip
306
+ info = & json.ChainInfoResult {Count : 0 }
307
+ var blockNode * blockchain.BlockNode
308
+ err := md .Foreach (endBlock , uint (count ), meerdag .All , func (block meerdag.IBlock ) (bool , error ) {
309
+ if block .GetID () <= 0 {
310
+ return true , nil
311
+ }
312
+ blockNode = api .node .GetBlockChain ().GetBlockNode (block )
313
+ if blockNode == nil {
314
+ return true , nil
315
+ }
316
+ info .Count ++
317
+ totalTxs += blockNode .GetPriority ()
318
+ if blockNode .GetPriority () <= 1 {
319
+ emptyBlocks ++
320
+ }
321
+ startBlock = block
261
322
return true , nil
323
+ })
324
+ if err != nil {
325
+ return nil , err
326
+ }
327
+ if blockNode != nil {
328
+ totalTxs -= blockNode .GetPriority ()
329
+ if totalTxs < 0 {
330
+ totalTxs = 0
331
+ }
332
+ if blockNode .GetPriority () <= 1 {
333
+ emptyBlocks --
334
+ }
335
+ }
336
+ endNode := api .node .GetBlockChain ().GetBlockNode (endBlock )
337
+ if endNode == nil {
338
+ return nil , fmt .Errorf ("No block:%s" , endBlock .GetHash ().String ())
262
339
}
263
- info .Count ++
264
- totalTxs += blockNode .GetPriority ()
265
- if blockNode .GetPriority () <= 1 {
340
+ totalTxs += endNode .GetPriority ()
341
+ if endNode .GetPriority () <= 1 {
266
342
emptyBlocks ++
267
343
}
268
- start = block
269
- return true , nil
270
- })
271
- if err != nil {
272
- return nil , err
273
344
}
345
+
274
346
if info .Count <= 0 {
275
347
return nil , fmt .Errorf ("No blocks" )
276
348
}
277
- totalTxs -= blockNode .GetPriority ()
278
- if totalTxs < 0 {
279
- totalTxs = 0
280
- }
281
- if blockNode .GetPriority () <= 1 {
282
- emptyBlocks --
283
- }
284
- info .Start = fmt .Sprintf ("%s (order:%d)" , start .GetHash ().String (), start .GetOrder ())
285
- startNode := api .node .GetBlockChain ().GetBlockHeader (start )
349
+
350
+ info .Start = fmt .Sprintf ("%s (order:%d)" , startBlock .GetHash ().String (), startBlock .GetOrder ())
351
+ info .End = fmt .Sprintf ("%s (order:%d)" , endBlock .GetHash ().String (), endBlock .GetOrder ())
352
+ startNode := api .node .GetBlockChain ().GetBlockHeader (startBlock )
286
353
if startNode == nil {
287
- return nil , fmt .Errorf ("No block:%s" , start .GetHash ().String ())
354
+ return nil , fmt .Errorf ("No block:%s" , startBlock .GetHash ().String ())
288
355
}
289
- endNode := api .node .GetBlockChain ().GetBlockNode (mainTip )
356
+ endNode := api .node .GetBlockChain ().GetBlockNode (endBlock )
290
357
if endNode == nil {
291
- return nil , fmt .Errorf ("No block:%s" , mainTip .GetHash ().String ())
292
- }
293
- totalTxs += endNode .GetPriority ()
294
- if endNode .GetPriority () <= 1 {
295
- emptyBlocks ++
358
+ return nil , fmt .Errorf ("No block:%s" , endBlock .GetHash ().String ())
296
359
}
360
+
297
361
totalTime := endNode .GetTimestamp () - startNode .Timestamp .Unix ()
298
362
if totalTime < 0 {
299
363
totalTime = 0
@@ -304,7 +368,7 @@ func (api *PublicBlockChainAPI) GetChainInfo(lastCount int) (interface{}, error)
304
368
info .BlocksPerSecond = float64 (info .Count ) / float64 (totalTime )
305
369
info .TxsPerSecond = float64 (totalTxs ) / float64 (totalTime )
306
370
307
- totalHeight := int64 (mainTip .GetHeight ()) - int64 (start .GetHeight ())
371
+ totalHeight := int64 (endBlock .GetHeight ()) - int64 (startBlock .GetHeight ())
308
372
if totalHeight < 0 {
309
373
totalHeight = 0
310
374
}
@@ -315,7 +379,7 @@ func (api *PublicBlockChainAPI) GetChainInfo(lastCount int) (interface{}, error)
315
379
}
316
380
info .EmptyBlockRate = fmt .Sprintf ("%d%%" , uint64 (emptyBlocks * 100 )/ info .Count )
317
381
info .ProcessQueueSize = int32 (api .node .GetBlockChain ().ProcessQueueSize ())
318
- return info , nil
382
+ return * info , nil
319
383
}
320
384
321
385
type PrivateBlockChainAPI struct {
0 commit comments