Files
2025-09-24 04:01:37 -05:00

114 lines
2.8 KiB
Go

package block
import (
"context"
"time"
"github.com/luxfi/ids"
)
// Block is a block in the chain
type Block interface {
ID() ids.ID
Parent() ids.ID // Alias for ParentID for compatibility
ParentID() ids.ID
Height() uint64
Timestamp() time.Time
Status() uint8
Verify(context.Context) error
Accept(context.Context) error
Reject(context.Context) error
Bytes() []byte
}
// ChainVM defines the interface for a blockchain VM
type ChainVM interface {
// Initialize initializes the VM
Initialize(
ctx context.Context,
chainCtx interface{},
db interface{},
genesisBytes []byte,
upgradeBytes []byte,
configBytes []byte,
msgChan interface{},
fxs []interface{},
appSender interface{},
) error
// BuildBlock builds a new block
BuildBlock(context.Context) (Block, error)
// ParseBlock parses a block from bytes
ParseBlock(context.Context, []byte) (Block, error)
// GetBlock gets a block by ID
GetBlock(context.Context, ids.ID) (Block, error)
// GetBlockIDAtHeight gets block ID at a specific height
GetBlockIDAtHeight(context.Context, uint64) (ids.ID, error)
// SetPreference sets the preferred block
SetPreference(context.Context, ids.ID) error
// LastAccepted returns the last accepted block
LastAccepted(context.Context) (ids.ID, error)
}
// StateSyncMode defines state sync modes
type StateSyncMode uint8
const (
StateSyncSkipped StateSyncMode = iota
StateSyncStatic
StateSyncDynamic
)
// StateSyncableVM defines a VM that supports state sync
type StateSyncableVM interface {
ChainVM
// StateSyncEnabled returns whether state sync is enabled
StateSyncEnabled(context.Context) (bool, error)
// GetOngoingSyncStateSummary returns the ongoing sync state summary
GetOngoingSyncStateSummary(context.Context) (StateSummary, error)
// GetLastStateSummary returns the last state summary
GetLastStateSummary(context.Context) (StateSummary, error)
// ParseStateSummary parses a state summary
ParseStateSummary(context.Context, []byte) (StateSummary, error)
// GetStateSummary gets a state summary by height
GetStateSummary(context.Context, uint64) (StateSummary, error)
}
// StateSummary represents a state summary
type StateSummary interface {
ID() ids.ID
Height() uint64
Bytes() []byte
Accept(context.Context) (StateSyncMode, error)
}
// BuildBlockWithContextVM extends ChainVM with context support
type BuildBlockWithContextVM interface {
ChainVM
BuildBlockWithContext(context.Context, *Context) (Block, error)
}
// Context provides context for building blocks
type Context struct {
PChainHeight uint64
}
// WithVerifyContext provides verify context support
type WithVerifyContext interface {
// VerifyWithContext verifies with context
VerifyWithContext(context.Context, *Context) error
// ShouldVerifyWithContext returns whether to verify with context
ShouldVerifyWithContext(context.Context) (bool, error)
}