solders_address_lookup_table_account/
lib.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use derive_more::{From, Into};
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use serde::{Deserialize, Serialize};
use solana_program::clock::Slot;
use solana_program::slot_hashes::SlotHashes as SlotHashesOriginal;
use solana_program::{
    address_lookup_table::{
        instruction::derive_lookup_table_address as derive_lookup_table_address_original,
        state::{
            AddressLookupTable as AddressLookupTableOriginal,
            LookupTableMeta as LookupTableMetaOriginal,
            LookupTableStatus as LookupTableStatusOriginal,
        },
        AddressLookupTableAccount as AddressLookupTableAccountOriginal,
    },
    pubkey::Pubkey as PubkeyOriginal,
};
use solders_hash::Hash;
use solders_macros::{common_methods, richcmp_eq_only, EnumIntoPy};
use solders_pubkey::Pubkey;
use solders_traits_core::{
    handle_py_value_err, impl_display, py_from_bytes_general_via_bincode,
    pybytes_general_via_bincode, RichcmpEqualityOnly,
};
use std::borrow::Cow;

macro_rules! impl_defaults {
    ($s: ident) => {
        impl_display!($s);
        pybytes_general_via_bincode!($s);
        py_from_bytes_general_via_bincode!($s);

        solders_traits_core::common_methods_default!($s);
        impl RichcmpEqualityOnly for $s {}
    };
}

#[derive(Serialize, Deserialize)]
#[serde(remote = "AddressLookupTableAccountOriginal")]
struct AddressLookupTableAccountOriginalDef {
    key: PubkeyOriginal,
    addresses: Vec<PubkeyOriginal>,
}

/// The definition of address lookup table accounts as used by ``MessageV0``.
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, From, Into)]
#[pyclass(module = "solders.address_lookup_table_account", subclass)]
pub struct AddressLookupTableAccount(
    #[serde(with = "AddressLookupTableAccountOriginalDef")] AddressLookupTableAccountOriginal,
);

impl_defaults!(AddressLookupTableAccount);

#[richcmp_eq_only]
#[common_methods]
#[pymethods]
impl AddressLookupTableAccount {
    #[new]
    pub fn new(key: Pubkey, addresses: Vec<Pubkey>) -> Self {
        AddressLookupTableAccountOriginal {
            key: key.into(),
            addresses: addresses.into_iter().map(|a| a.into()).collect(),
        }
        .into()
    }

    #[getter]
    pub fn key(&self) -> Pubkey {
        self.0.key.into()
    }

    #[getter]
    pub fn addresses(&self) -> Vec<Pubkey> {
        self.0
            .addresses
            .clone()
            .into_iter()
            .map(|a| a.into())
            .collect()
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, From, Into)]
#[pyclass(module = "solders.address_lookup_table_account", subclass)]
pub struct LookupTableStatusDeactivating(pub usize);
impl_defaults!(LookupTableStatusDeactivating);

#[richcmp_eq_only]
#[common_methods]
#[pymethods]
impl LookupTableStatusDeactivating {
    #[new]
    pub fn new(remaining_blocks: usize) -> Self {
        Self(remaining_blocks)
    }

    #[getter]
    pub fn remaining_slots(&self) -> usize {
        self.0
    }
}

#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize, EnumIntoPy, FromPyObject)]
pub enum LookupTableStatusTagged {
    Deactivating(LookupTableStatusDeactivating),
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
#[pyclass(module = "solders.address_lookup_table_account")]
pub enum LookupTableStatusFieldless {
    Activated,
    Deactivated,
}

#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize, EnumIntoPy, FromPyObject)]
#[serde(untagged)]
pub enum LookupTableStatusType {
    Fieldless(LookupTableStatusFieldless),
    Tagged(LookupTableStatusTagged),
}

impl From<LookupTableStatusOriginal> for LookupTableStatusType {
    fn from(status: LookupTableStatusOriginal) -> Self {
        match status {
            LookupTableStatusOriginal::Activated => {
                Self::Fieldless(LookupTableStatusFieldless::Activated)
            }
            LookupTableStatusOriginal::Deactivated => {
                Self::Fieldless(LookupTableStatusFieldless::Deactivated)
            }
            LookupTableStatusOriginal::Deactivating { remaining_blocks } => {
                Self::Tagged(LookupTableStatusTagged::Deactivating(
                    LookupTableStatusDeactivating::new(remaining_blocks),
                ))
            }
        }
    }
}

impl From<LookupTableStatusType> for LookupTableStatusOriginal {
    fn from(status: LookupTableStatusType) -> Self {
        match status {
            LookupTableStatusType::Fieldless(LookupTableStatusFieldless::Activated) => {
                Self::Activated
            }
            LookupTableStatusType::Fieldless(LookupTableStatusFieldless::Deactivated) => {
                Self::Deactivated
            }
            LookupTableStatusType::Tagged(LookupTableStatusTagged::Deactivating(
                remaining_blocks,
            )) => Self::Deactivating {
                remaining_blocks: remaining_blocks.remaining_slots(),
            },
        }
    }
}

#[pyclass(module = "solders.address_lookup_table_account", subclass)]
#[derive(Debug, PartialEq, Eq, From, Into, Serialize, Deserialize)]
pub struct SlotHashes(SlotHashesOriginal);

impl Clone for SlotHashes {
    fn clone(&self) -> Self {
        SlotHashes(SlotHashesOriginal::new(self.0.slot_hashes()))
    }
}

impl_defaults!(SlotHashes);

#[richcmp_eq_only]
#[common_methods]
#[pymethods]
impl SlotHashes {
    #[new]
    pub fn new(slot_hashes: Vec<(Slot, Hash)>) -> Self {
        SlotHashes(SlotHashesOriginal::new(
            &slot_hashes
                .into_iter()
                .map(|(slot, hash)| (slot, hash.into()))
                .collect::<Vec<_>>(),
        ))
    }
    #[getter]
    pub fn slot_hashes(&self) -> Vec<(Slot, Hash)> {
        self.0
            .slot_hashes()
            .iter()
            .map(|(slot, hash)| (*slot, (*hash).into()))
            .collect()
    }
}

#[pyclass(module = "solders.address_lookup_table_account", subclass)]
#[derive(Clone, Debug, PartialEq, Eq, From, Into, Serialize, Deserialize)]
pub struct LookupTableMeta(LookupTableMetaOriginal);

impl_defaults!(LookupTableMeta);

#[richcmp_eq_only]
#[common_methods]
#[pymethods]
impl LookupTableMeta {
    #[new]
    #[pyo3(signature = (deactivation_slot = u64::MAX, last_extended_slot = 0, last_extended_slot_start_index = 0, authority = None, padding = 0))]
    pub fn new(
        deactivation_slot: u64,
        last_extended_slot: u64,
        last_extended_slot_start_index: u8,
        authority: Option<Pubkey>,
        padding: u16,
    ) -> Self {
        LookupTableMetaOriginal {
            deactivation_slot,
            last_extended_slot,
            last_extended_slot_start_index,
            authority: authority.map(Into::into),
            _padding: padding,
        }
        .into()
    }

    #[getter]
    pub fn deactivation_slot(&self) -> u64 {
        self.0.deactivation_slot
    }

    #[getter]
    pub fn last_extended_slot(&self) -> u64 {
        self.0.last_extended_slot
    }

    #[getter]
    pub fn last_extended_slot_start_index(&self) -> u8 {
        self.0.last_extended_slot_start_index
    }

    #[getter]
    pub fn authority(&self) -> Option<Pubkey> {
        self.0.authority.map(Into::into)
    }

    #[getter]
    pub fn padding(&self) -> u16 {
        self.0._padding
    }

    pub fn is_active(&self, current_slot: Slot, slot_hashes: SlotHashes) -> bool {
        self.0.is_active(current_slot, &slot_hashes.into())
    }

    pub fn status(&self, current_slot: Slot, slot_hashes: SlotHashes) -> LookupTableStatusType {
        self.0.status(current_slot, &slot_hashes.into()).into()
    }
}

#[derive(Serialize, Deserialize)]
#[serde(remote = "AddressLookupTableOriginal")]
pub struct AddressLookupTableOriginalDef<'a> {
    meta: LookupTableMetaOriginal,
    addresses: Cow<'a, [PubkeyOriginal]>,
}

#[pyclass(module = "solders.address_lookup_table_account", subclass)]
#[derive(Clone, Debug, PartialEq, From, Into, Serialize, Deserialize)]
pub struct AddressLookupTable(
    #[serde(with = "AddressLookupTableOriginalDef")] AddressLookupTableOriginal<'static>,
);

impl_defaults!(AddressLookupTable);

#[richcmp_eq_only]
#[common_methods]
#[pymethods]
impl AddressLookupTable {
    #[new]
    pub fn new(meta: LookupTableMeta, addresses: Vec<Pubkey>) -> Self {
        AddressLookupTableOriginal {
            meta: meta.into(),
            addresses: Cow::from(addresses.into_iter().map(Into::into).collect::<Vec<_>>()),
        }
        .into()
    }

    #[getter]
    pub fn meta(&self) -> LookupTableMeta {
        self.0.meta.clone().into()
    }

    #[getter]
    pub fn addresses(&self) -> Vec<Pubkey> {
        self.0.addresses.iter().map(Into::into).collect()
    }

    pub fn get_active_addresses_len(
        &self,
        current_slot: Slot,
        slot_hashes: SlotHashes,
    ) -> PyResult<usize> {
        handle_py_value_err(
            self.0
                .get_active_addresses_len(current_slot, &slot_hashes.into()),
        )
    }

    pub fn lookup(
        &self,
        current_slot: Slot,
        indexes: Vec<u8>,
        slot_hashes: SlotHashes,
    ) -> PyResult<Vec<Pubkey>> {
        handle_py_value_err(
            self.0
                .lookup(current_slot, indexes.as_slice(), &slot_hashes.into())
                .map(|v| v.into_iter().map(Into::into).collect::<Vec<_>>()),
        )
    }

    #[staticmethod]
    pub fn deserialize(data: &[u8]) -> PyResult<Self> {
        let address_looking_table = AddressLookupTableOriginal::deserialize(data)
            .map_err(|e| PyErr::new::<PyValueError, _>(format!("{:?}", e)))?;

        let addresses = Cow::from(
            address_looking_table
                .addresses
                .iter()
                .map(Clone::clone)
                .collect::<Vec<_>>(),
        );
        Ok(AddressLookupTableOriginal {
            meta: address_looking_table.meta,
            addresses,
        }
        .into())
    }
}

#[pyfunction]
pub fn derive_lookup_table_address(
    authority_address: Pubkey,
    recent_block_slot: u64,
) -> (Pubkey, u8) {
    let (lookup_table_address, bump_seed) =
        derive_lookup_table_address_original(&authority_address.into(), recent_block_slot);
    (lookup_table_address.into(), bump_seed)
}