Skip to content

Commit

Permalink
add raw data conversions from u256
Browse files Browse the repository at this point in the history
  • Loading branch information
sslivkoff committed Aug 30, 2023
1 parent a5c7b73 commit bf04823
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 17 deletions.
2 changes: 1 addition & 1 deletion crates/cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub struct Args {

/// Set output datatype(s) of U256 integers
/// [default: binary, string, f64]
#[arg(long, help_heading = "Content Options", verbatim_doc_comment)]
#[arg(long, num_args(1..), help_heading = "Content Options", verbatim_doc_comment)]
pub u256_types: Option<Vec<String>>,

/// Use hex string encoding for binary columns
Expand Down
10 changes: 8 additions & 2 deletions crates/cli/src/parse/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,21 @@ fn parse_schemas(args: &Args) -> Result<HashMap<Datatype, Table>, ParseError> {
let u256_types = if let Some(raw_u256_types) = &args.u256_types {
let mut u256_types: HashSet<U256Type> = HashSet::new();
for raw in raw_u256_types.iter() {
// let g: f64 = raw;
let u256_type = match raw.to_lowercase() {
raw if raw == "binary" => U256Type::Binary,
raw if raw == "string" => U256Type::String,
raw if raw == "str" => U256Type::String,
raw if raw == "f32" => U256Type::F32,
raw if raw == "float32" => U256Type::F32,
raw if raw == "f64" => U256Type::F64,
raw if raw == "float" => U256Type::F64,
raw if raw == "float64" => U256Type::F64,
raw if raw == "float" => U256Type::F64,
raw if raw == "u32" => U256Type::U32,
raw if raw == "uint32" => U256Type::U32,
raw if raw == "u64" => U256Type::U64,
raw if raw == "uint64" => U256Type::U64,
raw if raw == "decimal128" => U256Type::Decimal128,
raw if raw == "d128" => U256Type::Decimal128,
_ => return Err(ParseError::ParseError("bad u256 type".to_string())),
};
u256_types.insert(u256_type);
Expand Down
10 changes: 5 additions & 5 deletions crates/freeze/src/datasets/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use crate::{
types::{
conversions::{ToVecHex, ToVecU8},
BlockChunk, Blocks, CollectError, ColumnType, Dataset, Datatype, RowFilter, Source, Table,
TransactionChunk,
TransactionChunk, U256Type,
},
with_series, with_series_binary,
with_series, with_series_binary, with_series_u256,
};

pub(crate) type BlockTxGasTuple<TX> = Result<(Block<TX>, Option<Vec<u32>>), CollectError>;
Expand Down Expand Up @@ -321,7 +321,7 @@ pub(crate) struct TransactionColumns {
nonce: Vec<u64>,
from_address: Vec<Vec<u8>>,
to_address: Vec<Option<Vec<u8>>>,
value: Vec<String>,
value: Vec<U256>,
input: Vec<Vec<u8>>,
gas_limit: Vec<u32>,
gas_used: Vec<u32>,
Expand Down Expand Up @@ -364,7 +364,7 @@ impl TransactionColumns {
with_series!(cols, "nonce", self.nonce, schema);
with_series_binary!(cols, "from_address", self.from_address, schema);
with_series_binary!(cols, "to_address", self.to_address, schema);
with_series!(cols, "value", self.value, schema);
with_series_u256!(cols, "value", self.value, schema);
with_series_binary!(cols, "input", self.input, schema);
with_series!(cols, "gas_limit", self.gas_limit, schema);
with_series!(cols, "gas_used", self.gas_used, schema);
Expand Down Expand Up @@ -471,7 +471,7 @@ fn process_transaction(
columns.nonce.push(tx.nonce.as_u64());
}
if schema.has_column("value") {
columns.value.push(tx.value.to_string());
columns.value.push(tx.value);
}
if schema.has_column("input") {
columns.input.push(tx.input.to_vec());
Expand Down
73 changes: 73 additions & 0 deletions crates/freeze/src/types/dataframes/creation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,76 @@ macro_rules! with_series_binary {
}
};
}

/// convert a Vec to variety of u256 Series representations
#[macro_export]
macro_rules! with_series_u256 {
($all_series:expr, $name:expr, $value:expr, $schema:expr) => {
if $schema.has_column($name) {
// binary
if $schema.u256_types.contains(&U256Type::Binary) {
let name = $name.to_string() + U256Type::Binary.suffix().as_str();
let name = name.as_str();

let converted: Vec<Vec<u8>> = $value.iter().map(|v| v.to_vec_u8()).collect();
if let Some(ColumnType::Hex) = $schema.column_type($name) {
$all_series.push(Series::new(name, converted.to_vec_hex()));
} else {
$all_series.push(Series::new(name, converted));
}
}

// string
if $schema.u256_types.contains(&U256Type::String) {
let name = $name.to_string() + U256Type::String.suffix().as_str();
let name = name.as_str();

let converted: Vec<String> = $value.iter().map(|v| v.to_string()).collect();
$all_series.push(Series::new(name, converted));
}

// float32
if $schema.u256_types.contains(&U256Type::F32) {
let name = $name.to_string() + U256Type::F32.suffix().as_str();
let name = name.as_str();

let converted: Vec<Option<f32>> =
$value.iter().map(|v| v.to_string().parse::<f32>().ok()).collect();
$all_series.push(Series::new(name, converted));
}

// float64
if $schema.u256_types.contains(&U256Type::F64) {
let name = $name.to_string() + U256Type::F64.suffix().as_str();
let name = name.as_str();

let converted: Vec<Option<f64>> =
$value.iter().map(|v| v.to_string().parse::<f64>().ok()).collect();
$all_series.push(Series::new(name, converted));
}

// u32
if $schema.u256_types.contains(&U256Type::U32) {
let name = $name.to_string() + U256Type::U32.suffix().as_str();
let name = name.as_str();

let converted: Vec<u32> = $value.iter().map(|v| v.as_u32()).collect();
$all_series.push(Series::new(name, converted));
}

// u64
if $schema.u256_types.contains(&U256Type::U64) {
let name = $name.to_string() + U256Type::U64.suffix().as_str();
let name = name.as_str();

let converted: Vec<u64> = $value.iter().map(|v| v.as_u64()).collect();
$all_series.push(Series::new(name, converted));
}

// decimal128
if $schema.u256_types.contains(&U256Type::Decimal128) {
panic!("DECIMAL128 not implemented")
}
}
};
}
18 changes: 9 additions & 9 deletions crates/freeze/src/types/schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ pub enum U256Type {
F32,
/// F64 representation
F64,
/// U32 representation
U32,
/// U64 representation
U64,
/// Decimal128 representation
Decimal128,
/// U64High representation
U64High,
/// U64Low representation
U64Low,
}

impl U256Type {
Expand All @@ -67,9 +67,9 @@ impl U256Type {
U256Type::String => ColumnType::String,
U256Type::F32 => ColumnType::Float32,
U256Type::F64 => ColumnType::Float64,
U256Type::U32 => ColumnType::UInt32,
U256Type::U64 => ColumnType::UInt64,
U256Type::Decimal128 => ColumnType::Decimal128,
U256Type::U64High => ColumnType::UInt64,
U256Type::U64Low => ColumnType::UInt64,
}
}

Expand All @@ -80,9 +80,9 @@ impl U256Type {
U256Type::String => "_string".to_string(),
U256Type::F32 => "_f32".to_string(),
U256Type::F64 => "_f64".to_string(),
U256Type::U64High => "_u64_high".to_string(),
U256Type::U64Low => "_u64_low".to_string(),
U256Type::Decimal128 => "_decimal128".to_string(),
U256Type::U32 => "_u32".to_string(),
U256Type::U64 => "_u64".to_string(),
U256Type::Decimal128 => "_d128".to_string(),
}
}
}
Expand Down

0 comments on commit bf04823

Please sign in to comment.