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
- Variables
- func RunPrecompiledContract(p PrecompiledContract, input []byte, contract *vmtypes.Contract) (ret []byte, err error)
- func WriteLogs(writer io.Writer, logs []*vmtypes.Log)
- func WriteTrace(writer io.Writer, logs []StructLog)
- type EVMInterpreter
- type InterpreterConfig
- type LogConfig
- type Memory
- func (m *Memory) Data() []byte
- func (m *Memory) Get(offset, size int64) (cpy []byte)
- func (m *Memory) GetPtr(offset, size int64) []byte
- func (m *Memory) Len() int
- func (m *Memory) Print()
- func (m *Memory) Resize(size uint64)
- func (m *Memory) Set(offset, size uint64, value []byte)
- func (m *Memory) Set32(offset uint64, val *big.Int)
- type PrecompiledContract
- type Stack
- type Storage
- type StructLog
- type StructLogger
- func (l *StructLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error
- func (l *StructLogger) CaptureFault(ctx *vmtypes.Context, pc uint64, op instruction.OpCode, gas, cost uint64, ...) error
- func (l *StructLogger) CaptureStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, ...) error
- func (l *StructLogger) CaptureState(ctx *vmtypes.Context, pc uint64, op instruction.OpCode, gas, cost uint64, ...) error
- func (l *StructLogger) Error() error
- func (l *StructLogger) Output() []byte
- func (l *StructLogger) StructLogs() []StructLog
- func (l *StructLogger) Write(writer io.Writer)
- type Tracer
Constants ¶
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 ¶
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 WriteTrace ¶
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.
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.
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 ¶
ErrorString formats the log's error as a string.
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 ¶
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.