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
//! ## A (de)serializer for RLP encoding in ETH
//! 
//! ### Not Supported Types 
//! 
//! - bool
//! - float numbers
//! - maps
//! - enum (only deserialize)
//! 
//! We do not support enum when deserializing because we lost some information (i.e. variant //! index) about the original value when serializing.
//! 
//! We have to choose this approach because there is no enums in Golang while ETH is written in go. Treating enums as a transparent layer can make our furture implementation compatiable with ETH.
//! 
//! ### Design principle
//! 
//! Accroding to the ETH Yellow Paper, all supported data structure can be represented with either recursive list of byte arrays ![](https://latex.codecogs.com/svg.latex?\mathbb{L}) or byte arrays ![](https://latex.codecogs.com/svg.latex?\mathbb{B}). So we can transform all Rust's compound types, for example, tuple, struct and list, into lists. And then encode them as exactly described in the paper
//! 
//! For example, the structure in `test::test_embeded_struct`, can be internally treated as the following form:
//! 
//! ```
//! [
//!     "This is a tooooooooooooo loooooooooooooooooooong tag", 
//!     [
//!         114514, 
//!         [191, -9810], 
//!         [
//!             [[], [[]], [[], [[]]]]
//!         ]
//!     ], 
//!     "哼.啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊"
//! ]
//! ```
//! 
//! ### ZST serialization
//! 
//! In Rust, we can represent 'empty' in many ways, for example:
//! 
//! ```
//! [], (), "", b"", struct Empty, Variant::Empty, None, PhantomData<T>
//! ```
//! 
//! In out implementation:
//! 
//! 1. `[]` and `()` are considered empty list, thus should be serialized into 0xc0
//! 2. All other ZSTs are considered empty, thus should be serialized into 0x80
//! 
//! To better understand ZSTs' behavior when serializing, try this code:
//! 
//! ```rust
//! #[test]
//! fn test_compound_zst() {
//!     #[derive(Serialize, Debug, PartialEq, Eq)]
//!     struct ZST;
//! 
//!     #[derive(Serialize, Debug, PartialEq, Eq)]
//!     enum Simple {
//!         Empty(ZST),
//!         #[allow(dead_code)]
//!         Int((u32, u64))
//!     }
//! 
//!     #[derive(Serialize, Debug, PartialEq, Eq)]
//!     struct ContainZST(Simple);
//! 
//!     #[derive(Serialize, Debug, PartialEq, Eq)]
//!     struct StructZST {
//!         zst: Simple
//!     }
//! 
//!     let zst = Simple::Empty(ZST);
//!     let zst_res = to_bytes(&zst).unwrap();
//!     assert_eq!(zst_res, [0x80]);
//! 
//!     let with_zst = ContainZST(Simple::Empty(ZST));
//!     let with_zst_res = to_bytes(&with_zst).unwrap();
//!     // the container is transparent because its a newtype
//!     assert_eq!(with_zst_res, [0x80]);
//!     
//!     let with_zst = StructZST { zst: Simple::Empty(ZST) };
//!     let with_zst_res = to_bytes(&with_zst).unwrap();
//!     // the container is a list, to this is equivlent to [""]
//!     assert_eq!(with_zst_res, [0xc1, 0x80]);
//!     }
//! ```
//! 
//! ### RLP Proxy 
//! 
//! We have a `RlpProxy` struct that implemented `Deserialize` trait, which just stores the original rlp encoded data after deserialization (no matter what type it is). You can gain more control over the deserialization process with it. Check out `de::RlpProxy` to find more about it.
//! 
//! ### (de)serializers for frequently used types
//! 
//! We provide two (de)serializers for frequently used types in blockchain.
//! 
//! - `biguint` for `num_bigint::BigUint`
//! - `byte_array` for `[u8; N]`
//! 
//! Put `#[serde(with = "biguint")]` or `#[serde(with = "byte_array")]` before your struct **field** to use them.


pub mod ser;
pub mod error;
pub mod rlp;
pub mod de;
pub mod types;

#[cfg(test)]
mod test {
    use num_bigint::BigUint;
    use serde::{Serialize, Deserialize};
    use serde_bytes::Bytes;
    use hex;

    use crate::de::RlpProxy;
    use crate::rlp::to_bytes;
    use crate::rlp::from_bytes;
    use crate::types::{biguint, byte_array};

    /// The transcation is the #0 transcation of 
    /// https://api.etherscan.io/api?module=proxy&action=eth_getBlockByNumber&tag=0xa1a489&boolean=true&apikey=YourApiKeyToken
    /// The encoded data is from README of 
    /// https://github.com/zhangchiqing/merkle-patricia-trie
    #[test]
    fn test_bn() {
        #[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
        struct LegacyTx {
            nonce: u64,
            #[serde(with = "biguint")]
            gas_price: BigUint,
            gas_limit: u64,
            #[serde(with = "byte_array")]
            to: [u8; 20],
            #[serde(with = "biguint")]
            value: BigUint,
            #[serde(with = "serde_bytes")]
            data: Vec<u8>,
            #[serde(with = "biguint")]
            v: BigUint,
            #[serde(with = "biguint")]
            r: BigUint,
            #[serde(with = "biguint")]
            s: BigUint
        }

        let mut to = [0; 20];
        to.copy_from_slice(&hex::decode("a3bed4e1c75d00fa6f4e5e6922db7261b5e9acd2").unwrap());

        let bn = |s| BigUint::from_bytes_be(&hex::decode(s).unwrap());
        
        let tx = LegacyTx {
            nonce: 0xa5,
            gas_price: bn("2e90edd000"),
            gas_limit: 0x12bc2,
            to,
            value: bn("00"),
            data: hex::decode("a9059cbb0000000000000000000000008bda8b9823b8490e8cf220dc7b91d97da1c54e250000000000000000000000000000000000000000000000056bc75e2d63100000").unwrap(),
            v: bn("26"),
            r: bn("6c89b57113cf7da8aed7911310e03d49be5e40de0bd73af4c9c54726c478691b"),
            s: bn("56223f039fab98d47c71f84190cf285ce8fc7d9181d6769387e5efd0a970e2e9")
        };

        let expected = "f8ab81a5852e90edd00083012bc294a3bed4e1c75d00fa6f4e5e6922db7261b5e9acd280b844a9059cbb0000000000000000000000008bda8b9823b8490e8cf220dc7b91d97da1c54e250000000000000000000000000000000000000000000000056bc75e2d6310000026a06c89b57113cf7da8aed7911310e03d49be5e40de0bd73af4c9c54726c478691ba056223f039fab98d47c71f84190cf285ce8fc7d9181d6769387e5efd0a970e2e9";

        let encoded = to_bytes(&tx).unwrap();
        let orig: LegacyTx = from_bytes(&encoded).unwrap();
        assert_eq!(orig, tx);
        assert_eq!(hex::encode(encoded), expected);
    }

    #[test]
    fn test_proxy() {
        #[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
        #[serde(from = "RlpProxy")]
        enum Classify {
            Zero(u8),
            One(u8),
            Ten((u8, u8))
        }
        
        impl From<RlpProxy> for Classify {
            fn from(proxy: RlpProxy) -> Self {
                let raw = proxy.raw();
                let mut tree = proxy.rlp_tree();
                if tree.value_count() == 2 {
                    return Classify::Ten(from_bytes(raw).unwrap())
                }

                let val = tree.next().unwrap()[0];
                match val {
                    0 => Classify::Zero(0),
                    1 => Classify::One(1),
                    _ => panic!("Value Error.")
                }
            }
        }

        let value = Classify::Ten((12, 34));
        let encoded = to_bytes(&value).unwrap();
        let origin = from_bytes(&encoded).unwrap();

        assert_eq!(value, origin);
    }

    #[test]
    fn test_long_string() {
        #[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
        struct LongStr<'a>(&'a str);

        let long_str = LongStr("Lorem ipsum dolor sit amet, consectetur adipisicing elit");
        let expected: Vec<u8> = [0xb8_u8, 0x38_u8]
            .into_iter()
            .chain(long_str.0.as_bytes().to_owned())
            .collect();
        
        let origin: LongStr = from_bytes(&expected).unwrap();
        assert_eq!(to_bytes(&long_str).unwrap(), expected);
        assert_eq!(long_str, origin);
    }

    #[test]
    fn test_set_theoretic_definition() {
        // [ [], [[]], [ [], [[]] ] ]
        #[derive(Serialize, Debug, PartialEq, Eq, Deserialize)]
        struct Three<T>(T);

        let three = Three(((), ((),), ((), ((),))));

        let three_expected = [0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0];
        let origin: Three<((), ((),), ((), ((),)))> = from_bytes(&three_expected).unwrap();
        assert_eq!(to_bytes(&three).unwrap(), three_expected);
        assert_eq!(origin, three)
    }

    #[test]
    fn test_1024() {
        #[derive(Serialize, Debug, PartialEq, Eq, Deserialize)]
        struct Int(u16);

        let simp_str = Int(1024);
        let simp_str_expected = [0x82, 0x04, 0x00];
        let origin: Int = from_bytes(&simp_str_expected).unwrap();

        assert_eq!(to_bytes(&simp_str).unwrap(), simp_str_expected);
        assert_eq!(simp_str, origin)
    }

    #[test]
    fn test_15() {
        #[derive(Serialize, Debug, PartialEq, Eq, Deserialize)]
        struct Int(u8);

        let simp_str = Int(15);
        let simp_str_expected = [0xf];
        let origin: Int = from_bytes(&simp_str_expected).unwrap();

        assert_eq!(to_bytes(&simp_str).unwrap(), simp_str_expected);
        assert_eq!(origin, simp_str)
    }

    #[test]
    fn test_zero() {
        #[derive(Serialize, Debug, PartialEq, Eq, Deserialize)]
        struct Int(u8);

        let simp_str = Int(0);
        let simp_str_expected = [0x80];
        let origin: Int = from_bytes(&simp_str_expected).unwrap();

        assert_eq!(to_bytes(&simp_str).unwrap(), simp_str_expected);
        assert_eq!(origin, simp_str)
    }

    #[test]
    fn test_empty() {
        let simp_str = Bytes::new(b"");
        let simp_str_expected = [0x80];
        let origin: &Bytes = from_bytes(&simp_str_expected).unwrap();

        assert_eq!(to_bytes(&simp_str).unwrap(), simp_str_expected);
        assert_eq!(origin, simp_str)
    }

    #[test]
    fn test_bytes() {
        let simp_str = Bytes::new(b"dog");
        let simp_str_expected = [0x83, b'd', b'o', b'g'];
        let origin: &Bytes = from_bytes(&simp_str_expected).unwrap();

        assert_eq!(to_bytes(&simp_str).unwrap(), simp_str_expected);
        assert_eq!(origin, simp_str)
    }

    #[test]
    fn test_list() {
        #[derive(Serialize, Debug, PartialEq, Eq, Deserialize)]
        struct SimpList {
            #[serde(with = "serde_bytes")]
            cat: Vec<u8>,
            #[serde(with = "serde_bytes")]
            dog: Vec<u8>
        }

        let simp_list = SimpList {
            cat: b"cat".to_vec(),
            dog: b"dog".to_vec(),
        };
        let simp_list_expected = [0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
        let origin: SimpList = from_bytes(&simp_list_expected).unwrap();

        assert_eq!(to_bytes(&simp_list).unwrap(), simp_list_expected);
        assert_eq!(origin, simp_list)
    }

    #[test]
    fn test_empty_list() {
        #[derive(Serialize, Debug, PartialEq, Eq, Deserialize)]
        struct SimpList {
        }

        let simp_list = SimpList {
        };

        let simp_list_expected = [0xc0];
        let origin: SimpList = from_bytes(&simp_list_expected).unwrap();

        assert_eq!(to_bytes(&simp_list).unwrap(), simp_list_expected);
        assert_eq!(origin, simp_list)
    }

    #[test]
    fn test_boxed_value() {
        #[derive(Serialize, Debug, PartialEq, Eq, Deserialize, Clone)]
        struct Boxed {
            a: Box<String>
        }

        let b = Boxed { a: Box::new("dog".into()) };
        let expected = [0xc4, 0x83, b'd', b'o', b'g'];
        let origin: Boxed = from_bytes(&expected).unwrap();

        assert_eq!(origin, b);
        assert_eq!(to_bytes(&b).unwrap(), expected);
    }

    #[test]
    fn test_simple_enum() {
        #[derive(Serialize, Debug, PartialEq, Eq)]
        enum Simple {
            #[allow(dead_code)]
            Empty,
            Int(u32)
        }

        #[derive(Serialize, Debug, PartialEq, Eq)]
        struct Equiv(u32);

        let simple_enum = Simple::Int(114514);
        let equiv = Equiv(114514);

        let enum_res = to_bytes(&simple_enum).unwrap();
        let struct_res = to_bytes(&equiv).unwrap();

        assert_eq!(enum_res, struct_res);
    }

    #[test]
    fn test_unit_variant() {
        #[derive(Serialize, Debug, PartialEq, Eq)]
        enum Simple {
            Empty,
            #[allow(dead_code)]
            Int(u32)
        }

        let unit_variant = Simple::Empty;
        let empty_res = to_bytes(&unit_variant).unwrap();

        assert_eq!(empty_res, [0x80]);
    }

    #[test]
    fn test_variant_tuple() {
        #[derive(Serialize, Debug, PartialEq, Eq)]
        enum Simple {
            #[allow(dead_code)]
            Empty,
            Int((u32, u64))
        }

        #[derive(Serialize, Debug, PartialEq, Eq)]
        struct Equiv((u32, u64));

        let simple_enum = Simple::Int((114514, 1919810));
        let equiv = Equiv((114514, 1919810));

        let enum_res = to_bytes(&simple_enum).unwrap();
        let struct_res = to_bytes(&equiv).unwrap();

        assert_eq!(enum_res, struct_res);
    }

    #[test]
    fn test_compound_zst() {
        #[derive(Serialize, Debug, PartialEq, Eq)]
        struct ZST;

        #[derive(Serialize, Debug, PartialEq, Eq)]
        enum Simple {
            Empty(ZST),
            #[allow(dead_code)]
            Int((u32, u64))
        }

        #[derive(Serialize, Debug, PartialEq, Eq)]
        struct ContainZST(Simple);

        #[derive(Serialize, Debug, PartialEq, Eq)]
        struct StructZST {
            zst: Simple
        }

        let zst = Simple::Empty(ZST);
        let zst_res = to_bytes(&zst).unwrap();
        assert_eq!(zst_res, [0x80]);

        let with_zst = ContainZST(Simple::Empty(ZST));
        let with_zst_res = to_bytes(&with_zst).unwrap();
        // the container is transparent because its a newtype
        assert_eq!(with_zst_res, [0x80]);
        
        let with_zst = StructZST { zst: Simple::Empty(ZST) };
        let with_zst_res = to_bytes(&with_zst).unwrap();
        // the container is a list, to this is equivlent to [""]
        assert_eq!(with_zst_res, [0xc1, 0x80]);
    }

    #[test]
    fn test_variant_struct() {
        #[derive(Serialize, Debug, PartialEq, Eq, Deserialize, Clone)]
        struct Third<T> {
            inner: T
        }
        #[derive(Serialize, Debug, PartialEq, Eq, Deserialize, Clone)]
        struct Embeding<'a> {
            tag: &'a str,
            ed: Embedded,
            #[serde(with = "serde_bytes")]
            bytes: Vec<u8>
        }
        #[derive(Serialize, Debug, PartialEq, Eq, Deserialize, Clone)]
        struct Embedded {
            time: u64,
            out: (u8, u32),
            three: Third<((), ((),), ((), ((),)))>
        }

        #[derive(Serialize, Debug, PartialEq, Eq)]
        enum Simple<'a> {
            #[allow(dead_code)]
            Empty,
            #[allow(dead_code)]
            Int((u32, u64, Embedded)),
            Struct(Embeding<'a>)
        }

        #[derive(Serialize, Debug, PartialEq, Eq)]
        struct Equiv((u32, u64));

        let embed = Embeding {
            tag: "This is a tooooooooooooo loooooooooooooooooooong tag",
            ed: Embedded {
                time: 114514,
                out: (191, 9810),
                three: Third {
                    inner: ((), ((),), ((), ((),)))
                }
            },
            bytes: "哼.啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊".as_bytes().to_vec()
        };

        let simple_enum = Simple::Struct(embed.clone());

        let simple_res = to_bytes(&simple_enum).unwrap();
        let struct_res = to_bytes(&embed).unwrap();

        assert_eq!(simple_res, struct_res);
    }

    #[test]
    fn test_zst_struct() {
        #[derive(Serialize, Debug, PartialEq, Eq, Deserialize)]
        struct ZST;

        #[derive(Serialize, Debug, PartialEq, Eq, Deserialize)]
        struct WithZST {
            f1: u8,
            f2: ZST,
            f3: (),
            f4: u8,
        }

        let zst = WithZST {
            f1: 1,
            f2: ZST,
            f3: (),
            f4: 4
        };

        let encoded = to_bytes(&zst).unwrap();
        let origin: WithZST = from_bytes(&encoded).unwrap();
        let expected = [0xc4, 0x1, 0x80, 0xc0, 0x4];
        
        assert_eq!(origin, zst);
        assert_eq!(encoded, expected);
    }

    #[test]
    fn test_embedded_struct() {
        #[derive(Serialize, Debug, PartialEq, Eq, Deserialize)]
        struct Third<T> {
            inner: T
        }
        #[derive(Serialize, Debug, PartialEq, Eq, Deserialize)]
        struct Embeding<'a> {
            tag: &'a str,
            ed: Embedded,
            #[serde(with = "serde_bytes")]
            bytes: Vec<u8>
        }
        #[derive(Serialize, Debug, PartialEq, Eq, Deserialize)]
        struct Embedded {
            time: u64,
            out: (u8, u32),
            three: Third<((), ((),), ((), ((),)))>
        }

        let embed = Embeding {
            tag: "This is a tooooooooooooo loooooooooooooooooooong tag",
            ed: Embedded {
                time: 114514,
                out: (191, 9810),
                three: Third {
                    inner: ((), ((),), ((), ((),)))
                }
            },
            bytes: "哼.啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊".as_bytes().to_vec()
        };

        let encode = to_bytes(&embed).unwrap();
        let origin: Embeding = from_bytes(&encode).unwrap();
        assert_eq!(embed, origin);
    }

}