Skip to content

Commit b2377df

Browse files
authored
feat(feynman): compute compression ratio on tx data (#261)
* feat(feynman): compute compression ratio on tx data * update compute compression ratio * clippy * update tests * adjust
1 parent 081f1b4 commit b2377df

File tree

4 files changed

+39
-32
lines changed

4 files changed

+39
-32
lines changed

crates/scroll/alloy/evm/src/tx/compression.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ mod zstd_compression {
4747
/// This is computed as:
4848
/// `max(1, original_size * TX_L1_FEE_PRECISION_U256 / encoded_size)`
4949
pub fn compute_compression_ratio<T: AsRef<[u8]>>(bytes: &T) -> U256 {
50+
// By definition, the compression ratio of empty data is infinity
51+
if bytes.as_ref().is_empty() {
52+
return U256::MAX
53+
}
54+
5055
// Instantiate the compressor
5156
let mut compressor = compressor(N_BLOCK_SIZE_TARGET);
5257

@@ -253,40 +258,45 @@ mod tests {
253258
#[test]
254259
fn test_compute_compression_ratio() -> eyre::Result<()> {
255260
// eth-transfer
256-
// https://scrollscan.com/tx/0x8c7eba9a56e25c4402a1d9fdbe6fbe70e6f6f89484b2e4f5c329258a924193b4
257-
let bytes = bytes!("0x02f86b83082750830461a40183830fb782523f94802b65b5d9016621e66003aed0b16615093f328b8080c001a0a1fa6bbede5ae355eaec83fdcda65eab240476895e649576552850de726596cca0424eb1f5221865817b270d85caf8611d35ea6d7c2e86c9c31af5c06df04a2587");
261+
let bytes = bytes!("0x");
258262
let ratio = compute_compression_ratio(&bytes);
259-
assert_eq!(ratio, U256::from(1_000_000_000u64)); // 1x (not compressible)
263+
assert_eq!(ratio, U256::MAX); // empty data is infinitely compressible by definition
260264

261265
// scr-transfer
262266
// https://scrollscan.com/tx/0x7b681ce914c9774aff364d2b099b2ba41dea44bcd59dbebb9d4c4b6853893179
263-
let bytes = bytes!("0x02f8b28308275001830f4240840279876683015c2894d29687c813d741e2f938f4ac377128810e217b1b80b844a9059cbb000000000000000000000000687b50a70d33d71f9a82dd330b8c091e4d77250800000000000000000000000000000000000000000000000ac96dda943e512bb9c080a0fdacacd07ed7c708e2193b803d731d3d288dcd39c317f321f243cd790406868ba0285444ab799632c88fd47c874c218bceb1589843949b5bc0f3ead1df069f3233");
267+
let bytes = bytes!("0xa9059cbb000000000000000000000000687b50a70d33d71f9a82dd330b8c091e4d77250800000000000000000000000000000000000000000000000ac96dda943e512bb9");
264268
let ratio = compute_compression_ratio(&bytes);
265-
assert_eq!(ratio, U256::from(1_117_283_950u64)); // 1.1x
269+
assert_eq!(ratio, U256::from(1_387_755_102u64)); // 1.4x
266270

267271
// syncswap-swap
268272
// https://scrollscan.com/tx/0x59a7b72503400b6719f3cb670c7b1e7e45ce5076f30b98bdaad3b07a5d0fbc02
269-
let bytes = bytes!("0xf902cf830887d783a57282830493e09480e38291e06339d10aab483c65695d004dbd5c6980b902642cc4081e00000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000005ec79b80000000000000000000000000000000000000000000000000003328b944c400000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006000000000000000000000000053000000000000000000000000000000000000040000000000000000000000000000000000000000000000000091a94863ca800000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000814a23b053fd0f102aeeda0459215c2444799c7000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000600000000000000000000000005300000000000000000000000000000000000004000000000000000000000000485ca81b70255da2fe3fd0814b57d1b08fce784e0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000083104ec3a050db0fbfa3fd83aa9077abdd4edb3dc504661d6fb3b39f973fe994de8fb0ac41a044983fa3d16aa0e156a1b3382fa763f9831be5a5c158f849be524d41d100ab52");
273+
let bytes = bytes!("0x2cc4081e00000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000005ec79b80000000000000000000000000000000000000000000000000003328b944c400000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006000000000000000000000000053000000000000000000000000000000000000040000000000000000000000000000000000000000000000000091a94863ca800000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000814a23b053fd0f102aeeda0459215c2444799c7000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000600000000000000000000000005300000000000000000000000000000000000004000000000000000000000000485ca81b70255da2fe3fd0814b57d1b08fce784e00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000");
270274
let ratio = compute_compression_ratio(&bytes);
271-
assert_eq!(ratio, U256::from(3_059_322_033u64)); // 3.1x
275+
assert_eq!(ratio, U256::from(4_857_142_857u64)); // 4.8x
272276

273277
// uniswap-swap
274278
// https://scrollscan.com/tx/0x65b268bd8ef416f44983ee277d748de044243272b0f106b71ff03cc8501a05da
275-
let bytes = bytes!("0xf9014e830887e0836b92a7830493e094fc30937f5cde93df8d48acaf7e6f5d8d8a31f63680b8e45023b4df00000000000000000000000006efdbff2a14a7c8e15944d1f4a48f9f95f663a4000000000000000000000000530000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000001f4000000000000000000000000485ca81b70255da2fe3fd0814b57d1b08fce784e000000000000000000000000000000000000000000000000006a94d74f43000000000000000000000000000000000000000000000000000000000000045af675000000000000000000000000000000000000000000000000000000000000000083104ec4a0a527358d5bfb89dcc7939265c6add9faf4697415174723e509f795ad44021d98a0776f4a8a8a51da98b70d960a5bd1faf3c79b8dddc0bc2c642a4c2634c6990f02");
279+
let bytes = bytes!("0x5023b4df00000000000000000000000006efdbff2a14a7c8e15944d1f4a48f9f95f663a4000000000000000000000000530000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000001f4000000000000000000000000485ca81b70255da2fe3fd0814b57d1b08fce784e000000000000000000000000000000000000000000000000006a94d74f43000000000000000000000000000000000000000000000000000000000000045af6750000000000000000000000000000000000000000000000000000000000000000");
276280
let ratio = compute_compression_ratio(&bytes);
277-
assert_eq!(ratio, U256::from(1_710_659_898u64)); // 1.7x
281+
assert_eq!(ratio, U256::from(2_620_689_655u64)); // 2.6x
278282

279283
// etherfi-deposit
280284
// https://scrollscan.com/tx/0x41a77736afd54134b6c673e967c9801e326495074012b4033bd557920cbe5a71
281-
let bytes = bytes!("0x02f901d58308275082f88a834c4b4084044c7166831066099462f623161fdb6564925c3f9b783cbdfef4ce8aec80b9016463baa26000000000000000000000000077a7e3215a621a9935d32a046212ebfcffa3bff900000000000000000000000006efdbff2a14a7c8e15944d1f4a48f9f95f663a400000000000000000000000008c6f91e2b681faf5e17227f2a44c307b3c1364c0000000000000000000000000000000000000000000000000000000002d4cae000000000000000000000000000000000000000000000000000000000028f7f83000000000000000000000000249e3fa81d73244f956ecd529715323b6d02f24b00000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000041a95314c3a11f86cc673f2afd60d27f559cb2edcc0da5af030adffc97f9a5edc3314efbadd32878e289017f644a4afa365da5367fefe583f7c4ff0c6047e2c1ff1b00000000000000000000000000000000000000000000000000000000000000c080a05b2d22b8aaf6d334471e74899cfc4c81186f8a94f278c97ee211727d8027ceafa0450352d9a1782180c27a03889d317d31e725dda38a5bd3c0531950b879ed50a1");
285+
let bytes = bytes!("0x63baa26000000000000000000000000077a7e3215a621a9935d32a046212ebfcffa3bff900000000000000000000000006efdbff2a14a7c8e15944d1f4a48f9f95f663a400000000000000000000000008c6f91e2b681faf5e17227f2a44c307b3c1364c0000000000000000000000000000000000000000000000000000000002d4cae000000000000000000000000000000000000000000000000000000000028f7f83000000000000000000000000249e3fa81d73244f956ecd529715323b6d02f24b00000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000041a95314c3a11f86cc673f2afd60d27f559cb2edcc0da5af030adffc97f9a5edc3314efbadd32878e289017f644a4afa365da5367fefe583f7c4ff0c6047e2c1ff1b00000000000000000000000000000000000000000000000000000000000000");
282286
let ratio = compute_compression_ratio(&bytes);
283-
assert_eq!(ratio, U256::from(1_496_835_443u64)); // 1.4x
287+
assert_eq!(ratio, U256::from(1_788_944_723u64)); // 1.8x
284288

285289
// edgepushoracle-postupdate
286290
// https://scrollscan.com/tx/0x8271c68146a3b07b1ebf52ce0b550751f49cbd72fa0596ef14ff56d1f23a0bec
287-
let bytes = bytes!("0xf9046f83015e4c836e0b6b8303cbf8946a5b3ab3274b738eab25205af6e2d4dd7781292480b9040449a1a4fb000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000005f725f60000000000000000000000000000000000000000000000000000000003d0cac600000000000000000000000000000000000000000000000000000000685d50cd000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000004155903b95865fc5a5dd7d4d876456140dd0b815695647fc41eb1924f4cfe267265130b5a5d77125c44cf6a5a81edba6d5850ba00f90ab83281c9b44e17528fd74010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000416f000e0498733998e6a1a6454e116c1b1f95f7e000400b6a54029406cf288bdc615b62de8e2db533d6010ca57001e0b8a4b3f05ed516a31830516c52b9df206e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000410dabc77a807d729ff62c3be740d492d884f026ad2770fa7c4bdec569e201643656b07f2009d2129173738571417734a3df051cebc7b8233bec6d9471c21c098700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041eb009614c939170e9ff3d3e06c3a2c45810fe46a364ce28ecec5e220f5fd86cd6e0f70ab9093dd6b22b69980246496b600c8fcb054047962d4128efa48b692f301000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041a31b4dd4f0a482372d75c7a8c5f11aa8084a5f358579866f1d25a26a15beb2b5153400bfa7fa3d6fba138c02dd1eb8a5a97d62178d98c5632a153396a566e5ed000000000000000000000000000000000000000000000000000000000000000083104ec4a05cb4eee77676d432c672008594825b957d34ae5dd786ed294501849bb1ce285aa01325f37cdc945863ec0474932102bc944cb98a663db6d30ae23c2ebb5f9ce070");
291+
let bytes = bytes!("0x49a1a4fb000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000005f725f60000000000000000000000000000000000000000000000000000000003d0cac600000000000000000000000000000000000000000000000000000000685d50cd000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000004155903b95865fc5a5dd7d4d876456140dd0b815695647fc41eb1924f4cfe267265130b5a5d77125c44cf6a5a81edba6d5850ba00f90ab83281c9b44e17528fd74010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000416f000e0498733998e6a1a6454e116c1b1f95f7e000400b6a54029406cf288bdc615b62de8e2db533d6010ca57001e0b8a4b3f05ed516a31830516c52b9df206e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000410dabc77a807d729ff62c3be740d492d884f026ad2770fa7c4bdec569e201643656b07f2009d2129173738571417734a3df051cebc7b8233bec6d9471c21c098700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041eb009614c939170e9ff3d3e06c3a2c45810fe46a364ce28ecec5e220f5fd86cd6e0f70ab9093dd6b22b69980246496b600c8fcb054047962d4128efa48b692f301000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041a31b4dd4f0a482372d75c7a8c5f11aa8084a5f358579866f1d25a26a15beb2b5153400bfa7fa3d6fba138c02dd1eb8a5a97d62178d98c5632a153396a566e5ed0000000000000000000000000000000000000000000000000000000000000000");
292+
let ratio = compute_compression_ratio(&bytes);
293+
assert_eq!(ratio, U256::from(2_441_805_225u64)); // 2.4x
294+
295+
// intmax-post
296+
// https://scrollscan.com/tx/0x7244e27223cdd79ba0f0e3990c746e5d524e35dbcc200f0a7e664ffdc6d08eef
297+
let bytes = bytes!("0x9b6babf0f0372bb253e060ecbdd3dbef8b832b0e743148bd807bfcf665593a56a18bac69000000000000000000000000000000000000000000000000000000006861676d0000000000000000000000000000000000000000000000000000000000000015800000000000000000000000000000000000000000000000000000000000000029a690c4ef1e18884a11f73c8595fb721f964a3e2bee809800c474278f024bcd05a76119827e6c464cee8620f616a9a23d41305eb9f9682f9d2eaf964325fcd71147783453566f27ce103a2398d96719ee22ba51b89b92cdf952af817929329403b75ae310b23cf250041d53c82bef431fa2527e2dd68b49f45f06feb2bd09f011358fe2650b8987ea2bb39bb6e28ce770f4fc9c4f064d0ae7573a1450452b501a5b0d3454d254dbf9db7094f4ca1f5056143f5c70dee4126443a6150d9e51bd05dac7e9a2bd48a8797ac6e9379d400c5ce1815b10846eaf0d80dca3a727ffd0075387e0f1bc1b363c81ecf8d05a4b654ac6fbe1cdc7c741a5c0bbeabde4138906009129ca033af12094fd7306562d9735b2fe757f021b7eb3320f8a814a286a10130969de2783e49871b80e967cfba630e6bdef2fd1d2b1076c6c3f5fd9ae5800000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000000012a98f1556efe81340fad3e59044b8139ce62f1d5d50b44b680de9422b1ddbf1a");
288298
let ratio = compute_compression_ratio(&bytes);
289-
assert_eq!(ratio, U256::from(2_139_097_744u64)); // 2.1x
299+
assert_eq!(ratio, U256::from(1_298_578_199u64)); // 1.3x
290300

291301
Ok(())
292302
}

crates/scroll/alloy/evm/src/tx/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,9 @@ impl FromTxWithEncoded<ScrollTxEnvelope> for ScrollTransactionIntoTxEnv<TxEnv> {
160160
};
161161

162162
let encoded = (!tx.is_l1_message()).then_some(encoded);
163-
let compression_ratio = encoded.as_ref().map(compute_compression_ratio);
164-
Self::new(base, encoded, compression_ratio)
163+
// Note: We compute the transaction ratio on tx.data, not on the full encoded transaction.
164+
let compression_ratio = compute_compression_ratio(base.input());
165+
Self::new(base, encoded, Some(compression_ratio))
165166
}
166167
}
167168

@@ -267,8 +268,9 @@ impl FromRecoveredTx<ScrollTxEnvelope> for ScrollTransactionIntoTxEnv<TxEnv> {
267268
},
268269
};
269270

270-
let rlp_bytes = (!tx.is_l1_message()).then_some(envelope.into());
271-
let compression_ratio = rlp_bytes.as_ref().map(compute_compression_ratio);
272-
Self::new(base, rlp_bytes, compression_ratio)
271+
let encoded = (!tx.is_l1_message()).then_some(envelope.into());
272+
// Note: We compute the transaction ratio on tx.data, not on the full encoded transaction.
273+
let compression_ratio = compute_compression_ratio(base.input());
274+
Self::new(base, encoded, Some(compression_ratio))
273275
}
274276
}

crates/scroll/evm/src/execute.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ mod tests {
4646

4747
use alloy_consensus::{
4848
transaction::{Recovered, SignerRecoverable},
49-
Block, BlockBody, Header, SignableTransaction, Signed, TxLegacy,
49+
Block, BlockBody, Header, SignableTransaction, Signed, Transaction, TxLegacy,
5050
};
5151
use alloy_eips::{
5252
eip7702::{constants::PER_EMPTY_ACCOUNT_COST, Authorization, SignedAuthorization},
53-
Encodable2718, Typed2718,
53+
Typed2718,
5454
};
5555
use alloy_evm::{
5656
block::{BlockExecutionResult, BlockExecutor},
@@ -511,7 +511,7 @@ mod tests {
511511
#[test]
512512
fn test_execute_transactions_legacy_feynman_fork() -> eyre::Result<()> {
513513
// Execute legacy transaction on feynman block
514-
let expected_l1_fee = U256::from(100);
514+
let expected_l1_fee = U256::from(10);
515515
execute_transaction(
516516
ScrollTxType::Legacy,
517517
CURIE_BLOCK_NUMBER + 1,
@@ -546,7 +546,7 @@ mod tests {
546546
#[test]
547547
fn test_execute_transactions_eip2930_feynman_fork() -> eyre::Result<()> {
548548
// Execute eip2930 transaction on feynman block
549-
let expected_l1_fee = U256::from(103);
549+
let expected_l1_fee = U256::from(10);
550550
execute_transaction(
551551
ScrollTxType::Eip2930,
552552
CURIE_BLOCK_NUMBER + 1,
@@ -581,7 +581,7 @@ mod tests {
581581
#[test]
582582
fn test_execute_transaction_eip1559_feynman_fork() -> eyre::Result<()> {
583583
// Execute eip1559 transaction on feynman block
584-
let expected_l1_fee = U256::from(104);
584+
let expected_l1_fee = U256::from(10);
585585
execute_transaction(
586586
ScrollTxType::Eip1559,
587587
CURIE_BLOCK_NUMBER + 1,
@@ -622,7 +622,7 @@ mod tests {
622622
#[test]
623623
fn test_execute_transactions_eip7702_feynman_fork() -> eyre::Result<()> {
624624
// Execute eip7702 transaction on feynman block
625-
let expected_l1_fee = U256::from(198);
625+
let expected_l1_fee = U256::from(19);
626626
execute_transaction(
627627
ScrollTxType::Eip7702,
628628
CURIE_BLOCK_NUMBER + 1,
@@ -641,13 +641,8 @@ mod tests {
641641
transaction(ScrollTxType::Eip1559, MIN_TRANSACTION_GAS),
642642
transaction(ScrollTxType::Eip7702, MIN_TRANSACTION_GAS),
643643
];
644-
let compression_ratios = transactions
645-
.iter()
646-
.map(|tx| {
647-
let encoded = tx.encoded_2718();
648-
compute_compression_ratio(&encoded)
649-
})
650-
.collect::<Vec<_>>();
644+
let compression_ratios =
645+
transactions.iter().map(|tx| compute_compression_ratio(tx.input())).collect::<Vec<_>>();
651646
let with_compression_ratios = execute_block(
652647
transactions.clone(),
653648
CURIE_BLOCK_NUMBER + 1,

crates/scroll/txpool/src/validator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ where
166166
let mut encoded = Vec::with_capacity(valid_tx.transaction().encoded_length());
167167
let tx = valid_tx.transaction().clone_into_consensus();
168168
tx.encode_2718(&mut encoded);
169-
let compression_ratio = compute_compression_ratio(&encoded);
169+
let compression_ratio = compute_compression_ratio(valid_tx.transaction().input());
170170

171171
let cost_addition = match l1_block_info.l1_tx_data_fee(
172172
self.chain_spec(),

0 commit comments

Comments
 (0)