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
//! Serialization formats for finalized data.
//!
//! # Correctness
//!
//! [`crate::constants::state_database_format_version_in_code()`] must be incremented
//! each time the database format (column, serialization, etc) changes.

use std::sync::Arc;

pub mod block;
pub mod chain;
pub mod shielded;
pub mod transparent;
pub mod upgrade;

#[cfg(feature = "shielded-scan")]
pub mod scan;

#[cfg(any(test, feature = "proptest-impl"))]
mod tests;

pub use block::{TransactionIndex, TransactionLocation, MAX_ON_DISK_HEIGHT};
pub use transparent::{OutputIndex, OutputLocation};

#[cfg(feature = "shielded-scan")]
pub use scan::{
    SaplingScannedDatabaseEntry, SaplingScannedDatabaseIndex, SaplingScannedResult,
    SaplingScanningKey,
};

#[cfg(any(test, feature = "proptest-impl"))]
pub use tests::KV;

/// Helper type for writing types to disk as raw bytes.
/// Also used to convert key types to raw bytes for disk lookups.
pub trait IntoDisk {
    /// The type used to write bytes to disk,
    /// and compare a value as a key to on-disk keys.
    type Bytes: AsRef<[u8]>;

    /// Converts the current type into serialized raw bytes.
    ///
    /// Used to convert keys to bytes in [`ReadDisk`][1],
    /// and keys and values to bytes in [`WriteDisk`][2].
    ///
    /// # Panics
    ///
    /// - if the input data doesn't serialize correctly
    ///
    /// [1]: super::disk_db::ReadDisk
    /// [2]: super::disk_db::WriteDisk
    fn as_bytes(&self) -> Self::Bytes;
}

/// Helper type for reading types from disk as raw bytes.
pub trait FromDisk: Sized {
    /// Converts raw disk bytes back into the deserialized type.
    ///
    /// Used to convert keys and values from bytes in [`ReadDisk`][1].
    ///
    /// # Panics
    ///
    /// - if the input data doesn't deserialize correctly
    ///
    /// [1]: super::disk_db::ReadDisk
    fn from_bytes(bytes: impl AsRef<[u8]>) -> Self;
}

// Generic serialization impls

impl<'a, T> IntoDisk for &'a T
where
    T: IntoDisk,
{
    type Bytes = T::Bytes;

    fn as_bytes(&self) -> Self::Bytes {
        T::as_bytes(*self)
    }
}

impl<T> IntoDisk for Arc<T>
where
    T: IntoDisk,
{
    type Bytes = T::Bytes;

    fn as_bytes(&self) -> Self::Bytes {
        T::as_bytes(self)
    }
}

impl<T> FromDisk for Arc<T>
where
    T: FromDisk,
{
    fn from_bytes(bytes: impl AsRef<[u8]>) -> Self {
        Arc::new(T::from_bytes(bytes))
    }
}

// Commonly used serialization impls

impl IntoDisk for () {
    type Bytes = [u8; 0];

    fn as_bytes(&self) -> Self::Bytes {
        []
    }
}

impl FromDisk for () {
    #[allow(clippy::unused_unit)]
    fn from_bytes(bytes: impl AsRef<[u8]>) -> Self {
        assert_eq!(
            bytes.as_ref().len(),
            0,
            "unexpected data in zero-sized column family type",
        );

        ()
    }
}

/// Access database keys or values as raw bytes.
/// Mainly for use in tests, runtime checks, or format compatibility code.
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct RawBytes(Vec<u8>);

// Note: don't implement From or Into for RawBytes, because it makes it harder to spot in reviews.
// Instead, implement IntoDisk and FromDisk on the original type, or a specific wrapper type.

impl RawBytes {
    /// Create a new raw byte key or data.
    ///
    /// Mainly for use in tests or runtime checks.
    /// These methods
    pub fn new_raw_bytes(bytes: Vec<u8>) -> Self {
        Self(bytes)
    }

    /// Create a new raw byte key or data.
    /// Mainly for use in tests.
    pub fn raw_bytes(&self) -> &Vec<u8> {
        &self.0
    }
}

impl IntoDisk for RawBytes {
    type Bytes = Vec<u8>;

    fn as_bytes(&self) -> Self::Bytes {
        self.raw_bytes().clone()
    }
}

impl FromDisk for RawBytes {
    fn from_bytes(bytes: impl AsRef<[u8]>) -> Self {
        Self::new_raw_bytes(bytes.as_ref().to_vec())
    }
}

// Serialization Modification Functions

/// Truncates `mem_bytes` to `disk_len`, by removing zero bytes from the start of the slice.
/// Used to discard unused zero bytes during serialization.
///
/// Return `None` if any of the truncated bytes are non-zero
///
/// # Panics
///
/// - if `mem_bytes` is shorter than `disk_len`.
pub fn truncate_zero_be_bytes(mem_bytes: &[u8], disk_len: usize) -> Option<&[u8]> {
    let discarded_bytes = mem_bytes
        .len()
        .checked_sub(disk_len)
        .expect("unexpected `mem_bytes` length: must be at least `disk_len`");

    if discarded_bytes == 0 {
        return Some(mem_bytes);
    }

    let (discarded, truncated) = mem_bytes.split_at(discarded_bytes);

    if !discarded.iter().all(|&byte| byte == 0) {
        return None;
    }

    assert_eq!(truncated.len(), disk_len);

    Some(truncated)
}

/// Expands `disk_bytes` to `mem_len`, by adding zero bytes at the start of the slice.
/// Used to zero-fill bytes that were discarded during serialization.
///
/// # Panics
///
/// - if `disk_bytes` is longer than `mem_len`
pub fn expand_zero_be_bytes(disk_bytes: &[u8], mem_len: usize) -> Vec<u8> {
    let extra_bytes = mem_len
        .checked_sub(disk_bytes.len())
        .expect("unexpected `disk_bytes` length: must not exceed `mem_len`");

    if extra_bytes == 0 {
        return disk_bytes.to_vec();
    }

    let mut expanded = vec![0; extra_bytes];
    expanded.extend(disk_bytes);

    assert_eq!(expanded.len(), mem_len);

    expanded
}