Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: avoiding posting during price surges #179

Open
wants to merge 138 commits into
base: master
Choose a base branch
from

Conversation

segfault-magnet
Copy link
Contributor

Closes: #158
Added a simple algorithm to help prevent posting when eth transaction fees are high.

You can visualize how it works by using the simulator:

cargo run --release --bin fee_algo_simulation   

simulator_graph

The simulator lets you see how much a transaction with N_BLOBS would have cost at different times in the past. The blob transaction fee is calculated like this:

21,000 * (base_fee + reward) + N_BLOBS * BLOB_SIZE * base_blob_fee

You’ll see three fee values: the current fee, the short-term fee, and the long-term fee.

Right now, “short-term” is set to the last 5 minutes, and “long-term” covers the past 3 hours, but you can adjust these settings.

We decide it’s a good time to send a transaction when the short_term_fee drops below long_term_fee * max_fee_multiplier. In the simulator, this is shown with green shaded areas.

The max_fee_multiplier starts at start_max_fee_multiplier (eg. 0.8) and can go up to end_max_fee_multiplier (e.g. 1.7). As we fall behind on posting L2 blocks, the multiplier linearly increases towards the maximum (when we've reached max_l2_blocks_behind).

If the multiplier hits the max value, the algorithm turns off, and we’ll accept any fee.

On the flip side, if the fee goes below always_acceptable_fee, we’ll disable the algorithm because it’s better to stay up-to-date than to save just a tiny bit of eth.

All of the above-mentioned values can be tweaked in the simulator:
tweaking

This feature has been running on the testnet and is showing promising results, as you can see from the grafana graphs below:

testnet_graphana

At around 6:10 the prices start to rise, we wait them out and continue posting after they settle back down.

This PR also keeps track of how many L2 blocks we’re behind. This helps both for monitoring and for the algorithm itself. It’s calculated by:

  • Determining the starting height of the oldest bundle we should post to L1
  • If we’re not posting anything (meaning a bundle is still being optimized), we use the last bundled height plus one.

@@ -1,332 +1,6 @@
pub mod service {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to a separate file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing changed

@segfault-magnet
Copy link
Contributor Author

segfault-magnet commented Jan 6, 2025

Deployed to mainnet, here is today's price surge
works

We waited it out and posted the accumulated 13 tx (w 6 blobs a piece) after prices settled for a total of 0.105 ETH.

hal3e
hal3e previously approved these changes Jan 9, 2025
Copy link
Contributor

@hal3e hal3e left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work. 🚀

let missing_fees = self.download_missing_fees(&fees, height_range).await?;

self.update_cache(&mut cache_guard, missing_fees.clone());
drop(cache_guard);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just caught my eye. Is there a specific reason to drop it manually ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to drop it ASAP, this is the earliest it can die without needing to wait until the end.

We don't need to hold it during the sorting or validating below.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
big enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve detection of blob fee spikes and adaptive posting
3 participants