Skip to content

Commit

Permalink
feat: isolate test_movement_client_should_publish_package and clean u…
Browse files Browse the repository at this point in the history
…p test
  • Loading branch information
andygolay committed Aug 23, 2024
1 parent 04c0e03 commit 509e98f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 70 deletions.
73 changes: 45 additions & 28 deletions protocol-units/bridge/chains/movement/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::utils::MovementAddress;
use anyhow::{Error, Result};
use aptos_sdk::{
move_types::language_storage::TypeTag,
rest_client::{Client, FaucetClient, Transaction},
rest_client::{Client, FaucetClient},
types::LocalAccount,
};
use aptos_types::account_address::AccountAddress;
Expand All @@ -17,8 +17,9 @@ use bridge_shared::{
},
};
use rand::prelude::*;
use rand::Rng;
use serde::Serialize;
use std::{env, fs, io::{Read, Write}, path::PathBuf, process::{Command, Stdio}};
use std::{env, fs, io::{Read, Write}, path::{Path, PathBuf}, process::{Command, Stdio}};
use std::str::FromStr;
use std::{
sync::{mpsc, Arc, Mutex, RwLock},
Expand Down Expand Up @@ -195,7 +196,9 @@ impl MovementClient {
}

pub fn publish_for_test(&mut self) -> Result<()> {
//println!("Current directory: {:?}", env::current_dir());

let random_seed = rand::thread_rng().gen_range(0, 1000000).to_string();

let mut process = Command::new("movement")
.args(&["init"])
.stdin(Stdio::piped())
Expand All @@ -206,16 +209,16 @@ impl MovementClient {

let stdin: &mut std::process::ChildStdin = process.stdin.as_mut().expect("Failed to open stdin");

// Press enter for the first prompt
stdin.write_all(b"yes\n").expect("Failed to write to stdin");
let movement_dir = PathBuf::from(".movement");

if movement_dir.exists() {
stdin.write_all(b"yes\n").expect("Failed to write to stdin");
}

// Write "local" to the second prompt
stdin.write_all(b"local\n").expect("Failed to write to stdin");

// Press enter for the third prompt
stdin.write_all(b"\n").expect("Failed to write to stdin");

// Close stdin to indicate that no more input will be provided
drop(stdin);

let addr_output = process
Expand Down Expand Up @@ -245,7 +248,7 @@ impl MovementClient {
"--address",
address,
"--seed",
"256789",
&random_seed,
])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
Expand Down Expand Up @@ -294,24 +297,32 @@ impl MovementClient {
let updated_content = move_toml_content
.lines()
.map(|line| {
if line.starts_with("resource_addr = ") {
format!(r#"resource_addr = "{}""#, formatted_resource_address)
} else if line.starts_with("atomic_bridge = ") {
format!(r#"atomic_bridge = "{}""#, formatted_resource_address)
} else if line.starts_with("moveth = ") {
format!(r#"moveth = "{}""#, formatted_resource_address)
} else if line.starts_with("master_minter = ") {
format!(r#"master_minter = "{}""#, formatted_resource_address)
} else if line.starts_with("minter = ") {
format!(r#"minter = "{}""#, formatted_resource_address)
} else if line.starts_with("admin = ") {
format!(r#"admin = "{}""#, formatted_resource_address)
} else if line.starts_with("origin_addr = ") {
format!(r#"origin_addr = "{}""#, address)
} else if line.starts_with("source_account = ") {
format!(r#"source_account = "{}""#, address)
} else {
line.to_string()
match line {
_ if line.starts_with("resource_addr = ") => {
format!(r#"resource_addr = "{}""#, formatted_resource_address)
}
_ if line.starts_with("atomic_bridge = ") => {
format!(r#"atomic_bridge = "{}""#, formatted_resource_address)
}
_ if line.starts_with("moveth = ") => {
format!(r#"moveth = "{}""#, formatted_resource_address)
}
_ if line.starts_with("master_minter = ") => {
format!(r#"master_minter = "{}""#, formatted_resource_address)
}
_ if line.starts_with("minter = ") => {
format!(r#"minter = "{}""#, formatted_resource_address)
}
_ if line.starts_with("admin = ") => {
format!(r#"admin = "{}""#, formatted_resource_address)
}
_ if line.starts_with("origin_addr = ") => {
format!(r#"origin_addr = "{}""#, address)
}
_ if line.starts_with("source_account = ") => {
format!(r#"source_account = "{}""#, address)
}
_ => line.to_string(),
}
})
.collect::<Vec<_>>()
Expand All @@ -333,7 +344,7 @@ impl MovementClient {
"--address-name",
"moveth",
"--seed",
"256789",
&random_seed,
"--package-dir",
"../move-modules"
])
Expand All @@ -350,6 +361,12 @@ impl MovementClient {
eprintln!("stderr: {}", String::from_utf8_lossy(&output2.stderr));
}


if movement_dir.exists() {
fs::remove_dir_all(movement_dir).expect("Failed to delete .movement directory");
println!(".movement directory deleted successfully.");
}

Ok(())
}

Expand Down
67 changes: 33 additions & 34 deletions protocol-units/bridge/integration-tests/tests/eth_movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,29 @@ use alloy::{
use anyhow::Context;
use anyhow::Result;

use aptos_framework;
use aptos_sdk::{
coin_client::CoinClient,
rest_client::{Client, FaucetClient},
transaction_builder::aptos_stdlib,
types::LocalAccount,
};
use bridge_integration_tests::TestHarness;
use bridge_shared::{
bridge_contracts::{BridgeContractCounterparty, BridgeContractInitiator}, bridge_monitoring::BridgeContractInitiatorEvent, initiator_contract::SmartContractInitiatorEvent, types::{Amount, BridgeTransferId, HashLock, HashLockPreImage, InitiatorAddress, RecipientAddress, TimeLock}
bridge_contracts::{BridgeContractCounterparty, BridgeContractInitiator}, bridge_monitoring::BridgeContractInitiatorEvent, types::{Amount, BridgeTransferId, HashLock, HashLockPreImage, InitiatorAddress, RecipientAddress, TimeLock}
};

use ethereum_bridge::{
types::EthAddress,
event_types::EthChainEvent,
event_logging::EthInitiatorMonitoring
};

use movement_bridge::{utils::MovementAddress, MovementClient};
use rand::{rngs::StdRng, SeedableRng};
use movement_bridge::utils::MovementAddress;
use rand;
use tokio;
use futures::{channel::mpsc::{self, UnboundedReceiver}, Stream, StreamExt};
use futures::{channel::mpsc::{self, UnboundedReceiver}, StreamExt};

use aptos_language_e2e_tests::{
account::Account, common_transactions::peer_to_peer_txn, executor::FakeExecutor,
};
use aptos_logger::Logger;
use aptos_types::{
account_address::{create_resource_address, AccountAddress}, account_config::{DepositEvent, WithdrawEvent}, transaction::{ExecutionStatus, SignedTransaction, TransactionOutput, TransactionStatus}, PeerId
};
use bcs;
use std::{
convert::TryFrom,
process::{Command, Stdio},
str::FromStr,
time::Instant,
};
use aptos_types::account_address::AccountAddress;
use tracing;
use tracing_subscriber;

use url::Url;

#[tokio::test]
#[ignore]
async fn test_movement_client_build_and_fund_accounts() -> Result<(), anyhow::Error> {
Expand All @@ -57,7 +38,7 @@ async fn test_movement_client_build_and_fund_accounts() -> Result<(), anyhow::Er
let rest_client = movement_client.rest_client();
let coin_client = CoinClient::new(&rest_client);
let faucet_client = movement_client.faucet_client().expect("Failed to get // FaucetClient");
let mut alice = LocalAccount::generate(&mut rand::rngs::OsRng);
let alice = LocalAccount::generate(&mut rand::rngs::OsRng);
let bob = LocalAccount::generate(&mut rand::rngs::OsRng);

// Print account addresses.
Expand Down Expand Up @@ -98,18 +79,36 @@ async fn test_movement_client_build_and_fund_accounts() -> Result<(), anyhow::Er

#[tokio::test]
//#[ignore]
async fn test_movement_client_happy_path() -> Result<(), anyhow::Error> {
async fn test_movement_client_should_publish_package() -> Result<(), anyhow::Error> {

// at beginning of test, check whether .movement is present with a conditional statement so it works either way
// at end of test, delete .movement

let _ = tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.try_init();

let (mut harness, mut child) = TestHarness::new_with_movement().await;
{ let mut movement_client = harness.movement_client_mut().expect("Failed to get MovementClient");
{ let movement_client = harness.movement_client_mut().expect("Failed to get MovementClient");

let rest_client = movement_client.rest_client();
let coin_client = CoinClient::new(&rest_client);
let faucet_client = movement_client.faucet_client().expect("Failed to get FaucetClient");
let _ = movement_client.publish_for_test();
}

child.kill().await.context("Failed to kill the child process")?;

Ok(())
}

#[tokio::test]
#[ignore]
async fn test_movement_client_should_successfully_call_lock() -> Result<(), anyhow::Error> {

let _ = tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.try_init();

let (mut harness, mut child) = TestHarness::new_with_movement().await;
{ let movement_client = harness.movement_client_mut().expect("Failed to get MovementClient");

let _ = movement_client.publish_for_test();
}
Expand All @@ -122,16 +121,16 @@ async fn test_movement_client_happy_path() -> Result<(), anyhow::Error> {
let time_lock = 3600;
let amount = 100;

let txn = harness
harness
.movement_client_mut()
.expect("Failed to get MovmentClient")
.lock_bridge_transfer_assets(
BridgeTransferId(bridge_transfer_id),
HashLock(hash_lock),
TimeLock(100),
TimeLock(time_lock),
InitiatorAddress(initiator),
RecipientAddress(recipient),
Amount(1000), // Eth
Amount(amount), // Eth
)
.await
.expect("Failed to complete bridge transfer");
Expand Down Expand Up @@ -254,7 +253,7 @@ async fn test_eth_client_should_successfully_get_bridge_transfer_id() -> Result<
harness.deploy_init_contracts().await;

let rpc_url = "ws://localhost:8545";
let (event_sender, mut event_receiver): (mpsc::UnboundedSender<_>, UnboundedReceiver<_>) = mpsc::unbounded();
let (_event_sender, event_receiver): (mpsc::UnboundedSender<_>, UnboundedReceiver<_>) = mpsc::unbounded();

let mut monitoring = EthInitiatorMonitoring::run(rpc_url, event_receiver, signer_address).await?;

Expand Down
16 changes: 8 additions & 8 deletions protocol-units/bridge/move-modules/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ version = "1.0.0"
authors = []

[addresses]
resource_addr = "0x389497d8fd32545f3bb2882d0b043c7e036a68700ab3add8b56dad0f06d6d7cb"
origin_addr = "0x84f978fa58adab3c1fbd99dc187074438455dbe3c0a217e00dbdd15342cd3122"
atomic_bridge = "0x389497d8fd32545f3bb2882d0b043c7e036a68700ab3add8b56dad0f06d6d7cb"
moveth = "0x389497d8fd32545f3bb2882d0b043c7e036a68700ab3add8b56dad0f06d6d7cb"
master_minter = "0x389497d8fd32545f3bb2882d0b043c7e036a68700ab3add8b56dad0f06d6d7cb"
minter = "0x389497d8fd32545f3bb2882d0b043c7e036a68700ab3add8b56dad0f06d6d7cb"
admin = "0x389497d8fd32545f3bb2882d0b043c7e036a68700ab3add8b56dad0f06d6d7cb"
source_account = "0x84f978fa58adab3c1fbd99dc187074438455dbe3c0a217e00dbdd15342cd3122"
resource_addr = "0x92c5e407d5e13eb51ab95afd262e7d5223be49373489e88cc6dd4ff9d9a05779"
origin_addr = "0x7dc72e7296e4029142aba3e60612088d87573234828fa844c10ddeadb79f34fe"
atomic_bridge = "0x92c5e407d5e13eb51ab95afd262e7d5223be49373489e88cc6dd4ff9d9a05779"
moveth = "0x92c5e407d5e13eb51ab95afd262e7d5223be49373489e88cc6dd4ff9d9a05779"
master_minter = "0x92c5e407d5e13eb51ab95afd262e7d5223be49373489e88cc6dd4ff9d9a05779"
minter = "0x92c5e407d5e13eb51ab95afd262e7d5223be49373489e88cc6dd4ff9d9a05779"
admin = "0x92c5e407d5e13eb51ab95afd262e7d5223be49373489e88cc6dd4ff9d9a05779"
source_account = "0x7dc72e7296e4029142aba3e60612088d87573234828fa844c10ddeadb79f34fe"
pauser = "0xdafe"
denylister = "0xcade"

Expand Down

0 comments on commit 509e98f

Please sign in to comment.