Skip to main content

miden_client/rpc/domain/
storage_map.rs

1use alloc::string::ToString;
2use alloc::vec::Vec;
3
4use miden_protocol::Word;
5use miden_protocol::account::StorageSlotName;
6use miden_protocol::block::BlockNumber;
7
8use crate::rpc::domain::MissingFieldHelper;
9use crate::rpc::{RpcConversionError, RpcError, generated as proto};
10
11// STORAGE MAP INFO
12// ================================================================================================
13
14/// Represents a `proto::rpc::SyncStorageMapsResponse` with fields converted into domain
15/// types. Contains information of updated map slots in a given range of blocks specified on
16/// request. Also provides the current chain tip while processing the request.
17pub struct StorageMapInfo {
18    /// Current chain tip
19    pub chain_tip: BlockNumber,
20    /// The block number of the last check included in this response.
21    pub block_number: BlockNumber,
22    /// The list of storage map updates.
23    pub updates: Vec<StorageMapUpdate>,
24}
25
26// STORAGE MAP INFO CONVERSION
27// ================================================================================================
28
29impl TryFrom<proto::rpc::SyncAccountStorageMapsResponse> for StorageMapInfo {
30    type Error = RpcError;
31
32    fn try_from(value: proto::rpc::SyncAccountStorageMapsResponse) -> Result<Self, Self::Error> {
33        let pagination_info = value.pagination_info.ok_or(
34            proto::rpc::SyncAccountStorageMapsResponse::missing_field(stringify!(pagination_info)),
35        )?;
36        let chain_tip = pagination_info.chain_tip;
37        let block_number = pagination_info.block_num;
38
39        let updates = value
40            .updates
41            .into_iter()
42            .map(TryInto::try_into)
43            .collect::<Result<Vec<_>, _>>()?;
44
45        Ok(Self {
46            chain_tip: chain_tip.into(),
47            block_number: block_number.into(),
48            updates,
49        })
50    }
51}
52
53// STORAGE MAP UPDATE
54// ================================================================================================
55
56/// Represents a `proto::rpc::StorageMapUpdate`
57pub struct StorageMapUpdate {
58    /// Block number in which the slot was updated.
59    pub block_num: BlockNumber,
60    /// Name of the storage slot.
61    pub slot_name: StorageSlotName,
62    /// The storage map key
63    pub key: Word,
64    /// The storage map value.
65    pub value: Word,
66}
67
68// STORAGE MAP UPDATE CONVERSION
69// ================================================================================================
70
71impl TryFrom<proto::rpc::StorageMapUpdate> for StorageMapUpdate {
72    type Error = RpcConversionError;
73
74    fn try_from(value: proto::rpc::StorageMapUpdate) -> Result<Self, Self::Error> {
75        let block_num = value.block_num;
76
77        let slot_name = StorageSlotName::new(value.slot_name)
78            .map_err(|err| RpcConversionError::InvalidField(err.to_string()))?;
79
80        let key: Word = value
81            .key
82            .ok_or(proto::rpc::StorageMapUpdate::missing_field(stringify!(key)))?
83            .try_into()?;
84
85        let value: Word = value
86            .value
87            .ok_or(proto::rpc::StorageMapUpdate::missing_field(stringify!(value)))?
88            .try_into()?;
89
90        Ok(Self {
91            block_num: block_num.into(),
92            slot_name,
93            key,
94            value,
95        })
96    }
97}