-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathobjects.rs
264 lines (231 loc) · 8.67 KB
/
objects.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
260
261
262
263
264
use std::collections::HashMap;
use cairo_vm::types::builtin_name::BuiltinName;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::execution_resources::GasVector;
use starknet_api::transaction::fields::{
AccountDeploymentData,
AllResourceBounds,
Fee,
GasVectorComputationMode,
PaymasterData,
ResourceBounds,
Tip,
TransactionSignature,
ValidResourceBounds,
};
use starknet_api::transaction::{
signed_tx_version,
TransactionHash,
TransactionOptions,
TransactionVersion,
};
use strum_macros::EnumIter;
use crate::abi::constants as abi_constants;
use crate::blockifier::block::BlockInfo;
use crate::execution::call_info::{CallInfo, ExecutionSummary};
use crate::fee::fee_utils::get_fee_by_gas_vector;
use crate::fee::receipt::TransactionReceipt;
use crate::transaction::errors::{TransactionExecutionError, TransactionPreValidationError};
#[cfg(test)]
#[path = "objects_test.rs"]
pub mod objects_test;
pub type TransactionExecutionResult<T> = Result<T, TransactionExecutionError>;
pub type TransactionPreValidationResult<T> = Result<T, TransactionPreValidationError>;
macro_rules! implement_getters {
($(($field:ident, $field_type:ty)),*) => {
$(pub fn $field(&self) -> $field_type {
match self{
Self::Current(context) => context.common_fields.$field,
Self::Deprecated(context) => context.common_fields.$field,
}
})*
};
}
/// Contains the account information of the transaction (outermost call).
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TransactionInfo {
Current(CurrentTransactionInfo),
Deprecated(DeprecatedTransactionInfo),
}
impl TransactionInfo {
implement_getters!(
(transaction_hash, TransactionHash),
(version, TransactionVersion),
(nonce, Nonce),
(sender_address, ContractAddress),
(only_query, bool)
);
pub fn signature(&self) -> TransactionSignature {
match self {
Self::Current(context) => context.common_fields.signature.clone(),
Self::Deprecated(context) => context.common_fields.signature.clone(),
}
}
pub fn is_v0(&self) -> bool {
self.version() == TransactionVersion::ZERO
}
pub fn signed_version(&self) -> TransactionVersion {
signed_tx_version(&self.version(), &TransactionOptions { only_query: self.only_query() })
}
pub fn enforce_fee(&self) -> bool {
match self {
TransactionInfo::Current(context) => {
context.resource_bounds.max_possible_fee() > Fee(0)
}
TransactionInfo::Deprecated(context) => context.max_fee != Fee(0),
}
}
pub fn gas_mode(&self) -> GasVectorComputationMode {
match self {
TransactionInfo::Current(info) => {
info.resource_bounds.get_gas_vector_computation_mode()
}
TransactionInfo::Deprecated(_) => GasVectorComputationMode::NoL2Gas,
}
}
}
impl HasRelatedFeeType for TransactionInfo {
fn version(&self) -> TransactionVersion {
self.version()
}
fn is_l1_handler(&self) -> bool {
false
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CurrentTransactionInfo {
pub common_fields: CommonAccountFields,
pub resource_bounds: ValidResourceBounds,
pub tip: Tip,
pub nonce_data_availability_mode: DataAvailabilityMode,
pub fee_data_availability_mode: DataAvailabilityMode,
pub paymaster_data: PaymasterData,
pub account_deployment_data: AccountDeploymentData,
}
impl CurrentTransactionInfo {
/// Fetch the L1 resource bounds, if they exist.
// TODO(Nimrod): Consider removing this function and add equivalent method to
// `ValidResourceBounds`.
pub fn l1_resource_bounds(&self) -> ResourceBounds {
match self.resource_bounds {
ValidResourceBounds::L1Gas(bounds) => bounds,
ValidResourceBounds::AllResources(AllResourceBounds { l1_gas, .. }) => l1_gas,
}
}
#[cfg(any(test, feature = "testing"))]
pub fn create_for_testing() -> Self {
Self {
common_fields: CommonAccountFields::default(),
resource_bounds: ValidResourceBounds::create_for_testing_no_fee_enforcement(),
tip: Tip::default(),
nonce_data_availability_mode: DataAvailabilityMode::L2,
fee_data_availability_mode: DataAvailabilityMode::L2,
paymaster_data: PaymasterData::default(),
account_deployment_data: AccountDeploymentData::default(),
}
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct DeprecatedTransactionInfo {
pub common_fields: CommonAccountFields,
pub max_fee: Fee,
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct CommonAccountFields {
pub transaction_hash: TransactionHash,
pub version: TransactionVersion,
pub signature: TransactionSignature,
pub nonce: Nonce,
pub sender_address: ContractAddress,
pub only_query: bool,
}
/// Contains the information gathered by the execution of a transaction.
#[cfg_attr(any(test, feature = "testing"), derive(Clone))]
#[cfg_attr(feature = "transaction_serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Default, PartialEq)]
pub struct TransactionExecutionInfo {
/// Transaction validation call info; [None] for `L1Handler`.
pub validate_call_info: Option<CallInfo>,
/// Transaction execution call info; [None] for `Declare`.
pub execute_call_info: Option<CallInfo>,
/// Fee transfer call info; [None] for `L1Handler`.
pub fee_transfer_call_info: Option<CallInfo>,
pub revert_error: Option<String>,
/// The receipt of the transaction.
/// Including the actual fee that was charged (in units of the relevant fee token),
/// actual gas consumption the transaction is charged for data availability,
/// actual execution resources the transaction is charged for
/// (including L1 gas and additional OS resources estimation),
/// and total gas consumed.
pub receipt: TransactionReceipt,
}
impl TransactionExecutionInfo {
pub fn non_optional_call_infos(&self) -> impl Iterator<Item = &CallInfo> {
self.validate_call_info
.iter()
.chain(self.execute_call_info.iter())
.chain(self.fee_transfer_call_info.iter())
}
pub fn is_reverted(&self) -> bool {
self.revert_error.is_some()
}
/// Returns a summary of transaction execution, including executed class hashes, visited storage
/// entries, L2-to-L1_payload_lengths, and the number of emitted events.
pub fn summarize(&self) -> ExecutionSummary {
CallInfo::summarize_many(self.non_optional_call_infos())
}
}
pub trait ExecutionResourcesTraits {
fn total_n_steps(&self) -> usize;
fn prover_builtins(&self) -> HashMap<BuiltinName, usize>;
}
impl ExecutionResourcesTraits for ExecutionResources {
fn total_n_steps(&self) -> usize {
self.n_steps
// Memory holes are slightly cheaper than actual steps, but we count them as such
// for simplicity.
+ self.n_memory_holes
// The "segment arena" builtin is not part of the prover (not in any proof layout);
// It is transformed into regular steps by the OS program - each instance requires
// approximately 10 steps.
+ abi_constants::N_STEPS_PER_SEGMENT_ARENA_BUILTIN
* self
.builtin_instance_counter
.get(&BuiltinName::segment_arena)
.cloned()
.unwrap_or_default()
}
fn prover_builtins(&self) -> HashMap<BuiltinName, usize> {
let mut builtins = self.builtin_instance_counter.clone();
// See "total_n_steps" documentation.
builtins.remove(&BuiltinName::segment_arena);
builtins
}
}
pub trait HasRelatedFeeType {
fn version(&self) -> TransactionVersion;
fn is_l1_handler(&self) -> bool;
fn fee_type(&self) -> FeeType {
if self.is_l1_handler() || self.version() < TransactionVersion::THREE {
FeeType::Eth
} else {
FeeType::Strk
}
}
fn get_fee_by_gas_vector(&self, block_info: &BlockInfo, gas_vector: GasVector) -> Fee {
get_fee_by_gas_vector(block_info, gas_vector, &self.fee_type())
}
}
#[derive(Clone, Copy, Hash, EnumIter, Eq, PartialEq)]
pub enum FeeType {
Strk,
Eth,
}
pub trait TransactionInfoCreator {
fn create_tx_info(&self) -> TransactionInfo;
}
pub trait TransactionInfoCreatorInner {
fn create_tx_info(&self, only_query: bool) -> TransactionInfo;
}