|
| 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