Skip to content

Commit

Permalink
Multi key query RPC should return Vec<Option<V>> (#980)
Browse files Browse the repository at this point in the history
* If the tx hash is invalid, filled None when returned

* change assert to debug_assert to avoid server panic
  • Loading branch information
baichuan3 authored Oct 16, 2023
1 parent 042bc09 commit 0965f5a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 36 deletions.
21 changes: 6 additions & 15 deletions crates/rooch-rpc-server/src/server/rooch_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,22 +217,12 @@ impl RoochAPIServer for RoochServer {
.get_tx_sequence_info_mapping_by_hash(tx_hashes.clone())
.await?;

let mut tx_orders = vec![];
for item in tx_sequence_info_mapping {
if item.is_none() {
return Err(JsonRpcError::Custom(String::from(
"Invalid tx hash or tx order",
)));
}
tx_orders.push(item.unwrap().tx_order);
}

let data = self
.aggregate_service
.get_transaction_results_by_hash_and_order(tx_hashes, tx_orders)
.get_transaction_with_info(tx_hashes, tx_sequence_info_mapping)
.await?
.into_iter()
.map(|item| Some(TransactionWithInfoView::from(item)))
.map(|item| item.map(TransactionWithInfoView::from))
.collect::<Vec<_>>();

Ok(data)
Expand Down Expand Up @@ -274,10 +264,10 @@ impl RoochAPIServer for RoochServer {
.map_or(cursor, |m| m.clone().map(|v| v.tx_order));

let mut tx_hashes = vec![];
for item in tx_sequence_info_mapping {
for item in tx_sequence_info_mapping.clone() {
if item.is_none() {
return Err(JsonRpcError::Custom(String::from(
"Invalid tx hash or tx order",
"The tx hash corresponding to tx order does not exist",
)));
}
tx_hashes.push(item.unwrap().tx_hash);
Expand All @@ -286,9 +276,10 @@ impl RoochAPIServer for RoochServer {

let data = self
.aggregate_service
.get_transaction_results_by_hash_and_order(tx_hashes, tx_orders)
.get_transaction_with_info(tx_hashes, tx_sequence_info_mapping)
.await?
.into_iter()
.flatten()
.map(TransactionWithInfoView::from)
.collect::<Vec<_>>();

Expand Down
69 changes: 48 additions & 21 deletions crates/rooch-rpc-server/src/service/aggregate_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use rooch_types::account::BalanceInfo;
use rooch_types::framework::account_coin_store::AccountCoinStoreModule;
use rooch_types::framework::coin::{CoinInfo, CoinModule};
use rooch_types::framework::coin_store::CoinStore;
use rooch_types::transaction::TransactionSequenceInfoMapping;
use std::collections::HashMap;
use tokio::runtime::Handle;

Expand Down Expand Up @@ -175,11 +176,19 @@ impl AggregateService {
}
}

pub async fn get_transaction_results_by_hash_and_order(
pub async fn get_transaction_with_info(
&self,
tx_hashes: Vec<H256>,
tx_orders: Vec<u128>,
) -> Result<Vec<TransactionWithInfo>> {
tx_sequence_info_mapping: Vec<Option<TransactionSequenceInfoMapping>>,
) -> Result<Vec<Option<TransactionWithInfo>>> {
// If the tx hash is invalid, filled None when returned.
let tx_orders = tx_sequence_info_mapping
.clone()
.iter()
.flatten()
.map(|m| m.tx_order)
.collect();

let transactions = self
.rpc_service
.get_transactions_by_hash(tx_hashes.clone())
Expand All @@ -195,26 +204,44 @@ impl AggregateService {
.get_transaction_execution_infos_by_hash(tx_hashes.clone())
.await?;

assert!(
transactions.len() == sequence_infos.len()
debug_assert!(
transactions.len() >= sequence_infos.len()
&& transactions.len() == execution_infos.len()
);
let mut transaction_with_info: Vec<TransactionWithInfo> = vec![];
for (index, _tx_hash) in tx_hashes.iter().enumerate() {
let transaction_result = TransactionWithInfo {
transaction: transactions[index].clone().ok_or(anyhow::anyhow!(
"Transaction should have value when construct TransactionResult"
))?,
sequence_info: sequence_infos[index].clone().ok_or(anyhow::anyhow!(
"TransactionSequenceInfo should have value when construct TransactionResult"
))?,
execution_info: execution_infos[index].clone().ok_or(anyhow::anyhow!(
"TransactionExecutionInfo should have value when construct TransactionResult"
))?,
};
transaction_with_info.push(transaction_result)
}
Ok(transaction_with_info)
let sequence_info_map = sequence_infos
.into_iter()
.flatten()
.map(|sequence_info| (sequence_info.tx_order, sequence_info))
.collect::<HashMap<_, _>>(); // collect into a hashmap

tx_sequence_info_mapping
.iter()
.enumerate()
.map(|(index, tx_mapping_opt)| {
match tx_mapping_opt {
Some(tx_mapping) => {
let sequence_info = match sequence_info_map.get(&tx_mapping.tx_order) {
Some(v) => v.clone(),
None => {
return Err(anyhow::anyhow!(
"TransactionSequenceInfo should exist when construct TransactionWithInfo"
))
}
};
Ok(Some(TransactionWithInfo {
transaction: transactions[index].clone().ok_or(anyhow::anyhow!(
"Transaction should exist when construct TransactionWithInfo"
))?,
sequence_info,
execution_info: execution_infos[index].clone().ok_or(anyhow::anyhow!(
"TransactionExecutionInfo should exist when construct TransactionWithInfo"
))?,
}))
},
None => Ok(None),
}
})
.collect::<Result<Vec<_>>>()
}
}

Expand Down

0 comments on commit 0965f5a

Please sign in to comment.