Skip to content

Commit

Permalink
use pending nonce from last successful submission
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperFluffy committed Dec 23, 2024
1 parent 8dc382a commit e9152cb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
10 changes: 10 additions & 0 deletions crates/astria-auctioneer/src/auctioneer/inner/auction/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ pub(in crate::auctioneer::inner) struct Factory {
pub(in crate::auctioneer::inner) sequencer_chain_id: String,
pub(in crate::auctioneer::inner) rollup_id: RollupId,
pub(in crate::auctioneer::inner) cancellation_token: CancellationToken,
/// `last_successful_nonce + 1` is used for submitting an auction winner to Sequencer
/// if an auction worker was not able to receive the last pending
/// nonce from Sequencer in time. Starts unset at the beginning of the program and
/// is set externally via Factory::set_last_succesful_nonce`.
pub(in crate::auctioneer::inner) last_successful_nonce: Option<u32>,
}

impl Factory {
Expand Down Expand Up @@ -61,6 +66,7 @@ impl Factory {
sequencer_chain_id: self.sequencer_chain_id.clone(),
rollup_id: self.rollup_id,
cancellation_token: cancellation_token.clone(),
last_successful_nonce: self.last_successful_nonce,
};

Auction {
Expand All @@ -75,4 +81,8 @@ impl Factory {
worker: tokio::task::spawn(auction.run()),
}
}

pub(in crate::auctioneer::inner) fn set_last_successful_nonce(&mut self, nonce: u32) {
self.last_successful_nonce.replace(nonce);
}
}
47 changes: 40 additions & 7 deletions crates/astria-auctioneer/src/auctioneer/inner/auction/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ use crate::{
sequencer_key::SequencerKey,
};

const SUBMISSION_TIMEOUT: Duration = Duration::from_secs(30);

pub(super) struct Worker {
/// The sequencer's ABCI client, used for submitting transactions
pub(super) sequencer_abci_client: sequencer_client::HttpClient,
Expand All @@ -104,6 +106,12 @@ pub(super) struct Worker {
/// Rollup ID to submit the auction result to
pub(super) rollup_id: RollupId,
pub(super) cancellation_token: CancellationToken,
/// `last_successful_nonce + 1` is used for submitting an auction winner to
/// Sequencer if the worker was not able to receive the last pending nonce
/// from Sequencer in time (in time = by the time the winner was ready to be
/// submitted). Is usually only unset if no auction was yet submitted (for example
/// at the beginning of the program).
pub(super) last_successful_nonce: Option<u32>,
}

impl Worker {
Expand Down Expand Up @@ -143,6 +151,17 @@ impl Worker {
source,
});
}
None if self.last_successful_nonce.is_some() => {
let nonce = self
.last_successful_nonce
.expect("in arm that checks for last_successful_nonce == Some")
.saturating_add(1);
info!(
"request for latest pending nonce did not return in time; using `{nonce}`
instead (last successful nonce + 1)"
);
nonce
}
None => return Err(Error::NoNonce),
};

Expand All @@ -165,7 +184,8 @@ impl Worker {
// immediately ("optimistically") submit with the most recent pending nonce (if the
// publisher updated it in the meantime) or just nonce + 1 (if it didn't yet update)?

let submission_fut = submit_winner(self.sequencer_abci_client.clone(), transaction);
let submission_fut =
submit_winner_with_timeout(self.sequencer_abci_client.clone(), transaction);
tokio::pin!(submission_fut);
loop {
select!(
Expand Down Expand Up @@ -268,6 +288,11 @@ pub(in crate::auctioneer::inner) enum Error {
NoNonce,
#[error("task fetching nonce from Sequencer panicked")]
NonceFetchPanicked { source: tokio::task::JoinError },
#[error(
"submission of winner to Sequencer elapsed after {}",
humantime::format_duration(SUBMISSION_TIMEOUT)
)]
SubmissionElapsed { source: tokio::time::error::Elapsed },
#[error("encountered an error when sending the winning bid to Sequencer")]
SubmissionFailed {
source: sequencer_client::extension_trait::Error,
Expand Down Expand Up @@ -332,14 +357,22 @@ async fn get_pending_nonce(sequencer_channel: SequencerChannel, address: Address
}
}

async fn submit_winner(
async fn submit_winner_with_timeout(
client: sequencer_client::HttpClient,
transaction: Transaction,
) -> Result<tx_sync::Response, Error> {
client
.submit_transaction_sync(transaction)
.await
.map_err(|source| Error::SubmissionFailed {
match tokio::time::timeout(
SUBMISSION_TIMEOUT,
client.submit_transaction_sync(transaction),
)
.await
{
Ok(Ok(rsp)) => Ok(rsp),
Ok(Err(source)) => Err(Error::SubmissionFailed {
source,
}),
Err(source) => Err(Error::SubmissionElapsed {
source,
})
}),
}
}
8 changes: 8 additions & 0 deletions crates/astria-auctioneer/src/auctioneer/inner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ impl Inner {
sequencer_chain_id,
rollup_id,
cancellation_token: shutdown_token.child_token(),
last_successful_nonce: None,
};

Ok(Self {
Expand Down Expand Up @@ -185,6 +186,13 @@ impl Inner {
auction_id: auction::Id,
res: Result<auction::Summary, auction::Error>,
) -> Result<auction::Summary, auction::Error> {
if let Ok(auction::Summary::Submitted {
nonce_used, ..
}) = &res
{
self.auction_factory.set_last_successful_nonce(*nonce_used);
}

let _ = self.running_auction.take();
res
}
Expand Down

0 comments on commit e9152cb

Please sign in to comment.