Skip to content

Commit

Permalink
updated SerializedTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
idky137 committed Oct 23, 2024
1 parent d37eb76 commit 0cc96d9
Show file tree
Hide file tree
Showing 6 changed files with 471 additions and 409 deletions.
18 changes: 9 additions & 9 deletions Cargo.lock

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

92 changes: 77 additions & 15 deletions zaino-fetch/src/jsonrpc/response.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//! Request and response types for jsonRPC client.

use hex::{FromHex, ToHex};
use hex::FromHex;
use indexmap::IndexMap;
use serde::Deserialize;

use crate::primitives::transaction::{
NoteCommitmentSubtreeIndex, SerializedTransaction, SubtreeRpcData, ZcashScript,
};
use crate::primitives::transaction::{NoteCommitmentSubtreeIndex, SubtreeRpcData, ZcashScript};

/// Response to a `getinfo` RPC request.
///
Expand Down Expand Up @@ -95,7 +93,7 @@ impl AsRef<[u8]> for SerializedBlock {
impl From<Vec<u8>> for SerializedBlock {
fn from(bytes: Vec<u8>) -> Self {
Self {
inner: zebra_chain::block::SerializedBlock::new(bytes),
inner: zebra_chain::block::SerializedBlock::from(bytes),
}
}
}
Expand All @@ -106,16 +104,6 @@ impl From<zebra_chain::block::SerializedBlock> for SerializedBlock {
}
}

impl serde::Serialize for SerializedBlock {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let hex_string = self.as_ref().encode_hex::<String>();
serializer.serialize_str(&hex_string)
}
}

impl<'de> serde::Deserialize<'de> for SerializedBlock {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down Expand Up @@ -280,6 +268,80 @@ impl<'de> Deserialize<'de> for GetTreestateResponse {
}
}

/// A wrapper struct for a zebra serialized transaction.
///
/// Stores bytes that are guaranteed to be deserializable into a [`Transaction`].
///
/// Sorts in lexicographic order of the transaction's serialized data.
#[derive(Clone, Eq, PartialEq)]
pub struct SerializedTransaction {
/// Transaction bytes.
pub inner: zebra_chain::transaction::SerializedTransaction,
}

impl std::fmt::Display for SerializedTransaction {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str(&hex::encode(&self.inner))
}
}

impl std::fmt::Debug for SerializedTransaction {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let data_hex = hex::encode(&self.inner);

f.debug_tuple("SerializedTransaction")
.field(&data_hex)
.finish()
}
}

impl AsRef<[u8]> for SerializedTransaction {
fn as_ref(&self) -> &[u8] {
self.inner.as_ref()
}
}

impl From<Vec<u8>> for SerializedTransaction {
fn from(bytes: Vec<u8>) -> Self {
Self {
inner: zebra_chain::transaction::SerializedTransaction::from(bytes),
}
}
}

impl From<zebra_chain::transaction::SerializedTransaction> for SerializedTransaction {
fn from(inner: zebra_chain::transaction::SerializedTransaction) -> Self {
SerializedTransaction { inner }
}
}

impl<'de> serde::Deserialize<'de> for SerializedTransaction {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let v = serde_json::Value::deserialize(deserializer)?;
if let Some(hex_str) = v.as_str() {
let bytes = hex::decode(hex_str).map_err(serde::de::Error::custom)?;
Ok(SerializedTransaction {
inner: zebra_chain::transaction::SerializedTransaction::from(bytes),
})
} else {
Err(serde::de::Error::custom("expected a hex string"))
}
}
}

impl hex::FromHex for SerializedTransaction {
type Error = <Vec<u8> as hex::FromHex>::Error;

fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
let bytes = <Vec<u8>>::from_hex(hex)?;

Ok(bytes.into())
}
}

/// Contains raw transaction, encoded as hex bytes.
///
/// This is used for the output parameter of [`JsonRpcConnector::get_raw_transaction`].
Expand Down
2 changes: 1 addition & 1 deletion zaino-fetch/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// pub mod address;
// pub mod block;
pub mod chain;
// pub mod chain;
pub mod error;
pub mod height;
pub mod transaction;
Loading

0 comments on commit 0cc96d9

Please sign in to comment.