vm

package
v0.97.1 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2021 License: MIT Imports: 26 Imported by: 6

Documentation

Index

Constants

View Source
const (
	// MaxInvocationStackSize is the maximum size of an invocation stack.
	MaxInvocationStackSize = 1024

	// MaxTryNestingDepth is the maximum level of TRY nesting allowed,
	// that is you can't have more exception handling contexts than this.
	MaxTryNestingDepth = 16

	// MaxStackSize is the maximum number of items allowed to be
	// on all stacks at once.
	MaxStackSize = 2 * 1024
)
View Source
const MaxMultisigKeys = 1024

MaxMultisigKeys is the maximum number of used keys for correct multisig contract.

Variables

This section is empty.

Functions

func CheckMultisigPar added in v0.90.0

func CheckMultisigPar(v *VM, curve elliptic.Curve, h []byte, pkeys [][]byte, sigs [][]byte) bool

CheckMultisigPar checks if sigs contains sufficient valid signatures.

func GetInteropID

func GetInteropID(parameter []byte) uint32

GetInteropID converts instruction parameter to an interop ID.

func IsMultiSigContract

func IsMultiSigContract(script []byte) bool

IsMultiSigContract checks whether the passed script is a multi-signature contract.

func IsScriptCorrect added in v0.93.0

func IsScriptCorrect(script []byte, methods bitfield.Field) error

IsScriptCorrect checks script for errors and mask provided for correctness wrt instruction boundaries. Normally it returns nil, but can return some specific error if there is any.

func IsSignatureContract

func IsSignatureContract(script []byte) bool

IsSignatureContract checks whether the passed script is a signature check contract.

func IsStandardContract

func IsStandardContract(script []byte) bool

IsStandardContract checks whether the passed script is a signature or multi-signature contract.

func ParseMultiSigContract

func ParseMultiSigContract(script []byte) (int, [][]byte, bool)

ParseMultiSigContract returns number of signatures and list of public keys from the verification script of the contract.

func ParseSignatureContract added in v0.94.0

func ParseSignatureContract(script []byte) ([]byte, bool)

ParseSignatureContract parses simple signature contract and returns public key.

Types

type CheckReturnState added in v0.92.0

type CheckReturnState byte

CheckReturnState represents possible states of stack after opcode.RET was processed.

const (
	// NoCheck performs no return values check.
	NoCheck CheckReturnState = 0
	// EnsureIsEmpty checks that stack is empty and panics if not.
	EnsureIsEmpty CheckReturnState = 1
	// EnsureNotEmpty checks that stack contains not more than 1 element and panics if not.
	// It pushes stackitem.Null on stack in case if there's no elements.
	EnsureNotEmpty CheckReturnState = 2
)

type Context

type Context struct {

	// ParamCount specifies number of parameters.
	ParamCount int
	// RetCount specifies number of return values.
	RetCount int
	// NEF represents NEF file for the current contract.
	NEF *nef.File
	// contains filtered or unexported fields
}

Context represents the current execution context of the VM.

func NewContext

func NewContext(b []byte) *Context

NewContext returns a new Context object.

func NewContextWithParams added in v0.93.0

func NewContextWithParams(b []byte, pcount int, rvcount int, pos int) *Context

NewContextWithParams creates new Context objects using script, parameter count, return value count and initial position in script.

func (*Context) Convert added in v0.90.0

func (c *Context) Convert(_ stackitem.Type) (stackitem.Item, error)

Convert implements stackitem.Item interface.

func (*Context) Copy

func (c *Context) Copy() *Context

Copy returns an new exact copy of c.

func (*Context) CurrInstr

func (c *Context) CurrInstr() (int, opcode.Opcode)

CurrInstr returns the current instruction and opcode.

func (*Context) Dup

func (c *Context) Dup() stackitem.Item

Dup implements stackitem.Item interface.

func (*Context) Equals

func (c *Context) Equals(s stackitem.Item) bool

Equals implements stackitem.Item interface.

func (*Context) Estack added in v0.92.0

func (c *Context) Estack() *Stack

Estack returns the evaluation stack of c.

func (*Context) GetCallFlags added in v0.90.0

func (c *Context) GetCallFlags() callflag.CallFlag

GetCallFlags returns calling flags context was created with.

func (*Context) IP

func (c *Context) IP() int

IP returns current instruction offset in the context script.

func (*Context) IsDeployed added in v0.92.0

func (c *Context) IsDeployed() bool

IsDeployed returns whether this context contains deployed contract.

func (*Context) LenInstr

func (c *Context) LenInstr() int

LenInstr returns the number of instructions loaded.

func (*Context) Next

func (c *Context) Next() (opcode.Opcode, []byte, error)

Next returns the next instruction to execute with its parameter if any. After its invocation the instruction pointer points to the instruction being returned.

func (*Context) NextIP

func (c *Context) NextIP() int

NextIP returns next instruction pointer.

func (*Context) NextInstr added in v0.78.3

func (c *Context) NextInstr() (int, opcode.Opcode)

NextInstr returns the next instruction and opcode.

func (*Context) Program

func (c *Context) Program() []byte

Program returns the loaded program.

func (*Context) ScriptHash

func (c *Context) ScriptHash() util.Uint160

ScriptHash returns a hash of the script in the current context.

func (*Context) String

func (c *Context) String() string

func (*Context) TryBool added in v0.92.0

func (c *Context) TryBool() (bool, error)

TryBool implements stackitem.Item interface.

func (*Context) TryBytes

func (c *Context) TryBytes() ([]byte, error)

TryBytes implements stackitem.Item interface.

func (*Context) TryInteger added in v0.90.0

func (c *Context) TryInteger() (*big.Int, error)

TryInteger implements stackitem.Item interface.

func (*Context) Type added in v0.90.0

func (c *Context) Type() stackitem.Type

Type implements stackitem.Item interface.

func (*Context) Value

func (c *Context) Value() interface{}

Value implements stackitem.Item interface.

type Element

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

Element represents an element in the double linked list (the stack), which will hold the underlying stackitem.Item.

func NewElement

func NewElement(v interface{}) *Element

NewElement returns a new Element object, with its underlying value inferred to the corresponding type.

func (*Element) Array

func (e *Element) Array() []stackitem.Item

Array attempts to get the underlying value of the element as an array of other items. Will panic if the item type is different which will be caught by the VM.

func (*Element) BigInt

func (e *Element) BigInt() *big.Int

BigInt attempts to get the underlying value of the element as a big integer. Will panic if the assertion failed which will be caught by the VM.

func (*Element) Bool

func (e *Element) Bool() bool

Bool converts an underlying value of the element to a boolean if it's possible to do so, it will panic otherwise.

func (*Element) Bytes

func (e *Element) Bytes() []byte

Bytes attempts to get the underlying value of the element as a byte array. Will panic if the assertion failed which will be caught by the VM.

func (*Element) BytesOrNil added in v0.92.0

func (e *Element) BytesOrNil() []byte

BytesOrNil attempts to get the underlying value of the element as a byte array or nil. Will panic if the assertion failed which will be caught by the VM.

func (*Element) Interop

func (e *Element) Interop() *stackitem.Interop

Interop attempts to get the underlying value of the element as an interop item.

func (*Element) Item

func (e *Element) Item() stackitem.Item

Item returns Item contained in the element.

func (*Element) Next

func (e *Element) Next() *Element

Next returns the next element in the stack.

func (*Element) Prev

func (e *Element) Prev() *Element

Prev returns the previous element in the stack.

func (*Element) String added in v0.91.0

func (e *Element) String() string

String attempts to get string from the element value. It is assumed to be use in interops and panics if string is not a valid UTF-8 byte sequence.

func (*Element) Value

func (e *Element) Value() interface{}

Value returns value of the Item contained in the element.

type Slot added in v0.90.0

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

Slot is a fixed-size slice of stack items.

func (*Slot) Clear added in v0.91.0

func (s *Slot) Clear()

Clear removes all slot variables from reference counter.

func (*Slot) Get added in v0.90.0

func (s *Slot) Get(i int) stackitem.Item

Get returns item contained in i-th slot.

func (*Slot) Set added in v0.90.0

func (s *Slot) Set(i int, item stackitem.Item)

Set sets i-th storage slot.

func (*Slot) Size added in v0.90.0

func (s *Slot) Size() int

Size returns slot size.

type Stack

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

Stack represents a Stack backed by a double linked list.

func NewStack

func NewStack(n string) *Stack

NewStack returns a new stack name by the given name.

func (*Stack) Back

func (s *Stack) Back() *Element

Back returns the element at the end of the stack. Nil if the stack is empty.

func (*Stack) Clear

func (s *Stack) Clear()

Clear clears all elements on the stack and set its length to 0.

func (*Stack) Dup

func (s *Stack) Dup(n int) *Element

Dup duplicates and returns the element at position n. Dup is used for copying elements on to the top of its own stack.

s.Push(s.Peek(0)) // will result in unexpected behaviour.
s.Push(s.Dup(0)) // is the correct approach.

func (*Stack) InsertAt

func (s *Stack) InsertAt(e *Element, n int) *Element

InsertAt inserts the given item (n) deep on the stack. Be very careful using it and _always_ check both e and n before invocation as it will silently do wrong things otherwise.

func (*Stack) Iter

func (s *Stack) Iter(f func(*Element))

Iter iterates over all the elements int the stack, starting from the top of the stack.

s.Iter(func(elem *Element) {
	// do something with the element.
})

func (*Stack) IterBack

func (s *Stack) IterBack(f func(*Element))

IterBack iterates over all the elements of the stack, starting from the bottom of the stack.

s.IterBack(func(elem *Element) {
	// do something with the element.
})

func (*Stack) Len

func (s *Stack) Len() int

Len returns the number of elements that are on the stack.

func (*Stack) MarshalJSON

func (s *Stack) MarshalJSON() ([]byte, error)

MarshalJSON implements JSON marshalling interface.

func (*Stack) Peek

func (s *Stack) Peek(n int) *Element

Peek returns the element (n) far in the stack beginning from the top of the stack.

n = 0 => will return the element on top of the stack.

func (*Stack) Pop

func (s *Stack) Pop() *Element

Pop removes and returns the element on top of the stack.

func (*Stack) PopSigElements added in v0.90.0

func (s *Stack) PopSigElements() ([][]byte, error)

PopSigElements pops keys or signatures from the stack as needed for CHECKMULTISIG.

func (*Stack) Push

func (s *Stack) Push(e *Element)

Push pushes the given element on the stack.

func (*Stack) PushVal

func (s *Stack) PushVal(v interface{})

PushVal pushes the given value on the stack. It will infer the underlying Item to its corresponding type.

func (*Stack) Remove

func (s *Stack) Remove(e *Element) *Element

Remove removes and returns the given element from the stack.

func (*Stack) RemoveAt

func (s *Stack) RemoveAt(n int) *Element

RemoveAt removes the element (n) deep on the stack beginning from the top of the stack.

func (*Stack) ReverseTop added in v0.90.0

func (s *Stack) ReverseTop(n int) error

ReverseTop reverses top n items of the stack.

func (*Stack) Roll

func (s *Stack) Roll(n int) error

Roll brings an item with the given index to the top of the stack, moving all the other elements down accordingly. It does all of that without popping and pushing elements.

func (*Stack) Swap

func (s *Stack) Swap(n1, n2 int) error

Swap swaps two elements on the stack without popping and pushing them.

func (*Stack) ToArray added in v0.91.0

func (s *Stack) ToArray() []stackitem.Item

ToArray converts stack to an array of stackitems with top item being the last.

func (*Stack) Top

func (s *Stack) Top() *Element

Top returns the element on top of the stack. Nil if the stack is empty.

type State

type State uint8

State of the VM.

const (
	// NoneState represents NONE VM state.
	NoneState State = 0
	// HaltState represents HALT VM state.
	HaltState State = 1 << iota
	// FaultState represents FAULT VM state.
	FaultState
	// BreakState represents BREAK VM state.
	BreakState
)

Available States.

func StateFromString

func StateFromString(s string) (st State, err error)

StateFromString converts string into the VM State.

func (State) HasFlag

func (s State) HasFlag(f State) bool

HasFlag checks for State flag presence.

func (State) MarshalJSON

func (s State) MarshalJSON() (data []byte, err error)

MarshalJSON implements the json.Marshaler interface.

func (State) String

func (s State) String() string

String implements the stringer interface.

func (*State) UnmarshalJSON

func (s *State) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements the json.Marshaler interface.

type StateMessage

type StateMessage string

StateMessage is a vm state message which could be used as additional info for example by cli.

type SyscallHandler added in v0.91.0

type SyscallHandler = func(*VM, uint32) error

SyscallHandler is a type for syscall handler.

type VM

type VM struct {
	GasLimit int64

	// SyscallHandler handles SYSCALL opcode.
	SyscallHandler func(v *VM, id uint32) error

	// LoadToken handles CALLT opcode.
	LoadToken func(id int32) error

	// Invocations is a script invocation counter.
	Invocations map[util.Uint160]int
	// contains filtered or unexported fields
}

VM represents the virtual machine.

func New

func New() *VM

New returns a new VM object ready to load AVM bytecode scripts.

func NewWithTrigger added in v0.90.0

func NewWithTrigger(t trigger.Type) *VM

NewWithTrigger returns a new VM for executions triggered by t.

func (*VM) AddBreakPoint

func (v *VM) AddBreakPoint(n int)

AddBreakPoint adds a breakpoint to the current context.

func (*VM) AddBreakPointRel

func (v *VM) AddBreakPointRel(n int)

AddBreakPointRel adds a breakpoint relative to the current instruction pointer.

func (*VM) AddGas added in v0.90.0

func (v *VM) AddGas(gas int64) bool

AddGas consumes specified amount of gas. It returns true iff gas limit wasn't exceeded.

func (*VM) AtBreakpoint

func (v *VM) AtBreakpoint() bool

AtBreakpoint returns whether VM is at breakpoint.

func (*VM) Call added in v0.91.0

func (v *VM) Call(ctx *Context, offset int)

Call calls method by offset. It is similar to Jump but also pushes new context to the invocation stack and increments invocation counter for the corresponding context script hash.

func (*VM) Context

func (v *VM) Context() *Context

Context returns the current executed context. Nil if there is no context, which implies no program is loaded.

func (*VM) Estack

func (v *VM) Estack() *Stack

Estack returns the evaluation stack so interop hooks can utilize this.

func (*VM) GasConsumed

func (v *VM) GasConsumed() int64

GasConsumed returns the amount of GAS consumed during execution.

func (*VM) GetCallingScriptHash added in v0.90.0

func (v *VM) GetCallingScriptHash() util.Uint160

GetCallingScriptHash implements ScriptHashGetter interface.

func (*VM) GetCurrentScriptHash added in v0.90.0

func (v *VM) GetCurrentScriptHash() util.Uint160

GetCurrentScriptHash implements ScriptHashGetter interface.

func (*VM) GetEntryScriptHash added in v0.90.0

func (v *VM) GetEntryScriptHash() util.Uint160

GetEntryScriptHash implements ScriptHashGetter interface.

func (*VM) HasFailed

func (v *VM) HasFailed() bool

HasFailed returns whether VM is in the failed state now. Usually used to check status after Run.

func (*VM) HasHalted

func (v *VM) HasHalted() bool

HasHalted returns whether VM is in Halt state.

func (*VM) HasStopped

func (v *VM) HasStopped() bool

HasStopped returns whether VM is in Halt or Failed state.

func (*VM) Istack

func (v *VM) Istack() *Stack

Istack returns the invocation stack so interop hooks can utilize this.

func (*VM) Jump added in v0.91.0

func (v *VM) Jump(ctx *Context, offset int)

Jump performs jump to the offset.

func (*VM) Load

func (v *VM) Load(prog []byte)

Load initializes the VM with the program given.

func (*VM) LoadArgs

func (v *VM) LoadArgs(method []byte, args []stackitem.Item)

LoadArgs loads in the arguments used in the Mian entry point.

func (*VM) LoadFileWithFlags added in v0.95.1

func (v *VM) LoadFileWithFlags(path string, f callflag.CallFlag) error

LoadFileWithFlags loads a program in NEF format from the given path, ready to execute it.

func (*VM) LoadScript

func (v *VM) LoadScript(b []byte)

LoadScript loads a script from the internal script table. It will immediately push a new context created from this script to the invocation stack and starts executing it.

func (*VM) LoadScriptWithCallingHash added in v0.92.0

func (v *VM) LoadScriptWithCallingHash(caller util.Uint160, b []byte, hash util.Uint160,
	f callflag.CallFlag, hasReturn bool, paramCount uint16)

LoadScriptWithCallingHash is similar to LoadScriptWithHash but sets calling hash explicitly. It should be used for calling from native contracts.

func (*VM) LoadScriptWithFlags added in v0.90.0

func (v *VM) LoadScriptWithFlags(b []byte, f callflag.CallFlag)

LoadScriptWithFlags loads script and sets call flag to f.

func (*VM) LoadScriptWithHash added in v0.90.0

func (v *VM) LoadScriptWithHash(b []byte, hash util.Uint160, f callflag.CallFlag)

LoadScriptWithHash if similar to the LoadScriptWithFlags method, but it also loads given script hash directly into the Context to avoid its recalculations and to make is possible to override it for deployed contracts with special hashes (the function assumes that it is used for deployed contracts setting context's parameters accordingly). It's up to user of this function to make sure the script and hash match each other.

func (*VM) LoadWithFlags added in v0.95.1

func (v *VM) LoadWithFlags(prog []byte, f callflag.CallFlag)

LoadWithFlags initializes the VM with the program and flags given.

func (*VM) PopResult

func (v *VM) PopResult() interface{}

PopResult is used to pop the first item of the evaluation stack. This allows us to test compiler and vm in a bi-directional way.

func (*VM) PrintOps

func (v *VM) PrintOps(out io.Writer)

PrintOps prints the opcodes of the current loaded program to stdout.

func (*VM) PushContextScriptHash added in v0.90.0

func (v *VM) PushContextScriptHash(n int) error

PushContextScriptHash pushes to evaluation stack the script hash of the invocation stack element number n.

func (*VM) Ready

func (v *VM) Ready() bool

Ready returns true if the VM ready to execute the loaded program. Will return false if no program is loaded.

func (*VM) Run

func (v *VM) Run() error

Run starts the execution of the loaded program.

func (*VM) SetPriceGetter

func (v *VM) SetPriceGetter(f func(opcode.Opcode, []byte) int64)

SetPriceGetter registers the given PriceGetterFunc in v. f accepts vm's Context, current instruction and instruction parameter.

func (*VM) Stack

func (v *VM) Stack(n string) string

Stack returns json formatted representation of the given stack.

func (*VM) State

func (v *VM) State() State

State returns the state for the VM.

func (*VM) Step

func (v *VM) Step() error

Step 1 instruction in the program.

func (*VM) StepInto

func (v *VM) StepInto() error

StepInto behaves the same as “step over” in case if the line does not contain a function. Otherwise the debugger will enter the called function and continue line-by-line debugging there.

func (*VM) StepOut

func (v *VM) StepOut() error

StepOut takes the debugger to the line where the current function was called.

func (*VM) StepOver

func (v *VM) StepOver() error

StepOver takes the debugger to the line that will step over a given line. If the line contains a function the function will be executed and the result returned without debugging each line.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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