Skip to content

Commit

Permalink
Move consensus constructor into the test_utils crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Eligioo committed Aug 24, 2024
1 parent c23fbfe commit d4b192a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 31 deletions.
33 changes: 33 additions & 0 deletions test-utils/src/consensus.rs
Original file line number Diff line number Diff line change
@@ -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<N: Network + TestNetwork>(
blockchain: Arc<RwLock<Blockchain>>,
net: Arc<N>,
) -> Consensus<N> {
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)
}
1 change: 1 addition & 0 deletions test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 2 additions & 31 deletions validator/src/proposal_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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};
Expand All @@ -588,28 +581,6 @@ mod test {
r#macro::ProposalTopic,
};

/// Given a blockchain and a network creates an instance of Consensus.
async fn consensus<N: NetworkInterface + TestNetwork>(
blockchain: Arc<RwLock<Blockchain>>,
net: Arc<N>,
) -> Consensus<N> {
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<MockNetwork>,
TemporaryBlockProducer,
Expand Down

0 comments on commit d4b192a

Please sign in to comment.