github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/evm/testutils/emulator.go (about)

     1  package testutils
     2  
     3  import (
     4  	"math/big"
     5  
     6  	gethCommon "github.com/onflow/go-ethereum/common"
     7  
     8  	gethTypes "github.com/onflow/go-ethereum/core/types"
     9  
    10  	"github.com/onflow/flow-go/fvm/evm/types"
    11  )
    12  
    13  type TestEmulator struct {
    14  	BalanceOfFunc           func(address types.Address) (*big.Int, error)
    15  	NonceOfFunc             func(address types.Address) (uint64, error)
    16  	CodeOfFunc              func(address types.Address) (types.Code, error)
    17  	CodeHashOfFunc          func(address types.Address) ([]byte, error)
    18  	DirectCallFunc          func(call *types.DirectCall) (*types.Result, error)
    19  	RunTransactionFunc      func(tx *gethTypes.Transaction) (*types.Result, error)
    20  	DryRunTransactionFunc   func(tx *gethTypes.Transaction, address gethCommon.Address) (*types.Result, error)
    21  	BatchRunTransactionFunc func(txs []*gethTypes.Transaction) ([]*types.Result, error)
    22  }
    23  
    24  var _ types.Emulator = &TestEmulator{}
    25  
    26  // NewBlock returns a new block
    27  func (em *TestEmulator) NewBlockView(_ types.BlockContext) (types.BlockView, error) {
    28  	return em, nil
    29  }
    30  
    31  // NewBlock returns a new block view
    32  func (em *TestEmulator) NewReadOnlyBlockView(_ types.BlockContext) (types.ReadOnlyBlockView, error) {
    33  	return em, nil
    34  }
    35  
    36  // BalanceOf returns the balance of this address
    37  func (em *TestEmulator) BalanceOf(address types.Address) (*big.Int, error) {
    38  	if em.BalanceOfFunc == nil {
    39  		panic("method not set")
    40  	}
    41  	return em.BalanceOfFunc(address)
    42  }
    43  
    44  // NonceOfFunc returns the nonce for this address
    45  func (em *TestEmulator) NonceOf(address types.Address) (uint64, error) {
    46  	if em.NonceOfFunc == nil {
    47  		panic("method not set")
    48  	}
    49  	return em.NonceOfFunc(address)
    50  }
    51  
    52  // CodeOf returns the code for this address
    53  func (em *TestEmulator) CodeOf(address types.Address) (types.Code, error) {
    54  	if em.CodeOfFunc == nil {
    55  		panic("method not set")
    56  	}
    57  	return em.CodeOfFunc(address)
    58  }
    59  
    60  // CodeHashOf returns the code hash for this address
    61  func (em *TestEmulator) CodeHashOf(address types.Address) ([]byte, error) {
    62  	if em.CodeHashOfFunc == nil {
    63  		panic("method not set")
    64  	}
    65  	return em.CodeHashOfFunc(address)
    66  }
    67  
    68  // DirectCall executes a direct call
    69  func (em *TestEmulator) DirectCall(call *types.DirectCall) (*types.Result, error) {
    70  	if em.DirectCallFunc == nil {
    71  		panic("method not set")
    72  	}
    73  	return em.DirectCallFunc(call)
    74  }
    75  
    76  // RunTransaction runs a transaction and collect gas fees to the coinbase account
    77  func (em *TestEmulator) RunTransaction(tx *gethTypes.Transaction) (*types.Result, error) {
    78  	if em.RunTransactionFunc == nil {
    79  		panic("method not set")
    80  	}
    81  	return em.RunTransactionFunc(tx)
    82  }
    83  
    84  // BatchRunTransactions batch runs transactions and collect gas fees to the coinbase account
    85  func (em *TestEmulator) BatchRunTransactions(txs []*gethTypes.Transaction) ([]*types.Result, error) {
    86  	if em.BatchRunTransactionFunc == nil {
    87  		panic("method not set")
    88  	}
    89  	return em.BatchRunTransactionFunc(txs)
    90  }
    91  
    92  // DryRunTransaction simulates transaction execution
    93  func (em *TestEmulator) DryRunTransaction(tx *gethTypes.Transaction, address gethCommon.Address) (*types.Result, error) {
    94  	if em.DryRunTransactionFunc == nil {
    95  		panic("method not set")
    96  	}
    97  	return em.DryRunTransactionFunc(tx, address)
    98  }