Skip to content

Commit

Permalink
started adding block parser
Browse files Browse the repository at this point in the history
  • Loading branch information
idky137 committed Jun 6, 2024
1 parent 9c73ccd commit f4d6e28
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
5 changes: 5 additions & 0 deletions zingo-rpc/src/blockcache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! Zingo-Proxy Block Cache and Mempool State Engine.

pub mod block;
pub mod transaction;
pub mod utils;
39 changes: 39 additions & 0 deletions zingo-rpc/src/blockcache/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! Block fetching and deserialization functionality.

/// Block header data.
#[derive(Debug)]
pub struct BlockHeaderData {
pub version: i32,
pub hash_prev_block: Vec<u8>,
pub hash_merkle_root: Vec<u8>,
pub hash_final_sapling_root: Vec<u8>,
pub time: u32,
pub n_bits_bytes: Vec<u8>,
pub nonce: Vec<u8>,
pub solution: Vec<u8>,
}

/// Complete block header.
#[derive(Debug)]
pub struct FullBlockHeader {
pub raw_block_header: BlockHeaderData,
pub cached_hash: Vec<u8>,
}

/// Zingo-Proxy Block.
#[derive(Debug)]
pub struct FullBlock {
pub hdr: FullBlockHeader,
pub vtx: Vec<super::transaction::FullTransaction>,
pub height: i32,
}

// impl parse_from_slice for block_header(&[u8]) -> Result<(Self, &[u8]), ParseError>

// impl parse_from_slice for full_block(&[u8]) -> Result<(Self, &[u8]), ParseError>

// impl parse_full_block(&[u8]) -> Result<Self, Error>

// impl to_compact(Self) -> Result<compact_block, Error>

// impl parse_to_compact(&[u8]) -> Result<compact_block, Error>
88 changes: 88 additions & 0 deletions zingo-rpc/src/blockcache/transaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//! Transaction fetching and deserialization functionality.

/// Transparent input.
#[derive(Debug)]
pub struct TxIn {
pub script_sig: Vec<u8>,
}

/// Transparent output.
#[derive(Debug)]
pub struct TxOut {
pub value: u64,
}

/// Sapling input.
#[derive(Debug)]
pub struct Spend {
pub nullifier: Vec<u8>,
}

/// Sapling output.
#[derive(Debug)]
pub struct Output {
pub cmu: Vec<u8>,
pub ephemeral_key: Vec<u8>,
pub enc_ciphertext: Vec<u8>,
}

#[derive(Debug)]
pub struct JoinSplit {
// Define fields based on your needs
}

/// Orchard actions.
#[derive(Debug)]
pub struct Action {
pub nullifier: Vec<u8>,
pub cmx: Vec<u8>,
pub ephemeral_key: Vec<u8>,
pub enc_ciphertext: Vec<u8>,
}

/// Raw transactrion.
#[derive(Debug)]
pub struct TransactionData {
pub f_overwintered: bool,
pub version: u32,
pub n_version_group_id: u32,
pub consensus_branch_id: u32,
pub transparent_inputs: Vec<TxIn>,
pub transparent_outputs: Vec<TxOut>,
pub shielded_spends: Vec<Spend>,
pub shielded_outputs: Vec<Output>,
pub join_splits: Vec<JoinSplit>,
pub orchard_actions: Vec<Action>,
}

/// Zingo-Proxy transaction data.
#[derive(Debug)]
pub struct FullTransaction {
pub raw_transaction: TransactionData,
pub raw_bytes: Vec<u8>,
pub tx_id: Vec<u8>,
}

// impl parse_from_slice for TxIn(&[u8]) -> Result<(Self, &[u8]), ParseError>

// impl parse_from_slice for TxOut(&[u8]) -> Result<(Self, &[u8]), ParseError>

// imple parse_transparent(&[u8]) -> Result<(????, &[u8]), ParseError>

// impl parse_from_slice for Spend(&[u8]) -> Result<(Self, &[u8]), ParseError>

// impl parse_from_slice for Output(&[u8]) -> Result<(Self, &[u8]), ParseError>

// impl parse_from_slice for JoinSplit(&[u8]) -> Result<(Self, &[u8]), ParseError>

// impl parse_from_slice for Action(&[u8]) -> Result<(Self, &[u8]), ParseError>

// impl parse_v4(&[u8]) -> Result<(????, &[u8]), ParseError>

// impl parse_v5(&[u8]) -> Result<(????, &[u8]), ParseError>

// impl parse_from_slice for transaction(&[u8]) -> Result<(Self, &[u8]), ParseError>

// impl to_compact(Self) -> Result<compact_transaction, Error>

// impl parse_to_compact(&[u8]) -> Result<(compact_transaction, &[u8]), Error>
14 changes: 14 additions & 0 deletions zingo-rpc/src/blockcache/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! Blockcache utility functionality.

/// Parser Error Type.
#[derive(Debug)]
pub enum ParseError {
Io(std::io::Error),
InvalidData(String),
}

trait ParseFromSlice {
fn parse_from_slice(data: &[u8]) -> Result<(&[u8], Self), ParseError>
where
Self: Sized;
}
1 change: 1 addition & 0 deletions zingo-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![warn(missing_docs)]
#![forbid(unsafe_code)]

pub mod blockcache;
pub mod jsonrpc;
pub mod nym;
pub mod primitives;
Expand Down

0 comments on commit f4d6e28

Please sign in to comment.