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
use serde::{Deserialize, Serialize};
use std::{
collections::{
btree_map::{Entry, IntoIter, Iter, Values},
BTreeMap,
},
iter::FromIterator,
ops::Index,
};
use crate::{Address, BigNum, UnifiedNum};
pub type UnifiedMap = Map<Address, UnifiedNum>;
pub type BalancesMap = Map<Address, BigNum>;
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(transparent)]
pub struct Map<K: Ord, V>(BTreeMap<K, V>);
impl Map<Address, UnifiedNum> {
pub fn to_precision(&self, precision: u8) -> BalancesMap {
self.iter()
.map(|(address, unified_num)| (*address, unified_num.to_precision(precision)))
.collect()
}
}
impl<K: Ord, V> Default for Map<K, V> {
fn default() -> Self {
Map(BTreeMap::default())
}
}
impl<K: Ord, V> Index<&'_ K> for Map<K, V> {
type Output = V;
fn index(&self, index: &K) -> &Self::Output {
self.0.index(index)
}
}
impl<K: Ord, V> Map<K, V> {
pub fn iter(&self) -> Iter<'_, K, V> {
self.0.iter()
}
pub fn values(&self) -> Values<'_, K, V> {
self.0.values()
}
pub fn get(&self, key: &K) -> Option<&V> {
self.0.get(key)
}
pub fn contains_key(&self, key: &K) -> bool {
self.0.contains_key(key)
}
pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
self.0.entry(key)
}
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
self.0.insert(key, value)
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl<K: Ord, V> FromIterator<(K, V)> for Map<K, V> {
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
let btree_map: BTreeMap<K, V> = iter.into_iter().collect();
Map(btree_map)
}
}
impl<K: Ord, V> IntoIterator for Map<K, V> {
type Item = (K, V);
type IntoIter = IntoIter<K, V>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
#[cfg(test)]
mod test {
use serde_json::json;
use super::*;
use crate::test_util::{FOLLOWER, LEADER};
#[test]
fn test_unified_map_de_serialization() {
let unified_map: UnifiedMap = vec![
(*LEADER, UnifiedNum::from(50_u64)),
(*FOLLOWER, UnifiedNum::from(100_u64)),
]
.into_iter()
.collect();
let actual_json = serde_json::to_value(&unified_map).expect("Should serialize it");
let expected_json = json!({
"0xf3f583AEC5f7C030722Fe992A5688557e1B86ef7":"100",
"0x80690751969B234697e9059e04ed72195c3507fa":"50"
});
assert_eq!(expected_json, actual_json);
let balances_map_from_json: UnifiedMap =
serde_json::from_value(actual_json).expect("Should deserialize it");
assert_eq!(unified_map, balances_map_from_json);
}
#[test]
fn test_balances_map_de_serialization() {
let balances_map: BalancesMap = vec![
(*LEADER, BigNum::from(50_u64)),
(*FOLLOWER, BigNum::from(100_u64)),
]
.into_iter()
.collect();
let actual_json = serde_json::to_value(&balances_map).expect("Should serialize it");
let expected_json = json!({
"0xf3f583AEC5f7C030722Fe992A5688557e1B86ef7":"100",
"0x80690751969B234697e9059e04ed72195c3507fa":"50"
});
assert_eq!(expected_json, actual_json);
let balances_map_from_json: BalancesMap =
serde_json::from_value(actual_json).expect("Should deserialize it");
assert_eq!(balances_map, balances_map_from_json);
}
#[test]
fn test_balances_map_deserialization_with_same_keys() {
let json = json!({
"0x80690751969B234697e9059e04ed72195c3507fa":"100",
"0xf3f583AEC5f7C030722Fe992A5688557e1B86ef7":"20",
"0x80690751969B234697e9059e04ed72195c3507fa":"50"
});
let actual_deserialized: BalancesMap =
serde_json::from_value(json).expect("Should deserialize it");
let expected_deserialized: BalancesMap = vec![
(*LEADER, BigNum::from(50_u64)),
(*FOLLOWER, BigNum::from(20_u64)),
]
.into_iter()
.collect();
assert_eq!(expected_deserialized, actual_deserialized);
}
}