@@ -60,15 +60,15 @@ pub fn follower_index_to_dot_movement(
60
60
follower_index : u8 ,
61
61
dot_movement : & DotMovement ,
62
62
) -> Result < DotMovement , anyhow:: Error > {
63
- // index 0 is default .moevement path
63
+ // Index 0 is the default .movement path (leader)
64
64
if follower_index == 0 {
65
65
return Ok ( dot_movement. clone ( ) ) ;
66
66
}
67
67
68
- // otherwise , modify the path to include the follower index
68
+ // Otherwise , modify the path to include the follower index.
69
69
let mut follower_dot_movement = dot_movement. clone ( ) ;
70
70
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.
72
72
let new_path_str = format ! ( "{}-follower-{}" , path. display( ) , follower_index) ;
73
73
let new_path = std:: path:: PathBuf :: from ( new_path_str) ;
74
74
info ! ( "Follower path: {:?}" , new_path) ;
@@ -100,7 +100,7 @@ use std::future::Future;
100
100
101
101
/// Checks whether results from calling different nodes match.
102
102
/// 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.
104
104
pub async fn check_matching < T , F , Fut > (
105
105
lead_dot_movement : & DotMovement ,
106
106
follower_count : u8 ,
@@ -113,30 +113,44 @@ where
113
113
{
114
114
let mut last_result: Option < T > = None ;
115
115
for i in 0 ..=follower_count {
116
- // get all of the info
116
+ // Get all of the info.
117
117
let ( follower_dot_movement, config, rest_client, faucet_client) =
118
118
get_follower_config ( i, lead_dot_movement) ?;
119
119
120
- // call the closure
120
+ // Call the closure.
121
121
let result = closure ( follower_dot_movement, config, rest_client, faucet_client) . await ?;
122
122
123
123
info ! ( "Result from follower {}: {:?}" , i, result) ;
124
124
125
- // compare the result to the last result
125
+ // Compare the result to the last result.
126
126
if let Some ( last_result) = last_result {
127
127
if result != last_result {
128
128
return Err ( anyhow:: anyhow!( "Results do not match" ) ) ;
129
129
}
130
130
}
131
131
132
- // update the last result
132
+ // Update the last result.
133
133
last_result = Some ( result) ;
134
134
}
135
135
136
136
Ok ( ( ) )
137
137
}
138
138
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.
140
154
pub async fn pick_one < T , F , Fut > (
141
155
lead_dot_movement : & DotMovement ,
142
156
follower_count : u8 ,
@@ -180,54 +194,45 @@ pub async fn basic_coin_transfers(
180
194
info ! ( "Alice: {}" , alice. read( ) . await . address( ) . to_hex_literal( ) ) ;
181
195
info ! ( "Bob: {}" , bob. read( ) . await . address( ) . to_hex_literal( ) ) ;
182
196
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.
185
198
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 ;
195
203
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" ) ?;
200
208
201
- Ok ( ( ) )
202
- }
203
- } ,
204
- )
209
+ Ok ( ( ) )
210
+ }
211
+ } )
205
212
. await ?;
206
213
207
- // Bob
214
+ // Use a random node to create Bob's account.
208
215
let bob_clone = bob. clone ( ) ;
209
216
pick_one (
210
217
lead_dot_movement,
211
218
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| {
214
220
let bob = bob_clone. clone ( ) ;
215
-
216
221
async move {
217
222
let bob = bob. write ( ) . await ;
218
223
219
224
faucet_client
220
225
. create_account ( bob. address ( ) )
221
226
. await
222
- . context ( "Failed to fund Bob's account" ) ?;
227
+ . context ( "Failed to create Bob's account" ) ?;
223
228
224
229
Ok ( ( ) )
225
230
}
226
231
} ,
227
232
)
228
233
. await ?;
229
234
230
- // check all the coin balances are equal
235
+ // Check all the coin balances are equal.
231
236
let alice_clone = alice. clone ( ) ;
232
237
let bob_clone = bob. clone ( ) ;
233
238
check_matching (
@@ -257,7 +262,7 @@ pub async fn basic_coin_transfers(
257
262
lead_dot_movement,
258
263
follower_count,
259
264
move |_dot_movement, _config, rest_client, _faucet_client| async move {
260
- // get the latest ledger version
265
+ // Get the latest ledger version.
261
266
let latest_ledger_version = rest_client. get_ledger_information ( ) . await ?. inner ( ) . version ;
262
267
263
268
let block_by_version =
@@ -268,34 +273,30 @@ pub async fn basic_coin_transfers(
268
273
)
269
274
. await ?;
270
275
271
- // Have Alice send Bob some coins.
276
+ // Have Alice send Bob some coins using the leader .
272
277
let alice_clone = alice. clone ( ) ;
273
278
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
+ } )
296
297
. await ?;
297
298
298
- // check all the coin balances are equal
299
+ // Check all the coin balances are equal.
299
300
let alice_clone = alice. clone ( ) ;
300
301
let bob_clone = bob. clone ( ) ;
301
302
check_matching (
@@ -321,7 +322,7 @@ pub async fn basic_coin_transfers(
321
322
)
322
323
. await ?;
323
324
324
- // Have Alice send Bob some coins.
325
+ // Have Alice send Bob some more coins using a random node .
325
326
let alice_clone = alice. clone ( ) ;
326
327
let bob_clone = bob. clone ( ) ;
327
328
pick_one (
@@ -348,7 +349,7 @@ pub async fn basic_coin_transfers(
348
349
)
349
350
. await ?;
350
351
351
- // check all the coin balances are equal
352
+ // Check all the coin balances are equal.
352
353
check_matching (
353
354
lead_dot_movement,
354
355
follower_count,
@@ -372,12 +373,12 @@ pub async fn basic_coin_transfers(
372
373
)
373
374
. await ?;
374
375
375
- // check that all the block hashes are equal
376
+ // Check that all the block hashes are equal.
376
377
check_matching (
377
378
lead_dot_movement,
378
379
follower_count,
379
380
move |_dot_movement, _config, rest_client, _faucet_client| async move {
380
- // get the latest ledger version
381
+ // Get the latest ledger version.
381
382
let latest_ledger_version = rest_client. get_ledger_information ( ) . await ?. inner ( ) . version ;
382
383
383
384
let block_by_version =
@@ -398,16 +399,16 @@ async fn main() -> Result<(), anyhow::Error> {
398
399
} ;
399
400
let _guard = movement_tracing:: init_tracing_subscriber ( tracing_config) ;
400
401
401
- // get the lead dot movement from the environment
402
+ // Get the lead dot movement from the environment.
402
403
let dot_movement = DotMovement :: try_from_env ( ) ?;
403
404
404
- // get the follower count from the first argument
405
+ // Get the follower count from the first argument.
405
406
let follower_count = std:: env:: args ( )
406
407
. nth ( 1 )
407
408
. ok_or_else ( || anyhow:: anyhow!( "Expected follower count as first argument" ) ) ?;
408
409
let follower_count = u8:: from_str ( follower_count. as_str ( ) ) ?;
409
410
410
- // run basic coin transfers
411
+ // Run basic coin transfers.
411
412
basic_coin_transfers ( & dot_movement, follower_count) . await ?;
412
413
413
414
Ok ( ( ) )
0 commit comments