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

Bump gas price based on elapsed blocks for cancelling #3302

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
20 changes: 17 additions & 3 deletions crates/driver/src/domain/mempools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ use {
anyhow::Context,
ethrpc::block_stream::into_stream,
futures::{FutureExt, StreamExt, future::select_ok},
std::ops::Sub,
thiserror::Error,
tracing::Instrument,
web3::types::BlockNumber,
};

/// Factor by how much a transaction fee needs to be increased to override a
Expand Down Expand Up @@ -150,10 +152,12 @@ impl Mempools {
})
}
TxStatus::Pending => {
let blocks_elapsed = block.number.sub(submitted_at_block);

// Check if the current block reached the submission deadline block number
if block.number >= submission_deadline {
let cancellation_tx_hash = self
.cancel(mempool, settlement.gas.price, solver)
.cancel(mempool, settlement.gas.price, solver, blocks_elapsed)
.await
.context("cancellation tx due to deadline failed")?;
tracing::info!(
Expand All @@ -173,7 +177,7 @@ impl Mempools {
if let Err(err) = self.ethereum.estimate_gas(tx).await {
if err.is_revert() {
let cancellation_tx_hash = self
.cancel(mempool, settlement.gas.price, solver)
.cancel(mempool, settlement.gas.price, solver, blocks_elapsed)
.await
.context("cancellation tx due to revert failed")?;
tracing::info!(
Expand Down Expand Up @@ -214,7 +218,9 @@ impl Mempools {
mempool: &infra::mempool::Mempool,
pending: eth::GasPrice,
solver: &Solver,
blocks_elapsed: u64,
) -> Result<TxId, Error> {
let gas_price_bump_factor = GAS_PRICE_BUMP.powf(blocks_elapsed.max(1) as f64);
Copy link
Contributor

Choose a reason for hiding this comment

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

According to the issue #3241 description I think we need to just multiply the GAS_PRICE_BUMP by blocks_elapsed.

Copy link
Author

Choose a reason for hiding this comment

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

Oops, yeah 😅

Copy link
Author

Choose a reason for hiding this comment

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

Thanks.. Fixed it!

Copy link
Contributor

Choose a reason for hiding this comment

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

@3shv your original version was actually correct (with the exception of using .powf() instead of powi()). Perhaps the issue was worded unfortunately.
The reason is that according to EIP-1559 the base fee can increase at most 12.5% from block n to n+1. So if we want to get the highest possible gas price after 3 blocks we would compute:
originalGasPrice * 1.125 * 1.125 * 1.125 or originalGasPrice * 1.125.powi(3).

Copy link
Author

Choose a reason for hiding this comment

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

oh, that's interesting.
Thanks for the clarification. Learning new stuff everyday 😇

let cancellation = eth::Tx {
from: solver.address(),
to: solver.address(),
Expand All @@ -225,8 +231,16 @@ impl Mempools {
let gas = competition::solution::settlement::Gas {
estimate: CANCELLATION_GAS_AMOUNT.into(),
limit: CANCELLATION_GAS_AMOUNT.into(),
price: pending * GAS_PRICE_BUMP,
price: pending * gas_price_bump_factor,
};
tracing::debug!(
blocks_elapsed = ?blocks_elapsed,
original_gas_price = ?pending,
new_gas_price = ?(pending * gas_price_bump_factor),
bump_factor = ?gas_price_bump_factor,
"Cancelling transaction with adjusted gas price"
);

mempool.submit(cancellation, gas, solver).await
}
}
Expand Down