Skip to content

Commit 1244b41

Browse files
committed
fees: update fee scalar
1 parent 3f79927 commit 1244b41

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

l2geth/core/types/transaction.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/ethereum/go-ethereum/common/hexutil"
2828
"github.com/ethereum/go-ethereum/crypto"
2929
"github.com/ethereum/go-ethereum/rlp"
30+
"github.com/ethereum/go-ethereum/rollup/fees"
3031
)
3132

3233
//go:generate gencodec -type txdata -field-override txdataMarshaling -out gen_tx_json.go
@@ -225,7 +226,7 @@ func (tx *Transaction) UnmarshalJSON(input []byte) error {
225226

226227
func (tx *Transaction) Data() []byte { return common.CopyBytes(tx.data.Payload) }
227228
func (tx *Transaction) Gas() uint64 { return tx.data.GasLimit }
228-
func (tx *Transaction) L2Gas() uint64 { return tx.data.GasLimit % 100_000_000 }
229+
func (tx *Transaction) L2Gas() uint64 { return fees.DecodeL2GasLimitU64(tx.data.GasLimit) }
229230
func (tx *Transaction) GasPrice() *big.Int { return new(big.Int).Set(tx.data.Price) }
230231
func (tx *Transaction) Value() *big.Int { return new(big.Int).Set(tx.data.Amount) }
231232
func (tx *Transaction) Nonce() uint64 { return tx.data.AccountNonce }

l2geth/rollup/fees/rollup_fee.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@ import (
1111
// transaction in gas.
1212
const overhead uint64 = 4200 + 200*params.TxDataNonZeroGasEIP2028
1313

14-
// hundredMillion is a constant used in the gas encoding formula
15-
const hundredMillion uint64 = 100_000_000
16-
1714
// feeScalar is used to scale the calculations in EncodeL2GasLimit
1815
// to prevent them from being too large
19-
const feeScalar uint64 = 1000
16+
const feeScalar uint64 = 10_000_000
2017

2118
// TxGasPrice is a constant that determines the result of `eth_gasPrice`
2219
// It is scaled upwards by 50%
@@ -27,8 +24,10 @@ const TxGasPrice uint64 = feeScalar + (feeScalar / 2)
2724
// BigTxGasPrice is the L2GasPrice as type big.Int
2825
var BigTxGasPrice = new(big.Int).SetUint64(TxGasPrice)
2926
var bigFeeScalar = new(big.Int).SetUint64(feeScalar)
30-
var bigHundredMillion = new(big.Int).SetUint64(hundredMillion)
31-
var BigTenThousand = new(big.Int).SetUint64(10000)
27+
28+
const tenThousand = 10000
29+
30+
var BigTenThousand = new(big.Int).SetUint64(tenThousand)
3231

3332
// EncodeTxGasLimit computes the `tx.gasLimit` based on the L1/L2 gas prices and
3433
// the L2 gas limit. The L2 gas limit is encoded inside of the lower order bits
@@ -42,12 +41,14 @@ var BigTenThousand = new(big.Int).SetUint64(10000)
4241
// the fee, so increasing the L2 Gas limit will increase the fee paid.
4342
// The calculation is:
4443
// l1GasLimit = zero_count(data) * 4 + non_zero_count(data) * 16 + overhead
44+
// roundedL2GasLimit = ceilmod(l2GasLimit, 10_000)
4545
// l1Fee = l1GasPrice * l1GasLimit
46-
// l2Fee = l2GasPrice * l2GasLimit
46+
// l2Fee = l2GasPrice * roundedL2GasLimit
4747
// sum = l1Fee + l2Fee
4848
// scaled = sum / scalar
49-
// rounded = ceilmod(scaled, hundredMillion)
50-
// result = rounded + l2GasLimit
49+
// rounded = ceilmod(scaled, tenThousand)
50+
// roundedScaledL2GasLimit = roundedL2GasLimit / tenThousand
51+
// result = rounded + roundedScaledL2GasLimit
5152
// Note that for simplicity purposes, only the calldata is passed into this
5253
// function when in reality the RLP encoded transaction should be. The
5354
// additional cost is added to the overhead constant to prevent the need to RLP
@@ -59,7 +60,7 @@ func EncodeTxGasLimit(data []byte, l1GasPrice, l2GasLimit, l2GasPrice *big.Int)
5960
l2Fee := new(big.Int).Mul(l2GasPrice, roundedL2GasLimit)
6061
sum := new(big.Int).Add(l1Fee, l2Fee)
6162
scaled := new(big.Int).Div(sum, bigFeeScalar)
62-
rounded := Ceilmod(scaled, bigHundredMillion)
63+
rounded := Ceilmod(scaled, BigTenThousand)
6364
roundedScaledL2GasLimit := new(big.Int).Div(roundedL2GasLimit, BigTenThousand)
6465
result := new(big.Int).Add(rounded, roundedScaledL2GasLimit)
6566
return result
@@ -81,6 +82,11 @@ func DecodeL2GasLimit(gasLimit *big.Int) *big.Int {
8182
return new(big.Int).Mul(scaled, BigTenThousand)
8283
}
8384

85+
func DecodeL2GasLimitU64(gasLimit uint64) uint64 {
86+
scaled := gasLimit % tenThousand
87+
return scaled * tenThousand
88+
}
89+
8490
// calculateL1GasLimit computes the L1 gasLimit based on the calldata and
8591
// constant sized overhead. The overhead can be decreased as the cost of the
8692
// batch submission goes down via contract optimizations. This will not overflow

packages/core-utils/src/fees.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { BigNumber } from 'ethers'
66
import { remove0x } from './common'
77

88
const hundredMillion = BigNumber.from(100_000_000)
9-
const feeScalar = 1000
9+
const feeScalar = 10_000_000
1010
export const TxGasPrice = BigNumber.from(feeScalar + feeScalar / 2)
1111
const txDataZeroGas = 4
1212
const txDataNonZeroGasEIP2028 = 16
@@ -38,7 +38,7 @@ function encode(input: EncodableL2GasLimit): BigNumber {
3838
const l2Fee = roundedL2GasLimit.mul(l2GasPrice)
3939
const sum = l1Fee.add(l2Fee)
4040
const scaled = sum.div(feeScalar)
41-
const rounded = ceilmod(scaled, hundredMillion)
41+
const rounded = ceilmod(scaled, tenThousand)
4242
const roundedScaledL2GasLimit = roundedL2GasLimit.div(tenThousand)
4343
return rounded.add(roundedScaledL2GasLimit)
4444
}

0 commit comments

Comments
 (0)