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(composer)!: Add chain_id check on executor build #1175

Merged
merged 38 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
ee76511
feat(composer): check chain_id against sequencer_chain_id
eoroshiba May 31, 2024
6259c3a
feat(composer): check sequencer_chain_id against client chain_id
eoroshiba Jun 4, 2024
451dec2
Merge from upstream
eoroshiba Jun 4, 2024
89f03ab
Merge branch 'main' into eoroshiba
eoroshiba Jun 4, 2024
58d47b2
Swapped Genesis for Sequencer Client Status
eoroshiba Jun 5, 2024
c020f7e
Added chain_id check on composer startup. Formatted.
eoroshiba Jun 7, 2024
406a97a
Added chain_id check on composer startup
eoroshiba Jun 7, 2024
31ad1e9
minor syntax changes, added comments for test cases
eoroshiba Jun 7, 2024
39c9be1
Merge remote-tracking branch 'upstream' into eoroshiba
eoroshiba Jun 7, 2024
e915b8f
merge upstream changes
eoroshiba Jun 7, 2024
abf0144
minor updates
eoroshiba Jun 10, 2024
4923b8f
merge from upstream
eoroshiba Jun 10, 2024
42250cb
minor changes
eoroshiba Jun 10, 2024
5382a75
Merge branch 'main' of https://github.com/eoroshiba/astria into eoros…
eoroshiba Jun 13, 2024
edf423e
cargo format
eoroshiba Jun 13, 2024
7f1d9a1
make build function sync, remove status mock guard, ad-hoc json creation
eoroshiba Jun 13, 2024
b69f7a7
move response json inside mount, move chain_id check out of run_until…
eoroshiba Jun 13, 2024
f3c96ff
use tendermint genesis, move validation to executor, other minor changes
eoroshiba Jun 15, 2024
f7201de
Merge branch 'main' into eoroshiba
eoroshiba Jun 15, 2024
24e4b17
minor changes
eoroshiba Jun 15, 2024
aef7fb1
Merge branch 'eoroshiba' of https://github.com/eoroshiba/astria into …
eoroshiba Jun 15, 2024
b8d148f
minor changes
eoroshiba Jun 25, 2024
7a26d15
Merge remote-tracking branch 'upstream' into eoroshiba
eoroshiba Jun 25, 2024
be387af
minor changes
eoroshiba Jun 25, 2024
e3f13d7
Merge branch 'main' into eoroshiba
eoroshiba Jun 27, 2024
531bb4e
Merge remote-tracking branch 'upstream' into eoroshiba
eoroshiba Jul 1, 2024
d3c638c
Merge branch 'eoroshiba' of https://github.com/eoroshiba/astria into …
eoroshiba Jul 1, 2024
8523e04
Streamline chain_id mismatch implementation and testing
eoroshiba Jul 1, 2024
f5c9fd4
Suggested change from @SuperFluffy
eoroshiba Jul 1, 2024
03d14dc
Add helper function for asserting chain ID error
eoroshiba Jul 1, 2024
b853f2a
Merge branch 'eoroshiba' of https://github.com/eoroshiba/astria into …
eoroshiba Jul 1, 2024
33ae83d
minor improvements requested by @superfluffy
eoroshiba Jul 2, 2024
7d1a2e9
Merge branch 'main' into eoroshiba
eoroshiba Jul 2, 2024
4d2ebb7
minor update for clippy
eoroshiba Jul 3, 2024
2f86488
Merge branch 'eoroshiba' of https://github.com/eoroshiba/astria into …
eoroshiba Jul 3, 2024
0a931f5
@superfluffy suggestion
eoroshiba Jul 3, 2024
01c938e
Merge branch 'astriaorg:main' into eoroshiba
eoroshiba Jul 7, 2024
c23101b
Merge branch 'main' into eoroshiba
ethanoroshiba Jul 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 82 additions & 4 deletions crates/astria-composer/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,21 @@ use futures::{
use pin_project_lite::pin_project;
use prost::Message as _;
use sequencer_client::{
tendermint_rpc::endpoint::broadcast::tx_sync,
tendermint_rpc::{
endpoint::broadcast::tx_sync,
Client as _,
},
Address,
SequencerClientExt as _,
};
use tendermint::crypto::Sha256;
use tokio::{
select,
sync::{
mpsc,
mpsc::error::SendTimeoutError,
mpsc::{
self,
error::SendTimeoutError,
},
watch,
},
time::{
Expand Down Expand Up @@ -94,7 +99,19 @@ pub(crate) use builder::Builder;
const BUNDLE_DRAINING_DURATION: Duration = Duration::from_secs(16);

type StdError = dyn std::error::Error;

#[derive(Debug, thiserror::Error)]
pub(crate) enum EnsureChainIdError {
// <other error variants from getting genesis, etc>
eoroshiba marked this conversation as resolved.
Show resolved Hide resolved
#[error("failed to obtain sequencer chain ID")]
eoroshiba marked this conversation as resolved.
Show resolved Hide resolved
FailedToGetChainId,
eoroshiba marked this conversation as resolved.
Show resolved Hide resolved
#[error("failed to obtain sequencer genesis")]
FailedToGetGenesis,
eoroshiba marked this conversation as resolved.
Show resolved Hide resolved
#[error("expected chain ID `{expected}`, but received `{actual}`")]
WrongChainId {
expected: String,
actual: tendermint::chain::Id,
},
}
/// The `Executor` interfaces with the sequencer. It handles account nonces, transaction signing,
/// and transaction submission.
/// The `Executor` receives `Vec<Action>` from the bundling logic, packages them with a nonce into
Expand Down Expand Up @@ -199,6 +216,17 @@ impl Executor {
/// An error is returned if connecting to the sequencer fails.
#[instrument(skip_all, fields(address = %self.address))]
pub(super) async fn run_until_stopped(mut self) -> eyre::Result<()> {
select!(
biased;
() = self.shutdown_token.cancelled() => {
info!("received shutdown signal while running initialization routines; exiting");
return Ok(());
}

res = self.pre_run_checks() => {
res?;
eoroshiba marked this conversation as resolved.
Show resolved Hide resolved
}
);
let mut submission_fut: Fuse<Instrumented<SubmitFut>> = Fuse::terminated();
let mut nonce = get_latest_nonce(self.sequencer_client.clone(), self.address, self.metrics)
.await
Expand Down Expand Up @@ -417,6 +445,56 @@ impl Executor {

reason.map(|_| ())
}

/// Performs initialization checks prior to running the executor
async fn pre_run_checks(&self) -> eyre::Result<()> {
self.ensure_chain_id_is_correct().await?;
Ok(())
}

/// Performs check to ensure the configured chain ID matches the remote chain ID
pub(crate) async fn ensure_chain_id_is_correct(&self) -> Result<(), EnsureChainIdError> {
let remote_chain_id = self
.get_sequencer_chain_id()
.await
eoroshiba marked this conversation as resolved.
Show resolved Hide resolved
.map_err(|_| EnsureChainIdError::FailedToGetChainId)?;
if remote_chain_id.as_str() != self.sequencer_chain_id {
return Err(EnsureChainIdError::WrongChainId {
expected: self.sequencer_chain_id.clone(),
actual: remote_chain_id,
});
}
Ok(())
}

/// Fetch chain id from the sequencer client
async fn get_sequencer_chain_id(&self) -> eyre::Result<tendermint::chain::Id> {
eoroshiba marked this conversation as resolved.
Show resolved Hide resolved
let retry_config = tryhard::RetryFutureConfig::new(u32::MAX)
.exponential_backoff(Duration::from_millis(100))
.max_delay(Duration::from_secs(20))
.on_retry(
|attempt: u32,
next_delay: Option<Duration>,
error: &sequencer_client::tendermint_rpc::Error| {
let wait_duration = next_delay
.map(humantime::format_duration)
.map(tracing::field::display);
warn!(
attempt,
wait_duration,
error = error as &dyn std::error::Error,
"attempt to fetch sequencer genesis info; retrying after backoff",
);
futures::future::ready(())
},
);
let client_genesis: tendermint::Genesis =
tryhard::retry_fn(|| self.sequencer_client.genesis())
.with_config(retry_config)
.await
.map_err(|_| EnsureChainIdError::FailedToGetGenesis)?;
Ok(client_genesis.chain_id)
}
}

/// Queries the sequencer for the latest nonce with an exponential backoff
Expand Down
Loading
Loading