vm

package
v0.0.0-...-f2ae4c7 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2020 License: Apache-2.0 Imports: 21 Imported by: 0

README

CVM

CertiK virtual machine (CVM) handles the underlying gas calculation and corresponding error handling for contract deployments and calls.

Behavior

These are some differences in behavior between CVM and EVM.

  1. GASPRICE operation
    1. In EVM, tx.gasprice (triggers GASPRICE opcode) returns the gasprice for the transaction.
    2. Instead, CVM returns 0. It has to do with the opcode GASPRICE_DEPRECATED. Relevant handling can be found in vm.go.
    3. They consume the same amount of gas.
    4. This might change in the future.
  2. DIFFICULTY operation
    1. In EVM, DIFFICULTY returns the current mining difficulty of the chain.
    2. Instead, CVM returns 0. This is due to the consensus model difference.
    3. Will not be changed in the future (might need update when Ethereum goes POS).
  3. COINBASE operation
    1. In EVM, COINBASE pushes the miner address to the stack.
    2. Instaed, CVM pushes 0 to the stack, as there is no concept of mining.
    3. Will not change in the future.

Gas Cost

These are some differences in gas cost between CVM and EVM.

  1. SSTORE opcode gas calculation
    1. CVM does not implement EIP-1283 or EIP-2200.
    2. Instead, it adds a simple NOOP gas (200) case to the original Petersburg gas calculation logic, for the case where the new value is equal to the original value.
  2. SELFDESTRUCT opcode gas calculation
    1. as CVM doesn't have the identical access structure as EVM, EIP-158 is ignored.
    2. However, EIP 150 is taken account, so if selfdestruct() is called to an unexisting address, it will consume CreateBySelfDestruct amount of gas.

Minor difference

These are some differences that do not affect behavior or gas cost.

  1. BLOCKHEIGHT operation
    1. BLOCKHEIGHT is named NUMBER in EVM.
    2. They do the same thing, consume the same gas.
  2. There are some missing opcodes in Burrow compared to Geth
    1. COINBASE
    2. NUMBER (Block height)
    3. CHAINID

Documentation

Index

Constants

View Source
const (
	// Basic gas units
	GasBase    uint64 = 2
	GasVeryLow uint64 = 3
	GasLow     uint64 = 5
	GasMid     uint64 = 8
	GasHigh    uint64 = 10
	GasExtStep uint64 = 20 // homestead extcode cost

	// Constantinople
	GasExtcodeSize          uint64 = 700
	GasExtcodeCopy          uint64 = 700
	GasExtcodeHash          uint64 = 700
	GasBalance              uint64 = 700
	GasSLoad                uint64 = 800
	GasCalls                uint64 = 700
	GasSelfdestruct         uint64 = 5000
	GasExpByte              uint64 = 50
	GasCreateBySelfdestruct uint64 = 25000

	CallValueTransferGas uint64 = 9000  // Paid for CALL when the value transfer is non-zero.
	CallNewAccountGas    uint64 = 25000 // Paid for CALL when the destination address didn't exist prior.
	QuadCoeffDiv         uint64 = 512   // Divisor for the quadratic particle of the memory cost equation.
	LogDataGas           uint64 = 8     // Per byte in a LOG* operation's data.

	Sha3Gas     uint64 = 30 // Once per SHA3 operation.
	Sha3WordGas uint64 = 6  // Once per word of the SHA3 operation's data.

	SstoreSetGas    uint64 = 20000 // Once per SLOAD operation.
	SstoreResetGas  uint64 = 5000  // Once per SSTORE operation if the zeroness changes from zero.
	SstoreClearGas  uint64 = 5000  // Once per SSTORE operation if the zeroness doesn't change.
	SstoreRefundGas uint64 = 15000 // Once per SSTORE operation if the zeroness changes to zero.

	NetSstoreNoopGas uint64 = 200 // Once per SSTORE operation if the value doesn't change.

	JumpdestGas           uint64 = 1     // Once per JUMPDEST operation.
	CallGas               uint64 = 40    // Once per CALL operation & message call transaction.
	ExpGas                uint64 = 10    // Once per EXP instruction
	LogGas                uint64 = 375   // Per LOG* operation.
	CopyGas               uint64 = 3     //
	LogTopicGas           uint64 = 375   // Multiplied by the * of the LOG*, per LOG transaction. e.g. LOG0 incurs 0 * c_txLogTopicGas, LOG4 incurs 4 * c_txLogTopicGas.
	CreateGas             uint64 = 32000 // Once per CREATE operation & contract-creation transaction.
	SelfdestructRefundGas uint64 = 24000 // Refunded following a suicide operation.
	MemoryGas             uint64 = 3     // Times the address of the (highest referenced byte in memory + 1). NOTE: referencing happens on read, write and in instructions such as RETURN and CALL.

	ExtcodeCopyBase = 20
)

These are based on Ethereum yellow paper.

Variables

This section is empty.

Functions

func Get

func Get(st *Stack, n int) *big.Int

Get returns the n-th element from the stack

func GetWord256

func GetWord256(st *Stack, n int) Word256

GetWord256 is similar with Get, but returns with Word256 type (default type in the Stack).

func Min

func Min(x, y uint64) uint64

Min returns the smaller of two uint64 integers.

func SafeAdd

func SafeAdd(x, y uint64) (uint64, bool)

SafeAdd add x and y and sets the overflow flag. of==true means an overflow has occurred.

func SafeMul

func SafeMul(x, y uint64) (uint64, bool)

SafeMul multiplies x by y and sets the overflow flag. of==true means an overflow has occurred.

func SafeSub

func SafeSub(x, y uint64) (uint64, bool)

SafeSub Subtracts y from x, and sets the overflow flag. of==true means an overflow has occurred.

Types

type CVM

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

func NewCVM

func NewCVM(options CVMOptions) *CVM

func (*CVM) Contract

func (vm *CVM) Contract(code []byte) *CVMContract

Contract returns a CVMContract with the provided CVM and code.

func (*CVM) Dispatch

func (vm *CVM) Dispatch(acc *acm.Account) engine.Callable

Dispatch dispatches an account to be used externally from another engine.

func (*CVM) Execute

func (vm *CVM) Execute(st acmstate.ReaderWriter, blockchain engine.Blockchain, eventSink exec.EventSink,
	params engine.CallParams, code []byte) ([]byte, error)

Initiate an EVM call against the provided state pushing events to eventSink. code should contain the EVM bytecode, input the CallData (readable by CALLDATALOAD), value the amount of native token to transfer with the call an quantity metering the number of computational steps available to the execution according to the gas schedule.

func (*CVM) GetRefund

func (vm *CVM) GetRefund() uint64

GetRefund returns the refund counter of the vm.

func (*CVM) SetExternals

func (vm *CVM) SetExternals(externals engine.Dispatcher)

SetExternals sets external callables to be added to the engine for mutual contract calling.

func (*CVM) SetLogger

func (vm *CVM) SetLogger(logger *logging.Logger)

SetLogger sets the logger for the CVM instance.

func (*CVM) SetNonce

func (vm *CVM) SetNonce(nonce []byte)

SetNonce setss a new nonce and resets the sequence number. Nonces should only be used once! A global counter or sufficient randomness will work.

type CVMContract

type CVMContract struct {
	*CVM
	// contains filtered or unexported fields
}

CVMContract is the basic VM and code pair needed to make contract calls in CVM.

func (*CVMContract) Call

func (c *CVMContract) Call(state engine.State, params engine.CallParams) ([]byte, error)

Call executes the CVM contract call with the given state of the blockchain and parameters.

type CVMOptions

type CVMOptions struct {
	MemoryProvider           func(errors.Sink) gasMemory
	Natives                  *native.Natives
	Nonce                    []byte
	DebugOpcodes             bool
	DumpTokens               bool
	CallStackMaxDepth        uint64
	DataStackInitialCapacity uint64
	DataStackMaxDepth        uint64
	Logger                   *logging.Logger
}

CVMOptions are parameters that are generally stable across a burrow configuration. Defaults will be used for any zero values.

Jump to

Keyboard shortcuts

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