Skip to content

Commit

Permalink
removed unwraps in PR production code
Browse files Browse the repository at this point in the history
  • Loading branch information
idky137 committed Jun 18, 2024
1 parent 4e91fff commit 917072e
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 163 deletions.
2 changes: 1 addition & 1 deletion zingo-rpc/src/blockcache/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ pub async fn get_block_from_node(
}
Ok(GetBlockResponse::Raw(block_hex)) => Ok(FullBlock::parse_to_compact(
block_hex.as_ref(),
Some(display_txids_to_server(tx)),
Some(display_txids_to_server(tx)?),
trees.sapling.size as u32,
trees.orchard.size as u32,
)?),
Expand Down
16 changes: 11 additions & 5 deletions zingo-rpc/src/blockcache/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ pub enum ParseError {
/// Errors from the JsonRPC client.
#[error("JsonRPC Connector Error: {0}")]
JsonRpcError(#[from] JsonRpcConnectorError),
/// UTF-8 conversion error.
#[error("UTF-8 Error: {0}")]
Utf8Error(#[from] std::str::Utf8Error),
/// Hexadecimal parsing error.
#[error("Hex Parse Error: {0}")]
ParseIntError(#[from] std::num::ParseIntError),
}

/// Used for decoding zcash blocks from a bytestring.
Expand Down Expand Up @@ -121,18 +127,18 @@ pub fn read_zcash_script_i64(cursor: &mut Cursor<&[u8]>) -> Result<i64, ParseErr
}

/// Takes a vec of big endian hex encoded txids and returns them as a vec of little endian raw bytes.
pub fn display_txids_to_server(txids: Vec<String>) -> Vec<Vec<u8>> {
pub fn display_txids_to_server(txids: Vec<String>) -> Result<Vec<Vec<u8>>, ParseError> {
txids
.iter()
.map(|txid| {
txid.as_bytes()
.chunks(2)
.map(|chunk| {
let hex_pair = std::str::from_utf8(chunk).unwrap();
u8::from_str_radix(hex_pair, 16).unwrap()
let hex_pair = std::str::from_utf8(chunk).map_err(ParseError::from)?;
u8::from_str_radix(hex_pair, 16).map_err(ParseError::from)
})
.rev()
.collect()
.collect::<Result<Vec<u8>, _>>()
})
.collect()
.collect::<Result<Vec<Vec<u8>>, _>>()
}
26 changes: 19 additions & 7 deletions zingo-rpc/src/jsonrpc/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,21 +445,33 @@ impl<'de> Deserialize<'de> for GetTransactionResponse {
{
let v = serde_json::Value::deserialize(deserializer)?;
if v.get("height").is_some() && v.get("confirmations").is_some() {
let hex = serde_json::from_value(v["hex"].clone()).map_err(serde::de::Error::custom)?;
let height = v["height"]
.as_i64()
.ok_or_else(|| serde::de::Error::custom("Missing or invalid height"))?
as i32;
let confirmations = v["confirmations"]
.as_u64()
.ok_or_else(|| serde::de::Error::custom("Missing or invalid confirmations"))?
as u32;
let obj = GetTransactionResponse::Object {
hex: serde_json::from_value(v["hex"].clone()).unwrap(),
height: v["height"].as_i64().unwrap() as i32,
confirmations: v["confirmations"].as_u64().unwrap() as u32,
hex,
height,
confirmations,
};
Ok(obj)
} else if v.get("hex").is_some() && v.get("txid").is_some() {
let hex = serde_json::from_value(v["hex"].clone()).map_err(serde::de::Error::custom)?;
let obj = GetTransactionResponse::Object {
hex: serde_json::from_value(v["hex"].clone()).unwrap(),
height: -1 as i32,
confirmations: 0 as u32,
hex,
height: -1,
confirmations: 0,
};
Ok(obj)
} else {
let raw = GetTransactionResponse::Raw(serde_json::from_value(v.clone()).unwrap());
let raw = GetTransactionResponse::Raw(
serde_json::from_value(v.clone()).map_err(serde::de::Error::custom)?,
);
Ok(raw)
}
}
Expand Down
Loading

0 comments on commit 917072e

Please sign in to comment.