pfc-tester 0.1.3

Smart Contract Testing Tools
Documentation
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
use clap::{Arg, ArgMatches};
use dotenv::dotenv;
use glob::glob;
use pfc_tester::errors::TerraRustTestingError;
use pfc_tester::errors::TerraRustTestingError::ExecResponseFail;
use pfc_tester::{NAME, VERSION};
use rand::distributions::Alphanumeric;
use rand::Rng;
use secp256k1::Secp256k1;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use terra_rust_api::bank::MsgSend;
use terra_rust_api::core_types::Coin;
use terra_rust_api::{LCDResult, MsgExecuteContract, PrivateKey, Terra};
use terra_rust_cli::cli_helpers;
use terra_rust_wallet::Wallet;

#[derive(Deserialize, Serialize, Debug)]
pub struct ExecResponse {
    pub msg_index: Option<usize>,
    pub event_type: Option<String>,
    pub attribute_key: Option<String>,
    pub attribute_value: Option<String>,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct ExecCommand {
    // pub wallet: Option<String>,
    pub coins: Option<String>,
    pub json: serde_json::Value,
    pub response: Option<Vec<ExecResponse>>,
    pub error: Option<bool>,
    pub error_message: Option<String>,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct QueryCommand {
    pub json: serde_json::Value,
    pub response: serde_json::Value,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct SendCommand {
    pub to: String,
    pub coins: String,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct QueryArrayCommand {
    pub json: serde_json::Value,
    pub response: Vec<serde_json::Value>,
}
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub enum CommandType {
    Exec(ExecCommand),
    Query(QueryCommand),
    QueryArray(QueryArrayCommand),
    Send(SendCommand),
}
#[derive(Deserialize, Serialize, Debug)]
pub struct Command {
    pub sender: Option<String>,
    pub contract: String,
    pub comment: Option<String>,
    pub to_run: CommandType,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct CommandFile {
    /// generate a random string of 10 a-zA-Z letters which will be constant for the life of the run. E:RAND_FILE_1 -> n
    pub random_per_file: Option<u64>,
    /// generate a random string of 10 a-zA-Z letters which will be constant for the life of the command. E:RAND_CMD_1 -> n
    pub random_per_command: Option<u64>,
    pub commands: Vec<Command>,
}
/// Run a query, and validate the response matches
async fn run_query<'a, C: secp256k1::Signing + secp256k1::Context>(
    terra: &Terra,
    account: &str,
    contract: &str,
    query: serde_json::Value,
    response: serde_json::Value,
    comment: Option<String>,
    secp: &Secp256k1<C>,
    wallet: Option<Wallet<'a>>,
    seed: Option<&str>,
    command_vars: HashMap<String, String>,
) -> Result<(), TerraRustTestingError> {
    let query_str = cli_helpers::expand_block(
        &serde_json::to_string(&query)?,
        Some(account.into()),
        secp,
        wallet.clone(),
        seed,
        Some(command_vars.clone()),
        true,
    )?;
    let response_str = cli_helpers::expand_block(
        &serde_json::to_string(&response)?,
        Some(account.into()),
        secp,
        wallet,
        seed,
        Some(command_vars),
        true,
    )?;
    let response_returned = terra
        .wasm()
        .query::<LCDResult<serde_json::Value>>(contract, &query_str)
        .await?
        .result;
    let response_returned_str = serde_json::to_string(&response_returned)?;
    if response_str != response_returned_str {
        Err(TerraRustTestingError::QueryResponseFail(
            comment.unwrap_or_else(|| "-".into()),
            contract.into(),
            response_str,
            response_returned_str,
        ))
    } else {
        log::info!(
            "OK: Query {} {}",
            comment.unwrap_or_else(|| "-".into()),
            contract
        );

        Ok(())
    }
}

/// exec an action, and validate the attributes are present
async fn run_exec<'a, C: secp256k1::Signing + secp256k1::Context>(
    terra: &Terra,
    secp: &Secp256k1<C>,
    private_key: &PrivateKey,
    contract: &str,
    action: serde_json::Value,
    coins: Option<String>,
    responses_wanted: Option<Vec<ExecResponse>>,
    comment: Option<String>,
    wallet: Option<Wallet<'a>>,
    seed: Option<&str>,
    sleep: u64,
    retries: usize,
    should_error: Option<bool>,
    error_message: Option<String>,
    command_vars: HashMap<String, String>,
) -> Result<(), TerraRustTestingError> {
    let sender = private_key.public_key(secp).account()?;
    let action_str = cli_helpers::expand_block(
        &serde_json::to_string(&action)?,
        Some(sender.clone()),
        secp,
        wallet.clone(),
        seed,
        Some(command_vars.clone()),
        true,
    )?;

    let coin_vec = if let Some(coin_str) = coins {
        Coin::parse_coins(&coin_str)?
    } else {
        vec![]
    };
    let should_fail = should_error.unwrap_or(false);
    let message = MsgExecuteContract::create_from_json(&sender, contract, &action_str, &coin_vec)?;
    let resp = terra
        .submit_transaction_sync(
            secp,
            private_key,
            vec![message],
            Some(format!(
                "{}/{}",
                NAME.unwrap_or("PFC-TEST"),
                VERSION.unwrap_or("DEV")
            )),
        )
        .await;
    match resp {
        Ok(positive_response) => {
            let hash = positive_response.txhash;
            if should_fail {
                return Err(TerraRustTestingError::ExecResponseShouldHaveFailed(
                    comment.unwrap_or_else(|| "-".into()),
                    contract.into(),
                    hash,
                ));
            }
            let tx = terra
                .tx()
                .get_and_wait_v1(&hash, retries, tokio::time::Duration::from_secs(sleep))
                .await?;

            if let Some(responses) = responses_wanted {
                for response in &responses {
                    let mut matched = false;
                    let response_event_type =
                        &response.event_type.clone().unwrap_or_else(|| "-".into());
                    let response_attr_key =
                        &response.attribute_key.clone().unwrap_or_else(|| "-".into());
                    let response_attr_value = cli_helpers::expand_block(
                        &response
                            .attribute_value
                            .clone()
                            .unwrap_or_else(|| "-".into()),
                        Some(sender.clone()),
                        secp,
                        wallet.clone(),
                        seed,
                        Some(command_vars.clone()),
                        true,
                    )?;

                    if let Some(logs) = &tx.tx_response.logs {
                        for log_entry in logs {
                            if response.msg_index.is_none()
                                || (log_entry.msg_index.unwrap_or(0) == response.msg_index.unwrap())
                            {
                                for event_v in &log_entry.events {
                                    if response_event_type == "-"
                                        || response_event_type == &event_v.s_type
                                    {
                                        for attr in &event_v.attributes {
                                            let attr_val =
                                                attr.value.clone().unwrap_or_else(|| "-".into());
                                            if (response_attr_key == "-"
                                                || response_attr_key == &attr.key)
                                                && (response_attr_value == "-"
                                                    || response_attr_value.as_str() == attr_val)
                                            {
                                                matched = true;
                                                log::debug!(
                                                    "event {} {} {:?}",
                                                    log_entry.msg_index.unwrap_or(0),
                                                    event_v.s_type,
                                                    event_v.attributes
                                                );
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if !matched {
                        return Err(ExecResponseFail(
                            comment.unwrap_or_else(|| "-".into()),
                            contract.into(),
                            response_event_type.to_string(),
                            response_attr_key.to_string(),
                            response_attr_value,
                            hash,
                        ));
                    }
                }
            }
            log::info!(
                "OK: Exec {} {} {}",
                comment.unwrap_or_else(|| "-".into()),
                contract,
                hash
            );
        }
        Err(err_response) => {
            if should_fail {
                if let Some(error_mesage_wanted) = error_message {
                    if error_mesage_wanted == err_response.to_string() {
                        log::info!(
                            "OK: Exec {} {} -- Error matched",
                            comment.unwrap_or_else(|| "-".into()),
                            contract
                        );
                    } else {
                        return Err(TerraRustTestingError::ExecResponseShouldHaveFailedMessage(
                            comment.unwrap_or_else(|| "-".into()),
                            contract.into(),
                            error_mesage_wanted,
                            err_response.to_string(),
                        ));
                    }
                } else {
                    log::info!(
                        "OK: Exec {} {} -- Error",
                        comment.unwrap_or_else(|| "-".into()),
                        contract
                    );
                    log::debug!("{}", err_response.to_string())
                }
            } else {
                return Err(err_response.into());
            }
        }
    }

    Ok(())
}

/// exec an action, and validate the attributes are present
async fn run_send<'a, C: secp256k1::Signing + secp256k1::Context>(
    terra: &Terra,
    secp: &Secp256k1<C>,
    private_key: &PrivateKey,
    to: String,
    coins: String,
    comment: Option<String>,
    wallet: Wallet<'a>,
    seed: Option<&str>,
    sleep: u64,
    retries: usize,
    command_vars: HashMap<String, String>,
) -> Result<(), TerraRustTestingError> {
    let sender = private_key.public_key(secp).account()?;
    let send_to = cli_helpers::expand_block(
        &to,
        Some(sender.clone()),
        secp,
        Some(wallet),
        seed,
        Some(command_vars),
        true,
    )?;

    let coin_vec = Coin::parse_coins(&coins)?;

    let message = MsgSend::create(sender.clone(), send_to.clone(), coin_vec)?;
    let hash = terra
        .submit_transaction_sync(
            secp,
            private_key,
            vec![message],
            Some(format!(
                "{}/{}",
                NAME.unwrap_or("PFC-TEST"),
                VERSION.unwrap_or("DEV")
            )),
        )
        .await?
        .txhash;

    let _tx = terra
        .tx()
        .get_and_wait_v1(&hash, retries, tokio::time::Duration::from_secs(sleep))
        .await?;

    log::info!(
        "OK Bank Send {} {}-> {} # {}",
        comment.unwrap_or_else(|| "-".into()),
        sender,
        send_to,
        coins
    );
    Ok(())
}

async fn run_command_file<'a, C: secp256k1::Context + secp256k1::Signing>(
    terra: &Terra,
    secp: &Secp256k1<C>,
    wallet: &Wallet<'a>,
    default_key: &PrivateKey,
    command_file: CommandFile,
    seed: Option<&str>,
    sleep: u64,
    retries: usize,
) -> Result<(), TerraRustTestingError> {
    let mut rng = rand::thread_rng();
    let mut file_vars: HashMap<String, String> = Default::default();

    if let Some(rand_per_file) = command_file.random_per_file {
        for i in 1..rand_per_file + 1 {
            let word: String = (0..10).map(|_| rng.sample(Alphanumeric) as char).collect();
            file_vars.insert(format!("RAND_FILE_{}", i), word);
        }
    }

    for command in command_file.commands {
        let mut command_vars: HashMap<String, String> = file_vars.clone();
        if let Some(rand_per_command) = command_file.random_per_command {
            for i in 1..rand_per_command + 1 {
                let word: String = (0..10).map(|_| rng.sample(Alphanumeric) as char).collect();
                command_vars.insert(format!("RAND_CMD_{}", i), word);
            }
        }
        let sender = if let Some(key_name) = command.sender {
            wallet.get_private_key(secp, &key_name, None)?
        } else {
            default_key.clone()
        };
        match command.to_run {
            CommandType::Query(query) => {
                let _resp = run_query(
                    terra,
                    &sender.public_key(secp).account()?,
                    &command.contract,
                    query.json,
                    query.response,
                    command.comment,
                    secp,
                    Some(wallet.clone()),
                    seed,
                    command_vars,
                )
                .await?;
            }
            CommandType::QueryArray(_query) => {
                todo!("not yet")
            }
            CommandType::Exec(exec) => {
                let _resp = run_exec(
                    terra,
                    secp,
                    &sender,
                    &command.contract,
                    exec.json,
                    exec.coins,
                    exec.response,
                    command.comment,
                    Some(wallet.clone()),
                    seed,
                    sleep,
                    retries,
                    exec.error,
                    exec.error_message,
                    command_vars,
                )
                .await?;
            }
            CommandType::Send(send) => {
                let _resp = run_send(
                    terra,
                    secp,
                    &sender,
                    send.to,
                    send.coins,
                    command.comment,
                    wallet.clone(),
                    seed,
                    sleep,
                    retries,
                    command_vars,
                )
                .await?;
            }
        }
    }
    Ok(())
}
async fn run_it(matches: &ArgMatches) -> Result<(), TerraRustTestingError> {
    dotenv::from_filename("code_id.env").ok();
    dotenv::from_filename("contracts.env").ok();
    let sleep = cli_helpers::get_arg_value(matches, "sleep")?.parse::<u64>()?;
    let retries = cli_helpers::get_arg_value(matches, "retries")?.parse::<usize>()?;

    let secp = Secp256k1::new();
    let terra = cli_helpers::lcd_from_args(matches).await?;
    let from_key = cli_helpers::get_private_key(&secp, matches)?;
    let wallet = cli_helpers::wallet_from_args(matches)?;
    let seed = cli_helpers::seed_from_args(matches);
    let glob_pattern = if let Some(dir) = matches.value_of("test-directory") {
        format!("{}/*.test.json", dir)
    } else if let Some(file) = matches.value_of("test-file") {
        file.to_string()
    } else {
        panic!("Either pass in a test-directory or a test-file");
    };
    //let resource_directory = cli_helpers::get_arg_value(matches, "test-directory")?;
    for test_file in glob(&glob_pattern)? {
        let file = test_file?;
        log::info!("{}", file.display());
        // sender is NONE here. will expand later
        let expanded_json = cli_helpers::get_json_block_expanded(
            file.as_os_str().to_str().unwrap(),
            None,
            &secp,
            Some(wallet.clone()),
            seed,
            None,
            false,
        )?;

        let command_file: CommandFile = serde_json::from_value(expanded_json)?;

        run_command_file(
            &terra,
            &secp,
            &wallet,
            &from_key,
            command_file,
            seed,
            sleep,
            retries,
        )
        .await?;
    }
    Ok(())
}
async fn run() -> anyhow::Result<()> {
    let app = cli_helpers::gen_cli("PFC-Replayer", "pfc-replayer").args(&[
        Arg::new("test-directory")
            .long("test-directory")
            .takes_value(true)
            .value_name("test-directory")
            .help("directory where test (*.test.json) JSON resides"),
        Arg::new("test-file")
            .long("test-file")
            .takes_value(true)
            .value_name("test-file")
            .help("test file to replay"),
        Arg::new("retries")
            .long("retries")
            .takes_value(true)
            .value_name("retries")
            .required(false)
            .default_value("5")
            .help("amount of times to retry fetching hash"),
        Arg::new("sleep")
            .long("sleep")
            .takes_value(true)
            .value_name("sleep")
            .required(false)
            .default_value("3")
            .help("amount of seconds before retying to fetch hash"),
    ]);

    Ok(run_it(&app.get_matches()).await?)
}
#[tokio::main]
async fn main() {
    dotenv().ok();
    env_logger::init();

    if let Err(ref err) = run().await {
        log::error!("{}", err);
        err.chain()
            .skip(1)
            .for_each(|cause| log::error!("because: {}", cause));

        // The backtrace is not always generated. Try to run this example
        // with `$env:RUST_BACKTRACE=1`.
        //    if let Some(backtrace) = e.backtrace() {
        //        log::debug!("backtrace: {:?}", backtrace);
        //    }

        ::std::process::exit(1);
    }
}