Skip to content

Commit

Permalink
feat(rpc): STATE_UPDATE DTO
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirko-von-Leipzig committed Mar 1, 2024
1 parent 7a3868a commit 246078c
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions crates/rpc/src/dto/state_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<serialize::Ok, serialize::Error> {
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,
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 246078c

Please sign in to comment.