1use std::{path::PathBuf, sync::Arc};
6
7use fastcrypto::traits::KeyPair;
8use iota_archival::reader::ArchiveReaderBalancer;
9use iota_config::{
10 ExecutionCacheConfig,
11 certificate_deny_config::CertificateDenyConfig,
12 genesis::Genesis,
13 node::{
14 AuthorityOverloadConfig, AuthorityStorePruningConfig, DBCheckpointConfig,
15 ExpensiveSafetyCheckConfig,
16 },
17 transaction_deny_config::TransactionDenyConfig,
18};
19use iota_network::randomness;
20use iota_protocol_config::{Chain, ProtocolConfig};
21use iota_swarm_config::{genesis_config::AccountConfig, network_config::NetworkConfig};
22use iota_types::{
23 base_types::AuthorityName, crypto::AuthorityKeyPair, digests::ChainIdentifier,
24 executable_transaction::VerifiedExecutableTransaction, iota_system_state::IotaSystemStateTrait,
25 object::Object, supported_protocol_versions::SupportedProtocolVersions,
26 transaction::VerifiedTransaction,
27};
28use prometheus::Registry;
29
30use super::{backpressure::BackpressureManager, epoch_start_configuration::EpochFlag};
31use crate::{
32 authority::{
33 AuthorityState, AuthorityStore,
34 authority_per_epoch_store::AuthorityPerEpochStore,
35 authority_store_pruner::ObjectsCompactionFilter,
36 authority_store_tables::{
37 AuthorityPerpetualTables, AuthorityPerpetualTablesOptions, AuthorityPrunerTables,
38 },
39 epoch_start_configuration::EpochStartConfiguration,
40 },
41 checkpoints::CheckpointStore,
42 epoch::{
43 committee_store::CommitteeStore, epoch_metrics::EpochMetrics, randomness::RandomnessManager,
44 },
45 execution_cache::build_execution_cache,
46 grpc_indexes::{GRPC_INDEXES_DIR, GrpcIndexesStore},
47 jsonrpc_index::IndexStore,
48 mock_consensus::{ConsensusMode, MockConsensusClient},
49 module_cache_metrics::ResolverMetrics,
50 signature_verifier::SignatureVerifierMetrics,
51};
52
53#[derive(Default, Clone)]
54pub struct TestAuthorityBuilder<'a> {
55 store_base_path: Option<PathBuf>,
56 store: Option<Arc<AuthorityStore>>,
57 transaction_deny_config: Option<TransactionDenyConfig>,
58 certificate_deny_config: Option<CertificateDenyConfig>,
59 protocol_config: Option<ProtocolConfig>,
60 reference_gas_price: Option<u64>,
61 node_keypair: Option<&'a AuthorityKeyPair>,
62 genesis: Option<&'a Genesis>,
63 starting_objects: Option<&'a [Object]>,
64 expensive_safety_checks: Option<ExpensiveSafetyCheckConfig>,
65 disable_indexer: bool,
66 accounts: Vec<AccountConfig>,
67 insert_genesis_checkpoint: bool,
70 authority_overload_config: Option<AuthorityOverloadConfig>,
71 cache_config: Option<ExecutionCacheConfig>,
72 disable_execute_genesis_transactions: bool,
73 chain_override: Option<Chain>,
74}
75
76impl<'a> TestAuthorityBuilder<'a> {
77 pub fn new() -> Self {
78 Self::default()
79 }
80
81 pub fn with_store_base_path(mut self, path: PathBuf) -> Self {
82 assert!(self.store_base_path.replace(path).is_none());
83 self
84 }
85
86 pub fn with_starting_objects(mut self, objects: &'a [Object]) -> Self {
87 assert!(self.starting_objects.replace(objects).is_none());
88 self
89 }
90
91 pub fn with_store(mut self, store: Arc<AuthorityStore>) -> Self {
92 assert!(self.store.replace(store).is_none());
93 self
94 }
95
96 pub fn with_transaction_deny_config(mut self, config: TransactionDenyConfig) -> Self {
97 assert!(self.transaction_deny_config.replace(config).is_none());
98 self
99 }
100
101 pub fn with_certificate_deny_config(mut self, config: CertificateDenyConfig) -> Self {
102 assert!(self.certificate_deny_config.replace(config).is_none());
103 self
104 }
105
106 pub fn with_protocol_config(mut self, config: ProtocolConfig) -> Self {
107 assert!(self.protocol_config.replace(config).is_none());
108 self
109 }
110
111 pub fn with_reference_gas_price(mut self, reference_gas_price: u64) -> Self {
112 assert!(self.genesis.is_none());
115 assert!(
116 self.reference_gas_price
117 .replace(reference_gas_price)
118 .is_none()
119 );
120 self
121 }
122
123 pub fn with_genesis_and_keypair(
124 mut self,
125 genesis: &'a Genesis,
126 keypair: &'a AuthorityKeyPair,
127 ) -> Self {
128 assert!(self.genesis.replace(genesis).is_none());
129 assert!(self.node_keypair.replace(keypair).is_none());
130 self
131 }
132
133 pub fn with_keypair(mut self, keypair: &'a AuthorityKeyPair) -> Self {
134 assert!(self.node_keypair.replace(keypair).is_none());
135 self
136 }
137
138 pub fn with_network_config(self, config: &'a NetworkConfig, node_idx: usize) -> Self {
141 self.with_genesis_and_keypair(
142 &config.genesis,
143 config.validator_configs()[node_idx].authority_key_pair(),
144 )
145 }
146
147 pub fn disable_indexer(mut self) -> Self {
148 self.disable_indexer = true;
149 self
150 }
151
152 pub fn insert_genesis_checkpoint(mut self) -> Self {
153 self.insert_genesis_checkpoint = true;
154 self
155 }
156
157 pub fn with_expensive_safety_checks(mut self, config: ExpensiveSafetyCheckConfig) -> Self {
158 assert!(self.expensive_safety_checks.replace(config).is_none());
159 self
160 }
161
162 pub fn with_accounts(mut self, accounts: Vec<AccountConfig>) -> Self {
163 self.accounts = accounts;
164 self
165 }
166
167 pub fn with_authority_overload_config(mut self, config: AuthorityOverloadConfig) -> Self {
168 assert!(self.authority_overload_config.replace(config).is_none());
169 self
170 }
171
172 pub fn with_cache_config(mut self, config: ExecutionCacheConfig) -> Self {
173 self.cache_config = Some(config);
174 self
175 }
176
177 pub fn disable_execute_genesis_transactions(mut self) -> Self {
178 self.disable_execute_genesis_transactions = true;
179 self
180 }
181
182 pub fn with_chain_override(mut self, chain: Chain) -> Self {
183 self.chain_override = Some(chain);
184 self
185 }
186
187 pub async fn build(self) -> Arc<AuthorityState> {
188 let mut local_network_config_builder =
189 iota_swarm_config::network_config_builder::ConfigBuilder::new_with_temp_dir()
190 .with_accounts(self.accounts)
191 .with_reference_gas_price(self.reference_gas_price.unwrap_or(500));
192 if let Some(protocol_config) = &self.protocol_config {
193 local_network_config_builder =
194 local_network_config_builder.with_protocol_version(protocol_config.version);
195 }
196
197 let local_network_config = local_network_config_builder.build();
198 let genesis = &self.genesis.unwrap_or(&local_network_config.genesis);
199 let genesis_committee = genesis.committee().unwrap();
200 let storage_dir = self
201 .store_base_path
202 .unwrap_or_else(|| iota_common::tempdir().keep());
203 let mut config = local_network_config.validator_configs()[0].clone();
204 let registry = Registry::new();
205 let mut pruner_db = None;
206 if config
207 .authority_store_pruning_config
208 .enable_compaction_filter
209 {
210 pruner_db = Some(Arc::new(AuthorityPrunerTables::open(
211 &storage_dir.join("store"),
212 )));
213 }
214 let compaction_filter = pruner_db
215 .clone()
216 .map(|db| ObjectsCompactionFilter::new(db, ®istry));
217
218 let authority_store = match self.store {
219 Some(store) => store,
220 None => {
221 let perpetual_tables_options = AuthorityPerpetualTablesOptions {
222 compaction_filter,
223 ..Default::default()
224 };
225 let perpetual_tables = Arc::new(AuthorityPerpetualTables::open(
226 &storage_dir.join("store"),
227 Some(perpetual_tables_options),
228 ));
229 AuthorityStore::open_with_committee_for_testing(
231 perpetual_tables,
232 &genesis_committee,
233 genesis,
234 )
235 .await
236 .unwrap()
237 }
238 };
239 if let Some(cache_config) = self.cache_config {
240 config.execution_cache_config = cache_config;
241 }
242
243 let keypair = if let Some(keypair) = self.node_keypair {
244 keypair.copy()
245 } else {
246 config.authority_key_pair().copy()
247 };
248
249 let secret = Arc::pin(keypair.copy());
250 let name: AuthorityName = secret.public().into();
251 let cache_metrics = Arc::new(ResolverMetrics::new(®istry));
252 let signature_verifier_metrics = SignatureVerifierMetrics::new(®istry);
253 let _guard = self
256 .protocol_config
257 .map(|config| ProtocolConfig::apply_overrides_for_testing(move |_, _| config.clone()));
258 let epoch_flags = EpochFlag::default_flags_for_new_epoch(&config);
259 let epoch_start_configuration = EpochStartConfiguration::new(
260 genesis.iota_system_object().into_epoch_start_state(),
261 *genesis.checkpoint().digest(),
262 &genesis.objects(),
263 epoch_flags,
264 )
265 .unwrap();
266 let expensive_safety_checks = self.expensive_safety_checks.unwrap_or_default();
267
268 let checkpoint_store = CheckpointStore::new(&storage_dir.join("checkpoints"));
269 let backpressure_manager =
270 BackpressureManager::new_from_checkpoint_store(&checkpoint_store);
271
272 let cache_traits = build_execution_cache(
273 &config.execution_cache_config,
274 ®istry,
275 &authority_store,
276 backpressure_manager.clone(),
277 );
278
279 let chain_id = ChainIdentifier::from(*genesis.checkpoint().digest());
280 let chain = match self.chain_override {
281 Some(chain) => chain,
282 None => chain_id.chain(),
283 };
284
285 let epoch_store = AuthorityPerEpochStore::new(
286 name,
287 Arc::new(genesis_committee.clone()),
288 &storage_dir.join("store"),
289 None,
290 EpochMetrics::new(®istry),
291 epoch_start_configuration,
292 cache_traits.backing_package_store.clone(),
293 cache_metrics,
294 signature_verifier_metrics,
295 &expensive_safety_checks,
296 (chain_id, chain),
297 checkpoint_store
298 .get_highest_executed_checkpoint_seq_number()
299 .unwrap()
300 .unwrap_or(0),
301 )
302 .expect("failed to create authority per epoch store");
303 let committee_store = Arc::new(CommitteeStore::new(
304 storage_dir.join("epochs"),
305 &genesis_committee,
306 None,
307 ));
308
309 if self.insert_genesis_checkpoint {
310 checkpoint_store.insert_genesis_checkpoint(
311 genesis.checkpoint(),
312 genesis.checkpoint_contents().clone(),
313 &epoch_store,
314 );
315 }
316 let index_store = if self.disable_indexer {
317 None
318 } else {
319 Some(Arc::new(IndexStore::new(
320 storage_dir.join("indexes"),
321 ®istry,
322 epoch_store
323 .protocol_config()
324 .max_move_identifier_len_as_option(),
325 )))
326 };
327 let grpc_indexes_store = if self.disable_indexer {
328 None
329 } else {
330 Some(Arc::new(
331 GrpcIndexesStore::new(
332 storage_dir.join(GRPC_INDEXES_DIR),
333 Arc::clone(&authority_store),
334 &checkpoint_store,
335 )
336 .await,
337 ))
338 };
339
340 let transaction_deny_config = self.transaction_deny_config.unwrap_or_default();
341 let certificate_deny_config = self.certificate_deny_config.unwrap_or_default();
342 let authority_overload_config = self.authority_overload_config.unwrap_or_default();
343 let pruning_config = AuthorityStorePruningConfig::default();
344
345 config.transaction_deny_config = transaction_deny_config;
346 config.certificate_deny_config = certificate_deny_config;
347 config.authority_overload_config = authority_overload_config;
348 config.authority_store_pruning_config = pruning_config;
349
350 let chain_identifier = ChainIdentifier::from(*genesis.checkpoint().digest());
351 let policy_config = config.policy_config.clone();
352 let firewall_config = config.firewall_config.clone();
353
354 let state = AuthorityState::new(
355 name,
356 secret,
357 SupportedProtocolVersions::SYSTEM_DEFAULT,
358 authority_store,
359 cache_traits,
360 epoch_store.clone(),
361 committee_store,
362 index_store,
363 grpc_indexes_store,
364 checkpoint_store,
365 ®istry,
366 genesis.objects(),
367 &DBCheckpointConfig::default(),
368 config.clone(),
369 ArchiveReaderBalancer::default(),
370 None,
371 chain_identifier,
372 pruner_db,
373 None,
374 policy_config,
375 firewall_config,
376 )
377 .await;
378
379 let consensus_client = Box::new(MockConsensusClient::new(
381 Arc::downgrade(&state),
382 ConsensusMode::Noop,
383 ));
384 let randomness_manager = RandomnessManager::try_new(
385 Arc::downgrade(&epoch_store),
386 consensus_client,
387 randomness::Handle::new_stub(),
388 &keypair,
389 )
390 .await;
391 if let Some(randomness_manager) = randomness_manager {
392 epoch_store
395 .set_randomness_manager(randomness_manager)
396 .await
397 .unwrap();
398 }
399
400 if !self.disable_execute_genesis_transactions {
401 state
407 .try_execute_immediately(
408 &VerifiedExecutableTransaction::new_from_checkpoint(
409 VerifiedTransaction::new_unchecked(genesis.transaction().clone()),
410 genesis.epoch(),
411 genesis.checkpoint().sequence_number,
412 ),
413 None,
414 &state.epoch_store_for_testing(),
415 )
416 .unwrap();
417
418 let batch = state
419 .get_cache_commit()
420 .build_db_batch(epoch_store.epoch(), &[*genesis.transaction().digest()]);
421
422 state.get_cache_commit().commit_transaction_outputs(
423 epoch_store.epoch(),
424 batch,
425 &[*genesis.transaction().digest()],
426 );
427 }
428
429 if let Some(starting_objects) = self.starting_objects {
435 state
436 .insert_objects_unsafe_for_testing_only(starting_objects)
437 .await;
438 };
439 state
440 }
441}