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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

pub mod compatibility_test;
pub mod fixed_tps_test;
pub mod gas_price_test;
pub mod partial_nodes_down_test;
pub mod performance_test;
pub mod reconfiguration_test;
pub mod state_sync_performance;

use diem_sdk::{transaction_builder::TransactionFactory, types::PeerId};
use forge::{NetworkContext, NodeExt, Result, TxnEmitter, TxnStats, Version};
use rand::SeedableRng;
use std::{
    convert::TryInto,
    time::{Duration, Instant},
};
use tokio::runtime::Runtime;

async fn batch_update(
    ctx: &mut NetworkContext<'_>,
    validators_to_update: &[PeerId],
    version: &Version,
) -> Result<()> {
    for validator in validators_to_update {
        ctx.swarm().upgrade_validator(*validator, version)?;
    }

    ctx.swarm().health_check().await?;
    let deadline = Instant::now() + Duration::from_secs(60);
    for validator in validators_to_update {
        ctx.swarm()
            .validator_mut(*validator)
            .unwrap()
            .wait_until_healthy(deadline)
            .await?;
    }

    Ok(())
}

pub fn generate_traffic<'t>(
    ctx: &mut NetworkContext<'t>,
    validators: &[PeerId],
    duration: Duration,
    gas_price: u64,
    fixed_tps: Option<u64>,
) -> Result<TxnStats> {
    let rt = Runtime::new()?;
    let rng = SeedableRng::from_rng(ctx.core().rng())?;
    let validator_clients = ctx
        .swarm()
        .validators()
        .filter(|v| validators.contains(&v.peer_id()))
        .map(|n| n.rest_client())
        .collect::<Vec<_>>();
    let mut emit_job_request = ctx.global_job.clone();
    let chain_info = ctx.swarm().chain_info();
    let transaction_factory = TransactionFactory::new(chain_info.chain_id);
    let mut emitter = TxnEmitter::new(
        chain_info.treasury_compliance_account,
        chain_info.designated_dealer_account,
        validator_clients[0].clone(),
        transaction_factory,
        rng,
    );

    emit_job_request = emit_job_request
        .rest_clients(validator_clients)
        .gas_price(gas_price);
    if let Some(target_tps) = fixed_tps {
        emit_job_request = emit_job_request.fixed_tps(target_tps.try_into().unwrap());
    }
    let stats = rt.block_on(emitter.emit_txn_for(duration, emit_job_request))?;

    Ok(stats)
}