Skip to content

Commit

Permalink
have the bridge subcmd match the rest
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperFluffy committed Sep 30, 2024
1 parent 86d0729 commit 54c899a
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 84 deletions.
87 changes: 13 additions & 74 deletions crates/astria-cli/src/bridge/collect.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use std::{
collections::BTreeMap,
path::{
Path,
PathBuf,
},
path::PathBuf,
sync::Arc,
time::Duration,
};
Expand All @@ -12,20 +8,15 @@ use astria_bridge_contracts::{
GetWithdrawalActions,
GetWithdrawalActionsBuilder,
};
use astria_core::{
primitive::v1::{
asset::{
self,
},
Address,
use astria_core::primitive::v1::{
asset::{
self,
},
protocol::transaction::v1alpha1::Action,
Address,
};
use clap::Args;
use color_eyre::eyre::{
self,
bail,
ensure,
eyre,
OptionExt as _,
WrapErr as _,
Expand All @@ -49,8 +40,10 @@ use tracing::{
warn,
};

#[derive(Args, Debug)]
pub(crate) struct WithdrawalEventsArgs {
use super::ActionsByRollupHeight;

#[derive(clap::Args, Debug)]
pub(super) struct Args {
/// The websocket endpoint of a geth compatible rollup.
#[arg(long)]
rollup_endpoint: String,
Expand Down Expand Up @@ -88,8 +81,8 @@ pub(crate) struct WithdrawalEventsArgs {
force: bool,
}

impl WithdrawalEventsArgs {
pub(crate) async fn run(self) -> eyre::Result<()> {
impl Args {
pub(super) async fn run(self) -> eyre::Result<()> {
let Self {
rollup_endpoint,
contract_address,
Expand All @@ -103,7 +96,8 @@ impl WithdrawalEventsArgs {
force,
} = self;

let output = open_output(&output, force).wrap_err("failed to open output for writing")?;
let output =
super::open_output(&output, force).wrap_err("failed to open output for writing")?;

let block_provider = connect_to_rollup(&rollup_endpoint)
.await
Expand Down Expand Up @@ -209,36 +203,6 @@ async fn block_to_actions(
actions_by_rollup_height.insert(rollup_height, actions)
}

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(transparent)]
pub(crate) struct ActionsByRollupHeight(BTreeMap<u64, Vec<Action>>);

impl ActionsByRollupHeight {
fn new() -> Self {
Self(BTreeMap::new())
}

pub(crate) fn into_inner(self) -> BTreeMap<u64, Vec<Action>> {
self.0
}

#[instrument(skip_all, err)]
fn insert(&mut self, rollup_height: u64, actions: Vec<Action>) -> eyre::Result<()> {
ensure!(
self.0.insert(rollup_height, actions).is_none(),
"already collected actions for block at rollup height `{rollup_height}`; no 2 blocks \
with the same height should have been seen",
);
Ok(())
}

#[instrument(skip_all, fields(target = %output.path.display()), err)]
fn write_to_output(self, output: Output) -> eyre::Result<()> {
let writer = std::io::BufWriter::new(output.handle);
serde_json::to_writer(writer, &self.0).wrap_err("failed writing actions to file")
}
}

/// Constructs a block stream from `start` until `maybe_end`, if `Some`.
/// Constructs an open ended stream from `start` if `None`.
#[instrument(skip_all, fields(start, end = maybe_end), err)]
Expand Down Expand Up @@ -292,31 +256,6 @@ async fn create_stream_of_blocks(
Ok(subscription)
}

#[derive(Debug)]
struct Output {
handle: std::fs::File,
path: PathBuf,
}

#[instrument(skip(target), fields(target = %target.as_ref().display()), err)]
fn open_output<P: AsRef<Path>>(target: P, overwrite: bool) -> eyre::Result<Output> {
let handle = if overwrite {
let mut options = std::fs::File::options();
options.write(true).create(true).truncate(true);
options
} else {
let mut options = std::fs::File::options();
options.write(true).create_new(true);
options
}
.open(&target)
.wrap_err("failed to open specified file for writing")?;
Ok(Output {
handle,
path: target.as_ref().to_path_buf(),
})
}

#[instrument(err)]
async fn connect_to_rollup(rollup_endpoint: &str) -> eyre::Result<Arc<Provider<Ws>>> {
let retry_config = tryhard::RetryFutureConfig::new(10)
Expand Down
79 changes: 74 additions & 5 deletions crates/astria-cli/src/bridge/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
pub(crate) mod collect;
pub(crate) mod submit;
mod collect;
mod submit;

use std::{
collections::BTreeMap,
path::{
Path,
PathBuf,
},
};

use astria_core::protocol::transaction::v1alpha1::Action;
use clap::Subcommand;
use color_eyre::eyre;
use color_eyre::eyre::{
self,
ensure,
WrapErr as _,
};
use tracing::instrument;

/// Interact with a Sequencer node
#[derive(Debug, clap::Args)]
Expand All @@ -23,6 +37,61 @@ impl Args {
#[derive(Debug, Subcommand)]
enum Command {
/// Commands for interacting with Sequencer accounts
CollectWithdrawals(collect::WithdrawalEventsArgs),
SubmitWithdrawals(submit::WithdrawalEventsArgs),
CollectWithdrawals(collect::Args),
SubmitWithdrawals(submit::Args),
}

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(transparent)]
struct ActionsByRollupHeight(BTreeMap<u64, Vec<Action>>);

impl ActionsByRollupHeight {
fn new() -> Self {
Self(BTreeMap::new())
}

fn into_inner(self) -> BTreeMap<u64, Vec<Action>> {
self.0
}

#[instrument(skip_all, err)]
fn insert(&mut self, rollup_height: u64, actions: Vec<Action>) -> eyre::Result<()> {
ensure!(
self.0.insert(rollup_height, actions).is_none(),
"already collected actions for block at rollup height `{rollup_height}`; no 2 blocks \
with the same height should have been seen",
);
Ok(())
}

#[instrument(skip_all, fields(target = %output.path.display()), err)]
fn write_to_output(self, output: Output) -> eyre::Result<()> {
let writer = std::io::BufWriter::new(output.handle);
serde_json::to_writer(writer, &self.0).wrap_err("failed writing actions to file")
}
}

#[derive(Debug)]
struct Output {
handle: std::fs::File,
path: PathBuf,
}

#[instrument(skip(target), fields(target = %target.as_ref().display()), err)]
fn open_output<P: AsRef<Path>>(target: P, overwrite: bool) -> eyre::Result<Output> {
let handle = if overwrite {
let mut options = std::fs::File::options();
options.write(true).create(true).truncate(true);
options
} else {
let mut options = std::fs::File::options();
options.write(true).create_new(true);
options
}
.open(&target)
.wrap_err("failed to open specified file for writing")?;
Ok(Output {
handle,
path: target.as_ref().to_path_buf(),
})
}
9 changes: 4 additions & 5 deletions crates/astria-cli/src/bridge/submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use astria_sequencer_client::{
HttpClient,
SequencerClientExt as _,
};
use clap::Args;
use color_eyre::eyre::{
self,
ensure,
Expand All @@ -30,8 +29,8 @@ use tracing::{
warn,
};

#[derive(Args, Debug)]
pub(crate) struct WithdrawalEventsArgs {
#[derive(clap::Args, Debug)]
pub(crate) struct Args {
#[arg(long, short)]
input: PathBuf,
#[arg(long)]
Expand All @@ -44,7 +43,7 @@ pub(crate) struct WithdrawalEventsArgs {
sequencer_url: String,
}

impl WithdrawalEventsArgs {
impl Args {
pub(crate) async fn run(self) -> eyre::Result<()> {
let signing_key = read_signing_key(&self.signing_key).wrap_err_with(|| {
format!(
Expand Down Expand Up @@ -97,7 +96,7 @@ impl WithdrawalEventsArgs {
}
}

fn read_actions<P: AsRef<Path>>(path: P) -> eyre::Result<super::collect::ActionsByRollupHeight> {
fn read_actions<P: AsRef<Path>>(path: P) -> eyre::Result<super::ActionsByRollupHeight> {
let s = std::fs::read_to_string(path).wrap_err("failed buffering file contents as string")?;
serde_json::from_str(&s)
.wrap_err("failed deserializing file contents height-to-sequencer-actions serde object")
Expand Down

0 comments on commit 54c899a

Please sign in to comment.