-
Notifications
You must be signed in to change notification settings - Fork 10
shim: query CLI #34
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
shim: query CLI #34
Changes from all commits
37183e9
1a5e3bf
13ea1d9
9701579
6e7a4af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use crate::cli::{Cli, Commands}; | ||
use clap::Parser; | ||
|
||
pub mod query; | ||
pub mod serve; | ||
|
||
pub async fn dispatch() -> anyhow::Result<()> { | ||
init_logging()?; | ||
let cli = Cli::parse(); | ||
match cli.command { | ||
Commands::Serve(params) => serve::run(params).await?, | ||
Commands::Simulate => { | ||
anyhow::bail!("simulate subcommand not yet implemented") | ||
} | ||
Commands::Query(params) => query::run(params).await?, | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn init_logging() -> anyhow::Result<()> { | ||
use tracing_subscriber::fmt; | ||
use tracing_subscriber::prelude::*; | ||
let filter = tracing_subscriber::EnvFilter::builder() | ||
.with_default_directive(tracing_subscriber::filter::LevelFilter::INFO.into()) | ||
.from_env_lossy(); | ||
tracing_subscriber::registry() | ||
.with(fmt::layer()) | ||
.with(filter) | ||
.try_init()?; | ||
Ok(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use crate::{ | ||
cli::query::{Commands, Params}, | ||
sugondat_rpc, | ||
}; | ||
|
||
mod submit; | ||
|
||
pub async fn run(params: Params) -> anyhow::Result<()> { | ||
match params.command { | ||
Commands::Submit(params) => submit::run(params).await?, | ||
} | ||
Ok(()) | ||
} | ||
|
||
async fn connect_rpc( | ||
conn_params: crate::cli::SugondatRpcParams, | ||
) -> anyhow::Result<sugondat_rpc::Client> { | ||
sugondat_rpc::Client::new(conn_params.node_url).await | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use anyhow::Context; | ||
|
||
use super::connect_rpc; | ||
use crate::cli::query::submit::Params; | ||
|
||
pub async fn run(params: Params) -> anyhow::Result<()> { | ||
let Params { | ||
blob_path, | ||
namespace, | ||
rpc, | ||
} = params; | ||
let blob = read_blob(&blob_path) | ||
.with_context(|| format!("cannot read blob file path '{}'", blob_path))?; | ||
let namespace = read_namespace(&namespace)?; | ||
let client = connect_rpc(rpc).await?; | ||
tracing::info!("submitting blob to namespace {}", namespace); | ||
let block_hash = client.submit_blob(blob, namespace).await?; | ||
tracing::info!("submitted blob to block hash 0x{}", hex::encode(block_hash)); | ||
Ok(()) | ||
} | ||
|
||
/// Reads a blob from either a file or stdin. | ||
fn read_blob(path: &str) -> anyhow::Result<Vec<u8>> { | ||
use std::io::Read; | ||
let mut blob = Vec::new(); | ||
if path == "-" { | ||
tracing::debug!("reading blob contents from stdin"); | ||
std::io::stdin().read_to_end(&mut blob)?; | ||
} else { | ||
std::fs::File::open(path)?.read_to_end(&mut blob)?; | ||
} | ||
Ok(blob) | ||
} | ||
|
||
/// Reads the namespace from a given namespace specifier. | ||
/// | ||
/// The original namespace format is a 4-byte vector. so we support both the original format and | ||
/// a more human-readable format, which is an unsigned 32-bit integer. To distinguish between the | ||
/// two, the byte vector must be prefixed with `0x`. | ||
/// | ||
/// The integer is interpreted as little-endian. | ||
fn read_namespace(namespace: &str) -> anyhow::Result<sugondat_nmt::Namespace> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. another possible implementation would be: fn read_namespace(namespace: &str) -> anyhow::Result<sugondat_nmt::Namespace> {
if let Some(hex) = namespace.strip_prefix("0x") {
let namespace = hex::decode(hex)?;
let namespace: [u8; 4] = namespace.try_into().map_err(|e: Vec<u8>| {
anyhow::anyhow!("namespace must be 4 bytes long, but was {}", e.len())
})?;
Ok(sugondat_nmt::Namespace::from_raw_bytes(namespace))
}
let namespace_id = namespace
.parse::<u32>()
.with_context(|| format!("cannot parse namespace id '{}'", namespace))?;
Ok(sugondat_nmt::Namespace::with_namespace_id(namespace_id))
} or cleaner but technically more computation required because the from_str_radix would perform an useless translation to u32 fn read_namespace(mut namespace: &str) -> anyhow::Result<sugondat_nmt::Namespace> {
let namespace_id = match namespace.strip_prefix("0x") {
Some(hex) => u32::from_str_radix(hex, 16).with_context(|| format!("cannot parse namespace id '{}'", namespace))?,
None => namespace.parse::<u32>().with_context(|| format!("cannot parse namespace id '{}'", namespace))?
};
Ok(sugondat_nmt::Namespace::with_namespace_id(namespace_id))
} (didn't test the error handling) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hah, this is a funny one. I specifically went for the implementation that you see here to disallow passing That said, I don't think this matters much and it's OK either way. Feel free to file a PR with the fix. I actually prefer the first option: for one, it's not unlikely that we change the size of the namespace #12 . Note that you it seems you are missing a return though. |
||
if namespace.starts_with("0x") { | ||
let namespace = namespace.trim_start_matches("0x"); | ||
let namespace = hex::decode(namespace)?; | ||
let namespace: [u8; 4] = namespace.try_into().map_err(|e: Vec<u8>| { | ||
anyhow::anyhow!("namespace must be 4 bytes long, but was {}", e.len()) | ||
})?; | ||
Ok(sugondat_nmt::Namespace::from_raw_bytes(namespace)) | ||
} else { | ||
let namespace_id = namespace | ||
.parse::<u32>() | ||
.with_context(|| format!("cannot parse namespace id '{}'", namespace))?; | ||
Ok(sugondat_nmt::Namespace::with_namespace_id(namespace_id)) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these are query parameters for the DA layer block / number, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep! generally shim doesn't know anything about rollup blocks. Only DA blocks and blobs.