interop

package
v0.0.0-...-886a677 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2024 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

Package interop contains implementations of Neo interop functions.

This is not the package you use from smart contracts, refer to pkg/interop for that.

Index

Constants

View Source
const (
	// DefaultBaseExecFee specifies the default multiplier for opcode and syscall prices.
	DefaultBaseExecFee = 30
)

Variables

This section is empty.

Functions

func Sort

func Sort(fs []Function)

Sort sorts interop functions by id.

Types

type Context

type Context struct {
	Chain         Ledger
	Container     hash.Hashable
	Network       uint32
	Hardforks     map[string]uint32
	Natives       []Contract
	Trigger       trigger.Type
	Block         *block.Block
	NonceData     [16]byte
	Tx            *transaction.Transaction
	DAO           *dao.Simple
	Notifications []state.NotificationEvent
	Log           *zap.Logger
	VM            *vm.VM
	Functions     []Function
	Invocations   map[util.Uint160]int

	GetRandomCounter uint32
	// contains filtered or unexported fields
}

Context represents context in which interops are executed.

func NewContext

func NewContext(trigger trigger.Type, bc Ledger, d *dao.Simple, baseExecFee, baseStorageFee int64,
	getContract func(*dao.Simple, util.Uint160) (*state.Contract, error), natives []Contract,
	loadTokenFunc func(ic *Context, id int32) error,
	block *block.Block, tx *transaction.Transaction, log *zap.Logger) *Context

NewContext returns new interop context.

func (*Context) AddNotification

func (ic *Context) AddNotification(hash util.Uint160, name string, item *stackitem.Array)

AddNotification creates notification event and appends it to the notification list.

func (*Context) BaseExecFee

func (ic *Context) BaseExecFee() int64

BaseExecFee represents factor to multiply syscall prices with.

func (*Context) BaseStorageFee

func (ic *Context) BaseStorageFee() int64

BaseStorageFee represents price for storing one byte of data in the contract storage.

func (*Context) BlockHeight

func (ic *Context) BlockHeight() uint32

BlockHeight returns the latest persisted and stored block height/index. Persisting block index is not taken into account. If Context's block is set, then BlockHeight calculations relies on persisting block index.

func (*Context) CurrentBlockHash

func (ic *Context) CurrentBlockHash() util.Uint256

CurrentBlockHash returns current block hash got from Context's block if it's set.

func (*Context) Exec

func (ic *Context) Exec() error

Exec executes loaded VM script and calls registered finalizers to release the occupied resources.

func (*Context) Finalize

func (ic *Context) Finalize()

Finalize calls all registered cancel functions to release the occupied resources.

func (*Context) GetBlock

func (ic *Context) GetBlock(hash util.Uint256) (*block.Block, error)

GetBlock returns block if it exists and available at the current Context's height.

func (*Context) GetContract

func (ic *Context) GetContract(hash util.Uint160) (*state.Contract, error)

GetContract returns a contract by its hash in the current interop context.

func (*Context) GetFunction

func (ic *Context) GetFunction(id uint32) *Function

GetFunction returns metadata for interop with the specified id.

func (*Context) GetPrice

func (ic *Context) GetPrice(op opcode.Opcode, parameter []byte) int64

GetPrice returns a price for executing op with the provided parameter.

func (*Context) InitNonceData

func (ic *Context) InitNonceData()

InitNonceData initializes nonce to be used in `GetRandom` calculations.

func (*Context) IsHardforkActivation

func (ic *Context) IsHardforkActivation(hf config.Hardfork) bool

IsHardforkActivation denotes whether current block height is the height of specified hardfork activation.

func (*Context) IsHardforkEnabled

func (ic *Context) IsHardforkEnabled(hf config.Hardfork) bool

IsHardforkEnabled tells whether specified hard-fork enabled at the current context height.

func (*Context) LoadToken

func (ic *Context) LoadToken(id int32) error

LoadToken wraps externally provided load-token loading function providing it with context, this function can then be easily used by VM.

func (*Context) RegisterCancelFunc

func (ic *Context) RegisterCancelFunc(f context.CancelFunc)

RegisterCancelFunc adds the given function to the list of functions to be called after the VM finishes script execution.

func (*Context) ReuseVM

func (ic *Context) ReuseVM(v *vm.VM)

ReuseVM resets given VM and allows to reuse it in the current context.

func (*Context) Signers

func (ic *Context) Signers() []transaction.Signer

Signers returns signers witnessing the current execution context.

func (*Context) SpawnVM

func (ic *Context) SpawnVM() *vm.VM

SpawnVM spawns a new VM with the specified gas limit and set context.VM field.

func (*Context) SyscallHandler

func (ic *Context) SyscallHandler(_ *vm.VM, id uint32) error

SyscallHandler handles syscall with id.

func (*Context) UseSigners

func (ic *Context) UseSigners(s []transaction.Signer)

UseSigners allows overriding signers used in this context.

type Contract

type Contract interface {
	// Initialize performs native contract initialization on contract deploy or update.
	// Active hardfork is passed as the second argument.
	Initialize(*Context, *config.Hardfork, *HFSpecificContractMD) error
	// ActiveIn returns the hardfork native contract is active starting from or nil in case
	// it's always active.
	ActiveIn() *config.Hardfork
	// InitializeCache aimed to initialize contract's cache when the contract has
	// been deployed, but in-memory cached data were lost due to the node reset.
	// It should be called each time after node restart iff the contract was
	// deployed and no Initialize method was called.
	InitializeCache(blockHeight uint32, d *dao.Simple) error
	// Metadata returns generic native contract metadata.
	Metadata() *ContractMD
	OnPersist(*Context) error
	PostPersist(*Context) error
}

Contract is an interface for all native contracts.

type ContractMD

type ContractMD struct {
	ID   int32
	Hash util.Uint160
	Name string

	// ActiveHFs is a map of hardforks that contract should react to. Contract update should be called for active
	// hardforks. Note, that unlike the C# implementation, this map doesn't include contract's activation hardfork.
	// This map is being initialized on contract creation and used as a read-only, hence, not protected
	// by mutex.
	ActiveHFs map[config.Hardfork]struct{}
	// contains filtered or unexported fields
}

ContractMD represents a generic hardfork-independent native contract instance.

func NewContractMD

func NewContractMD(name string, id int32, onManifestConstruction ...func(*manifest.Manifest)) *ContractMD

NewContractMD returns Contract with the specified fields set. onManifestConstruction callback every time after hardfork-specific manifest creation and aimed to finalize the manifest.

func (*ContractMD) AddEvent

func (c *ContractMD) AddEvent(md Event)

AddEvent adds a new event to the native contract.

func (*ContractMD) AddMethod

func (c *ContractMD) AddMethod(md *MethodAndPrice, desc *manifest.Method)

AddMethod adds a new method to a native contract.

func (*ContractMD) BuildHFSpecificMD

func (c *ContractMD) BuildHFSpecificMD(activeIn *config.Hardfork)

BuildHFSpecificMD generates and caches contract's descriptor for every known hardfork.

func (*ContractMD) HFSpecificContractMD

func (c *ContractMD) HFSpecificContractMD(hf *config.Hardfork) *HFSpecificContractMD

HFSpecificContractMD returns hardfork-specific native contract metadata, i.e. with methods, events and script corresponding to the specified hardfork. If hardfork is not specified, then default metadata will be returned (methods, events and script that are always active). Calling this method for hardforks older than the contract activation hardfork is a no-op.

type Event

type Event struct {
	HFSpecificEvent
	ActiveFrom *config.Hardfork
	ActiveTill *config.Hardfork
}

Event is a generic hardfork-independent native contract event descriptor.

type Function

type Function struct {
	ID   uint32
	Name string
	Func func(*Context) error
	// ParamCount is a number of function parameters.
	ParamCount int
	Price      int64
	// RequiredFlags is a set of flags which must be set during script invocations.
	// Default value is NoneFlag i.e. no flags are required.
	RequiredFlags callflag.CallFlag
}

Function binds function name, id with the function itself and the price, it's supposed to be inited once for all interopContexts, so it doesn't use vm.InteropFuncPrice directly.

type HFSpecificContractMD

type HFSpecificContractMD struct {
	state.ContractBase
	Methods []HFSpecificMethodAndPrice
	Events  []HFSpecificEvent
}

HFSpecificContractMD is a hardfork-specific native contract descriptor.

func (*HFSpecificContractMD) GetMethod

func (c *HFSpecificContractMD) GetMethod(name string, paramCount int) (HFSpecificMethodAndPrice, bool)

GetMethod returns method `name` with the specified number of parameters.

func (*HFSpecificContractMD) GetMethodByOffset

func (c *HFSpecificContractMD) GetMethodByOffset(offset int) (HFSpecificMethodAndPrice, bool)

GetMethodByOffset returns method with the provided offset. Offset is offset of `System.Contract.CallNative` syscall.

type HFSpecificEvent

type HFSpecificEvent struct {
	MD *manifest.Event
}

HFSpecificEvent is a hardfork-specific native contract event descriptor.

type HFSpecificMethodAndPrice

type HFSpecificMethodAndPrice struct {
	Func          Method
	MD            *manifest.Method
	CPUFee        int64
	StorageFee    int64
	SyscallOffset int
	RequiredFlags callflag.CallFlag
}

HFSpecificMethodAndPrice is a hardfork-specific native contract method descriptor.

type Ledger

type Ledger interface {
	BlockHeight() uint32
	CurrentBlockHash() util.Uint256
	GetBlock(hash util.Uint256) (*block.Block, error)
	GetConfig() config.Blockchain
	GetHeaderHash(uint32) util.Uint256
}

Ledger is the interface to Blockchain required for Context functionality.

type Method

type Method = func(ic *Context, args []stackitem.Item) stackitem.Item

Method is a signature for a native method.

type MethodAndPrice

type MethodAndPrice struct {
	HFSpecificMethodAndPrice
	ActiveFrom *config.Hardfork
	ActiveTill *config.Hardfork
}

MethodAndPrice is a generic hardfork-independent native contract method descriptor.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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