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

     1  package fvm_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/onflow/cadence"
     8  	"github.com/onflow/cadence/runtime"
     9  	"github.com/onflow/cadence/runtime/common"
    10  	"github.com/onflow/cadence/runtime/interpreter"
    11  	"github.com/onflow/cadence/runtime/sema"
    12  	"github.com/rs/zerolog"
    13  	"github.com/stretchr/testify/require"
    14  
    15  	"github.com/onflow/flow-go/fvm"
    16  	"github.com/onflow/flow-go/fvm/storage/testutils"
    17  	"github.com/onflow/flow-go/model/flow"
    18  )
    19  
    20  func TestSafetyCheck(t *testing.T) {
    21  
    22  	t.Run("parsing error in transaction", func(t *testing.T) {
    23  
    24  		buffer := &bytes.Buffer{}
    25  		log := zerolog.New(buffer)
    26  		code := `X`
    27  
    28  		proc := fvm.Transaction(&flow.TransactionBody{Script: []byte(code)}, 0)
    29  
    30  		context := fvm.NewContext(
    31  			fvm.WithLogger(log),
    32  			fvm.WithAuthorizationChecksEnabled(false),
    33  			fvm.WithSequenceNumberCheckAndIncrementEnabled(false))
    34  
    35  		txnState := testutils.NewSimpleTransaction(nil)
    36  
    37  		executor := proc.NewExecutor(context, txnState)
    38  		err := fvm.Run(executor)
    39  		require.Nil(t, err)
    40  		require.Error(t, executor.Output().Err)
    41  
    42  		require.NotContains(t, buffer.String(), "programs")
    43  		require.NotContains(t, buffer.String(), "codes")
    44  
    45  	})
    46  
    47  	t.Run("checking error in transaction", func(t *testing.T) {
    48  
    49  		buffer := &bytes.Buffer{}
    50  		log := zerolog.New(buffer)
    51  
    52  		code := `transaction(arg: X) { }`
    53  
    54  		proc := fvm.Transaction(&flow.TransactionBody{Script: []byte(code)}, 0)
    55  
    56  		context := fvm.NewContext(
    57  			fvm.WithLogger(log),
    58  			fvm.WithAuthorizationChecksEnabled(false),
    59  			fvm.WithSequenceNumberCheckAndIncrementEnabled(false))
    60  
    61  		txnState := testutils.NewSimpleTransaction(nil)
    62  
    63  		executor := proc.NewExecutor(context, txnState)
    64  		err := fvm.Run(executor)
    65  		require.Nil(t, err)
    66  		require.Error(t, executor.Output().Err)
    67  
    68  		require.NotContains(t, buffer.String(), "programs")
    69  		require.NotContains(t, buffer.String(), "codes")
    70  	})
    71  }
    72  
    73  type ErrorReturningRuntime struct {
    74  	TxErrors []error
    75  }
    76  
    77  var _ runtime.Runtime = &ErrorReturningRuntime{}
    78  
    79  func (e *ErrorReturningRuntime) Config() runtime.Config {
    80  	panic("Config not expected")
    81  }
    82  
    83  func (e *ErrorReturningRuntime) NewScriptExecutor(script runtime.Script, context runtime.Context) runtime.Executor {
    84  	panic("NewScriptExecutor not expected")
    85  }
    86  
    87  func (e *ErrorReturningRuntime) NewTransactionExecutor(script runtime.Script, context runtime.Context) runtime.Executor {
    88  	panic("NewTransactionExecutor not expected")
    89  }
    90  
    91  func (e *ErrorReturningRuntime) NewContractFunctionExecutor(contractLocation common.AddressLocation, functionName string, arguments []cadence.Value, argumentTypes []sema.Type, context runtime.Context) runtime.Executor {
    92  	panic("NewContractFunctionExecutor not expected")
    93  }
    94  
    95  func (e *ErrorReturningRuntime) SetInvalidatedResourceValidationEnabled(_ bool) {
    96  	panic("SetInvalidatedResourceValidationEnabled not expected")
    97  }
    98  
    99  func (e *ErrorReturningRuntime) SetResourceOwnerChangeHandlerEnabled(_ bool) {
   100  	panic("SetResourceOwnerChangeHandlerEnabled not expected")
   101  }
   102  
   103  func (e *ErrorReturningRuntime) ExecuteTransaction(_ runtime.Script, _ runtime.Context) error {
   104  	if len(e.TxErrors) == 0 {
   105  		panic("no tx errors left")
   106  	}
   107  
   108  	errToReturn := e.TxErrors[0]
   109  	e.TxErrors = e.TxErrors[1:]
   110  	return errToReturn
   111  }
   112  
   113  func (*ErrorReturningRuntime) ExecuteScript(_ runtime.Script, _ runtime.Context) (cadence.Value, error) {
   114  	panic("ExecuteScript not expected")
   115  }
   116  
   117  func (*ErrorReturningRuntime) ParseAndCheckProgram(_ []byte, _ runtime.Context) (*interpreter.Program, error) {
   118  	panic("ParseAndCheckProgram not expected")
   119  }
   120  
   121  func (*ErrorReturningRuntime) SetCoverageReport(_ *runtime.CoverageReport) {
   122  	panic("not used coverage")
   123  }
   124  
   125  func (*ErrorReturningRuntime) SetContractUpdateValidationEnabled(_ bool) {
   126  	panic("SetContractUpdateValidationEnabled not expected")
   127  }
   128  
   129  func (*ErrorReturningRuntime) SetAtreeValidationEnabled(_ bool) {
   130  	panic("SetAtreeValidationEnabled not expected")
   131  }
   132  
   133  func (e *ErrorReturningRuntime) ReadStored(_ common.Address, _ cadence.Path, _ runtime.Context) (cadence.Value, error) {
   134  	return nil, nil
   135  }
   136  
   137  func (e *ErrorReturningRuntime) InvokeContractFunction(_ common.AddressLocation, _ string, _ []cadence.Value, _ []sema.Type, _ runtime.Context) (cadence.Value, error) {
   138  	panic("InvokeContractFunction not expected")
   139  }
   140  
   141  func (e *ErrorReturningRuntime) SetTracingEnabled(_ bool) {
   142  	panic("SetTracingEnabled not expected")
   143  }
   144  
   145  func (*ErrorReturningRuntime) SetDebugger(_ *interpreter.Debugger) {
   146  	panic("SetDebugger not expected")
   147  }
   148  
   149  func (ErrorReturningRuntime) Storage(runtime.Context) (*runtime.Storage, *interpreter.Interpreter, error) {
   150  	panic("Storage not expected")
   151  }