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
use uint::Uint;
#[derive(Debug, PartialEq, Deserialize, Clone)]
pub struct Linear {
pub base: usize,
pub word: usize,
}
#[derive(Debug, PartialEq, Deserialize, Clone)]
pub struct Modexp {
pub divisor: usize,
}
#[derive(Debug, PartialEq, Deserialize, Clone)]
pub struct AltBn128Pairing {
pub base: usize,
pub pair: usize,
}
#[derive(Debug, PartialEq, Deserialize, Clone)]
pub enum Pricing {
#[serde(rename="linear")]
Linear(Linear),
#[serde(rename="modexp")]
Modexp(Modexp),
#[serde(rename="alt_bn128_pairing")]
AltBn128Pairing(AltBn128Pairing),
}
#[derive(Debug, PartialEq, Deserialize, Clone)]
pub struct Builtin {
pub name: String,
pub pricing: Pricing,
pub activate_at: Option<Uint>,
}
#[cfg(test)]
mod tests {
use serde_json;
use spec::builtin::{Builtin, Pricing, Linear, Modexp};
use uint::Uint;
#[test]
fn builtin_deserialization() {
let s = r#"{
"name": "ecrecover",
"pricing": { "linear": { "base": 3000, "word": 0 } }
}"#;
let deserialized: Builtin = serde_json::from_str(s).unwrap();
assert_eq!(deserialized.name, "ecrecover");
assert_eq!(deserialized.pricing, Pricing::Linear(Linear { base: 3000, word: 0 }));
assert!(deserialized.activate_at.is_none());
}
#[test]
fn activate_at() {
let s = r#"{
"name": "late_start",
"activate_at": 100000,
"pricing": { "modexp": { "divisor": 5 } }
}"#;
let deserialized: Builtin = serde_json::from_str(s).unwrap();
assert_eq!(deserialized.name, "late_start");
assert_eq!(deserialized.pricing, Pricing::Modexp(Modexp { divisor: 5 }));
assert_eq!(deserialized.activate_at, Some(Uint(100000.into())));
}
}