Skip to content

Commit

Permalink
fix: loading storage tree is infallible
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirko-von-Leipzig committed Sep 8, 2023
1 parent 215549a commit 5c07c43
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 16 deletions.
7 changes: 2 additions & 5 deletions crates/merkle-tree/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,11 @@ pub struct StorageCommitmentTree<'tx> {
}

impl<'tx> StorageCommitmentTree<'tx> {
pub fn load(
transaction: &'tx Transaction<'tx>,
root: StorageCommitment,
) -> anyhow::Result<Self> {
pub fn load(transaction: &'tx Transaction<'tx>, root: StorageCommitment) -> Self {
let tree = MerkleTree::new(root.0);
let storage = transaction.storage_trie_reader();

Ok(Self { tree, storage })
Self { tree, storage }
}

pub fn with_verify_hashes(mut self, verify_hashes: bool) -> Self {
Expand Down
3 changes: 2 additions & 1 deletion crates/merkle-tree/src/contract_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ pub fn update_contract_state(

// Load the contract tree and insert the updates.
let new_root = if !updates.is_empty() {
let mut contract_tree = ContractsStorageTree::load(transaction, old_root).with_verify_hashes(verify_hashes);
let mut contract_tree =
ContractsStorageTree::load(transaction, old_root).with_verify_hashes(verify_hashes);
for (key, value) in updates {
contract_tree
.set(*key, *value)
Expand Down
5 changes: 3 additions & 2 deletions crates/merkle-tree/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use std::{cell::RefCell, rc::Rc};
pub struct MerkleTree<H: FeltHash, const HEIGHT: usize> {
root: Rc<RefCell<InternalNode>>,
_hasher: std::marker::PhantomData<H>,
/// If enables, node hashes are verified as they are resolved. This allows
/// If enables, node hashes are verified as they are resolved. This allows
/// testing for database corruption.
verify_hashes: bool,
}
Expand Down Expand Up @@ -527,7 +527,8 @@ impl<H: FeltHash, const HEIGHT: usize> MerkleTree<H, HEIGHT> {
if self.verify_hashes {
let calculated_hash = node.hash::<H>();

anyhow::ensure!(hash == calculated_hash,
anyhow::ensure!(
hash == calculated_hash,
r"Node data is corrupt.
Expected: {hash}
Expand Down
1 change: 0 additions & 1 deletion crates/pathfinder/src/state/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,6 @@ fn update_starknet_state(
.unwrap_or((StorageCommitment::ZERO, ClassCommitment::ZERO));

let mut storage_commitment_tree = StorageCommitmentTree::load(transaction, storage_commitment)
.context("Loading storage trie")?
.with_verify_hashes(verify_hashes);

for (contract, update) in &state_update.contract_updates {
Expand Down
8 changes: 3 additions & 5 deletions crates/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ pub mod test_utils {
.unwrap();

let mut storage_commitment_tree =
StorageCommitmentTree::load(&db_txn, StorageCommitment(Felt::ZERO)).unwrap();
StorageCommitmentTree::load(&db_txn, StorageCommitment(Felt::ZERO));
let contract_state_hash = update_contract_state(
contract0_addr,
&contract0_update,
Expand All @@ -291,8 +291,7 @@ pub mod test_utils {
.insert_storage_trie(storage_commitment0, &nodes)
.unwrap();

let mut storage_commitment_tree =
StorageCommitmentTree::load(&db_txn, storage_commitment0).unwrap();
let mut storage_commitment_tree = StorageCommitmentTree::load(&db_txn, storage_commitment0);
let contract_state_hash = update_contract_state(
contract1_addr,
&contract1_update0,
Expand Down Expand Up @@ -324,8 +323,7 @@ pub mod test_utils {
.insert_storage_trie(storage_commitment1, &nodes)
.unwrap();

let mut storage_commitment_tree =
StorageCommitmentTree::load(&db_txn, storage_commitment1).unwrap();
let mut storage_commitment_tree = StorageCommitmentTree::load(&db_txn, storage_commitment1);
let contract_state_hash = update_contract_state(
contract1_addr,
&contract1_update2,
Expand Down
3 changes: 1 addition & 2 deletions crates/rpc/src/pathfinder/methods/get_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ pub async fn get_proof(
)
};

let mut storage_commitment_tree =
StorageCommitmentTree::load(&tx, storage_commitment).context("Loading storage trie")?;
let mut storage_commitment_tree = StorageCommitmentTree::load(&tx, storage_commitment);

// Generate a proof for this contract. If the contract does not exist, this will
// be a "non membership" proof.
Expand Down

0 comments on commit 5c07c43

Please sign in to comment.