From 2b96865574773777762909d6536bcd7b6ac59c84 Mon Sep 17 00:00:00 2001 From: Tbelleng <117627242+Tbelleng@users.noreply.github.com> Date: Wed, 17 Jan 2024 14:13:02 +0000 Subject: [PATCH] feat: getNonce fails for uninitialized accounts (#1374) --- .devcontainer/devcontainer.json | 6 +++--- CHANGELOG.md | 1 + .../storage/src/overrides/schema_v1_override.rs | 2 ++ starknet-rpc-test/get_block_with_txs.rs | 11 ++++------- starknet-rpc-test/get_nonce.rs | 15 ++++++++------- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index ee58312177..2fa188886c 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -6,11 +6,11 @@ "ghcr.io/devcontainers/features/git:1": {}, "ghcr.io/devcontainers/features/github-cli:1": {}, "ghcr.io/devcontainers/features/node:1": {}, - "ghcr.io/devcontainers-contrib/features/protoc-asdf:1": {} + "ghcr.io/devcontainers-contrib/features/protoc-asdf:1": {}, }, "hostRequirements": { "cpus": 8, "memory": "16gb", - "storage": "128gb" - } + "storage": "128gb", + }, } diff --git a/CHANGELOG.md b/CHANGELOG.md index c77c099396..df893c73ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ - fix: `tempdir` crate has been deprecated; use `tempfile` instead - dev: add avail and celestia crates behind a feature flag - dev: replace md5 with sha3_256 hash function +- feat: fixing getNonce Rpc Call and adding a new test ## v0.6.0 diff --git a/crates/client/storage/src/overrides/schema_v1_override.rs b/crates/client/storage/src/overrides/schema_v1_override.rs index fd101831c9..3205213b45 100644 --- a/crates/client/storage/src/overrides/schema_v1_override.rs +++ b/crates/client/storage/src/overrides/schema_v1_override.rs @@ -117,6 +117,8 @@ where } fn nonce(&self, block_hash: ::Hash, address: ContractAddress) -> Option { + self.contract_class_hash_by_address(block_hash, address)?; + let storage_nonce_prefix = storage_prefix_build(PALLET_STARKNET, STARKNET_NONCE); let nonce = self.query_storage::( block_hash, diff --git a/starknet-rpc-test/get_block_with_txs.rs b/starknet-rpc-test/get_block_with_txs.rs index 3f1d8c5cce..3791aed2ad 100644 --- a/starknet-rpc-test/get_block_with_txs.rs +++ b/starknet-rpc-test/get_block_with_txs.rs @@ -101,14 +101,13 @@ async fn works_with_deploy_account_txn(madara: &ThreadSafeMadaraClient) -> Resul let contract_address_salt = FieldElement::ONE; let max_fee = FieldElement::from_hex_be(MAX_FEE_OVERRIDE).unwrap(); - let (deploy_nonce, block) = { + let block = { let mut madara_write_lock = madara.write().await; let oz_factory = build_oz_account_factory(&rpc, "0x123", class_hash).await; let account_deploy_txn = build_deploy_account_tx(&oz_factory, FieldElement::ONE); let funding_account = build_single_owner_account(&rpc, SIGNER_PRIVATE, ARGENT_CONTRACT_ADDRESS, true); let account_address = account_deploy_txn.address(); - let deploy_nonce = rpc.get_nonce(BlockId::Tag(BlockTag::Latest), account_deploy_txn.address()).await?; // We execute the funding in a different block, because we have no way to guarantee the tx execution // order once in the mempool @@ -122,12 +121,10 @@ async fn works_with_deploy_account_txn(madara: &ThreadSafeMadaraClient) -> Resul madara_write_lock.create_block_with_txs(vec![Transaction::AccountDeployment(account_deploy_txn)]).await?; - let block = match rpc.get_block_with_txs(BlockId::Tag(BlockTag::Latest)).await.unwrap() { + match rpc.get_block_with_txs(BlockId::Tag(BlockTag::Latest)).await.unwrap() { MaybePendingBlockWithTxs::Block(block) => block, MaybePendingBlockWithTxs::PendingBlock(_) => return Err(anyhow!("Expected block, got pending block")), - }; - - (deploy_nonce, block) + } }; assert_eq!(block.transactions.len(), 1); @@ -135,7 +132,7 @@ async fn works_with_deploy_account_txn(madara: &ThreadSafeMadaraClient) -> Resul StarknetTransaction::DeployAccount(tx) => tx, _ => return Err(anyhow!("Expected an deploy transaction v1")), }; - assert_eq!(tx.nonce, deploy_nonce); + assert_eq!(tx.nonce, 0u8.into()); assert_eq!(tx.max_fee, max_fee); assert_eq!(tx.contract_address_salt, contract_address_salt); assert_eq!(tx.class_hash, class_hash); diff --git a/starknet-rpc-test/get_nonce.rs b/starknet-rpc-test/get_nonce.rs index a3122402e8..4550ec4c44 100644 --- a/starknet-rpc-test/get_nonce.rs +++ b/starknet-rpc-test/get_nonce.rs @@ -36,16 +36,17 @@ async fn fail_non_existing_block(madara: &ThreadSafeMadaraClient) -> Result<(), #[rstest] #[tokio::test] -async fn work_ok_non_used_contract_address(madara: &ThreadSafeMadaraClient) -> Result<(), anyhow::Error> { +async fn fail_non_existing_contract(madara: &ThreadSafeMadaraClient) -> Result<(), anyhow::Error> { let rpc = madara.get_starknet_client().await; - assert_eq!( - rpc.get_nonce( - BlockId::Number(0), - FieldElement::from_hex_be("0x4269DEADBEEF").expect("Invalid Contract Address") + assert_matches!( + rpc + .get_nonce( + BlockId::Tag(BlockTag::Latest), + FieldElement::from_hex_be("0x51e59c2c182a58fb0a74349bfa4769cbbcba32547591dd3fb1def8623997d00").unwrap(), ) - .await?, - FieldElement::ZERO + .await, + Err(ProviderError::StarknetError(StarknetErrorWithMessage { code: MaybeUnknownErrorCode::Known(code), .. })) if code == StarknetError::ContractNotFound ); Ok(())