github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/evm/types/emulator.go (about) 1 package types 2 3 import ( 4 "math/big" 5 6 gethCommon "github.com/onflow/go-ethereum/common" 7 gethTypes "github.com/onflow/go-ethereum/core/types" 8 gethVM "github.com/onflow/go-ethereum/core/vm" 9 gethCrypto "github.com/onflow/go-ethereum/crypto" 10 ) 11 12 var ( 13 DefaultDirectCallBaseGasUsage = uint64(21_000) 14 DefaultDirectCallGasPrice = uint64(0) 15 16 // anything block number above 0 works here 17 BlockNumberForEVMRules = big.NewInt(1) 18 ) 19 20 type Precompile interface { 21 gethVM.PrecompiledContract 22 Address() Address 23 } 24 25 // BlockContext holds the context needed for the emulator operations 26 type BlockContext struct { 27 ChainID *big.Int 28 BlockNumber uint64 29 BlockTimestamp uint64 30 DirectCallBaseGasUsage uint64 31 DirectCallGasPrice uint64 32 GasFeeCollector Address 33 GetHashFunc func(n uint64) gethCommon.Hash 34 Random gethCommon.Hash 35 36 // a set of extra precompiles to be injected 37 ExtraPrecompiles []Precompile 38 } 39 40 // NewDefaultBlockContext returns a new default block context 41 func NewDefaultBlockContext(BlockNumber uint64) BlockContext { 42 return BlockContext{ 43 ChainID: FlowEVMPreviewNetChainID, 44 BlockNumber: BlockNumber, 45 DirectCallBaseGasUsage: DefaultDirectCallBaseGasUsage, 46 DirectCallGasPrice: DefaultDirectCallGasPrice, 47 GetHashFunc: func(n uint64) gethCommon.Hash { // default returns some random hash values 48 return gethCommon.BytesToHash(gethCrypto.Keccak256([]byte(new(big.Int).SetUint64(n).String()))) 49 }, 50 } 51 } 52 53 // ReadOnlyBlockView provides a read only view of a block 54 type ReadOnlyBlockView interface { 55 // BalanceOf returns the balance of this address 56 BalanceOf(address Address) (*big.Int, error) 57 // NonceOf returns the nonce of this address 58 NonceOf(address Address) (uint64, error) 59 // CodeOf returns the code for this address 60 CodeOf(address Address) (Code, error) 61 // CodeHashOf returns the code hash for this address 62 CodeHashOf(address Address) ([]byte, error) 63 } 64 65 // BlockView facilitates execution of a transaction or a direct evm call in the context of a block 66 // Any error returned by any of the methods (e.g. stateDB errors) if non-fatal stops the outer flow transaction 67 // if fatal stops the node. 68 // EVM validation errors and EVM execution errors are part of the returned result 69 // and should be handled separately. 70 type BlockView interface { 71 // DirectCall executes a direct call 72 DirectCall(call *DirectCall) (*Result, error) 73 74 // RunTransaction executes an evm transaction 75 RunTransaction(tx *gethTypes.Transaction) (*Result, error) 76 77 // DryRunTransaction executes unsigned transaction but does not persist the state changes, 78 // since transaction is not signed, from address is used as the signer. 79 DryRunTransaction(tx *gethTypes.Transaction, from gethCommon.Address) (*Result, error) 80 81 // BatchRunTransactions executes a batch of evm transactions producing 82 // a slice of execution Result where each result corresponds to each 83 // item in the txs slice. 84 BatchRunTransactions(txs []*gethTypes.Transaction) ([]*Result, error) 85 } 86 87 // Emulator emulates an evm-compatible chain 88 type Emulator interface { 89 // constructs a new block view 90 NewReadOnlyBlockView(ctx BlockContext) (ReadOnlyBlockView, error) 91 92 // constructs a new block 93 NewBlockView(ctx BlockContext) (BlockView, error) 94 }