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
// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0

use std::{convert::TryFrom, path::Path};

use anyhow::{bail, format_err, Result};
use aptos::common::types::EncodingType;
use aptos_config::keys::ConfigKey;
use aptos_crypto::ed25519::Ed25519PrivateKey;
use aptos_sdk::types::chain_id::ChainId;
use clap::{ArgEnum, Parser};

use serde::{Deserialize, Serialize};
use url::Url;

const DEFAULT_API_PORT: u16 = 8080;

#[derive(Clone, Debug, Default, Deserialize, Parser, Serialize)]
pub struct MintArgs {
    /// Ed25519PrivateKey for minting coins
    #[clap(long, parse(try_from_str = ConfigKey::from_encoded_string))]
    pub mint_key: Option<ConfigKey<Ed25519PrivateKey>>,

    #[clap(long, default_value = "mint.key", conflicts_with = "mint-key")]
    pub mint_file: String,
}

impl MintArgs {
    pub fn get_mint_key(&self) -> Result<Ed25519PrivateKey> {
        let key = match &self.mint_key {
            Some(ref key) => key.private_key(),
            None => EncodingType::BCS
                .load_key::<Ed25519PrivateKey>("mint key pair", Path::new(&self.mint_file))?,
        };
        Ok(key)
    }
}

#[derive(Clone, Debug, Default, Deserialize, Parser, Serialize)]
pub struct ClusterArgs {
    /// Nodes the cluster should connect to, e.g. http://node.mysite.com:8080
    /// If the port is not provided, it is assumed to be 8080.
    #[clap(short, long, required = true, min_values = 1, parse(try_from_str = parse_target))]
    pub targets: Vec<Url>,

    /// If set, try to use public peers instead of localhost.
    #[clap(long)]
    pub vasp: bool,

    #[clap(long, default_value = "TESTING")]
    pub chain_id: ChainId,

    #[clap(flatten)]
    pub mint_args: MintArgs,
}

#[derive(Debug, Clone, Copy, ArgEnum, Deserialize, Parser, Serialize)]
pub enum TransactionType {
    P2P,
    AccountGeneration,
    NftMint,
}

impl Default for TransactionType {
    fn default() -> Self {
        TransactionType::P2P
    }
}

#[derive(Clone, Debug, Default, Deserialize, Parser, Serialize)]
pub struct EmitArgs {
    #[clap(long, default_value = "15")]
    pub accounts_per_client: usize,

    #[clap(long)]
    pub workers_per_ac: Option<usize>,

    #[clap(long, default_value = "0")]
    pub wait_millis: u64,

    #[clap(long)]
    pub burst: bool,

    /// This can only be set in conjunction with --burst. By default, when burst
    /// is enabled, we check stats once at the end of the emitter run. If you
    /// would like to opt out of that, you can use this flag.
    #[structopt(long, requires = "burst")]
    pub do_not_check_stats_at_end: bool,

    #[clap(long, default_value = "30")]
    pub txn_expiration_time_secs: u64,

    /// Time to run --emit-tx for in seconds.
    #[clap(long, default_value = "60")]
    pub duration: u64,

    #[clap(long, help = "Percentage of invalid txs", default_value = "0")]
    pub invalid_tx: usize,

    #[clap(long, arg_enum, default_value = "p2p", ignore_case = true)]
    pub transaction_type: TransactionType,
}

fn parse_target(target: &str) -> Result<Url> {
    let mut url = Url::try_from(target).map_err(|e| {
        format_err!(
            "Failed to parse listen address, try adding a scheme, e.g. http://: {}",
            e
        )
    })?;
    if url.scheme().is_empty() {
        bail!("Scheme must not be empty, try prefixing URL with http://");
    }
    if url.port_or_known_default().is_none() {
        url.set_port(Some(DEFAULT_API_PORT)).map_err(|_| {
            anyhow::anyhow!(
                "Failed to set port to default value, make sure you have set a scheme like http://"
            )
        })?;
    }
    Ok(url)
}