@@ -15,14 +15,19 @@ use aptos_types::{
15
15
} ;
16
16
use bytes:: Bytes ;
17
17
use move_core_types:: { account_address:: AccountAddress , language_storage:: StructTag } ;
18
+ use std:: cmp:: Ordering ;
19
+ use std:: collections:: BTreeSet ;
20
+ use std:: fmt:: Display ;
21
+ use std:: fmt:: Formatter ;
18
22
use std:: str:: FromStr ;
19
23
use tracing:: { debug, info} ;
20
24
25
+ #[ derive( Debug ) ]
21
26
pub enum FailedComparison {
22
27
MissingStateValue ( StateKey ) ,
23
28
NotMissingStateValue ( StateKey ) ,
24
29
RawStateDiverge {
25
- movement_state_key : StateKey ,
30
+ state_key : StateKey ,
26
31
movement_value : Bytes ,
27
32
maptos_state_value : Bytes ,
28
33
} ,
@@ -38,66 +43,149 @@ pub enum FailedComparison {
38
43
} ,
39
44
}
40
45
41
- impl From < FailedComparison > for ValidationError {
42
- fn from ( fail : FailedComparison ) -> Self {
43
- match fail {
44
- FailedComparison :: MissingStateValue ( movement_state_key) => ValidationError :: Unsatisfied (
45
- format ! (
46
- "Movement Aptos is missing a value for {:?}" ,
47
- movement_state_key
48
- )
49
- . into ( ) ,
50
- ) ,
51
- FailedComparison :: NotMissingStateValue ( movement_state_key) => ValidationError :: Unsatisfied (
52
- format ! (
53
- "Movement Aptos is unexpectedly not missing a value for {:?}" ,
54
- movement_state_key
55
- )
56
- . into ( ) ,
57
- ) ,
58
- FailedComparison :: RawStateDiverge {
59
- movement_state_key,
60
- movement_value,
61
- maptos_state_value,
62
- } => ValidationError :: Unsatisfied (
63
- format ! (
46
+ impl PartialEq for FailedComparison {
47
+ fn eq ( & self , other : & Self ) -> bool {
48
+ match ( self , other) {
49
+ (
50
+ FailedComparison :: MissingStateValue ( state_key) ,
51
+ FailedComparison :: MissingStateValue ( other_state_key) ,
52
+ )
53
+ | (
54
+ FailedComparison :: NotMissingStateValue ( state_key) ,
55
+ FailedComparison :: NotMissingStateValue ( other_state_key) ,
56
+ )
57
+ | (
58
+ FailedComparison :: RawStateDiverge { state_key, .. } ,
59
+ FailedComparison :: RawStateDiverge {
60
+ state_key : other_state_key,
61
+ ..
62
+ } ,
63
+ ) => state_key == other_state_key,
64
+ (
65
+ FailedComparison :: AccountDiverge { address, .. } ,
66
+ FailedComparison :: AccountDiverge {
67
+ address : other_address,
68
+ ..
69
+ } ,
70
+ )
71
+ | (
72
+ FailedComparison :: BalanceDiverge { address, .. } ,
73
+ FailedComparison :: BalanceDiverge {
74
+ address : other_address,
75
+ ..
76
+ } ,
77
+ ) => address == other_address,
78
+ _ => false ,
79
+ }
80
+ }
81
+ }
82
+
83
+ impl Eq for FailedComparison { }
84
+
85
+ impl Ord for FailedComparison {
86
+ fn cmp ( & self , other : & Self ) -> Ordering {
87
+ match ( self , other) {
88
+ (
89
+ FailedComparison :: MissingStateValue ( state_key) ,
90
+ FailedComparison :: MissingStateValue ( other_state_key) ,
91
+ )
92
+ | (
93
+ FailedComparison :: NotMissingStateValue ( state_key) ,
94
+ FailedComparison :: NotMissingStateValue ( other_state_key) ,
95
+ )
96
+ | (
97
+ FailedComparison :: RawStateDiverge { state_key, .. } ,
98
+ FailedComparison :: RawStateDiverge {
99
+ state_key : other_state_key,
100
+ ..
101
+ } ,
102
+ ) => state_key. cmp ( other_state_key) ,
103
+ (
104
+ FailedComparison :: AccountDiverge { address, .. } ,
105
+ FailedComparison :: AccountDiverge {
106
+ address : other_address,
107
+ ..
108
+ } ,
109
+ )
110
+ | (
111
+ FailedComparison :: BalanceDiverge { address, .. } ,
112
+ FailedComparison :: BalanceDiverge {
113
+ address : other_address,
114
+ ..
115
+ } ,
116
+ ) => address. cmp ( other_address) ,
117
+ ( FailedComparison :: MissingStateValue ( _) , _) => Ordering :: Less ,
118
+ ( FailedComparison :: NotMissingStateValue ( _) , _) => Ordering :: Less ,
119
+ ( FailedComparison :: RawStateDiverge { .. } , _) => Ordering :: Less ,
120
+ ( FailedComparison :: AccountDiverge { .. } , _) => Ordering :: Less ,
121
+ ( FailedComparison :: BalanceDiverge { .. } , _) => Ordering :: Less ,
122
+ }
123
+ }
124
+ }
125
+
126
+ impl PartialOrd for FailedComparison {
127
+ fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
128
+ Some ( self . cmp ( other) )
129
+ }
130
+ }
131
+
132
+ impl Display for FailedComparison {
133
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
134
+ match self {
135
+ FailedComparison :: MissingStateValue ( movement_state_key) =>
136
+ write ! ( f,
137
+ "Movement Aptos is missing a value for {:?}" ,
138
+ movement_state_key
139
+ )
140
+ ,
141
+ FailedComparison :: NotMissingStateValue ( movement_state_key) =>
142
+ write ! ( f,
143
+ "Movement Aptos is unexpectedly not missing a value for {:?}" ,
144
+ movement_state_key
145
+ ) ,
146
+ FailedComparison :: RawStateDiverge {
147
+ state_key,
148
+ movement_value,
149
+ maptos_state_value,
150
+ } =>
151
+ write ! ( f,
64
152
"Movement state value for {:?} is {:?}, while Movement Aptos state value is {:?}" ,
65
- movement_state_key ,
153
+ state_key ,
66
154
movement_value,
67
155
maptos_state_value
68
- )
69
- . into ( ) ,
70
- ) ,
71
- FailedComparison :: AccountDiverge {
72
- address,
73
- movement_account,
74
- movement_aptos_account,
75
- } => ValidationError :: Unsatisfied (
76
- format ! (
156
+ ) ,
157
+ FailedComparison :: AccountDiverge {
158
+ address,
159
+ movement_account,
160
+ movement_aptos_account,
161
+ } =>
162
+ write ! ( f,
77
163
"Movement account for {:?} is {:?}, while Movement Aptos account is {:?}" ,
78
164
address. to_standard_string( ) ,
79
165
movement_account,
80
166
movement_aptos_account
81
- )
82
- . into ( ) ,
83
- ) ,
84
- FailedComparison :: BalanceDiverge {
85
- address,
86
- movement_balance,
87
- movement_aptos_balance,
88
- } => ValidationError :: Unsatisfied (
89
- format ! (
167
+ ) ,
168
+ FailedComparison :: BalanceDiverge {
169
+ address,
170
+ movement_balance,
171
+ movement_aptos_balance,
172
+ } =>
173
+ write ! ( f,
90
174
"Movement balance for 0x{} is {} coin(s), while Movement Aptos balance is {} coin(s)" ,
91
175
address. short_str_lossless( ) ,
92
176
movement_balance,
93
177
movement_aptos_balance
94
- )
95
- . into ( ) ,
96
- ) ,
178
+ ) ,
97
179
}
98
180
}
99
181
}
100
182
183
+ impl From < FailedComparison > for ValidationError {
184
+ fn from ( fail : FailedComparison ) -> Self {
185
+ ValidationError :: Unsatisfied ( fail. to_string ( ) . into ( ) )
186
+ }
187
+ }
188
+
101
189
/// This check iterates over all global state keys starting at ledger version 0.
102
190
/// For each state key it fetches the state view for the latest ledger version,
103
191
/// from the old Movment database and the new Aptos database. The state view bytes
@@ -114,7 +202,7 @@ impl GlobalStorageIncludes {
114
202
mvt_version : u64 ,
115
203
movement_aptos_storage : & MovementAptosStorage ,
116
204
aptos_version : u64 ,
117
- ) -> Result < Vec < FailedComparison > , ValidationError > {
205
+ ) -> Result < BTreeSet < FailedComparison > , ValidationError > {
118
206
info ! ( "checking global state keys and values" ) ;
119
207
debug ! ( "movement_ledger_version: {:?}" , mvt_version) ;
120
208
debug ! ( "aptos_ledger_version: {:?}" , aptos_version) ;
@@ -140,7 +228,7 @@ impl GlobalStorageIncludes {
140
228
let account = StructTag :: from_str ( "0x1::account::Account" ) . unwrap ( ) ;
141
229
let coin = StructTag :: from_str ( "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>" ) . unwrap ( ) ;
142
230
143
- let mut failed_list = vec ! [ ] ;
231
+ let mut failed_list = BTreeSet :: new ( ) ;
144
232
145
233
for movement_state_key in movement_global_state_keys {
146
234
debug ! (
@@ -164,7 +252,7 @@ impl GlobalStorageIncludes {
164
252
Some ( val) => val,
165
253
None => {
166
254
failed_list
167
- . push ( FailedComparison :: MissingStateValue ( movement_state_key) ) ;
255
+ . insert ( FailedComparison :: MissingStateValue ( movement_state_key) ) ;
168
256
break ;
169
257
} ,
170
258
} ;
@@ -177,7 +265,7 @@ impl GlobalStorageIncludes {
177
265
movement_value,
178
266
maptos_state_value,
179
267
) ? {
180
- failed_list. push ( fail) ;
268
+ failed_list. insert ( fail) ;
181
269
}
182
270
} ,
183
271
Path :: Resource ( tag) if tag == coin => {
@@ -186,7 +274,7 @@ impl GlobalStorageIncludes {
186
274
movement_value,
187
275
maptos_state_value,
188
276
) ? {
189
- failed_list. push ( fail) ;
277
+ failed_list. insert ( fail) ;
190
278
}
191
279
} ,
192
280
_ => {
@@ -195,7 +283,7 @@ impl GlobalStorageIncludes {
195
283
movement_value,
196
284
maptos_state_value,
197
285
) {
198
- failed_list. push ( fail) ;
286
+ failed_list. insert ( fail) ;
199
287
}
200
288
} ,
201
289
}
@@ -205,7 +293,7 @@ impl GlobalStorageIncludes {
205
293
movement_value,
206
294
maptos_state_value,
207
295
) {
208
- failed_list. push ( fail) ;
296
+ failed_list. insert ( fail) ;
209
297
} ;
210
298
}
211
299
} ,
@@ -218,7 +306,7 @@ impl GlobalStorageIncludes {
218
306
{
219
307
Some ( _) => {
220
308
failed_list
221
- . push ( FailedComparison :: NotMissingStateValue ( movement_state_key) ) ;
309
+ . insert ( FailedComparison :: NotMissingStateValue ( movement_state_key) ) ;
222
310
break ;
223
311
} ,
224
312
None => { } ,
@@ -251,22 +339,20 @@ impl GlobalStorageIncludes {
251
339
aptos_ledger_version,
252
340
) ?;
253
341
254
- if failed_list. len ( ) > 0 {
255
- let val = failed_list. swap_remove ( 0 ) ;
256
- Err ( val. into ( ) )
257
- } else {
258
- Ok ( ( ) )
342
+ match failed_list. pop_first ( ) {
343
+ None => Ok ( ( ) ) ,
344
+ Some ( val) => Err ( val. into ( ) ) ,
259
345
}
260
346
}
261
347
262
348
fn compare_raw_state (
263
- movement_state_key : StateKey ,
349
+ state_key : StateKey ,
264
350
movement_value : Bytes ,
265
351
maptos_state_value : Bytes ,
266
352
) -> Option < FailedComparison > {
267
353
if movement_value != maptos_state_value {
268
354
Some ( FailedComparison :: RawStateDiverge {
269
- movement_state_key ,
355
+ state_key ,
270
356
movement_value,
271
357
maptos_state_value,
272
358
} )
0 commit comments