Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
fix: serialize tx_hashes as hex for json response (#30)
Browse files Browse the repository at this point in the history
closes #22
  • Loading branch information
rllola authored Oct 24, 2023
1 parent 1e70877 commit 5947a48
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
13 changes: 9 additions & 4 deletions src/server/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,19 @@ impl LastCommitInfo {
}
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[repr(transparent)]
pub struct HashID(
#[serde(serialize_with = "serialize_hex", deserialize_with = "from_hex")] pub Vec<u8>,
);

/// Relevant information regarding blocks
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct BlockInfo {
#[serde(serialize_with = "serialize_hex", deserialize_with = "from_hex")]
pub block_id: Vec<u8>,
pub block_id: HashID,
pub header: Header,
pub last_commit: Option<LastCommitInfo>,
pub tx_hashes: Vec<Vec<u8>>,
pub tx_hashes: Vec<HashID>,
}

impl From<BlockInfo> for Header {
Expand Down Expand Up @@ -236,7 +241,7 @@ impl TryFrom<&Row> for BlockInfo {
};

Ok(BlockInfo {
block_id,
block_id: HashID(block_id),
header,
last_commit,
tx_hashes: vec![],
Expand Down
4 changes: 2 additions & 2 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ async fn get_tx_hashes(
) -> Result<(), Error> {
let rows = state.db.get_tx_hashes_block(hash).await?;

let mut tx_hashes: Vec<Vec<u8>> = vec![];
let mut tx_hashes: Vec<blocks::HashID> = vec![];
for row in rows.iter() {
let hash: Vec<u8> = row.try_get("hash")?;
let hash = blocks::HashID(row.try_get("hash")?);
tx_hashes.push(hash);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/block_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mod block_tests {

let header = response.json_body_as::<BlockInfo>().unwrap();

let hash_str = hex::encode(&header.block_id);
let hash_str = hex::encode(&header.block_id.0);

// now retrieve same block but by hash:
let new_header = hc
Expand Down

0 comments on commit 5947a48

Please sign in to comment.