testutil

package
v1.21.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2025 License: BSD-3-Clause Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBlockNotFound          = errors.New("block not found")
	ErrHashVerificationFailed = errors.New("hash verification failed")
	ErrInvalidBlockSequence   = errors.New("invalid block sequence")
)
View Source
var (
	ErrInvalidNetworkID = errors.New("invalid network ID: cannot be 0")
	ErrNoValidators     = errors.New("at least one validator is required")
)
View Source
var (
	ErrMigrationCancelled = errors.New("migration cancelled")
	ErrInvalidCheckpoint  = errors.New("invalid checkpoint")
)

Functions

func BodyKey

func BodyKey(number uint64, hash common.Hash) []byte

BodyKey constructs a block body key from block number and hash

func CalculateGenesisHash

func CalculateGenesisHash(cfg GenesisConfig) common.Hash

CalculateGenesisHash computes the genesis block hash

func CalculateStateRoot

func CalculateStateRoot(accounts map[common.Address]*big.Int) common.Hash

CalculateStateRoot computes a deterministic state root from accounts

func CanonicalKey

func CanonicalKey(number uint64) []byte

CanonicalKey constructs a canonical hash key from block number

func ComputeBlockHash

func ComputeBlockHash(block *TestBlock) common.Hash

ComputeBlockHash computes a deterministic hash for a test block

func CreateMultiChainGenesis

func CreateMultiChainGenesis(cfg MultiChainGenesisConfig) (*genesis.UnparsedConfig, error)

CreateMultiChainGenesis creates a full network genesis with P, X, and C chains

func CreatePersistentTestDatabase

func CreatePersistentTestDatabase(path string) (database.Database, error)

CreatePersistentTestDatabase creates a test database that persists to disk This is useful for integration tests that need to verify data across restarts

func CreateTestDatabase

func CreateTestDatabase(path string) database.Database

CreateTestDatabase creates a new test database For unit tests, we use an in-memory database for speed

func HeaderKey

func HeaderKey(number uint64, hash common.Hash) []byte

HeaderKey constructs a header key from block number and hash

func IsHeaderKey

func IsHeaderKey(key []byte) bool

IsHeaderKey checks if a key is a header key

func OpenTestDatabase

func OpenTestDatabase(path string) database.Database

OpenTestDatabase opens an existing test database For unit tests, we return a new memory database In integration tests, this would open the actual database

func ParseHeaderKey

func ParseHeaderKey(key []byte) (bool, uint64, common.Hash)

ParseHeaderKey parses a header key and returns block number and hash

func WriteTestBlocks

func WriteTestBlocks(db database.Database, count int)

WriteTestBlocks writes test blocks to a database

Types

type AllocationConfig

type AllocationConfig struct {
	Address common.Address
	Balance *big.Int
}

AllocationConfig defines an allocation in genesis

type BlockImporter

type BlockImporter struct {
	// contains filtered or unexported fields
}

BlockImporter handles importing blocks into a database

func NewBlockImporter

func NewBlockImporter(path string) *BlockImporter

NewBlockImporter creates a new block importer

func (*BlockImporter) Close

func (i *BlockImporter) Close() error

Close closes the block importer

func (*BlockImporter) GetBlock

func (i *BlockImporter) GetBlock(number uint64) (*TestBlock, error)

GetBlock retrieves a block by number

func (*BlockImporter) GetBlockHash

func (i *BlockImporter) GetBlockHash(number uint64) (common.Hash, error)

GetBlockHash retrieves the hash for a block number

func (*BlockImporter) GetCanonicalHash

func (i *BlockImporter) GetCanonicalHash(number uint64) (common.Hash, error)

GetCanonicalHash retrieves the canonical hash for a block number

func (*BlockImporter) ImportBlock

func (i *BlockImporter) ImportBlock(block *TestBlock) error

ImportBlock imports a block into the database

func (*BlockImporter) ImportBlockWithVerification

func (i *BlockImporter) ImportBlockWithVerification(block *TestBlock) error

ImportBlockWithVerification imports a block and verifies its hash

type GenesisBlock

type GenesisBlock struct {
	Number       uint64
	Hash         common.Hash
	ParentHash   common.Hash
	StateRoot    common.Hash
	Timestamp    uint64
	GasLimit     uint64
	BaseFee      *big.Int
	Header       []byte
	Transactions []byte
}

GenesisBlock represents a genesis block with essential fields

func CreateGenesisBlock

func CreateGenesisBlock(cfg GenesisConfig) (*GenesisBlock, error)

CreateGenesisBlock creates a genesis block from configuration

type GenesisConfig

type GenesisConfig struct {
	NetworkID         uint32
	ChainID           *big.Int
	Timestamp         uint64
	GasLimit          uint64
	BaseFee           *big.Int
	PreFundedAccounts map[common.Address]*big.Int
	Validators        []ValidatorConfig
}

GenesisConfig defines configuration for creating a genesis block

func DefaultGenesisConfig

func DefaultGenesisConfig() GenesisConfig

DefaultGenesisConfig returns a default genesis configuration for testing

type MigrationStats

type MigrationStats struct {
	KeysMigrated       uint64
	BlocksMigrated     uint64
	BytesMigrated      uint64
	LastProcessedBlock uint64
	Errors             []error
}

MigrationStats contains statistics about a migration

type MultiChainGenesisConfig

type MultiChainGenesisConfig struct {
	NetworkID   uint32
	Timestamp   uint64
	Validators  []ValidatorConfig
	Allocations []AllocationConfig
}

MultiChainGenesisConfig defines configuration for multi-chain genesis

type ProgressCallback

type ProgressCallback func(current, total uint64)

ProgressCallback is called during migration to report progress

type StateMigrator

type StateMigrator struct {
	// contains filtered or unexported fields
}

StateMigrator handles migrating state between databases

func NewStateMigrator

func NewStateMigrator(srcPath, dstPath string) *StateMigrator

NewStateMigrator creates a new state migrator

func NewStateMigratorWithDatabases

func NewStateMigratorWithDatabases(srcDB, dstDB database.Database) *StateMigrator

NewStateMigratorWithDatabases creates a state migrator with injected databases for testing

func (*StateMigrator) Close

func (m *StateMigrator) Close() error

Close closes the migrator and its databases

func (*StateMigrator) Migrate

func (m *StateMigrator) Migrate(ctx context.Context) (*MigrationStats, error)

Migrate performs the state migration

func (*StateMigrator) SetNamespace

func (m *StateMigrator) SetNamespace(namespace []byte)

SetNamespace sets the namespace to strip from source keys

func (*StateMigrator) SetProgressCallback

func (m *StateMigrator) SetProgressCallback(cb ProgressCallback)

SetProgressCallback sets the progress callback

func (*StateMigrator) SetStartFrom

func (m *StateMigrator) SetStartFrom(blockNum uint64)

SetStartFrom sets the block number to start migration from

func (*StateMigrator) SetStopAfter

func (m *StateMigrator) SetStopAfter(count uint64)

SetStopAfter sets the number of blocks to migrate before stopping

type TestBlock

type TestBlock struct {
	Number       uint64
	Hash         common.Hash
	ParentHash   common.Hash
	StateRoot    common.Hash
	Timestamp    uint64
	GasLimit     uint64
	GasUsed      uint64
	BaseFee      *big.Int
	Header       *types.Header
	Transactions []*types.Transaction
	Body         []byte
}

TestBlock represents a simplified block for testing

func CreateTestBlock

func CreateTestBlock(number uint64, parentHash common.Hash) *TestBlock

CreateTestBlock creates a test block with the given parameters

type TestChainConfig

type TestChainConfig struct {
	ChainID *big.Int `json:"chainId"`
}

TestChainConfig represents a minimal chain config for testing

type TestGenesis

type TestGenesis struct {
	Config     *TestChainConfig                      `json:"config"`
	Timestamp  uint64                                `json:"timestamp,omitempty"`
	GasLimit   uint64                                `json:"gasLimit"`
	Difficulty *big.Int                              `json:"difficulty"`
	Alloc      map[common.Address]TestGenesisAccount `json:"alloc"`
}

TestGenesis represents a minimal genesis for testing

func CreateTestGenesis

func CreateTestGenesis(networkID uint32, accounts map[common.Address]*big.Int) *TestGenesis

CreateTestGenesis creates a test genesis with the given configuration

type TestGenesisAccount

type TestGenesisAccount struct {
	Balance *big.Int `json:"balance"`
}

TestGenesisAccount represents a genesis account for testing

type ValidatorConfig

type ValidatorConfig struct {
	NodeID ids.NodeID
	Weight uint64
}

ValidatorConfig defines a validator for genesis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL