Skip to content

Commit 20632ef

Browse files
committed
return a BtreeSet to do set diff
1 parent c122fdf commit 20632ef

File tree

1 file changed

+147
-61
lines changed

1 file changed

+147
-61
lines changed

movement-migration/validation-tool/src/checks/node/global_storage_includes.rs

Lines changed: 147 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@ use aptos_types::{
1515
};
1616
use bytes::Bytes;
1717
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;
1822
use std::str::FromStr;
1923
use tracing::{debug, info};
2024

25+
#[derive(Debug)]
2126
pub enum FailedComparison {
2227
MissingStateValue(StateKey),
2328
NotMissingStateValue(StateKey),
2429
RawStateDiverge {
25-
movement_state_key: StateKey,
30+
state_key: StateKey,
2631
movement_value: Bytes,
2732
maptos_state_value: Bytes,
2833
},
@@ -38,66 +43,149 @@ pub enum FailedComparison {
3843
},
3944
}
4045

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,
64152
"Movement state value for {:?} is {:?}, while Movement Aptos state value is {:?}",
65-
movement_state_key,
153+
state_key,
66154
movement_value,
67155
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,
77163
"Movement account for {:?} is {:?}, while Movement Aptos account is {:?}",
78164
address.to_standard_string(),
79165
movement_account,
80166
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,
90174
"Movement balance for 0x{} is {} coin(s), while Movement Aptos balance is {} coin(s)",
91175
address.short_str_lossless(),
92176
movement_balance,
93177
movement_aptos_balance
94-
)
95-
.into(),
96-
),
178+
),
97179
}
98180
}
99181
}
100182

183+
impl From<FailedComparison> for ValidationError {
184+
fn from(fail: FailedComparison) -> Self {
185+
ValidationError::Unsatisfied(fail.to_string().into())
186+
}
187+
}
188+
101189
/// This check iterates over all global state keys starting at ledger version 0.
102190
/// For each state key it fetches the state view for the latest ledger version,
103191
/// from the old Movment database and the new Aptos database. The state view bytes
@@ -114,7 +202,7 @@ impl GlobalStorageIncludes {
114202
mvt_version: u64,
115203
movement_aptos_storage: &MovementAptosStorage,
116204
aptos_version: u64,
117-
) -> Result<Vec<FailedComparison>, ValidationError> {
205+
) -> Result<BTreeSet<FailedComparison>, ValidationError> {
118206
info!("checking global state keys and values");
119207
debug!("movement_ledger_version: {:?}", mvt_version);
120208
debug!("aptos_ledger_version: {:?}", aptos_version);
@@ -140,7 +228,7 @@ impl GlobalStorageIncludes {
140228
let account = StructTag::from_str("0x1::account::Account").unwrap();
141229
let coin = StructTag::from_str("0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>").unwrap();
142230

143-
let mut failed_list = vec![];
231+
let mut failed_list = BTreeSet::new();
144232

145233
for movement_state_key in movement_global_state_keys {
146234
debug!(
@@ -164,7 +252,7 @@ impl GlobalStorageIncludes {
164252
Some(val) => val,
165253
None => {
166254
failed_list
167-
.push(FailedComparison::MissingStateValue(movement_state_key));
255+
.insert(FailedComparison::MissingStateValue(movement_state_key));
168256
break;
169257
},
170258
};
@@ -177,7 +265,7 @@ impl GlobalStorageIncludes {
177265
movement_value,
178266
maptos_state_value,
179267
)? {
180-
failed_list.push(fail);
268+
failed_list.insert(fail);
181269
}
182270
},
183271
Path::Resource(tag) if tag == coin => {
@@ -186,7 +274,7 @@ impl GlobalStorageIncludes {
186274
movement_value,
187275
maptos_state_value,
188276
)? {
189-
failed_list.push(fail);
277+
failed_list.insert(fail);
190278
}
191279
},
192280
_ => {
@@ -195,7 +283,7 @@ impl GlobalStorageIncludes {
195283
movement_value,
196284
maptos_state_value,
197285
) {
198-
failed_list.push(fail);
286+
failed_list.insert(fail);
199287
}
200288
},
201289
}
@@ -205,7 +293,7 @@ impl GlobalStorageIncludes {
205293
movement_value,
206294
maptos_state_value,
207295
) {
208-
failed_list.push(fail);
296+
failed_list.insert(fail);
209297
};
210298
}
211299
},
@@ -218,7 +306,7 @@ impl GlobalStorageIncludes {
218306
{
219307
Some(_) => {
220308
failed_list
221-
.push(FailedComparison::NotMissingStateValue(movement_state_key));
309+
.insert(FailedComparison::NotMissingStateValue(movement_state_key));
222310
break;
223311
},
224312
None => {},
@@ -251,22 +339,20 @@ impl GlobalStorageIncludes {
251339
aptos_ledger_version,
252340
)?;
253341

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()),
259345
}
260346
}
261347

262348
fn compare_raw_state(
263-
movement_state_key: StateKey,
349+
state_key: StateKey,
264350
movement_value: Bytes,
265351
maptos_state_value: Bytes,
266352
) -> Option<FailedComparison> {
267353
if movement_value != maptos_state_value {
268354
Some(FailedComparison::RawStateDiverge {
269-
movement_state_key,
355+
state_key,
270356
movement_value,
271357
maptos_state_value,
272358
})

0 commit comments

Comments
 (0)