Skip to content

Commit ed0ff3b

Browse files
authored
Merge pull request #105 from movementlabsxyz/l-monninger/accounts-citerion-redux
chore: second stage of balances criterion merge
2 parents b8b614a + 4bdc20b commit ed0ff3b

File tree

9 files changed

+262
-1
lines changed

9 files changed

+262
-1
lines changed

Cargo.lock

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,11 @@ mtma-node-test-global-storage-includes-criterion = { path = "checks/node/citeria
189189
mtma-node-test-global-storage-not-empty-criterion = { path = "checks/node/citeria/global-storage-not-empty" }
190190

191191

192-
### e2e
192+
### migrator
193193
mtma-migrator-test-types = { path = "checks/migrator/util/types" }
194194
mtma-migrator-test-accounts-equal-criterion = { path = "checks/migrator/citeria/accounts-equal" }
195+
mtma-migrator-test-balances-equal-criterion = { path = "checks/migrator/citeria/balances-equal" }
196+
195197
## util
196198
bcs-ext = { path = "util/bcs-ext" }
197199
movement-syncing = { path = "util/movement/syncing" }
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[package]
2+
name = "mtma-migrator-balances-equal"
3+
version = { workspace = true }
4+
edition = { workspace = true }
5+
license = { workspace = true }
6+
authors = { workspace = true }
7+
homepage = { workspace = true }
8+
publish = { workspace = true }
9+
rust-version = { workspace = true }
10+
11+
[dependencies]
12+
mtma-types = { workspace = true }
13+
mtma-migrator-types = { workspace = true }
14+
mtma-migrator-test-types = { workspace = true }
15+
anyhow = { workspace = true }
16+
mtma-node-replay-core = { workspace = true }
17+
mtma-node-test-global-storage-injective-criterion = { workspace = true }
18+
mtma-node-test-global-storage-includes-criterion = { workspace = true }
19+
mtma-node-test-global-storage-not-empty-criterion = { workspace = true }
20+
mtma-migrator-test-balances-equal-criterion = { workspace = true }
21+
mtma-node-preludes = { workspace = true }
22+
mtma-node-null-core = { workspace = true }
23+
tokio = { workspace = true }
24+
chrono = { workspace = true }
25+
rand = { workspace = true }
26+
kestrel = { workspace = true }
27+
mtma-node-test-types = { workspace = true }
28+
29+
[dev-dependencies]
30+
tracing-test = { workspace = true }
31+
tracing = { workspace = true }
32+
33+
[lints]
34+
workspace = true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Migration Executor Checks Sketchpad
2+
A sketchpad for playing around with checks on the testpad.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#[cfg(test)]
2+
pub mod test {
3+
4+
use anyhow::Context;
5+
use mtma_migrator_test_balances_equal_criterion::BalancesEqual;
6+
use mtma_migrator_test_types::check::checked_migration;
7+
use mtma_migrator_types::migrator::{movement_migrator::Overlays, MovementMigrator};
8+
use mtma_node_null_core::config::Config as MtmaNullConfig;
9+
use mtma_node_test_types::prelude::Prelude;
10+
use tracing::info;
11+
12+
#[tokio::test(flavor = "multi_thread")]
13+
#[tracing_test::traced_test]
14+
async fn test_balances_equal() -> Result<(), anyhow::Error> {
15+
// use a scope to ensure everything is dropped
16+
{
17+
// Form the migrator.
18+
let mut movement_migrator = MovementMigrator::try_temp()?;
19+
movement_migrator.set_overlays(Overlays::default());
20+
21+
// Start the migrator so that it's running in the background.
22+
// In the future, some migrators may be for already running nodes.
23+
let movement_migrator_for_task = movement_migrator.clone();
24+
let movement_migrator_task = kestrel::task(async move {
25+
movement_migrator_for_task.run().await?;
26+
Ok::<_, anyhow::Error>(())
27+
});
28+
29+
// wait for the rest client to be ready
30+
// once we have this, there should also be a config, so we can then kill off the migrator and proceed
31+
movement_migrator
32+
.wait_for_rest_client_ready(tokio::time::Duration::from_secs(600)) // we wait for up to ten minutes because the nix flake in .vendors/movementcan be a bit slow the first time
33+
.await
34+
.context(
35+
"failed to wait for movement migrator rest client while running accounts equal manual prelude",
36+
)?;
37+
38+
kestrel::end!(movement_migrator_task)?;
39+
40+
// Form the prelude.
41+
// todo: this needs to be updated to use the prelude generator
42+
let prelude = Prelude::new_empty();
43+
44+
// Form the migration.
45+
let migration_config = MtmaNullConfig::default();
46+
let migration = migration_config.build()?;
47+
48+
// Run the checked migration.
49+
let balances_equal = BalancesEqual::new();
50+
info!("Running migration");
51+
match checked_migration(
52+
&mut movement_migrator,
53+
&prelude,
54+
&migration,
55+
vec![balances_equal],
56+
)
57+
.await
58+
{
59+
Ok(()) => {}
60+
Err(e) => {
61+
info!("Migration failed: {:?}", e);
62+
return Err(anyhow::anyhow!("Migration failed: {:?}", e));
63+
}
64+
}
65+
info!("Migration succeeded");
66+
}
67+
68+
// exit the test is fine when you only have one test per crate because when cargo test is run across a workspace, it actually multi-processes the tests by crate
69+
std::process::exit(0);
70+
71+
Ok(())
72+
}
73+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod balances_equal;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "mtma-migrator-test-balances-equal-criterion"
3+
version = { workspace = true }
4+
edition = { workspace = true }
5+
license = { workspace = true }
6+
authors = { workspace = true }
7+
homepage = { workspace = true }
8+
publish = { workspace = true }
9+
rust-version = { workspace = true }
10+
11+
[dependencies]
12+
mtma-migrator-test-types = { workspace = true }
13+
bcs = { workspace = true }
14+
bcs-ext = { workspace = true }
15+
tokio = { workspace = true }
16+
anyhow = { workspace = true }
17+
tracing = { workspace = true }
18+
19+
[lints]
20+
workspace = true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Balances Equal
2+
Checks that the balances in native coin for an account are equal.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use anyhow::Context;
2+
use bcs_ext::{comparison::BcsEq, conversion::BcsInto};
3+
use mtma_migrator_test_types::criterion::{
4+
Criterion, CriterionError, Criterionish, MovementAptosMigrator, MovementMigrator,
5+
};
6+
use tracing::info;
7+
8+
pub struct BalancesEqual;
9+
10+
impl BalancesEqual {
11+
pub fn new() -> Self {
12+
Self {}
13+
}
14+
15+
pub fn criterion() -> Criterion<Self> {
16+
Criterion::new(Self {})
17+
}
18+
}
19+
20+
impl Criterionish for BalancesEqual {
21+
async fn satisfies(
22+
&self,
23+
movement_migrator: &MovementMigrator,
24+
movement_aptos_migrator: &MovementAptosMigrator,
25+
) -> Result<(), CriterionError> {
26+
let movement_rest_client = movement_migrator
27+
.wait_for_rest_client_ready(tokio::time::Duration::from_secs(30))
28+
.await
29+
.context(
30+
"failed to wait for movement migrator rest client while checking accounts equal",
31+
)
32+
.map_err(|e| CriterionError::Internal(e.into()))?;
33+
let movement_aptos_rest_client = movement_aptos_migrator
34+
.wait_for_rest_client_ready(tokio::time::Duration::from_secs(30))
35+
.await
36+
.context("failed to wait for movement aptos migrator rest client while checking accounts equal")
37+
.map_err(|e| CriterionError::Internal(e.into()))?;
38+
39+
let movement_node =
40+
movement_migrator.node().await.map_err(|e| CriterionError::Internal(e.into()))?;
41+
42+
info!("Iterating over movement node accounts");
43+
for account_address_res in movement_node
44+
.iter_account_addresses(0)
45+
.map_err(|e| CriterionError::Internal(e.into()))?
46+
{
47+
let account_address = match account_address_res
48+
.context("account address is none")
49+
.map_err(|e| CriterionError::Internal(e.into()))
50+
{
51+
Ok(account_address) => account_address,
52+
Err(e) => {
53+
info!("Transaction has no sender: {:?}", e);
54+
continue;
55+
}
56+
};
57+
58+
info!("Getting movement account balance");
59+
let movement_account_balance = movement_rest_client
60+
.get_account_balance(account_address)
61+
.await
62+
.map_err(|e| {
63+
CriterionError::Internal(format!("failed to get account: {:?}", e).into())
64+
})?
65+
.into_inner();
66+
67+
info!("Getting movement aptos address");
68+
let movement_aptos_account_address =
69+
account_address.bcs_into().map_err(|e| CriterionError::Internal(e.into()))?;
70+
71+
info!("Getting movement aptos account balance");
72+
let movement_aptos_account_balance = movement_aptos_rest_client
73+
.view_apt_account_balance(movement_aptos_account_address)
74+
.await
75+
.map_err(|e| {
76+
CriterionError::Internal(format!("Failed to get account: {:?}", e).into())
77+
})?
78+
.into_inner();
79+
80+
info!("Comparing balances");
81+
if u64::from(movement_account_balance.coin.value) != movement_aptos_account_balance {
82+
return Err(CriterionError::Unsatisfied(
83+
format!("movement and movement aptos account balances have different values: {:?} != {:?}", movement_account_balance, movement_aptos_account_balance).into(),
84+
));
85+
}
86+
87+
}
88+
89+
Ok(())
90+
}
91+
}

0 commit comments

Comments
 (0)