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
use crate::{bytes::Bytes, hash::{H64, H256, H520}, uint::Uint};
use serde::Deserialize;
#[derive(Debug, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "camelCase")]
pub struct Vapory {
pub nonce: H64,
pub mix_hash: H256,
}
#[derive(Debug, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuthorityRoundSeal {
pub step: Uint,
pub signature: H520,
}
#[derive(Debug, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct TendermintSeal {
pub round: Uint,
pub proposal: H520,
pub precommits: Vec<H520>,
}
#[derive(Debug, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "camelCase")]
pub enum Seal {
Vapory(Vapory),
AuthorityRound(AuthorityRoundSeal),
Tendermint(TendermintSeal),
Generic(Bytes),
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use super::{AuthorityRoundSeal, Bytes, Vapory, H64, H256, H520, TendermintSeal, Seal, Uint};
use vapory_types::{U256, H64 as Vap64, H256 as Vap256, H520 as Vap520};
#[test]
fn seal_deserialization() {
let s = r#"[{
"vapory": {
"nonce": "0x0000000000000042",
"mixHash": "0x1000000000000000000000000000000000000000000000000000000000000001"
}
},{
"generic": "0xe011bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa"
},{
"authorityRound": {
"step": "0x0",
"signature": "0x2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002"
}
},{
"tendermint": {
"round": "0x3",
"proposal": "0x3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003",
"precommits": [
"0x4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004"
]
}
}]"#;
let deserialized: Vec<Seal> = serde_json::from_str(s).unwrap();
assert_eq!(deserialized.len(), 4);
assert_eq!(deserialized[0], Seal::Vapory(Vapory {
nonce: H64(Vap64::from_str("0000000000000042").unwrap()),
mix_hash: H256(Vap256::from_str("1000000000000000000000000000000000000000000000000000000000000001").unwrap())
}));
assert_eq!(deserialized[1], Seal::Generic(Bytes::new(vec![
0xe0, 0x11, 0xbb, 0xe8, 0xdb, 0x4e, 0x34, 0x7b, 0x4e, 0x8c, 0x93, 0x7c, 0x1c, 0x83, 0x70, 0xe4,
0xb5, 0xed, 0x33, 0xad, 0xb3, 0xdb, 0x69, 0xcb, 0xdb, 0x7a, 0x38, 0xe1, 0xe5, 0x0b, 0x1b, 0x82, 0xfa])));
assert_eq!(deserialized[2], Seal::AuthorityRound(AuthorityRoundSeal {
step: Uint(U256::from(0x0)),
signature: H520(Vap520::from_str("2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002").unwrap())
}));
assert_eq!(deserialized[3], Seal::Tendermint(TendermintSeal {
round: Uint(U256::from(0x3)),
proposal: H520(Vap520::from_str("3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003").unwrap()),
precommits: vec![H520(Vap520::from_str("4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004").unwrap())]
}));
}
}