cairo_lang_sierra/extensions/modules/starknet/
getter.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use std::marker::PhantomData;

use super::interoperability::ContractAddressType;
use super::syscalls::SyscallGenericLibfunc;
use super::{felt252_span_ty, span_ty};
use crate::extensions::boxing::box_ty;
use crate::extensions::felt252::Felt252Type;
use crate::extensions::int::unsigned::{Uint32Type, Uint64Type};
use crate::extensions::int::unsigned128::Uint128Type;
use crate::extensions::lib_func::SignatureSpecializationContext;
use crate::extensions::structure::StructType;
use crate::extensions::{NamedType, NoGenericArgsGenericType, SpecializationError};
use crate::ids::{ConcreteTypeId, UserTypeId};
use crate::program::GenericArg;

/// Trait for implementing getters.
pub trait GetterTraits: Default {
    /// The generic libfunc id for the getter libfunc.
    const STR_ID: &'static str;
    /// The simple sierra generic type returned by the getter.
    type InfoType: NoGenericArgsGenericType;
}

/// Same as GetterTraits, but with a function to return the concrete TypeId.
pub trait GetterTraitsEx: Default {
    /// The generic libfunc id for the getter libfunc.
    const STR_ID: &'static str;
    /// The simple sierra generic type returned by the getter.
    fn info_type_id(
        context: &dyn SignatureSpecializationContext,
    ) -> Result<ConcreteTypeId, SpecializationError>;
}

impl<TGetterTraits: GetterTraits> GetterTraitsEx for TGetterTraits {
    const STR_ID: &'static str = TGetterTraits::STR_ID;
    fn info_type_id(
        context: &dyn SignatureSpecializationContext,
    ) -> Result<ConcreteTypeId, SpecializationError> {
        context.get_concrete_type(TGetterTraits::InfoType::id(), &[])
    }
}

/// Libfunc for a getter system call.
#[derive(Default)]
pub struct GetterLibfunc<TGetterTraitsEx: GetterTraitsEx> {
    _phantom: PhantomData<TGetterTraitsEx>,
}
impl<TGetterTraitsEx: GetterTraitsEx> SyscallGenericLibfunc for GetterLibfunc<TGetterTraitsEx> {
    const STR_ID: &'static str = TGetterTraitsEx::STR_ID;

    fn input_tys(
        _context: &dyn SignatureSpecializationContext,
    ) -> Result<Vec<crate::ids::ConcreteTypeId>, SpecializationError> {
        Ok(vec![])
    }

    fn success_output_tys(
        context: &dyn SignatureSpecializationContext,
    ) -> Result<Vec<crate::ids::ConcreteTypeId>, SpecializationError> {
        Ok(vec![TGetterTraitsEx::info_type_id(context)?])
    }
}

/// Helper for ExecutionInfo type.
fn get_execution_info_type(
    context: &dyn SignatureSpecializationContext,
) -> Result<ConcreteTypeId, SpecializationError> {
    let felt252_ty = context.get_concrete_type(Felt252Type::id(), &[])?;
    let contract_address_ty = context.get_concrete_type(ContractAddressType::id(), &[])?;
    context.get_concrete_type(
        StructType::id(),
        &[
            GenericArg::UserType(UserTypeId::from_string("core::starknet::info::ExecutionInfo")),
            // block_info
            GenericArg::Type(box_ty(context, get_block_info_type(context)?)?),
            // tx_info
            GenericArg::Type(box_ty(context, get_tx_info_type(context)?)?),
            // caller_address
            GenericArg::Type(contract_address_ty.clone()),
            // contract_address
            GenericArg::Type(contract_address_ty),
            // entry_point_selector
            GenericArg::Type(felt252_ty),
        ],
    )
}

/// Helper for v2::ExecutionInfo type.
fn get_execution_info_v2_type(
    context: &dyn SignatureSpecializationContext,
) -> Result<ConcreteTypeId, SpecializationError> {
    let felt252_ty = context.get_concrete_type(Felt252Type::id(), &[])?;
    let contract_address_ty = context.get_concrete_type(ContractAddressType::id(), &[])?;
    context.get_concrete_type(
        StructType::id(),
        &[
            GenericArg::UserType(UserTypeId::from_string(
                "core::starknet::info::v2::ExecutionInfo",
            )),
            // block_info
            GenericArg::Type(box_ty(context, get_block_info_type(context)?)?),
            // tx_info
            GenericArg::Type(box_ty(context, get_tx_info_v2_type(context)?)?),
            // caller_address
            GenericArg::Type(contract_address_ty.clone()),
            // contract_address
            GenericArg::Type(contract_address_ty),
            // entry_point_selector
            GenericArg::Type(felt252_ty),
        ],
    )
}

/// Helper for BlockInfo type.
fn get_block_info_type(
    context: &dyn SignatureSpecializationContext,
) -> Result<ConcreteTypeId, SpecializationError> {
    let contract_address_ty = context.get_concrete_type(ContractAddressType::id(), &[])?;
    let u64_ty = context.get_concrete_type(Uint64Type::id(), &[])?;
    context.get_concrete_type(
        StructType::id(),
        &[
            GenericArg::UserType(UserTypeId::from_string("core::starknet::info::BlockInfo")),
            // block_number
            GenericArg::Type(u64_ty.clone()),
            // block_timestamp
            GenericArg::Type(u64_ty),
            // sequencer_address
            GenericArg::Type(contract_address_ty),
        ],
    )
}

/// Helper for TxInfo type.
fn get_tx_info_type(
    context: &dyn SignatureSpecializationContext,
) -> Result<ConcreteTypeId, SpecializationError> {
    let felt252_ty = context.get_concrete_type(Felt252Type::id(), &[])?;
    let contract_address_ty = context.get_concrete_type(ContractAddressType::id(), &[])?;
    let u128_ty = context.get_concrete_type(Uint128Type::id(), &[])?;
    context.get_concrete_type(
        StructType::id(),
        &[
            GenericArg::UserType(UserTypeId::from_string("core::starknet::info::TxInfo")),
            // version
            GenericArg::Type(felt252_ty.clone()),
            // account_contract_address
            GenericArg::Type(contract_address_ty),
            // max_fee
            GenericArg::Type(u128_ty),
            // signature
            GenericArg::Type(felt252_span_ty(context)?),
            // transaction_hash
            GenericArg::Type(felt252_ty.clone()),
            // chain_id
            GenericArg::Type(felt252_ty.clone()),
            // nonce
            GenericArg::Type(felt252_ty),
        ],
    )
}

/// Cairo level user type name for `ResourceBounds`.
const RESOURCE_BOUNDS_USER_TYPE_ID: &str = "core::starknet::info::v2::ResourceBounds";

/// User type for `Span<ResourceBounds>`.
fn resource_bounds_span_ty(
    context: &dyn SignatureSpecializationContext,
) -> Result<ConcreteTypeId, SpecializationError> {
    span_ty(context, get_resource_bounds_type(context)?, RESOURCE_BOUNDS_USER_TYPE_ID)
}

/// Helper for ResourceBounds type.
fn get_resource_bounds_type(
    context: &dyn SignatureSpecializationContext,
) -> Result<ConcreteTypeId, SpecializationError> {
    let felt252_ty = context.get_concrete_type(Felt252Type::id(), &[])?;
    let u64_ty = context.get_concrete_type(Uint64Type::id(), &[])?;
    let u128_ty = context.get_concrete_type(Uint128Type::id(), &[])?;
    context.get_concrete_type(
        StructType::id(),
        &[
            GenericArg::UserType(UserTypeId::from_string(RESOURCE_BOUNDS_USER_TYPE_ID)),
            // resource
            GenericArg::Type(felt252_ty),
            // max_amount
            GenericArg::Type(u64_ty),
            // max_price_per_unit
            GenericArg::Type(u128_ty),
        ],
    )
}

/// Helper for v2::TxInfo type.
fn get_tx_info_v2_type(
    context: &dyn SignatureSpecializationContext,
) -> Result<ConcreteTypeId, SpecializationError> {
    let felt252_ty = context.get_concrete_type(Felt252Type::id(), &[])?;
    let felt252_span_ty = felt252_span_ty(context)?;
    let contract_address_ty = context.get_concrete_type(ContractAddressType::id(), &[])?;
    let u32_ty = context.get_concrete_type(Uint32Type::id(), &[])?;
    let u128_ty = context.get_concrete_type(Uint128Type::id(), &[])?;
    context.get_concrete_type(
        StructType::id(),
        &[
            GenericArg::UserType(UserTypeId::from_string("core::starknet::info::v2::TxInfo")),
            // version
            GenericArg::Type(felt252_ty.clone()),
            // account_contract_address
            GenericArg::Type(contract_address_ty),
            // max_fee
            GenericArg::Type(u128_ty.clone()),
            // signature
            GenericArg::Type(felt252_span_ty.clone()),
            // transaction_hash
            GenericArg::Type(felt252_ty.clone()),
            // chain_id
            GenericArg::Type(felt252_ty.clone()),
            // nonce
            GenericArg::Type(felt252_ty),
            // resource_bounds
            GenericArg::Type(resource_bounds_span_ty(context)?),
            // tip
            GenericArg::Type(u128_ty),
            // paymaster_data
            GenericArg::Type(felt252_span_ty.clone()),
            // nonce_data_availability_mode
            GenericArg::Type(u32_ty.clone()),
            // fee_data_availability_mode
            GenericArg::Type(u32_ty),
            // account_deployment_data
            GenericArg::Type(felt252_span_ty),
        ],
    )
}

#[derive(Default)]
pub struct GetExecutionInfoTrait {}
impl GetterTraitsEx for GetExecutionInfoTrait {
    const STR_ID: &'static str = "get_execution_info_syscall";

    fn info_type_id(
        context: &dyn SignatureSpecializationContext,
    ) -> Result<ConcreteTypeId, SpecializationError> {
        box_ty(context, get_execution_info_type(context)?)
    }
}

#[derive(Default)]
pub struct GetExecutionInfoV2Trait {}
impl GetterTraitsEx for GetExecutionInfoV2Trait {
    const STR_ID: &'static str = "get_execution_info_v2_syscall";

    fn info_type_id(
        context: &dyn SignatureSpecializationContext,
    ) -> Result<ConcreteTypeId, SpecializationError> {
        box_ty(context, get_execution_info_v2_type(context)?)
    }
}