blockchain

package
v0.0.0-...-dd56834 Latest Latest
Warning

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

Go to latest
Published: May 23, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BlockGasTargetDivisor uint64 = 1024 // The bound divisor of the gas limit, used in update calculations

)

Variables

View Source
var (
	ErrNoBlock              = errors.New("no block data passed in")
	ErrParentNotFound       = errors.New("parent block not found")
	ErrInvalidParentHash    = errors.New("parent block hash is invalid")
	ErrParentHashMismatch   = errors.New("invalid parent block hash")
	ErrInvalidBlockSequence = errors.New("invalid block sequence")
	ErrInvalidSha3Uncles    = errors.New("invalid block sha3 uncles root")
	ErrInvalidTxRoot        = errors.New("invalid block transactions root")
	ErrInvalidReceiptsSize  = errors.New("invalid number of receipts")
	ErrInvalidStateRoot     = errors.New("invalid block state root")
	ErrInvalidGasUsed       = errors.New("invalid block gas used")
	ErrInvalidReceiptsRoot  = errors.New("invalid block receipts root")
)

Functions

func AppendNewTestHeaders

func AppendNewTestHeaders(headers []*types.Header, n int) []*types.Header

AppendNewTestHeaders creates n new headers from an already existing chain

func AppendNewTestheadersWithSeed

func AppendNewTestheadersWithSeed(headers []*types.Header, n int, seed uint64) []*types.Header

AppendNewTestheadersWithSeed creates n new headers from an already existing chain

func HeadersToBlocks

func HeadersToBlocks(headers []*types.Header) []*types.Block

func NewTestHeaders

func NewTestHeaders(n int) []*types.Header

NewTestHeaders creates a chain of valid headers

func NewTestHeadersWithSeed

func NewTestHeadersWithSeed(genesis *types.Header, n int, seed uint64) []*types.Header

NewTestHeadersWithSeed creates a new chain with a seed factor

Types

type BlockResult

type BlockResult struct {
	Root     types.Hash
	Receipts []*types.Receipt
	TotalGas uint64
}

type Blockchain

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

Blockchain is a blockchain reference

func NewBlockchain

func NewBlockchain(
	logger hclog.Logger,
	db storage.Storage,
	config *chain.Chain,
	consensus Verifier,
	executor Executor,
	txSigner TxSigner,
) (*Blockchain, error)

NewBlockchain creates a new blockchain object

func NewMockBlockchain

func NewMockBlockchain(
	callbackMap map[TestCallbackType]interface{},
) (*Blockchain, error)

NewMockBlockchain constructs a new mock blockchain

func NewTestBlockchain

func NewTestBlockchain(t *testing.T, headers []*types.Header) *Blockchain

NewTestBlockchain creates a new dummy blockchain for testing

func TestBlockchain

func TestBlockchain(t *testing.T, genesis *chain.Genesis) *Blockchain

func (*Blockchain) CalculateGasLimit

func (b *Blockchain) CalculateGasLimit(number uint64) (uint64, error)

CalculateGasLimit returns the gas limit of the next block after parent

func (*Blockchain) Close

func (b *Blockchain) Close() error

Close closes the DB connection

func (*Blockchain) ComputeGenesis

func (b *Blockchain) ComputeGenesis() error

ComputeGenesis computes the genesis hash, and updates the blockchain reference

func (*Blockchain) Config

func (b *Blockchain) Config() *chain.Params

Config returns the blockchain configuration

func (*Blockchain) CurrentTD

func (b *Blockchain) CurrentTD() *big.Int

CurrentTD returns the current total difficulty (atomic)

func (*Blockchain) Empty

func (b *Blockchain) Empty() bool

Empty checks if the blockchain is empty

func (*Blockchain) Genesis

func (b *Blockchain) Genesis() types.Hash

Genesis returns the genesis block

func (*Blockchain) GetAvgGasPrice

func (b *Blockchain) GetAvgGasPrice() *big.Int

GetAvgGasPrice returns the average gas price for the chain

func (*Blockchain) GetBlock

func (b *Blockchain) GetBlock(hash types.Hash, number uint64, full bool) (*types.Block, bool)

GetBlock returns the block using the hash

func (*Blockchain) GetBlockByHash

func (b *Blockchain) GetBlockByHash(hash types.Hash, full bool) (*types.Block, bool)

GetBlockByHash returns the block using the block hash

func (*Blockchain) GetBlockByNumber

func (b *Blockchain) GetBlockByNumber(blockNumber uint64, full bool) (*types.Block, bool)

GetBlockByNumber returns the block using the block number

func (*Blockchain) GetBodyByHash

func (b *Blockchain) GetBodyByHash(hash types.Hash) (*types.Body, bool)

GetBodyByHash returns the body by their hash

func (*Blockchain) GetCachedReceipts

func (b *Blockchain) GetCachedReceipts(headerHash types.Hash) ([]*types.Receipt, error)

GetCachedReceipts retrieves cached receipts for given headerHash

func (*Blockchain) GetChainTD

func (b *Blockchain) GetChainTD() (*big.Int, bool)

GetChainTD returns the latest difficulty

func (*Blockchain) GetConsensus

func (b *Blockchain) GetConsensus() Verifier

func (*Blockchain) GetForks

func (b *Blockchain) GetForks() ([]types.Hash, error)

GetForks returns the forks

func (*Blockchain) GetHashByNumber

func (b *Blockchain) GetHashByNumber(blockNumber uint64) types.Hash

GetHashByNumber returns the block hash using the block number

func (*Blockchain) GetHashHelper

func (b *Blockchain) GetHashHelper(header *types.Header) func(i uint64) (res types.Hash)

GetHashHelper is used by the EVM, so that the SC can get the hash of the header number

func (*Blockchain) GetHeader

func (b *Blockchain) GetHeader(hash types.Hash, number uint64) (*types.Header, bool)

GetHeader returns the block header using the hash

func (*Blockchain) GetHeaderByHash

func (b *Blockchain) GetHeaderByHash(hash types.Hash) (*types.Header, bool)

GetHeaderByHash returns the header by his hash

func (*Blockchain) GetHeaderByNumber

func (b *Blockchain) GetHeaderByNumber(n uint64) (*types.Header, bool)

GetHeaderByNumber returns the header using the block number

func (*Blockchain) GetParent

func (b *Blockchain) GetParent(header *types.Header) (*types.Header, bool)

GetParent returns the parent header

func (*Blockchain) GetReceiptsByHash

func (b *Blockchain) GetReceiptsByHash(hash types.Hash) ([]*types.Receipt, error)

GetReceiptsByHash returns the receipts by their hash

func (*Blockchain) GetTD

func (b *Blockchain) GetTD(hash types.Hash) (*big.Int, bool)

GetTD returns the difficulty for the header hash

func (*Blockchain) Header

func (b *Blockchain) Header() *types.Header

Header returns the current header (atomic)

func (*Blockchain) ReadTxLookup

func (b *Blockchain) ReadTxLookup(hash types.Hash) (types.Hash, bool)

ReadTxLookup returns the block hash using the transaction hash

func (*Blockchain) SetConsensus

func (b *Blockchain) SetConsensus(c Verifier)

SetConsensus sets the consensus

func (*Blockchain) SubscribeEvents

func (b *Blockchain) SubscribeEvents() Subscription

SubscribeEvents returns a blockchain event subscription

func (*Blockchain) VerifyFinalizedBlock

func (b *Blockchain) VerifyFinalizedBlock(block *types.Block) (*types.FullBlock, error)

VerifyFinalizedBlock verifies that the block is valid by performing a series of checks. It is assumed that the block status is sealed (committed)

func (*Blockchain) VerifyPotentialBlock

func (b *Blockchain) VerifyPotentialBlock(block *types.Block) error

VerifyPotentialBlock does the minimal block verification without consulting the consensus layer. Should only be used if consensus checks are done outside the method call

func (*Blockchain) WriteBlock

func (b *Blockchain) WriteBlock(block *types.Block, source string) error

WriteBlock writes a single block to the local blockchain. It doesn't do any kind of verification, only commits the block to the DB

func (*Blockchain) WriteFullBlock

func (b *Blockchain) WriteFullBlock(fblock *types.FullBlock, source string) error

WriteFullBlock writes a single block to the local blockchain. It doesn't do any kind of verification, only commits the block to the DB This function is a copy of WriteBlock but with a full block which does not require to compute again the Receipts.

func (*Blockchain) WriteHeaders

func (b *Blockchain) WriteHeaders(headers []*types.Header) error

WriteHeaders writes an array of headers

func (*Blockchain) WriteHeadersWithBodies

func (b *Blockchain) WriteHeadersWithBodies(headers []*types.Header) error

WriteHeadersWithBodies writes a batch of headers

type Event

type Event struct {
	// Old chain (removed headers) if there was a reorg
	OldChain []*types.Header

	// New part of the chain (or a fork)
	NewChain []*types.Header

	// Difficulty is the new difficulty created with this event
	Difficulty *big.Int

	// Type is the type of event
	Type EventType

	// Source is the source that generated the blocks for the event
	// right now it can be either the Sealer or the Syncer
	Source string
}

Event is the blockchain event that gets passed to the listeners

func (*Event) AddNewHeader

func (e *Event) AddNewHeader(newHeader *types.Header)

AddNewHeader appends a header to the event's NewChain array

func (*Event) AddOldHeader

func (e *Event) AddOldHeader(oldHeader *types.Header)

AddOldHeader appends a header to the event's OldChain array

func (*Event) Header

func (e *Event) Header() *types.Header

Header returns the latest block header for the event

func (*Event) SetDifficulty

func (e *Event) SetDifficulty(b *big.Int)

SetDifficulty sets the event difficulty

type EventType

type EventType int
const (
	EventHead  EventType = iota // New head event
	EventReorg                  // Chain reorganization event
	EventFork                   // Chain fork event
)

type Executor

type Executor interface {
	ProcessBlock(parentRoot types.Hash, block *types.Block, blockCreator types.Address) (*state.Transition, error)
}

type MockSubscription

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

func NewMockSubscription

func NewMockSubscription() *MockSubscription

func (MockSubscription) Close

func (s MockSubscription) Close()

Close closes the subscription

func (MockSubscription) GetEvent

func (s MockSubscription) GetEvent() *Event

GetEvent returns the event from the subscription (BLOCKING)

func (MockSubscription) GetEventCh

func (s MockSubscription) GetEventCh() chan *Event

GetEventCh creates a new event channel, and returns it

func (*MockSubscription) Push

func (m *MockSubscription) Push(e *Event)

type MockVerifier

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

func (*MockVerifier) GetBlockCreator

func (m *MockVerifier) GetBlockCreator(header *types.Header) (types.Address, error)

func (*MockVerifier) HookGetBlockCreator

func (m *MockVerifier) HookGetBlockCreator(fn getBlockCreatorDelegate)

func (*MockVerifier) HookPreCommitState

func (m *MockVerifier) HookPreCommitState(fn preStateCommitDelegate)

func (*MockVerifier) HookProcessHeaders

func (m *MockVerifier) HookProcessHeaders(fn processHeadersDelegate)

func (*MockVerifier) HookVerifyHeader

func (m *MockVerifier) HookVerifyHeader(fn verifyHeaderDelegate)

func (*MockVerifier) PreCommitState

func (m *MockVerifier) PreCommitState(header *types.Header, txn *state.Transition) error

func (*MockVerifier) ProcessHeaders

func (m *MockVerifier) ProcessHeaders(headers []*types.Header) error

func (*MockVerifier) VerifyHeader

func (m *MockVerifier) VerifyHeader(header *types.Header) error

type Subscription

type Subscription interface {
	GetEventCh() chan *Event
	GetEvent() *Event
	Close()
}

Subscription is the blockchain subscription interface

type TestCallbackType

type TestCallbackType string
const (
	VerifierCallback TestCallbackType = "VerifierCallback"
	ExecutorCallback TestCallbackType = "ExecutorCallback"
	ChainCallback    TestCallbackType = "ChainCallback"
	StorageCallback  TestCallbackType = "StorageCallback"
)

type TxSigner

type TxSigner interface {
	// Sender returns the sender of the transaction
	Sender(tx *types.Transaction) (types.Address, error)
}

type Verifier

type Verifier interface {
	VerifyHeader(header *types.Header) error
	ProcessHeaders(headers []*types.Header) error
	GetBlockCreator(header *types.Header) (types.Address, error)
	PreCommitState(header *types.Header, txn *state.Transition) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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