From d4b192a08b1a5b86c0e8b7ca6769a45659a70714 Mon Sep 17 00:00:00 2001 From: Stefan Date: Sat, 24 Aug 2024 15:00:02 +0200 Subject: [PATCH] Move consensus constructor into the `test_utils` crate --- test-utils/src/consensus.rs | 33 ++++++++++++++++++++++++++++++++ test-utils/src/lib.rs | 1 + validator/src/proposal_buffer.rs | 33 ++------------------------------ 3 files changed, 36 insertions(+), 31 deletions(-) create mode 100644 test-utils/src/consensus.rs diff --git a/test-utils/src/consensus.rs b/test-utils/src/consensus.rs new file mode 100644 index 0000000000..b268f3c983 --- /dev/null +++ b/test-utils/src/consensus.rs @@ -0,0 +1,33 @@ +use std::sync::Arc; + +use nimiq_blockchain::Blockchain; +use nimiq_blockchain_proxy::BlockchainProxy; +use nimiq_bls::cache::PublicKeyCache; +use nimiq_consensus::{sync::syncer_proxy::SyncerProxy, Consensus}; +use nimiq_network_interface::network::Network; +use nimiq_zkp_component::ZKPComponent; +use parking_lot::{Mutex, RwLock}; + +use crate::test_network::TestNetwork; + +/// Given a blockchain and a network creates an instance of Consensus. +pub async fn consensus( + blockchain: Arc>, + net: Arc, +) -> Consensus { + let blockchain_proxy = BlockchainProxy::from(&blockchain); + + let zkp_proxy = ZKPComponent::new(blockchain_proxy.clone(), Arc::clone(&net), None) + .await + .proxy(); + + let syncer = SyncerProxy::new_history( + blockchain_proxy.clone(), + Arc::clone(&net), + Arc::new(Mutex::new(PublicKeyCache::new(10))), + net.subscribe_events(), + ) + .await; + + Consensus::new(blockchain_proxy, net, syncer, 0, zkp_proxy) +} diff --git a/test-utils/src/lib.rs b/test-utils/src/lib.rs index 8ef1e0d6cb..17b5c13164 100644 --- a/test-utils/src/lib.rs +++ b/test-utils/src/lib.rs @@ -2,6 +2,7 @@ pub mod accounts_revert; pub mod block_production; pub mod blockchain; pub mod blockchain_with_rng; +pub mod consensus; pub mod mock_node; pub mod node; pub mod test_custom_block; diff --git a/validator/src/proposal_buffer.rs b/validator/src/proposal_buffer.rs index 0f05627584..0f8e1bf78d 100644 --- a/validator/src/proposal_buffer.rs +++ b/validator/src/proposal_buffer.rs @@ -558,12 +558,7 @@ mod test { use futures::{FutureExt, StreamExt}; use nimiq_block::MacroHeader; - use nimiq_blockchain::Blockchain; - use nimiq_blockchain_proxy::BlockchainProxy; - use nimiq_bls::cache::PublicKeyCache; - use nimiq_consensus::{ - sync::syncer_proxy::SyncerProxy, Consensus, ConsensusEvent, ConsensusProxy, - }; + use nimiq_consensus::{ConsensusEvent, ConsensusProxy}; use nimiq_keys::{KeyPair as SchnorrKeyPair, PrivateKey as SchnorrPrivateKey}; use nimiq_network_interface::network::Network as NetworkInterface; use nimiq_network_mock::{MockHub, MockNetwork}; @@ -573,13 +568,11 @@ mod test { use nimiq_test_log::test; use nimiq_test_utils::{ block_production::{TemporaryBlockProducer, SIGNING_KEY}, - test_network::TestNetwork, + consensus::consensus, }; use nimiq_time::{sleep, timeout}; use nimiq_utils::spawn; use nimiq_validator_network::network_impl::ValidatorNetworkImpl; - use nimiq_zkp_component::ZKPComponent; - use parking_lot::{Mutex, RwLock}; use tokio::select; use super::{ProposalAndPubsubId, ProposalBuffer}; @@ -588,28 +581,6 @@ mod test { r#macro::ProposalTopic, }; - /// Given a blockchain and a network creates an instance of Consensus. - async fn consensus( - blockchain: Arc>, - net: Arc, - ) -> Consensus { - let blockchain_proxy = BlockchainProxy::from(&blockchain); - - let zkp_proxy = ZKPComponent::new(blockchain_proxy.clone(), Arc::clone(&net), None) - .await - .proxy(); - - let syncer = SyncerProxy::new_history( - blockchain_proxy.clone(), - Arc::clone(&net), - Arc::new(Mutex::new(PublicKeyCache::new(10))), - net.subscribe_events(), - ) - .await; - - Consensus::new(blockchain_proxy, net, syncer, 0, zkp_proxy) - } - async fn setup() -> ( ConsensusProxy, TemporaryBlockProducer,