Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue when the node at height 250 is a binary #32

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ rstest = "0.18.2"
test-log = "0.2.15"
indexmap = "2.2.6"
criterion = "0.5.1"
serde_json = "1.0.68"

[[bench]]
name = "storage"
Expand Down
49 changes: 48 additions & 1 deletion src/tests/proof.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![cfg(all(feature = "std", feature = "rocksdb"))]
use bitvec::vec::BitVec;
use bitvec::{order::Msb0, vec::BitVec, view::BitView};
use pathfinder_common::{hash::PedersenHash, trie::TrieNode};
use pathfinder_crypto::Felt as PathfinderFelt;
use pathfinder_merkle_tree::tree::{MerkleTree, TestStorage};
Expand Down Expand Up @@ -111,6 +111,53 @@ fn assert_eq_proof(bonsai_proof: &[ProofNode], pathfinder_proof: &[TrieNode]) {
}
}

#[test]
fn debug_deoxys() {
// Load storage_data.json file
let storage_data = include_str!("storage_data.json");
let storage_data: Vec<Vec<(String, String)>> = serde_json::from_str(storage_data).unwrap();
let tempdir = tempfile::tempdir().unwrap();
let db = create_rocks_db(tempdir.path()).unwrap();
let config = BonsaiStorageConfig::default();
let mut storage = pathfinder_merkle_tree::tree::TestStorage::default();
let mut id_builder = BasicIdBuilder::new();
let mut bonsai_storage =
BonsaiStorage::<_, _, Pedersen>::new(RocksDB::new(&db, RocksDBConfig::default()), config)
.unwrap();
let mut pathfinder_merkle_tree: MerkleTree<PedersenHash, 251> =
pathfinder_merkle_tree::tree::MerkleTree::empty();
let identifier =
Felt::from_hex("0x04acd4b2a59eae7196f6a5c26ead8cb5f9d7ad3d911096418a23357bb2eac075")
.unwrap()
.to_bytes_be()
.to_vec();
for block_changes in storage_data.iter() {
for pair in block_changes.iter() {
let key = keyer(Felt::from_hex(&pair.0).unwrap());
let value = Felt::from_hex(&pair.1).unwrap();
bonsai_storage.insert(&identifier, &key, &value).unwrap();
pathfinder_merkle_tree
.set(
&storage,
key,
PathfinderFelt::from_hex_str(&pair.1).unwrap(),
)
.unwrap();
}
bonsai_storage.commit(id_builder.new_id()).unwrap();
let (_, root_id) = commit_and_persist(pathfinder_merkle_tree.clone(), &mut storage);
let pathfinder_root = storage.nodes.get(&root_id).unwrap().0;
let bonsai_root = bonsai_storage.root_hash(&identifier).unwrap();
println!("{:#02x}", bonsai_root);
println!("{:#02x}", pathfinder_root);
assert_eq!(pathfinder_root.to_be_bytes(), bonsai_root.to_bytes_be());
}
}

fn keyer(felt: Felt) -> BitVec<u8, Msb0> {
felt.to_bytes_be().view_bits()[5..].to_bitvec()
}

#[test]
fn basic_proof() {
let identifier = vec![];
Expand Down
25 changes: 25 additions & 0 deletions src/tests/storage_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[
[
[
"0x00a76c5ddd7dfe35c6511a0d8085a9ab719e85edfc8ef4abbccf60dc1340b558",
"0x05c1bca21a1851421fbbf73ad2e0f82c50bc67fb09fdb221bdc2fd96fed49db8"
],
[
"0x00a76c5ddd7dfe35c6511a0d8085a9ab719e85edfc8ef4abbccf60dc1340b559",
"0x03dbd160736e9b9b51ea9a79a8ed86f427a62e0e377d60335d2ec895c27025bb"
]
],
[
[
"0x00a76c5ddd7dfe35c6511a0d8085a9ab719e85edfc8ef4abbccf60dc1340b559",
"0x00dd45c619a4842d6e392a60041fc2a2a896f4e6e2d056acdff7e9c64d9a1819"
]
],
[
[
"0x00a76c5ddd7dfe35c6511a0d8085a9ab719e85edfc8ef4abbccf60dc1340b559",
"0x03dbd160736e9b9b51ea9a79a8ed86f427a62e0e377d60335d2ec895c27025bb"
]
]
]

11 changes: 7 additions & 4 deletions src/trie/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,8 @@ impl<H: StarkHash + Send + Sync> MerkleTree<H> {
Direction::Left => binary.left = NodeHandle::Hash(value),
Direction::Right => binary.right = NodeHandle::Hash(value),
};
self.cache_leaf_modified
.insert(key_bytes, InsertOrRemove::Insert(value));
}
}
_ => {}
Expand Down Expand Up @@ -1214,7 +1216,6 @@ impl<H: StarkHash + Send + Sync> MerkleTree<H> {
self.latest_node_id.next_id();
*prev_handle = NodeHandle::InMemory(self.latest_node_id);
let node = self.storage_nodes.0.entry(self.latest_node_id).insert(node);

(self.latest_node_id, node.into_mut())
}
NodeHandle::InMemory(node_id) => {
Expand All @@ -1227,8 +1228,7 @@ impl<H: StarkHash + Send + Sync> MerkleTree<H> {
.ok_or(BonsaiStorageError::Trie(
"Couldn't get node from temp storage".to_string(),
))?;

(node_id.clone(), node)
(node_id, node)
}
};

Expand All @@ -1242,6 +1242,9 @@ impl<H: StarkHash + Send + Sync> MerkleTree<H> {
Node::Binary(binary_node) => {
let next_direction = binary_node.direction(dst);
path.0.push(bool::from(next_direction));
if path.0 == dst {
break; // found it :)
}
prev_handle = binary_node.get_child_mut(next_direction);
}

Expand Down Expand Up @@ -1269,7 +1272,7 @@ impl<H: StarkHash + Send + Sync> MerkleTree<H> {
path: &Path,
) -> Result<Option<Node>, BonsaiStorageError<DB::DatabaseError>> {
let path: Vec<u8> = path.into();
db.get(&TrieKey::new(&identifier, TrieKeyType::Trie, &path))?
db.get(&TrieKey::new(identifier, TrieKeyType::Trie, &path))?
.map(|node| {
Node::decode(&mut node.as_slice()).map_err(|err| {
BonsaiStorageError::Trie(format!("Couldn't decode node: {}", err))
Expand Down
Loading