core

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: MIT Imports: 8 Imported by: 6

Documentation

Overview

nolint

Index

Constants

View Source
const (
	// TXDATA is a cost for storing transaction data included into the block. Charged per 8 byte.
	TXDATA uint64 = 128
	// TX is an intrinsic cost for every transaction.
	TX uint64 = 20000
	// SPAWN is an intrinsic cost for every spawn, on top of TX cost.
	SPAWN uint64 = 30000
	// STORE is a cost for storing new data, in precompiles charged only for SPAWN.
	STORE uint64 = 5000
	// UPDATE is a cost of updating mutable state (nonce, amount of coins, precompile specific state).
	UPDATE uint64 = 725
	// LOAD is a cost for loading immutable and mutable state from disk.
	LOAD uint64 = 182
	// ACCOUNT_ACCESS is a cost of the account access.
	ACCOUNT_ACCESS uint64 = 2500
	// EDVERIFY is a cost for running ed25519 single signature verification.
	EDVERIFY uint64 = 3000
)
View Source
const (
	PUBLIC_KEY_SIZE      = 32
	ACCOUNT_HEADER_SIZE  = 36 // includes balance (8), nonce (8) and template address (24)
	ACCOUNT_BALANCE_SIZE = 8
)
View Source
const (
	// MethodSpawn ...
	MethodSpawn = 0
	// MethodSpend ...
	MethodSpend = 16
)
View Source
const TxSizeLimit = 1024

Variables

View Source
var (
	// ErrInternal raised on any unexpected error due to internal conditions.
	// Most likely due to the disk failures.
	ErrInternal = errors.New("internal")
	// ErrMalformed raised if transaction cannot be decoded properly.
	ErrMalformed = errors.New("malformed tx")
	// ErrInvalidNonce raised due to the expected nonce mismatch.
	ErrInvalidNonce = errors.New("invalid nonce")
	// ErrNoBalance raised if transaction run out of balance during execution.
	ErrNoBalance = errors.New("no balance")
	// ErrMaxGas raised if tx consumed over MaxGas value.
	ErrMaxGas = errors.New("max gas")
	// ErrMaxSpend raised if tx transferred over MaxSpend value.
	ErrMaxSpend = errors.New("max spend")
	// ErrSpawned raised if account already spawned.
	ErrSpawned = errors.New("account already spawned")
	// ErrNotSpawned raised if account is not spawned.
	ErrNotSpawned = errors.New("account is not spawned")
	// ErrMismatchedTemplate raised if target account doesn't match template account.
	ErrTemplateMismatch = errors.New("relay template mismatch")
	// ErrTxLimit overflows max tx size.
	ErrTxLimit = errors.New("overflows tx limit")
)

Functions

func IntrinsicGas

func IntrinsicGas(baseGas uint64, tx []byte) uint64

IntrinsicGas computes intrinsic gas from base gas and storage cost.

func MaxGas

func MaxGas(baseGas, fixedGas uint64, tx []byte) uint64

MaxGas computes total gas cost by adding fixed gas to intrinsic gas cost.

func SigningBody

func SigningBody(genesis, tx []byte) []byte

func SizeGas

func SizeGas(gas uint64, size int) uint64

SizeGas computes total gas cost for a value of the specific size. Gas is charged for every 8 bytes, rounded up.

func TxDataGas

func TxDataGas(size int) uint64

Types

type Account

type Account = types.Account

Account is an alis to types.Account.

type AccountLoader

type AccountLoader interface {
	Get(Address) (Account, error)
}

AccountLoader is an interface for loading accounts.

type AccountUpdater

type AccountUpdater interface {
	Update(Account) error
}

AccountUpdater is an interface for updating accounts.

type Address

type Address = types.Address

Address is an alias to types.Address.

func ComputePrincipal

func ComputePrincipal(template Address, args scale.Encodable) Address

ComputePrincipal address as the last 20 bytes from blake3(scale(template || args)).

type Context

type Context struct {
	Registry HandlerRegistry
	Loader   AccountLoader

	// LayerID of the block.
	LayerID   LayerID
	GenesisID types.Hash20

	PrincipalHandler  Handler
	PrincipalTemplate Template
	PrincipalAccount  Account

	ParseOutput ParseOutput
	Gas         struct {
		BaseGas  uint64
		FixedGas uint64
	}
	Header Header
	Args   scale.Encodable
	// contains filtered or unexported fields
}

Context serves 2 purposes: - maintains changes to the system state, that will be applied only after succeful execution - accumulates set of reusable objects and data.

func (*Context) Apply

func (c *Context) Apply(updater AccountUpdater) error

Apply is executed if transaction was consumed.

func (*Context) Consume

func (c *Context) Consume(gas uint64) (err error)

Consume gas from the account after validation passes.

func (*Context) Consumed

func (c *Context) Consumed() uint64

Consumed gas.

func (*Context) Fee

func (c *Context) Fee() uint64

Fee computed from consumed gas.

func (*Context) GetGenesisID

func (c *Context) GetGenesisID() Hash20

GetGenesisID returns genesis id.

func (*Context) Handler

func (c *Context) Handler() Handler

Handler of the principal account.

func (*Context) Layer

func (c *Context) Layer() LayerID

Layer returns block layer id.

func (*Context) Principal

func (c *Context) Principal() Address

Principal returns address of the account that signed transaction.

func (*Context) Relay

func (c *Context) Relay(remoteTemplate, address Address, call func(Host) error) error

Relay call to the remote account.

func (*Context) Spawn

func (c *Context) Spawn(args scale.Encodable) error

Spawn account.

func (*Context) Template

func (c *Context) Template() Template

Template of the principal account.

func (*Context) Transfer

func (c *Context) Transfer(to Address, amount uint64) error

Transfer amount to the address after validation passes.

func (*Context) Updated

func (c *Context) Updated() []types.Address

Updated list of addresses.

type DBLoader

type DBLoader struct {
	sql.Executor
}

func (DBLoader) Get

func (db DBLoader) Get(address types.Address) (types.Account, error)

type Handler

type Handler interface {
	// Parse header and arguments from the payload.
	Parse(Host, uint8, *scale.Decoder) (ParseOutput, error)
	// Args returns method arguments for the method.
	Args(uint8) scale.Type

	// Exec dispatches execution request based on the method selector.
	Exec(Host, uint8, scale.Encodable) error

	// New instantiates Template from spawn arguments.
	New(any) (Template, error)
	// Load template with stored immutable state.
	Load([]byte) (Template, error)
}

Handler provides set of static templates method that are not directly attached to the state.

type HandlerRegistry

type HandlerRegistry interface {
	Get(Address) Handler
}

HandlerRegistry stores handlers for templates.

type Hash20

type Hash20 = types.Hash20

Hash20 is an alias to types.Hash20.

type Hash32

type Hash32 = types.Hash32

Hash32 is an alias to types.Hash32.

type Header = types.TxHeader

Header is an alias to types.TxHeader.

type Host

type Host interface {
	Consume(uint64) error
	Spawn(scale.Encodable) error
	Transfer(Address, uint64) error
	Relay(expectedTemplate, address Address, call func(Host) error) error

	Principal() Address
	Handler() Handler
	Template() Template
	Layer() LayerID
	GetGenesisID() Hash20
}

Host API with methods and data that are required by templates.

type LayerID

type LayerID = types.LayerID

LayerID is a layer type.

type Nonce

type Nonce = types.Nonce

Nonce is an alias to types.Nonce.

type ParseOutput

type ParseOutput struct {
	Nonce    Nonce
	GasPrice uint64
}

ParseOutput contains all fields that are returned by Parse call.

type Payload

type Payload struct {
	Nonce    Nonce
	GasPrice uint64
}

Payload is a generic payload for all transactions.

func (*Payload) DecodeScale

func (t *Payload) DecodeScale(dec *scale.Decoder) (total int, err error)

func (*Payload) EncodeScale

func (t *Payload) EncodeScale(enc *scale.Encoder) (total int, err error)

type PublicKey

type PublicKey = types.Hash32

PublicKey is an alias to Hash32.

type RemoteContext

type RemoteContext struct {
	*Context
	// contains filtered or unexported fields
}

RemoteContext ...

func (*RemoteContext) Handler

func (r *RemoteContext) Handler() Handler

Handler ...

func (*RemoteContext) Template

func (r *RemoteContext) Template() Template

Template ...

func (*RemoteContext) Transfer

func (r *RemoteContext) Transfer(to Address, amount uint64) error

Transfer ...

type Signature

type Signature = types.EdSignature

Signature is an alias to types.EdSignature.

type StagedCache

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

StagedCache is a passthrough cache for accounts state and enforces order for updated accounts.

func NewStagedCache

func NewStagedCache(loader AccountLoader) *StagedCache

NewStagedCache returns instance of the staged cache.

func (*StagedCache) Get

func (ss *StagedCache) Get(address Address) (Account, error)

Get a copy of the Account state for the address.

func (*StagedCache) IterateChanged

func (ss *StagedCache) IterateChanged(f func(*Account) bool)

IterateChanged accounts in the order they were updated.

func (*StagedCache) Update

func (ss *StagedCache) Update(account Account) error

Update cache with a copy of the account state.

type Template

type Template interface {
	// Template needs to implement scale.Encodable as mutable and immutable state will be stored as a blob of bytes.
	scale.Encodable
	// MaxSpend decodes MaxSpend value for the transaction. Transaction will fail
	// if it spends more than that.
	MaxSpend(uint8, any) (uint64, error)
	// BaseGas is an intrinsic cost for executing a transaction. If this cost is not covered
	// transaction will be ineffective.
	BaseGas(uint8) uint64
	// LoadGas is a cost to load account from disk.
	LoadGas() uint64
	// ExecGas is a cost to execution a method.
	ExecGas(uint8) uint64
	// Verify security of the transaction.
	Verify(Host, []byte, *scale.Decoder) bool
}

Template is a concrete Template type initialized with mutable and immutable state.

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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