Skip to content

Commit

Permalink
Merge pull request #20 from HerodotusDev/datapoint-to-string
Browse files Browse the repository at this point in the history
refactor: result unify as string
  • Loading branch information
rkdud007 authored Feb 7, 2024
2 parents 62511b4 + e35750c commit d591507
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 196 deletions.
14 changes: 5 additions & 9 deletions crates/common/src/block/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use alloy_primitives::{hex, FixedBytes, U256};
use alloy_rlp::{Decodable, Encodable as _, RlpDecodable, RlpEncodable};
use serde::{Deserialize, Serialize};

use crate::datalake::base::DataPoint;

#[derive(Debug)]
pub enum AccountField {
Nonce,
Expand Down Expand Up @@ -93,15 +91,13 @@ impl AccountField {
}
}

pub fn decode_account_field(account_rlp: &str, field: AccountField) -> DataPoint {
pub fn decode_account_field(account_rlp: &str, field: AccountField) -> String {
let decoded = <Account>::decode(&mut hex::decode(account_rlp).unwrap().as_slice()).unwrap();
match field {
AccountField::Nonce => DataPoint::Int(u64::from_str(&decoded.nonce.to_string()).unwrap()),
AccountField::Balance => {
DataPoint::Int(u64::from_str(&decoded.balance.to_string()).unwrap())
}
AccountField::StorageRoot => DataPoint::Str(decoded.storage_root.to_string()),
AccountField::CodeHash => DataPoint::Str(decoded.code_hash.to_string()),
AccountField::Nonce => decoded.nonce.to_string(),
AccountField::Balance => decoded.balance.to_string(),
AccountField::StorageRoot => decoded.storage_root.to_string(),
AccountField::CodeHash => decoded.code_hash.to_string(),
}
}

Expand Down
62 changes: 21 additions & 41 deletions crates/common/src/block/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use alloy_rlp::{Decodable, Encodable};
use reth_primitives::Header;
use serde::{Deserialize, Serialize};

use crate::datalake::base::DataPoint;

pub struct BlockHeader(Header);

impl BlockHeader {
Expand Down Expand Up @@ -209,49 +207,31 @@ impl FromStr for HeaderField {
}
}

pub fn decode_header_field(header_rlp: &str, field: HeaderField) -> DataPoint {
pub fn decode_header_field(header_rlp: &str, field: HeaderField) -> String {
let decoded =
<Header as Decodable>::decode(&mut hex::decode(header_rlp).unwrap().as_slice()).unwrap();

match field {
HeaderField::ParentHash => DataPoint::Str(decoded.parent_hash.to_string()),
HeaderField::OmmerHash => DataPoint::Str(decoded.ommers_hash.to_string()),
HeaderField::Beneficiary => DataPoint::Str(decoded.beneficiary.to_string()),
HeaderField::StateRoot => DataPoint::Str(decoded.state_root.to_string()),
HeaderField::TransactionsRoot => DataPoint::Str(decoded.transactions_root.to_string()),
HeaderField::ReceiptsRoot => DataPoint::Str(decoded.receipts_root.to_string()),
HeaderField::LogsBloom => DataPoint::Str(decoded.logs_bloom.to_string()),
HeaderField::Difficulty => {
DataPoint::Int(u64::from_str(&decoded.difficulty.to_string()).unwrap())
}
HeaderField::Number => DataPoint::Int(u64::from_str(&decoded.number.to_string()).unwrap()),
HeaderField::GasLimit => {
DataPoint::Int(u64::from_str(&decoded.gas_limit.to_string()).unwrap())
}
HeaderField::GasUsed => {
DataPoint::Int(u64::from_str(&decoded.gas_used.to_string()).unwrap())
}
HeaderField::Timestamp => {
DataPoint::Int(u64::from_str(&decoded.timestamp.to_string()).unwrap())
}
HeaderField::ExtraData => DataPoint::Str(decoded.extra_data.to_string()),
HeaderField::MixHash => DataPoint::Str(decoded.mix_hash.to_string()),
HeaderField::Nonce => DataPoint::Int(u64::from_str(&decoded.nonce.to_string()).unwrap()),
HeaderField::BaseFeePerGas => {
DataPoint::Int(u64::from_str(&decoded.base_fee_per_gas.unwrap().to_string()).unwrap())
}
HeaderField::WithdrawalsRoot => {
DataPoint::Str(decoded.withdrawals_root.unwrap().to_string())
}
HeaderField::BlobGasUsed => {
DataPoint::Int(u64::from_str(&decoded.blob_gas_used.unwrap().to_string()).unwrap())
}
HeaderField::ExcessBlobGas => {
DataPoint::Int(u64::from_str(&decoded.excess_blob_gas.unwrap().to_string()).unwrap())
}
HeaderField::ParentBeaconBlockRoot => {
DataPoint::Str(decoded.parent_beacon_block_root.unwrap().to_string())
}
HeaderField::ParentHash => decoded.parent_hash.to_string(),
HeaderField::OmmerHash => decoded.ommers_hash.to_string(),
HeaderField::Beneficiary => decoded.beneficiary.to_string(),
HeaderField::StateRoot => decoded.state_root.to_string(),
HeaderField::TransactionsRoot => decoded.transactions_root.to_string(),
HeaderField::ReceiptsRoot => decoded.receipts_root.to_string(),
HeaderField::LogsBloom => decoded.logs_bloom.to_string(),
HeaderField::Difficulty => decoded.difficulty.to_string(),
HeaderField::Number => decoded.number.to_string(),
HeaderField::GasLimit => decoded.gas_limit.to_string(),
HeaderField::GasUsed => decoded.gas_used.to_string(),
HeaderField::Timestamp => decoded.timestamp.to_string(),
HeaderField::ExtraData => decoded.extra_data.to_string(),
HeaderField::MixHash => decoded.mix_hash.to_string(),
HeaderField::Nonce => decoded.nonce.to_string(),
HeaderField::BaseFeePerGas => decoded.base_fee_per_gas.unwrap().to_string(),
HeaderField::WithdrawalsRoot => decoded.withdrawals_root.unwrap().to_string(),
HeaderField::BlobGasUsed => decoded.blob_gas_used.unwrap().to_string(),
HeaderField::ExcessBlobGas => decoded.excess_blob_gas.unwrap().to_string(),
HeaderField::ParentBeaconBlockRoot => decoded.parent_beacon_block_root.unwrap().to_string(),
}
}

Expand Down
8 changes: 3 additions & 5 deletions crates/common/src/compiler/block_sampled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ use crate::{
use anyhow::Result;
use tokio::sync::RwLock;

use crate::datalake::base::DataPoint;

pub async fn compile_block_sampled_datalake(
block_range_start: usize,
block_range_end: usize,
sampled_property: &str,
increment: usize,
fetcher: Arc<RwLock<AbstractFetcher>>,
) -> Result<Vec<DataPoint>> {
) -> Result<Vec<String>> {
let mut abstract_fetcher = fetcher.write().await;
let property_parts: Vec<&str> = sampled_property.split('.').collect();
let collection = property_parts[0];

let mut aggregation_set: Vec<DataPoint> = Vec::new();
let mut aggregation_set: Vec<String> = Vec::new();

match collection {
"header" => {
Expand Down Expand Up @@ -75,7 +73,7 @@ pub async fn compile_block_sampled_datalake(
.get_storage_value(i, account.to_string(), slot.to_string())
.await;

aggregation_set.push(DataPoint::Str(value));
aggregation_set.push(value);
}
}
_ => todo!(),
Expand Down
6 changes: 2 additions & 4 deletions crates/common/src/compiler/test.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use anyhow::Result;

use crate::datalake::base::DataPoint;

pub async fn test_closer() -> Result<Vec<DataPoint>> {
pub async fn test_closer() -> Result<Vec<String>> {
println!("test_closer");
Ok(vec![DataPoint::Int(1)])
Ok(vec!["1".to_string()])
}
15 changes: 2 additions & 13 deletions crates/common/src/datalake/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,11 @@ use crate::fetcher::AbstractFetcher;

use super::Datalake;

/// DataPoint is a type that can be used to store data in a Datalake
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum DataPoint {
Int(u64),
Float(f64),
Str(String),
}

/// DatalakeBase is a type that can be used to store data
pub struct DatalakeBase {
pub identifier: String,
pub datalakes_pipeline: Vec<Datalake>,
pub datapoints: Vec<DataPoint>,
pub datapoints: Vec<String>,
}

impl fmt::Debug for DatalakeBase {
Expand Down Expand Up @@ -46,10 +38,7 @@ impl DatalakeBase {
// self.identifier = format!("{}{}", self.identifier, other.identifier);
// }

pub async fn compile(
&mut self,
fetcher: Arc<RwLock<AbstractFetcher>>,
) -> Result<Vec<DataPoint>> {
pub async fn compile(&mut self, fetcher: Arc<RwLock<AbstractFetcher>>) -> Result<Vec<String>> {
self.datapoints.clear();
for datalake_type in &self.datalakes_pipeline {
let result_datapoints = match datalake_type {
Expand Down
4 changes: 2 additions & 2 deletions crates/common/src/datalake/block_sampled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use tokio::sync::RwLock;
use crate::compiler::block_sampled::compile_block_sampled_datalake;

use super::{
base::{DataPoint, DatalakeBase, Derivable},
base::{DatalakeBase, Derivable},
Datalake,
};

Expand Down Expand Up @@ -88,7 +88,7 @@ impl BlockSampledDatalake {
})
}

pub async fn compile(&self, fetcher: Arc<RwLock<AbstractFetcher>>) -> Result<Vec<DataPoint>> {
pub async fn compile(&self, fetcher: Arc<RwLock<AbstractFetcher>>) -> Result<Vec<String>> {
compile_block_sampled_datalake(
self.block_range_start,
self.block_range_end,
Expand Down
4 changes: 2 additions & 2 deletions crates/common/src/datalake/dynamic_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::{bail, Result};
use crate::compiler::test::test_closer;

use super::{
base::{DataPoint, DatalakeBase, Derivable},
base::{DatalakeBase, Derivable},
Datalake,
};

Expand Down Expand Up @@ -90,7 +90,7 @@ impl DynamicLayoutDatalake {
})
}

pub async fn compile(&self) -> Result<Vec<DataPoint>> {
pub async fn compile(&self) -> Result<Vec<String>> {
test_closer().await?;
Ok(vec![])
}
Expand Down
Loading

0 comments on commit d591507

Please sign in to comment.