github.com/onflow/flow-go@v0.33.17/fvm/evm/types/emulator.go (about)

     1  package types
     2  
     3  import (
     4  	"math/big"
     5  
     6  	gethTypes "github.com/ethereum/go-ethereum/core/types"
     7  )
     8  
     9  var (
    10  	DefaultDirectCallBaseGasUsage = uint64(21_000)
    11  	DefaultDirectCallGasPrice     = uint64(0)
    12  
    13  	// anything block number above 0 works here
    14  	BlockNumberForEVMRules = big.NewInt(1)
    15  )
    16  
    17  // BlockContext holds the context needed for the emulator operations
    18  type BlockContext struct {
    19  	BlockNumber            uint64
    20  	DirectCallBaseGasUsage uint64
    21  	DirectCallGasPrice     uint64
    22  	GasFeeCollector        Address
    23  }
    24  
    25  // NewDefaultBlockContext returns a new default block context
    26  func NewDefaultBlockContext(BlockNumber uint64) BlockContext {
    27  	return BlockContext{
    28  		BlockNumber:            BlockNumber,
    29  		DirectCallBaseGasUsage: DefaultDirectCallBaseGasUsage,
    30  		DirectCallGasPrice:     DefaultDirectCallGasPrice,
    31  	}
    32  }
    33  
    34  // ReadOnlyBlockView provides a read only view of a block
    35  type ReadOnlyBlockView interface {
    36  	// BalanceOf returns the balance of this address
    37  	BalanceOf(address Address) (*big.Int, error)
    38  	// NonceOf returns the nonce of this address
    39  	NonceOf(address Address) (uint64, error)
    40  	// CodeOf returns the code for this address (if smart contract is deployed at this address)
    41  	CodeOf(address Address) (Code, error)
    42  }
    43  
    44  // BlockView facilitates execution of a transaction or a direct evm  call in the context of a block
    45  // Errors returned by the methods are one of the followings:
    46  // - Fatal error
    47  // - Database error (non-fatal)
    48  // - EVM validation error
    49  // - EVM execution error
    50  type BlockView interface {
    51  	// executes a direct call
    52  	DirectCall(call *DirectCall) (*Result, error)
    53  
    54  	// RunTransaction executes an evm transaction
    55  	RunTransaction(tx *gethTypes.Transaction) (*Result, error)
    56  }
    57  
    58  // Emulator emulates an evm-compatible chain
    59  type Emulator interface {
    60  	// constructs a new block view
    61  	NewReadOnlyBlockView(ctx BlockContext) (ReadOnlyBlockView, error)
    62  
    63  	// constructs a new block
    64  	NewBlockView(ctx BlockContext) (BlockView, error)
    65  }