Skip to content

Commit

Permalink
Move a file, fix errors after merging and moving
Browse files Browse the repository at this point in the history
  • Loading branch information
shenkeyao committed Aug 22, 2023
1 parent 8bc17ad commit d4afbfa
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 128 deletions.
60 changes: 0 additions & 60 deletions task-impls/src/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,63 +105,3 @@ pub fn handle_event<TYPES: NodeType, I: NodeImplementation<TYPES>>(
}
(None, state)
}

pub async fn build_api(
node_id: u64,
) -> SystemContextHandle<SequencingTestTypes, SequencingMemoryImpl> {
let builder = TestMetadata::default_multiple_rounds();

let launcher = builder.gen_launcher::<SequencingTestTypes, SequencingMemoryImpl>();

let networks = (launcher.resource_generator.channel_generator)(node_id);
let storage = (launcher.resource_generator.storage)(node_id);
let config = launcher.resource_generator.config.clone();

let initializer = HotShotInitializer::<
SequencingTestTypes,
<SequencingMemoryImpl as NodeImplementation<SequencingTestTypes>>::Leaf,
>::from_genesis(<SequencingMemoryImpl as TestableNodeImplementation<
SequencingTestTypes,
>>::block_genesis())
.unwrap();

let known_nodes = config.known_nodes.clone();
let known_nodes_with_stake = config.known_nodes_with_stake.clone();
let private_key = <BN254Pub as SignatureKey>::generated_from_seed_indexed([0u8; 32], node_id).1;
let public_key = <SequencingTestTypes as NodeType>::SignatureKey::from_private(&private_key);
let quorum_election_config = config.election_config.clone().unwrap_or_else(|| {
<QuorumEx<SequencingTestTypes, SequencingMemoryImpl> as ConsensusExchange<
SequencingTestTypes,
Message<SequencingTestTypes, SequencingMemoryImpl>,
>>::Membership::default_election_config(config.total_nodes.get() as u64)
});

let committee_election_config = config.election_config.clone().unwrap_or_else(|| {
<CommitteeEx<SequencingTestTypes, SequencingMemoryImpl> as ConsensusExchange<
SequencingTestTypes,
Message<SequencingTestTypes, SequencingMemoryImpl>,
>>::Membership::default_election_config(config.total_nodes.get() as u64)
});
let exchanges =
<SequencingMemoryImpl as NodeImplementation<SequencingTestTypes>>::Exchanges::create(
known_nodes_with_stake.clone(),
known_nodes.clone(),
(quorum_election_config, committee_election_config),
networks,
public_key,
public_key.get_stake_table_entry(1u64),
private_key.clone(),
);
SystemContext::init(
public_key,
private_key,
node_id,
config,
storage,
exchanges,
initializer,
NoMetrics::boxed(),
)
.await
.expect("Could not init hotshot")
}
3 changes: 3 additions & 0 deletions testing/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use hotshot_task::{event_stream::ChannelStream, task_impls::HSTWithEvent};

/// Initialized system context handle.
pub mod system_handle;

/// builder
pub mod test_builder;

Expand Down
75 changes: 75 additions & 0 deletions testing/src/system_handle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use crate::node_types::SequencingMemoryImpl;
use crate::node_types::SequencingTestTypes;
use crate::test_builder::TestMetadata;
use hotshot::traits::{NodeImplementation, TestableNodeImplementation};
use hotshot::types::bn254::BN254Pub;
use hotshot::types::SignatureKey;
use hotshot::types::SystemContextHandle;
use hotshot::{HotShotInitializer, SystemContext};
use hotshot_types::message::Message;
use hotshot_types::traits::election::Membership;
use hotshot_types::traits::metrics::NoMetrics;
use hotshot_types::traits::node_implementation::CommitteeEx;
use hotshot_types::traits::node_implementation::ExchangesType;
use hotshot_types::traits::node_implementation::QuorumEx;
use hotshot_types::traits::{election::ConsensusExchange, node_implementation::NodeType};

pub async fn build_system_handle(
node_id: u64,
) -> SystemContextHandle<SequencingTestTypes, SequencingMemoryImpl> {
let builder = TestMetadata::default_multiple_rounds();

let launcher = builder.gen_launcher::<SequencingTestTypes, SequencingMemoryImpl>();

let networks = (launcher.resource_generator.channel_generator)(node_id);
let storage = (launcher.resource_generator.storage)(node_id);
let config = launcher.resource_generator.config.clone();

let initializer = HotShotInitializer::<
SequencingTestTypes,
<SequencingMemoryImpl as NodeImplementation<SequencingTestTypes>>::Leaf,
>::from_genesis(<SequencingMemoryImpl as TestableNodeImplementation<
SequencingTestTypes,
>>::block_genesis())
.unwrap();

let known_nodes = config.known_nodes.clone();
let known_nodes_with_stake = config.known_nodes_with_stake.clone();
let private_key = <BN254Pub as SignatureKey>::generated_from_seed_indexed([0u8; 32], node_id).1;
let public_key = <SequencingTestTypes as NodeType>::SignatureKey::from_private(&private_key);
let quorum_election_config = config.election_config.clone().unwrap_or_else(|| {
<QuorumEx<SequencingTestTypes, SequencingMemoryImpl> as ConsensusExchange<
SequencingTestTypes,
Message<SequencingTestTypes, SequencingMemoryImpl>,
>>::Membership::default_election_config(config.total_nodes.get() as u64)
});

let committee_election_config = config.election_config.clone().unwrap_or_else(|| {
<CommitteeEx<SequencingTestTypes, SequencingMemoryImpl> as ConsensusExchange<
SequencingTestTypes,
Message<SequencingTestTypes, SequencingMemoryImpl>,
>>::Membership::default_election_config(config.total_nodes.get() as u64)
});
let exchanges =
<SequencingMemoryImpl as NodeImplementation<SequencingTestTypes>>::Exchanges::create(
known_nodes_with_stake.clone(),
known_nodes.clone(),
(quorum_election_config, committee_election_config),
networks,
public_key,
public_key.get_stake_table_entry(1u64),
private_key.clone(),
);
SystemContext::init(
public_key,
private_key,
node_id,
config,
storage,
exchanges,
initializer,
NoMetrics::boxed(),
)
.await
.expect("Could not init hotshot")
}
36 changes: 10 additions & 26 deletions testing/tests/consensus_task.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,26 @@
use commit::Committable;
use either::Right;
use hotshot::traits::NodeImplementation;
use hotshot::types::SystemContextHandle;
use hotshot::{certificate::QuorumCertificate, traits::TestableNodeImplementation, SystemContext};

use hotshot::certificate::QuorumCertificate;
use hotshot::tasks::add_consensus_task;

use hotshot::traits::Block;
use hotshot::types::bn254::BN254Pub;
use hotshot::types::SignatureKey;
use hotshot::HotShotInitializer;
use hotshot::types::SystemContextHandle;
use hotshot::HotShotSequencingConsensusApi;
use hotshot_consensus::traits::ConsensusSharedApi;
use hotshot_task::event_stream::ChannelStream;

use hotshot_task_impls::events::SequencingHotShotEvent;
use hotshot_task_impls::harness::run_harness;
use hotshot_testing::node_types::SequencingMemoryImpl;
use hotshot_testing::node_types::SequencingTestTypes;
use hotshot_testing::test_builder::TestMetadata;
use hotshot_types::message::GeneralConsensusMessage;
use hotshot_types::message::Proposal;

use hotshot_types::data::QuorumProposal;
use hotshot_types::data::SequencingLeaf;
use hotshot_types::data::ViewNumber;
use hotshot_types::message::Message;

use hotshot_types::traits::election::Membership;

use hotshot_types::message::GeneralConsensusMessage;
use hotshot_types::message::Proposal;
use hotshot_types::traits::election::QuorumExchangeType;
use hotshot_types::traits::election::SignedCertificate;
use hotshot_types::traits::metrics::NoMetrics;

use hotshot_types::traits::node_implementation::CommitteeEx;
use hotshot_types::traits::node_implementation::ExchangesType;

use hotshot::types::bn254::BN254Pub;
use hotshot_task_impls::harness::run_harness;
use hotshot_types::traits::node_implementation::QuorumEx;
use hotshot_types::traits::signature_key::EncodedSignature;
use hotshot_types::traits::{
election::ConsensusExchange, node_implementation::NodeType, state::ConsensusTime,
Expand Down Expand Up @@ -183,12 +167,12 @@ async fn build_vote(
)]
#[cfg_attr(feature = "async-std-executor", async_std::test)]
async fn test_consensus_task() {
use hotshot_task_impls::harness::build_api;
use hotshot_testing::system_handle::build_system_handle;

async_compatibility_layer::logging::setup_logging();
async_compatibility_layer::logging::setup_backtrace();

let handle = build_api(1).await;
let handle = build_system_handle(1).await;
let (private_key, public_key) = key_pair_for_id(1);

let mut input = Vec::new();
Expand Down Expand Up @@ -223,12 +207,12 @@ async fn test_consensus_task() {
)]
#[cfg_attr(feature = "async-std-executor", async_std::test)]
async fn test_consensus_vote() {
use hotshot_task_impls::harness::build_api;
use hotshot_testing::system_handle::build_system_handle;

async_compatibility_layer::logging::setup_logging();
async_compatibility_layer::logging::setup_backtrace();

let handle = build_api(2).await;
let handle = build_system_handle(2).await;
let (private_key, public_key) = key_pair_for_id(1);

let mut input = Vec::new();
Expand Down
50 changes: 8 additions & 42 deletions testing/tests/da_task.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,13 @@
use commit::Committable;
use hotshot::rand::SeedableRng;
use hotshot::traits::election::static_committee::GeneralStaticCommittee;
use hotshot::traits::election::static_committee::StaticElectionConfig;
use hotshot::traits::election::vrf::JfPubKey;
use hotshot::traits::implementations::MemoryStorage;
use hotshot::types::SignatureKey;
use hotshot::types::SystemContextHandle;
use hotshot::HotShotInitializer;
use hotshot::HotShotSequencingConsensusApi;
use hotshot::{certificate::QuorumCertificate, traits::TestableNodeImplementation, SystemContext};
use hotshot_consensus::traits::ConsensusSharedApi;
use hotshot_task_impls::events::SequencingHotShotEvent;
use hotshot_testing::node_types::SequencingMemoryImpl;
use hotshot_testing::node_types::SequencingTestTypes;
use hotshot_testing::node_types::{
StaticMembership, StaticMemoryDAComm, StaticMemoryQuorumComm, StaticMemoryViewSyncComm,
};
use hotshot_testing::test_builder::TestMetadata;
use hotshot_types::certificate::ViewSyncCertificate;
use hotshot_types::data::DAProposal;
use hotshot_types::data::QuorumProposal;
use hotshot_types::data::SequencingLeaf;
use hotshot_types::data::ViewNumber;
use hotshot_types::message::Message;
use hotshot_types::message::SequencingMessage;
use hotshot_types::traits::election::Membership;
use hotshot_types::traits::metrics::NoMetrics;
use hotshot_types::traits::node_implementation::CommitteeEx;
use hotshot_types::traits::node_implementation::ExchangesType;
use hotshot_types::traits::node_implementation::QuorumEx;
use hotshot_types::traits::node_implementation::SequencingQuorumEx;
use hotshot_types::traits::node_implementation::ViewSyncEx;
use hotshot_types::traits::{
election::ConsensusExchange, node_implementation::NodeType, state::ConsensusTime,
};
use hotshot_types::{certificate::DACertificate, vote::ViewSyncData};
use jf_primitives::signatures::BLSSignatureScheme;
use hotshot_types::traits::{election::ConsensusExchange, state::ConsensusTime};
use std::collections::HashMap;

#[cfg(test)]
Expand All @@ -49,7 +21,8 @@ async fn test_da_task() {
demos::sdemo::{SDemoBlock, SDemoNormalBlock},
tasks::add_da_task,
};
use hotshot_task_impls::harness::{run_harness, build_api};
use hotshot_task_impls::harness::run_harness;
use hotshot_testing::system_handle::build_system_handle;
use hotshot_types::{
message::{CommitteeConsensusMessage, Proposal},
traits::election::CommitteeExchangeType,
Expand All @@ -59,17 +32,13 @@ async fn test_da_task() {
async_compatibility_layer::logging::setup_backtrace();

// Build the API for node 2.
let handle = build_api::<
hotshot_testing::node_types::SequencingTestTypes,
hotshot_testing::node_types::SequencingMemoryImpl,
>(2)
.await;
let handle = build_system_handle(2).await;
let api: HotShotSequencingConsensusApi<SequencingTestTypes, SequencingMemoryImpl> =
HotShotSequencingConsensusApi {
inner: handle.hotshot.inner.clone(),
};
let committee_exchange = api.inner.exchanges.committee_exchange().clone();
let pub_key = api.public_key().clone();
let pub_key = *api.public_key();
let block = SDemoBlock::Normal(SDemoNormalBlock {
previous_state: (),
transactions: Vec::new(),
Expand All @@ -94,14 +63,14 @@ async fn test_da_task() {
input.push(SequencingHotShotEvent::ViewChange(ViewNumber::new(2)));
input.push(SequencingHotShotEvent::DAProposalRecv(
message.clone(),
pub_key.clone(),
pub_key,
));
input.push(SequencingHotShotEvent::Shutdown);

output.insert(SequencingHotShotEvent::ViewChange(ViewNumber::new(1)), 1);
output.insert(SequencingHotShotEvent::SendDABlockData(block), 1);
output.insert(
SequencingHotShotEvent::DAProposalSend(message.clone(), pub_key.clone()),
SequencingHotShotEvent::DAProposalSend(message.clone(), pub_key),
1,
);
if let Ok(Some(vote_token)) = committee_exchange.make_vote_token(ViewNumber::new(2)) {
Expand All @@ -111,10 +80,7 @@ async fn test_da_task() {
output.insert(SequencingHotShotEvent::DAVoteSend(vote), 1);
}
}
output.insert(
SequencingHotShotEvent::DAProposalRecv(message, pub_key.clone()),
1,
);
output.insert(SequencingHotShotEvent::DAProposalRecv(message, pub_key), 1);
output.insert(SequencingHotShotEvent::ViewChange(ViewNumber::new(2)), 1);
output.insert(SequencingHotShotEvent::Shutdown, 1);

Expand Down

0 comments on commit d4afbfa

Please sign in to comment.