mantlenetworkio / mantle

Mantle | Mass adoption of decentralized & token-governed technologies. With Mantle Network, Mantle Treasury, and token holder-governed products initiatives.
https://www.mantle.xyz
MIT License
227 stars 101 forks source link

[batch-submitter]: gas optimization / Change groupedBlocks array from many to one element for gas optimization #1360

Open guoshijiang opened 1 year ago

guoshijiang commented 1 year ago

Simplify groupedBlocks array to include only one element to reduce gas consumption.

Current calldata of CTC appendSequencerBatch:

0xd0f8934400003675710005910000f1000000000000000000000000000000000000030000000064edbe71000112f4500000090000000064edbe71000112f4510000080000000064edbe76000112f4510000050000000064edbe7b000112f4510000050000000064edbe7b000112f4520000090000000064edbe80000112f45200000a0000000064edbe85000112f4520000010000000064edbe8a000112f4520000040000000064edbe8a000112f45300000a0000000064edbe8f000112f4530000070000000064edbe94000112f4530000030000000064edbe94000112f4540000090000000064edbe99000112f4540000090000000064edbe9e000112f45400000b0000000064edbea4000112f4550000120000000064edbeaa000112f4550000080000000064edbeaf000112f4560000060000000064edbeb6000112f4560000010000000064edbeb6000112f4570000060000000064edbebb000112f4570000040000000064edbec0000112f4570000010000000064edbec7000112f4570000040000000064edbec7000112f4580000080000000064edbecc000112f4580000050000000064edbed1000112f45900000d0000000064edbed6000112f4590000090000000064edbedb000112f4590000080000000064edbee1000112f45a00000b0000000064edbee7000112f45a0000020000000064edbee7000112f45b0000080000000064edbeec000112f45b0000060000000064edbef2000112f45b0000040000000064edbef7000112f45b0000050000000064edbef7000112f45c00000a0000000064edbefc000112f45c0000090000000064edbf01000112f45c0000030000000064edbf01000112f45d00000b0000000064edbf06000112f45d0000030000000064edbf0b000112f45d0000010000000064edbf0b000112f45e0000090000000064edbf10000112f45e0000060000000064edbf16000112f45e0000050000000064edbf1b000112f45f0000070000000064edbf20000112f45f0000010000000064edbf26000112f45f0000040000000064edbf26000112f4600000060000000064edbf2b000112f4600000020000000064edbf31000112f4600000020000000064edbf31000112f4610000080000000064edbf36000112f4610000070000000064edbf3b000112f4610000060000000064edbf41000112f4620000060000000064edbf46000112f4620000010000000064edbf4b000112f4620000040000000064edbf4b000112f4630000070000000064edbf50000112f4630000020000000064edbf55000112f4630000030000000064edbf55000112f4640000070000000064edbf5a000112f4640000080000000064edbf5f000112f4640000060000000064edbf64000112f4650000090000000064edbf69000112f4650000010000000064edbf6e000112f4650000030000000064edbf6e000112f46600000b0000000064edbf74000112f4660000030000000064edbf7a000112f4660000070000000064edbf7a000112f4670000090000000064edbf80000112f4670000020000000064edbf85000112f4670000050000000064edbf85000112f4680000070000000064edbf8a000112f4680000050000000064edbf8f000112f46800000a0000000064edbf96000112f4690000080000000064edbf9b000112f4690000010000000064edbfa0000112f4690000090000000064edbfa0000112f46a0000040000000064edbfa5000112f46a0000010000000064edbfaa000112f46a0000080000000064edbfaa000112f46b0000090000000064edbfaf000112f46b0000050000000064edbfb4000112f46b0000040000000064edbfb4000112f46c0000090000000064edbfb9000112f46c0000040000000064edbfbe000112f46c0000020000000064edbfbe000112f46d00000c0000000064edbfc3000112f46d0000040000000064edbfc8000112f46d0000050000000064edbfcd000112f46d0000060000000064edbfd2000112f46e0000040000000064edbfd7000112f46e0000050000000064edbfdc000112f46f0000050000000064edbfe1000112f46f0000030000000064edbfe6000112f46f0000070000000064edbfe6000112f4700000060000000064edbfeb000112f4700000070000000064edbff0000112f4700000010000000064edbff0000112f4710000020000000064edbff5000112f4710000050000000064edbffa000112f4710000070000000064edbfff000112f47200000a0000000064edc004000112f4720000010000000064edc009000112f4720000050000000064edc009000112f4730000090000000064edc00e000112f4730000070000000064edc013000112f4730000040000000064edc013000112f4740000070000000064edc018000112f47400000a0000000064edc01d000112f4740000010000000064edc022000112f4740000040000000064edc022000112f47500000b0000000064edc027000112f4750000060000000064edc02c000112f4750000010000000064edc02c000112f4760000070000000064edc032000112f4760000020000000064edc038000112f4760000050000000064edc038000112f4770000040000000064edc03d000112f4770000080000000064edc042000112f4770000030000000064edc048000112f4770000060000000064edc048000112f4780000040000000064edc04d000112f4780000020000000064edc052000112f4780000070000000064edc052000112f4790000080000000064edc057000112f4790000010000000064edc05d000112f4790000060000000064edc05d000112f47a00000c0000000064edc062000112f47a0000090000000064edc067000112f47a0000080000000064edc06c000112f47b0000080000000064edc071000112f47b0000040000000064edc076000112f47c0000110000000064edc07b000112f47c0000050000000064edc080000112f47c0000010000000064edc080000112f47d0000100000000064edc085000112f47d0000070000000064edc08a000112f47d0000060000000064edc08a000112f47e00000a0000000064edc08f000112f47e00000b0000000064edc094000112f47e0000060000010064edc099000112f47e0000030000000064edc099000112f47f0000040000000064edc09f000112f47f0000040000000064edc0a5000112f47f0000040000000064edc0a5000112f48000000a0000000064edc0aa000112f4800000040000000064edc0af000112f4800000040000000064edc0af000112f4810000040000000064edc0b4000112f48100000c0000000064edc0b9000112f48100000c0000000064edc0bf000112f4820000060000000064edc0c4000112f4820000090000000064edc0c9000112f48200000b0000000064edc0c9000112f48300000c0000000064edc0ce000112f4830000030000000064edc0d3000112f4830000050000000064edc0d3000112f48400000a0000000064edc0db000112f4840000010000000064edc0e0000112f4840000050000000064edc0e0000112f48500000b0000000064edc0e5000112f48500000a0000000064edc0ea000112f4850000070000000064edc0f0000112f4860000080000000064edc0f5000112f4860000030000000064edc0fa000112f4860000050000000064edc0fa000112f48700000c0000000064edc0ff000112f4870000040000000064edc104000112f4870000070000000064edc104000112f48800000b0000000064edc109000112f4880000060000000064edc10e000112f4880000050000000064edc114000112f4890000040000000064edc119000112f4890000070000000064edc120000112f48a0000080000000064edc125000112f48a0000040000000064edc12a000112f48a0000010000000064edc12a000112f48b0000070000000064edc130000112f48b0000020000000064edc135000112f48b0000060000000064edc135000112f48c0000060000000064edc13a000112f48c0000060000000064edc13f000112f48c0000010000000064edc144000112f48c0000060000000064edc144000112f48d0000070000000064edc149000112f48d0000050000000064edc14f000112f48e0000050000000064edc154000112f48e0000020000000064edc15a000112f48e0000040000000064edc15a000112f48f0000070000000064edc15f000112f48f0000060000000064edc164000112f48f0000040000000064edc164000112f49000000b0000000064edc169000112f4900000070000000064edc16e000112f4900000010000000064edc173000112f4900000050000000064edc173000112f4910000060000000064edc178000112f4910000040000000064edc17d000112f4910000070000000064edc17d000112f49200000a0000000064edc182000112f4920000090000000064edc187000112f4920000010000000064edc18c000112f4920000030000010064edc18c000112f4930000020000000064edc18c000112f4930000080000000064edc191000112f4930000030000000064edc196000112f49300000a0000000064edc196000112f49400000c0000000064edc19d000112f4940000030000000064edc1a2000112f4940000050000000064edc1a9000112f4950000070000000064edc1b0000112f4960000050000000064edc1b5000112f4960000010000000064edc1bb000112f4960000080000000064edc1bb000112f4970000060000000064edc1c0000112f4970000010000000064edc1c6000112f4970000030000000064edc1c6000112f4980000060000000064edc1cb000112f4980000030000000064edc1d0000112f4980000080000000064edc1d0000112f4990000060000000064edc1d5000112f4990000090000000064edc1db000112f4990000020000000064edc1db000112f49a0000030000000064edc1e0000112f49a0000030000000064edc1e5000112f49a0000020000000064edc1ea000112f49a0000030000000064edc1ea000112f49b0000050000000064edc1f0000112f49b0000010000000064edc1f5000112f49b0000040000000064edc1f5000112f49c0000090000000064edc1fa000112f49c0000080000000064edc1ff000112f49c0000060000000064edc204000112f49d00000a0000000064edc209000112f49d0000080000000064edc20f000112f49e00000b0000000064edc216000112f49e0000040000000064edc21b000112f49e0000060000000064edc21b000112f49f0000080000000064edc220000112f49f0000010000000064edc225000112f49f0000010000000064edc225000112f4a0

The above huge calldata includes a context array. And there is an array iteration in appendSequencerBatch.

        for (uint32 i = 0; i < numContexts; i++) {
            BatchContext memory nextContext = _getBatchContext(i);

            // Now we can update our current context.
            curContext = nextContext;

            // Process sequencer transactions first.
            numSequencerTransactions += uint32(curContext.numSequencedTransactions);

            // Now process any subsequent queue transactions.
            nextQueueIndex += uint40(curContext.numSubsequentQueueTransactions);
        }

If it can be optimized into an array with only one element, the later rollup transactions will be much smaller than current rollup transactions and expensive array iteration in smart contract will be avoided, which might significantly reduce rollup gas consumption.

According to our analysis, after optimization, each batch rollup transaction will reduce 1/18 to 1/6 gas consumption