mirror of
https://github.com/luxfi/sdk.git
synced 2026-10-30 15:19:23 +00:00
Lux Partners Limited is not the entity behind this work. Every place the old name stood — copyright lines, site footers, brand config, legal and compliance text — now names the company that actually holds it. lux.fund is the exception and is not renamed to match: that site belongs to Lux Assets Management LLC, a different company, and it says so. Two kinds of line were left for a person rather than rewritten. A dated news item recording that Lux Partners Limited was established in the Isle of Man is a historical fact, and renaming it would falsify the record. And a sentence asserting that the company is domiciled in the Isle of Man is a claim about a jurisdiction, not a name — an Inc or an LLC is not an Isle of Man company, so swapping the name there would assert something untrue. Co-authored-by: Hanzo Dev <[email protected]>
124 lines
2.6 KiB
Go
124 lines
2.6 KiB
Go
// Copyright (C) 2020-2025, Lux Industries Inc All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package blockchain
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// Metrics tracks blockchain performance metrics
|
|
type Metrics struct {
|
|
mu sync.RWMutex
|
|
|
|
// Block metrics
|
|
BlocksProduced uint64
|
|
LastBlockTime time.Time
|
|
AverageBlockTime time.Duration
|
|
|
|
// Transaction metrics
|
|
TxProcessed uint64
|
|
TxFailed uint64
|
|
TPS float64
|
|
|
|
// Network metrics
|
|
PeersConnected int
|
|
NetworkLatency time.Duration
|
|
|
|
// Resource metrics
|
|
CPUUsage float64
|
|
MemoryUsage uint64
|
|
DiskUsage uint64
|
|
}
|
|
|
|
// NewMetrics creates a new metrics instance
|
|
func NewMetrics() *Metrics {
|
|
return &Metrics{}
|
|
}
|
|
|
|
// RecordBlock records a new block
|
|
func (m *Metrics) RecordBlock(blockTime time.Time) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
|
|
m.BlocksProduced++
|
|
if !m.LastBlockTime.IsZero() {
|
|
interval := blockTime.Sub(m.LastBlockTime)
|
|
if m.AverageBlockTime == 0 {
|
|
m.AverageBlockTime = interval
|
|
} else {
|
|
// Exponential moving average
|
|
m.AverageBlockTime = time.Duration(float64(m.AverageBlockTime)*0.9 + float64(interval)*0.1)
|
|
}
|
|
}
|
|
m.LastBlockTime = blockTime
|
|
}
|
|
|
|
// RecordTransaction records a transaction
|
|
func (m *Metrics) RecordTransaction(success bool) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
|
|
if success {
|
|
m.TxProcessed++
|
|
} else {
|
|
m.TxFailed++
|
|
}
|
|
}
|
|
|
|
// UpdateTPS updates transactions per second
|
|
func (m *Metrics) UpdateTPS(tps float64) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
|
|
m.TPS = tps
|
|
}
|
|
|
|
// UpdateNetwork updates network metrics
|
|
func (m *Metrics) UpdateNetwork(peers int, latency time.Duration) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
|
|
m.PeersConnected = peers
|
|
m.NetworkLatency = latency
|
|
}
|
|
|
|
// UpdateResources updates resource usage metrics
|
|
func (m *Metrics) UpdateResources(cpu float64, memory, disk uint64) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
|
|
m.CPUUsage = cpu
|
|
m.MemoryUsage = memory
|
|
m.DiskUsage = disk
|
|
}
|
|
|
|
// GetSnapshot returns a snapshot of current metrics
|
|
func (m *Metrics) GetSnapshot() map[string]interface{} {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
|
|
return map[string]interface{}{
|
|
"blocks": map[string]interface{}{
|
|
"produced": m.BlocksProduced,
|
|
"lastBlockTime": m.LastBlockTime,
|
|
"averageBlockTime": m.AverageBlockTime.String(),
|
|
},
|
|
"transactions": map[string]interface{}{
|
|
"processed": m.TxProcessed,
|
|
"failed": m.TxFailed,
|
|
"tps": m.TPS,
|
|
},
|
|
"network": map[string]interface{}{
|
|
"peers": m.PeersConnected,
|
|
"latency": m.NetworkLatency.String(),
|
|
},
|
|
"resources": map[string]interface{}{
|
|
"cpuUsage": m.CPUUsage,
|
|
"memoryUsage": m.MemoryUsage,
|
|
"diskUsage": m.DiskUsage,
|
|
},
|
|
}
|
|
}
|