taiko-mono icon indicating copy to clipboard operation
taiko-mono copied to clipboard

Protocol: Optimization suggestions for ETH deposits from L1 to L2

Open dantaik opened this issue 1 year ago • 3 comments

The PR for enabling ETH deposits from L1 to L2 via block header's withdrawalsRoot will be merged soon. However, there are two potential optimizations that can be implemented to improve the efficiency of the deposit process and save gas costs:

  1. Use a ring buffer instead of an ever-growing array (EthDeposit[] ethDeposits) to reuse state slots.
  2. Use a lookup from address to EthDeposit to group multiple deposits from the same address together.

Ring buffer

Instead of using an ever-growing array to store ETH deposits, we can implement a ring buffer to reuse state slots and save gas costs. This approach involves creating an array with a fixed size and wrapping around to the beginning of the array once the end is reached. By doing so, we can avoid the need to constantly resize the array and allocate new storage slots. BTW, L2 proposed blocks are already using ring buffer, you can take a look at its implementation.

Lookup table

To group multiple deposits from the same address together, we can implement a lookup table that maps addresses to their corresponding deposit objects. This will allow us to easily retrieve and update the deposit object for a given address when a new deposit is made.

Here's an example implementation of the lookup table:

mapping(address => uint64 depositId) ethDepositLookup;

When a user makes a deposit, we can check if ethDepositLookup returns a valid depositId for the user's address. If so, we simply add the new deposit amount to the existing deposit amount. If not, we create a new deposit object and add it to the lookup table.

When a deposit is processed, the corresponding lookup entry must be deleted or set to a non-zero value to be reused.

Call for help

This is a great first-issue for anyone who would like to contribute to our protocol development. If you're interested in helping out, please feel free to submit a pull request with your proposed changes.

dantaik avatar Apr 18 '23 05:04 dantaik

Hi, @dantaik can you assign this issue to me. i would like to work on it.

deepaksing avatar Apr 20 '23 06:04 deepaksing

Please think more. These ideas may turn out to have some downside :)

dantaik avatar Apr 26 '23 14:04 dantaik

@dantaik can I implement the lookup table?

Venoox avatar Jun 05 '23 07:06 Venoox

👍👌👍

rmzndmr avatar Jul 14 '23 19:07 rmzndmr