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
use bm::{Error, ReadBackend, WriteBackend, Construct};
use primitive_types::U256;
use alloc::vec::Vec;

use crate::{ElementalFixedVec, FromCompactVectorTree, FromCompositeVectorTree,
            ElementalFixedVecRef, IntoCompactVectorTree,
            IntoCompositeVectorTree, CompatibleConstruct};
use crate::utils::{mix_in_length, decode_with_length};

/// Traits for list converting into a tree structure.
pub trait IntoCompositeListTree {
    /// Convert this list into merkle tree, writing nodes into the
    /// given database, and using the maximum length specified.
    fn into_composite_list_tree<DB: WriteBackend>(
        &self,
        db: &mut DB,
        max_len: Option<u64>
    ) -> Result<<DB::Construct as Construct>::Value, Error<DB::Error>> where
        DB::Construct: CompatibleConstruct;
}

/// Traits for list converting into a tree structure.
pub trait IntoCompactListTree {
    /// Convert this list into merkle tree, writing nodes into the
    /// given database, and using the maximum length specified.
    fn into_compact_list_tree<DB: WriteBackend>(
        &self,
        db: &mut DB,
        max_len: Option<u64>
    ) -> Result<<DB::Construct as Construct>::Value, Error<DB::Error>> where
        DB::Construct: CompatibleConstruct;
}

/// Traits for list converting from a tree structure.
pub trait FromCompositeListTree: Sized {
    /// Convert this type from merkle tree, reading nodes from the
    /// given database, with given maximum length.
    fn from_composite_list_tree<DB: ReadBackend>(
        root: &<DB::Construct as Construct>::Value,
        db: &mut DB,
        max_len: Option<u64>,
    ) -> Result<Self, Error<DB::Error>> where
        DB::Construct: CompatibleConstruct;
}

/// Traits for list converting from a tree structure.
pub trait FromCompactListTree: Sized {
    /// Convert this type from merkle tree, reading nodes from the
    /// given database, with given maximum length.
    fn from_compact_list_tree<DB: ReadBackend>(
        root: &<DB::Construct as Construct>::Value,
        db: &mut DB,
        max_len: Option<u64>,
    ) -> Result<Self, Error<DB::Error>> where
        DB::Construct: CompatibleConstruct;
}

#[derive(Debug, Clone, Eq, PartialEq)]
/// Variable `Vec` reference. In `ssz`'s definition, this is a "list".
pub struct ElementalVariableVecRef<'a, T>(pub &'a [T]);
#[derive(Debug, Clone, Eq, PartialEq)]
/// Variable `Vec` value. In `ssz`'s definition, this is a "list".
pub struct ElementalVariableVec<T>(pub Vec<T>);

macro_rules! impl_packed {
    ( $t:ty ) => {
        impl<'a> IntoCompactListTree for ElementalVariableVecRef<'a, $t> {
            fn into_compact_list_tree<DB: WriteBackend>(
                &self,
                db: &mut DB,
                max_len: Option<u64>
            ) -> Result<<DB::Construct as Construct>::Value, Error<DB::Error>> where
                DB::Construct: CompatibleConstruct,
            {
                let len = self.0.len();

                mix_in_length(&ElementalFixedVecRef(&self.0).into_compact_vector_tree(db, max_len)?,
                              db, len)
            }
        }
    }
}

impl_packed!(bool);
impl_packed!(u8);
impl_packed!(u16);
impl_packed!(u32);
impl_packed!(u64);
impl_packed!(u128);
impl_packed!(U256);

impl<'a, T> IntoCompositeListTree for ElementalVariableVecRef<'a, T> where
    for<'b> ElementalFixedVecRef<'b, T>: IntoCompositeVectorTree,
{
    fn into_composite_list_tree<DB: WriteBackend>(
        &self,
        db: &mut DB,
        max_len: Option<u64>
    ) -> Result<<DB::Construct as Construct>::Value, Error<DB::Error>> where
        DB::Construct: CompatibleConstruct,
    {
        let len = self.0.len();

        mix_in_length(&ElementalFixedVecRef(&self.0).into_composite_vector_tree(db, max_len)?,
                      db, len)
    }
}

fn from_list_tree<T, F, DB: ReadBackend>(
    root: &<DB::Construct as Construct>::Value,
    db: &mut DB,
    max_len: Option<u64>,
    f: F
) -> Result<ElementalVariableVec<T>, Error<DB::Error>> where
    DB::Construct: CompatibleConstruct,
    F: FnOnce(&<DB::Construct as Construct>::Value, &mut DB, usize, Option<u64>) -> Result<ElementalFixedVec<T>, Error<DB::Error>>
{
    let (vector_root, len) = decode_with_length::<<DB::Construct as Construct>::Value, _>(root, db)?;

    let vector = f(
        &vector_root, db, len, max_len
    )?;

    Ok(ElementalVariableVec(vector.0))
}

impl<T> FromCompactListTree for ElementalVariableVec<T> where
    ElementalFixedVec<T>: FromCompactVectorTree,
{
    fn from_compact_list_tree<DB: ReadBackend>(
        root: &<DB::Construct as Construct>::Value,
        db: &mut DB,
        max_len: Option<u64>,
    ) -> Result<Self, Error<DB::Error>> where
        DB::Construct: CompatibleConstruct,
    {
        from_list_tree(root, db, max_len, |vector_root, db, len, max_len| {
            ElementalFixedVec::<T>::from_compact_vector_tree(
                vector_root, db, len, max_len
            )
        })
    }
}

impl<T> FromCompositeListTree for ElementalVariableVec<T> where
    ElementalFixedVec<T>: FromCompositeVectorTree,
{
    fn from_composite_list_tree<DB: ReadBackend>(
        root: &<DB::Construct as Construct>::Value,
        db: &mut DB,
        max_len: Option<u64>,
    ) -> Result<Self, Error<DB::Error>> where
        DB::Construct: CompatibleConstruct,
    {
        from_list_tree(root, db, max_len, |vector_root, db, len, max_len| {
            ElementalFixedVec::<T>::from_composite_vector_tree(
                vector_root, db, len, max_len
            )
        })
    }
}

impl<T> IntoCompactListTree for ElementalVariableVec<T> where
    for<'a> ElementalVariableVecRef<'a, T>: IntoCompactListTree,
{
    fn into_compact_list_tree<DB: WriteBackend>(
        &self,
        db: &mut DB,
        max_len: Option<u64>
    ) -> Result<<DB::Construct as Construct>::Value, Error<DB::Error>> where
        DB::Construct: CompatibleConstruct,
    {
        ElementalVariableVecRef(&self.0).into_compact_list_tree(db, max_len)
    }
}

impl<T> IntoCompositeListTree for ElementalVariableVec<T> where
    for<'a> ElementalVariableVecRef<'a, T>: IntoCompositeListTree,
{
    fn into_composite_list_tree<DB: WriteBackend>(
        &self,
        db: &mut DB,
        max_len: Option<u64>
    ) -> Result<<DB::Construct as Construct>::Value, Error<DB::Error>> where
        DB::Construct: CompatibleConstruct,
    {
        ElementalVariableVecRef(&self.0).into_composite_list_tree(db, max_len)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{IntoTree, FromTree, DigestConstruct};

    use bm::InMemoryBackend;
    use sha2::Sha256;

    #[test]
    fn test_plain() {
        let data = {
            let mut ret = Vec::new();
            for i in 0..17u16 {
                ret.push(i);
            }
            ret
        };

        let mut db = InMemoryBackend::<DigestConstruct<Sha256>>::default();
        let encoded = data.into_tree(&mut db).unwrap();
        let decoded = Vec::<u16>::from_tree(&encoded, &mut db).unwrap();
        assert_eq!(data, decoded);
    }
}