-
Notifications
You must be signed in to change notification settings - Fork 3
/
mod.rs
246 lines (208 loc) · 7.31 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
pub mod constants;
mod erc20_bridge;
mod eth_bridge;
use std::future::Future;
use std::process::Command;
use std::time::Duration;
use std::{env, fs};
use rstest::rstest;
use url::Url;
use crate::contract_clients::config::Clients;
use crate::tests::erc20_bridge::erc20_bridge_test_helper;
use crate::tests::eth_bridge::eth_bridge_test_helper;
use crate::{bootstrap, setup_core_contract, setup_l2, BootstrapperOutput, ConfigFile};
async fn test_setup(args: &ConfigFile, clients: &Clients) -> BootstrapperOutput {
// Setup L1 (core contract)
let core_contract_client = setup_core_contract(args, clients).await;
let core_contract_address = core_contract_client.core_contract_client.address();
let core_contract_implementation_address = core_contract_client.core_contract_client.implementation_address();
// Create a new config with the core contract addresses
let mut config = get_test_config_file();
config.core_contract_address = Some(format!("{:?}", core_contract_address));
config.core_contract_implementation_address = Some(format!("{:?}", core_contract_implementation_address));
ensure_toolchain().expect("Not able to ensure toolchain exists.");
wait_for_madara().await.expect("Failed to start madara!");
// Setup L2 with the updated config
let l2_output = setup_l2(&mut config, clients).await;
BootstrapperOutput {
starknet_contract_address: Some(core_contract_address),
starknet_contract_implementation_address: Some(core_contract_implementation_address),
..l2_output
}
}
#[rstest]
#[tokio::test]
#[ignore = "ignored because we have a e2e test, and this is for a local test"]
async fn deploy_bridge() -> Result<(), anyhow::Error> {
env_logger::init();
let mut config = get_test_config_file();
let clients = Clients::init_from_config(&config).await;
bootstrap(&mut config, &clients).await;
Ok(())
}
#[rstest]
#[tokio::test]
#[ignore = "ignored because we have a e2e test, and this is for a local test"]
async fn deposit_and_withdraw_eth_bridge() -> Result<(), anyhow::Error> {
env_logger::init();
let mut config = get_test_config_file();
let clients = Clients::init_from_config(&config).await;
let out = bootstrap(&mut config, &clients).await;
let eth_bridge_setup = out.eth_bridge_setup_outputs.unwrap();
let _ = eth_bridge_test_helper(
&clients,
&config,
eth_bridge_setup.l2_eth_proxy_address,
eth_bridge_setup.l2_eth_bridge_proxy_address,
eth_bridge_setup.l1_bridge,
)
.await;
Ok(())
}
#[rstest]
#[tokio::test]
#[ignore = "ignored because we have a e2e test, and this is for a local test"]
async fn deposit_and_withdraw_erc20_bridge() -> Result<(), anyhow::Error> {
env_logger::init();
let mut config = get_test_config_file();
let clients = Clients::init_from_config(&config).await;
let out = bootstrap(&mut config, &clients).await;
let eth_token_setup = out.erc20_bridge_setup_outputs.unwrap();
let _ = erc20_bridge_test_helper(
&clients,
&config,
eth_token_setup.test_erc20_token_address,
eth_token_setup.token_bridge,
eth_token_setup.l2_token_bridge,
)
.await;
Ok(())
}
#[rstest]
#[tokio::test]
async fn deposit_tests_both_bridges() -> Result<(), anyhow::Error> {
env_logger::init();
let config = get_test_config_file();
// This will kill the madara when this test fails/passes
let _port_killer = PortKiller;
let clients = Clients::init_from_config(&config).await;
let out = test_setup(&config, &clients).await;
let eth_bridge_setup = out.eth_bridge_setup_outputs.unwrap();
let eth_token_setup = out.erc20_bridge_setup_outputs.unwrap();
let _ = eth_bridge_test_helper(
&clients,
&config,
eth_bridge_setup.l2_eth_proxy_address,
eth_bridge_setup.l2_eth_bridge_proxy_address,
eth_bridge_setup.l1_bridge,
)
.await;
let _ = erc20_bridge_test_helper(
&clients,
&config,
eth_token_setup.test_erc20_token_address,
eth_token_setup.token_bridge,
eth_token_setup.l2_token_bridge,
)
.await;
Ok(())
}
// Create a struct that will kill the process on port when dropped
struct PortKiller;
impl Drop for PortKiller {
fn drop(&mut self) {
kill_process_on_port(19944);
}
}
fn kill_process_on_port(port: u16) {
Command::new("sh")
.arg("-c")
.arg(format!("lsof -i :{} | grep LISTEN | awk '{{print $2}}' | xargs kill -9", port))
.output()
.expect("Failed to execute command");
}
fn get_test_config_file() -> ConfigFile {
ConfigFile::default()
}
async fn wait_for_madara() -> color_eyre::Result<()> {
env::set_current_dir("madara").expect("madara folder doesn't exist.");
fs::create_dir_all("../madara-dbs").expect("unable to create folders");
env::set_var("RUST_LOG", "info");
Command::new("cargo")
.arg("+1.81")
.arg("run")
.arg("--release")
.arg("--")
.arg("--name")
.arg("madara")
.arg("--base-path")
.arg("../madara-dbs/madara_pathfinder_test_11")
.arg("--rpc-port")
.arg("19944")
.arg("--rpc-cors")
.arg("*")
.arg("--rpc-external")
.arg("--sequencer")
.arg("--chain-config-path")
.arg("configs/presets/devnet.yaml")
.arg("--feeder-gateway-enable")
.arg("--gateway-enable")
.arg("--gateway-external")
.arg("--gas-price")
.arg("0")
.arg("--blob-gas-price")
.arg("0")
.arg("--rpc-admin")
.arg("--rpc-admin-port")
.arg("19943")
.arg("--l1-endpoint")
.arg("http://localhost:8545")
.spawn()?;
env::set_current_dir("../").expect("Navigate back failed.");
wait_for_madara_to_be_ready(Url::parse("http://localhost:19944")?).await?;
Ok(())
}
fn ensure_toolchain() -> color_eyre::Result<()> {
let output = Command::new("rustup").arg("toolchain").arg("list").output()?;
let output_str = String::from_utf8_lossy(&output.stdout);
if !output_str.contains("1.81") {
Command::new("rustup").arg("install").arg("1.81").status()?;
}
Ok(())
}
pub async fn wait_for_madara_to_be_ready(rpc_url: Url) -> color_eyre::Result<()> {
// We are fine with `expect` here as this function is called in the intial phases of the
// program execution
let endpoint = rpc_url.join("/health").expect("Request to health endpoint failed");
// We would wait for about 20-25 mins for madara to be ready
wait_for_cond(
|| async {
let res = reqwest::get(endpoint.clone()).await?;
res.error_for_status()?;
Ok(true)
},
Duration::from_secs(5),
250,
)
.await
.expect("Could not get health of Madara");
Ok(())
}
pub async fn wait_for_cond<F: Future<Output = color_eyre::Result<bool>>>(
mut cond: impl FnMut() -> F,
duration: Duration,
attempt_number: usize,
) -> color_eyre::Result<bool> {
let mut attempt = 0;
loop {
let err = match cond().await {
Ok(result) => return Ok(result),
Err(err) => err,
};
attempt += 1;
if attempt >= attempt_number {
panic!("No answer from the node after {attempt} attempts: {:#}", err)
}
tokio::time::sleep(duration).await;
}
}