stf

package module
v0.0.0-...-ffc3f76 Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: Apache-2.0 Imports: 22 Imported by: 1

README

State Transition Function (STF)

STF is a function that takes a state and an action as input and returns the next state. It does not assume the execution model of the application nor consensus.

The state transition function receives a read only instance of state. It does not directly write to disk, instead it will return the state changes which has undergone within the application. The state transition function is deterministic, meaning that given the same input, it will always produce the same output.

BranchDB

BranchDB is a cache of all the reads done within a block, simulation or transaction validation. It takes a read-only instance of state and creates its own write instance using a btree. After all state transitions are done, the new change sets are returned to the caller.

The BranchDB can be replaced and optimized for specific use cases. The implementation is as follows

   type branchdb func(state store.ReaderMap) store.WriterMap

GasMeter

GasMeter is a utility that keeps track of the gas consumed by the state transition function. It is used to limit the amount of computation that can be done within a block.

The GasMeter can be replaced and optimized for specific use cases. The implementation is as follows:

type (
	// gasMeter is a function type that takes a gas limit as input and returns a gas.Meter.
	// It is used to measure and limit the amount of gas consumed during the execution of a function.
	gasMeter func(gasLimit uint64) gas.Meter

	// wrapGasMeter is a function type that wraps a gas meter and a store writer map.
	wrapGasMeter func(meter gas.Meter, store store.WriterMap) store.WriterMap
)

THe wrappGasMeter is used in order to consume gas. Application developers can seamlsessly replace the gas meter with their own implementation in order to customize consumption of gas.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoHandler = errors.New("no handler")

Functions

func NewEventService

func NewEventService() event.Service

func NewGasMeterService

func NewGasMeterService() gas.Service

NewGasMeterService creates a new instance of the gas meter service.

func NewKVStoreService

func NewKVStoreService(address []byte) store.KVStoreService

func NewMemoryStoreService

func NewMemoryStoreService(address []byte) store.MemoryStoreService

func NewMsgRouterService

func NewMsgRouterService(msgRouterBuilder *MsgRouterBuilder) router.Service

NewMsgRouterService implements router.Service.

func NewQueryRouterService

func NewQueryRouterService(queryRouterBuilder *MsgRouterBuilder) router.Service

NewQueryRouterService implements router.Service.

func TypedEventToEvent

func TypedEventToEvent(tev gogoproto.Message) (event.Event, error)

TypedEventToEvent takes typed event and converts to Event object

Types

type BranchService

type BranchService struct{}

func (BranchService) Execute

func (bs BranchService) Execute(ctx context.Context, f func(ctx context.Context) error) error

func (BranchService) ExecuteWithGasLimit

func (bs BranchService) ExecuteWithGasLimit(
	ctx context.Context,
	gasLimit uint64,
	f func(ctx context.Context) error,
) (gasUsed uint64, err error)

type HeaderService

type HeaderService struct{}

func (HeaderService) HeaderInfo

func (h HeaderService) HeaderInfo(ctx context.Context) header.Info

type MsgRouterBuilder

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

func NewMsgRouterBuilder

func NewMsgRouterBuilder() *MsgRouterBuilder

NewMsgRouterBuilder is a router that routes messages to their respective handlers.

func (*MsgRouterBuilder) Build

func (b *MsgRouterBuilder) Build() (appmodulev2.Handler, error)

func (*MsgRouterBuilder) HandlerExists

func (b *MsgRouterBuilder) HandlerExists(msgType string) bool

func (*MsgRouterBuilder) RegisterGlobalPostHandler

func (b *MsgRouterBuilder) RegisterGlobalPostHandler(handler appmodulev2.PostMsgHandler)

func (*MsgRouterBuilder) RegisterGlobalPreHandler

func (b *MsgRouterBuilder) RegisterGlobalPreHandler(handler appmodulev2.PreMsgHandler)

func (*MsgRouterBuilder) RegisterHandler

func (b *MsgRouterBuilder) RegisterHandler(msgType string, handler appmodulev2.Handler) error

func (*MsgRouterBuilder) RegisterPostHandler

func (b *MsgRouterBuilder) RegisterPostHandler(msgType string, handler appmodulev2.PostMsgHandler)

func (*MsgRouterBuilder) RegisterPreHandler

func (b *MsgRouterBuilder) RegisterPreHandler(msgType string, handler appmodulev2.PreMsgHandler)

type STF

type STF[T transaction.Tx] struct {
	// contains filtered or unexported fields
}

STF is a struct that manages the state transition component of the app.

func NewSTF

func NewSTF[T transaction.Tx](
	handleMsg func(ctx context.Context, msg transaction.Msg) (transaction.Msg, error),
	handleQuery func(ctx context.Context, req transaction.Msg) (transaction.Msg, error),
	doPreBlock func(ctx context.Context, txs []T) error,
	doBeginBlock func(ctx context.Context) error,
	doEndBlock func(ctx context.Context) error,
	doTxValidation func(ctx context.Context, tx T) error,
	doValidatorUpdate func(ctx context.Context) ([]appmodulev2.ValidatorUpdate, error),
	postTxExec func(ctx context.Context, tx T, success bool) error,
	branch func(store store.ReaderMap) store.WriterMap,
) *STF[T]

NewSTF returns a new STF instance.

func (STF[T]) DeliverBlock

func (s STF[T]) DeliverBlock(
	ctx context.Context,
	block *appmanager.BlockRequest[T],
	state store.ReaderMap,
) (blockResult *appmanager.BlockResponse, newState store.WriterMap, err error)

DeliverBlock is our state transition function. It takes a read only view of the state to apply the block to, executes the block and returns the block results and the new state.

func (STF[T]) Message

func (s STF[T]) Message(ctx context.Context, msg transaction.Msg) (response transaction.Msg, err error)

func (STF[T]) Query

func (s STF[T]) Query(
	ctx context.Context,
	state store.ReaderMap,
	gasLimit uint64,
	req transaction.Msg,
) (transaction.Msg, error)

Query executes the query on the provided state with the provided gas limits.

func (STF[T]) RunWithCtx

func (s STF[T]) RunWithCtx(
	ctx context.Context,
	state store.ReaderMap,
	closure func(ctx context.Context) error,
) (store.WriterMap, error)

RunWithCtx is made to support genesis, if genesis was just the execution of messages instead of being something custom then we would not need this. PLEASE DO NOT USE. TODO: Remove

func (STF[T]) Simulate

func (s STF[T]) Simulate(
	ctx context.Context,
	state store.ReaderMap,
	gasLimit uint64,
	tx T,
) (appmanager.TxResult, store.WriterMap)

Simulate simulates the execution of a tx on the provided state.

func (STF[T]) ValidateTx

func (s STF[T]) ValidateTx(
	ctx context.Context,
	state store.ReaderMap,
	gasLimit uint64,
	tx T,
) appmanager.TxResult

ValidateTx will run only the validation steps required for a transaction. Validations are run over the provided state, with the provided gas limit.

Directories

Path Synopsis
Package branch defines a Store that can be used to wrap readable state to make it writable.
Package branch defines a Store that can be used to wrap readable state to make it writable.

Jump to

Keyboard shortcuts

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