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
use crate::mir::val_def::ValId;
use crate::mir::{constant::TryExtractFromError, expr::InvalidArgumentError};
use crate::types::type_unify::TypeUnificationError;
use super::{
constant_store::ConstantStore,
sigma_byte_reader::{SigmaByteRead, SigmaByteReader},
sigma_byte_writer::{SigmaByteWrite, SigmaByteWriter},
};
use crate::types::smethod::MethodId;
use bounded_vec::BoundedVec;
use bounded_vec::BoundedVecOutOfBounds;
use io::Cursor;
use sigma_ser::vlq_encode;
use std::convert::TryInto;
use std::io;
use thiserror::Error;
#[derive(Error, Eq, PartialEq, Debug, Clone)]
pub enum SigmaSerializationError {
#[error("IO error: {0}")]
Io(String),
#[error("serialization not yet implemented: {0}")]
NotImplementedYet(&'static str),
#[error("Unexpected value: {0:?}")]
UnexpectedValue(TryExtractFromError),
#[error("serialization not supported: {0}")]
NotSupported(&'static str),
}
impl From<io::Error> for SigmaSerializationError {
fn from(error: io::Error) -> Self {
SigmaSerializationError::Io(error.to_string())
}
}
impl From<TryExtractFromError> for SigmaSerializationError {
fn from(e: TryExtractFromError) -> Self {
SigmaSerializationError::UnexpectedValue(e)
}
}
#[derive(Error, Eq, PartialEq, Debug, Clone)]
pub enum SigmaParsingError {
#[error("invalid op code: {0}")]
InvalidOpCode(u8),
#[error("not implemented op error")]
NotImplementedOpCode(String),
#[error("type parsing error")]
InvalidTypeCode(u8),
#[error("vlq encode error: {0}")]
VlqEncode(#[from] vlq_encode::VlqEncodingError),
#[error("IO error: {0}")]
Io(String),
#[error("misc error")]
Misc(String),
#[error("parsing not yet implemented: {0}")]
NotImplementedYet(String),
#[error("Constant with index {0} not found in constant store")]
ConstantForPlaceholderNotFound(u32),
#[error("Value out of bounds: {0}")]
ValueOutOfBounds(String),
#[error("Tuple items out of bounds: {0}")]
TupleItemsOutOfBounds(usize),
#[error("ValDef type for an index {0:?} not found in ValDefTypeStore store")]
ValDefIdNotFound(ValId),
#[error("Invalid argument: {0:?}")]
InvalidArgument(#[from] InvalidArgumentError),
#[error("No method id {0:?} found in type companion with type id {1:?} ")]
UnknownMethodId(MethodId, u8),
#[error("parsing not supported: {0}")]
NotSupported(&'static str),
#[error("serialization error: {0}")]
SerializationError(#[from] SigmaSerializationError),
#[error("Invalid item quantity in BoundedVec: {0}")]
BoundedVecOutOfBounds(#[from] BoundedVecOutOfBounds),
}
impl From<io::Error> for SigmaParsingError {
fn from(error: io::Error) -> Self {
SigmaParsingError::Io(error.to_string())
}
}
impl From<&io::Error> for SigmaParsingError {
fn from(error: &io::Error) -> Self {
SigmaParsingError::Io(error.to_string())
}
}
impl From<TypeUnificationError> for SigmaParsingError {
fn from(e: TypeUnificationError) -> Self {
SigmaParsingError::Misc(format!("{:?}", e))
}
}
pub type SigmaSerializeResult = Result<(), SigmaSerializationError>;
pub trait SigmaSerializable: Sized {
fn sigma_serialize<W: SigmaByteWrite>(&self, w: &mut W) -> SigmaSerializeResult;
fn sigma_parse<R: SigmaByteRead>(r: &mut R) -> Result<Self, SigmaParsingError>;
fn sigma_serialize_bytes(&self) -> Result<Vec<u8>, SigmaSerializationError> {
let mut data = Vec::new();
let mut w = SigmaByteWriter::new(&mut data, None);
self.sigma_serialize(&mut w)?;
Ok(data)
}
fn sigma_parse_bytes(bytes: &[u8]) -> Result<Self, SigmaParsingError> {
let cursor = Cursor::new(bytes);
let mut sr = SigmaByteReader::new(cursor, ConstantStore::empty());
Self::sigma_parse(&mut sr)
}
}
impl<T: SigmaSerializable> SigmaSerializable for Vec<T> {
fn sigma_serialize<W: SigmaByteWrite>(&self, w: &mut W) -> SigmaSerializeResult {
w.put_u32(self.len() as u32)?;
self.iter().try_for_each(|i| i.sigma_serialize(w))
}
fn sigma_parse<R: SigmaByteRead>(r: &mut R) -> Result<Self, SigmaParsingError> {
let items_count = r.get_u32()?;
let mut items = Vec::with_capacity(items_count as usize);
for _ in 0..items_count {
items.push(T::sigma_parse(r)?);
}
Ok(items)
}
}
impl<T: SigmaSerializable, const L: usize, const U: usize> SigmaSerializable
for BoundedVec<T, L, U>
{
fn sigma_serialize<W: SigmaByteWrite>(&self, w: &mut W) -> SigmaSerializeResult {
self.as_vec().sigma_serialize(w)
}
fn sigma_parse<R: SigmaByteRead>(r: &mut R) -> Result<Self, SigmaParsingError> {
Ok(Vec::<T>::sigma_parse(r)?.try_into()?)
}
}
impl SigmaSerializable for u32 {
fn sigma_serialize<W: SigmaByteWrite>(&self, w: &mut W) -> SigmaSerializeResult {
w.put_u32(*self)?;
Ok(())
}
fn sigma_parse<R: SigmaByteRead>(r: &mut R) -> Result<Self, SigmaParsingError> {
let v = r.get_u32()?;
Ok(v)
}
}
impl<T: SigmaSerializable> SigmaSerializable for Option<Box<T>> {
fn sigma_serialize<W: SigmaByteWrite>(&self, w: &mut W) -> SigmaSerializeResult {
match self {
Some(v) => {
w.put_u8(1)?;
v.sigma_serialize(w)
}
None => Ok(w.put_u8(0)?),
}
}
fn sigma_parse<R: SigmaByteRead>(r: &mut R) -> Result<Self, SigmaParsingError> {
let tag = r.get_u8()?;
Ok(if tag != 0 {
Some(T::sigma_parse(r)?.into())
} else {
None
})
}
}
#[allow(clippy::expect_used)]
pub fn sigma_serialize_roundtrip<T: SigmaSerializable>(v: &T) -> T {
let mut data = Vec::new();
let mut w = SigmaByteWriter::new(&mut data, None);
v.sigma_serialize(&mut w).expect("serialization failed");
let cursor = Cursor::new(&mut data[..]);
let mut sr = SigmaByteReader::new(cursor, ConstantStore::empty());
T::sigma_parse(&mut sr).expect("parse failed")
}