starknet_devnet_types/rpc/
state.rs

1use num_bigint::BigUint;
2use serde::Serialize;
3use starknet_types_core::felt::Felt;
4
5use super::block::BlockRoot;
6use crate::contract_address::ContractAddress;
7use crate::felt::{BlockHash, ClassHash, CompiledClassHash, Nonce};
8use crate::patricia_key::PatriciaKey;
9
10pub type Balance = BigUint;
11
12#[derive(Debug, Clone, Serialize)]
13#[cfg_attr(feature = "testing", derive(serde::Deserialize), serde(deny_unknown_fields))]
14pub enum StateUpdateResult {
15    StateUpdate(StateUpdate),
16    PreConfirmedStateUpdate(PreConfirmedStateUpdate),
17}
18
19impl StateUpdateResult {
20    pub fn get_state_diff(&self) -> &ThinStateDiff {
21        match self {
22            StateUpdateResult::StateUpdate(s) => &s.state_diff,
23            StateUpdateResult::PreConfirmedStateUpdate(s) => &s.state_diff,
24        }
25    }
26}
27
28#[derive(Debug, Clone, Serialize)]
29#[cfg_attr(feature = "testing", derive(serde::Deserialize), serde(deny_unknown_fields))]
30pub struct StateUpdate {
31    pub block_hash: BlockHash,
32    pub new_root: BlockRoot,
33    pub old_root: BlockRoot,
34    pub state_diff: ThinStateDiff,
35}
36
37impl StateUpdate {
38    /// New and old root are not computed - Devnet does not store block data in a tree.
39    pub fn new(block_hash: Felt, state_diff: ThinStateDiff) -> Self {
40        Self { block_hash, new_root: Felt::default(), old_root: Felt::default(), state_diff }
41    }
42}
43
44#[derive(Debug, Clone, Serialize)]
45#[cfg_attr(feature = "testing", derive(serde::Deserialize), serde(deny_unknown_fields))]
46pub struct PreConfirmedStateUpdate {
47    pub old_root: Option<BlockRoot>,
48    pub state_diff: ThinStateDiff,
49}
50
51#[derive(Debug, Default, Clone, Serialize)]
52#[cfg_attr(
53    feature = "testing",
54    derive(serde::Deserialize, Eq, PartialEq),
55    serde(deny_unknown_fields)
56)]
57pub struct ThinStateDiff {
58    pub deployed_contracts: Vec<DeployedContract>,
59    pub storage_diffs: Vec<StorageDiff>,
60    pub declared_classes: Vec<ClassHashPair>,
61    pub deprecated_declared_classes: Vec<ClassHash>,
62    pub nonces: Vec<ContractNonce>,
63    pub replaced_classes: Vec<ReplacedClasses>,
64    // In Devnet, this will always be None as:
65    // 1) There is no migration process when starting Devnet without forking as state is empty.
66    // 2) When forking, there is no RPC support for fetching compiled classes from origin (yet).
67    //    This is added for adherence to Starknet spec and/or future use.
68    #[serde(skip_serializing_if = "Option::is_none")]
69    pub migrated_compiled_classes: Option<Vec<ClassHashPair>>,
70}
71
72/// A deployed contract in Starknet.
73#[derive(Debug, Default, Clone, Serialize)]
74#[cfg_attr(
75    feature = "testing",
76    derive(serde::Deserialize, Eq, PartialEq),
77    serde(deny_unknown_fields)
78)]
79pub struct DeployedContract {
80    pub address: ContractAddress,
81    pub class_hash: ClassHash,
82}
83
84/// Storage differences in Starknet.
85// Invariant: Storage keys are strictly increasing. In particular, no key appears twice.
86#[derive(Debug, Default, Clone, Serialize)]
87#[cfg_attr(
88    feature = "testing",
89    derive(serde::Deserialize, Eq, PartialEq),
90    serde(deny_unknown_fields)
91)]
92pub struct StorageDiff {
93    pub address: ContractAddress,
94    pub storage_entries: Vec<StorageEntry>,
95}
96
97/// A storage entry in a contract.
98#[derive(Debug, Default, Clone, Serialize)]
99#[cfg_attr(
100    feature = "testing",
101    derive(serde::Deserialize, Eq, PartialEq),
102    serde(deny_unknown_fields)
103)]
104pub struct StorageEntry {
105    pub key: PatriciaKey,
106    pub value: Felt,
107}
108
109#[derive(Debug, Clone, Default, Serialize)]
110#[cfg_attr(feature = "testing", derive(PartialEq, Eq, serde::Deserialize))]
111pub struct ClassHashPair {
112    pub class_hash: ClassHash,
113    pub compiled_class_hash: CompiledClassHash,
114}
115
116#[derive(Debug, Clone, Default, Serialize)]
117#[cfg_attr(feature = "testing", derive(Eq, PartialEq, serde::Deserialize))]
118pub struct ReplacedClasses {
119    pub contract_address: ContractAddress,
120    pub class_hash: ClassHash,
121}
122
123/// The nonce of a Starknet contract.
124#[derive(Debug, Clone, Serialize)]
125#[cfg_attr(
126    feature = "testing",
127    derive(serde::Deserialize, Eq, PartialEq),
128    serde(deny_unknown_fields)
129)]
130pub struct ContractNonce {
131    pub contract_address: ContractAddress,
132    pub nonce: Nonce,
133}