Skip to content

Commit

Permalink
fix(get_proof): incorrect ProofNode serialization, missing fields sho…
Browse files Browse the repository at this point in the history
…uld be serialized as nulls
  • Loading branch information
CHr15F0x committed Jan 15, 2025
1 parent 2b6d578 commit e7bc5cd
Showing 1 changed file with 61 additions and 9 deletions.
70 changes: 61 additions & 9 deletions crates/rpc/src/pathfinder/methods/get_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,12 @@ impl crate::dto::SerializeForVersion for ProofNodes {
let mut s = serializer.serialize_struct()?;
match self.0 {
TrieNode::Binary { left, right } => {
s.serialize_field("type", &"binary")?;
s.serialize_field("left", left)?;
s.serialize_field("right", right)?;
let mut inner = serializer.serialize_struct()?;
inner.serialize_field("left", left)?;
inner.serialize_field("right", right)?;
let inner = inner.end()?;

s.serialize_field("binary", &inner)?;
}
TrieNode::Edge { child, path } => {
let value = Felt::from_bits(path).unwrap();
Expand All @@ -143,9 +146,12 @@ impl crate::dto::SerializeForVersion for ProofNodes {
len: path.len(),
};

s.serialize_field("type", &"edge")?;
s.serialize_field("path", &path)?;
s.serialize_field("child", child)?;
let mut inner = serializer.serialize_struct()?;
inner.serialize_field("path", &path)?;
inner.serialize_field("child", child)?;
let inner = inner.end()?;

s.serialize_field("edge", &inner)?;
}
}
s.end()
Expand Down Expand Up @@ -227,8 +233,8 @@ impl crate::dto::SerializeForVersion for GetProofOutput {
serializer: crate::dto::Serializer,
) -> Result<crate::dto::Ok, crate::dto::Error> {
let mut serializer = serializer.serialize_struct()?;
serializer.serialize_optional("state_commitment", self.state_commitment)?;
serializer.serialize_optional("class_commitment", self.class_commitment)?;
serializer.serialize_optional_with_null("state_commitment", self.state_commitment)?;
serializer.serialize_optional_with_null("class_commitment", self.class_commitment)?;
serializer.serialize_field("contract_proof", &self.contract_proof)?;
serializer.serialize_optional("contract_data", self.contract_data.clone())?;
serializer.end()
Expand All @@ -252,7 +258,7 @@ impl crate::dto::SerializeForVersion for GetClassProofOutput {
serializer: crate::dto::Serializer,
) -> Result<crate::dto::Ok, crate::dto::Error> {
let mut serializer = serializer.serialize_struct()?;
serializer.serialize_optional("class_commitment", self.class_commitment)?;
serializer.serialize_optional_with_null("class_commitment", self.class_commitment)?;
serializer.serialize_field("class_proof", &self.class_proof)?;
serializer.end()
}
Expand Down Expand Up @@ -499,6 +505,52 @@ mod tests {

use super::*;

mod serialization {
use bitvec::prelude::*;

use super::*;
use crate::dto::SerializeForVersion;

#[test]
fn serialize_proof_nodes() {
let nodes = ProofNodes(vec![
TrieNode::Binary {
left: Felt::from_u64(0),
right: Felt::from_u64(1),
},
TrieNode::Edge {
child: Felt::from_u64(2),
path: bitvec::bitvec![u8, Msb0; 1, 1],
},
]);
let actual = nodes
.serialize(crate::dto::Serializer {
version: crate::RpcVersion::default(),
})
.unwrap();
let expected = serde_json::json!(
[
{
"binary": {
"left": "0x0",
"right": "0x1",
}
},
{
"edge": {
"path": {
"value": "0x3",
"len": 2,
},
"child": "0x2",
}
},
]
);
assert_eq!(actual, expected);
}
}

mod get_proof {
use super::*;

Expand Down

0 comments on commit e7bc5cd

Please sign in to comment.