Skip to content

Commit 0f2ba07

Browse files
committed
common, core, light: add block age into info logs
1 parent c37238c commit 0f2ba07

4 files changed

Lines changed: 74 additions & 14 deletions

File tree

‎common/format.go‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,45 @@ func (d PrettyDuration) String() string {
3838
}
3939
return label
4040
}
41+
42+
// PrettyAge is a pretty printed version of a time.Duration value that rounds
43+
// the values up to a single most significant unit, days/weeks/years included.
44+
type PrettyAge time.Time
45+
46+
// ageUnits is a list of units the age pretty printing uses.
47+
var ageUnits = []struct {
48+
Size time.Duration
49+
Symbol string
50+
}{
51+
{12 * 30 * 24 * time.Hour, "y"},
52+
{30 * 24 * time.Hour, "mo"},
53+
{7 * 24 * time.Hour, "w"},
54+
{24 * time.Hour, "d"},
55+
{time.Hour, "h"},
56+
{time.Minute, "m"},
57+
{time.Second, "s"},
58+
}
59+
60+
// String implements the Stringer interface, allowing pretty printing of duration
61+
// values rounded to the most significant time unit.
62+
func (t PrettyAge) String() string {
63+
// Calculate the time difference and handle the 0 cornercase
64+
diff := time.Since(time.Time(t))
65+
if diff < time.Second {
66+
return "0"
67+
}
68+
// Accumulate a precision of 3 components before returning
69+
result, prec := "", 0
70+
71+
for _, unit := range ageUnits {
72+
if diff > unit.Size {
73+
result = fmt.Sprintf("%s%d%s", result, diff/unit.Size, unit.Symbol)
74+
diff %= unit.Size
75+
76+
if prec += 1; prec >= 3 {
77+
break
78+
}
79+
}
80+
}
81+
return result
82+
}

‎core/blockchain.go‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ func (bc *BlockChain) loadLastState() error {
251251
blockTd := bc.GetTd(currentBlock.Hash(), currentBlock.NumberU64())
252252
fastTd := bc.GetTd(currentFastBlock.Hash(), currentFastBlock.NumberU64())
253253

254-
log.Info("Loaded most recent local header", "number", currentHeader.Number, "hash", currentHeader.Hash(), "td", headerTd)
255-
log.Info("Loaded most recent local full block", "number", currentBlock.Number(), "hash", currentBlock.Hash(), "td", blockTd)
256-
log.Info("Loaded most recent local fast block", "number", currentFastBlock.Number(), "hash", currentFastBlock.Hash(), "td", fastTd)
254+
log.Info("Loaded most recent local header", "number", currentHeader.Number, "hash", currentHeader.Hash(), "td", headerTd, "age", common.PrettyAge(time.Unix(currentHeader.Time.Int64(), 0)))
255+
log.Info("Loaded most recent local full block", "number", currentBlock.Number(), "hash", currentBlock.Hash(), "td", blockTd, "age", common.PrettyAge(time.Unix(currentBlock.Time().Int64(), 0)))
256+
log.Info("Loaded most recent local fast block", "number", currentFastBlock.Number(), "hash", currentFastBlock.Hash(), "td", fastTd, "age", common.PrettyAge(time.Unix(currentFastBlock.Time().Int64(), 0)))
257257

258258
return nil
259259
}
@@ -850,13 +850,16 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
850850
}
851851
bc.mu.Unlock()
852852

853-
log.Info("Imported new block receipts",
854-
"count", stats.processed,
855-
"elapsed", common.PrettyDuration(time.Since(start)),
856-
"number", head.Number(),
857-
"hash", head.Hash(),
853+
context := []interface{}{
854+
"count", stats.processed, "elapsed", common.PrettyDuration(time.Since(start)),
855+
"number", head.Number(), "hash", head.Hash(), "age", common.PrettyAge(time.Unix(head.Time().Int64(), 0)),
858856
"size", common.StorageSize(bytes),
859-
"ignored", stats.ignored)
857+
}
858+
if stats.ignored > 0 {
859+
context = append(context, []interface{}{"ignored", stats.ignored}...)
860+
}
861+
log.Info("Imported new block receipts", context...)
862+
860863
return 0, nil
861864
}
862865

@@ -1229,8 +1232,13 @@ func (st *insertStats) report(chain []*types.Block, index int, cache common.Stor
12291232
context := []interface{}{
12301233
"blocks", st.processed, "txs", txs, "mgas", float64(st.usedGas) / 1000000,
12311234
"elapsed", common.PrettyDuration(elapsed), "mgasps", float64(st.usedGas) * 1000 / float64(elapsed),
1232-
"number", end.Number(), "hash", end.Hash(), "cache", cache,
1235+
"number", end.Number(), "hash", end.Hash(),
12331236
}
1237+
if timestamp := time.Unix(end.Time().Int64(), 0); time.Since(timestamp) > time.Minute {
1238+
context = append(context, []interface{}{"age", common.PrettyAge(timestamp)}...)
1239+
}
1240+
context = append(context, []interface{}{"cache", cache}...)
1241+
12341242
if st.queued > 0 {
12351243
context = append(context, []interface{}{"queued", st.queued}...)
12361244
}

‎core/headerchain.go‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,18 @@ func (hc *HeaderChain) InsertHeaderChain(chain []*types.Header, writeHeader WhCa
281281
}
282282
// Report some public statistics so the user has a clue what's going on
283283
last := chain[len(chain)-1]
284-
log.Info("Imported new block headers", "count", stats.processed, "elapsed", common.PrettyDuration(time.Since(start)),
285-
"number", last.Number, "hash", last.Hash(), "ignored", stats.ignored)
284+
285+
context := []interface{}{
286+
"count", stats.processed, "elapsed", common.PrettyDuration(time.Since(start)),
287+
"number", last.Number, "hash", last.Hash(),
288+
}
289+
if timestamp := time.Unix(last.Time.Int64(), 0); time.Since(timestamp) > time.Minute {
290+
context = append(context, []interface{}{"age", common.PrettyAge(timestamp)}...)
291+
}
292+
if stats.ignored > 0 {
293+
context = append(context, []interface{}{"ignored", stats.ignored}...)
294+
}
295+
log.Info("Imported new block headers", context...)
286296

287297
return 0, nil
288298
}

‎light/lightchain.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (self *LightChain) loadLastState() error {
157157
// Issue a status log and return
158158
header := self.hc.CurrentHeader()
159159
headerTd := self.GetTd(header.Hash(), header.Number.Uint64())
160-
log.Info("Loaded most recent local header", "number", header.Number, "hash", header.Hash(), "td", headerTd)
160+
log.Info("Loaded most recent local header", "number", header.Number, "hash", header.Hash(), "td", headerTd, "age", common.PrettyAge(time.Unix(header.Time.Int64(), 0)))
161161

162162
return nil
163163
}
@@ -488,7 +488,7 @@ func (self *LightChain) SyncCht(ctx context.Context) bool {
488488

489489
// Ensure the chain didn't move past the latest block while retrieving it
490490
if self.hc.CurrentHeader().Number.Uint64() < header.Number.Uint64() {
491-
log.Info("Updated latest header based on CHT", "number", header.Number, "hash", header.Hash())
491+
log.Info("Updated latest header based on CHT", "number", header.Number, "hash", header.Hash(), "age", common.PrettyAge(time.Unix(header.Time.Int64(), 0)))
492492
self.hc.SetCurrentHeader(header)
493493
}
494494
return true

0 commit comments

Comments
 (0)