-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathcomponents.rs
More file actions
131 lines (119 loc) · 4.99 KB
/
Copy pathcomponents.rs
File metadata and controls
131 lines (119 loc) · 4.99 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
use crate::trace::types::{
CallerAddress, ContractAddress, ContractName, Events, Gas, TransformedCallResult,
TransformedCalldata,
};
use blockifier::execution::entry_point::CallType;
use paste::paste;
use starknet_api::contract_class::EntryPointType;
use std::collections::HashSet;
/// Represents a set of [`Component`] that will be included in a trace.
pub struct Components {
set: HashSet<Component>,
}
impl Components {
/// Creates a new [`Components`] instance with the specified set of [`Component`].
#[must_use]
pub fn new(components: HashSet<Component>) -> Self {
Self { set: components }
}
/// Checks if a specific [`Component`] is included in the set.
#[must_use]
pub fn contains(&self, component: &Component) -> bool {
self.set.contains(component)
}
}
/// Components that will be included in the trace.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Component {
/// The name of the contract being called.
ContractName,
/// The type of the entry point being called (e.g., `External`, `L1Handler`, etc.).
EntryPointType,
/// The calldata of the call, transformed for display.
Calldata,
/// The address of the contract being called.
ContractAddress,
/// The address of the caller contract.
CallerAddress,
/// The type of the call (e.g., `Call`, `Delegate`, etc.).
CallType,
/// The result of the call, transformed for display.
CallResult,
/// The raw events emitted by the call.
Events,
/// The L2 gas used by the call.
Gas,
}
macro_rules! impl_component_container {
// Short form - uses the same name for both the Component variant and the value type
($variant:ident) => {
impl_component_container!($variant, $variant);
};
// Full form - Component name and different value type
($variant:ident, $ty:ty) => {
paste! {
#[doc = concat!(
"Container for a `", stringify!($ty),
"` that is computed only if `Component::", stringify!($variant),
"` is included in the [`Components`]."
)]
#[derive(Debug, Clone)]
pub struct [<$variant Container>] {
value: Option<$ty>,
}
impl [<$variant Container>] {
#[doc = concat!(
"Creates a new [`", stringify!($variant), "Container`] from a specific value.\n\n",
"This will store the value only if `Component::", stringify!($variant), "` is present in the [`Components`]."
)]
#[must_use]
pub fn new(components: &Components, value: $ty) -> Self {
Self {
value: components.contains(&Component::$variant).then_some(value),
}
}
#[doc = concat!(
"Creates a new [`", stringify!($variant), "Container`] using a lazy supplier function.\n\n",
"The function will be called only if `Component::", stringify!($variant), "` is present in [`Components`]."
)]
#[must_use]
pub fn new_lazy(components: &Components, supply_fn: impl FnOnce() -> $ty) -> Self {
Self {
value: components.contains(&Component::$variant).then(supply_fn),
}
}
#[doc = "Returns a reference to the contained value if it was computed.\n\nReturns `None` otherwise."]
#[must_use]
pub fn as_option(&self) -> Option<&$ty> {
self.value.as_ref()
}
}
impl Components {
#[doc = concat!(
"Returns a [`", stringify!($variant), "Container`] from a direct value.\n\n",
"The value will only be stored if `Component::", stringify!($variant), "` is in the set."
)]
#[must_use]
pub fn [<$variant:snake>](&self, value: $ty) -> [<$variant Container>] {
[<$variant Container>]::new(self, value)
}
#[doc = concat!(
"Returns a [`", stringify!($variant), "Container`] using a lazy supplier function.\n\n",
"The function will only be called if `Component::", stringify!($variant), "` is in the set."
)]
pub fn [<$variant:snake _lazy>](&self, supply_fn: impl FnOnce() -> $ty) -> [<$variant Container>] {
[<$variant Container>]::new_lazy(self, supply_fn)
}
}
}
};
}
impl_component_container!(ContractName);
impl_component_container!(EntryPointType);
impl_component_container!(Calldata, TransformedCalldata);
impl_component_container!(ContractAddress);
impl_component_container!(CallerAddress);
impl_component_container!(CallType);
impl_component_container!(CallResult, TransformedCallResult);
impl_component_container!(Gas);
impl_component_container!(Events);