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
use anyhow::ensure;
use diem_operational_tool::json_rpc::JsonRpcClientWrapper;
use diem_rest_client::Client as RestClient;
use diem_sdk::{
transaction_builder::TransactionFactory,
types::on_chain_config::{ConsensusConfigV2, OnChainConsensusConfig},
};
use forge::{execute_and_wait_transactions, NetworkContext, NetworkTest, NodeExt, Result, Test};
use rand::{
rngs::{OsRng, StdRng},
seq::IteratorRandom,
Rng, SeedableRng,
};
use std::time::Instant;
use tokio::runtime::Runtime;
pub struct ReconfigurationTest;
impl Test for ReconfigurationTest {
fn name(&self) -> &'static str {
"reconfiguration-test"
}
}
impl NetworkTest for ReconfigurationTest {
fn run<'t>(&self, ctx: &mut NetworkContext<'t>) -> Result<()> {
let mut rng = StdRng::from_seed(OsRng.gen());
let client = JsonRpcClientWrapper::new(ctx.swarm().chain_info().json_rpc_url);
let validator_info = client
.validator_set(None)
.expect("Unable to fetch validator set");
let affected_peer_id = *validator_info[0].account_address();
let validator_config = client
.validator_config(affected_peer_id)
.expect("Unable to fetch validator config");
let affected_pod_name = std::str::from_utf8(&validator_config.human_name)
.unwrap()
.to_string();
let validator_clients = ctx
.swarm()
.validators()
.map(|n| n.rest_client())
.collect::<Vec<_>>();
let tx_factory = TransactionFactory::new(ctx.swarm().chain_info().chain_id);
let mut diem_root_account = ctx.swarm().chain_info().root_account;
let allowed_nonce = 0;
let rt = Runtime::new()?;
let full_node_client = validator_clients.iter().choose(&mut rng).unwrap();
let timer = Instant::now();
let count = 101;
rt.block_on(async {
expect_epoch(full_node_client, 1).await.unwrap();
{
println!("Remove and add back {}.", affected_pod_name);
let validator_name = affected_pod_name.as_bytes().to_vec();
let remove_txn = diem_root_account.sign_with_transaction_builder(
tx_factory.remove_validator_and_reconfigure(
allowed_nonce,
validator_name.clone(),
affected_peer_id,
),
);
execute_and_wait_transactions(
full_node_client,
&mut diem_root_account,
vec![remove_txn],
)
.await
.unwrap();
expect_epoch(full_node_client, 2).await.unwrap();
let add_txn = diem_root_account.sign_with_transaction_builder(
tx_factory.add_validator_and_reconfigure(
allowed_nonce,
validator_name.clone(),
affected_peer_id,
),
);
execute_and_wait_transactions(
full_node_client,
&mut diem_root_account,
vec![add_txn],
)
.await
.unwrap();
expect_epoch(full_node_client, 3).await.unwrap();
}
{
println!("Switch decoupled-execution on and off repetitively.");
let upgrade_config = OnChainConsensusConfig::V2(ConsensusConfigV2 {
two_chain: true,
decoupled_execution: true,
back_pressure_limit: 10,
exclude_round: 20,
});
let downgrade_config = OnChainConsensusConfig::default();
for i in 1..count / 2 {
let upgrade_txn = diem_root_account.sign_with_transaction_builder(
tx_factory.update_diem_consensus_config(
allowed_nonce,
bcs::to_bytes(&upgrade_config).unwrap(),
),
);
execute_and_wait_transactions(
full_node_client,
&mut diem_root_account,
vec![upgrade_txn],
)
.await
.unwrap();
expect_epoch(full_node_client, (i + 1) * 2).await.unwrap();
let downgrade_txn = diem_root_account.sign_with_transaction_builder(
tx_factory.update_diem_consensus_config(
allowed_nonce,
bcs::to_bytes(&downgrade_config).unwrap(),
),
);
execute_and_wait_transactions(
full_node_client,
&mut diem_root_account,
vec![downgrade_txn],
)
.await
.unwrap();
expect_epoch(full_node_client, (i + 1) * 2 + 1)
.await
.unwrap();
}
}
if count % 2 == 1 {
let magic_number = 42;
println!("Bump DiemVersion to {}", magic_number);
let update_txn = diem_root_account.sign_with_transaction_builder(
tx_factory.update_diem_version(allowed_nonce, magic_number),
);
execute_and_wait_transactions(
full_node_client,
&mut diem_root_account,
vec![update_txn],
)
.await
.unwrap();
expect_epoch(full_node_client, count + 1).await.unwrap();
}
});
let elapsed = timer.elapsed();
ctx.report.report_text(format!(
"Reconfiguration: total epoch: {} finished in {} seconds",
count,
elapsed.as_secs()
));
Ok(())
}
}
async fn expect_epoch(client: &RestClient, expected_epoch: u64) -> anyhow::Result<()> {
let config = client.get_epoch_configuration().await?.into_inner();
let next_block_epoch = *config.next_block_epoch.inner();
ensure!(
next_block_epoch == expected_epoch,
"Expect next block epoch {}, actual {}",
expected_epoch,
next_block_epoch
);
Ok(())
}