Skip to content

Commit 4c1dc4d

Browse files
committed
Estimate compression ratio
1 parent 49574cb commit 4c1dc4d

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

rollup/fees/rollup_fee.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package fees
22

33
import (
44
"bytes"
5+
"github.com/scroll-tech/da-codec/encoding/zstd"
6+
"github.com/scroll-tech/go-ethereum/log"
57
"math"
68
"math/big"
79

@@ -203,11 +205,22 @@ func calculateEncodedRollupFeeFeeFeynman(
203205
execScalar *big.Int,
204206
blobScalar *big.Int,
205207
) *big.Int {
206-
// tx size (RLP-encoded)
207208
txSize := big.NewInt(int64(len(data)))
208209

209-
// compression_ratio(tx) = 1 (placeholder, scaled to match scalars precision)
210-
compressionRatio := big.NewInt(rcfg.Precision.Int64())
210+
// Default compression ratio is 1.0 (no compression)
211+
compressionRatioInt := big.NewInt(rcfg.Precision.Int64())
212+
213+
if len(data) != 0 {
214+
compressedBytes, err := zstd.CompressScrollBatchBytes(data)
215+
if err != nil {
216+
log.Error("Compress batch compress failed, using 1.0", "err", err)
217+
} else {
218+
compressedSize := big.NewInt(int64(len(compressedBytes)))
219+
// compressionRatioInt = (compressedSize * precision) / txSize
220+
compressionRatioInt.Mul(compressedSize, rcfg.Precision)
221+
compressionRatioInt.Div(compressionRatioInt, txSize)
222+
}
223+
}
211224

212225
// compute gas components
213226
execGas := new(big.Int).Mul(execScalar, l1BaseFee)
@@ -217,7 +230,7 @@ func calculateEncodedRollupFeeFeeFeynman(
217230
feePerByte := new(big.Int).Add(execGas, blobGas)
218231

219232
// rollupFee = compression_ratio * tx_size * feePerByte
220-
rollupFee := new(big.Int).Mul(compressionRatio, txSize)
233+
rollupFee := new(big.Int).Mul(compressionRatioInt, txSize)
221234
rollupFee.Mul(rollupFee, feePerByte)
222235

223236
// Divide by rcfg.Precision (once for ratio, once for scalar)

0 commit comments

Comments
 (0)