-
Notifications
You must be signed in to change notification settings - Fork 3
/
blocklogger.go
77 lines (63 loc) · 2.15 KB
/
blocklogger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) 2015-2017 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package blocklogger
import (
"time"
"github.com/bugnanetwork/bugnad/domain/consensus/model/externalapi"
"github.com/bugnanetwork/bugnad/util/mstime"
)
// BlockLogger is a type tracking the amount of blocks/headers/transactions to log the time it took to receive them
type BlockLogger struct {
receivedLogBlocks int64
receivedLogHeaders int64
receivedLogTransactions int64
lastBlockLogTime time.Time
}
// NewBlockLogger creates a new instance with zeroed blocks/headers/transactions/time counters.
func NewBlockLogger() *BlockLogger {
return &BlockLogger{
receivedLogBlocks: 0,
receivedLogHeaders: 0,
receivedLogTransactions: 0,
lastBlockLogTime: time.Now(),
}
}
// LogBlock logs a new block blue score as an information message
// to show progress to the user. In order to prevent spam, it limits logging to
// one message every 10 seconds with duration and totals included.
func (bl *BlockLogger) LogBlock(block *externalapi.DomainBlock) {
if len(block.Transactions) == 0 {
bl.receivedLogHeaders++
} else {
bl.receivedLogBlocks++
}
bl.receivedLogTransactions += int64(len(block.Transactions))
now := time.Now()
duration := now.Sub(bl.lastBlockLogTime)
if duration < time.Second*10 {
return
}
// Truncate the duration to 10s of milliseconds.
truncatedDuration := duration.Round(10 * time.Millisecond)
// Log information about new block blue score.
blockStr := "blocks"
if bl.receivedLogBlocks == 1 {
blockStr = "block"
}
txStr := "transactions"
if bl.receivedLogTransactions == 1 {
txStr = "transaction"
}
headerStr := "headers"
if bl.receivedLogBlocks == 1 {
headerStr = "header"
}
log.Infof("Processed %d %s and %d %s in the last %s (%d %s, %s)",
bl.receivedLogBlocks, blockStr, bl.receivedLogHeaders, headerStr, truncatedDuration, bl.receivedLogTransactions,
txStr, mstime.UnixMilliseconds(block.Header.TimeInMilliseconds()))
bl.receivedLogBlocks = 0
bl.receivedLogHeaders = 0
bl.receivedLogTransactions = 0
bl.lastBlockLogTime = now
}