diff --git a/Cargo.lock b/Cargo.lock index 6a383a695d..4a066bb2fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2186,11 +2186,12 @@ dependencies = [ [[package]] name = "bonsai-trie" version = "0.1.0" -source = "git+https://github.com/dojoengine/bonsai-trie/?branch=kariy/public-path#9108a1264fd82f5e68f5056f8df767042440825f" +source = "git+https://github.com/dojoengine/bonsai-trie/?branch=kariy/indexmap#d2741d49d14210f675195a9a607910bf76bbed04" dependencies = [ "bitvec", "derive_more 0.99.18", "hashbrown 0.14.5", + "indexmap 2.5.0", "log", "parity-scale-codec", "rayon", diff --git a/crates/katana/rpc/rpc-types/src/trie.rs b/crates/katana/rpc/rpc-types/src/trie.rs index 1a7ea2d3cd..58f797cc12 100644 --- a/crates/katana/rpc/rpc-types/src/trie.rs +++ b/crates/katana/rpc/rpc-types/src/trie.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::ops::{Deref, DerefMut}; use katana_primitives::contract::StorageKey; use katana_primitives::{ContractAddress, Felt}; @@ -84,45 +84,26 @@ pub struct ContractLeafData { pub struct ContractStorageProofs { pub nodes: Vec, } +#[derive(Debug, Serialize, Deserialize)] +pub struct NodeWithHash { + pub node_hash: Felt, + pub node: MerkleNode, +} -#[derive(Debug, Default)] -pub struct Nodes(pub HashMap); - -impl Serialize for Nodes { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - use serde::ser::SerializeSeq; - - #[derive(Debug, Serialize)] - struct NodeEntry<'a> { - node_hash: &'a Felt, - node: &'a MerkleNode, - } +#[derive(Debug, Default, Serialize, Deserialize)] +#[serde(transparent)] +pub struct Nodes(pub Vec); - let mut seq = serializer.serialize_seq(Some(self.0.len()))?; - for (node_hash, node) in &self.0 { - seq.serialize_element(&NodeEntry { node_hash, node })?; - } - seq.end() +impl Deref for Nodes { + type Target = Vec; + fn deref(&self) -> &Self::Target { + &self.0 } } -impl<'de> Deserialize<'de> for Nodes { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - #[derive(Debug, Deserialize)] - struct NodeEntry { - node_hash: Felt, - node: MerkleNode, - } - - let entries: Vec = Vec::deserialize(deserializer)?; - let map = entries.into_iter().map(|entry| (entry.node_hash, entry.node)).collect(); - Ok(Nodes(map)) +impl DerefMut for Nodes { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 } } @@ -130,13 +111,19 @@ impl<'de> Deserialize<'de> for Nodes { impl From for Nodes { fn from(value: MultiProof) -> Self { - Self(value.0.into_iter().map(|(hash, node)| (hash, MerkleNode::from(node))).collect()) + Self( + value + .0 + .into_iter() + .map(|(hash, node)| NodeWithHash { node_hash: hash, node: MerkleNode::from(node) }) + .collect(), + ) } } impl From for MultiProof { fn from(value: Nodes) -> Self { - Self(value.0.into_iter().map(|(hash, node)| (hash, ProofNode::from(node))).collect()) + Self(value.0.into_iter().map(|node| (node.node_hash, ProofNode::from(node.node))).collect()) } } diff --git a/crates/katana/trie/Cargo.toml b/crates/katana/trie/Cargo.toml index 871009cf7f..5c6f948e4b 100644 --- a/crates/katana/trie/Cargo.toml +++ b/crates/katana/trie/Cargo.toml @@ -17,7 +17,7 @@ starknet-types-core.workspace = true thiserror.workspace = true [dependencies.bonsai-trie] -branch = "kariy/public-path" +branch = "kariy/indexmap" default-features = false features = [ "std" ] git = "https://github.com/dojoengine/bonsai-trie/"