github.com/koko1123/flow-go-1@v0.29.6/fvm/runtime/wrapped_cadence_runtime.go (about)

     1  package runtime
     2  
     3  import (
     4  	"github.com/onflow/cadence"
     5  	"github.com/onflow/cadence/runtime"
     6  	"github.com/onflow/cadence/runtime/common"
     7  	"github.com/onflow/cadence/runtime/interpreter"
     8  	"github.com/onflow/cadence/runtime/sema"
     9  
    10  	"github.com/koko1123/flow-go-1/fvm/errors"
    11  )
    12  
    13  // WrappedCadenceRuntime wraps cadence runtime to handle errors.
    14  // Errors from cadence runtime should be handled with `errors.HandleRuntimeError` before the FVM can understand them.
    15  // Handling all possible locations, where the error could be coming from, here,
    16  // makes it impossible to forget to handle the error.
    17  type WrappedCadenceRuntime struct {
    18  	runtime.Runtime
    19  }
    20  
    21  func (wr WrappedCadenceRuntime) NewScriptExecutor(s runtime.Script, c runtime.Context) runtime.Executor {
    22  	return WrappedCadenceExecutor{wr.Runtime.NewScriptExecutor(s, c)}
    23  }
    24  
    25  func (wr WrappedCadenceRuntime) ExecuteScript(s runtime.Script, c runtime.Context) (cadence.Value, error) {
    26  	v, err := wr.Runtime.ExecuteScript(s, c)
    27  	return v, errors.HandleRuntimeError(err)
    28  }
    29  
    30  func (wr WrappedCadenceRuntime) NewTransactionExecutor(s runtime.Script, c runtime.Context) runtime.Executor {
    31  	return WrappedCadenceExecutor{wr.Runtime.NewTransactionExecutor(s, c)}
    32  }
    33  
    34  func (wr WrappedCadenceRuntime) ExecuteTransaction(s runtime.Script, c runtime.Context) error {
    35  	err := wr.Runtime.ExecuteTransaction(s, c)
    36  	return errors.HandleRuntimeError(err)
    37  }
    38  
    39  func (wr WrappedCadenceRuntime) NewContractFunctionExecutor(
    40  	contractLocation common.AddressLocation,
    41  	functionName string,
    42  	arguments []cadence.Value,
    43  	argumentTypes []sema.Type,
    44  	context runtime.Context,
    45  ) runtime.Executor {
    46  	return WrappedCadenceExecutor{wr.Runtime.NewContractFunctionExecutor(contractLocation, functionName, arguments, argumentTypes, context)}
    47  }
    48  
    49  func (wr WrappedCadenceRuntime) InvokeContractFunction(
    50  	contractLocation common.AddressLocation,
    51  	functionName string,
    52  	arguments []cadence.Value,
    53  	argumentTypes []sema.Type,
    54  	context runtime.Context,
    55  ) (cadence.Value, error) {
    56  	v, err := wr.Runtime.InvokeContractFunction(contractLocation, functionName, arguments, argumentTypes, context)
    57  	return v, errors.HandleRuntimeError(err)
    58  }
    59  
    60  func (wr WrappedCadenceRuntime) ParseAndCheckProgram(source []byte, context runtime.Context) (*interpreter.Program, error) {
    61  	p, err := wr.Runtime.ParseAndCheckProgram(source, context)
    62  	return p, errors.HandleRuntimeError(err)
    63  }
    64  
    65  func (wr WrappedCadenceRuntime) ReadStored(address common.Address, path cadence.Path, context runtime.Context) (cadence.Value, error) {
    66  	v, err := wr.Runtime.ReadStored(address, path, context)
    67  	return v, errors.HandleRuntimeError(err)
    68  }
    69  
    70  func (wr WrappedCadenceRuntime) ReadLinked(address common.Address, path cadence.Path, context runtime.Context) (cadence.Value, error) {
    71  	v, err := wr.Runtime.ReadLinked(address, path, context)
    72  	return v, errors.HandleRuntimeError(err)
    73  }
    74  
    75  func (wr WrappedCadenceRuntime) Storage(context runtime.Context) (*runtime.Storage, *interpreter.Interpreter, error) {
    76  	s, i, err := wr.Runtime.Storage(context)
    77  	return s, i, errors.HandleRuntimeError(err)
    78  }
    79  
    80  type WrappedCadenceExecutor struct {
    81  	runtime.Executor
    82  }
    83  
    84  func (we WrappedCadenceExecutor) Preprocess() error {
    85  	return errors.HandleRuntimeError(we.Executor.Preprocess())
    86  }
    87  
    88  func (we WrappedCadenceExecutor) Execute() error {
    89  	return errors.HandleRuntimeError(we.Executor.Execute())
    90  }
    91  
    92  func (we WrappedCadenceExecutor) Result() (cadence.Value, error) {
    93  	v, err := we.Executor.Result()
    94  	return v, errors.HandleRuntimeError(err)
    95  }