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

Commit

Permalink
Merge branch 'main' into remove-duplicated-polkadot-sdk-repo
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelabro authored Nov 18, 2023
2 parents 58359c4 + d90cb1a commit 4707887
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## Next release

- chore: remove crates that have been copy-pasted from plkdtSDK
- fix: Wait for 1 minute for transaction to be processed in
get_transaction_receipt rpc
- ci: Fix starknet foundry sncast not found
- fix: Ensure transaction checks are compatible with starknet-rs
- ci: Run Starknet Foundry tests against Madara RPC
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion crates/client/rpc-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,8 @@ pub trait StarknetRpcApi {

/// Returns the receipt of a transaction by transaction hash.
#[method(name = "getTransactionReceipt")]
fn get_transaction_receipt(&self, transaction_hash: FieldElement) -> RpcResult<MaybePendingTransactionReceipt>;
async fn get_transaction_receipt(
&self,
transaction_hash: FieldElement,
) -> RpcResult<MaybePendingTransactionReceipt>;
}
1 change: 1 addition & 0 deletions crates/client/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ mp-hashers = { workspace = true }
mp-transactions = { workspace = true, features = ["client"] }
serde_json = { workspace = true, default-features = true }
thiserror = { workspace = true }
tokio = { workspace = true, default-features = true, features = ["time"] }

[dev-dependencies]
rstest = { workspace = true }
Expand Down
61 changes: 46 additions & 15 deletions crates/client/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::sync::Arc;
use errors::StarknetRpcApiError;
use jsonrpsee::core::{async_trait, RpcResult};
use log::error;
use mc_db::Backend as MadaraBackend;
pub use mc_rpc_core::utils::*;
use mc_rpc_core::Felt;
pub use mc_rpc_core::StarknetRpcApiServer;
Expand Down Expand Up @@ -887,24 +888,53 @@ where
/// # Arguments
///
/// * `transaction_hash` - Transaction hash corresponding to the transaction.
fn get_transaction_receipt(&self, transaction_hash: FieldElement) -> RpcResult<MaybePendingTransactionReceipt> {
let block_hash_from_db = self
.backend
.mapping()
.block_hash_from_transaction_hash(H256::from(transaction_hash.to_bytes_be()))
.map_err(|e| {
error!("Failed to get transaction's substrate block hash from mapping_db: {e}");
StarknetRpcApiError::TxnHashNotFound
})?;
async fn get_transaction_receipt(
&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 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 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;
}
};
}

let substrate_block_hash = match block_hash_from_db {
Some(block_hash) => block_hash,
None => {
// If the transaction is still in the pool, the receipt
// is not available, thus considered as not found.
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 block: mp_block::Block =
get_block_by_block_hash(self.client.as_ref(), substrate_block_hash).unwrap_or_default();
Expand All @@ -920,6 +950,7 @@ where
StarknetRpcApiError::InternalServerError
})?
.ok_or(StarknetRpcApiError::BlockNotFound)?;

let chain_id = self.chain_id()?.0.into();

let (tx_type, events) = self
Expand Down

0 comments on commit 4707887

Please sign in to comment.