diff --git a/cmd/bfa_client/bfa_client.go b/cmd/bfa_client/bfa_client.go index 4ba1ab506cb83fce825fe534cf1920980449ffb1..774d9b659f476cbe0492a7991540c7d4d668e4ad 100644 --- a/cmd/bfa_client/bfa_client.go +++ b/cmd/bfa_client/bfa_client.go @@ -76,7 +76,7 @@ func updateURL(url string) (updated string) { // First, we try IPC updated = defaultIPC if bfaNetworkDir := os.Getenv("BFANETWORKDIR"); bfaNetworkDir != "" { - updated = bfaNetworkDir + "/node/get.ipc" + updated = bfaNetworkDir + "/node/geth.ipc" } if fileInfo, err := os.Stat(updated); err == nil && (fileInfo.Mode()&os.ModeSocket) != 0 { return @@ -573,16 +573,40 @@ func transfers() { } } +type blockStats struct { + BlockNumber int64 `json:"block_number,omitempty"` + Timestamp uint64 `json:"timestamp,omitempty"` +} + // Stats collect statistics about a sealer type Stats struct { - FirstSeen int64 `json:"first_seen,omitempty"` - LastSeen int64 `json:"last_seen,omitempty"` - FirstBlockSealed int64 `json:"first_block_sealed,omitempty"` - LastBlockSealed int64 `json:"last_block_sealed,omitempty"` - BlocksSealed int64 `json:"blocks_sealed"` - BlocksAlive int64 `json:"blocks_alive"` - Availability float64 `json:"availability"` - recent bool + FirstSeen blockStats `json:"first_seen,omitempty"` + LastSeen blockStats `json:"last_seen,omitempty"` + FirstSealed blockStats `json:"first_sealed,omitempty"` + LastSealed blockStats `json:"last_sealed,omitempty"` + BlocksSealed int64 `json:"blocks_sealed"` + BlocksAlive int64 `json:"blocks_alive"` + Availability float64 `json:"availability"` + recent bool +} + +type blockTimestamp struct { + cache map[int64]uint64 + node *bfa.Node +} + +func (bt *blockTimestamp) init(node *bfa.Node) { + bt.cache = make(map[int64]uint64) + bt.node = node +} + +func (bt *blockTimestamp) get(blockNumber int64) (timestamp uint64) { + if timestamp, ok := bt.cache[blockNumber]; ok { + return timestamp + } + timestamp = uint64(bt.node.BlockByNumber(blockNumber).Time) + bt.cache[blockNumber] = timestamp + return } func calculateSealerStats(url string, first int64, last int64, factor int64) (sealerStats map[bfa.Address]*Stats) { @@ -633,9 +657,9 @@ func calculateSealerStats(url string, first int64, last int64, factor int64) (se blocksProcessed++ if stats, ok := sealerStats[sealer]; ok { // We are interested in this sealer - stats.LastBlockSealed = blockNumber - if stats.FirstBlockSealed == 0 { - stats.FirstBlockSealed = blockNumber + stats.LastSealed.BlockNumber = blockNumber + if stats.FirstSealed.BlockNumber == 0 { + stats.FirstSealed.BlockNumber = blockNumber } stats.BlocksSealed++ stats.recent = true @@ -644,23 +668,23 @@ func calculateSealerStats(url string, first int64, last int64, factor int64) (se for sealer := range snapshot.Signers { if stats, ok := sealerStats[sealer]; ok { // We are only interested if the sealer is in sealersStats - stats.LastSeen = block + stats.LastSeen.BlockNumber = block var firstSeen bool - if stats.FirstSeen == 0 { + if stats.FirstSeen.BlockNumber == 0 { // we have never seen this sealer before fmt.Fprintf(os.Stderr, "Nuevo sellador: %v\n", sealer.Hex()) - stats.FirstSeen = block + stats.FirstSeen.BlockNumber = block firstSeen = true } if !stats.recent { // sealer is not in snapshot.Recents - if stats.FirstBlockSealed > 0 { + if stats.FirstSealed.BlockNumber > 0 { // Sealer has signed at least once, we never zero its BlocksAlive - if stats.LastBlockSealed > shouldHaveSealedAfter { + if stats.LastSealed.BlockNumber > shouldHaveSealedAfter { stats.BlocksAlive += blocksProcessed continue } - } else if stats.FirstSeen <= shouldHaveSealedAfter { + } else if stats.FirstSeen.BlockNumber <= shouldHaveSealedAfter { // Sealer has never signed since we've seen it for the first time stats.BlocksAlive = 0 } else if !firstSeen { @@ -675,8 +699,8 @@ func calculateSealerStats(url string, first int64, last int64, factor int64) (se stats.recent = false // sealer is in snapshot.Recents if firstSeen { - stats.FirstSeen = stats.FirstBlockSealed - stats.BlocksAlive = block - stats.FirstSeen + 1 + stats.FirstSeen.BlockNumber = stats.FirstSealed.BlockNumber + stats.BlocksAlive = block - stats.FirstSeen.BlockNumber + 1 } else { stats.BlocksAlive += blocksProcessed } @@ -696,10 +720,18 @@ func calculateSealerStats(url string, first int64, last int64, factor int64) (se } } for _, stats := range sealerStats { - if stats.LastSeen == 0 { + bt := blockTimestamp{} + bt.init(node) + if stats.LastSeen.BlockNumber == 0 { continue } - blocksSeen := stats.LastSeen - stats.FirstSeen + 1 + stats.LastSeen.Timestamp = bt.get(stats.LastSeen.BlockNumber) + stats.FirstSeen.Timestamp = bt.get(stats.FirstSeen.BlockNumber) + if stats.FirstSealed.BlockNumber > 0 { + stats.FirstSealed.Timestamp = bt.get(stats.FirstSealed.BlockNumber) + stats.LastSealed.Timestamp = bt.get(stats.LastSealed.BlockNumber) + } + blocksSeen := stats.LastSeen.BlockNumber - stats.FirstSeen.BlockNumber + 1 stats.Availability = float64(stats.BlocksAlive) / float64(blocksSeen) } return