Files
consensus/core/consensus.go
T
zeekay fabc249f2a fix: PendingTxs MessageType must be iota+1 to match block constants
PendingTxs was 0 (iota) but block.PendingTxs is 1 (iota+1).
ForwardVMNotifications casts by value, so mismatch caused P-chain
to never produce blocks (stuck at height 0).
2026-02-22 13:07:33 -08:00

125 lines
3.1 KiB
Go

// Package core provides core consensus interfaces and contracts.
// This package has zero dependencies on vm modules to avoid import cycles.
package core
import (
"context"
"github.com/luxfi/consensus/engine/interfaces"
"github.com/luxfi/ids"
)
// BlockState represents block storage for consensus
type BlockState interface {
// GetBlock gets a block
GetBlock(ids.ID) (Block, error)
// PutBlock puts a block
PutBlock(Block) error
// GetLastAccepted gets last accepted
GetLastAccepted() (ids.ID, error)
// SetLastAccepted sets last accepted
SetLastAccepted(ids.ID) error
}
// Block represents a block
type Block interface {
ID() ids.ID
ParentID() ids.ID
Height() uint64
Timestamp() int64
Bytes() []byte
Verify(context.Context) error
Accept(context.Context) error
Reject(context.Context) error
}
// Tx represents a transaction
type Tx interface {
ID() ids.ID
Bytes() []byte
Verify(context.Context) error
Accept(context.Context) error
}
// UTXO represents an unspent transaction output
type UTXO interface {
ID() ids.ID
TxID() ids.ID
OutputIndex() uint32
Amount() uint64
}
// MessageType defines types of messages sent between VMs and consensus
type MessageType uint32
// Message types for VM communication.
// Values MUST match engine/chain/block.MessageType constants (iota + 1):
// - block.PendingTxs = 1
// - block.StateSyncDone = 2
//
// The ForwardVMNotifications bridge casts block.MessageType to core.MessageType
// by value, so these must be numerically identical.
const (
PendingTxs MessageType = iota + 1 // = 1, matches block.PendingTxs
StateSyncDone // = 2, matches block.StateSyncDone
)
// Message represents a message from VM to consensus engine
type Message struct {
Type MessageType
Data []byte
}
// Fx represents a feature extension interface
type Fx interface {
Initialize(vm interface{}) error
Bootstrapping() error
Bootstrapped() error
}
// FxLifecycle represents the lifecycle methods for feature extensions
type FxLifecycle interface {
Initialize(vm interface{}) error
Bootstrapping() error
Bootstrapped() error
Shutdown() error
}
// VM is an alias to engine/interfaces.VM for backwards compatibility
// Import from github.com/luxfi/consensus/engine/interfaces for the full VM interface
type VM = interfaces.VM
// AppError represents an application-level error
type AppError struct {
Code int
Message string
}
func (e AppError) Error() string { return e.Message }
// State represents the operational state of a VM or consensus engine
type State = interfaces.State
// VMState is an alias for State (for compatibility with older code)
type VMState = interfaces.State
// Re-export state constants from interfaces package
const (
Unknown = interfaces.Unknown
Starting = interfaces.Starting
Syncing = interfaces.Syncing
Bootstrapping = interfaces.Bootstrapping
Ready = interfaces.Ready
Degraded = interfaces.Degraded
Stopping = interfaces.Stopping
Stopped = interfaces.Stopped
// Legacy aliases for VMState constants (old naming convention)
VMStateSyncing = interfaces.Syncing
VMBootstrapping = interfaces.Bootstrapping
VMNormalOp = interfaces.Ready
)