github.com/koko1123/flow-go-1@v0.29.6/fvm/transactionPayerBalanceChecker_test.go (about) 1 package fvm_test 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/mock" 8 "github.com/stretchr/testify/require" 9 10 "github.com/onflow/cadence" 11 12 "github.com/koko1123/flow-go-1/fvm" 13 fvmmock "github.com/koko1123/flow-go-1/fvm/environment/mock" 14 "github.com/koko1123/flow-go-1/fvm/errors" 15 "github.com/koko1123/flow-go-1/fvm/state" 16 "github.com/koko1123/flow-go-1/fvm/utils" 17 "github.com/koko1123/flow-go-1/model/flow" 18 ) 19 20 func TestTransactionPayerBalanceChecker(t *testing.T) { 21 payer := flow.HexToAddress("1") 22 t.Run("TransactionFeesEnabled == false disables the balance check", func(t *testing.T) { 23 env := &fvmmock.Environment{} 24 env.On("TransactionFeesEnabled").Return(false) 25 26 proc := &fvm.TransactionProcedure{} 27 proc.Transaction = &flow.TransactionBody{} 28 proc.Transaction.Payer = payer 29 30 txnState := state.NewTransactionState( 31 utils.NewSimpleView(), 32 state.DefaultParameters()) 33 34 d := fvm.TransactionPayerBalanceChecker{} 35 maxFees, err := d.CheckPayerBalanceAndReturnMaxFees(proc, txnState, env) 36 require.NoError(t, err) 37 require.Equal(t, uint64(0), maxFees) 38 }) 39 40 t.Run("errors during CheckPayerBalanceAndGetMaxTxFees invocation are wrapped and returned", func(t *testing.T) { 41 someError := fmt.Errorf("some error") 42 43 env := &fvmmock.Environment{} 44 env.On("TransactionFeesEnabled").Return(true) 45 env.On("CheckPayerBalanceAndGetMaxTxFees", mock.Anything, mock.Anything, mock.Anything).Return( 46 nil, 47 someError) 48 49 proc := &fvm.TransactionProcedure{} 50 proc.Transaction = &flow.TransactionBody{} 51 proc.Transaction.Payer = payer 52 53 txnState := state.NewTransactionState( 54 utils.NewSimpleView(), 55 state.DefaultParameters()) 56 57 d := fvm.TransactionPayerBalanceChecker{} 58 maxFees, err := d.CheckPayerBalanceAndReturnMaxFees(proc, txnState, env) 59 require.Error(t, err) 60 require.True(t, errors.HasErrorCode(err, errors.FailureCodePayerBalanceCheckFailure)) 61 require.ErrorIs(t, err, someError) 62 require.Equal(t, uint64(0), maxFees) 63 }) 64 65 t.Run("unexpected result type from CheckPayerBalanceAndGetMaxTxFees causes error", func(t *testing.T) { 66 env := &fvmmock.Environment{} 67 env.On("TransactionFeesEnabled").Return(true) 68 env.On("CheckPayerBalanceAndGetMaxTxFees", mock.Anything, mock.Anything, mock.Anything).Return( 69 cadence.Struct{}, 70 nil) 71 72 proc := &fvm.TransactionProcedure{} 73 proc.Transaction = &flow.TransactionBody{} 74 proc.Transaction.Payer = payer 75 76 txnState := state.NewTransactionState( 77 utils.NewSimpleView(), 78 state.DefaultParameters()) 79 80 d := fvm.TransactionPayerBalanceChecker{} 81 maxFees, err := d.CheckPayerBalanceAndReturnMaxFees(proc, txnState, env) 82 require.Error(t, err) 83 require.True(t, errors.HasErrorCode(err, errors.FailureCodePayerBalanceCheckFailure)) 84 require.Equal(t, uint64(0), maxFees) 85 }) 86 87 t.Run("if payer can pay return max fees", func(t *testing.T) { 88 env := &fvmmock.Environment{} 89 env.On("TransactionFeesEnabled").Return(true) 90 env.On("CheckPayerBalanceAndGetMaxTxFees", mock.Anything, mock.Anything, mock.Anything).Return( 91 cadence.Struct{ 92 Fields: []cadence.Value{ 93 cadence.NewBool(true), 94 cadence.UFix64(100), 95 cadence.UFix64(100), 96 }, 97 }, 98 nil) 99 100 proc := &fvm.TransactionProcedure{} 101 proc.Transaction = &flow.TransactionBody{} 102 proc.Transaction.Payer = payer 103 104 txnState := state.NewTransactionState( 105 utils.NewSimpleView(), 106 state.DefaultParameters()) 107 108 d := fvm.TransactionPayerBalanceChecker{} 109 maxFees, err := d.CheckPayerBalanceAndReturnMaxFees(proc, txnState, env) 110 require.NoError(t, err) 111 require.Equal(t, uint64(100), maxFees) 112 }) 113 114 t.Run("if payer cannot pay return insufficient balance error", func(t *testing.T) { 115 env := &fvmmock.Environment{} 116 env.On("TransactionFeesEnabled").Return(true) 117 env.On("CheckPayerBalanceAndGetMaxTxFees", mock.Anything, mock.Anything, mock.Anything).Return( 118 cadence.Struct{ 119 Fields: []cadence.Value{ 120 cadence.NewBool(false), 121 cadence.UFix64(100), 122 cadence.UFix64(101), 123 }, 124 }, 125 nil) 126 127 proc := &fvm.TransactionProcedure{} 128 proc.Transaction = &flow.TransactionBody{} 129 proc.Transaction.Payer = payer 130 131 txnState := state.NewTransactionState( 132 utils.NewSimpleView(), 133 state.DefaultParameters()) 134 135 d := fvm.TransactionPayerBalanceChecker{} 136 maxFees, err := d.CheckPayerBalanceAndReturnMaxFees(proc, txnState, env) 137 require.Error(t, err) 138 require.Contains(t, err.Error(), errors.NewInsufficientPayerBalanceError(payer, 100).Error()) 139 require.Equal(t, uint64(0), maxFees) 140 }) 141 }