-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathserialization_helpers.rs
More file actions
613 lines (548 loc) · 20.2 KB
/
Copy pathserialization_helpers.rs
File metadata and controls
613 lines (548 loc) · 20.2 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
//! Implement a human-friendly serialization for some types.
//! This allows us to use an efficient binary representation when serializing
//! to storage, but have a nice string-based representation when using JSON.
//!
//! For example, a `Hash` can be serialized as `[13, 53, 125, ...]` or as `"0d357d..."`.
// Ideally all this code would be generated with a `#[serde(human_readable_string)]` macro.
use crate::{
chain::{
GenesisBlockInfo, Hash, OutputPointer, PublicKeyHash, RADRetrieve, RADType,
ValueTransferOutput, SHA256,
},
get_environment,
utxo_pool::UtxoSelectionStrategy,
};
use serde::{
de::{self, IntoDeserializer, MapAccess, SeqAccess, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};
use std::{fmt, fmt::Display, str::FromStr};
#[derive(Deserialize, Serialize)]
enum HashSerializationHelper {
/// SHA-256 Hash
SHA256(SHA256),
}
impl From<Hash> for HashSerializationHelper {
fn from(x: Hash) -> Self {
match x {
Hash::SHA256(a) => HashSerializationHelper::SHA256(a),
}
}
}
impl From<HashSerializationHelper> for Hash {
fn from(x: HashSerializationHelper) -> Self {
match x {
HashSerializationHelper::SHA256(a) => Hash::SHA256(a),
}
}
}
impl Serialize for Hash {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if serializer.is_human_readable() {
serializer.collect_str(&self)
} else {
HashSerializationHelper::from(*self).serialize(serializer)
}
}
}
impl<'de> Deserialize<'de> for Hash {
fn deserialize<D>(deserializer: D) -> Result<Hash, D::Error>
where
D: Deserializer<'de>,
{
if deserializer.is_human_readable() {
String::deserialize(deserializer)?
.parse::<Hash>()
.map_err(de::Error::custom)
} else {
HashSerializationHelper::deserialize(deserializer).map(Into::into)
}
}
}
#[derive(Deserialize, Serialize)]
struct PublicKeyHashSerializationHelper {
hash: [u8; 20],
}
impl From<PublicKeyHash> for PublicKeyHashSerializationHelper {
fn from(x: PublicKeyHash) -> Self {
Self { hash: x.hash }
}
}
impl From<PublicKeyHashSerializationHelper> for PublicKeyHash {
fn from(x: PublicKeyHashSerializationHelper) -> Self {
PublicKeyHash { hash: x.hash }
}
}
impl Serialize for PublicKeyHash {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if serializer.is_human_readable() {
serializer.collect_str(&self.bech32(get_environment()))
} else {
PublicKeyHashSerializationHelper::from(*self).serialize(serializer)
}
}
}
impl<'de> Deserialize<'de> for PublicKeyHash {
fn deserialize<D>(deserializer: D) -> Result<PublicKeyHash, D::Error>
where
D: Deserializer<'de>,
{
if deserializer.is_human_readable() {
let s = String::deserialize(deserializer)?;
PublicKeyHash::from_bech32(get_environment(), &s).map_err(de::Error::custom)
} else {
PublicKeyHashSerializationHelper::deserialize(deserializer).map(Into::into)
}
}
}
#[derive(Deserialize, Serialize)]
struct OutputPointerSerializationHelper {
pub transaction_id: Hash,
pub output_index: u32,
}
impl From<OutputPointer> for OutputPointerSerializationHelper {
fn from(x: OutputPointer) -> Self {
OutputPointerSerializationHelper {
transaction_id: x.transaction_id,
output_index: x.output_index,
}
}
}
impl From<OutputPointerSerializationHelper> for OutputPointer {
fn from(x: OutputPointerSerializationHelper) -> Self {
OutputPointer {
transaction_id: x.transaction_id,
output_index: x.output_index,
}
}
}
impl Serialize for OutputPointer {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if serializer.is_human_readable() {
serializer.collect_str(&self)
} else {
OutputPointerSerializationHelper::from(*self).serialize(serializer)
}
}
}
impl<'de> Deserialize<'de> for OutputPointer {
fn deserialize<D>(deserializer: D) -> Result<OutputPointer, D::Error>
where
D: Deserializer<'de>,
{
if deserializer.is_human_readable() {
String::deserialize(deserializer)?
.parse::<OutputPointer>()
.map_err(de::Error::custom)
} else {
OutputPointerSerializationHelper::deserialize(deserializer).map(Into::into)
}
}
}
/// Serialization helper for `GenesisBlockInfo`.
#[derive(Deserialize)]
pub struct GenesisBlock {
alloc: Vec<Vec<GenesisValueTransferOutput>>,
}
#[derive(Deserialize)]
struct GenesisValueTransferOutput {
#[serde(deserialize_with = "deserialize_from_str")]
address: PublicKeyHash,
#[serde(deserialize_with = "deserialize_from_str")]
value: u64,
#[serde(deserialize_with = "deserialize_from_str")]
timelock: u64,
}
// https://serde.rs/attr-bound.html
/// Deserialize a type `S` by deserializing a string, then using the `FromStr`
/// impl of `S` to create the result. The generic type `S` is not required to
/// implement `Deserialize`.
fn deserialize_from_str<'de, S, D>(deserializer: D) -> Result<S, D::Error>
where
S: FromStr,
S::Err: Display,
D: Deserializer<'de>,
{
let s: &str = Deserialize::deserialize(deserializer)?;
S::from_str(s).map_err(de::Error::custom)
}
impl From<GenesisValueTransferOutput> for ValueTransferOutput {
fn from(x: GenesisValueTransferOutput) -> Self {
Self {
pkh: x.address,
value: x.value,
time_lock: x.timelock,
}
}
}
impl From<GenesisBlock> for GenesisBlockInfo {
fn from(x: GenesisBlock) -> Self {
Self {
alloc: x
.alloc
.into_iter()
.map(|alloc| alloc.into_iter().map(ValueTransferOutput::from).collect())
.collect(),
}
}
}
#[derive(Serialize, Deserialize)]
enum UtxoSelectionStrategyName {
#[serde(rename = "random", alias = "Random")]
Random,
#[serde(rename = "big_first", alias = "BigFirst")]
BigFirst,
#[serde(rename = "small_first", alias = "SmallFirst")]
SmallFirst,
}
impl From<UtxoSelectionStrategyName> for UtxoSelectionStrategy {
fn from(x: UtxoSelectionStrategyName) -> UtxoSelectionStrategy {
match x {
UtxoSelectionStrategyName::Random => UtxoSelectionStrategy::Random { from: None },
UtxoSelectionStrategyName::BigFirst => UtxoSelectionStrategy::BigFirst { from: None },
UtxoSelectionStrategyName::SmallFirst => {
UtxoSelectionStrategy::SmallFirst { from: None }
}
}
}
}
impl<'a> From<&'a UtxoSelectionStrategy> for UtxoSelectionStrategyName {
fn from(x: &'a UtxoSelectionStrategy) -> UtxoSelectionStrategyName {
match x {
UtxoSelectionStrategy::Random { .. } => UtxoSelectionStrategyName::Random,
UtxoSelectionStrategy::BigFirst { .. } => UtxoSelectionStrategyName::BigFirst,
UtxoSelectionStrategy::SmallFirst { .. } => UtxoSelectionStrategyName::SmallFirst,
}
}
}
// #[serde(untagged)] is a great way to allow serializing as one of many possible representations,
// however the error messages are really bad:
// "data did not match any variant of untagged enum UtxoSelectionStrategyHelper"
// There is a pull request in serde to fix this:
// https://github.com/serde-rs/serde/pull/1544
// But it is not merged, so the alternative is to manually implement a visitor to deserialize this.
#[derive(Deserialize, Serialize)]
#[serde(untagged)]
enum UtxoSelectionStrategyHelper {
String(UtxoSelectionStrategyName),
Object(UtxoSelectionStrategyObject),
}
#[derive(Deserialize, Serialize)]
struct UtxoSelectionStrategyObject {
strategy: UtxoSelectionStrategyName,
from: Option<PublicKeyHash>,
}
impl From<UtxoSelectionStrategyHelper> for UtxoSelectionStrategy {
fn from(x: UtxoSelectionStrategyHelper) -> UtxoSelectionStrategy {
match x {
UtxoSelectionStrategyHelper::String(name) => name.into(),
UtxoSelectionStrategyHelper::Object(UtxoSelectionStrategyObject { strategy, from }) => {
let mut strategy = UtxoSelectionStrategy::from(strategy);
*strategy.get_from_mut() = from;
strategy
}
}
}
}
impl<'a> From<&'a UtxoSelectionStrategy> for UtxoSelectionStrategyHelper {
fn from(x: &'a UtxoSelectionStrategy) -> UtxoSelectionStrategyHelper {
let name = UtxoSelectionStrategyName::from(x);
match x.get_from() {
None => {
// If from field is None, serialize self as string
UtxoSelectionStrategyHelper::String(name)
}
Some(from) => {
// If from field is Some, serialize as {"strategy": name, "from": from }
UtxoSelectionStrategyHelper::Object(UtxoSelectionStrategyObject {
strategy: name,
from: Some(*from),
})
}
}
}
}
impl Serialize for UtxoSelectionStrategy {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
UtxoSelectionStrategyHelper::from(self).serialize(serializer)
}
}
struct UtxoSelectionStrategyVisitor;
impl<'de> Visitor<'de> for UtxoSelectionStrategyVisitor {
type Value = UtxoSelectionStrategy;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "a string or an object")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
// Delegate implementation of visit_str to UtxoSelectionStrategyName
UtxoSelectionStrategyName::deserialize(v.into_deserializer())
.map(|x| UtxoSelectionStrategyHelper::String(x).into())
}
fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
// Delegate implementation of visit_map to UtxoSelectionStrategyObject
UtxoSelectionStrategyObject::deserialize(de::value::MapAccessDeserializer::new(map))
.map(|x| UtxoSelectionStrategyHelper::Object(x).into())
}
}
impl<'de> Deserialize<'de> for UtxoSelectionStrategy {
fn deserialize<D>(deserializer: D) -> Result<UtxoSelectionStrategy, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_any(UtxoSelectionStrategyVisitor)
}
}
/// Serialization helper for `RADRetrieve`.
/// Serializes `RADRetrieve` as a 2-field struct: `(db_version: u32, rad_retrieve: RADRetrieve)`.
/// Deserializes `RADRetrieve` in a backwards compatible way with bincode.
#[derive(Serialize)]
struct RADRetrieveSerializationHelperVersioned(u32, RADRetrieveSerializationHelperBincode);
impl RADRetrieveSerializationHelperVersioned {
const LATEST_VERSION: u32 = 3;
}
/// This should be the same as `RADRetrieve`, it exists because we want to use the automatically
/// derived serialization code.
#[derive(Serialize, Deserialize)]
struct RADRetrieveSerializationHelperJson {
/// Kind of retrieval
pub kind: RADType,
/// URL
#[serde(default, skip_serializing_if = "String::is_empty")]
pub url: String,
/// Serialized RADON script
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub script: Vec<u8>,
/// Body of a HTTP-POST request
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub body: Vec<u8>,
/// Extra headers of a HTTP-GET, HTTP-HEAD or HTTP-POST request
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub headers: Vec<(String, String)>,
}
/// This should be the same as `RADRetrieveSerializationHelperJson`, but bincode does not support
/// the serde attribute `skip_serializing_if`.
#[derive(Serialize, Deserialize)]
struct RADRetrieveSerializationHelperBincode {
/// Kind of retrieval
pub kind: RADType,
/// URL
pub url: String,
/// Serialized RADON script
pub script: Vec<u8>,
/// Body of a HTTP-POST request
pub body: Vec<u8>,
/// Extra headers of a HTTP-GET, HTTP-HEAD or HTTP-POST request
pub headers: Vec<(String, String)>,
}
impl From<RADRetrieve> for RADRetrieveSerializationHelperVersioned {
fn from(x: RADRetrieve) -> Self {
let db_version = Self::LATEST_VERSION;
let RADRetrieve {
kind,
url,
script,
body,
headers,
} = x;
Self(
db_version,
RADRetrieveSerializationHelperBincode {
kind,
url,
script,
body,
headers,
},
)
}
}
impl From<RADRetrieveSerializationHelperVersioned> for RADRetrieve {
fn from(x: RADRetrieveSerializationHelperVersioned) -> Self {
let RADRetrieveSerializationHelperVersioned(db_version, rad_retrieve) = x;
assert_eq!(
db_version,
RADRetrieveSerializationHelperVersioned::LATEST_VERSION
);
let RADRetrieveSerializationHelperBincode {
kind,
url,
script,
body,
headers,
} = rad_retrieve;
Self {
kind,
url,
script,
body,
headers,
}
}
}
impl From<RADRetrieveSerializationHelperBincode> for RADRetrieveSerializationHelperVersioned {
fn from(rad_retrieve: RADRetrieveSerializationHelperBincode) -> Self {
let db_version = Self::LATEST_VERSION;
Self(db_version, rad_retrieve)
}
}
impl From<RADRetrieve> for RADRetrieveSerializationHelperJson {
fn from(x: RADRetrieve) -> Self {
let RADRetrieve {
kind,
url,
script,
body,
headers,
} = x;
Self {
kind,
url,
script,
body,
headers,
}
}
}
impl From<RADRetrieveSerializationHelperJson> for RADRetrieve {
fn from(x: RADRetrieveSerializationHelperJson) -> Self {
let RADRetrieveSerializationHelperJson {
kind,
url,
script,
body,
headers,
} = x;
Self {
kind,
url,
script,
body,
headers,
}
}
}
struct RADRetrieveSerializationHelperVersionedVisitor;
impl<'de> Visitor<'de> for RADRetrieveSerializationHelperVersionedVisitor {
type Value = RADRetrieveSerializationHelperVersioned;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "struct RADRetrieveSerializationHelperVersioned")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
// Always deserialize as the latest version
let latest_version = Self::Value::LATEST_VERSION;
// Ensure backwards compatibility when using bincode.
// The first element of the old version of RADRetrieve was a RADType, which is a simple enum
// with 3 fields. This enum is serialized by bincode as a u32, with the possible values 0,
// 1, 2.
// The new helper type RADRetrieveSerializationHelperVersioned is serialized with a u32
// db_version field as the first field.
// Therefore, we can deserialize the first u32 value, and select the correct strategy
// depending on it. If the db_version is 0, 1, or 2, this is the old version RADRetrieve so
// we need to deserialize the two missing fields (url, script) next. Otherwise, this is the
// actual db_version value, so we can use it to select the correct helper.
// Currently there is only one helper: RADRetrieveSerializationHelperBincode, which uses
// db_version 3.
let db_version: u32 = seq
.next_element()?
.ok_or_else(|| de::Error::missing_field("db_version"))?;
match db_version {
0..=2 => {
let kind = match db_version {
0 => RADType::Unknown,
1 => RADType::HttpGet,
2 => RADType::Rng,
_ => unreachable!(),
};
// In the original definition, `RADRetrieve` was a 3-field struct, and in this
// implementation it is a 2-field struct. Since we have already deserialized the
// value of the first field, we treat the 2 remaining fields `url` and `script` as
// if it was one field with value `(url, script)`.
// Treating them as 2 separate fields does not work because `seq.next_element()`
// returns `None` for the second element, because
// `RADRetrieveSerializationHelperVersioned` does only have 2 fields.
// This could cause problems in some serialization formats where `(a, b, c)` is not
// equivalent to `(a, (b, c))`, but bincode seems to be happy, and we only care
// about being backwards compatible with bincode.
let (url, script): (String, Vec<u8>) = seq
.next_element()?
.ok_or_else(|| de::Error::missing_field("rad_retrieve"))?;
// The new fields `body` and `headers` which were missing in this version of
// `RADRetrieve` will have the default value
let rad_retrieve = RADRetrieveSerializationHelperBincode {
kind,
url,
script,
body: vec![],
headers: vec![],
};
Ok(RADRetrieveSerializationHelperVersioned(
latest_version,
rad_retrieve,
))
}
3 => {
// Version 3: deserialize as 2-field struct: `(db_version, rad_retrieve)`.
let rad_retrieve: RADRetrieveSerializationHelperBincode = seq
.next_element()?
.ok_or_else(|| de::Error::missing_field("rad_retrieve"))?;
Ok(RADRetrieveSerializationHelperVersioned(
latest_version,
rad_retrieve,
))
}
unknown_version => Err(de::Error::custom(format!(
"RADRetrieve: unknown db_version {}, expected one of 0, 1, 2, 3",
unknown_version
))),
}
}
}
impl<'de> Deserialize<'de> for RADRetrieveSerializationHelperVersioned {
fn deserialize<D>(deserializer: D) -> Result<RADRetrieveSerializationHelperVersioned, D::Error>
where
D: Deserializer<'de>,
{
// Deserialize using visitor. This is a 2-field tuple struct:
// (db_version: u32, rad_retrieve: RADRetrieve)
deserializer.deserialize_tuple_struct(
"RADRetrieveSerializationHelperVersioned",
2,
RADRetrieveSerializationHelperVersionedVisitor,
)
}
}
impl Serialize for RADRetrieve {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if serializer.is_human_readable() {
// Serialize skipping default fields to improve readability
RADRetrieveSerializationHelperJson::from(self.clone()).serialize(serializer)
} else {
// Serialize by prepending a `db_version: u32` field, to ensure easier migrations in the
// future. This always serializes in the format of the latest version.
RADRetrieveSerializationHelperVersioned::from(self.clone()).serialize(serializer)
}
}
}
impl<'de> Deserialize<'de> for RADRetrieve {
fn deserialize<D>(deserializer: D) -> Result<RADRetrieve, D::Error>
where
D: Deserializer<'de>,
{
if deserializer.is_human_readable() {
// Deserialize by assuming that missing fields have default value
RADRetrieveSerializationHelperJson::deserialize(deserializer).map(Into::into)
} else {
// Deserialize by reading the format version. Ensures backwards compatibility.
RADRetrieveSerializationHelperVersioned::deserialize(deserializer).map(Into::into)
}
}
}