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
// -*- mode: rust; -*-
//
// Copyright (c) 2019 Web 3 Foundation
//
// Authors:
// - Jeffrey Burdges <jeff@web3.foundation>

#![cfg_attr(not(feature = "std"), no_std)]
#![deny(unsafe_code)]
#![doc = include_str!("../README.md")]

use ark_std::{
    borrow::Borrow,
    fmt,
    io::{self, Read, Write},
    vec::Vec,
};
type ArkResult<T> = Result<T, io::Error>;
use ark_serialize::{
    self, CanonicalDeserialize, CanonicalSerialize, Compress, SerializationError, Validate,
};

use parity_scale_codec::{self as scale, Decode, Encode, Input, Output};
// type ScaleResult<T> = Result<T,scale::Error>;

pub mod rw;
use rw::*;

#[cfg(feature = "hazmat")]
pub mod hazmat;

#[cfg(test)]
mod tests;

/*
error: `(Compress, Validate)` is forbidden as the type of a const generic parameter
   --> src/lib.rs:145:33
    = note: the only supported types are integers, `bool` and `char`
*/

/// Arkworks' serialization modes, morally (Compress, Validate) but
/// const generics only supports integers, `bool` and `char` right now.
pub type Usage = u8; // (Compress, Validate)

/// Arkworks' serialization modes hack.
pub const fn make_usage(compress: Compress, validate: Validate) -> Usage {
    let c = match compress {
        Compress::Yes => 0,
        Compress::No => 1,
    };
    let v = match validate {
        Validate::Yes => 0,
        Validate::No => 2,
    };
    c | v
}

pub const fn is_compressed(u: Usage) -> Compress {
    // u.0
    assert!(u < 4);
    if u & 1 == 1 {
        Compress::No
    } else {
        Compress::Yes
    }
}

pub const fn is_validated(u: Usage) -> Validate {
    // u.1
    assert!(u < 4);
    if u & 2 == 2 {
        Validate::No
    } else {
        Validate::Yes
    }
}

/// ArkScale usage for typical wire formats, like block data and gossip messages.  Always safe.
pub const WIRE: Usage = make_usage(Compress::Yes, Validate::Yes);

/// ArkScale usage which neither compresses nor validates inputs,
/// only for usage in host calls where the runtime already performed
/// validation checks.
pub const HOST_CALL: Usage = make_usage(Compress::No, Validate::No);

/// Arkworks type wrapped for serialization by Scale
pub struct ArkScale<T, const U: Usage = WIRE>(pub T);

impl<T, const U: Usage> From<T> for ArkScale<T, U> {
    fn from(t: T) -> ArkScale<T, U> {
        ArkScale(t)
    }
}

// impl<'a,T: Clone, const U: Usage> From<&'a T> for ArkScale<T,U> {
//     fn from(t: &'a T) -> ArkScale<T,U> { ArkScale(t.clone()) }
// }

impl<T: CanonicalDeserialize, const U: Usage> Decode for ArkScale<T, U> {
    fn decode<I: Input>(input: &mut I) -> Result<Self, scale::Error> {
        <T as CanonicalDeserialize>::deserialize_with_mode(
            InputAsRead(input),
            is_compressed(U),
            is_validated(U),
        )
        .map(|v| ArkScale(v))
        .map_err(ark_error_to_scale_error)
    }

    // fn skip<I: Input>(input: &mut I) -> Result<(), Error> { ... }

    // fn encoded_fixed_size() -> Option<usize> { ... }
}

const OOPS: &'static str =
    "Arkworks serialization failed, but Scale cannot handle serialization failures.";

impl<T: CanonicalSerialize, const U: Usage> Encode for ArkScale<T, U> {
    fn size_hint(&self) -> usize {
        self.0.serialized_size(is_compressed(U))
    }

    fn encode_to<O: Output + ?Sized>(&self, dest: &mut O) {
        self.0
            .serialize_with_mode(OutputAsWrite(dest), is_compressed(U))
            .expect(OOPS);
    }

    // TODO:  Arkworks wants an io::Write, so we ignre the rule that
    // value types override using_encoded.
    // fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R;

    fn encoded_size(&self) -> usize {
        self.0.serialized_size(is_compressed(U))
    }
}

pub struct ArkScaleRef<'a, T, const U: Usage = WIRE>(pub &'a T);

impl<'a, T, const U: Usage> From<&'a T> for ArkScaleRef<'a, T, U> {
    fn from(t: &'a T) -> ArkScaleRef<'a, T, U> {
        ArkScaleRef(t)
    }
}

impl<'a, T: CanonicalSerialize, const U: Usage> Encode for ArkScaleRef<'a, T, U> {
    fn size_hint(&self) -> usize {
        self.0.serialized_size(is_compressed(U))
    }

    fn encode_to<O: Output + ?Sized>(&self, dest: &mut O) {
        self.0
            .serialize_with_mode(OutputAsWrite(dest), is_compressed(U))
            .expect(OOPS);
    }

    // TODO:  Arkworks wants an io::Write, so we ignre the rule that
    // value types override using_encoded.
    // fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R;

    fn encoded_size(&self) -> usize {
        self.0.serialized_size(is_compressed(U))
    }
}

/// Arkworks' `CanonicalSerialize` cannot consume `Iterator`s directly,
/// but `iter_ark_to_ark_bytes` serializes exactly like `Vec<T>`,
/// `&'a [T]`, or `[T]` do with `CanonicalSerialize`.
///
/// Returns errors as `ark_serialize::SerializationError`.
pub fn iter_ark_to_ark_bytes<T, B, I>(iter: I, usage: Usage) -> Result<Vec<u8>, SerializationError>
where
    T: CanonicalSerialize,
    B: Borrow<T>,
    I: IntoIterator<Item = B>,
{
    const LL: usize = 8;
    let mut iter = iter.into_iter();
    let len = iter.size_hint().0;
    let first = iter.next();
    let mut vec = if let Some(ref e) = first {
        let size = e.borrow().serialized_size(is_compressed(usage));
        Vec::with_capacity(LL + size * (1 + len))
    } else {
        Vec::with_capacity(LL)
    };
    vec.extend_from_slice(&[0u8; LL]);
    if let Some(e) = first {
        e.borrow()
            .serialize_with_mode(&mut vec, is_compressed(usage))?;
        let mut l = 1;
        for e in iter {
            e.borrow()
                .serialize_with_mode(&mut vec, is_compressed(usage))?;
            l += 1;
        }
        debug_assert_eq!(
            l, len,
            "Iterator::size_hint underestimate would slow down release execution."
        );
        // let err = |_| scale_error_to_ark_error(scale::Error::from("Arkworks cannot serialize more than 2^32 items."));
        // let l = u32::try_from(l).map_err(err) ?;
        (&mut vec)[0..LL].copy_from_slice(&(l as u64).to_le_bytes());
    }
    Ok(vec)
}

/// Arkworks' `CanonicalSerialize` cannot consume `Iterator`s directly,
/// but `iter_ark_to_scale_bytes` serializes exactly like
/// `ArkScale(Vec<T>)`, `ArkScale(&'a [T])`, or `ArkScale([T])` do
/// under `parity_scale_codec::Encode`.
///
/// Returns errors as `parity_scale_codec::Error`.
pub fn iter_ark_to_scale_bytes<T, B, I>(iter: I, usage: Usage) -> Result<Vec<u8>, scale::Error>
where
    T: CanonicalSerialize,
    B: Borrow<T>,
    I: IntoIterator<Item = B>,
{
    iter_ark_to_ark_bytes(iter, usage).map_err(ark_error_to_scale_error)
}