1
- use std:: collections:: BTreeMap ;
1
+ use serde:: { Deserialize , Serialize } ;
2
+ use std:: {
3
+ collections:: {
4
+ btree_map:: { Entry , IntoIter , Iter , Values } ,
5
+ BTreeMap ,
6
+ } ,
7
+ iter:: FromIterator ,
8
+ ops:: Index ,
9
+ } ;
2
10
3
- use crate :: { BigNum , ValidatorId } ;
4
- use std:: collections:: btree_map:: { Entry , IntoIter , Iter , Values } ;
11
+ use crate :: { Address , BigNum , UnifiedNum } ;
5
12
6
- use serde:: { Deserialize , Serialize } ;
7
- use std:: iter:: FromIterator ;
8
- use std:: ops:: Index ;
13
+ pub type UnifiedMap = Map < Address , UnifiedNum > ;
14
+ pub type BalancesMap = Map < Address , BigNum > ;
9
15
10
- #[ derive( Clone , Debug , Default , Serialize , Deserialize , PartialEq , Eq ) ]
16
+ #[ derive( Clone , Debug , Serialize , Deserialize , PartialEq , Eq ) ]
11
17
#[ serde( transparent) ]
12
- pub struct BalancesMap ( BTreeMap < ValidatorId , BigNum > ) ;
18
+ pub struct Map < K : Ord , V > ( BTreeMap < K , V > ) ;
19
+
20
+ impl < K : Ord , V > Default for Map < K , V > {
21
+ fn default ( ) -> Self {
22
+ Map ( BTreeMap :: default ( ) )
23
+ }
24
+ }
13
25
14
- impl Index < & ' _ ValidatorId > for BalancesMap {
15
- type Output = BigNum ;
26
+ impl < K : Ord , V > Index < & ' _ K > for Map < K , V > {
27
+ type Output = V ;
16
28
17
- fn index ( & self , index : & ValidatorId ) -> & Self :: Output {
29
+ fn index ( & self , index : & K ) -> & Self :: Output {
18
30
self . 0 . index ( index)
19
31
}
20
32
}
21
33
22
- impl BalancesMap {
23
- pub fn iter ( & self ) -> Iter < ' _ , ValidatorId , BigNum > {
34
+ impl < K : Ord , V > Map < K , V > {
35
+ pub fn iter ( & self ) -> Iter < ' _ , K , V > {
24
36
self . 0 . iter ( )
25
37
}
26
38
27
- pub fn values ( & self ) -> Values < ' _ , ValidatorId , BigNum > {
39
+ pub fn values ( & self ) -> Values < ' _ , K , V > {
28
40
self . 0 . values ( )
29
41
}
30
42
31
- pub fn get ( & self , key : & ValidatorId ) -> Option < & BigNum > {
43
+ pub fn get ( & self , key : & K ) -> Option < & V > {
32
44
self . 0 . get ( key)
33
45
}
34
46
35
- pub fn contains_key ( & self , key : & ValidatorId ) -> bool {
47
+ pub fn contains_key ( & self , key : & K ) -> bool {
36
48
self . 0 . contains_key ( key)
37
49
}
38
50
39
- pub fn entry ( & mut self , key : ValidatorId ) -> Entry < ' _ , ValidatorId , BigNum > {
51
+ pub fn entry ( & mut self , key : K ) -> Entry < ' _ , K , V > {
40
52
self . 0 . entry ( key)
41
53
}
42
54
43
- pub fn insert ( & mut self , key : ValidatorId , value : BigNum ) -> Option < BigNum > {
55
+ pub fn insert ( & mut self , key : K , value : V ) -> Option < V > {
44
56
self . 0 . insert ( key, value)
45
57
}
46
58
@@ -53,18 +65,18 @@ impl BalancesMap {
53
65
}
54
66
}
55
67
56
- impl FromIterator < ( ValidatorId , BigNum ) > for BalancesMap {
57
- fn from_iter < I : IntoIterator < Item = ( ValidatorId , BigNum ) > > ( iter : I ) -> Self {
68
+ impl < K : Ord , V > FromIterator < ( K , V ) > for Map < K , V > {
69
+ fn from_iter < I : IntoIterator < Item = ( K , V ) > > ( iter : I ) -> Self {
58
70
// @TODO: Is there better way to do this?
59
- let btree_map: BTreeMap < ValidatorId , BigNum > = iter. into_iter ( ) . collect ( ) ;
71
+ let btree_map: BTreeMap < K , V > = iter. into_iter ( ) . collect ( ) ;
60
72
61
- BalancesMap ( btree_map)
73
+ Map ( btree_map)
62
74
}
63
75
}
64
76
65
- impl IntoIterator for BalancesMap {
66
- type Item = ( ValidatorId , BigNum ) ;
67
- type IntoIter = IntoIter < ValidatorId , BigNum > ;
77
+ impl < K : Ord , V > IntoIterator for Map < K , V > {
78
+ type Item = ( K , V ) ;
79
+ type IntoIter = IntoIter < K , V > ;
68
80
69
81
fn into_iter ( self ) -> Self :: IntoIter {
70
82
self . 0 . into_iter ( )
@@ -73,42 +85,73 @@ impl IntoIterator for BalancesMap {
73
85
74
86
#[ cfg( test) ]
75
87
mod test {
88
+ use serde_json:: json;
89
+
76
90
use super :: * ;
77
- use crate :: util:: tests:: prep_db:: IDS ;
78
- use crate :: BigNum ;
91
+ use crate :: util:: tests:: prep_db:: ADDRESSES ;
79
92
80
93
#[ test]
81
- fn test_balances_map_serialization ( ) {
82
- let data = vec ! [
83
- ( IDS [ "leader" ] . clone( ) , BigNum :: from( 50_u64 ) ) ,
84
- ( IDS [ "follower" ] . clone( ) , BigNum :: from( 100_u64 ) ) ,
85
- ] ;
94
+ fn test_unified_map_de_serialization ( ) {
95
+ let unified_map: UnifiedMap = vec ! [
96
+ ( ADDRESSES [ "leader" ] . clone( ) , UnifiedNum :: from( 50_u64 ) ) ,
97
+ ( ADDRESSES [ "follower" ] . clone( ) , UnifiedNum :: from( 100_u64 ) ) ,
98
+ ]
99
+ . into_iter ( )
100
+ . collect ( ) ;
101
+
102
+ let actual_json = serde_json:: to_value ( & unified_map) . expect ( "Should serialize it" ) ;
103
+ let expected_json = json ! ( {
104
+ "0xC91763D7F14ac5c5dDfBCD012e0D2A61ab9bDED3" : 100 ,
105
+ "0xce07CbB7e054514D590a0262C93070D838bFBA2e" : 50
106
+ } ) ;
86
107
87
- let balances_map: BalancesMap = data. into_iter ( ) . collect ( ) ;
108
+ assert_eq ! ( expected_json, actual_json) ;
109
+
110
+ let balances_map_from_json: UnifiedMap =
111
+ serde_json:: from_value ( actual_json) . expect ( "Should deserialize it" ) ;
112
+
113
+ assert_eq ! ( unified_map, balances_map_from_json) ;
114
+ }
115
+
116
+ #[ test]
117
+ fn test_balances_map_de_serialization ( ) {
118
+ let balances_map: BalancesMap = vec ! [
119
+ ( ADDRESSES [ "leader" ] . clone( ) , BigNum :: from( 50_u64 ) ) ,
120
+ ( ADDRESSES [ "follower" ] . clone( ) , BigNum :: from( 100_u64 ) ) ,
121
+ ]
122
+ . into_iter ( )
123
+ . collect ( ) ;
88
124
89
- let actual_json = serde_json:: to_string ( & balances_map) . expect ( "Should serialize it" ) ;
90
- let expected_json = r#"{"0xC91763D7F14ac5c5dDfBCD012e0D2A61ab9bDED3":"100","0xce07CbB7e054514D590a0262C93070D838bFBA2e":"50"}"# ;
125
+ let actual_json = serde_json:: to_value ( & balances_map) . expect ( "Should serialize it" ) ;
126
+ let expected_json = json ! ( {
127
+ "0xC91763D7F14ac5c5dDfBCD012e0D2A61ab9bDED3" : "100" ,
128
+ "0xce07CbB7e054514D590a0262C93070D838bFBA2e" : "50"
129
+ } ) ;
91
130
92
131
assert_eq ! ( expected_json, actual_json) ;
93
132
94
133
let balances_map_from_json: BalancesMap =
95
- serde_json:: from_str ( & actual_json) . expect ( "Should deserialize it" ) ;
134
+ serde_json:: from_value ( actual_json) . expect ( "Should deserialize it" ) ;
96
135
97
136
assert_eq ! ( balances_map, balances_map_from_json) ;
98
137
}
99
138
100
139
#[ test]
101
140
fn test_balances_map_deserialization_with_same_keys ( ) {
102
141
// the first is ETH Checksummed, the second is lowercase!
103
- let json = r#"{"0xC91763D7F14ac5c5dDfBCD012e0D2A61ab9bDED3":"100","0xc91763d7f14ac5c5ddfbcd012e0d2a61ab9bded3":"20","0xce07CbB7e054514D590a0262C93070D838bFBA2e":"50"}"# ;
142
+ let json = json ! ( {
143
+ "0xC91763D7F14ac5c5dDfBCD012e0D2A61ab9bDED3" : "100" ,
144
+ "0xc91763d7f14ac5c5ddfbcd012e0d2a61ab9bded3" : "20" ,
145
+ "0xce07CbB7e054514D590a0262C93070D838bFBA2e" : "50"
146
+ } ) ;
104
147
105
148
let actual_deserialized: BalancesMap =
106
- serde_json:: from_str ( & json) . expect ( "Should deserialize it" ) ;
149
+ serde_json:: from_value ( json) . expect ( "Should deserialize it" ) ;
107
150
108
151
let expected_deserialized: BalancesMap = vec ! [
109
- ( IDS [ "leader" ] . clone( ) , BigNum :: from( 50_u64 ) ) ,
152
+ ( ADDRESSES [ "leader" ] . clone( ) , BigNum :: from( 50_u64 ) ) ,
110
153
// only the second should be accepted, as it appears second in the string and it's the latest one
111
- ( IDS [ "follower" ] . clone( ) , BigNum :: from( 20_u64 ) ) ,
154
+ ( ADDRESSES [ "follower" ] . clone( ) , BigNum :: from( 20_u64 ) ) ,
112
155
]
113
156
. into_iter ( )
114
157
. collect ( ) ;
0 commit comments