vm

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2015 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package vm implements the Ethereum Virtual Machine.

Index

Constants

View Source
const (
	StdVmTy Type = iota
	JitVmTy
	MaxVmTy

	LogTyPretty byte = 0x1
	LogTyDiff   byte = 0x2
)

Variables

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

	U256 = common.U256
	S256 = common.S256

	Zero = common.Big0
	One  = 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 (
	EnableJit   bool // Enables the JIT VM
	ForceJit    bool // Force the JIT, skip byte VM
	MaxProgSize int  // Max cache size for JIT Programs
)
View Source
var Debug bool

Global Debug flag indicating Debug VM (full logging)

View Source
var DepthError = fmt.Errorf("Max call depth exceeded (%d)", params.CallCreateDepth)
View Source
var OutOfGasError = errors.New("Out of gas")
View Source
var Precompiled = PrecompiledContracts()

Functions

func CompileProgram added in v1.1.0

func CompileProgram(program *Program) (err error)

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

func Disasm added in v0.9.39

func Disasm(code []byte) []string

func Disassemble added in v0.9.39

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

func GetProgramStatus added in v1.1.0

func GetProgramStatus(id common.Hash) progStatus

GenProgramStatus returns the status of the given program id

func IsStackErr added in v0.9.39

func IsStackErr(err error) bool

func PrecompiledContracts added in v0.9.39

func PrecompiledContracts() map[string]*PrecompiledAccount

XXX Could set directly. Testing requires resetting and setting of pre compiled contracts.

func RunProgram added in v1.1.0

func RunProgram(program *Program, env Environment, context *Context, input []byte) ([]byte, error)

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

func SetJITCacheSize added in v1.1.0

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 added in v0.9.39

func StdErrFormat(logs []StructLog)

func Transfer added in v0.9.39

func Transfer(from, to Account, amount *big.Int) error

generic transfer method

func UseGas added in v0.9.39

func UseGas(gas, amount *big.Int) bool

Types

type Account added in v0.9.39

type Account interface {
	SubBalance(amount *big.Int)
	AddBalance(amount *big.Int)
	Balance() *big.Int
	Address() common.Address
}

type Address added in v0.9.39

type Address interface {
	Call(in []byte) []byte
}

type Context

type Context struct {
	Code     []byte
	Input    []byte
	CodeAddr *common.Address

	Gas, UsedGas, Price *big.Int

	Args []byte
	// contains filtered or unexported fields
}

func NewContext added in v0.9.39

func NewContext(caller ContextRef, object ContextRef, value, gas, price *big.Int) *Context

Create a new context for the given data items.

func (*Context) Address added in v0.9.39

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

* Set / Get

func (*Context) GetByte added in v0.9.39

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

func (*Context) GetOp added in v0.9.39

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

func (*Context) Return added in v0.9.39

func (c *Context) Return(ret []byte) []byte

func (*Context) ReturnGas added in v0.9.39

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

Implement the caller interface

func (*Context) SetCallCode added in v0.9.39

func (self *Context) SetCallCode(addr *common.Address, code []byte)

func (*Context) SetCode added in v0.9.39

func (self *Context) SetCode(code []byte)

func (*Context) UseGas added in v0.9.39

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

* Gas functions

type ContextRef added in v0.9.39

type ContextRef interface {
	ReturnGas(*big.Int, *big.Int)
	Address() common.Address
	SetCode([]byte)
}

type Environment added in v0.9.39

type Environment interface {
	State() *state.StateDB

	Origin() common.Address
	BlockNumber() *big.Int
	GetHash(n uint64) common.Hash
	Coinbase() common.Address
	Time() *big.Int
	Difficulty() *big.Int
	GasLimit() *big.Int
	CanTransfer(from Account, balance *big.Int) bool
	Transfer(from, to Account, amount *big.Int) error
	AddLog(*state.Log)
	AddStructLog(StructLog)
	StructLogs() []StructLog

	VmType() Type

	Depth() int
	SetDepth(i int)

	Call(me ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
	CallCode(me ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
	Create(me ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, ContextRef)
}

Environment is is required by the virtual machine to get information from it's own isolated environment. For an example see `core.VMEnv`

type Memory

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

func NewMemory

func NewMemory() *Memory

func (*Memory) Data

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

func (*Memory) Get

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

func (*Memory) GetPtr

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

func (*Memory) Len

func (m *Memory) Len() int

func (*Memory) Print

func (m *Memory) Print()

func (*Memory) Resize

func (m *Memory) Resize(size uint64)

func (*Memory) Set

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

type OpCode

type OpCode byte
const (
	// 0x0 range - arithmetic ops
	STOP OpCode = iota
	ADD
	MUL
	SUB
	DIV
	SDIV
	MOD
	SMOD
	ADDMOD
	MULMOD
	EXP
	SIGNEXTEND
)

Op codes

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 (
	// 0xf0 range - closures
	CREATE OpCode = 0xf0 + iota
	CALL
	CALLCODE
	RETURN

	// 0x70 range - other
	SUICIDE = 0xff
)

func StringToOp

func StringToOp(str string) OpCode

func (OpCode) String

func (o OpCode) String() string

type PrecompiledAccount added in v0.9.39

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

func (PrecompiledAccount) Call added in v0.9.39

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

type Program added in v1.1.0

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 added in v1.1.0

func GetProgram(id common.Hash) *Program

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

func NewProgram added in v1.1.0

func NewProgram(code []byte) *Program

NewProgram returns a new JIT program

type StackError added in v0.9.39

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

func StackErr added in v0.9.39

func StackErr(req, has int) StackError

func (StackError) Error added in v0.9.39

func (self StackError) Error() string

type StructLog

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

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

type Type added in v0.9.39

type Type byte

type VirtualMachine added in v0.9.39

type VirtualMachine interface {
	Env() Environment
	Run(context *Context, data []byte) ([]byte, error)
}

func NewJitVm added in v0.9.39

func NewJitVm(env Environment) VirtualMachine

func NewVm added in v0.9.39

func NewVm(env Environment) VirtualMachine

type Vm added in v0.9.39

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

Vm implements VirtualMachine

func New added in v0.9.39

func New(env Environment) *Vm

New returns a new Virtual Machine

func (*Vm) Env added in v0.9.39

func (self *Vm) Env() Environment

Environment returns the current workable state of the VM

func (*Vm) Run added in v0.9.39

func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error)

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

func (*Vm) RunPrecompiled added in v0.9.39

func (self *Vm) RunPrecompiled(p *PrecompiledAccount, input []byte, context *Context) (ret []byte, err error)

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

Jump to

Keyboard shortcuts

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