vm

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: May 23, 2019 License: MIT Imports: 12 Imported by: 3

Documentation

Index

Constants

View Source
const (
	PushInt = iota
	PushBool
	PushChar
	PushStr
	Push
	Dup
	Roll
	Swap
	Pop
	Add
	Sub
	Mul
	Div
	Mod
	Exp
	Neg
	Eq
	NotEq
	Lt
	Gt
	LtEq
	GtEq
	ShiftL
	ShiftR
	BitwiseAnd
	BitwiseOr
	BitwiseXor
	BitwiseNot
	NoOp
	Jmp
	JmpTrue
	JmpFalse
	Call
	CallTrue
	CallExt
	Ret
	Size
	StoreLoc
	StoreSt
	LoadLoc
	LoadSt
	Address // Address of account
	Issuer  // Owner of smart contract account
	Balance // Balance of account
	Caller
	CallVal  // Amount of bazo coins transacted in transaction
	CallData // Parameters and function signature hash
	NewMap
	MapHasKey
	MapGetVal
	MapSetVal
	MapRemove
	NewArr
	ArrAppend
	ArrInsert
	ArrRemove
	ArrAt
	ArrLen
	NewStr
	StoreFld
	LoadFld
	SHA3
	CheckSig
	ErrHalt
	Halt
)

Supported Bazo OpCodes.

View Source
const (
	BYTES = iota + 1
	BYTE
	LABEL
	ADDR
)

Supported OpCode argument types

View Source
const UINT16_MAX uint16 = 65535

Variables

View Source
var OpCodes = []OpCode{
	{PushInt, "pushint", 1, []int{BYTES}, 1, 1},
	{PushBool, "pushbool", 1, []int{BYTE}, 1, 1},
	{PushChar, "pushchar", 1, []int{BYTE}, 1, 1},
	{PushStr, "pushstr", 1, []int{BYTES}, 1, 1},
	{Push, "push", 1, []int{BYTES}, 1, 1},
	{Dup, "dup", 0, nil, 1, 2},
	{Roll, "roll", 1, []int{BYTE}, 1, 2},
	{Swap, "swap", 0, nil, 1, 2},
	{Pop, "pop", 0, nil, 1, 1},
	{Add, "add", 0, nil, 1, 2},
	{Sub, "sub", 0, nil, 1, 2},
	{Mul, "mult", 0, nil, 1, 2},
	{Div, "div", 0, nil, 1, 2},
	{Mod, "mod", 0, nil, 1, 2},
	{Exp, "exp", 0, nil, 1, 2},
	{Neg, "neg", 0, nil, 1, 2},
	{Eq, "eq", 0, nil, 1, 2},
	{NotEq, "neq", 0, nil, 1, 2},
	{Lt, "lt", 0, nil, 1, 2},
	{Gt, "gt", 0, nil, 1, 2},
	{LtEq, "lte", 0, nil, 1, 2},
	{GtEq, "gte", 0, nil, 1, 2},
	{ShiftL, "shiftl", 0, nil, 1, 2},
	{ShiftR, "shiftr", 0, nil, 1, 2},
	{BitwiseAnd, "bitwiseand", 0, nil, 1, 2},
	{BitwiseOr, "bitwiseor", 0, nil, 1, 2},
	{BitwiseXor, "bitwisexor", 0, nil, 1, 2},
	{BitwiseNot, "bitwisenot", 0, nil, 1, 2},
	{NoOp, "nop", 0, nil, 1, 1},
	{Jmp, "jmp", 1, []int{LABEL}, 1, 1},
	{JmpTrue, "jmptrue", 1, []int{LABEL}, 1, 1},
	{JmpFalse, "jmpfalse", 1, []int{LABEL}, 1, 1},
	{Call, "call", 2, []int{LABEL, BYTE}, 1, 1},
	{CallTrue, "callif", 2, []int{LABEL, BYTE}, 1, 1},
	{CallExt, "callext", 3, []int{ADDR, BYTE, BYTE, BYTE, BYTE, BYTE}, 1000, 2},
	{Ret, "ret", 0, nil, 1, 1},
	{Size, "size", 0, nil, 1, 1},
	{StoreLoc, "storeloc", 1, []int{BYTE}, 1, 2},
	{StoreSt, "storest", 1, []int{BYTE}, 1000, 2},
	{LoadLoc, "loadloc", 1, []int{BYTE}, 1, 2},
	{LoadSt, "loadst", 1, []int{BYTE}, 10, 2},
	{Address, "address", 0, nil, 1, 1},
	{Issuer, "issuer", 0, nil, 1, 1},
	{Balance, "balance", 0, nil, 1, 1},
	{Caller, "caller", 0, nil, 1, 1},
	{CallVal, "callval", 0, nil, 1, 1},
	{CallData, "calldata", 0, nil, 1, 1},
	{NewMap, "newmap", 0, nil, 1, 2},
	{MapHasKey, "maphaskey", 0, nil, 1, 2},
	{MapGetVal, "mapgetval", 0, nil, 1, 2},
	{MapSetVal, "mapsetval", 0, nil, 1, 2},
	{MapRemove, "mapremove", 0, nil, 1, 2},
	{NewArr, "newarr", 0, nil, 1, 2},
	{ArrAppend, "arrappend", 0, nil, 1, 2},
	{ArrInsert, "arrinsert", 0, nil, 1, 2},
	{ArrRemove, "arrremove", 0, nil, 1, 2},
	{ArrAt, "arrat", 0, nil, 1, 2},
	{ArrLen, "arrlen", 0, nil, 1, 2},
	{NewStr, "newstr", 1, []int{BYTE}, 1, 2},
	{StoreFld, "storefld", 1, []int{BYTE}, 1, 2},
	{LoadFld, "loadfld", 1, []int{BYTE}, 1, 2},
	{SHA3, "sha3", 0, nil, 1, 2},
	{CheckSig, "checksig", 0, nil, 1, 2},
	{ErrHalt, "errhalt", 0, nil, 0, 1},
	{Halt, "halt", 0, nil, 0, 1},
}

OpCodes contains all OpCode definitions

Functions

func BigIntToByteArray added in v1.4.1

func BigIntToByteArray(value big.Int) []byte

func BigIntToPushableBytes

func BigIntToPushableBytes(element big.Int) []byte

func BigIntToString

func BigIntToString(element big.Int) string

func BigIntToUInt added in v1.4.1

func BigIntToUInt(value big.Int) (uint, error)

func BigIntToUInt16 added in v1.4.1

func BigIntToUInt16(value big.Int) (uint16, error)

func BoolToByteArray

func BoolToByteArray(value bool) []byte

func ByteArrayToBool

func ByteArrayToBool(ba []byte) bool

func ByteArrayToInt

func ByteArrayToInt(element []byte) int

func ByteArrayToUI16

func ByteArrayToUI16(element []byte) (uint16, error)

func SignedBigIntConversion

func SignedBigIntConversion(ba []byte, err error) (big.Int, error)

func SignedByteArrayConversion

func SignedByteArrayConversion(bi big.Int) []byte

func StrToBigInt

func StrToBigInt(element string) big.Int

func UInt16ToBigInt added in v1.4.1

func UInt16ToBigInt(value uint16) big.Int

func UInt16ToByteArray

func UInt16ToByteArray(element uint16) []byte

func UInt64ToByteArray

func UInt64ToByteArray(element uint64) []byte

func UnsignedBigIntConversion

func UnsignedBigIntConversion(ba []byte, err error) (big.Int, error)

Types

type Array

type Array []byte

func ArrayFromByteArray

func ArrayFromByteArray(arr []byte) (Array, error)

func NewArray

func NewArray() Array

func (*Array) Append

func (a *Array) Append(ba []byte) error

func (*Array) At

func (a *Array) At(index uint16) ([]byte, error)

func (*Array) DecrementSize

func (a *Array) DecrementSize() error

func (*Array) GetSize added in v1.4.1

func (a *Array) GetSize() (uint16, error)

func (*Array) IncrementSize

func (a *Array) IncrementSize() error

func (*Array) Insert

func (a *Array) Insert(index uint16, element []byte) error

Insert sets an element at a certain index of the array

func (*Array) Remove

func (a *Array) Remove(index uint16) error

Remove removes the element with the given index from the array

type CallStack

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

func NewCallStack

func NewCallStack() *CallStack

func (CallStack) GetLength

func (cs CallStack) GetLength() int

func (*CallStack) Peek

func (cs *CallStack) Peek() (frame *Frame, err error)

func (*CallStack) Pop

func (cs *CallStack) Pop() (frame *Frame, err error)

func (*CallStack) Push

func (cs *CallStack) Push(element *Frame)

type Context

type Context interface {
	GetContract() []byte
	GetContractVariable(index int) ([]byte, error)
	SetContractVariable(index int, value []byte) error
	GetAddress() [64]byte
	GetIssuer() [32]byte
	GetBalance() uint64
	GetSender() [32]byte
	GetAmount() uint64
	GetTransactionData() []byte
	GetFee() uint64
	GetSig1() [64]byte
}

Context is the VM execution context which is composed with data coming from the transaction and the account. Context interface declares functions required to start the execution of the contract.

type Frame

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

type Map

type Map []byte

func CreateMap added in v1.4.1

func CreateMap() Map

func MapFromByteArray

func MapFromByteArray(m []byte) (Map, error)

func (*Map) Append

func (m *Map) Append(key []byte, value []byte) error

func (*Map) DecrementSize

func (m *Map) DecrementSize() error

func (*Map) GetVal

func (m *Map) GetVal(key []byte) ([]byte, error)

func (*Map) IncrementSize

func (m *Map) IncrementSize()

func (*Map) MapContainsKey

func (m *Map) MapContainsKey(key []byte) (bool, error)

func (*Map) Remove

func (m *Map) Remove(key []byte) error

func (*Map) SetVal

func (m *Map) SetVal(key []byte, value []byte) error

type MockContext

type MockContext struct {
	protocol.Context
}

func NewMockContext

func NewMockContext(byteCode []byte) *MockContext

func (*MockContext) SetContract

func (mc *MockContext) SetContract(contract []byte)

type OpCode

type OpCode struct {
	Name     string
	Nargs    int
	ArgTypes []int
	// contains filtered or unexported fields
}

OpCode contains the code, name, number of arguments, argument types, gas price and gas factor of the opcode

type Stack

type Stack struct {
	Stack [][]byte
	// contains filtered or unexported fields
}

func NewStack

func NewStack() *Stack

func (Stack) GetLength

func (s Stack) GetLength() int

func (*Stack) PeekBytes

func (s *Stack) PeekBytes() (element []byte, err error)

func (*Stack) Pop

func (s *Stack) Pop() (element []byte, err error)

func (*Stack) PopIndexAt

func (s *Stack) PopIndexAt(index int) ([]byte, error)

func (*Stack) Push

func (s *Stack) Push(element []byte) error

type Struct added in v1.4.1

type Struct Array

Struct type represents the composite data type declaration that defines a group of variables.

type VM

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

VM is a stack-based virtual machine and executes the contract code sequentially.

func NewTestVM

func NewTestVM(byteCode []byte) VM

NewTestVM creates a new Bazo virtual machine with the test contract code.

func NewVM

func NewVM(context Context) VM

NewVM creates a new Bazo virtual machine with the context received from Bazo miner.

func (*VM) Exec

func (vm *VM) Exec(trace bool) bool

Exec executes the contract code and stores the result on evaluation stack.

func (*VM) GetErrorMsg

func (vm *VM) GetErrorMsg() string

GetErrorMsg peeks bytes from evaluation stack and returns the error message.

func (*VM) PeekEvalStack added in v1.4.1

func (vm *VM) PeekEvalStack() [][]byte

PeekEvalStack returns a copy of the complete evaluation stack

func (*VM) PeekResult

func (vm *VM) PeekResult() (element []byte, err error)

PeekResult returns the element on top of the stack

func (*VM) PopBytes

func (vm *VM) PopBytes(opCode OpCode) (elements []byte, err error)

PopBytes pops bytes from the evaluation stack.

func (*VM) PopSignedBigInt

func (vm *VM) PopSignedBigInt(opCode OpCode) (bigInt big.Int, err error)

PopSignedBigInt pops bytes from evaluation stack and convert it to a big integer with sign.

func (*VM) PopUnsignedBigInt

func (vm *VM) PopUnsignedBigInt(opCode OpCode) (bigInt big.Int, err error)

PopUnsignedBigInt pops bytes from evaluation stack and convert it to an unsigned big integer.

Jump to

Keyboard shortcuts

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