vm

package
v2.6.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2019 License: GPL-3.0 Imports: 27 Imported by: 9

Documentation

Overview

Package vm implements the vite virtual machine

Index

Constants

View Source
const (
	STOP opCode = iota
	ADD
	MUL
	SUB
	DIV
	SDIV
	MOD
	SMOD
	ADDMOD
	MULMOD
	EXP
	SIGNEXTEND
)

0x0 range - arithmetic ops.

View Source
const (
	LT opCode = iota + 0x10
	GT
	SLT
	SGT
	EQ
	ISZERO
	AND
	OR
	XOR
	NOT
	BYTE
	SHL
	SHR
	SAR
)

0x10 range - comparison ops.

View Source
const (
	ADDRESS opCode = 0x30 + iota
	BALANCE
	ORIGIN
	CALLER
	CALLVALUE
	CALLDATALOAD
	CALLDATASIZE
	CALLDATACOPY
	CODESIZE
	CODECOPY
	GASPRICE
	EXTCODESIZE
	EXTCODECOPY
	RETURNDATASIZE
	RETURNDATACOPY
	EXTCODEHASH
)

0x30 range - closure state.

View Source
const (
	BLOCKHASH opCode = 0x40 + iota
	COINBASE
	TIMESTAMP
	HEIGHT
	DIFFICULTY
	GASLIMIT
	TOKENID
	ACCOUNTHEIGHT
	PREVHASH
	FROMHASH
	SEED
	RANDOM
)

0x40 range - block operations.

View Source
const (
	POP opCode = 0x50 + iota
	MLOAD
	MSTORE
	MSTORE8
	SLOAD
	SSTORE
	JUMP
	JUMPI
	PC
	MSIZE
	GAS
	JUMPDEST
)

0x50 range - 'storage' and execution.

View Source
const (
	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
)

0x60 range.

View Source
const (
	LOG0 opCode = 0xa0 + iota
	LOG1
	LOG2
	LOG3
	LOG4
)

0xa0 range - logging ops.

View Source
const (
	CREATE opCode = 0xf0 + iota
	CALL
	CALLCODE
	RETURN
	DELEGATECALL
	STATICCALL = 0xfa

	REVERT       = 0xfd
	SELFDESTRUCT = 0xff
)

0xf0 range - closures.

View Source
const (
	BLAKE2B opCode = 0x21
)

0x20 range - hash ops.

Variables

This section is empty.

Functions

func AddContractABI

func AddContractABI(addr types.Address, info abi.ABIContract)

AddContractABI method is used for debug

func ContainsCertainStatusCode

func ContainsCertainStatusCode(code []byte) (requireSnapshot, requireSnapshotWithSeed bool)

ContainsCertainStatusCode method checks whether the input code contains certain op codes that read cross chain data.

func ContainsStatusCode

func ContainsStatusCode(code []byte) bool

ContainsStatusCode checks whether code includes status reading opcode.

func GasRequiredForBlock

func GasRequiredForBlock(db vm_db.VmDb, block *ledger.AccountBlock, gasTable *util.QuotaTable, sbHeight uint64) (uint64, error)

GasRequiredForBlock calculates gas required for a user account block.

func GetContractABI

func GetContractABI(addr types.Address) (abi.ABIContract, bool)

GetContractABI method is used for debug

func InitVMConfig

func InitVMConfig(isTest bool, isTestParam bool, isQuotaTestParam bool, isDebug bool, dataDir string)

InitVMConfig init global status of vm. This method is supposed be called when the node started. Parameters:

isTest: test mode, quota and balance is not checked under test mode.
isTestParam: use test params for built-in contracts.
isQuotaTestParam: use test params for quota calculation.
isDebug: print debug log.
datadir: print debug log under this directory.

func IsTest added in v1.2.0

func IsTest() bool

IsTest returns whether node is currently running under test mode or not.

Types

type NodeConfig

type NodeConfig struct {
	IsDebug bool

	ContractABIMap map[types.Address]abi.ABIContract
	// contains filtered or unexported fields
}

NodeConfig holds the global status of vm.

type VM

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

VM holds the runtime information of vite vm and provides the necessary tools to run a transfer transaction of a call contract transaction. It also provides an offchain getter method to read contract storage without sending a transaction. Node: The VM instance should never be reused and is not thread safe.

func NewVM

func NewVM(cr util.ConsensusReader) *VM

NewVM is a constructor of VM. This method is called before running an execution.

func (*VM) AppendBlock

func (context *VM) AppendBlock(block *ledger.AccountBlock)

AppendBlock method append a send block to send block list of a contract receive block

func (*VM) Cancel

func (vm *VM) Cancel()

Cancel method stops the running contract receive

func (*VM) ConsensusReader

func (vm *VM) ConsensusReader() util.ConsensusReader

ConsensusReader is a getter method.

func (*VM) GlobalStatus

func (vm *VM) GlobalStatus() util.GlobalStatus

GlobalStatus is a getter method.

func (*VM) OffChainReader added in v1.3.0

func (vm *VM) OffChainReader(db vm_db.VmDb, code []byte, data []byte) (result []byte, err error)

OffChainReader read contract storage without tx

func (*VM) RunV2

func (vm *VM) RunV2(db vm_db.VmDb, block *ledger.AccountBlock, sendBlock *ledger.AccountBlock, status util.GlobalStatus) (vmAccountBlock *vm_db.VmAccountBlock, isRetry bool, err error)

RunV2 method executes an account block, checks parameters, performs balance change and storage change, updates specific fields of the account block. This method is used to both executes an account block and verify an account block. Parameters:

db: current status, including current account address,
    prev account block and latest snapshot block.
block: account block to be executed.
sendBlock: when executing a receive block,
    sendBlock is the referring send block.
status: world status, only presents when executing
    a contract receive block and contract confirm time
    is greater than zero.

Returns:

vmAccountBlock: execute result, including db and block.
isRetry: whether this block should be executed again later.
err: specific error occurred during execution.

Notes:

  1. Input block and sendBlock will not be changed during execution. The return block in vmAccountBlock is a copy of input block.
  2. Return value of vmAccountBlock should be inserted into chain regardless of isRetry return value and err return value.
  3. Block should be executed again later if isRetry is true, regardless of vmAccountBlock return value and err return value.
  4. This method panics if chain forked during execution, retry later if panics.

Directories

Path Synopsis
abi
dex

Jump to

Keyboard shortcuts

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