Skip to content

Commit

Permalink
fix: correctly handle get_proof for contract without a root index
Browse files Browse the repository at this point in the history
Key changes include:
- Adjusting the get_proof method to return an empty list when no contract root is found.
- Adding checks for None values when retrieving contract roots.

Pathfinder returns an internal error when one tries to fetch a proof for contract with a `contract_state_hash` but without a `root_index` for the current block height. Pathfinder returns none which is propagated down to an internal error.

The following is an example of a request that fails without this fix:
```json
{
  "id": "0",
  "jsonrpc": "2.0",
  "method": "pathfinder_getProof",
  "params": {
    "block_id": {
      "block_number": 155006
    },
    "contract_address": "0x40a29e36c82f868dc2b5712bc6729c6132ca75ae46d6c75718ecbd49a0c9fb7",
    "keys": [
      "0x206f38f7e4f15e87567361213c28f235cccdaa1d7fd34c9db1dfe9489c6a091"
    ]
  }
}
```

When there is no root_index, this fix will return empty `ProofNodes` ie `ProofNodes(vec![])` for each of the keys.

This fix also prevents multiple calls to the database fetching the same root (same contract and block number) while looping through the keys.
  • Loading branch information
whichqua committed Oct 4, 2024
1 parent da0a0bf commit aa96876
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
9 changes: 1 addition & 8 deletions crates/merkle-tree/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,8 @@ impl<'tx> ContractsStorageTree<'tx> {
contract: ContractAddress,
block: BlockNumber,
key: &BitSlice<u8, Msb0>,
root: u64,
) -> anyhow::Result<Option<Vec<TrieNode>>> {
let root = tx
.contract_root_index(block, contract)
.context("Querying contract root index")?;

let Some(root) = root else {
return Ok(None);
};

let storage = ContractStorage {
tx,
block: Some(block),
Expand Down
41 changes: 25 additions & 16 deletions crates/rpc/src/pathfinder/methods/get_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,24 +280,33 @@ pub async fn get_proof(
.context("Querying contract's nonce")?
.unwrap_or_default();

let root = tx
.contract_root_index(header.number, input.contract_address)
.context("Querying contract root index")?;

let mut storage_proofs = Vec::new();
for k in &input.keys {
let proof = ContractsStorageTree::get_proof(
&tx,
input.contract_address,
header.number,
k.view_bits(),
)
.context("Get proof from contract state tree")?
.ok_or_else(|| {
let e = anyhow!(
"Storage proof missing for key {:?}, but should be present",
k
);
tracing::warn!("{e}");
e
})?;
storage_proofs.push(ProofNodes(proof));
if let Some(root) = root {
let proof = ContractsStorageTree::get_proof(
&tx,
input.contract_address,
header.number,
k.view_bits(),
root,
)
.context("Get proof from contract state tree")?
.ok_or_else(|| {
let e = anyhow!(
"Storage proof missing for key {:?}, but should be present",
k
);
tracing::warn!("{e}");
e
})?;
storage_proofs.push(ProofNodes(proof));
} else {
storage_proofs.push(ProofNodes(vec![]));
}
}

let contract_data = ContractData {
Expand Down

0 comments on commit aa96876

Please sign in to comment.