-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathround_manager_fuzzing.rs
256 lines (229 loc) · 8.49 KB
/
round_manager_fuzzing.rs
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
// Copyright © Aptos Foundation
// Parts of the project are originally copyright © Meta Platforms, Inc.
// SPDX-License-Identifier: Apache-2.0
use crate::{
block_storage::BlockStore,
liveness::{
proposal_generator::{
ChainHealthBackoffConfig, PipelineBackpressureConfig, ProposalGenerator,
},
rotating_proposer_election::RotatingProposer,
round_state::{ExponentialTimeInterval, NewRoundEvent, NewRoundReason, RoundState},
},
metrics_safety_rules::MetricsSafetyRules,
network::NetworkSender,
network_interface::{ConsensusNetworkClient, DIRECT_SEND, RPC},
payload_manager::PayloadManager,
persistent_liveness_storage::{PersistentLivenessStorage, RecoveryData},
round_manager::RoundManager,
test_utils::{EmptyStateComputer, MockPayloadManager, MockStorage},
util::{mock_time_service::SimulatedTimeService, time_service::TimeService},
};
use aptos_channels::{self, aptos_channel, message_queues::QueueStyle};
use aptos_config::{
config::{ConsensusConfig, QcAggregatorType},
network_id::NetworkId,
};
use aptos_consensus_types::proposal_msg::ProposalMsg;
use aptos_infallible::Mutex;
use aptos_network::{
application::{interface::NetworkClient, storage::PeersAndMetadata},
peer_manager::{ConnectionRequestSender, PeerManagerRequestSender},
protocols::{network, network::NewNetworkSender},
};
use aptos_safety_rules::{test_utils, SafetyRules, TSafetyRules};
use aptos_types::{
aggregate_signature::AggregateSignature,
epoch_change::EpochChangeProof,
epoch_state::EpochState,
ledger_info::{LedgerInfo, LedgerInfoWithSignatures},
on_chain_config::{Features, OnChainConsensusConfig, ValidatorSet, ValidatorTxnConfig},
validator_info::ValidatorInfo,
validator_signer::ValidatorSigner,
validator_verifier::ValidatorVerifier,
};
use futures::{channel::mpsc, executor::block_on};
use futures_channel::mpsc::unbounded;
use maplit::hashmap;
use once_cell::sync::Lazy;
use std::{sync::Arc, time::Duration};
use tokio::runtime::Runtime;
// This generates a proposal for round 1
pub fn generate_corpus_proposal() -> Vec<u8> {
let mut round_manager = create_node_for_fuzzing();
block_on(async {
let proposal = round_manager
.generate_proposal(NewRoundEvent {
round: 1,
reason: NewRoundReason::QCReady,
timeout: std::time::Duration::new(5, 0),
prev_round_votes: Vec::new(),
prev_round_timeout_votes: None,
})
.await;
// serialize and return proposal
serde_json::to_vec(&proposal.unwrap()).unwrap()
})
}
// optimization for the fuzzer
static STATIC_RUNTIME: Lazy<Runtime> = Lazy::new(|| Runtime::new().unwrap());
static FUZZING_SIGNER: Lazy<ValidatorSigner> = Lazy::new(|| ValidatorSigner::from_int(1));
// helpers
fn build_empty_store(
storage: Arc<dyn PersistentLivenessStorage>,
initial_data: RecoveryData,
) -> Arc<BlockStore> {
let (_commit_cb_sender, _commit_cb_receiver) = mpsc::unbounded::<LedgerInfoWithSignatures>();
Arc::new(BlockStore::new(
storage,
initial_data,
Arc::new(EmptyStateComputer),
10, // max pruned blocks in mem
Arc::new(SimulatedTimeService::new()),
10,
Arc::from(PayloadManager::DirectMempool),
))
}
// helpers for safety rule initialization
fn make_initial_epoch_change_proof(signer: &ValidatorSigner) -> EpochChangeProof {
let validator_info =
ValidatorInfo::new_with_test_network_keys(signer.author(), signer.public_key(), 1, 0);
let validator_set = ValidatorSet::new(vec![validator_info]);
let li = LedgerInfo::mock_genesis(Some(validator_set));
let lis = LedgerInfoWithSignatures::new(li, AggregateSignature::empty());
EpochChangeProof::new(vec![lis], false)
}
// TODO: MockStorage -> EmptyStorage
fn create_round_state() -> RoundState {
let base_timeout = std::time::Duration::new(60, 0);
let time_interval = Box::new(ExponentialTimeInterval::fixed(base_timeout));
let (round_timeout_sender, _) = aptos_channels::new_test(1_024);
let (delayed_qc_tx, _) = unbounded();
let time_service = Arc::new(SimulatedTimeService::new());
RoundState::new(
time_interval,
time_service,
round_timeout_sender,
delayed_qc_tx,
QcAggregatorType::NoDelay,
)
}
// Creates an RoundManager for fuzzing
fn create_node_for_fuzzing() -> RoundManager {
// signer is re-used accross fuzzing runs
let signer = FUZZING_SIGNER.clone();
// TODO: remove
let validator = ValidatorVerifier::new_single(signer.author(), signer.public_key());
let validator_set = (&validator).into();
// TODO: EmptyStorage
let (initial_data, storage) = MockStorage::start_for_testing(validator_set);
// TODO: remove
let proof = make_initial_epoch_change_proof(&signer);
let mut safety_rules = SafetyRules::new(test_utils::test_storage(&signer));
safety_rules.initialize(&proof).unwrap();
// TODO: mock channels
let (network_reqs_tx, _network_reqs_rx) = aptos_channel::new(QueueStyle::FIFO, 8, None);
let (connection_reqs_tx, _) = aptos_channel::new(QueueStyle::FIFO, 8, None);
let network_sender = network::NetworkSender::new(
PeerManagerRequestSender::new(network_reqs_tx),
ConnectionRequestSender::new(connection_reqs_tx),
);
let network_client = NetworkClient::new(
DIRECT_SEND.into(),
RPC.into(),
hashmap! {NetworkId::Validator => network_sender},
PeersAndMetadata::new(&[NetworkId::Validator]),
);
let consensus_network_client = ConsensusNetworkClient::new(network_client);
let (self_sender, _self_receiver) = aptos_channels::new_test(8);
let epoch_state = Arc::new(EpochState {
epoch: 1,
verifier: storage.get_validator_set().into(),
});
let network = Arc::new(NetworkSender::new(
signer.author(),
consensus_network_client,
self_sender,
epoch_state.verifier.clone(),
));
// TODO: mock
let block_store = build_empty_store(storage.clone(), initial_data);
// TODO: remove
let time_service = Arc::new(SimulatedTimeService::new());
block_on(time_service.sleep(Duration::from_millis(1)));
// TODO: remove
let proposal_generator = ProposalGenerator::new(
signer.author(),
block_store.clone(),
Arc::new(MockPayloadManager::new(None)),
time_service,
Duration::ZERO,
1,
1024,
10,
PipelineBackpressureConfig::new_no_backoff(),
ChainHealthBackoffConfig::new_no_backoff(),
false,
ValidatorTxnConfig::default_disabled(),
);
//
let round_state = create_round_state();
// TODO: have two different nodes, one for proposing, one for accepting a proposal
let proposer_election = Arc::new(RotatingProposer::new(vec![signer.author()], 1));
let (round_manager_tx, _) = aptos_channel::new(QueueStyle::LIFO, 1, None);
// event processor
RoundManager::new(
epoch_state,
Arc::clone(&block_store),
round_state,
proposer_election,
proposal_generator,
Arc::new(Mutex::new(MetricsSafetyRules::new(
Box::new(safety_rules),
storage.clone(),
))),
network,
storage,
OnChainConsensusConfig::default(),
round_manager_tx,
ConsensusConfig::default(),
Features::default(),
)
}
// This functions fuzzes a Proposal protobuffer (not a ConsensusMsg)
pub fn fuzz_proposal(data: &[u8]) {
// create node
let mut round_manager = create_node_for_fuzzing();
let proposal: ProposalMsg = match serde_json::from_slice(data) {
Ok(xx) => xx,
Err(_) => {
if cfg!(test) {
panic!();
}
return;
},
};
let proposal = match proposal.verify_well_formed() {
Ok(_) => proposal,
Err(e) => {
println!("{:?}", e);
if cfg!(test) {
panic!();
}
return;
},
};
block_on(async move {
// TODO: make sure this obtains a vote when testing
// TODO: make sure that if this obtains a vote, it's for round 1, etc.
let _ = round_manager.process_proposal_msg(proposal).await;
});
}
// This test is here so that the fuzzer can be maintained
#[test]
fn test_consensus_proposal_fuzzer() {
// generate a proposal
let proposal = generate_corpus_proposal();
// successfully parse it
fuzz_proposal(&proposal);
}