Skip to content

Commit

Permalink
fix(grpc): avoid panic in hash value parsing (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega authored Jun 22, 2024
1 parent ee97054 commit e7eae90
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
12 changes: 12 additions & 0 deletions src/serve/grpc/convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use bytes::Bytes;
use pallas::crypto::hash::Hash;
use tonic::Status;

pub fn bytes_to_hash32(data: &Bytes) -> Result<Hash<32>, Status> {
let array: [u8; 32] = data
.as_ref()
.try_into()
.map_err(|_| Status::invalid_argument("invalid hash value, needs to be 32-bytes long"))?;

Ok(Hash::<32>::new(array))
}
1 change: 1 addition & 0 deletions src/serve/grpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::ledger::store::LedgerStore;
use crate::wal::redb::WalStore;
use crate::{prelude::*, submit::Transaction};

mod convert;
mod query;
mod submit;
mod sync;
Expand Down
20 changes: 10 additions & 10 deletions src/serve/grpc/query.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::ledger::{store::LedgerStore, EraCbor, TxoRef};
use futures_core::Stream;
use itertools::Itertools;
use pallas::crypto::hash::Hash;
use itertools::Itertools as _;
use pallas::interop::utxorpc as interop;
use pallas::interop::utxorpc::spec as u5c;
use pallas::ledger::traverse::MultiEraOutput;
Expand All @@ -23,11 +22,6 @@ impl QueryServiceImpl {
}
}

fn bytes_to_hash(raw: &[u8]) -> Hash<32> {
let array: [u8; 32] = raw.try_into().unwrap();
Hash::<32>::new(array)
}

fn find_matching_set(
ledger: &LedgerStore,
query: u5c::cardano::TxOutputPattern,
Expand All @@ -47,6 +41,11 @@ fn find_matching_set(
Ok(set)
}

fn from_u5c_txoref(txo: u5c::query::TxoRef) -> Result<TxoRef, Status> {
let hash = super::convert::bytes_to_hash32(&txo.hash)?;
Ok(TxoRef(hash, txo.index))
}

fn into_u5c_utxo(
txo: &TxoRef,
body: &EraCbor,
Expand Down Expand Up @@ -94,14 +93,15 @@ impl u5c::query::query_service_server::QueryService for QueryServiceImpl {

info!("received new grpc query");

let keys = message
let keys: Vec<_> = message
.keys
.into_iter()
.map(|x| TxoRef(bytes_to_hash(&x.hash), x.index));
.map(from_u5c_txoref)
.try_collect()?;

let utxos = self
.ledger
.get_utxos(keys.collect_vec())
.get_utxos(keys)
.map_err(|e| Status::internal(e.to_string()))?;

let items: Vec<_> = utxos
Expand Down
17 changes: 12 additions & 5 deletions src/serve/grpc/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ use tonic::{Request, Response, Status};
use crate::ledger;
use crate::wal::{self, RawBlock, WalReader as _};

fn u5c_to_chain_point(block_ref: u5c::sync::BlockRef) -> wal::ChainPoint {
wal::ChainPoint::Specific(block_ref.index, block_ref.hash.as_ref().into())
fn u5c_to_chain_point(block_ref: u5c::sync::BlockRef) -> Result<wal::ChainPoint, Status> {
Ok(wal::ChainPoint::Specific(
block_ref.index,
super::convert::bytes_to_hash32(&block_ref.hash)?,
))
}

// fn raw_to_anychain2(raw: &[u8]) -> AnyChainBlock {
Expand Down Expand Up @@ -73,7 +76,11 @@ impl u5c::sync::chain_sync_service_server::ChainSyncService for ChainSyncService
) -> Result<Response<u5c::sync::FetchBlockResponse>, Status> {
let message = request.into_inner();

let points: Vec<_> = message.r#ref.into_iter().map(u5c_to_chain_point).collect();
let points: Vec<_> = message
.r#ref
.into_iter()
.map(u5c_to_chain_point)
.try_collect()?;

let out = self
.wal
Expand All @@ -94,7 +101,7 @@ impl u5c::sync::chain_sync_service_server::ChainSyncService for ChainSyncService
) -> Result<Response<u5c::sync::DumpHistoryResponse>, Status> {
let msg = request.into_inner();

let from = msg.start_token.map(u5c_to_chain_point);
let from = msg.start_token.map(u5c_to_chain_point).transpose()?;

let len = msg.max_items as usize + 1;

Expand Down Expand Up @@ -145,7 +152,7 @@ impl u5c::sync::chain_sync_service_server::ChainSyncService for ChainSyncService
.intersect
.into_iter()
.map(u5c_to_chain_point)
.collect();
.try_collect()?;

self.wal
.find_intersect(&intersect)
Expand Down

0 comments on commit e7eae90

Please sign in to comment.