-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathreceipt.rs
More file actions
190 lines (177 loc) · 6.92 KB
/
Copy pathreceipt.rs
File metadata and controls
190 lines (177 loc) · 6.92 KB
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
use starknet_api::core::ContractAddress;
use starknet_api::executable_transaction::TransactionType;
use starknet_api::execution_resources::{GasAmount, GasVector};
use starknet_api::transaction::fields::{Fee, GasVectorComputationMode};
use crate::context::TransactionContext;
use crate::execution::call_info::ExecutionSummary;
use crate::fee::resources::{
ComputationResources,
StarknetResources,
StateResources,
TransactionResources,
};
use crate::state::cached_state::StateChanges;
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::objects::HasRelatedFeeType;
#[cfg(test)]
#[path = "receipt_test.rs"]
pub mod test;
/// Parameters required to compute actual cost of a transaction.
struct TransactionReceiptParameters<'a> {
tx_context: &'a TransactionContext,
gas_mode: GasVectorComputationMode,
calldata_length: usize,
signature_length: usize,
code_size: usize,
state_changes: &'a StateChanges,
sender_address: Option<ContractAddress>,
l1_handler_payload_size: Option<usize>,
execution_summary_without_fee_transfer: ExecutionSummary,
tx_type: TransactionType,
reverted_steps: usize,
reverted_sierra_gas: GasAmount,
proof_facts_length: usize,
}
// TODO(Gilad): Use everywhere instead of passing the `actual_{fee,resources}` tuple, which often
// get passed around together.
#[cfg_attr(any(test, feature = "testing"), derive(Clone))]
#[cfg_attr(feature = "transaction_serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Default, Debug, PartialEq)]
pub struct TransactionReceipt {
pub fee: Fee,
pub gas: GasVector,
pub da_gas: GasVector,
pub resources: TransactionResources,
}
impl TransactionReceipt {
fn from_params(tx_receipt_params: TransactionReceiptParameters<'_>) -> Self {
let TransactionReceiptParameters {
tx_context,
gas_mode,
calldata_length,
signature_length,
code_size,
state_changes,
sender_address,
l1_handler_payload_size,
execution_summary_without_fee_transfer,
tx_type,
reverted_steps,
reverted_sierra_gas,
proof_facts_length,
} = tx_receipt_params;
let charged_resources = execution_summary_without_fee_transfer.charged_resources.clone();
let starknet_resources = StarknetResources::new(
calldata_length,
signature_length,
code_size,
StateResources::new(state_changes, sender_address, tx_context.fee_token_address()),
l1_handler_payload_size,
execution_summary_without_fee_transfer,
proof_facts_length,
);
// Transaction overhead ('additional') resources are computed in VM resources no matter what
// the tracked resources of the transaction are.
let os_vm_resources = tx_context
.block_context
.versioned_constants
.get_additional_os_tx_resources(
tx_type,
&starknet_resources,
tx_context.block_context.block_info.use_kzg_da,
)
.filter_unused_builtins();
let tx_resources = TransactionResources {
starknet_resources,
computation: ComputationResources {
tx_vm_resources: charged_resources.vm_resources.filter_unused_builtins(),
os_vm_resources,
n_reverted_steps: reverted_steps,
sierra_gas: charged_resources.gas_consumed,
reverted_sierra_gas,
},
};
let gas = tx_resources.to_gas_vector(
&tx_context.block_context.versioned_constants,
tx_context.block_context.block_info.use_kzg_da,
&gas_mode,
);
// Backward-compatibility.
let fee = if tx_type == TransactionType::Declare && tx_context.tx_info.is_v0() {
Fee(0)
} else {
tx_context.tx_info.get_fee_by_gas_vector(
&tx_context.block_context.block_info,
gas,
tx_context.effective_tip(),
)
};
let da_gas = tx_resources
.starknet_resources
.state
.da_gas_vector(tx_context.block_context.block_info.use_kzg_da);
Self { resources: tx_resources, gas, da_gas, fee }
}
/// Computes the receipt of an L1 handler transaction.
pub fn from_l1_handler<'a>(
tx_context: &'a TransactionContext,
l1_handler_payload_size: usize,
execution_summary_without_fee_transfer: ExecutionSummary,
state_changes: &'a StateChanges,
) -> Self {
Self::from_params(TransactionReceiptParameters {
tx_context,
gas_mode: GasVectorComputationMode::All, /* Although L1 handler resources are
* deprecated, we still want to compute a
* full gas vector. */
calldata_length: l1_handler_payload_size,
signature_length: 0, // Signature is validated on L1.
code_size: 0,
state_changes,
sender_address: None, // L1 handlers have no sender address.
l1_handler_payload_size: Some(l1_handler_payload_size),
execution_summary_without_fee_transfer,
tx_type: TransactionType::L1Handler,
reverted_steps: 0,
reverted_sierra_gas: GasAmount(0),
proof_facts_length: 0,
})
}
/// Computes the receipt of a reverted L1 handler transaction.
pub fn reverted_l1_handler(
tx_context: &TransactionContext,
l1_handler_payload_size: usize,
) -> Self {
Self::from_l1_handler(
tx_context,
l1_handler_payload_size,
ExecutionSummary::default(),
&StateChanges::default(),
)
}
/// Computes the receipt of an account transaction.
pub fn from_account_tx<'a>(
account_tx: &'a AccountTransaction,
tx_context: &'a TransactionContext,
state_changes: &'a StateChanges,
execution_summary_without_fee_transfer: ExecutionSummary,
reverted_steps: usize,
reverted_sierra_gas: GasAmount,
) -> Self {
Self::from_params(TransactionReceiptParameters {
tx_context,
gas_mode: tx_context.get_gas_vector_computation_mode(),
calldata_length: account_tx.calldata_length(),
signature_length: account_tx.signature_length(),
code_size: account_tx.declare_code_size(),
state_changes,
sender_address: Some(tx_context.tx_info.sender_address()),
l1_handler_payload_size: None,
execution_summary_without_fee_transfer,
tx_type: account_tx.tx_type(),
reverted_steps,
reverted_sierra_gas,
proof_facts_length: account_tx.proof_facts_length(),
})
}
}