Skip to content

Commit f4bf128

Browse files
committed
fix: nil transaction counters
1 parent 8c8cfca commit f4bf128

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

internal/block.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ type BlockMetrics struct {
3030
}
3131

3232
func GetBlockMetrics(ctx context.Context, client *api.ClientWithResponses, round uint64, window int) (*BlockMetrics, error) {
33-
var avgs BlockMetrics
33+
var avgs = BlockMetrics{
34+
AvgTime: 0,
35+
TPS: 0,
36+
}
3437
if round < uint64(window) {
35-
avgs.AvgTime = 0
36-
avgs.TPS = 0
3738
return &avgs, nil
3839
}
3940
var format api.GetBlockParamsFormat = "json"
@@ -52,12 +53,14 @@ func GetBlockMetrics(ctx context.Context, client *api.ClientWithResponses, round
5253
// Push to the transactions count list
5354
aTimestamp := time.Duration(a.JSON200.Block["ts"].(float64)) * time.Second
5455
bTimestamp := time.Duration(b.JSON200.Block["ts"].(float64)) * time.Second
55-
//
56-
aTransactions := a.JSON200.Block["tc"].(float64)
57-
bTransactions := b.JSON200.Block["tc"].(float64)
56+
// Transaction Counter
57+
aTransactions := a.JSON200.Block["tc"]
58+
bTransactions := b.JSON200.Block["tc"]
5859

5960
avgs.AvgTime = time.Duration((int(aTimestamp - bTimestamp)) / window)
60-
avgs.TPS = (aTransactions - bTransactions) / (float64(window) * avgs.AvgTime.Seconds())
61+
if aTransactions != nil && bTransactions != nil {
62+
avgs.TPS = (aTransactions.(float64) - bTransactions.(float64)) / (float64(window) * avgs.AvgTime.Seconds())
63+
}
6164

6265
return &avgs, nil
6366
}

0 commit comments

Comments
 (0)