vm

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2016 License: GPL-3.0 Imports: 17 Imported by: 0

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 Ethereum 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

This section is empty.

Variables

View Source
var (
	Pow256 = common.BigPow(2, 256) // Pow256 is 2**256

	U256 = common.U256 // Shortcut to common.U256
	S256 = common.S256 // Shortcut to common.S256

	Zero = common.Big0 // Shortcut to common.Big0
	One  = common.Big1 // Shortcut to common.Big1

)
View Source
var (
	GasQuickStep   = big.NewInt(2)
	GasFastestStep = big.NewInt(3)
	GasFastStep    = big.NewInt(5)
	GasMidStep     = big.NewInt(8)
	GasSlowStep    = big.NewInt(10)
	GasExtStep     = big.NewInt(20)

	GasReturn = big.NewInt(0)
	GasStop   = big.NewInt(0)

	GasContractByte = big.NewInt(200)
)
View Source
var CodeStoreOutOfGasError = errors.New("Contract creation code storage out of gas")
View Source
var DepthError = fmt.Errorf("Max call depth exceeded (%d)", params.CallCreateDepth)
View Source
var MaxProgSize int // Max cache size for JIT programs
View Source
var OutOfGasError = errors.New("Out of gas")
View Source
var Precompiled = PrecompiledContracts()

Precompiled contains the default set of ethereum contracts

Functions

func CompileProgram

func CompileProgram(program *Program) (err error)

CompileProgram compiles the given program and return an error when it fails

func Disasm

func Disasm(code []byte) []string

func Disassemble

func Disassemble(script []byte) (asm []string)

Disassemble disassembles the byte code and returns the string representation (human readable opcodes).

func GetProgramStatus

func GetProgramStatus(id common.Hash) progStatus

GenProgramStatus returns the status of the given program id

func MatchFn

func MatchFn(input, match []OpCode, matcherFn func(int) bool)

MatchFn searcher for match in the given input and calls matcheFn if it finds an appropriate match. matcherFn yields the starting position in the input. MatchFn will continue to search for a match until it reaches the end of the buffer or if matcherFn return false.

func PrecompiledContracts

func PrecompiledContracts() map[string]*PrecompiledAccount

PrecompiledContracts returns the default set of precompiled ethereum contracts defined by the ethereum yellow paper.

func RunProgram

func RunProgram(program *Program, env Environment, contract *Contract, input []byte) ([]byte, error)

RunProgram runs the program given the environment and contract and returns an error if the execution failed (non-consensus)

func SetJITCacheSize

func SetJITCacheSize(size int)

SetJITCacheSize recreates the program cache with the max given size. Setting a new cache is **not** thread safe. Use with caution.

func StdErrFormat

func StdErrFormat(logs []StructLog)

StdErrFormat formats a slice of StructLogs to human readable format

Types

type Account

type Account interface {
	SubBalance(amount *big.Int)
	AddBalance(amount *big.Int)
	SetBalance(*big.Int)
	SetNonce(uint64)
	Balance() *big.Int
	Address() common.Address
	ReturnGas(*big.Int, *big.Int)
	SetCode(common.Hash, []byte)
	ForEachStorage(cb func(key, value common.Hash) bool)
	Value() *big.Int
}

Account represents a contract or basic ethereum account.

type Config

type Config struct {
	Debug     bool
	EnableJit bool
	ForceJit  bool
	Tracer    Tracer
}

Config are the configuration options for the EVM

type Contract

type Contract struct {
	// CallerAddress is the result of the caller which initialised this
	// contract. However when the "call method" is delegated this value
	// needs to be initialised to that of the caller's caller.
	CallerAddress common.Address

	Code     []byte
	CodeHash common.Hash
	CodeAddr *common.Address
	Input    []byte

	Gas, UsedGas, Price *big.Int

	Args []byte

	DelegateCall bool
	// contains filtered or unexported fields
}

Contract represents an ethereum contract in the state database. It contains the the contract code, calling arguments. Contract implements ContractRef

func NewContract

func NewContract(caller ContractRef, object ContractRef, value, gas, price *big.Int) *Contract

NewContract returns a new contract environment for the execution of EVM.

func (*Contract) Address

func (c *Contract) Address() common.Address

Address returns the contracts address

func (*Contract) AsDelegate

func (c *Contract) AsDelegate() *Contract

AsDelegate sets the contract to be a delegate call and returns the current contract (for chaining calls)

func (*Contract) Caller

func (c *Contract) Caller() common.Address

Caller returns the caller of the contract.

Caller will recursively call caller when the contract is a delegate call, including that of caller's caller.

func (*Contract) Finalise

func (c *Contract) Finalise()

Finalise finalises the contract and returning any remaining gas to the original caller.

func (*Contract) ForEachStorage

func (self *Contract) ForEachStorage(cb func(key, value common.Hash) bool)

EachStorage iterates the contract's storage and calls a method for every key value pair.

func (*Contract) GetByte

func (c *Contract) GetByte(n uint64) byte

GetByte returns the n'th byte in the contract's byte array

func (*Contract) GetOp

func (c *Contract) GetOp(n uint64) OpCode

GetOp returns the n'th element in the contract's byte array

func (*Contract) ReturnGas

func (c *Contract) ReturnGas(gas, price *big.Int)

ReturnGas adds the given gas back to itself.

func (*Contract) SetCallCode

func (self *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte)

SetCallCode sets the code of the contract and address of the backing data object

func (*Contract) SetCode

func (self *Contract) SetCode(hash common.Hash, code []byte)

SetCode sets the code to the contract

func (*Contract) UseGas

func (c *Contract) UseGas(gas *big.Int) (ok bool)

UseGas attempts the use gas and subtracts it and returns true on success

func (*Contract) Value

func (c *Contract) Value() *big.Int

Value returns the contracts value (sent to it from it's caller)

type ContractRef

type ContractRef interface {
	ReturnGas(*big.Int, *big.Int)
	Address() common.Address
	Value() *big.Int
	SetCode(common.Hash, []byte)
	ForEachStorage(callback func(key, value common.Hash) bool)
}

ContractRef is a reference to the contract's backing object

type Database

type Database interface {
	GetAccount(common.Address) Account
	CreateAccount(common.Address) Account

	AddBalance(common.Address, *big.Int)
	GetBalance(common.Address) *big.Int

	GetNonce(common.Address) uint64
	SetNonce(common.Address, uint64)

	GetCodeHash(common.Address) common.Hash
	GetCodeSize(common.Address) int
	GetCode(common.Address) []byte
	SetCode(common.Address, []byte)

	AddRefund(*big.Int)
	GetRefund() *big.Int

	GetState(common.Address, common.Hash) common.Hash
	SetState(common.Address, common.Hash, common.Hash)

	Suicide(common.Address) bool
	HasSuicided(common.Address) bool

	// Exist reports whether the given account exists in state.
	// Notably this should also return true for suicided accounts.
	Exist(common.Address) bool
}

Database is a EVM database for full state querying.

type EVM

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

EVM is used to run Ethereum based contracts and will utilise the passed environment to query external sources for state information. The EVM will run the byte code VM or JIT VM based on the passed configuration.

func New

func New(env Environment, cfg Config) *EVM

New returns a new instance of the EVM.

func (*EVM) Run

func (evm *EVM) Run(contract *Contract, input []byte) (ret []byte, err error)

Run loops and evaluates the contract's code with the given input data

func (*EVM) RunPrecompiled

func (evm *EVM) RunPrecompiled(p *PrecompiledAccount, input []byte, contract *Contract) (ret []byte, err error)

RunPrecompile runs and evaluate the output of a precompiled contract defined in contracts.go

type Environment

type Environment interface {
	// The current ruleset
	RuleSet() RuleSet
	// The state database
	Db() Database
	// Creates a restorable snapshot
	SnapshotDatabase() int
	// Set database to previous snapshot
	RevertToSnapshot(int)
	// Address of the original invoker (first occurrence of the VM invoker)
	Origin() common.Address
	// The block number this VM is invoked on
	BlockNumber() *big.Int
	// The n'th hash ago from this block number
	GetHash(uint64) common.Hash
	// The handler's address
	Coinbase() common.Address
	// The current time (block time)
	Time() *big.Int
	// Difficulty set on the current block
	Difficulty() *big.Int
	// The gas limit of the block
	GasLimit() *big.Int
	// Determines whether it's possible to transact
	CanTransfer(from common.Address, balance *big.Int) bool
	// Transfers amount from one account to the other
	Transfer(from, to Account, amount *big.Int)
	// Adds a LOG to the state
	AddLog(*Log)
	// Type of the VM
	Vm() Vm
	// Get the curret calling depth
	Depth() int
	// Set the current calling depth
	SetDepth(i int)
	// Call another contract
	Call(me ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
	// Take another's contract code and execute within our own context
	CallCode(me ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
	// Same as CallCode except sender and value is propagated from parent to child scope
	DelegateCall(me ContractRef, addr common.Address, data []byte, gas, price *big.Int) ([]byte, error)
	// Create a new contract
	Create(me ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error)
	// Returns whether the call is in read only mode
	ReadOnly() bool
}

Environment is an EVM requirement and helper which allows access to outside information such as states.

type Log

type Log struct {
	// Consensus fields.
	Address common.Address // address of the contract that generated the event
	Topics  []common.Hash  // list of topics provided by the contract.
	Data    []byte         // supplied by the contract, usually ABI-encoded

	// Derived fields (don't reorder!).
	BlockNumber uint64      // block in which the transaction was included
	TxHash      common.Hash // hash of the transaction
	TxIndex     uint        // index of the transaction in the block
	BlockHash   common.Hash // hash of the block in which the transaction was included
	Index       uint        // index of the log in the receipt
}

Log represents a contract log event. These events are generated by the LOG opcode and stored/indexed by the node.

func NewLog

func NewLog(address common.Address, topics []common.Hash, data []byte, number uint64) *Log

func (*Log) DecodeRLP

func (l *Log) DecodeRLP(s *rlp.Stream) error

func (*Log) EncodeRLP

func (l *Log) EncodeRLP(w io.Writer) error

func (*Log) MarshalJSON

func (r *Log) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*Log) String

func (l *Log) String() string

func (*Log) UnmarshalJSON

func (r *Log) UnmarshalJSON(input []byte) error

UnmarshalJSON implements json.Umarshaler.

type LogConfig

type LogConfig struct {
	DisableMemory  bool // disable memory capture
	DisableStack   bool // disable stack capture
	DisableStorage bool // disable storage capture
	FullStorage    bool // show full storage (slow)
}

LogConfig are the configuration options for structured logger the EVM

type LogForStorage

type LogForStorage Log

LogForStorage is a wrapper around a Log that flattens and parses the entire content of a log, as opposed to only the consensus fields originally (by hiding the rlp interface methods).

type Logs

type Logs []*Log

type Memory

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

Memory implements a simple memory model for the ethereum virtual machine.

func NewMemory

func NewMemory() *Memory

func (*Memory) Data

func (m *Memory) Data() []byte

Data returns the backing slice

func (*Memory) Get

func (self *Memory) Get(offset, size int64) (cpy []byte)

Get returns offset + size as a new slice

func (*Memory) GetPtr

func (self *Memory) GetPtr(offset, size int64) []byte

GetPtr returns the offset + size

func (*Memory) Len

func (m *Memory) Len() int

Len returns the length of the backing slice

func (*Memory) Print

func (m *Memory) Print()

func (*Memory) Resize

func (m *Memory) Resize(size uint64)

Resize resizes the memory to size

func (*Memory) Set

func (m *Memory) Set(offset, size uint64, value []byte)

Set sets offset + size to value

type OpCode

type OpCode byte

OpCode is an EVM opcode

const (
	// 0x0 range - arithmetic ops
	STOP OpCode = iota
	ADD
	MUL
	SUB
	DIV
	SDIV
	MOD
	SMOD
	ADDMOD
	MULMOD
	EXP
	SIGNEXTEND
)
const (
	LT OpCode = iota + 0x10
	GT
	SLT
	SGT
	EQ
	ISZERO
	AND
	OR
	XOR
	NOT
	BYTE

	SHA3 = 0x20
)
const (
	// 0x30 range - closure state
	ADDRESS OpCode = 0x30 + iota
	BALANCE
	ORIGIN
	CALLER
	CALLVALUE
	CALLDATALOAD
	CALLDATASIZE
	CALLDATACOPY
	CODESIZE
	CODECOPY
	GASPRICE
	EXTCODESIZE
	EXTCODECOPY
)
const (

	// 0x40 range - block operations
	BLOCKHASH OpCode = 0x40 + iota
	COINBASE
	TIMESTAMP
	NUMBER
	DIFFICULTY
	GASLIMIT
)
const (
	// 0x50 range - 'storage' and execution
	POP OpCode = 0x50 + iota
	MLOAD
	MSTORE
	MSTORE8
	SLOAD
	SSTORE
	JUMP
	JUMPI
	PC
	MSIZE
	GAS
	JUMPDEST
)
const (
	// 0x60 range
	PUSH1 OpCode = 0x60 + iota
	PUSH2
	PUSH3
	PUSH4
	PUSH5
	PUSH6
	PUSH7
	PUSH8
	PUSH9
	PUSH10
	PUSH11
	PUSH12
	PUSH13
	PUSH14
	PUSH15
	PUSH16
	PUSH17
	PUSH18
	PUSH19
	PUSH20
	PUSH21
	PUSH22
	PUSH23
	PUSH24
	PUSH25
	PUSH26
	PUSH27
	PUSH28
	PUSH29
	PUSH30
	PUSH31
	PUSH32
	DUP1
	DUP2
	DUP3
	DUP4
	DUP5
	DUP6
	DUP7
	DUP8
	DUP9
	DUP10
	DUP11
	DUP12
	DUP13
	DUP14
	DUP15
	DUP16
	SWAP1
	SWAP2
	SWAP3
	SWAP4
	SWAP5
	SWAP6
	SWAP7
	SWAP8
	SWAP9
	SWAP10
	SWAP11
	SWAP12
	SWAP13
	SWAP14
	SWAP15
	SWAP16
)
const (
	LOG0 OpCode = 0xa0 + iota
	LOG1
	LOG2
	LOG3
	LOG4
)
const (
	PUSH OpCode = 0xb0 + iota
	DUP
	SWAP
)

unofficial opcodes used for parsing

const (
	// 0xf0 range - closures
	CREATE OpCode = 0xf0 + iota
	CALL
	CALLCODE
	RETURN
	DELEGATECALL

	SUICIDE = 0xff
)

func Parse

func Parse(code []byte) (opcodes []OpCode)

Parse parses all opcodes from the given code byte slice. This function performs no error checking and may return non-existing opcodes.

func StringToOp

func StringToOp(str string) OpCode

func (OpCode) IsPush

func (op OpCode) IsPush() bool

func (OpCode) IsStaticJump

func (op OpCode) IsStaticJump() bool

func (OpCode) String

func (o OpCode) String() string

type PrecompiledAccount

type PrecompiledAccount struct {
	Gas func(l int) *big.Int
	// contains filtered or unexported fields
}

PrecompiledAccount represents a native ethereum contract

func (PrecompiledAccount) Call

func (self PrecompiledAccount) Call(in []byte) []byte

Call calls the native function

type Program

type Program struct {
	Id common.Hash // Id of the program
	// contains filtered or unexported fields
}

Program is a compiled program for the JIT VM and holds all required for running a compiled JIT program.

func GetProgram

func GetProgram(id common.Hash) *Program

GetProgram returns the program by id or nil when non-existent

func NewProgram

func NewProgram(code []byte) *Program

NewProgram returns a new JIT program

type RuleSet

type RuleSet interface {
	IsHomestead(*big.Int) bool
	// GasTable returns the gas prices for this phase, which is based on
	// block number passed in.
	GasTable(*big.Int) params.GasTable
}

RuleSet is an interface that defines the current rule set during the execution of the EVM instructions (e.g. whether it's homestead)

type Stack

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

stack is an object for basic stack operations. Items popped to the stack are expected to be changed and modified. stack does not take care of adding newly initialised objects.

func (*Stack) Data

func (st *Stack) Data() []*big.Int

func (*Stack) Print

func (st *Stack) Print()

type Storage

type Storage map[common.Hash]common.Hash

func (Storage) Copy

func (self Storage) Copy() Storage

type StructLog

type StructLog struct {
	Pc      uint64
	Op      OpCode
	Gas     *big.Int
	GasCost *big.Int
	Memory  []byte
	Stack   []*big.Int
	Storage map[common.Hash]common.Hash
	Depth   int
	Err     error
}

StructLog is emitted to the Environment each cycle and lists information about the current internal state prior to the execution of the statement.

type StructLogger

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

StructLogger is an EVM state logger and implements Tracer.

StructLogger can capture state based on the given Log configuration and also keeps a track record of modified storage which is used in reporting snapshots of the contract their storage.

func NewStructLogger

func NewStructLogger(cfg *LogConfig) *StructLogger

NewLogger returns a new logger

func (*StructLogger) CaptureState

func (l *StructLogger) CaptureState(env Environment, pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, stack *Stack, contract *Contract, depth int, err error)

captureState logs a new structured log message and pushes it out to the environment

captureState also tracks SSTORE ops to track dirty values.

func (*StructLogger) StructLogs

func (l *StructLogger) StructLogs() []StructLog

StructLogs returns a list of captured log entries

type Tracer

type Tracer interface {
	CaptureState(env Environment, pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, stack *Stack, contract *Contract, depth int, err error)
}

Tracer is used to collect execution traces from an EVM transaction execution. CaptureState is called for each step of the VM with the current VM state. Note that reference types are actual VM data structures; make copies if you need to retain them beyond the current call.

type Type

type Type byte

Type is the VM type accepted by **NewVm**

const (
	StdVmTy Type = iota // Default standard VM
	JitVmTy             // LLVM JIT VM
	MaxVmTy
)

type VirtualMachine

type VirtualMachine interface {
	Run(*Contract, []byte) ([]byte, error)
}

VirtualMachine is an EVM interface

func NewJitVm

func NewJitVm(env Environment) VirtualMachine

type Vm

type Vm interface {
	// Run should execute the given contract with the input given in in
	// and return the contract execution return bytes or an error if it
	// failed.
	Run(c *Contract, in []byte) ([]byte, error)
}

Vm is the basic interface for an implementation of the EVM.

Directories

Path Synopsis
Package runtime provides a basic execution model for executing EVM code.
Package runtime provides a basic execution model for executing EVM code.

Jump to

Keyboard shortcuts

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