cmd/evm, core/vm, test: refactored VM and core
* Moved `vm.Transfer` to `core` package and changed execution to call `env.Transfer` instead of `core.Transfer` directly. * core/vm: byte code VM moved to jump table instead of switch * Moved `vm.Transfer` to `core` package and changed execution to call `env.Transfer` instead of `core.Transfer` directly. * Byte code VM now shares the same code as the JITVM * Renamed Context to Contract * Changed initialiser of state transition & unexported methods * Removed the Execution object and refactor `Call`, `CallCode` & `Create` in to their own functions instead of being methods. * Removed the hard dep on the state for the VM. The VM now depends on a Database interface returned by the environment. In the process the core now depends less on the statedb by usage of the env * Moved `Log` from package `core/state` to package `core/vm`.
This commit is contained in:
@ -17,39 +17,86 @@
|
||||
package vm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math/big"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/state"
|
||||
)
|
||||
|
||||
// Environment is is required by the virtual machine to get information from
|
||||
// it's own isolated environment. For an example see `core.VMEnv`
|
||||
type Environment interface {
|
||||
State() *state.StateDB
|
||||
// it's own isolated environment.
|
||||
|
||||
// Environment is an EVM requirement and helper which allows access to outside
|
||||
// information such like states.
|
||||
type Environment interface {
|
||||
// The state database
|
||||
Db() Database
|
||||
// Creates a restorable snapshot
|
||||
MakeSnapshot() Database
|
||||
// Set database to previous snapshot
|
||||
SetSnapshot(Database)
|
||||
// Address of the original invoker (first occurance of the VM invoker)
|
||||
Origin() common.Address
|
||||
// The block number this VM is invoken on
|
||||
BlockNumber() *big.Int
|
||||
// The n'th hash ago from this block number
|
||||
GetHash(n uint64) common.Hash
|
||||
// The handler's address
|
||||
Coinbase() common.Address
|
||||
// The current time (block time)
|
||||
Time() *big.Int
|
||||
// Difficulty set on the current block
|
||||
Difficulty() *big.Int
|
||||
// The gas limit of the block
|
||||
GasLimit() *big.Int
|
||||
CanTransfer(from Account, balance *big.Int) bool
|
||||
// Determines whether it's possible to transact
|
||||
CanTransfer(from common.Address, balance *big.Int) bool
|
||||
// Transfer from to to with amount set
|
||||
Transfer(from, to Account, amount *big.Int) error
|
||||
AddLog(*state.Log)
|
||||
// Adds a LOG to the state
|
||||
AddLog(*Log)
|
||||
// Adds a structured log to the env
|
||||
AddStructLog(StructLog)
|
||||
// Returns all coalesced structured logs
|
||||
StructLogs() []StructLog
|
||||
|
||||
// Type of the VM
|
||||
VmType() Type
|
||||
|
||||
// Current calling depth
|
||||
Depth() int
|
||||
SetDepth(i int)
|
||||
|
||||
Call(me ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
|
||||
CallCode(me ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
|
||||
Create(me ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, ContextRef)
|
||||
// Call another contract
|
||||
Call(me ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
|
||||
// Take another's contract code and execute within our own context
|
||||
CallCode(me ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
|
||||
// Create a new contract
|
||||
Create(me ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error)
|
||||
}
|
||||
|
||||
// Database is a EVM database for full state querying
|
||||
type Database interface {
|
||||
GetAccount(common.Address) Account
|
||||
CreateAccount(common.Address) Account
|
||||
|
||||
AddBalance(common.Address, *big.Int)
|
||||
GetBalance(common.Address) *big.Int
|
||||
|
||||
GetNonce(common.Address) uint64
|
||||
SetNonce(common.Address, uint64)
|
||||
|
||||
GetCode(common.Address) []byte
|
||||
SetCode(common.Address, []byte)
|
||||
|
||||
AddRefund(*big.Int)
|
||||
GetRefund() *big.Int
|
||||
|
||||
GetState(common.Address, common.Hash) common.Hash
|
||||
SetState(common.Address, common.Hash, common.Hash)
|
||||
|
||||
Delete(common.Address) bool
|
||||
Exist(common.Address) bool
|
||||
IsDeleted(common.Address) bool
|
||||
}
|
||||
|
||||
// StructLog is emited to the Environment each cycle and lists information about the curent internal state
|
||||
@ -68,18 +115,10 @@ type StructLog struct {
|
||||
type Account interface {
|
||||
SubBalance(amount *big.Int)
|
||||
AddBalance(amount *big.Int)
|
||||
SetBalance(*big.Int)
|
||||
SetNonce(uint64)
|
||||
Balance() *big.Int
|
||||
Address() common.Address
|
||||
}
|
||||
|
||||
// generic transfer method
|
||||
func Transfer(from, to Account, amount *big.Int) error {
|
||||
if from.Balance().Cmp(amount) < 0 {
|
||||
return errors.New("Insufficient balance in account")
|
||||
}
|
||||
|
||||
from.SubBalance(amount)
|
||||
to.AddBalance(amount)
|
||||
|
||||
return nil
|
||||
ReturnGas(*big.Int, *big.Int)
|
||||
SetCode([]byte)
|
||||
}
|
||||
|
Reference in New Issue
Block a user