Skip to content

Commit 194c627

Browse files
committed
Update tx submission logic to take into account the surge factor changes
1 parent 58d95ad commit 194c627

File tree

4 files changed

+121
-5
lines changed

4 files changed

+121
-5
lines changed

api/api.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,12 @@ func (b *BlockChainAPI) SendRawTransaction(
182182
return common.Hash{}, err
183183
}
184184

185-
id, err := b.evm.SendRawTransaction(ctx, input)
185+
feeParams, err := b.feeParameters.Get()
186+
if err != nil {
187+
return common.Hash{}, err
188+
}
189+
190+
id, err := b.evm.SendRawTransaction(ctx, input, feeParams)
186191
if err != nil {
187192
return handleError[common.Hash](err, l, b.collector)
188193
}

services/requester/requester.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ const estimateGasErrorRatio = 0.015
5353
type Requester interface {
5454
// SendRawTransaction will submit signed transaction data to the network.
5555
// The submitted EVM transaction hash is returned.
56-
SendRawTransaction(ctx context.Context, data []byte) (common.Hash, error)
56+
SendRawTransaction(
57+
ctx context.Context,
58+
data []byte,
59+
feeParams *models.FeeParameters,
60+
) (common.Hash, error)
5761

5862
// GetBalance returns the amount of wei for the given address in the state of the
5963
// given EVM block height.
@@ -163,7 +167,11 @@ func NewEVM(
163167
}, nil
164168
}
165169

166-
func (e *EVM) SendRawTransaction(ctx context.Context, data []byte) (common.Hash, error) {
170+
func (e *EVM) SendRawTransaction(
171+
ctx context.Context,
172+
data []byte,
173+
feeParams *models.FeeParameters,
174+
) (common.Hash, error) {
167175
tx := &types.Transaction{}
168176
if err := tx.UnmarshalBinary(data); err != nil {
169177
return common.Hash{}, err
@@ -224,8 +232,14 @@ func (e *EVM) SendRawTransaction(ctx context.Context, data []byte) (common.Hash,
224232
}
225233
}
226234

227-
if tx.GasPrice().Cmp(e.config.GasPrice) < 0 && e.config.EnforceGasPrice {
228-
return common.Hash{}, errs.NewTxGasPriceTooLowError(e.config.GasPrice)
235+
surgeFactor := uint64(feeParams.SurgeFactor)
236+
multiplier := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(8)), nil)
237+
gp := e.config.GasPrice.Uint64()
238+
gasPrice := new(big.Int).SetUint64(uint64(gp * surgeFactor))
239+
newGasPrice := new(big.Int).Div(gasPrice, multiplier)
240+
241+
if tx.GasPrice().Cmp(newGasPrice) < 0 && e.config.EnforceGasPrice {
242+
return common.Hash{}, errs.NewTxGasPriceTooLowError(newGasPrice)
229243
}
230244

231245
if e.config.TxStateValidation == config.LocalIndexValidation {

tests/e2e_web3js_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,40 @@ func TestWeb3_E2E(t *testing.T) {
114114
})
115115
})
116116

117+
t.Run("gas price with surge factor multipler", func(t *testing.T) {
118+
runWeb3TestWithSetup(t, "eth_gas_price_surge_test", func(emu emulator.Emulator) {
119+
res, err := flowSendTransaction(
120+
emu,
121+
`
122+
import FlowFees from 0xe5a8b7f23e8b548f
123+
124+
// This transaction sets the FlowFees parameters
125+
transaction() {
126+
let flowFeesAccountAdmin: &FlowFees.Administrator
127+
128+
prepare(signer: auth(BorrowValue) &Account) {
129+
self.flowFeesAccountAdmin = signer.storage.borrow<&FlowFees.Administrator>(
130+
from: /storage/flowFeesAdmin
131+
)
132+
?? panic("Unable to borrow reference to administrator resource")
133+
}
134+
135+
execute {
136+
self.flowFeesAccountAdmin.setFeeParameters(
137+
surgeFactor: 2.0,
138+
inclusionEffortCost: 1.0,
139+
executionEffortCost: 1.0
140+
)
141+
}
142+
}
143+
144+
`,
145+
)
146+
require.NoError(t, err)
147+
require.NoError(t, res.Error)
148+
})
149+
})
150+
117151
t.Run("test filter-related endpoints", func(t *testing.T) {
118152
runWeb3Test(t, "eth_filter_endpoints_test")
119153
})
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const utils = require('web3-utils')
2+
const { assert } = require('chai')
3+
const conf = require('./config')
4+
const helpers = require('./helpers')
5+
const web3 = conf.web3
6+
7+
it('updates the gas price', async () => {
8+
let gasPrice = await web3.eth.getGasPrice()
9+
assert.equal(gasPrice, 2n * conf.minGasPrice)
10+
11+
let receiver = web3.eth.accounts.create()
12+
13+
// make sure receiver balance is initially 0
14+
let receiverWei = await web3.eth.getBalance(receiver.address)
15+
assert.equal(receiverWei, 0n)
16+
17+
// get sender balance
18+
let senderBalance = await web3.eth.getBalance(conf.eoa.address)
19+
assert.equal(senderBalance, utils.toWei(conf.fundedAmount, 'ether'))
20+
21+
let txCount = await web3.eth.getTransactionCount(conf.eoa.address)
22+
assert.equal(0n, txCount)
23+
24+
let transferValue = utils.toWei('2.5', 'ether')
25+
// assert that the minimum acceptable gas price has been multiplied by the surge factor
26+
try {
27+
let transfer = await helpers.signAndSend({
28+
from: conf.eoa.address,
29+
to: receiver.address,
30+
value: transferValue,
31+
gasPrice: gasPrice - 10n,
32+
gasLimit: 55_000,
33+
})
34+
assert.fail('should not have gotten here')
35+
} catch (e) {
36+
assert.include(
37+
e.message,
38+
`the minimum accepted gas price for transactions is: ${gasPrice}`
39+
)
40+
}
41+
42+
let transfer = await helpers.signAndSend({
43+
from: conf.eoa.address,
44+
to: receiver.address,
45+
value: transferValue,
46+
gasPrice: gasPrice,
47+
gasLimit: 55_000,
48+
})
49+
assert.equal(transfer.receipt.status, conf.successStatus)
50+
assert.equal(transfer.receipt.from, conf.eoa.address)
51+
assert.equal(transfer.receipt.to, receiver.address)
52+
53+
let latestBlockNumber = await web3.eth.getBlockNumber()
54+
let latestBlock = await web3.eth.getBlock(latestBlockNumber)
55+
assert.equal(latestBlock.transactions.length, 2)
56+
57+
let transferTx = await web3.eth.getTransactionFromBlock(latestBlockNumber, 0)
58+
let transferTxReceipt = await web3.eth.getTransactionReceipt(transferTx.hash)
59+
assert.equal(transferTxReceipt.effectiveGasPrice, gasPrice)
60+
61+
let coinbaseFeesTx = await web3.eth.getTransactionFromBlock(latestBlockNumber, 1)
62+
assert.equal(coinbaseFeesTx.value, transferTxReceipt.gasUsed * gasPrice)
63+
})

0 commit comments

Comments
 (0)