vm

package
v0.0.0-...-1df7544 Latest Latest
Warning

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

Go to latest
Published: May 12, 2021 License: GPL-3.0 Imports: 12 Imported by: 1

Documentation

Overview

Package vm implements the Ethereum Virtual Machine.

The vm package implements two EVMs, a byte code VM and a JIT VM. The BC (Byte Code) VM loops over a set of bytes and executes them according to the set of rules defined in the MoacNode yellow paper. When the BC VM is invoked it invokes the JIT VM in a separate goroutine and compiles the byte code in JIT instructions.

The JIT VM, when invoked, loops around a set of pre-defined instructions until it either runs of gas, causes an internal error, returns or stops.

The JIT optimiser attempts to pre-compile instructions in to chunks or segments such as multiple PUSH operations and static JUMPs. It does this by analysing the opcodes and attempts to match certain regions to known sets. Whenever the optimiser finds said segments it creates a new instruction and replaces the first occurrence in the sequence.

Index

Constants

View Source
const (
	None = -1
)

Variables

This section is empty.

Functions

func Run

func Run(evm *EVM, snapshot int, contract *Contract, input []byte, precompiledContracts ContractsInterface, msgHash *common.Hash) ([]byte, error)

Run runs the given contract and takes care of running precompiles with a fallback to the byte code interpreter.

func SetEVM

func SetEVM(evm *EVM)

SetEVM the latest EVM instance.

Types

type CTXPool

type CTXPool interface {
	AddShardJoinCall(tx *types.Transaction) error
}

type CanTransferFunc

type CanTransferFunc func(StateDB, common.Address, *big.Int) bool

type Context

type Context struct {
	// CanTransfer returns whether the account contains
	// sufficient ether to transfer the value
	CanTransfer CanTransferFunc
	// Transfer transfers ether from one account to the other
	Transfer TransferFunc
	// GetHash returns the hash corresponding to n
	GetHash GetHashFunc

	// Message information
	Origin   common.Address // Provides information for ORIGIN
	GasPrice *big.Int       // Provides information for GASPRICE

	// Block information
	Coinbase    common.Address // Provides information for COINBASE
	GasLimit    *big.Int       // Provides information for GASLIMIT
	BlockNumber *big.Int       // Provides information for NUMBER
	Time        *big.Int       // Provides information for TIME
	Difficulty  *big.Int       // Provides information for DIFFICULTY
	CtxTxpool   CTXPool
}

Context provides the EVM with auxiliary information. Once provided it shouldn't be modified.

type ContractCallStruct

type ContractCallStruct struct {
	Sender       []byte
	Contractaddr []byte
	Input        []byte
	Gas          uint64
	Val          []byte
	Sync         bool
	Code         []byte
	Codehash     []byte
}

type ContractsInterface

type ContractsInterface interface {
	PrecompiledContractsPangu() map[common.Address]PrecompiledContract
	PrecompiledContractsByzantium() map[common.Address]PrecompiledContract
	RunPrecompiledContract(evm *EVM, snapshot int, p PrecompiledContract, input []byte, contract *Contract, hash *common.Hash) (ret []byte, err error)
	SystemContractCallAddr() common.Address
	SystemContractEntryAddr(num *big.Int) common.Address
	IsSystemCaller(caller ContractRef) (ret bool)
	WhiteListCallAddr() common.Address
}

type EVM

type EVM struct {
	// Context provides auxiliary blockchain related information
	Context
	// StateDB gives access to the underlying state
	StateDB StateDB

	// virtual machine configuration options used to initialise the
	// evm.
	VmConfig Config

	// MOAC additions:
	// handle the network traffic with SCS servers.
	Nr NetworkRelayInterface
	// contains filtered or unexported fields
}

EVM is the Ethereum Virtual Machine base object and provides the necessary tools to run a contract on the given state with the provided context. It should be noted that any error generated through any of the calls should be considered a revert-state-and-consume-all-gas operation, no checks on specific errors should ever be performed. The interpreter makes sure that any errors generated are to be considered faulty code.

The EVM should never be reused and is not thread safe.

func GetEVM

func GetEVM() *EVM

GetEVM the latest EVM instance.

func NewEVM

func NewEVM(ctx Context, statedb StateDB, chainConfig *params.ChainConfig, vmConfig Config, nr NetworkRelayInterface) *EVM

NewEVM retutrns a new EVM . The returned EVM is not thread safe and should only ever be used *once*.

func (*EVM) ApplyFlushChanges

func (evm *EVM) ApplyFlushChanges(flushResult map[int]*pb.ScsPushMsg) (err error)

func (*EVM) Call

func (evm *EVM) Call(
	caller ContractRef,
	addr common.Address,
	input []byte,
	gasLimit uint64,
	value *big.Int,
	synccall bool,
	shardingFlag uint64,
	precompiledContracts ContractsInterface,
	msgHash *common.Hash) (
	ret []byte,
	leftOverGas uint64,
	err error)

Call executes the contract associated with the addr with the given input as parameters. It also handles any necessary value transfer required and takes the necessary steps to create accounts and reverses the state in case of an execution error or failed value transfer.

func (*EVM) CallCode

func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int, precompiledContracts ContractsInterface, msgHash *common.Hash) (ret []byte, leftOverGas uint64, err error)

CallCode executes the contract associated with the addr with the given input as parameters. It also handles any necessary value transfer required and takes the necessary steps to create accounts and reverses the state in case of an execution error or failed value transfer.

CallCode differs from Call in the sense that it executes the given address' code with the caller as context.

func (*EVM) Cancel

func (evm *EVM) Cancel()

Cancel cancels any running EVM operation. This may be called concurrently and it's safe to be called multiple times.

func (*EVM) ChainConfig

func (evm *EVM) ChainConfig() *params.ChainConfig

ChainConfig returns the evmironment's chain configuration

func (*EVM) Create

func (evm *EVM) Create(caller ContractRef, code []byte, gasRemaining uint64, value *big.Int,
	shardflag uint64, precompiledContracts ContractsInterface, msgHash *common.Hash) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error)

Create creates a new contract using code as deployment code.

func (*EVM) CreateSystemContract

func (evm *EVM) CreateSystemContract(sysaddr common.Address, code []byte, precompiledContracts ContractsInterface, msgHash *common.Hash) (ret []byte, err error)

Create creates a system contract using code as deployment code.

func (*EVM) DelegateCall

func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []byte, gas uint64, precompiledContracts ContractsInterface, msgHash *common.Hash) (ret []byte, leftOverGas uint64, err error)

DelegateCall executes the contract associated with the addr with the given input as parameters. It reverses the state in case of an execution error.

DelegateCall differs from CallCode in the sense that it executes the given address' code with the caller as context and the caller is set to the caller of the caller.

func (*EVM) Interpreter

func (evm *EVM) Interpreter() *Interpreter

Interpreter returns the EVM interpreter

func (*EVM) Query

func (evm *EVM) Query(addr common.Address) (ret []byte, leftOverGas uint64, err error)

func (*EVM) StaticCall

func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte, gas uint64, precompiledContracts ContractsInterface, msgHash *common.Hash) (ret []byte, leftOverGas uint64, err error)

StaticCall executes the contract associated with the addr with the given input as parameters while disallowing any modifications to the state during the call. Opcodes that attempt to perform such modifications will result in exceptions instead of performing the modifications.

type GetHashFunc

type GetHashFunc func(uint64) common.Hash

GetHashFunc returns the nth block hash in the blockchain and is used by the BLOCKHASH EVM op code.

type NetworkRelayInterface

type NetworkRelayInterface interface {
	VnodePushMsg(conReq *pb.ScsPushMsg) (map[int]*pb.ScsPushMsg, error)
	NotifyScs(address common.Address, msg []byte, hash common.Hash, block *big.Int)
	UpdateWhiteState(block uint64)
}

type PrecompiledContract

type PrecompiledContract interface {
	RequiredGas(input []byte) uint64                                                                 // RequiredPrice calculates the contract gas use
	Run(evm *EVM, snapshot int, contract *Contract, input []byte, hash *common.Hash) ([]byte, error) // Run runs the precompiled contract
}

type QueryResult

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

type TransferFunc

type TransferFunc func(StateDB, common.Address, common.Address, *big.Int)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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