github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/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/onflow/flow-go/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) Storage(context runtime.Context) (*runtime.Storage, *interpreter.Interpreter, error) { 71 s, i, err := wr.Runtime.Storage(context) 72 return s, i, errors.HandleRuntimeError(err) 73 } 74 75 type WrappedCadenceExecutor struct { 76 runtime.Executor 77 } 78 79 func (we WrappedCadenceExecutor) Preprocess() error { 80 return errors.HandleRuntimeError(we.Executor.Preprocess()) 81 } 82 83 func (we WrappedCadenceExecutor) Execute() error { 84 return errors.HandleRuntimeError(we.Executor.Execute()) 85 } 86 87 func (we WrappedCadenceExecutor) Result() (cadence.Value, error) { 88 v, err := we.Executor.Result() 89 return v, errors.HandleRuntimeError(err) 90 }