mirror of
https://github.com/luxfi/consensus.git
synced 2026-09-24 00:26:58 +00:00
- Add context extension methods for uptime and node info - Add NoOpCalculator for uptime in test contexts - Update locked calculator with proper interface compliance - Remove experimental quasar event horizon duplicates - Add TypeScript/NAPI SDK bindings for consensus - Update Rust bindings and tests
64 lines
2.0 KiB
Go
64 lines
2.0 KiB
Go
// Package uptime provides uptime calculation functionality
|
|
package uptime
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/luxfi/ids"
|
|
)
|
|
|
|
// Calculator calculates uptime
|
|
type Calculator interface {
|
|
CalculateUptime(nodeID ids.NodeID, subnetID ids.ID) (time.Duration, time.Duration, error)
|
|
CalculateUptimePercent(nodeID ids.NodeID, subnetID ids.ID) (float64, error)
|
|
CalculateUptimePercentFrom(nodeID ids.NodeID, subnetID ids.ID, from time.Time) (float64, error)
|
|
SetCalculator(subnetID ids.ID, calc Calculator) error
|
|
}
|
|
|
|
// NoOpCalculator is a no-op implementation of Calculator
|
|
type NoOpCalculator struct{}
|
|
|
|
// CalculateUptime always returns 100% uptime
|
|
func (NoOpCalculator) CalculateUptime(ids.NodeID, ids.ID) (time.Duration, time.Duration, error) {
|
|
return 0, 0, nil
|
|
}
|
|
|
|
// CalculateUptimePercent always returns 100% uptime
|
|
func (NoOpCalculator) CalculateUptimePercent(ids.NodeID, ids.ID) (float64, error) {
|
|
return 1.0, nil
|
|
}
|
|
|
|
// CalculateUptimePercentFrom always returns 100% uptime
|
|
func (NoOpCalculator) CalculateUptimePercentFrom(ids.NodeID, ids.ID, time.Time) (float64, error) {
|
|
return 1.0, nil
|
|
}
|
|
|
|
// SetCalculator is a no-op
|
|
func (NoOpCalculator) SetCalculator(ids.ID, Calculator) error {
|
|
return nil
|
|
}
|
|
|
|
// ZeroUptimeCalculator is an implementation of Calculator that always returns 0% uptime.
|
|
// This is useful for testing scenarios where validators have "never connected".
|
|
type ZeroUptimeCalculator struct{}
|
|
|
|
// CalculateUptime always returns 0% uptime
|
|
func (ZeroUptimeCalculator) CalculateUptime(ids.NodeID, ids.ID) (time.Duration, time.Duration, error) {
|
|
return 0, 1, nil // 0 uptime out of 1 total (0%)
|
|
}
|
|
|
|
// CalculateUptimePercent always returns 0% uptime
|
|
func (ZeroUptimeCalculator) CalculateUptimePercent(ids.NodeID, ids.ID) (float64, error) {
|
|
return 0.0, nil
|
|
}
|
|
|
|
// CalculateUptimePercentFrom always returns 0% uptime
|
|
func (ZeroUptimeCalculator) CalculateUptimePercentFrom(ids.NodeID, ids.ID, time.Time) (float64, error) {
|
|
return 0.0, nil
|
|
}
|
|
|
|
// SetCalculator is a no-op
|
|
func (ZeroUptimeCalculator) SetCalculator(ids.ID, Calculator) error {
|
|
return nil
|
|
}
|