Remove unused vm import

This commit is contained in:
2025-12-19 08:49:11 -08:00
parent d5d7f5ffd2
commit 26132a93bf
12 changed files with 58 additions and 710 deletions
+7 -26
View File
@@ -3,6 +3,8 @@
// Package consensus provides a clean, single-import interface to the Lux consensus system.
// This is the main SDK surface for applications using Lux consensus.
//
// For VM-related types (State, Message, Fx), use github.com/luxfi/vm
package consensus
import (
@@ -11,7 +13,6 @@ import (
"github.com/luxfi/consensus/config"
consensuscontext "github.com/luxfi/consensus/context"
"github.com/luxfi/consensus/core/interfaces"
"github.com/luxfi/consensus/engine"
"github.com/luxfi/consensus/types"
)
@@ -36,9 +37,6 @@ type (
Status = types.Status
Decision = types.Decision
VoteType = types.VoteType
// State represents VM state for consensus operations
State = interfaces.StateEnum
)
// Constants re-exported for convenience
@@ -58,11 +56,6 @@ const (
StatusProcessing = types.StatusProcessing
StatusRejected = types.StatusRejected
StatusAccepted = types.StatusAccepted
// VM state constants
StateSyncing = interfaces.StateSyncing
Bootstrapping = interfaces.Bootstrapping
NormalOp = interfaces.NormalOp
)
// Variables re-exported for convenience
@@ -84,16 +77,11 @@ var (
// Context accessor functions re-exported from context package
var (
// GetNetworkID gets the network ID from context
GetNetworkID = consensuscontext.GetNetworkID
// GetSubnetID gets the subnet ID from context (deprecated, use GetNetID)
GetSubnetID = consensuscontext.GetSubnetID
// GetValidatorState gets the validator state from context
GetNetworkID = consensuscontext.GetNetworkID
GetSubnetID = consensuscontext.GetSubnetID
GetValidatorState = consensuscontext.GetValidatorState
// WithContext adds consensus context to a context
WithContext = consensuscontext.WithContext
// FromContext extracts consensus context from a context
FromContext = consensuscontext.FromContext
WithContext = consensuscontext.WithContext
FromContext = consensuscontext.FromContext
)
// DefaultConfig returns the default consensus configuration
@@ -113,7 +101,6 @@ func NewChainEngine() Engine {
// NewDAG creates a new DAG consensus engine (placeholder for future)
func NewDAG(cfg Config) Engine {
// TODO: Implement DAG engine
return engine.NewChain(cfg)
}
@@ -124,7 +111,6 @@ func NewDAGEngine() Engine {
// NewPQ creates a new post-quantum consensus engine (placeholder for future)
func NewPQ(cfg Config) Engine {
// TODO: Implement PQ engine
return engine.NewChain(cfg)
}
@@ -133,8 +119,6 @@ func NewPQEngine() Engine {
return NewPQ(DefaultConfig())
}
// Helper functions
// NewBlock creates a new block with default values
func NewBlock(id ID, parentID ID, height uint64, payload []byte) *Block {
return &Block{
@@ -176,15 +160,12 @@ func GetConfig(nodeCount int) config.Parameters {
case nodeCount <= 21:
return config.MainnetParams()
default:
// For larger networks, create custom params with K = nodeCount
params := config.MainnetParams()
params.K = nodeCount
// Alpha is already set to 0.69 from MainnetParams
// Adjust other parameters proportionally for 69% threshold
params.AlphaPreference = int(float64(nodeCount) * 0.69)
params.AlphaConfidence = int(float64(nodeCount) * 0.69)
params.BetaVirtuous = int(float64(nodeCount) * 0.69)
params.BetaRogue = nodeCount // Set to max for safety
params.BetaRogue = nodeCount
params.Beta = uint32(int(float64(nodeCount) * 0.69))
return params
}
-30
View File
@@ -1,30 +0,0 @@
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
// Package appsender provides application-level message sending interfaces
package appsender
import (
"context"
"github.com/luxfi/ids"
"github.com/luxfi/math/set"
)
// AppSender sends application-level messages.
type AppSender interface {
// SendAppRequest sends an application-level request to the given nodes.
SendAppRequest(ctx context.Context, nodeIDs set.Set[ids.NodeID], requestID uint32, appRequestBytes []byte) error
// SendAppResponse sends an application-level response to the given node.
SendAppResponse(ctx context.Context, nodeID ids.NodeID, requestID uint32, appResponseBytes []byte) error
// SendAppError sends an application-level error to the given node.
SendAppError(ctx context.Context, nodeID ids.NodeID, requestID uint32, errorCode int32, errorMessage string) error
// SendAppGossip sends an application-level gossip to all connected nodes.
SendAppGossip(ctx context.Context, nodeIDs set.Set[ids.NodeID], appGossipBytes []byte) error
// SendAppGossipSpecific sends an application-level gossip to specific nodes.
SendAppGossipSpecific(ctx context.Context, nodeIDs set.Set[ids.NodeID], appGossipBytes []byte) error
}
+2 -24
View File
@@ -1,6 +1,7 @@
// Copyright (C) 2019-2025, Lux Industries Inc All rights reserved.
// Package interfaces defines core consensus interfaces
// See the file LICENSE for licensing terms.
// Package interfaces defines core consensus interfaces.
package interfaces
import (
@@ -10,15 +11,10 @@ import (
// BCLookup provides blockchain lookup functionality
type BCLookup interface {
// Lookup returns the blockchain ID for an alias
Lookup(alias string) (ids.ID, error)
// GetAlias returns the alias for a blockchain ID
GetAlias(blockchainID ids.ID) (string, error)
// GetBlockchainID returns the blockchain ID for an alias
GetBlockchainID(alias string) (ids.ID, error)
// PrimaryAlias returns the primary alias for a blockchain
PrimaryAlias(blockchainID ids.ID) (string, error)
// Aliases returns all aliases for a blockchain
Aliases(blockchainID ids.ID) ([]string, error)
}
@@ -33,21 +29,3 @@ type VersionedDatabase struct {
Lock database.Database
Memory database.Database
}
// StateHolder holds state information
type StateHolder interface {
// GetState returns the current state
GetState() StateEnum
}
// StateEnum represents consensus state
type StateEnum uint8
const (
// StateSyncing indicates the node is syncing state
StateSyncing StateEnum = iota
// Bootstrapping indicates the node is bootstrapping
Bootstrapping
// NormalOp indicates normal operation
NormalOp
)
-191
View File
@@ -1,191 +0,0 @@
// Copyright (C) 2019-2025, Lux Industries Inc All rights reserved.
// See the file LICENSE for licensing terms.
package core
import (
"context"
"fmt"
"net/http"
consensuscontext "github.com/luxfi/consensus/context"
"github.com/luxfi/database/manager"
"github.com/luxfi/ids"
"github.com/luxfi/warp"
)
// VMState represents the state of a VM
type VMState uint8
const (
// VMInitializing is the state of a VM that is initializing
VMInitializing VMState = iota
// VMStateSyncing is the state of a VM that is syncing state
VMStateSyncing
// VMBootstrapping is the state of a VM that is bootstrapping
VMBootstrapping
// VMNormalOp is the state of a VM that is in normal operation
VMNormalOp
)
// VM defines the interface that all VMs must implement
type VM interface {
// Initialize initializes the VM
Initialize(
ctx context.Context,
chainCtx *consensuscontext.Context,
dbManager manager.Manager,
genesisBytes []byte,
upgradeBytes []byte,
configBytes []byte,
toEngine chan<- Message,
fxs []*Fx,
warpSender interface{}, // WarpSender interface from warp package
) error
// SetState sets the state of the VM
SetState(ctx context.Context, state VMState) error
// Shutdown shuts down the VM
Shutdown(ctx context.Context) error
// Version returns the version of the VM
Version(ctx context.Context) (string, error)
// HealthCheck returns nil if the VM is healthy
HealthCheck(ctx context.Context) (interface{}, error)
// CreateHandlers returns the HTTP handlers for the VM
CreateHandlers(ctx context.Context) (map[string]http.Handler, error)
// CreateStaticHandlers returns the static HTTP handlers for the VM
CreateStaticHandlers(ctx context.Context) (map[string]http.Handler, error)
// NewHTTPHandler returns a new HTTP handler for the VM
NewHTTPHandler(ctx context.Context) (http.Handler, error)
}
// Message defines a message that can be sent to the consensus engine
type Message struct {
Type MessageType
NodeID ids.NodeID
Content []byte
}
// MessageType defines the type of a message
type MessageType uint32
// String returns the string representation of the message type
func (m MessageType) String() string {
switch m {
case PendingTxs:
return "PendingTxs"
case PutBlock:
return "PutBlock"
case GetBlock:
return "GetBlock"
case GetAccepted:
return "GetAccepted"
case Accepted:
return "Accepted"
case GetAncestors:
return "GetAncestors"
case MultiPut:
return "MultiPut"
case GetFailed:
return "GetFailed"
case QueryFailed:
return "QueryFailed"
case Chits:
return "Chits"
case ChitsV2:
return "ChitsV2"
case GetAcceptedFrontier:
return "GetAcceptedFrontier"
case AcceptedFrontier:
return "AcceptedFrontier"
case GetAcceptedFrontierFailed:
return "GetAcceptedFrontierFailed"
case WarpRequest:
return "WarpRequest"
case WarpResponse:
return "WarpResponse"
case WarpGossip:
return "WarpGossip"
case StateSyncDone:
return "StateSyncDone"
default:
return "Unknown"
}
}
const (
// PendingTxs indicates pending transactions
PendingTxs MessageType = iota
// PutBlock indicates a block to be added
PutBlock
// GetBlock indicates a request for a block
GetBlock
// GetAccepted indicates a request for accepted blocks
GetAccepted
// Accepted indicates an accepted block
Accepted
// GetAncestors indicates a request for ancestors
GetAncestors
// MultiPut indicates multiple blocks
MultiPut
// GetFailed indicates a failed get request
GetFailed
// QueryFailed indicates a failed query
QueryFailed
// Chits indicates chits
Chits
// ChitsV2 indicates chits v2
ChitsV2
// GetAcceptedFrontier indicates a request for the accepted frontier
GetAcceptedFrontier
// AcceptedFrontier indicates the accepted frontier
AcceptedFrontier
// GetAcceptedFrontierFailed indicates a failed frontier request
GetAcceptedFrontierFailed
// WarpRequest indicates a warp request
WarpRequest
// WarpResponse indicates a warp response
WarpResponse
// WarpGossip indicates warp gossip
WarpGossip
// StateSyncDone indicates state sync completed
StateSyncDone
)
// Fx defines a feature extension
type Fx struct {
ID ids.ID
Fx interface{}
}
// AppError represents an application-level error for peer messaging
type AppError struct {
Code int32
Message string
}
// Error implements the error interface
func (e *AppError) Error() string {
if e == nil {
return ""
}
return fmt.Sprintf("app error %d: %s", e.Code, e.Message)
}
// SendConfig is an alias for warp.SendConfig for backwards compatibility
type SendConfig = warp.SendConfig
// Sender is an alias for warp.Sender for backwards compatibility
type Sender = warp.Sender
// Handler is an alias for warp.Handler for backwards compatibility
type Handler = warp.Handler
// Error is an alias for warp.Error for backwards compatibility
type Error = warp.Error
-346
View File
@@ -1,346 +0,0 @@
package core
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestMessageType_String(t *testing.T) {
tests := []struct {
name string
messageType MessageType
expected string
}{
{
name: "PendingTxs",
messageType: PendingTxs,
expected: "PendingTxs",
},
{
name: "PutBlock",
messageType: PutBlock,
expected: "PutBlock",
},
{
name: "GetBlock",
messageType: GetBlock,
expected: "GetBlock",
},
{
name: "GetAccepted",
messageType: GetAccepted,
expected: "GetAccepted",
},
{
name: "Accepted",
messageType: Accepted,
expected: "Accepted",
},
{
name: "GetAncestors",
messageType: GetAncestors,
expected: "GetAncestors",
},
{
name: "MultiPut",
messageType: MultiPut,
expected: "MultiPut",
},
{
name: "GetFailed",
messageType: GetFailed,
expected: "GetFailed",
},
{
name: "QueryFailed",
messageType: QueryFailed,
expected: "QueryFailed",
},
{
name: "Chits",
messageType: Chits,
expected: "Chits",
},
{
name: "ChitsV2",
messageType: ChitsV2,
expected: "ChitsV2",
},
{
name: "GetAcceptedFrontier",
messageType: GetAcceptedFrontier,
expected: "GetAcceptedFrontier",
},
{
name: "AcceptedFrontier",
messageType: AcceptedFrontier,
expected: "AcceptedFrontier",
},
{
name: "GetAcceptedFrontierFailed",
messageType: GetAcceptedFrontierFailed,
expected: "GetAcceptedFrontierFailed",
},
{
name: "WarpRequest",
messageType: WarpRequest,
expected: "WarpRequest",
},
{
name: "WarpResponse",
messageType: WarpResponse,
expected: "WarpResponse",
},
{
name: "WarpGossip",
messageType: WarpGossip,
expected: "WarpGossip",
},
{
name: "StateSyncDone",
messageType: StateSyncDone,
expected: "StateSyncDone",
},
{
name: "Unknown type (large positive)",
messageType: MessageType(9999),
expected: "Unknown",
},
{
name: "Unknown type (max uint32)",
messageType: MessageType(^uint32(0)),
expected: "Unknown",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.messageType.String()
require.Equal(t, tt.expected, result)
})
}
}
func TestMessageType_Constants(t *testing.T) {
// Verify constant ordering (iota values)
require.Equal(t, MessageType(0), PendingTxs)
require.Equal(t, MessageType(1), PutBlock)
require.Equal(t, MessageType(2), GetBlock)
require.Equal(t, MessageType(3), GetAccepted)
require.Equal(t, MessageType(4), Accepted)
require.Equal(t, MessageType(5), GetAncestors)
require.Equal(t, MessageType(6), MultiPut)
require.Equal(t, MessageType(7), GetFailed)
require.Equal(t, MessageType(8), QueryFailed)
require.Equal(t, MessageType(9), Chits)
require.Equal(t, MessageType(10), ChitsV2)
require.Equal(t, MessageType(11), GetAcceptedFrontier)
require.Equal(t, MessageType(12), AcceptedFrontier)
require.Equal(t, MessageType(13), GetAcceptedFrontierFailed)
require.Equal(t, MessageType(14), WarpRequest)
require.Equal(t, MessageType(15), WarpResponse)
require.Equal(t, MessageType(16), WarpGossip)
require.Equal(t, MessageType(17), StateSyncDone)
}
func TestMessageType_AllKnownTypes(t *testing.T) {
knownTypes := []MessageType{
PendingTxs,
PutBlock,
GetBlock,
GetAccepted,
Accepted,
GetAncestors,
MultiPut,
GetFailed,
QueryFailed,
Chits,
ChitsV2,
GetAcceptedFrontier,
AcceptedFrontier,
GetAcceptedFrontierFailed,
WarpRequest,
WarpResponse,
WarpGossip,
StateSyncDone,
}
for _, mt := range knownTypes {
result := mt.String()
require.NotEqual(t, "Unknown", result, "MessageType %d should not be Unknown", mt)
}
}
func TestAppError_Error(t *testing.T) {
tests := []struct {
name string
err *AppError
expected string
}{
{
name: "standard error",
err: &AppError{
Code: 100,
Message: "something went wrong",
},
expected: "app error 100: something went wrong",
},
{
name: "zero code",
err: &AppError{
Code: 0,
Message: "zero code error",
},
expected: "app error 0: zero code error",
},
{
name: "negative code",
err: &AppError{
Code: -1,
Message: "negative code error",
},
expected: "app error -1: negative code error",
},
{
name: "empty message",
err: &AppError{
Code: 500,
Message: "",
},
expected: "app error 500: ",
},
{
name: "large code",
err: &AppError{
Code: 2147483647,
Message: "max int32 code",
},
expected: "app error 2147483647: max int32 code",
},
{
name: "min int32 code",
err: &AppError{
Code: -2147483648,
Message: "min int32 code",
},
expected: "app error -2147483648: min int32 code",
},
{
name: "nil error",
err: nil,
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.err.Error()
require.Equal(t, tt.expected, result)
})
}
}
func TestAppError_Interface(t *testing.T) {
// Verify AppError implements error interface
var _ error = (*AppError)(nil)
err := &AppError{
Code: 404,
Message: "not found",
}
var e error = err
require.Equal(t, "app error 404: not found", e.Error())
}
func TestAppError_Nil(t *testing.T) {
var err *AppError
require.Equal(t, "", err.Error())
}
func TestMessage_Struct(t *testing.T) {
msg := Message{
Type: PendingTxs,
Content: []byte("test content"),
}
require.Equal(t, PendingTxs, msg.Type)
require.Equal(t, []byte("test content"), msg.Content)
require.Equal(t, "PendingTxs", msg.Type.String())
}
func TestVMState_Values(t *testing.T) {
// Verify VMState constants
require.Equal(t, VMState(0), VMInitializing)
require.Equal(t, VMState(1), VMStateSyncing)
require.Equal(t, VMState(2), VMBootstrapping)
require.Equal(t, VMState(3), VMNormalOp)
}
func TestFx_Struct(t *testing.T) {
fx := Fx{
Fx: "test fx",
}
require.Equal(t, "test fx", fx.Fx)
}
// Benchmarks
func BenchmarkMessageType_String(b *testing.B) {
messageTypes := []MessageType{
PendingTxs, PutBlock, GetBlock, GetAccepted, Accepted,
GetAncestors, MultiPut, GetFailed, QueryFailed, Chits,
ChitsV2, GetAcceptedFrontier, AcceptedFrontier, GetAcceptedFrontierFailed,
WarpRequest, WarpResponse, WarpGossip, StateSyncDone,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, mt := range messageTypes {
_ = mt.String()
}
}
}
func BenchmarkAppError_Error(b *testing.B) {
err := &AppError{
Code: 500,
Message: "internal server error",
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = err.Error()
}
}
func BenchmarkAppError_Error_Nil(b *testing.B) {
var err *AppError
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = err.Error()
}
}
// Examples
func ExampleMessageType_String() {
fmt.Println(PendingTxs.String())
fmt.Println(GetBlock.String())
fmt.Println(WarpRequest.String())
// Output:
// PendingTxs
// GetBlock
// WarpRequest
}
func ExampleAppError_Error() {
err := &AppError{
Code: 404,
Message: "resource not found",
}
fmt.Println(err.Error())
// Output: app error 404: resource not found
}
+8 -8
View File
@@ -6,7 +6,7 @@ import (
"time"
consensuscontext "github.com/luxfi/consensus/context"
"github.com/luxfi/consensus/core"
"github.com/luxfi/consensus/engine"
"github.com/luxfi/database/manager"
"github.com/luxfi/ids"
)
@@ -45,17 +45,17 @@ type ChainContext struct {
// DBManager manages databases
type DBManager = manager.Manager
// Re-export core types for backwards compatibility
// Re-export engine types
type (
MessageType = core.MessageType
Message = core.Message
Fx = core.Fx
MessageType = engine.MessageType
Message = engine.Message
Fx = engine.Fx
)
// Message type constants re-exported from core
// Message type constants
const (
PendingTxs = core.PendingTxs
StateSyncDone = core.StateSyncDone
PendingTxs = engine.PendingTxs
StateSyncDone = engine.StateSyncDone
)
// AppSender sends application-level messages
+6 -7
View File
@@ -5,7 +5,7 @@ import (
"sync"
"github.com/luxfi/consensus/config"
"github.com/luxfi/consensus/core"
"github.com/luxfi/consensus/engine"
"github.com/luxfi/ids"
)
@@ -24,16 +24,15 @@ type Engine interface {
IsBootstrapped() bool
}
// Re-export core message types
// Re-export message types for convenience
type (
MessageType = core.MessageType
Message = core.Message
MessageType = engine.MessageType
Message = engine.Message
)
// Message type constants
const (
PendingTxs = core.PendingTxs
StateSyncDone = core.StateSyncDone
PendingTxs = engine.PendingTxs
StateSyncDone = engine.StateSyncDone
)
// BlockBuilder is the interface for VMs that can build blocks
-37
View File
@@ -1,37 +0,0 @@
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
// Package core re-exports core types from github.com/luxfi/consensus/core
// for backwards compatibility. New code should import core directly.
package engine
import "github.com/luxfi/consensus/core"
// Re-export types from core package
type (
Fx = core.Fx
Message = core.Message
MessageType = core.MessageType
)
// Re-export message type constants
const (
PendingTxs = core.PendingTxs
PutBlock = core.PutBlock
GetBlock = core.GetBlock
GetAccepted = core.GetAccepted
Accepted = core.Accepted
GetAncestors = core.GetAncestors
MultiPut = core.MultiPut
GetFailed = core.GetFailed
QueryFailed = core.QueryFailed
Chits = core.Chits
ChitsV2 = core.ChitsV2
GetAcceptedFrontier = core.GetAcceptedFrontier
AcceptedFrontier = core.AcceptedFrontier
GetAcceptedFrontierFailed = core.GetAcceptedFrontierFailed
WarpRequest = core.WarpRequest
WarpResponse = core.WarpResponse
WarpGossip = core.WarpGossip
StateSyncDone = core.StateSyncDone
)
+5 -27
View File
@@ -1,10 +1,10 @@
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
// Package interfaces provides consensus engine interfaces.
package interfaces
import (
"context"
"github.com/luxfi/ids"
)
import "context"
// Engine defines the consensus engine interface
type Engine interface {
@@ -13,25 +13,3 @@ type Engine interface {
HealthCheck(context.Context) (interface{}, error)
IsBootstrapped() bool
}
// VM defines a virtual machine
type VM interface {
Initialize(context.Context, *VMConfig) error
Shutdown(context.Context) error
Version(context.Context) (string, error)
}
// VMConfig defines VM configuration
type VMConfig struct {
ChainID ids.ID
NetworkID uint32
NodeID ids.NodeID
PublicKey []byte
}
// Fx is a feature extension
type Fx interface {
Initialize(vm interface{}) error
Bootstrapping() error
Bootstrapped() error
}
+25
View File
@@ -0,0 +1,25 @@
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
// Package engine provides consensus engine implementations.
package engine
import "github.com/luxfi/vm"
// Type aliases imported from vm package for consensus internal use
type (
Message = vm.Message
MessageType = vm.MessageType
Fx = vm.Fx
FxLifecycle = vm.FxLifecycle
State = vm.State
)
// Re-export constants from vm package
const (
PendingTxs = vm.PendingTxs
StateSyncDone = vm.StateSyncDone
Syncing = vm.Syncing
Bootstrapping = vm.Bootstrapping
NormalOp = vm.NormalOp
)
+2 -2
View File
@@ -6,14 +6,14 @@ package vertex
import (
"context"
"github.com/luxfi/consensus/core"
"github.com/luxfi/consensus/engine/dag"
"github.com/luxfi/consensus/engine/interfaces"
"github.com/luxfi/ids"
)
// DAGVM defines the interface for a DAG-based VM
type DAGVM interface {
core.VM
interfaces.VM
// PendingTxs returns transactions that are pending
PendingTxs(context.Context) []dag.Transaction
+3 -12
View File
@@ -8,21 +8,12 @@ import (
"sync"
"time"
"github.com/luxfi/consensus/core"
"github.com/luxfi/consensus/engine"
"github.com/luxfi/log"
)
// Re-export core types for convenience
type (
MessageType = core.MessageType
VMMessage = core.Message
)
// Message type constants
const (
PendingTxs = core.PendingTxs
StateSyncDone = core.StateSyncDone
)
// VMMessage is an alias for engine.Message
type VMMessage = engine.Message
// Notifier is the interface for receiving VM notifications
type Notifier interface {