Skip to content

Commit 21f9179

Browse files
authored
fix: test-followers-consistent (#1127)
1 parent 4a31618 commit 21f9179

File tree

2 files changed

+69
-66
lines changed

2 files changed

+69
-66
lines changed

networks/movement/movement-client/src/bin/e2e/followers_consistent.rs

Lines changed: 67 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ pub fn follower_index_to_dot_movement(
6060
follower_index: u8,
6161
dot_movement: &DotMovement,
6262
) -> Result<DotMovement, anyhow::Error> {
63-
// index 0 is default .moevement path
63+
// Index 0 is the default .movement path (leader)
6464
if follower_index == 0 {
6565
return Ok(dot_movement.clone());
6666
}
6767

68-
// otherwise, modify the path to include the follower index
68+
// Otherwise, modify the path to include the follower index.
6969
let mut follower_dot_movement = dot_movement.clone();
7070
let path = follower_dot_movement.get_path().to_path_buf();
71-
// append -follower-{n} to the last component of the path
71+
// Append -follower-{n} to the last component of the path.
7272
let new_path_str = format!("{}-follower-{}", path.display(), follower_index);
7373
let new_path = std::path::PathBuf::from(new_path_str);
7474
info!("Follower path: {:?}", new_path);
@@ -100,7 +100,7 @@ use std::future::Future;
100100

101101
/// Checks whether results from calling different nodes match.
102102
/// Relies on the DOT_MOVEMENT config in each directory.
103-
/// Takes an async closure to which the an appropriate rest client and faucet client are passed.
103+
/// Takes an async closure to which the appropriate rest client and faucet client are passed.
104104
pub async fn check_matching<T, F, Fut>(
105105
lead_dot_movement: &DotMovement,
106106
follower_count: u8,
@@ -113,30 +113,44 @@ where
113113
{
114114
let mut last_result: Option<T> = None;
115115
for i in 0..=follower_count {
116-
// get all of the info
116+
// Get all of the info.
117117
let (follower_dot_movement, config, rest_client, faucet_client) =
118118
get_follower_config(i, lead_dot_movement)?;
119119

120-
// call the closure
120+
// Call the closure.
121121
let result = closure(follower_dot_movement, config, rest_client, faucet_client).await?;
122122

123123
info!("Result from follower {}: {:?}", i, result);
124124

125-
// compare the result to the last result
125+
// Compare the result to the last result.
126126
if let Some(last_result) = last_result {
127127
if result != last_result {
128128
return Err(anyhow::anyhow!("Results do not match"));
129129
}
130130
}
131131

132-
// update the last result
132+
// Update the last result.
133133
last_result = Some(result);
134134
}
135135

136136
Ok(())
137137
}
138138

139-
/// Picks one of the nodes to run the closure against at random
139+
/// Picks the leader to run the closure against.
140+
pub async fn pick_leader<T, F, Fut>(
141+
lead_dot_movement: &DotMovement,
142+
mut closure: F,
143+
) -> Result<T, anyhow::Error>
144+
where
145+
F: FnMut(DotMovement, movement_config::Config, Client, FaucetClient) -> Fut,
146+
Fut: Future<Output = Result<T, anyhow::Error>>,
147+
{
148+
let (leader_dot_movement, config, rest_client, faucet_client) =
149+
get_follower_config(0, lead_dot_movement)?;
150+
closure(leader_dot_movement, config, rest_client, faucet_client).await
151+
}
152+
153+
/// Picks one of the nodes to run the closure against at random.
140154
pub async fn pick_one<T, F, Fut>(
141155
lead_dot_movement: &DotMovement,
142156
follower_count: u8,
@@ -180,54 +194,45 @@ pub async fn basic_coin_transfers(
180194
info!("Alice: {}", alice.read().await.address().to_hex_literal());
181195
info!("Bob: {}", bob.read().await.address().to_hex_literal());
182196

183-
// Create the accounts on chain, but only fund Alice. Pick one node to do this against for each.
184-
// Alice
197+
// Create the accounts on chain, but only fund Alice. Use the leader for funding.
185198
let alice_clone = alice.clone();
186-
pick_one(
187-
lead_dot_movement,
188-
follower_count,
189-
move |_dot_movement, _config, _res_client, faucet_client| {
190-
// Clone `alice` to move it into the async block safely
191-
let alice = alice_clone.clone();
192-
193-
async move {
194-
let alice = alice.write().await;
199+
pick_leader(lead_dot_movement, move |_dot_movement, _config, _rest_client, faucet_client| {
200+
let alice = alice_clone.clone();
201+
async move {
202+
let alice = alice.write().await;
195203

196-
faucet_client
197-
.fund(alice.address(), 100_000_000)
198-
.await
199-
.context("Failed to fund Alice's account")?;
204+
faucet_client
205+
.fund(alice.address(), 100_000_000)
206+
.await
207+
.context("Failed to fund Alice's account")?;
200208

201-
Ok(())
202-
}
203-
},
204-
)
209+
Ok(())
210+
}
211+
})
205212
.await?;
206213

207-
// Bob
214+
// Use a random node to create Bob's account.
208215
let bob_clone = bob.clone();
209216
pick_one(
210217
lead_dot_movement,
211218
follower_count,
212-
move |_dot_movement, _config, _res_client, faucet_client| {
213-
// Clone `bob` to move it into the async block safely
219+
move |_dot_movement, _config, _rest_client, faucet_client| {
214220
let bob = bob_clone.clone();
215-
216221
async move {
217222
let bob = bob.write().await;
218223

219224
faucet_client
220225
.create_account(bob.address())
221226
.await
222-
.context("Failed to fund Bob's account")?;
227+
.context("Failed to create Bob's account")?;
223228

224229
Ok(())
225230
}
226231
},
227232
)
228233
.await?;
229234

230-
// check all the coin balances are equal
235+
// Check all the coin balances are equal.
231236
let alice_clone = alice.clone();
232237
let bob_clone = bob.clone();
233238
check_matching(
@@ -257,7 +262,7 @@ pub async fn basic_coin_transfers(
257262
lead_dot_movement,
258263
follower_count,
259264
move |_dot_movement, _config, rest_client, _faucet_client| async move {
260-
// get the latest ledger version
265+
// Get the latest ledger version.
261266
let latest_ledger_version = rest_client.get_ledger_information().await?.inner().version;
262267

263268
let block_by_version =
@@ -268,34 +273,30 @@ pub async fn basic_coin_transfers(
268273
)
269274
.await?;
270275

271-
// Have Alice send Bob some coins.
276+
// Have Alice send Bob some coins using the leader.
272277
let alice_clone = alice.clone();
273278
let bob_clone = bob.clone();
274-
pick_one(
275-
lead_dot_movement,
276-
follower_count,
277-
move |_dot_movement, _config, rest_client, _faucet_client| {
278-
let alice = alice_clone.clone();
279-
let bob = bob_clone.clone();
280-
async move {
281-
let mut alice = alice.write().await;
282-
let coin_client = CoinClient::new(&rest_client);
283-
let txn_hash = coin_client
284-
.transfer(&mut *alice, bob.read().await.address(), 1_000, None)
285-
.await
286-
.context("Failed to submit transaction to transfer coins")?;
287-
rest_client
288-
.wait_for_transaction(&txn_hash)
289-
.await
290-
.context("Failed when waiting for the transfer transaction")?;
291-
292-
Ok(())
293-
}
294-
},
295-
)
279+
pick_leader(lead_dot_movement, move |_dot_movement, _config, rest_client, _faucet_client| {
280+
let alice = alice_clone.clone();
281+
let bob = bob_clone.clone();
282+
async move {
283+
let mut alice = alice.write().await;
284+
let coin_client = CoinClient::new(&rest_client);
285+
let txn_hash = coin_client
286+
.transfer(&mut *alice, bob.read().await.address(), 1_000, None)
287+
.await
288+
.context("Failed to submit transaction to transfer coins")?;
289+
rest_client
290+
.wait_for_transaction(&txn_hash)
291+
.await
292+
.context("Failed when waiting for the transfer transaction")?;
293+
294+
Ok(())
295+
}
296+
})
296297
.await?;
297298

298-
// check all the coin balances are equal
299+
// Check all the coin balances are equal.
299300
let alice_clone = alice.clone();
300301
let bob_clone = bob.clone();
301302
check_matching(
@@ -321,7 +322,7 @@ pub async fn basic_coin_transfers(
321322
)
322323
.await?;
323324

324-
// Have Alice send Bob some coins.
325+
// Have Alice send Bob some more coins using a random node.
325326
let alice_clone = alice.clone();
326327
let bob_clone = bob.clone();
327328
pick_one(
@@ -348,7 +349,7 @@ pub async fn basic_coin_transfers(
348349
)
349350
.await?;
350351

351-
// check all the coin balances are equal
352+
// Check all the coin balances are equal.
352353
check_matching(
353354
lead_dot_movement,
354355
follower_count,
@@ -372,12 +373,12 @@ pub async fn basic_coin_transfers(
372373
)
373374
.await?;
374375

375-
// check that all the block hashes are equal
376+
// Check that all the block hashes are equal.
376377
check_matching(
377378
lead_dot_movement,
378379
follower_count,
379380
move |_dot_movement, _config, rest_client, _faucet_client| async move {
380-
// get the latest ledger version
381+
// Get the latest ledger version.
381382
let latest_ledger_version = rest_client.get_ledger_information().await?.inner().version;
382383

383384
let block_by_version =
@@ -398,16 +399,16 @@ async fn main() -> Result<(), anyhow::Error> {
398399
};
399400
let _guard = movement_tracing::init_tracing_subscriber(tracing_config);
400401

401-
// get the lead dot movement from the environment
402+
// Get the lead dot movement from the environment.
402403
let dot_movement = DotMovement::try_from_env()?;
403404

404-
// get the follower count from the first argument
405+
// Get the follower count from the first argument.
405406
let follower_count = std::env::args()
406407
.nth(1)
407408
.ok_or_else(|| anyhow::anyhow!("Expected follower count as first argument"))?;
408409
let follower_count = u8::from_str(follower_count.as_str())?;
409410

410-
// run basic coin transfers
411+
// Run basic coin transfers.
411412
basic_coin_transfers(&dot_movement, follower_count).await?;
412413

413414
Ok(())

process-compose/movement-full-node/process-compose.test-followers.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ processes:
111111
command: curl http://0.0.0.0:32731
112112

113113
test-followers-consistent:
114+
environment:
115+
- "MOVEMENT_SYNC=leader::follower-test-$MOVEMENT_SHARED_RANDOM_1<=>{default_signer_address_whitelist,maptos,maptos-storage,movement-da-db}/**"
114116
command: |
115117
# run the test
116118
cargo run --bin movement-tests-e2e-followers-consistent -- 2

0 commit comments

Comments
 (0)