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::{StorageMapKey, 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`
57#[derive(Debug, Clone)]
58pub struct StorageMapUpdate {
59    /// Block number in which the slot was updated.
60    pub block_num: BlockNumber,
61    /// Name of the storage slot.
62    pub slot_name: StorageSlotName,
63    /// The storage map key
64    pub key: StorageMapKey,
65    /// The storage map value.
66    pub value: Word,
67}
68
69// STORAGE MAP UPDATE CONVERSION
70// ================================================================================================
71
72impl TryFrom<proto::rpc::StorageMapUpdate> for StorageMapUpdate {
73    type Error = RpcConversionError;
74
75    fn try_from(value: proto::rpc::StorageMapUpdate) -> Result<Self, Self::Error> {
76        let block_num = value.block_num;
77
78        let slot_name = StorageSlotName::new(value.slot_name)
79            .map_err(|err| RpcConversionError::InvalidField(err.to_string()))?;
80
81        let key: StorageMapKey = value
82            .key
83            .ok_or(proto::rpc::StorageMapUpdate::missing_field(stringify!(key)))?
84            .try_into()?;
85
86        let value: Word = value
87            .value
88            .ok_or(proto::rpc::StorageMapUpdate::missing_field(stringify!(value)))?
89            .try_into()?;
90
91        Ok(Self {
92            block_num: block_num.into(),
93            slot_name,
94            key,
95            value,
96        })
97    }
98}