Skip to content

Commit

Permalink
Merge pull request #26 from Trantorian1/test/get_transaction_status
Browse files Browse the repository at this point in the history
✅ Tested `starknet_getTransactionStatus`
  • Loading branch information
antiyro authored Jan 11, 2024
2 parents e8cea3b + d31bd76 commit b074f4b
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 23 deletions.
24 changes: 12 additions & 12 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions unit_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ serde = "1.0.194"
serde_json = "1.0.110"
tokio = { version = "1", features = ["full"] }
url = "2.5.0"
starknet = { git = "https://github.com/xJonathanLEI/starknet-rs.git", rev = "a35ce22", default-features = false }
starknet-core = { git = "https://github.com/xJonathanLEI/starknet-rs.git", rev = "a35ce22", default-features = false }
starknet-providers = { git = "https://github.com/xJonathanLEI/starknet-rs.git", rev = "a35ce22", default-features = false }
starknet = { git = "https://github.com/xJonathanLEI/starknet-rs.git", rev = "64ebc36", default-features = false }
starknet-core = { git = "https://github.com/xJonathanLEI/starknet-rs.git", rev = "64ebc36", default-features = false }
starknet-providers = { git = "https://github.com/xJonathanLEI/starknet-rs.git", rev = "64ebc36", default-features = false }

[dev-dependencies]
jsonrpsee = { version = "0.21.0", features = ["client"] }
Expand Down
3 changes: 3 additions & 0 deletions unit_tests/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,6 @@ pub const TEST_CONTRACT_CLASS_HASH: &str = "";
pub const ETHEREUM_ADDRESS: &str = "";
pub const INVALID_ETHEREUM_ADDRESS: &str = "";
pub const SELECTOR_NAME: &str = "";

pub const ERR_DEOXYS: &str = "Error waiting for response from Deoxys client";
pub const ERR_PATHFINDER: &str = "Error waiting for response from Pathfinder client";
13 changes: 5 additions & 8 deletions unit_tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#![feature(assert_matches)]

use std::{fs::File, io::Read};

use constants::*;
use serde::Deserialize;
use starknet_core::{
types::{BroadcastedInvokeTransaction, BroadcastedTransaction, FieldElement},
utils::get_selector_from_name,
};

use anyhow::Context;
use serde::Deserialize;
use serde_json::from_str;
use std::{fs::File, io::Read};

pub mod constants;
pub mod fixtures;
pub mod macros;
Expand All @@ -28,13 +26,12 @@ impl TestConfig {

file.read_to_string(&mut content)?;

let config: TestConfig = from_str(&content)
.with_context(|| format!("Could not deserialize test at {path} into Config"))?;
let config: TestConfig = serde_json::from_str(&content)
.expect("Could not deserialize test at {path} into Config");

Ok(config)
}
}

pub trait TransactionFactory {
fn build(nonce: Option<FieldElement>) -> BroadcastedTransaction;
}
Expand Down
134 changes: 134 additions & 0 deletions unit_tests/tests/test_get_transaction_status.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#![feature(assert_matches)]

mod common;
use std::{assert_matches::assert_matches, collections::HashMap};

use common::*;
use starknet_core::types::{
BlockId, BlockTag, FieldElement, StarknetError, TransactionExecutionStatus, TransactionStatus,
};
use starknet_providers::{
jsonrpc::HttpTransport, JsonRpcClient, MaybeUnknownErrorCode, Provider, ProviderError,
StarknetErrorWithMessage,
};

///
/// Unit test for `starknet_getTransactionStatus`
///
/// purpose: call getTransactionStatus on non-existent transaction hash.
/// fail case: non-existent transaction hash.
///
#[rstest]
#[tokio::test]
async fn fail_invalid_transaction(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
let deoxys = &clients[DEOXYS];

let response_deoxys = deoxys
.get_transaction_status(FieldElement::ZERO)
.await
.err();

assert_matches!(
response_deoxys,
Some(ProviderError::StarknetError(StarknetErrorWithMessage {
message: _,
code: MaybeUnknownErrorCode::Known(StarknetError::TransactionHashNotFound)
}))
);
}

///
/// Unit test for `starknet_getTransactionStatus`
///
/// purpose: call getTransactionStatus on transaction which has been accepted on L1.
/// success case: retrieved transaction has been accepted on L1.
///
#[rstest]
#[tokio::test]
async fn work_transaction_accepted_on_l1(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];

let response_deoxys = deoxys
.get_transaction_status(FieldElement::from_hex_be(TRANSACTION_INVOKE).unwrap())
.await
.expect(ERR_DEOXYS);

let response_pathfinder = pathfinder
.get_transaction_status(FieldElement::from_hex_be(TRANSACTION_INVOKE).unwrap())
.await
.expect(ERR_PATHFINDER);

assert_matches!(response_deoxys, TransactionStatus::AcceptedOnL1(_));
assert_eq!(response_deoxys, response_pathfinder);
}

///
/// Unit test for `starknet_getTransactionStatus`
///
/// purpose: call getTransactionStatus on last transaction from the latest block.
/// success case: transaction is marked as accepted on L2.
///
#[rstest]
#[tokio::test]
async fn work_transaction_accepted_on_l2(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];

let transaction_count = deoxys
.get_block_transaction_count(BlockId::Tag(BlockTag::Latest))
.await
.expect(ERR_DEOXYS);

// last transaction of latest block
let transaction = deoxys
.get_transaction_by_block_id_and_index(
BlockId::Tag(BlockTag::Latest),
transaction_count - 1,
)
.await
.expect(ERR_DEOXYS);

let response_deoxys = deoxys
.get_transaction_status(transaction.transaction_hash())
.await
.expect(ERR_DEOXYS);

let response_pathfinder = pathfinder
.get_transaction_status(transaction.transaction_hash())
.await
.expect(ERR_PATHFINDER);

// note that transaction is still accepted on L2 if it is reverted!
assert_matches!(response_deoxys, TransactionStatus::AcceptedOnL2(_));
assert_eq!(response_deoxys, response_pathfinder);
}

///
/// Unit test for `starknet_getTransactionStatus`
///
/// purpose: call getTransactionStatus on reverted transaction.
/// success case: transaction is marked as reverted on L1.
///
#[rstest]
#[tokio::test]
async fn work_transaction_reverted(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];

let response_deoxys = deoxys
.get_transaction_status(FieldElement::from_hex_be(TRANSACTION_REVERTED).unwrap())
.await
.expect(ERR_DEOXYS);

let response_pathfinder = pathfinder
.get_transaction_status(FieldElement::from_hex_be(TRANSACTION_REVERTED).unwrap())
.await
.expect(ERR_PATHFINDER);

assert_matches!(
response_deoxys,
TransactionStatus::AcceptedOnL1(TransactionExecutionStatus::Reverted)
);
assert_eq!(response_deoxys, response_pathfinder);
}

0 comments on commit b074f4b

Please sign in to comment.