ovm

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2020 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FAST_FAIL = false
View Source
var MAX_LAYER = 1024
View Source
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.

View Source
var TestChainConfig = &ChainConfig{0}

Functions

func CanTransfer

func CanTransfer(db vmtypes.StateDB, addr common.Address, amount *big.Int) bool

CanTransfer checks whether there are enough funds in the address' account to make a transfer. This does not take the necessary gas in to account to make the transfer valid.

func NewOVMContext

func NewOVMContext(chainContext ChainContext, coinBase *common.Address, stateDB vmtypes.StateDB) *vmtypes.Context

NewOVMContext creates a new context for use in the OVM.

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 Transfer

func Transfer(db vmtypes.StateDB, sender, recipient common.Address, amount *big.Int)

Transfer subtracts amount from sender and adds amount to recipient using the given Db

Types

type ChainConfig

type ChainConfig struct {
	ChainID uint32 `json:"chainId"` // chainId identifies the current chain and is used for replay protection
}

ChainConfig is the core config which determines the blockchain settings.

ChainConfig is stored in the database on a per block basis. This means that any network, identified by its genesis block, can have its own set of configuration options.

func (*ChainConfig) String

func (c *ChainConfig) String() string

String implements the fmt.Stringer interface.

type ChainContext

type ChainContext interface {
}

ChainContext supports retrieving headers and consensus parameters from the current blockchain to be used during transaction processing.

type ConfigCompatError

type ConfigCompatError struct {
	What string
	// block numbers of the stored and new configurations
	StoredConfig, NewConfig *big.Int
	// the block number to which the local chain must be rewound to correct the error
	RewindTo uint64
}

ConfigCompatError is raised if the locally-stored blockchain is initialised with a ChainConfig that would alter the past.

func (*ConfigCompatError) Error

func (err *ConfigCompatError) Error() string

type DefaultChainContext

type DefaultChainContext struct {
}

type GasPool

type GasPool uint64

GasPool tracks the amount of gas available during execution of the transactions in a block. The zero value is a pool with zero gas available.

func (*GasPool) AddGas

func (gp *GasPool) AddGas(amount uint64) *GasPool

AddGas makes gas available for execution.

func (*GasPool) Gas

func (gp *GasPool) Gas() uint64

Gas returns the amount of gas remaining in the pool.

func (*GasPool) String

func (gp *GasPool) String() string

func (*GasPool) SubGas

func (gp *GasPool) SubGas(amount uint64) error

SubGas deducts the given amount from the pool if enough gas is available and returns an error otherwise.

type Interpreter

type Interpreter interface {
	// Run loops and evaluates the vmtypes.Contract's code with the given input data and returns
	// the return byte-slice and an error if one occurred.
	Run(Contract *types.Contract, input []byte, static bool) ([]byte, error)
	// CanRun tells if the vmtypes.Contract, passed as an argument, can be
	// run by the current interpreter. This is meant so that the
	// caller can do something like:
	//
	// “`golang
	// for _, interpreter := range interpreters {
	//   if interpreter.CanRun(vmtypes.Contract.code) {
	//     interpreter.Run(vmtypes.Contract.code, input)
	//   }
	// }
	// “`
	CanRun([]byte) bool

	// SetCaller will be called when OVM is initializing this intepreter.
	// OVM will tell which caller interpreter should use if there is a inter-contract call
	SetCaller(caller types.Caller)
}

Interpreter is used to run Ethereum based vmtypes.Contracts and will utilise the passed environment to query external sources for state information. The Interpreter will run the byte code VM based on the passed configuration.

type LayerStateDB

type LayerStateDB struct {
	Layers []vmtypes.StateDBDebug
	// contains filtered or unexported fields
}

LayerStateDB is the cascading storage for contracts. It consists of multiple layers, each of which represents the result of a contract. e.g. -.-#-+-#----- <- Latest changes after contract --+-------+-- <- ... ----.----.--- <- first contract ------------- <- StateDB (on disk) When accessing, watch from the top to the bottom. Same mechanism as Docker image layers. Add a layer each time a new contract is run

func NewLayerDB

func NewLayerDB(baseLayer vmtypes.StateDBDebug) *LayerStateDB

func (*LayerStateDB) AddBalance

func (l *LayerStateDB) AddBalance(addr common.Address, value *math.BigInt)

func (*LayerStateDB) AddLog

func (l *LayerStateDB) AddLog(log *vmtypes.Log)

func (*LayerStateDB) AddPreimage

func (l *LayerStateDB) AddPreimage(hash common.Hash, code []byte)

func (*LayerStateDB) AddRefund

func (l *LayerStateDB) AddRefund(value uint64)

func (*LayerStateDB) CreateAccount

func (l *LayerStateDB) CreateAccount(addr common.Address)

func (*LayerStateDB) CurrentLayer

func (l *LayerStateDB) CurrentLayer() int

func (*LayerStateDB) Empty

func (l *LayerStateDB) Empty(addr common.Address) bool

func (*LayerStateDB) Exist

func (l *LayerStateDB) Exist(addr common.Address) bool

func (*LayerStateDB) ForEachStorage

func (l *LayerStateDB) ForEachStorage(addr common.Address, f func(common.Hash, common.Hash) bool)

func (*LayerStateDB) GetBalance

func (l *LayerStateDB) GetBalance(addr common.Address) *math.BigInt

func (*LayerStateDB) GetCode

func (l *LayerStateDB) GetCode(addr common.Address) []byte

func (*LayerStateDB) GetCodeHash

func (l *LayerStateDB) GetCodeHash(addr common.Address) common.Hash

func (*LayerStateDB) GetCodeSize

func (l *LayerStateDB) GetCodeSize(addr common.Address) int

func (*LayerStateDB) GetCommittedState

func (l *LayerStateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash

func (*LayerStateDB) GetNonce

func (l *LayerStateDB) GetNonce(addr common.Address) uint64

func (*LayerStateDB) GetRefund

func (l *LayerStateDB) GetRefund() uint64

func (*LayerStateDB) GetState

func (l *LayerStateDB) GetState(addr common.Address, key common.Hash) common.Hash

func (*LayerStateDB) GetStateObject

func (l *LayerStateDB) GetStateObject(addr common.Address) *vmtypes.StateObject

func (*LayerStateDB) HasSuicided

func (l *LayerStateDB) HasSuicided(addr common.Address) bool

func (*LayerStateDB) MergeChanges

func (l *LayerStateDB) MergeChanges()

MergeChanges merges all layers that are above the bottom layer to one layer

func (*LayerStateDB) NewLayer

func (l *LayerStateDB) NewLayer() (index int, err error)

func (*LayerStateDB) PopLayer

func (l *LayerStateDB) PopLayer(index int) (err error)

func (*LayerStateDB) RevertToSnapshot

func (l *LayerStateDB) RevertToSnapshot(i int)

func (*LayerStateDB) SetCode

func (l *LayerStateDB) SetCode(addr common.Address, code []byte)

func (*LayerStateDB) SetNonce

func (l *LayerStateDB) SetNonce(addr common.Address, nonce uint64)

func (*LayerStateDB) SetState

func (l *LayerStateDB) SetState(addr common.Address, key common.Hash, value common.Hash)

func (*LayerStateDB) SetStateObject

func (l *LayerStateDB) SetStateObject(addr common.Address, stateObject *vmtypes.StateObject)

func (*LayerStateDB) Snapshot

func (l *LayerStateDB) Snapshot() int

func (*LayerStateDB) String

func (l *LayerStateDB) String() string

func (*LayerStateDB) SubBalance

func (l *LayerStateDB) SubBalance(addr common.Address, value *math.BigInt)

func (*LayerStateDB) SubRefund

func (l *LayerStateDB) SubRefund(value uint64)

func (*LayerStateDB) Suicide

func (l *LayerStateDB) Suicide(addr common.Address) bool

type MemoryStateDB

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

func NewMemoryStateDB

func NewMemoryStateDB() *MemoryStateDB

func (*MemoryStateDB) AddBalance

func (m *MemoryStateDB) AddBalance(addr common.Address, v *math.BigInt)

func (*MemoryStateDB) AddLog

func (m *MemoryStateDB) AddLog(*vmtypes.Log)

func (*MemoryStateDB) AddPreimage

func (m *MemoryStateDB) AddPreimage(hash common.Hash, preImage []byte)

func (*MemoryStateDB) AddRefund

func (m *MemoryStateDB) AddRefund(v uint64)

func (*MemoryStateDB) CreateAccount

func (m *MemoryStateDB) CreateAccount(addr common.Address)

func (*MemoryStateDB) Empty

func (m *MemoryStateDB) Empty(addr common.Address) bool

func (*MemoryStateDB) Exist

func (m *MemoryStateDB) Exist(addr common.Address) bool

func (*MemoryStateDB) ForEachStorage

func (m *MemoryStateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool)

func (*MemoryStateDB) GetBalance

func (m *MemoryStateDB) GetBalance(addr common.Address) *math.BigInt

func (*MemoryStateDB) GetCode

func (m *MemoryStateDB) GetCode(addr common.Address) []byte

func (*MemoryStateDB) GetCodeHash

func (m *MemoryStateDB) GetCodeHash(addr common.Address) common.Hash

func (*MemoryStateDB) GetCodeSize

func (m *MemoryStateDB) GetCodeSize(addr common.Address) int

func (*MemoryStateDB) GetCommittedState

func (m *MemoryStateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash

func (*MemoryStateDB) GetNonce

func (m *MemoryStateDB) GetNonce(addr common.Address) uint64

func (*MemoryStateDB) GetRefund

func (m *MemoryStateDB) GetRefund() uint64

func (*MemoryStateDB) GetState

func (m *MemoryStateDB) GetState(addr common.Address, key common.Hash) common.Hash

func (*MemoryStateDB) GetStateObject

func (m *MemoryStateDB) GetStateObject(addr common.Address) *vmtypes.StateObject

func (*MemoryStateDB) HasSuicided

func (m *MemoryStateDB) HasSuicided(addr common.Address) bool

func (*MemoryStateDB) RevertToSnapshot

func (m *MemoryStateDB) RevertToSnapshot(int)

func (*MemoryStateDB) SetCode

func (m *MemoryStateDB) SetCode(addr common.Address, code []byte)

func (*MemoryStateDB) SetNonce

func (m *MemoryStateDB) SetNonce(addr common.Address, nonce uint64)

func (*MemoryStateDB) SetState

func (m *MemoryStateDB) SetState(addr common.Address, key common.Hash, value common.Hash)

func (*MemoryStateDB) SetStateObject

func (m *MemoryStateDB) SetStateObject(addr common.Address, stateObject *vmtypes.StateObject)

func (*MemoryStateDB) Snapshot

func (m *MemoryStateDB) Snapshot() int

func (*MemoryStateDB) String

func (m *MemoryStateDB) String() string

func (*MemoryStateDB) SubBalance

func (m *MemoryStateDB) SubBalance(addr common.Address, v *math.BigInt)

func (*MemoryStateDB) SubRefund

func (m *MemoryStateDB) SubRefund(v uint64)

func (*MemoryStateDB) Suicide

func (m *MemoryStateDB) Suicide(addr common.Address) bool

type OVM

type OVM struct {
	// Context provides auxiliary blockchain related information
	VMContext *vmtypes.Context

	// chainConfig contains information about the current chain
	// ChainConfig *params.ChainConfig
	// chain rules contains the chain rules for the current epoch
	// chainRules params.Rules
	// virtual machine configuration options used to initialise the
	// evm.
	OVMConfigs *OVMConfig
	// global (to this context) ethereum virtual machine
	// used throughout the execution of the tx.
	Interpreters []Interpreter
	Interpreter  Interpreter
}

OVM is the Ethereum Virtual Machine base object and provides the necessary tools to run a contract on the given state with the provided context. It should be noted that any error generated through any of the calls should be considered a revert-state-and-consume-all-gas operation, no checks on specific errors should ever be performed. The interpreter makes sure that any errors generated are to be considered faulty Code.

The OVM should never be reused and is not thread safe.

func NewOVM

func NewOVM(ctx *vmtypes.Context, supportInterpreters []Interpreter, ovmConfig *OVMConfig) *OVM

NewOVM returns a new OVM. The returned OVM is not thread safe and should only ever be used *once*.

func (*OVM) Call

func (ovm *OVM) Call(caller vmtypes.ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int, txCall bool) (ret []byte, leftOverGas uint64, err error)

Call executes the contract associated with the addr with the given input as parameters. It also handles any necessary value transfer required and takes the necessary steps to create accounts and reverses the state in case of an execution error or failed value transfer.

func (*OVM) CallCode

func (ovm *OVM) CallCode(caller vmtypes.ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int) (ret []byte, leftOverGas uint64, err error)

CallCode executes the contract associated with the addr with the given input as parameters. It also handles any necessary value transfer required and takes the necessary steps to create accounts and reverses the state in case of an execution error or failed value transfer.

CallCode differs from Call in the sense that it executes the given address' Code with the caller as context.

func (*OVM) Cancel

func (ovm *OVM) Cancel()

Cancel cancels any running OVM operation. This may be called concurrently and it's safe to be called multiple times.

func (*OVM) Create

func (ovm *OVM) Create(caller vmtypes.ContractRef, code []byte, gas uint64, value *big.Int, txCall bool) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error)

Create creates a new contract using Code as deployment Code.

func (*OVM) Create2

func (ovm *OVM) Create2(caller vmtypes.ContractRef, code []byte, gas uint64, endowment *big.Int, salt *big.Int, txCall bool) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error)

Create2 creates a new contract using Code as deployment Code.

The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:] instead of the usual sender-and-Nonce-hash as the address where the contract is initialized at.

func (*OVM) DelegateCall

func (ovm *OVM) DelegateCall(caller vmtypes.ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error)

DelegateCall executes the contract associated with the addr with the given input as parameters. It reverses the state in case of an execution error.

DelegateCall differs from CallCode in the sense that it executes the given address' Code with the caller as context and the caller is set to the caller of the caller.

func (*OVM) StaticCall

func (ovm *OVM) StaticCall(caller vmtypes.ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error)

StaticCall executes the contract associated with the addr with the given input as parameters while disallowing any modifications to the state during the call. Opcodes that attempt to perform such modifications will result in exceptions instead of performing the modifications.

type OVMConfig

type OVMConfig struct {
	// NoRecursion disabled Interpreter call, callcode,
	// delegate call and create.
	NoRecursion bool
}

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 TxContext

type TxContext struct {
	From  common.Address
	To    common.Address
	Value *math.BigInt
	Data  []byte

	// Temporarily keep using gas as resource billing
	GasLimit   uint64
	GasPrice   *math.BigInt
	Coinbase   common.Address // Provides information for COINBASE
	SequenceID uint64         // Provides information for SequenceID

}

TxContext represents all information that evm needs to know about the tx current processing.

type VM

type VM interface {
	// Cancel cancels any running VM operation. This may be called concurrently and
	// it's safe to be called multiple times.
	Cancel()

	// Interpreter returns the current interpreter
	Interpreter() Interpreter

	// Call executes the vmtypes.Contract associated with the addr with the given input as
	// parameters. It also handles any necessary Value transfer required and takes
	// the necessary steps to create accounts and reverses the state in case of an
	// execution error or failed Value transfer.
	Call(caller vmtypes.ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int) (ret []byte, leftOverGas uint64, err error)

	// CallCode executes the vmtypes.Contract associated with the addr with the given input
	// as parameters. It also handles any necessary Value transfer required and takes
	// the necessary steps to create accounts and reverses the state in case of an
	// execution error or failed Value transfer.
	//
	// CallCode differs from Call in the sense that it executes the given address'
	// Code with the caller as context.
	CallCode(caller vmtypes.ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int) (ret []byte, leftOverGas uint64, err error)

	// DelegateCall executes the vmtypes.Contract associated with the addr with the given input
	// as parameters. It reverses the state in case of an execution error.
	//
	// DelegateCall differs from CallCode in the sense that it executes the given address'
	// Code with the caller as context and the caller is set to the caller of the caller.
	DelegateCall(caller vmtypes.ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error)

	// StaticCall executes the vmtypes.Contract associated with the addr with the given input
	// as parameters while disallowing any modifications to the state during the call.
	// Opcodes that attempt to perform such modifications will result in exceptions
	// instead of performing the modifications.
	StaticCall(caller vmtypes.ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error)

	// Create creates a new vmtypes.Contract using Code as deployment Code.
	Create(caller vmtypes.ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, ContractAddr common.Address, leftOverGas uint64, err error)

	// Create2 creates a new vmtypes.Contract using Code as deployment Code.
	//
	// The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]
	// instead of the usual sender-and-Nonce-hash as the address where the vmtypes.Contract is initialized at.
	Create2(caller vmtypes.ContractRef, code []byte, gas uint64, endowment *big.Int, salt *big.Int) (ret []byte, ContractAddr common.Address, leftOverGas uint64, err error)
}

Jump to

Keyboard shortcuts

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