|
1 | 1 | package txm |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "testing" |
5 | 6 |
|
| 7 | + "github.com/ethereum/go-ethereum/common" |
6 | 8 | evmtypes "github.com/ethereum/go-ethereum/core/types" |
| 9 | + |
7 | 10 | "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/mock" |
8 | 12 | "github.com/stretchr/testify/require" |
9 | 13 |
|
10 | 14 | "github.com/smartcontractkit/chainlink-common/pkg/logger" |
11 | 15 | "github.com/smartcontractkit/chainlink-evm/pkg/assets" |
12 | 16 | "github.com/smartcontractkit/chainlink-evm/pkg/gas" |
| 17 | + "github.com/smartcontractkit/chainlink-evm/pkg/gas/mocks" |
13 | 18 | "github.com/smartcontractkit/chainlink-evm/pkg/keys/keystest" |
14 | 19 | "github.com/smartcontractkit/chainlink-evm/pkg/testutils" |
15 | 20 | "github.com/smartcontractkit/chainlink-evm/pkg/txm/types" |
@@ -85,3 +90,147 @@ func TestAttemptBuilder_newDynamicFeeAttempt(t *testing.T) { |
85 | 90 | assert.Equal(t, gasLimit, a.GasLimit) |
86 | 91 | }) |
87 | 92 | } |
| 93 | + |
| 94 | +func TestAttemptBuilder_NewAgnosticBumpAttempt(t *testing.T) { |
| 95 | + address := testutils.NewAddress() |
| 96 | + lggr := logger.Test(t) |
| 97 | + var nonce uint64 = 77 |
| 98 | + priceMaxKey := func(addr common.Address) *assets.Wei { |
| 99 | + return assets.NewWeiI(1000) |
| 100 | + } |
| 101 | + |
| 102 | + t.Run("returns original attempt when AttemptCount is 0", func(t *testing.T) { |
| 103 | + mockEstimator := mocks.NewEvmFeeEstimator(t) |
| 104 | + ab := NewAttemptBuilder(priceMaxKey, mockEstimator, keystest.TxSigner(nil), 100) |
| 105 | + |
| 106 | + tx := &types.Transaction{ |
| 107 | + ID: 10, |
| 108 | + FromAddress: address, |
| 109 | + Nonce: &nonce, |
| 110 | + AttemptCount: 0, |
| 111 | + } |
| 112 | + |
| 113 | + gasPrice := assets.NewWeiI(100) |
| 114 | + initialFee := gas.EvmFee{GasPrice: gasPrice} |
| 115 | + mockEstimator.On("GetFee", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). |
| 116 | + Return(initialFee, uint64(21000), nil).Once() |
| 117 | + |
| 118 | + attempt, err := ab.NewAgnosticBumpAttempt(t.Context(), lggr, tx, false) |
| 119 | + require.NoError(t, err) |
| 120 | + assert.Equal(t, tx.ID, attempt.TxID) |
| 121 | + assert.Equal(t, gasPrice.String(), attempt.Fee.GasPrice.String()) |
| 122 | + assert.Equal(t, evmtypes.LegacyTxType, int(attempt.Type)) |
| 123 | + mockEstimator.AssertExpectations(t) |
| 124 | + }) |
| 125 | + |
| 126 | + t.Run("bumps once when AttemptCount is 1", func(t *testing.T) { |
| 127 | + mockEstimator := mocks.NewEvmFeeEstimator(t) |
| 128 | + ab := NewAttemptBuilder(priceMaxKey, mockEstimator, keystest.TxSigner(nil), 100) |
| 129 | + |
| 130 | + tx := &types.Transaction{ |
| 131 | + ID: 10, |
| 132 | + FromAddress: address, |
| 133 | + Nonce: &nonce, |
| 134 | + AttemptCount: 1, |
| 135 | + } |
| 136 | + |
| 137 | + gasPrice := assets.NewWeiI(100) |
| 138 | + initialFee := gas.EvmFee{GasPrice: gasPrice} |
| 139 | + bumpedFee := gas.EvmFee{GasPrice: gasPrice.Add(assets.NewWeiI(20))} |
| 140 | + mockEstimator.On("GetFee", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). |
| 141 | + Return(initialFee, uint64(21000), nil).Once() |
| 142 | + mockEstimator.On("BumpFee", mock.Anything, initialFee, mock.Anything, mock.Anything, mock.Anything). |
| 143 | + Return(bumpedFee, uint64(21000), nil).Once() |
| 144 | + |
| 145 | + attempt, err := ab.NewAgnosticBumpAttempt(t.Context(), lggr, tx, false) |
| 146 | + require.NoError(t, err) |
| 147 | + assert.Equal(t, tx.ID, attempt.TxID) |
| 148 | + assert.Equal(t, bumpedFee.GasPrice.String(), attempt.Fee.GasPrice.String()) |
| 149 | + mockEstimator.AssertExpectations(t) |
| 150 | + }) |
| 151 | + |
| 152 | + t.Run("bumps N times when AttemptCount is N", func(t *testing.T) { |
| 153 | + mockEstimator := mocks.NewEvmFeeEstimator(t) |
| 154 | + ab := NewAttemptBuilder(priceMaxKey, mockEstimator, keystest.TxSigner(nil), 100) |
| 155 | + |
| 156 | + tx := &types.Transaction{ |
| 157 | + ID: 10, |
| 158 | + FromAddress: address, |
| 159 | + Nonce: &nonce, |
| 160 | + AttemptCount: 3, |
| 161 | + } |
| 162 | + |
| 163 | + initialFee := gas.EvmFee{GasPrice: assets.NewWeiI(100)} |
| 164 | + firstBump := gas.EvmFee{GasPrice: assets.NewWeiI(110)} |
| 165 | + secondBump := gas.EvmFee{GasPrice: assets.NewWeiI(121)} |
| 166 | + thirdBump := gas.EvmFee{GasPrice: assets.NewWeiI(133)} |
| 167 | + mockEstimator.On("GetFee", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). |
| 168 | + Return(initialFee, uint64(21000), nil).Once() |
| 169 | + mockEstimator.On("BumpFee", mock.Anything, initialFee, mock.Anything, mock.Anything, mock.Anything). |
| 170 | + Return(firstBump, uint64(21000), nil).Once() |
| 171 | + mockEstimator.On("BumpFee", mock.Anything, firstBump, mock.Anything, mock.Anything, mock.Anything). |
| 172 | + Return(secondBump, uint64(21000), nil).Once() |
| 173 | + mockEstimator.On("BumpFee", mock.Anything, secondBump, mock.Anything, mock.Anything, mock.Anything). |
| 174 | + Return(thirdBump, uint64(21000), nil).Once() |
| 175 | + |
| 176 | + attempt, err := ab.NewAgnosticBumpAttempt(t.Context(), lggr, tx, false) |
| 177 | + require.NoError(t, err) |
| 178 | + assert.Equal(t, tx.ID, attempt.TxID) |
| 179 | + assert.Equal(t, thirdBump.GasPrice.String(), attempt.Fee.GasPrice.String()) |
| 180 | + mockEstimator.AssertExpectations(t) |
| 181 | + }) |
| 182 | + |
| 183 | + t.Run("returns last valid attempt when BumpFee fails", func(t *testing.T) { |
| 184 | + mockEstimator := mocks.NewEvmFeeEstimator(t) |
| 185 | + ab := NewAttemptBuilder(priceMaxKey, mockEstimator, keystest.TxSigner(nil), 100) |
| 186 | + |
| 187 | + tx := &types.Transaction{ |
| 188 | + ID: 10, |
| 189 | + FromAddress: address, |
| 190 | + Nonce: &nonce, |
| 191 | + AttemptCount: 3, |
| 192 | + } |
| 193 | + |
| 194 | + gasPrice := assets.NewWeiI(100) |
| 195 | + initialFee := gas.EvmFee{GasPrice: gasPrice} |
| 196 | + firstBump := gas.EvmFee{GasPrice: gasPrice.Add(assets.NewWeiI(20))} |
| 197 | + mockEstimator.On("GetFee", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). |
| 198 | + Return(initialFee, uint64(21000), nil).Once() |
| 199 | + mockEstimator.On("BumpFee", mock.Anything, initialFee, mock.Anything, mock.Anything, mock.Anything). |
| 200 | + Return(firstBump, uint64(21000), nil).Once() |
| 201 | + mockEstimator.On("BumpFee", mock.Anything, firstBump, mock.Anything, mock.Anything, mock.Anything). |
| 202 | + Return(gas.EvmFee{}, uint64(0), errors.New("transaction propagation issue: transactions are not being mined")).Once() |
| 203 | + |
| 204 | + attempt, err := ab.NewAgnosticBumpAttempt(t.Context(), lggr, tx, false) |
| 205 | + require.NoError(t, err) |
| 206 | + assert.Equal(t, tx.ID, attempt.TxID) |
| 207 | + // Should return the last valid bumped attempt |
| 208 | + assert.Equal(t, firstBump.GasPrice.String(), attempt.Fee.GasPrice.String()) |
| 209 | + mockEstimator.AssertExpectations(t) |
| 210 | + }) |
| 211 | + |
| 212 | + t.Run("caps bumps at maxBumpThreshold", func(t *testing.T) { |
| 213 | + mockEstimator := mocks.NewEvmFeeEstimator(t) |
| 214 | + ab := NewAttemptBuilder(priceMaxKey, mockEstimator, keystest.TxSigner(nil), 100) |
| 215 | + |
| 216 | + tx := &types.Transaction{ |
| 217 | + ID: 10, |
| 218 | + FromAddress: address, |
| 219 | + Nonce: &nonce, |
| 220 | + AttemptCount: 10, // More than maxBumpThreshold (5) |
| 221 | + } |
| 222 | + |
| 223 | + initialFee := gas.EvmFee{GasPrice: assets.NewWeiI(100)} |
| 224 | + bumpedFee := gas.EvmFee{GasPrice: initialFee.GasPrice.Add(assets.NewWeiI(20))} |
| 225 | + mockEstimator.On("GetFee", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). |
| 226 | + Return(initialFee, uint64(21000), nil).Once() |
| 227 | + // Should only bump 5 times (maxBumpThreshold) |
| 228 | + mockEstimator.On("BumpFee", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). |
| 229 | + Return(bumpedFee, uint64(21000), nil).Times(5) |
| 230 | + |
| 231 | + attempt, err := ab.NewAgnosticBumpAttempt(t.Context(), lggr, tx, false) |
| 232 | + require.NoError(t, err) |
| 233 | + assert.Equal(t, tx.ID, attempt.TxID) |
| 234 | + mockEstimator.AssertExpectations(t) |
| 235 | + }) |
| 236 | +} |
0 commit comments