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

     1  package backends
     2  
     3  import (
     4  	"github.com/onflow/atree"
     5  	"github.com/onflow/cadence"
     6  	"github.com/onflow/cadence/runtime"
     7  	"github.com/onflow/cadence/runtime/common"
     8  
     9  	"github.com/onflow/flow-go/fvm/environment"
    10  	"github.com/onflow/flow-go/fvm/errors"
    11  	"github.com/onflow/flow-go/fvm/evm/types"
    12  	"github.com/onflow/flow-go/fvm/meter"
    13  	"github.com/onflow/flow-go/model/flow"
    14  )
    15  
    16  // WrappedEnvironment wraps an FVM environment
    17  type WrappedEnvironment struct {
    18  	env environment.Environment
    19  }
    20  
    21  // NewWrappedEnvironment constructs a new wrapped environment
    22  func NewWrappedEnvironment(env environment.Environment) types.Backend {
    23  	return &WrappedEnvironment{env}
    24  }
    25  
    26  var _ types.Backend = &WrappedEnvironment{}
    27  
    28  func (we *WrappedEnvironment) GetValue(owner, key []byte) ([]byte, error) {
    29  	val, err := we.env.GetValue(owner, key)
    30  	return val, handleEnvironmentError(err)
    31  }
    32  
    33  func (we *WrappedEnvironment) SetValue(owner, key, value []byte) error {
    34  	err := we.env.SetValue(owner, key, value)
    35  	return handleEnvironmentError(err)
    36  }
    37  
    38  func (we *WrappedEnvironment) ValueExists(owner, key []byte) (bool, error) {
    39  	b, err := we.env.ValueExists(owner, key)
    40  	return b, handleEnvironmentError(err)
    41  }
    42  
    43  func (we *WrappedEnvironment) AllocateSlabIndex(owner []byte) (atree.SlabIndex, error) {
    44  	index, err := we.env.AllocateSlabIndex(owner)
    45  	return index, handleEnvironmentError(err)
    46  }
    47  
    48  func (we *WrappedEnvironment) MeterComputation(kind common.ComputationKind, intensity uint) error {
    49  	err := we.env.MeterComputation(kind, intensity)
    50  	return handleEnvironmentError(err)
    51  }
    52  
    53  func (we *WrappedEnvironment) ComputationUsed() (uint64, error) {
    54  	val, err := we.env.ComputationUsed()
    55  	return val, handleEnvironmentError(err)
    56  }
    57  
    58  func (we *WrappedEnvironment) ComputationIntensities() meter.MeteredComputationIntensities {
    59  	return we.env.ComputationIntensities()
    60  }
    61  
    62  func (we *WrappedEnvironment) ComputationAvailable(kind common.ComputationKind, intensity uint) bool {
    63  	return we.env.ComputationAvailable(kind, intensity)
    64  }
    65  
    66  func (we *WrappedEnvironment) MeterMemory(usage common.MemoryUsage) error {
    67  	err := we.env.MeterMemory(usage)
    68  	return handleEnvironmentError(err)
    69  }
    70  
    71  func (we *WrappedEnvironment) MemoryUsed() (uint64, error) {
    72  	val, err := we.env.MemoryUsed()
    73  	return val, handleEnvironmentError(err)
    74  }
    75  
    76  func (we *WrappedEnvironment) MeterEmittedEvent(byteSize uint64) error {
    77  	err := we.env.MeterEmittedEvent(byteSize)
    78  	return handleEnvironmentError(err)
    79  }
    80  
    81  func (we *WrappedEnvironment) TotalEmittedEventBytes() uint64 {
    82  	return we.env.TotalEmittedEventBytes()
    83  }
    84  
    85  func (we *WrappedEnvironment) InteractionUsed() (uint64, error) {
    86  	val, err := we.env.InteractionUsed()
    87  	return val, handleEnvironmentError(err)
    88  }
    89  
    90  func (we *WrappedEnvironment) EmitEvent(event cadence.Event) error {
    91  	err := we.env.EmitEvent(event)
    92  	return handleEnvironmentError(err)
    93  }
    94  
    95  func (we *WrappedEnvironment) Events() flow.EventsList {
    96  	return we.env.Events()
    97  
    98  }
    99  
   100  func (we *WrappedEnvironment) ServiceEvents() flow.EventsList {
   101  	return we.env.ServiceEvents()
   102  }
   103  
   104  func (we *WrappedEnvironment) ConvertedServiceEvents() flow.ServiceEventList {
   105  	return we.env.ConvertedServiceEvents()
   106  }
   107  
   108  func (we *WrappedEnvironment) Reset() {
   109  	we.env.Reset()
   110  }
   111  
   112  func (we *WrappedEnvironment) GetCurrentBlockHeight() (uint64, error) {
   113  	val, err := we.env.GetCurrentBlockHeight()
   114  	return val, handleEnvironmentError(err)
   115  }
   116  
   117  func (we *WrappedEnvironment) GetBlockAtHeight(height uint64) (
   118  	runtime.Block,
   119  	bool,
   120  	error,
   121  ) {
   122  	val, found, err := we.env.GetBlockAtHeight(height)
   123  	return val, found, handleEnvironmentError(err)
   124  }
   125  
   126  func (we *WrappedEnvironment) ReadRandom(buffer []byte) error {
   127  	err := we.env.ReadRandom(buffer)
   128  	return handleEnvironmentError(err)
   129  }
   130  
   131  func (we *WrappedEnvironment) Invoke(
   132  	spec environment.ContractFunctionSpec,
   133  	arguments []cadence.Value,
   134  ) (
   135  	cadence.Value,
   136  	error,
   137  ) {
   138  	val, err := we.env.Invoke(spec, arguments)
   139  	return val, handleEnvironmentError(err)
   140  }
   141  
   142  func (we *WrappedEnvironment) GenerateUUID() (uint64, error) {
   143  	uuid, err := we.env.GenerateUUID()
   144  	return uuid, handleEnvironmentError(err)
   145  }
   146  
   147  func handleEnvironmentError(err error) error {
   148  	if err == nil {
   149  		return nil
   150  	}
   151  
   152  	// fvm fatal errors
   153  	if errors.IsFailure(err) {
   154  		return types.NewFatalError(err)
   155  	}
   156  
   157  	return types.NewBackendError(err)
   158  }