mirror of
https://github.com/luxfi/consensus.git
synced 2026-09-24 00:26:58 +00:00
Reorganize consensus packages following Go conventions and best practices: Protocol Consolidation: - Move prism/, photon/, wave/ → protocol/ directory - Consolidate all protocol implementations under single namespace - Follows standard library pattern (encoding/json, encoding/xml) Package Naming: - Rename validators/ → validator/ (singular, lowercase per Go convention) - Aligns with stdlib patterns (net/http, database/sql) Import Path Updates: - Update all 241 Go files with new import paths - github.com/luxfi/consensus/prism → protocol/prism - github.com/luxfi/consensus/photon → protocol/photon - github.com/luxfi/consensus/wave → protocol/wave - github.com/luxfi/consensus/validators → validator Benefits: - Clear separation: core (contracts) vs protocol (algorithms) vs engine (runtime) - Improved discoverability (all protocols under protocol/) - Consistent with Go naming conventions - Zero breaking changes to core APIs - Easier to add new protocol implementations Test Results: - All 67+ tests passing across 21 packages - 100% test coverage maintained - No performance regression Structure: consensus/ ├── core/ # Pure contracts (interfaces, zero deps) ├── protocol/ # Consensus algorithms & mechanics │ ├── prism/ # Polling/quorum primitives │ ├── photon/ # Unary consensus │ ├── wave/ # N-ary consensus (FPC) │ └── ... # Other protocol phases ├── engine/ # Runtime glue (runs protocols) └── validator/ # Validator management (singular)
76 lines
2.0 KiB
Go
76 lines
2.0 KiB
Go
package nova
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/luxfi/consensus/protocol/prism"
|
|
"github.com/luxfi/consensus/protocol/ray"
|
|
"github.com/luxfi/consensus/protocol/wave"
|
|
)
|
|
|
|
// Nova implements linear blockchain consensus using the internal Ray engine
|
|
type Nova[T comparable] struct {
|
|
rayEngine *ray.Driver[T]
|
|
config Config
|
|
}
|
|
|
|
// Config holds configuration for Nova consensus mode
|
|
type Config struct {
|
|
SampleSize int // k parameter for sampling
|
|
Alpha float64 // threshold ratio
|
|
Beta uint32 // confidence threshold
|
|
RoundTO time.Duration // round timeout
|
|
GenesisHash [32]byte // genesis block hash
|
|
}
|
|
|
|
// NewNova creates a new Nova instance with Ray engine
|
|
func NewNova[T comparable](cfg Config, cut prism.Cut[T], tx wave.Transport[T], source ray.Source[T], sink ray.Sink[T]) *Nova[T] {
|
|
rayConfig := ray.Config{
|
|
PollSize: cfg.SampleSize,
|
|
Alpha: cfg.Alpha,
|
|
Beta: cfg.Beta,
|
|
RoundTO: cfg.RoundTO,
|
|
}
|
|
|
|
return &Nova[T]{
|
|
rayEngine: ray.NewDriver(rayConfig, cut, tx, source, sink),
|
|
config: cfg,
|
|
}
|
|
}
|
|
|
|
// Start begins Nova consensus operation
|
|
func (n *Nova[T]) Start(ctx context.Context) error {
|
|
return n.rayEngine.Start(ctx)
|
|
}
|
|
|
|
// Stop ends Nova consensus operation
|
|
func (n *Nova[T]) Stop(ctx context.Context) error {
|
|
return n.rayEngine.Stop(ctx)
|
|
}
|
|
|
|
// ProposeBlock proposes a new block for the current height
|
|
func (n *Nova[T]) ProposeBlock(ctx context.Context, block T) error {
|
|
return n.rayEngine.Propose(ctx, block)
|
|
}
|
|
|
|
// Tick performs one consensus round for linear chain progression
|
|
func (n *Nova[T]) Tick(ctx context.Context) error {
|
|
return n.rayEngine.Tick(ctx)
|
|
}
|
|
|
|
// GetPreference returns the current preferred block
|
|
func (n *Nova[T]) GetPreference() (T, bool) {
|
|
return n.rayEngine.GetPreference()
|
|
}
|
|
|
|
// IsFinalized checks if a block is finalized
|
|
func (n *Nova[T]) IsFinalized(block T) bool {
|
|
return n.rayEngine.IsFinalized(block)
|
|
}
|
|
|
|
// Height returns the current blockchain height
|
|
func (n *Nova[T]) Height() uint64 {
|
|
return n.rayEngine.Height()
|
|
}
|