alchemyplatform / rundler

An ERC-4337 Bundler in Rust
GNU Lesser General Public License v3.0
247 stars 30 forks source link

[pool] Track used nonces #496

Open dancoombs opened 7 months ago

dancoombs commented 7 months ago

Describe the feature Today our mempool has the following race condition:

Notation: [(Sender, Nonce Key, Nonce Value), Hash]

  1. [(A0, K0, V0), H0] sent to mempool
  2. [(A0, K0, V0), H0] bundled and bundle transaction is sent
  3. [(A0, K0, V0), H1]` sent to mempool (replacement)
  4. H1 is simulated and simulation passes
  5. H0 is mined, and mempool is updated
  6. H1 is added to the mempool

The race condition is between steps 4-6. Since the mempool object doesn't do nonce tracking, we rely on simulation to do the nonce check. However, its possible that between the time that simulation passes and the UO is added to the pool, that the "replaced" UO gets mined and the nonce is no longer valid. If the pool update happens before the "replacer" UO gets added to the pool, the "replacer" UO will be added to the pool and won't be subsequently removed.

The "replacer" UO will eventually be removed when the builder tries to bundle it and re-runs simulation. However, this is inefficient. We can prevent this by adding nonce tracking to the pool.

The pool can track a map of ((Sender, Nonce Key), Value) , with some sort of bound on size/time. The map can be used to prevent against this race condition, and keys can be dropped after a certain number of blocks. This map may be useful in other nonce tracking use cases.

Additional context Related: https://github.com/alchemyplatform/rundler/pull/493 https://github.com/alchemyplatform/rundler/issues/250 https://github.com/alchemyplatform/rundler/issues/75

github-actions[bot] commented 1 month ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] commented 2 weeks ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.