Skip to content

Commit

Permalink
feat: add verification of the RPC version (#1782)
Browse files Browse the repository at this point in the history
* feat: add verification of the RPC version

* fix: ensure torii test is waiting test txs to be mined
  • Loading branch information
glihm authored Apr 6, 2024
1 parent 1c90f0f commit 5015ea1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
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.

1 change: 1 addition & 0 deletions bin/sozo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ tracing-log = "0.1.3"
tracing.workspace = true
url.workspace = true
sozo-ops.workspace = true
katana-rpc-api.workspace = true

cainome = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.2.2" }

Expand Down
12 changes: 12 additions & 0 deletions bin/sozo/src/commands/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::{anyhow, Context, Result};
use clap::{Args, Subcommand};
use dojo_lang::compiler::MANIFESTS_DIR;
use dojo_world::metadata::{dojo_metadata_from_workspace, Environment};
use katana_rpc_api::starknet::RPC_SPEC_VERSION;
use scarb::core::{Config, Workspace};
use sozo_ops::migration;
use starknet::accounts::{Account, ConnectedAccount, SingleOwnerAccount};
Expand Down Expand Up @@ -77,6 +78,17 @@ pub async fn setup_env<'a>(

let (account, chain_id) = {
let provider = starknet.provider(env)?;

let spec_version = provider.spec_version().await?;

if spec_version != RPC_SPEC_VERSION {
return Err(anyhow!(
"Unsupported Starknet RPC version: {}, expected {}.",
spec_version,
RPC_SPEC_VERSION
));
}

let chain_id = provider.chain_id().await?;
let chain_id = parse_cairo_short_string(&chain_id)
.with_context(|| "Cannot parse chain_id as string")?;
Expand Down
33 changes: 28 additions & 5 deletions crates/katana/rpc/rpc/tests/torii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use dojo_test_utils::sequencer::{get_default_test_starknet_config, TestSequencer
use jsonrpsee::http_client::HttpClientBuilder;
use katana_core::sequencer::SequencerConfig;
use katana_rpc_api::dev::DevApiClient;
use katana_rpc_api::starknet::StarknetApiClient;
use katana_rpc_api::torii::ToriiApiClient;
use katana_rpc_types::transaction::{TransactionsPage, TransactionsPageCursor};
use starknet::accounts::{Account, Call};
use starknet::core::types::FieldElement;
use starknet::core::types::{FieldElement, TransactionStatus};
use starknet::core::utils::get_selector_from_name;
use tokio::time::sleep;

Expand Down Expand Up @@ -109,17 +110,39 @@ async fn test_get_transactions() {

let max_fee = FieldElement::from_hex_be(ENOUGH_GAS).unwrap();
let mut nonce = FieldElement::THREE;
let mut last_tx_hash = FieldElement::ZERO;

// Test only returns first 100 txns from pending block
for i in 0..101 {
let deploy_call = build_deploy_contract_call(declare_res.class_hash, (i + 2_u32).into());
let deploy_txn = account.execute(vec![deploy_call]).nonce(nonce).max_fee(max_fee);
deploy_txn.send().await.unwrap();
let res = deploy_txn.send().await.unwrap();
nonce += FieldElement::ONE;

if i == 100 {
last_tx_hash = res.transaction_hash;
}
}

// Wait until all pending txs have been mined.
// @kairy is there a more deterministic approach here?
sleep(Duration::from_millis(5000)).await;
assert!(last_tx_hash != FieldElement::ZERO);

// Poll the statux of the last tx sent.
let max_retry = 10;
let mut attempt = 0;
loop {
match client.transaction_status(last_tx_hash).await {
Ok(s) => {
if s != TransactionStatus::Received {
break;
}
}
Err(_) => {
assert!(attempt < max_retry);
sleep(Duration::from_millis(300)).await;
attempt += 1;
}
}
}

let start_cursor = response.cursor;
let response: TransactionsPage = client.get_transactions(start_cursor).await.unwrap();
Expand Down

0 comments on commit 5015ea1

Please sign in to comment.