Skip to content

Commit

Permalink
use clap fork to properly parse hyphenated block ranges (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
sslivkoff committed Aug 9, 2023
1 parent f81cd69 commit e710b25
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 34 deletions.
38 changes: 19 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ path = "src/main.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.3.4", features = ["derive", "color", "unstable-styles"] }
clap_cryo = { version = "4.3.21-cryo", features = ["derive", "color", "unstable-styles"] }
color-print = "0.3.4"
ethers = "2.0.7"
hex = "0.4.3"
Expand Down
12 changes: 6 additions & 6 deletions crates/cli/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use clap::Parser;
use clap_cryo::Parser;
use color_print::cstr;

/// Command line arguments
#[derive(Parser, Debug)]
#[command(name = "cryo", author, version, about = get_about_str(), long_about = None, styles=get_styles(), after_help=get_after_str())]
#[command(name = "cryo", author, version, about = get_about_str(), long_about = None, styles=get_styles(), after_help=get_after_str(), allow_negative_numbers = true)]
pub struct Args {
/// datatype to collect
#[arg(required = true, help=get_datatype_help(), num_args(1..))]
pub datatype: Vec<String>,

/// Block numbers, see syntax below
#[arg(short, long, allow_hyphen_values(true), help_heading = "Content Options")]
pub blocks: Option<String>,
#[arg(short, long, allow_negative_numbers = true, help_heading = "Content Options", num_args(1..))]
pub blocks: Option<Vec<String>>,

/// Transaction hashes, see syntax below
#[arg(
Expand Down Expand Up @@ -165,14 +165,14 @@ pub struct Args {
pub inner_request_size: u64,
}

pub(crate) fn get_styles() -> clap::builder::Styles {
pub(crate) fn get_styles() -> clap_cryo::builder::Styles {
let white = anstyle::Color::Rgb(anstyle::RgbColor(255, 255, 255));
let green = anstyle::Color::Rgb(anstyle::RgbColor(0, 225, 0));
let grey = anstyle::Color::Rgb(anstyle::RgbColor(170, 170, 170));
let title = anstyle::Style::new().bold().fg_color(Some(green));
let arg = anstyle::Style::new().bold().fg_color(Some(white));
let comment = anstyle::Style::new().fg_color(Some(grey));
clap::builder::Styles::styled()
clap_cryo::builder::Styles::styled()
.header(title)
.error(comment)
.usage(title)
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! cryo_cli is a cli for cryo_freeze

use clap::Parser;
use clap_cryo::Parser;

mod args;
mod parse;
Expand Down
85 changes: 80 additions & 5 deletions crates/cli/src/parse/blocks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use ethers::prelude::*;
use polars::prelude::*;

use cryo_freeze::{BlockChunk, Chunk, ChunkData, ParseError, Subchunk};

Expand All @@ -10,13 +11,87 @@ pub(crate) async fn parse_blocks(
args: &Args,
provider: Arc<Provider<Http>>,
) -> Result<Vec<(Chunk, Option<String>)>, ParseError> {
// parse inputs into BlockChunks
let block_chunks = match &args.blocks {
Some(inputs) => parse_block_inputs(inputs, &provider).await?,
None => return Err(ParseError::ParseError("could not parse block inputs".to_string())),
let (files, explicit_numbers): (Vec<&String>, Vec<&String>) = match &args.blocks {
Some(blocks) => blocks.iter().partition(|tx| std::path::Path::new(tx).exists()),
None => return Err(ParseError::ParseError("no blocks specified".to_string())),
};

postprocess_block_chunks(block_chunks, args, provider).await
let mut file_chunks = if !files.is_empty() {
let mut file_chunks = Vec::new();
for path in files {
let column = if path.contains(':') {
path.split(':')
.last()
.ok_or(ParseError::ParseError("could not parse txs path column".to_string()))?
} else {
"block_number"
};
let integers = read_integer_column(path, column)
.map_err(|_e| ParseError::ParseError("could not read input".to_string()))?;
let chunk = BlockChunk::Numbers(integers);
let chunk_label = path
.split("__")
.last()
.and_then(|s| s.strip_suffix(".parquet").map(|s| s.to_string()));
file_chunks.push((Chunk::Block(chunk), chunk_label));
}
file_chunks
} else {
Vec::new()
};

let explicit_chunks = if !explicit_numbers.is_empty() {
// parse inputs into BlockChunks
let mut block_chunks = Vec::new();
for explicit_number in explicit_numbers {
let outputs = parse_block_inputs(explicit_number, &provider).await?;
block_chunks.extend(outputs.into_iter());

Check warning on line 48 in crates/cli/src/parse/blocks.rs

View workflow job for this annotation

GitHub Actions / clippy

explicit call to `.into_iter()` in function argument accepting `IntoIterator`

warning: explicit call to `.into_iter()` in function argument accepting `IntoIterator` --> crates/cli/src/parse/blocks.rs:48:33 | 48 | block_chunks.extend(outputs.into_iter()); | ^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `outputs` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` --> /rustc/f88a8b71cebb730cbd5058c45ebcae1d4d9be377/library/core/src/iter/traits/collect.rs:371:18 = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion = note: `#[warn(clippy::useless_conversion)]` on by default

Check warning on line 48 in crates/cli/src/parse/blocks.rs

View workflow job for this annotation

GitHub Actions / clippy

explicit call to `.into_iter()` in function argument accepting `IntoIterator`

warning: explicit call to `.into_iter()` in function argument accepting `IntoIterator` --> crates/cli/src/parse/blocks.rs:48:33 | 48 | block_chunks.extend(outputs.into_iter()); | ^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `outputs` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` --> /rustc/f88a8b71cebb730cbd5058c45ebcae1d4d9be377/library/core/src/iter/traits/collect.rs:371:18 = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion = note: `#[warn(clippy::useless_conversion)]` on by default
}
postprocess_block_chunks(block_chunks, args, provider).await?
} else {
Vec::new()
};

file_chunks.extend(explicit_chunks.into_iter());

Check warning on line 55 in crates/cli/src/parse/blocks.rs

View workflow job for this annotation

GitHub Actions / clippy

explicit call to `.into_iter()` in function argument accepting `IntoIterator`

warning: explicit call to `.into_iter()` in function argument accepting `IntoIterator` --> crates/cli/src/parse/blocks.rs:55:24 | 55 | file_chunks.extend(explicit_chunks.into_iter()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `explicit_chunks` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` --> /rustc/f88a8b71cebb730cbd5058c45ebcae1d4d9be377/library/core/src/iter/traits/collect.rs:371:18 = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion

Check warning on line 55 in crates/cli/src/parse/blocks.rs

View workflow job for this annotation

GitHub Actions / clippy

explicit call to `.into_iter()` in function argument accepting `IntoIterator`

warning: explicit call to `.into_iter()` in function argument accepting `IntoIterator` --> crates/cli/src/parse/blocks.rs:55:24 | 55 | file_chunks.extend(explicit_chunks.into_iter()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `explicit_chunks` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` --> /rustc/f88a8b71cebb730cbd5058c45ebcae1d4d9be377/library/core/src/iter/traits/collect.rs:371:18 = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion
Ok(file_chunks)
}

fn read_integer_column(path: &str, column: &str) -> Result<Vec<u64>, ParseError> {
let file = std::fs::File::open(path)
.map_err(|_e| ParseError::ParseError("could not open file path".to_string()))?;

let df = ParquetReader::new(file)
.with_columns(Some(vec![column.to_string()]))
.finish()
.map_err(|_e| ParseError::ParseError("could not read data from column".to_string()))?;

let series = df
.column(column)
.map_err(|_e| ParseError::ParseError("could not get column".to_string()))?
.unique()
.map_err(|_e| ParseError::ParseError("could not get column".to_string()))?;

println!("{:?}", series);
match series.u32() {
Ok(ca) => ca
.into_iter()
.map(|v| {
v.ok_or_else(|| ParseError::ParseError("block number missing".to_string()))
.map(|data| data.into())
})
.collect(),
Err(_e) => match series.u64() {
Ok(ca) => ca
.into_iter()
.map(|v| {
v.ok_or_else(|| ParseError::ParseError("block number missing".to_string()))
})
.collect(),
Err(_e) => {
Err(ParseError::ParseError("could not convert to integer column".to_string()))
}
},
}
}

async fn postprocess_block_chunks(
Expand Down
2 changes: 1 addition & 1 deletion crates/python/src/collect_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use cryo_freeze::collect;
pub fn _collect(
py: Python<'_>,
datatype: String,
blocks: Option<String>,
blocks: Option<Vec<String>>,
txs: Option<Vec<String>>,
align: bool,
reorg_buffer: u64,
Expand Down
2 changes: 1 addition & 1 deletion crates/python/src/freeze_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use cryo_cli::{run, Args};
pub fn _freeze(
py: Python<'_>,
datatype: Vec<String>,
blocks: Option<String>,
blocks: Option<Vec<String>>,
txs: Option<Vec<String>>,
align: bool,
reorg_buffer: u64,
Expand Down

0 comments on commit e710b25

Please sign in to comment.