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)
84 lines
2.4 KiB
Go
84 lines
2.4 KiB
Go
package nebula
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/luxfi/consensus/protocol/field"
|
|
"github.com/luxfi/consensus/protocol/prism"
|
|
"github.com/luxfi/consensus/protocol/wave"
|
|
)
|
|
|
|
// VID represents a vertex identifier in the DAG
|
|
type VID interface{ comparable }
|
|
|
|
// Nebula implements DAG consensus using the internal Field engine
|
|
type Nebula[V VID] struct {
|
|
fieldEngine *field.Driver[V]
|
|
config Config
|
|
}
|
|
|
|
// Config holds configuration for Nebula consensus mode
|
|
type Config struct {
|
|
PollSize int // sample size for voting
|
|
Alpha float64 // threshold ratio
|
|
Beta uint32 // confidence threshold
|
|
RoundTO time.Duration // round timeout
|
|
GenesisSet []byte // genesis vertex set
|
|
}
|
|
|
|
// NewNebula creates a new Nebula instance with Field engine
|
|
func NewNebula[V VID](cfg Config, cut prism.Cut[V], tx wave.Transport[V], store field.Store[V], prop field.Proposer[V], com field.Committer[V]) *Nebula[V] {
|
|
fieldConfig := field.Config{
|
|
PollSize: cfg.PollSize,
|
|
Alpha: cfg.Alpha,
|
|
Beta: cfg.Beta,
|
|
RoundTO: cfg.RoundTO,
|
|
}
|
|
|
|
return &Nebula[V]{
|
|
fieldEngine: field.NewDriver(fieldConfig, cut, tx, store, prop, com),
|
|
config: cfg,
|
|
}
|
|
}
|
|
|
|
// Start begins Nebula DAG consensus operation
|
|
func (n *Nebula[V]) Start(ctx context.Context) error {
|
|
return n.fieldEngine.Start(ctx)
|
|
}
|
|
|
|
// Stop ends Nebula DAG consensus operation
|
|
func (n *Nebula[V]) Stop(ctx context.Context) error {
|
|
return n.fieldEngine.Stop(ctx)
|
|
}
|
|
|
|
// ProposeVertex proposes a new vertex to the DAG
|
|
func (n *Nebula[V]) ProposeVertex(ctx context.Context, parents []V) (V, error) {
|
|
return n.fieldEngine.Propose(ctx, parents)
|
|
}
|
|
|
|
// Tick performs one consensus round for DAG progression
|
|
func (n *Nebula[V]) Tick(ctx context.Context) error {
|
|
return n.fieldEngine.Tick(ctx)
|
|
}
|
|
|
|
// OnObserve should be called when observing new vertices from the network
|
|
func (n *Nebula[V]) OnObserve(ctx context.Context, vertex V) {
|
|
n.fieldEngine.OnObserve(ctx, vertex)
|
|
}
|
|
|
|
// GetFrontier returns the current DAG frontier (tips)
|
|
func (n *Nebula[V]) GetFrontier() []V {
|
|
return n.fieldEngine.GetFrontier()
|
|
}
|
|
|
|
// IsFinalized checks if a vertex is finalized in the DAG
|
|
func (n *Nebula[V]) IsFinalized(vertex V) bool {
|
|
return n.fieldEngine.IsFinalized(vertex)
|
|
}
|
|
|
|
// GetCommittedVertices returns vertices that have been committed in order
|
|
func (n *Nebula[V]) GetCommittedVertices() []V {
|
|
return n.fieldEngine.GetCommittedVertices()
|
|
}
|