vm

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2020 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package vm implements the Ethereum Virtual Machine.

The vm package implements one EVM, a byte code 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.

Index

Constants

View Source
const (
	GasQuickStep   uint64 = 2
	GasFastestStep uint64 = 3
	GasFastStep    uint64 = 5
	GasMidStep     uint64 = 8
	GasSlowStep    uint64 = 10
	GasExtStep     uint64 = 20

	GasReturn       uint64 = 0
	GasStop         uint64 = 0
	GasContractByte uint64 = 200
)

Gas costs

Variables

View Source
var PrecompiledContractsByzantium = map[common2.Address]PrecompiledContract{
	common2.BytesToAddress([]byte{1}): &ecrecover{},
	common2.BytesToAddress([]byte{2}): &sha256hash{},
	common2.BytesToAddress([]byte{3}): &ripemd160hash{},
	common2.BytesToAddress([]byte{4}): &dataCopy{},
	common2.BytesToAddress([]byte{5}): &bigModExp{},
	common2.BytesToAddress([]byte{6}): &bn256Add{},
	common2.BytesToAddress([]byte{7}): &bn256ScalarMul{},
	common2.BytesToAddress([]byte{8}): &bn256Pairing{},
}

PrecompiledContractsByzantium contains the default set of pre-compiled Ethereum contracts used in the Byzantium release.

Functions

func RunPrecompiledContract

func RunPrecompiledContract(p PrecompiledContract, input []byte, contract *vmtypes.Contract) (ret []byte, err error)

RunPrecompiledContract runs and evaluates the output of a precompiled contract.

func WriteLogs

func WriteLogs(writer io.Writer, logs []*vmtypes.Log)

WriteLogs writes vm Logs in a readable format to the given writer

func WriteTrace

func WriteTrace(writer io.Writer, logs []StructLog)

WriteTrace writes a formatted trace to the given writer

Types

type EVMInterpreter

type EVMInterpreter struct {
	Cfg *InterpreterConfig

	Caller vmtypes.Caller
	// contains filtered or unexported fields
}

EVMInterpreter represents an OVM interpreter

func NewEVMInterpreter

func NewEVMInterpreter(vmContext *vmtypes.Context, txContext *ovm.TxContext, cfg *InterpreterConfig) *EVMInterpreter

NewEVMInterpreter returns a new instance of the Interpreter.

func (*EVMInterpreter) CanRun

func (in *EVMInterpreter) CanRun(code []byte) bool

CanRun tells if the contract, passed as an argument, can be run by the current interpreter.

func (*EVMInterpreter) Run

func (in *EVMInterpreter) Run(contract *vmtypes.Contract, input []byte, readOnly bool) (ret []byte, err error)

Run loops and evaluates the contract's code with the given input data and returns the return byte-slice and an error if one occurred.

It's important to note that any errors returned by the interpreter should be considered a revert-and-consume-all-gas operation except for errExecutionReverted which means revert-and-keep-gas-left.

func (*EVMInterpreter) SetCaller

func (in *EVMInterpreter) SetCaller(caller vmtypes.Caller)

type InterpreterConfig

type InterpreterConfig struct {
	// Debug enabled debugging Interpreter options
	Debug bool
	// Tracer is the op code logger
	Tracer Tracer
	// Enable recording of SHA3/keccak preimages
	EnablePreimageRecording bool
}

InterpreterConfig are the configuration options for the Interpreter

type LogConfig

type LogConfig struct {
	DisableMemory  bool // disable memory capture
	DisableStack   bool // disable stack capture
	DisableStorage bool // disable storage capture
	Debug          bool // print output during capture end
	Limit          int  // maximum length of output, but zero means unlimited
}

LogConfig are the configuration options for structured logger the OVM

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

NewMemory returns a new memory model.

func (*Memory) Data

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

Data returns the backing slice

func (*Memory) Get

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

Get returns offset + size as a new slice

func (*Memory) GetPtr

func (m *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()

Print dumps the content of the memory.

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

func (*Memory) Set32

func (m *Memory) Set32(offset uint64, val *big.Int)

Set32 sets the 32 bytes starting at offset to the value of val, left-padded with zeroes to 32 bytes.

type PrecompiledContract

type PrecompiledContract interface {
	RequiredGas(input []byte) uint64  // RequiredPrice calculates the contract gas use
	Run(input []byte) ([]byte, error) // Run runs the precompiled contract
}

PrecompiledContract is the basic interface for native Go contracts. The implementation requires a deterministic gas count based on the input size of the Run method of the contract.

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) Back

func (st *Stack) Back(n int) *big.Int

Back returns the n'th item in stack

func (*Stack) Data

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

Data returns the underlying big.Int array.

func (*Stack) Print

func (st *Stack) Print()

Print dumps the content of the stack

type Storage

type Storage map[types.Hash]types.Hash

Storage represents a contract's storage.

func (Storage) Copy

func (s Storage) Copy() Storage

Copy duplicates the current storage.

type StructLog

type StructLog struct {
	Pc            uint64                    `json:"pc"`
	Op            instruction.OpCode        `json:"op"`
	Gas           uint64                    `json:"gas"`
	GasCost       uint64                    `json:"gasCost"`
	Memory        []byte                    `json:"memory"`
	MemorySize    int                       `json:"memSize"`
	Stack         []*big.Int                `json:"stack"`
	Storage       map[types.Hash]types.Hash `json:"-"`
	Depth         int                       `json:"depth"`
	RefundCounter uint64                    `json:"refund"`
	Err           error                     `json:"-"`
}

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

func (*StructLog) ErrorString

func (s *StructLog) ErrorString() string

ErrorString formats the log's error as a string.

func (*StructLog) OpName

func (s *StructLog) OpName() string

OpName formats the operand name in a human-readable format.

type StructLogger

type StructLogger struct {
	Logs []StructLog
	// contains filtered or unexported fields
}

StructLogger is an OVM 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

NewStructLogger returns a new logger

func (*StructLogger) CaptureEnd

func (l *StructLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error

CaptureEnd is called after the call finishes to finalize the tracing.

func (*StructLogger) CaptureFault

func (l *StructLogger) CaptureFault(ctx *vmtypes.Context, pc uint64, op instruction.OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *vmtypes.Contract, depth int, err error) error

CaptureFault implements the Tracer interface to trace an execution fault while running an opcode.

func (*StructLogger) CaptureStart

func (l *StructLogger) CaptureStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) error

CaptureStart implements the Tracer interface to initialize the tracing operation.

func (*StructLogger) CaptureState

func (l *StructLogger) CaptureState(ctx *vmtypes.Context, pc uint64, op instruction.OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *vmtypes.Contract, depth int, err error) 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) Error

func (l *StructLogger) Error() error

Error returns the VM error captured by the trace.

func (*StructLogger) Output

func (l *StructLogger) Output() []byte

Output returns the VM return value captured by the trace.

func (*StructLogger) StructLogs

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

StructLogs returns the captured log entries.

func (*StructLogger) Write

func (l *StructLogger) Write(writer io.Writer)

type Tracer

type Tracer interface {
	CaptureStart(from common.Address, to common.Address, call bool, input []byte, gas uint64, value *big.Int) error
	CaptureState(ctx *vmtypes.Context, pc uint64, op instruction.OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *vmtypes.Contract, depth int, err error) error
	CaptureFault(ctx *vmtypes.Context, pc uint64, op instruction.OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *vmtypes.Contract, depth int, err error) error
	CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error
	Write(writer io.Writer)
}

Tracer is used to collect execution traces from an OVM 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.

Jump to

Keyboard shortcuts

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