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

Commit

Permalink
feat: remove loop from getTxReceipt (#1398)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelabro authored Jan 23, 2024
1 parent 36698fe commit 89933d7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next release

- fix: remove waiting loop from `getTxReceipt`
- feat: types in `mp-transactions` impl a method to get their version
- feat: make L1 gas price a `const` of the `RuntimeConfig`
- fix: broken class hashes and contracts in genesis
Expand Down
53 changes: 9 additions & 44 deletions crates/client/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use errors::StarknetRpcApiError;
use jsonrpsee::core::{async_trait, RpcResult};
use jsonrpsee::types::error::CallError;
use log::error;
use mc_db::Backend as MadaraBackend;
use mc_genesis_data_provider::GenesisProvider;
pub use mc_rpc_core::utils::*;
pub use mc_rpc_core::{
Expand Down Expand Up @@ -1324,49 +1323,15 @@ where
&self,
transaction_hash: FieldElement,
) -> RpcResult<MaybePendingTransactionReceipt> {
async fn wait_for_tx_inclusion<B: sp_api::BlockT>(
madara_backend: Arc<MadaraBackend<B>>,
transaction_hash: FieldElement,
) -> Result<<B as BlockT>::Hash, StarknetRpcApiError> {
let substrate_block_hash;

loop {
let substrate_block_hash_from_db = madara_backend
.mapping()
.block_hash_from_transaction_hash(H256::from(transaction_hash.to_bytes_be()))
.map_err(|e| {
error!("Failed to interact with db backend error: {e}");
StarknetRpcApiError::InternalServerError
})?;

match substrate_block_hash_from_db {
Some(block_hash) => {
substrate_block_hash = block_hash;
break;
}
None => {
// TODO: hardcoded to match the blocktime; make it dynamic
tokio::time::sleep(std::time::Duration::from_millis(6000)).await;
continue;
}
};
}

Ok(substrate_block_hash)
}

let substrate_block_hash = match tokio::time::timeout(
std::time::Duration::from_millis(60000),
wait_for_tx_inclusion(self.backend.clone(), transaction_hash),
)
.await
{
Err(_) => {
error!("did not receive tx hash within 1 minute");
return Err(StarknetRpcApiError::TxnHashNotFound.into());
}
Ok(res) => res,
}?;
let substrate_block_hash = self
.backend
.mapping()
.block_hash_from_transaction_hash(H256::from(transaction_hash.to_bytes_be()))
.map_err(|e| {
error!("Failed to interact with db backend error: {e}");
StarknetRpcApiError::InternalServerError
})?
.ok_or(StarknetRpcApiError::TxnHashNotFound)?;

let starknet_block: mp_block::Block =
get_block_by_block_hash(self.client.as_ref(), substrate_block_hash).unwrap_or_default();
Expand Down

0 comments on commit 89933d7

Please sign in to comment.