From 246078cf3081a72b9f4d7485da9911ddee8b3779 Mon Sep 17 00:00:00 2001 From: Mirko von Leipzig Date: Fri, 1 Mar 2024 16:09:09 +0200 Subject: [PATCH] feat(rpc): STATE_UPDATE DTO --- crates/rpc/src/dto/state_update.rs | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/crates/rpc/src/dto/state_update.rs b/crates/rpc/src/dto/state_update.rs index 4ec0533e75..e9cc4d3b41 100644 --- a/crates/rpc/src/dto/state_update.rs +++ b/crates/rpc/src/dto/state_update.rs @@ -2,6 +2,8 @@ use super::serialize::SerializeForVersion; use crate::dto::*; use std::collections::HashMap; +pub struct StateUpdate<'a>(pub &'a pathfinder_common::StateUpdate); + pub struct PendingStateUpdate<'a>(pub &'a pathfinder_common::StateUpdate); pub struct StateDiff<'a>(pub &'a pathfinder_common::StateUpdate); @@ -16,6 +18,20 @@ pub struct DeployedContractItem<'a> { class_hash: &'a pathfinder_common::ClassHash, } +impl SerializeForVersion for StateUpdate<'_> { + fn serialize( + &self, + serializer: serialize::Serializer, + ) -> Result { + let mut serializer = serializer.serialize_struct()?; + serializer.serialize_field("block_hash", &BlockHash(&self.0.block_hash))?; + serializer.serialize_field("new_root", &Felt(&self.0.state_commitment.0))?; + serializer.serialize_field("old_root", &Felt(&self.0.parent_state_commitment.0))?; + serializer.serialize_field("state_diff", &StateDiff(self.0))?; + serializer.end() + } +} + impl SerializeForVersion for PendingStateUpdate<'_> { fn serialize( &self, @@ -232,6 +248,23 @@ mod tests { use pretty_assertions_sorted::assert_eq; use serde_json::json; + #[test] + fn state_update() { + let s = Serializer::default(); + let state_update = test_state_update(); + + let expected = json!({ + "block_hash": s.serialize(&BlockHash(&state_update.block_hash)).unwrap(), + "new_root": s.serialize(&Felt(&state_update.state_commitment.0)).unwrap(), + "old_root": s.serialize(&Felt(&state_update.parent_state_commitment.0)).unwrap(), + "state_diff": s.serialize(&StateDiff(&state_update)).unwrap(), + }); + + let encoded = StateUpdate(&state_update).serialize(s).unwrap(); + + assert_eq!(encoded, expected); + } + #[test] fn pending_state_update() { let s = Serializer::default();