From d1cff906608b199b8bf8592abb3b160964c759c2 Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Fri, 6 Sep 2024 20:58:50 -0400 Subject: [PATCH] rpc/types: add `from` module --- rpc/types/Cargo.toml | 6 ++++-- rpc/types/src/from/bin.rs | 1 + rpc/types/src/from/json.rs | 1 + rpc/types/src/from/misc.rs | 37 +++++++++++++++++++++++++++++++++++++ rpc/types/src/from/mod.rs | 4 ++++ rpc/types/src/from/other.rs | 1 + rpc/types/src/lib.rs | 1 + 7 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 rpc/types/src/from/bin.rs create mode 100644 rpc/types/src/from/json.rs create mode 100644 rpc/types/src/from/misc.rs create mode 100644 rpc/types/src/from/mod.rs create mode 100644 rpc/types/src/from/other.rs diff --git a/rpc/types/Cargo.toml b/rpc/types/Cargo.toml index 08b13b1..996e8f0 100644 --- a/rpc/types/Cargo.toml +++ b/rpc/types/Cargo.toml @@ -15,11 +15,13 @@ epee = ["dep:cuprate-epee-encoding"] [dependencies] cuprate-epee-encoding = { path = "../../net/epee-encoding", optional = true } +cuprate-helper = { path = "../../helper", default-features = false, features = ["cast", "map"] } cuprate-fixed-bytes = { path = "../../net/fixed-bytes" } cuprate-types = { path = "../../types" } -paste = { workspace = true } -serde = { workspace = true, optional = true } +hex = { workspace = true } +paste = { workspace = true } +serde = { workspace = true, optional = true } [dev-dependencies] cuprate-test-utils = { path = "../../test-utils" } diff --git a/rpc/types/src/from/bin.rs b/rpc/types/src/from/bin.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/rpc/types/src/from/bin.rs @@ -0,0 +1 @@ + diff --git a/rpc/types/src/from/json.rs b/rpc/types/src/from/json.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/rpc/types/src/from/json.rs @@ -0,0 +1 @@ + diff --git a/rpc/types/src/from/misc.rs b/rpc/types/src/from/misc.rs new file mode 100644 index 0000000..062f8cd --- /dev/null +++ b/rpc/types/src/from/misc.rs @@ -0,0 +1,37 @@ +use cuprate_types::VerifiedBlockInformation; + +use cuprate_helper::{cast::usize_to_u64, map::split_u128_into_low_high_bits}; + +use crate::misc::BlockHeader; + +impl From<&VerifiedBlockInformation> for BlockHeader { + fn from(b: &VerifiedBlockInformation) -> Self { + let (cumulative_difficulty_top64, cumulative_difficulty) = + split_u128_into_low_high_bits(b.cumulative_difficulty); + + Self { + block_size: usize_to_u64(b.block_blob.len()), + block_weight: usize_to_u64(b.weight), + cumulative_difficulty_top64, + cumulative_difficulty, + depth: todo!(), + difficulty_top64: todo!(), + difficulty: todo!(), + hash: hex::encode(b.block_hash), + height: usize_to_u64(b.height), + long_term_weight: usize_to_u64(b.long_term_weight), + major_version: b.block.header.hardfork_version, + miner_tx_hash: hex::encode(b.block.miner_transaction.hash()), + minor_version: b.block.header.hardfork_signal, + nonce: b.block.header.nonce, + num_txes: usize_to_u64(b.txs.len()), + orphan_status: todo!(), + pow_hash: hex::encode(b.pow_hash), + prev_hash: hex::encode(b.block.header.previous), + reward: todo!(), + timestamp: b.block.header.timestamp, + wide_cumulative_difficulty: todo!(), + wide_difficulty: todo!(), + } + } +} diff --git a/rpc/types/src/from/mod.rs b/rpc/types/src/from/mod.rs new file mode 100644 index 0000000..ffa8364 --- /dev/null +++ b/rpc/types/src/from/mod.rs @@ -0,0 +1,4 @@ +mod bin; +mod json; +mod misc; +mod other; diff --git a/rpc/types/src/from/other.rs b/rpc/types/src/from/other.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/rpc/types/src/from/other.rs @@ -0,0 +1 @@ + diff --git a/rpc/types/src/lib.rs b/rpc/types/src/lib.rs index 51ea3cc..c24d287 100644 --- a/rpc/types/src/lib.rs +++ b/rpc/types/src/lib.rs @@ -4,6 +4,7 @@ mod constants; mod defaults; mod free; +mod from; mod macros; mod rpc_call;