|
| 1 | +package ethutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "math/big" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/ethereum/go-ethereum/common" |
| 10 | + "github.com/ethereum/go-ethereum/core/types" |
| 11 | +) |
| 12 | + |
| 13 | +const checkInterval = 100 * time.Millisecond |
| 14 | + |
| 15 | +var maxGasPrice = big.NewInt(45000000000) // 45 Gwei |
| 16 | + |
| 17 | +func TestForceMining_FirstMined(t *testing.T) { |
| 18 | + originalTransaction := createTransaction(big.NewInt(20000000000)) // 20 Gwei |
| 19 | + |
| 20 | + mockBackend := &mockDeployBackend{} |
| 21 | + |
| 22 | + var resubmissionGasPrices []*big.Int |
| 23 | + |
| 24 | + resubmitFn := func(gasPrice *big.Int) (*types.Transaction, error) { |
| 25 | + resubmissionGasPrices = append(resubmissionGasPrices, gasPrice) |
| 26 | + return createTransaction(gasPrice), nil |
| 27 | + } |
| 28 | + |
| 29 | + // receipt is already there |
| 30 | + mockBackend.receipt = &types.Receipt{} |
| 31 | + |
| 32 | + waiter := NewMiningWaiter(mockBackend, checkInterval, maxGasPrice) |
| 33 | + waiter.ForceMining( |
| 34 | + originalTransaction, |
| 35 | + resubmitFn, |
| 36 | + ) |
| 37 | + |
| 38 | + resubmissionCount := len(resubmissionGasPrices) |
| 39 | + if resubmissionCount != 0 { |
| 40 | + t.Fatalf("expected no resubmissions; has: [%v]", resubmissionCount) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestForceMining_SecondMined(t *testing.T) { |
| 45 | + originalTransaction := createTransaction(big.NewInt(20000000000)) // 20 Gwei |
| 46 | + |
| 47 | + mockBackend := &mockDeployBackend{} |
| 48 | + |
| 49 | + var resubmissionGasPrices []*big.Int |
| 50 | + |
| 51 | + resubmitFn := func(gasPrice *big.Int) (*types.Transaction, error) { |
| 52 | + resubmissionGasPrices = append(resubmissionGasPrices, gasPrice) |
| 53 | + // first resubmission succeeded |
| 54 | + mockBackend.receipt = &types.Receipt{} |
| 55 | + return createTransaction(gasPrice), nil |
| 56 | + } |
| 57 | + |
| 58 | + waiter := NewMiningWaiter(mockBackend, checkInterval, maxGasPrice) |
| 59 | + waiter.ForceMining( |
| 60 | + originalTransaction, |
| 61 | + resubmitFn, |
| 62 | + ) |
| 63 | + |
| 64 | + resubmissionCount := len(resubmissionGasPrices) |
| 65 | + if resubmissionCount != 1 { |
| 66 | + t.Fatalf("expected one resubmission; has: [%v]", resubmissionCount) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestForceMining_MultipleAttempts(t *testing.T) { |
| 71 | + originalTransaction := createTransaction(big.NewInt(20000000000)) // 20 Gwei |
| 72 | + |
| 73 | + mockBackend := &mockDeployBackend{} |
| 74 | + |
| 75 | + var resubmissionGasPrices []*big.Int |
| 76 | + |
| 77 | + expectedAttempts := 3 |
| 78 | + expectedResubmissionGasPrices := []*big.Int{ |
| 79 | + big.NewInt(24000000000), // + 20% |
| 80 | + big.NewInt(28800000000), // + 20% |
| 81 | + big.NewInt(34560000000), // + 20% |
| 82 | + } |
| 83 | + |
| 84 | + attemptsSoFar := 1 |
| 85 | + resubmitFn := func(gasPrice *big.Int) (*types.Transaction, error) { |
| 86 | + resubmissionGasPrices = append(resubmissionGasPrices, gasPrice) |
| 87 | + if attemptsSoFar == expectedAttempts { |
| 88 | + mockBackend.receipt = &types.Receipt{} |
| 89 | + } else { |
| 90 | + attemptsSoFar++ |
| 91 | + } |
| 92 | + return createTransaction(gasPrice), nil |
| 93 | + } |
| 94 | + |
| 95 | + waiter := NewMiningWaiter(mockBackend, checkInterval, maxGasPrice) |
| 96 | + waiter.ForceMining( |
| 97 | + originalTransaction, |
| 98 | + resubmitFn, |
| 99 | + ) |
| 100 | + |
| 101 | + resubmissionCount := len(resubmissionGasPrices) |
| 102 | + if resubmissionCount != expectedAttempts { |
| 103 | + t.Fatalf( |
| 104 | + "expected [%v] resubmission; has: [%v]", |
| 105 | + expectedAttempts, |
| 106 | + resubmissionCount, |
| 107 | + ) |
| 108 | + } |
| 109 | + |
| 110 | + for resubmission, price := range resubmissionGasPrices { |
| 111 | + if price.Cmp(expectedResubmissionGasPrices[resubmission]) != 0 { |
| 112 | + t.Fatalf( |
| 113 | + "unexpected [%v] resubmission gas price\nexpected: [%v]\nactual: [%v]", |
| 114 | + resubmission, |
| 115 | + expectedResubmissionGasPrices[resubmission], |
| 116 | + price, |
| 117 | + ) |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func TestForceMining_MaxAllowedPriceReached(t *testing.T) { |
| 123 | + originalTransaction := createTransaction(big.NewInt(20000000000)) // 20 Gwei |
| 124 | + |
| 125 | + mockBackend := &mockDeployBackend{} |
| 126 | + |
| 127 | + var resubmissionGasPrices []*big.Int |
| 128 | + |
| 129 | + expectedAttempts := 5 |
| 130 | + expectedResubmissionGasPrices := []*big.Int{ |
| 131 | + big.NewInt(24000000000), // + 20% |
| 132 | + big.NewInt(28800000000), // + 20% |
| 133 | + big.NewInt(34560000000), // + 20% |
| 134 | + big.NewInt(41472000000), // + 20% |
| 135 | + big.NewInt(45000000000), // max allowed |
| 136 | + } |
| 137 | + |
| 138 | + resubmitFn := func(gasPrice *big.Int) (*types.Transaction, error) { |
| 139 | + resubmissionGasPrices = append(resubmissionGasPrices, gasPrice) |
| 140 | + // not setting mockBackend.receipt, mining takes a very long time |
| 141 | + return createTransaction(gasPrice), nil |
| 142 | + } |
| 143 | + |
| 144 | + waiter := NewMiningWaiter(mockBackend, checkInterval, maxGasPrice) |
| 145 | + waiter.ForceMining( |
| 146 | + originalTransaction, |
| 147 | + resubmitFn, |
| 148 | + ) |
| 149 | + |
| 150 | + resubmissionCount := len(resubmissionGasPrices) |
| 151 | + if resubmissionCount != expectedAttempts { |
| 152 | + t.Fatalf( |
| 153 | + "expected [%v] resubmission; has: [%v]", |
| 154 | + expectedAttempts, |
| 155 | + resubmissionCount, |
| 156 | + ) |
| 157 | + } |
| 158 | + |
| 159 | + for resubmission, price := range resubmissionGasPrices { |
| 160 | + if price.Cmp(expectedResubmissionGasPrices[resubmission]) != 0 { |
| 161 | + t.Fatalf( |
| 162 | + "unexpected [%v] resubmission gas price\nexpected: [%v]\nactual: [%v]", |
| 163 | + resubmission, |
| 164 | + expectedResubmissionGasPrices[resubmission], |
| 165 | + price, |
| 166 | + ) |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func TestForceMining_OriginalPriceHigherThanMaxAllowed(t *testing.T) { |
| 172 | + // original transaction was priced at 46 Gwei, the maximum allowed gas price |
| 173 | + // is 45 Gwei |
| 174 | + originalTransaction := createTransaction(big.NewInt(46000000000)) |
| 175 | + |
| 176 | + mockBackend := &mockDeployBackend{} |
| 177 | + |
| 178 | + var resubmissionGasPrices []*big.Int |
| 179 | + |
| 180 | + resubmitFn := func(gasPrice *big.Int) (*types.Transaction, error) { |
| 181 | + resubmissionGasPrices = append(resubmissionGasPrices, gasPrice) |
| 182 | + // not setting mockBackend.receipt, mining takes a very long time |
| 183 | + return createTransaction(gasPrice), nil |
| 184 | + } |
| 185 | + |
| 186 | + waiter := NewMiningWaiter(mockBackend, checkInterval, maxGasPrice) |
| 187 | + waiter.ForceMining( |
| 188 | + originalTransaction, |
| 189 | + resubmitFn, |
| 190 | + ) |
| 191 | + |
| 192 | + resubmissionCount := len(resubmissionGasPrices) |
| 193 | + if resubmissionCount != 0 { |
| 194 | + t.Fatalf("expected no resubmissions; has: [%v]", resubmissionCount) |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +func createTransaction(gasPrice *big.Int) *types.Transaction { |
| 199 | + return types.NewTransaction( |
| 200 | + 10, // nonce |
| 201 | + common.HexToAddress("0x131D387731bBbC988B312206c74F77D004D6B84b"), // to |
| 202 | + big.NewInt(0), // amount |
| 203 | + 200000, // gas limit |
| 204 | + gasPrice, // gas price |
| 205 | + []byte{}, // data |
| 206 | + ) |
| 207 | +} |
| 208 | + |
| 209 | +type mockDeployBackend struct { |
| 210 | + receipt *types.Receipt |
| 211 | +} |
| 212 | + |
| 213 | +func (mdb *mockDeployBackend) TransactionReceipt( |
| 214 | + ctx context.Context, |
| 215 | + txHash common.Hash, |
| 216 | +) (*types.Receipt, error) { |
| 217 | + return mdb.receipt, nil |
| 218 | +} |
| 219 | + |
| 220 | +func (mdb *mockDeployBackend) CodeAt( |
| 221 | + ctx context.Context, |
| 222 | + account common.Address, |
| 223 | + blockNumber *big.Int, |
| 224 | +) ([]byte, error) { |
| 225 | + panic("not implemented") |
| 226 | +} |
0 commit comments