Skip to content

Commit

Permalink
refactor(katana): bump bonsai-trie to use IndexMap in multiproof (#…
Browse files Browse the repository at this point in the history
…2823)

update bonsai-trie to use indexmap in multiproof
  • Loading branch information
kariy committed Dec 31, 2024
1 parent 5524d6f commit 247a4ca
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 39 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 24 additions & 37 deletions crates/katana/rpc/rpc-types/src/trie.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::ops::{Deref, DerefMut};

use katana_primitives::contract::StorageKey;
use katana_primitives::{ContractAddress, Felt};
Expand Down Expand Up @@ -84,59 +84,46 @@ pub struct ContractLeafData {
pub struct ContractStorageProofs {
pub nodes: Vec<Nodes>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct NodeWithHash {
pub node_hash: Felt,
pub node: MerkleNode,
}

#[derive(Debug, Default)]
pub struct Nodes(pub HashMap<Felt, MerkleNode>);

impl Serialize for Nodes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<NodeWithHash>);

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<NodeWithHash>;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<'de> Deserialize<'de> for Nodes {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Debug, Deserialize)]
struct NodeEntry {
node_hash: Felt,
node: MerkleNode,
}

let entries: Vec<NodeEntry> = 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
}
}

// --- Conversion from/to internal types for convenience

impl From<MultiProof> 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<Nodes> 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())
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/katana/trie/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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/"

0 comments on commit 247a4ca

Please sign in to comment.