Skip to main content

xrpl_common_stdlib/objects/generated/
ledger_hashes.rs

1// GENERATED -- do not hand-edit. Run scripts/generate-ledger-objects.sh to regenerate.
2
3/// Placeholder buffer size for fields whose XRPL wire type has no genuine Rust
4/// mapping yet (VECTOR256, XCHAIN_BRIDGE, PATHSET, ...). Such getters return
5/// raw, unparsed bytes; see the summary at the top of `generated/mod.rs`.
6const RAW_UNMAPPED_FIELD_SIZE: usize = 512;
7
8use crate::host::Result;
9use crate::host::error_codes::match_result_code;
10use crate::host::home_le_field;
11use crate::host::le_field;
12use crate::objects::traits::CurrentLedgerObjectCommonFields;
13use crate::objects::traits::LedgerObjectCommonFields;
14use crate::objects::{current_ledger_object, ledger_object};
15use crate::sfield;
16
17/// Trait providing access to fields specific to LedgerHashes objects in any ledger.
18pub trait LedgerHashesFields: LedgerObjectCommonFields {
19    /// DEPRECATED Do not use. (The "recent hashes" object on Mainnet has the value `2` in this
20    /// field as a result of an old software bug. That value gets carried forward as the "recent
21    /// hashes" object is updated. New "previous history" objects do not have this field, nor do
22    /// "recent hashes" objects in parallel networks started with more recent versions of `xrpld`.)
23    fn first_ledger_sequence(&self) -> Result<Option<u32>> {
24        ledger_object::get_field_optional(self.get_slot_num(), sfield::FirstLedgerSequence)
25    }
26
27    /// The Ledger Index of the last entry in this object's `Hashes` array.
28    fn last_ledger_sequence(&self) -> Result<Option<u32>> {
29        ledger_object::get_field_optional(self.get_slot_num(), sfield::LastLedgerSequence)
30    }
31
32    /// An array of up to 256 ledger hashes. The contents depend on which sub-type of `LedgerHashes`
33    /// object this is.
34    /// Raw bytes; VECTOR256 is not yet typed in Rust.
35    fn hashes(&self) -> Result<[u8; RAW_UNMAPPED_FIELD_SIZE]> {
36        let mut buffer = [0u8; RAW_UNMAPPED_FIELD_SIZE];
37        let result_code = unsafe {
38            le_field(
39                self.get_slot_num(),
40                sfield::Hashes.into(),
41                buffer.as_mut_ptr(),
42                buffer.len(),
43            )
44        };
45        match_result_code(result_code, || buffer)
46    }
47}
48
49/// Trait providing access to fields specific to the current LedgerHashes object.
50pub trait CurrentLedgerHashesFields: CurrentLedgerObjectCommonFields {
51    /// DEPRECATED Do not use. (The "recent hashes" object on Mainnet has the value `2` in this
52    /// field as a result of an old software bug. That value gets carried forward as the "recent
53    /// hashes" object is updated. New "previous history" objects do not have this field, nor do
54    /// "recent hashes" objects in parallel networks started with more recent versions of `xrpld`.)
55    fn first_ledger_sequence(&self) -> Result<Option<u32>> {
56        current_ledger_object::get_field_optional(sfield::FirstLedgerSequence)
57    }
58
59    /// The Ledger Index of the last entry in this object's `Hashes` array.
60    fn last_ledger_sequence(&self) -> Result<Option<u32>> {
61        current_ledger_object::get_field_optional(sfield::LastLedgerSequence)
62    }
63
64    /// An array of up to 256 ledger hashes. The contents depend on which sub-type of `LedgerHashes`
65    /// object this is.
66    /// Raw bytes; VECTOR256 is not yet typed in Rust.
67    fn hashes(&self) -> Result<[u8; RAW_UNMAPPED_FIELD_SIZE]> {
68        let mut buffer = [0u8; RAW_UNMAPPED_FIELD_SIZE];
69        let result_code =
70            unsafe { home_le_field(sfield::Hashes.into(), buffer.as_mut_ptr(), buffer.len()) };
71        match_result_code(result_code, || buffer)
72    }
73}
74
75#[derive(Debug, Clone, Copy, Eq, PartialEq)]
76pub struct LedgerHashes {
77    pub(crate) slot_num: i32,
78}
79
80impl LedgerHashes {
81    /// Binds this handle to a host-managed slot holding a LedgerHashes ledger object.
82    pub fn new(slot_num: i32) -> Self {
83        Self { slot_num }
84    }
85}
86
87impl LedgerObjectCommonFields for LedgerHashes {
88    fn get_slot_num(&self) -> i32 {
89        self.slot_num
90    }
91}
92
93impl LedgerHashesFields for LedgerHashes {}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98    use crate::host::host_bindings_trait::MockHostBindings;
99    use crate::host::setup_mock;
100    use crate::objects::test_utils::*;
101
102    #[test]
103    fn read_all_fields() {
104        let mut mock = MockHostBindings::new();
105        mock_all_fields_present(&mut mock);
106        let _guard = setup_mock(mock);
107
108        let obj = LedgerHashes::new(0);
109
110        assert!(obj.hashes().is_ok());
111        assert!(obj.first_ledger_sequence().is_ok());
112        assert!(obj.last_ledger_sequence().is_ok());
113    }
114
115    #[test]
116    fn optional_fields_none() {
117        let mut mock = MockHostBindings::new();
118        mock_all_fields_not_found(&mut mock);
119        let _guard = setup_mock(mock);
120
121        let obj = LedgerHashes::new(0);
122
123        assert!(obj.first_ledger_sequence().unwrap().is_none());
124        assert!(obj.last_ledger_sequence().unwrap().is_none());
125    }
126}