mirror of
https://github.com/luxfi/consensus.git
synced 2026-09-24 00:26:58 +00:00
- Remove unused packages: engine/dag/{getter,bootstrap,vertex/vertexmock},
engine/pq/{bootstrap,block}, core/{tracker,verify}
- Add sync.RWMutex to protocol/wave for thread-safe map access
- Increase engine/dag test coverage from 11.7% to 98.3%
- Add comprehensive tests for DAG consensus, vertex, and engine
- All 27 packages pass tests, 16 at 100% coverage
109 lines
3.0 KiB
Go
109 lines
3.0 KiB
Go
// Package examples demonstrates integration between consensus and node packages
|
|
package examples
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
enginecore "github.com/luxfi/consensus/engine"
|
|
"github.com/luxfi/ids"
|
|
)
|
|
|
|
// Example block for demonstration
|
|
type ExampleBlock struct {
|
|
id ids.ID
|
|
parentID ids.ID
|
|
height uint64
|
|
timestamp int64
|
|
data []byte
|
|
}
|
|
|
|
func (b *ExampleBlock) ID() ids.ID { return b.id }
|
|
func (b *ExampleBlock) ParentID() ids.ID { return b.parentID }
|
|
func (b *ExampleBlock) Height() uint64 { return b.height }
|
|
func (b *ExampleBlock) Timestamp() int64 { return b.timestamp }
|
|
func (b *ExampleBlock) Bytes() []byte { return b.data }
|
|
func (b *ExampleBlock) Verify(context.Context) error { return nil }
|
|
func (b *ExampleBlock) Accept(context.Context) error {
|
|
fmt.Printf("Block %s accepted at height %d\n", b.id, b.height)
|
|
return nil
|
|
}
|
|
func (b *ExampleBlock) Reject(context.Context) error {
|
|
fmt.Printf("Block %s rejected\n", b.id)
|
|
return nil
|
|
}
|
|
|
|
func RunNodeIntegrationExample() {
|
|
// Configure consensus parameters for fast finality
|
|
params := enginecore.ConsensusParams{
|
|
K: 20,
|
|
AlphaPreference: 15,
|
|
AlphaConfidence: 15,
|
|
Beta: 20,
|
|
ConcurrentPolls: 10,
|
|
OptimalProcessing: 10,
|
|
MaxOutstandingItems: 1000,
|
|
MaxItemProcessingTime: 30 * time.Second,
|
|
}
|
|
|
|
// Create consensus engine
|
|
consensus, err := enginecore.NewCGOConsensus(params)
|
|
if err != nil {
|
|
log.Fatalf("Failed to create consensus: %v", err)
|
|
}
|
|
|
|
fmt.Println("ā
Consensus engine initialized")
|
|
fmt.Printf("Parameters: K=%d, Alpha=%d, Beta=%d\n",
|
|
params.K, params.AlphaPreference, params.Beta)
|
|
|
|
// Create a sample block
|
|
block := &ExampleBlock{
|
|
id: ids.GenerateTestID(),
|
|
parentID: ids.Empty,
|
|
height: 1,
|
|
timestamp: time.Now().Unix(),
|
|
data: []byte("Genesis block"),
|
|
}
|
|
|
|
// Add block to consensus
|
|
if err := consensus.Add(block); err != nil {
|
|
log.Fatalf("Failed to add block: %v", err)
|
|
}
|
|
|
|
fmt.Printf("š¦ Added block %s to consensus\n", block.ID())
|
|
|
|
// Simulate voting from validators
|
|
fmt.Println("š³ļø Simulating validator votes...")
|
|
for i := 0; i < params.K; i++ {
|
|
if err := consensus.RecordPoll(block.ID(), true); err != nil {
|
|
log.Printf("Failed to record vote %d: %v", i, err)
|
|
continue
|
|
}
|
|
|
|
// Check if consensus achieved
|
|
if consensus.IsAccepted(block.ID()) {
|
|
fmt.Printf("ā
Consensus achieved after %d votes!\n", i+1)
|
|
break
|
|
}
|
|
}
|
|
|
|
// Verify final state
|
|
if consensus.IsAccepted(block.ID()) {
|
|
fmt.Printf("š Block %s has been accepted by consensus\n", block.ID())
|
|
fmt.Printf("Current preference: %s\n", consensus.GetPreference())
|
|
} else {
|
|
fmt.Printf("ā Block %s was not accepted\n", block.ID())
|
|
}
|
|
|
|
// Health check
|
|
if err := consensus.HealthCheck(); err != nil {
|
|
log.Printf("Health check failed: %v", err)
|
|
} else {
|
|
fmt.Println("š Consensus engine health check passed")
|
|
}
|
|
|
|
fmt.Println("\nš Node integration example completed successfully!")
|
|
}
|