massa_deferred_calls/
macros.rs

1pub(crate) const CALLS_TAG: u8 = 0u8;
2// slot fields
3pub(crate) const SLOT_TOTAL_GAS: u8 = 1u8;
4pub(crate) const SLOT_BASE_FEE: u8 = 2u8;
5
6// call fields
7pub(crate) const CALL_FIELD_SENDER_ADDRESS: u8 = 1u8;
8pub(crate) const CALL_FIELD_TARGET_SLOT: u8 = 2u8;
9pub(crate) const CALL_FIELD_TARGET_ADDRESS: u8 = 3u8;
10pub(crate) const CALL_FIELD_TARGET_FUNCTION: u8 = 4u8;
11pub(crate) const CALL_FIELD_PARAMETERS: u8 = 5u8;
12pub(crate) const CALL_FIELD_COINS: u8 = 6u8;
13pub(crate) const CALL_FIELD_MAX_GAS: u8 = 7u8;
14pub(crate) const CALL_FIELD_FEE: u8 = 8u8;
15pub(crate) const CALL_FIELD_CANCELED: u8 = 9u8;
16
17#[macro_export]
18macro_rules! deferred_call_slot_total_gas_key {
19    ($slot:expr) => {
20        [
21            DEFERRED_CALLS_PREFIX.as_bytes(),
22            &$slot[..],
23            &[$crate::macros::SLOT_TOTAL_GAS],
24        ]
25        .concat()
26    };
27}
28
29#[macro_export]
30macro_rules! deferred_call_slot_base_fee_key {
31    ($slot:expr) => {
32        [
33            DEFERRED_CALLS_PREFIX.as_bytes(),
34            &$slot[..],
35            &[$crate::macros::SLOT_BASE_FEE],
36        ]
37        .concat()
38    };
39}
40
41#[macro_export]
42macro_rules! deferred_slot_call_prefix_key {
43    ($slot:expr) => {
44        [
45            DEFERRED_CALLS_PREFIX.as_bytes(),
46            &$slot[..],
47            &[$crate::macros::CALLS_TAG],
48        ]
49        .concat()
50    };
51}
52
53#[macro_export]
54macro_rules! deferred_call_prefix_key {
55    ($id:expr, $slot:expr) => {
56        [&deferred_slot_call_prefix_key!($slot), &$id[..]].concat()
57    };
58}
59
60/// key formatting macro
61#[macro_export]
62macro_rules! deferred_call_field_key {
63    ($id:expr, $slot:expr, $field:expr) => {
64        [deferred_call_prefix_key!($id, $slot), vec![$field]].concat()
65    };
66}
67
68/// sender address key formatting macro
69#[macro_export]
70macro_rules! sender_address_key {
71    ($id:expr, $slot:expr) => {
72        deferred_call_field_key!($id, $slot, $crate::macros::CALL_FIELD_SENDER_ADDRESS)
73    };
74}
75
76/// target slot key formatting macro
77#[macro_export]
78macro_rules! target_slot_key {
79    ($id:expr, $slot:expr) => {
80        deferred_call_field_key!($id, $slot, $crate::macros::CALL_FIELD_TARGET_SLOT)
81    };
82}
83
84/// target address key formatting macro
85#[macro_export]
86macro_rules! target_address_key {
87    ($id:expr, $slot:expr) => {
88        deferred_call_field_key!($id, $slot, $crate::macros::CALL_FIELD_TARGET_ADDRESS)
89    };
90}
91
92/// target function key formatting macro
93#[macro_export]
94macro_rules! target_function_key {
95    ($id:expr, $slot:expr) => {
96        deferred_call_field_key!($id, $slot, $crate::macros::CALL_FIELD_TARGET_FUNCTION)
97    };
98}
99
100/// parameters key formatting macro
101#[macro_export]
102macro_rules! parameters_key {
103    ($id:expr, $slot:expr) => {
104        deferred_call_field_key!($id, $slot, $crate::macros::CALL_FIELD_PARAMETERS)
105    };
106}
107
108/// coins key formatting macro
109#[macro_export]
110macro_rules! coins_key {
111    ($id:expr, $slot:expr) => {
112        deferred_call_field_key!($id, $slot, $crate::macros::CALL_FIELD_COINS)
113    };
114}
115
116/// max gas key formatting macro
117#[macro_export]
118macro_rules! max_gas_key {
119    ($id:expr, $slot:expr) => {
120        deferred_call_field_key!($id, $slot, $crate::macros::CALL_FIELD_MAX_GAS)
121    };
122}
123
124/// fee key formatting macro
125#[macro_export]
126macro_rules! fee_key {
127    ($id:expr, $slot:expr) => {
128        deferred_call_field_key!($id, $slot, $crate::macros::CALL_FIELD_FEE)
129    };
130}
131
132/// cancelled key formatting macro
133#[macro_export]
134macro_rules! cancelled_key {
135    ($id:expr, $slot:expr) => {
136        deferred_call_field_key!($id, $slot, $crate::macros::CALL_FIELD_CANCELED)
137    };
138}
139
140#[cfg(test)]
141mod tests {
142    use massa_db_exports::DEFERRED_CALLS_PREFIX;
143    use massa_models::{
144        deferred_calls::{DeferredCallId, DeferredCallIdSerializer},
145        slot::Slot,
146    };
147    use massa_serialization::Serializer;
148
149    #[test]
150    fn test_deferred_call_prefix_key() {
151        let slot_ser = Slot {
152            period: 1,
153            thread: 5,
154        }
155        .to_bytes_key();
156
157        let id = DeferredCallId::new(
158            0,
159            Slot {
160                thread: 5,
161                period: 1,
162            },
163            1,
164            &[],
165        )
166        .unwrap();
167
168        let id_ser = DeferredCallIdSerializer::new();
169        let mut buf_id = Vec::new();
170        id_ser.serialize(&id, &mut buf_id).unwrap();
171
172        let prefix = ["deferred_calls/".as_bytes(), &slot_ser, &[0u8], &buf_id].concat();
173
174        assert_eq!(deferred_call_prefix_key!(buf_id, slot_ser), prefix);
175
176        // let to_check = [prefix[..], &[1u8]].concat();
177        // assert_eq!(sender_address_key!(buf_id, slot_ser), to_check);
178    }
179}