Skip to content

Commit

Permalink
Move versioning out of constants and into NodeType (#3391)
Browse files Browse the repository at this point in the history
  • Loading branch information
ss-es authored Jun 27, 2024
1 parent 07fcc10 commit 0c0a4dd
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 101 deletions.
7 changes: 7 additions & 0 deletions crates/example-types/src/node_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use hotshot_types::{
traits::node_implementation::NodeType,
};
use serde::{Deserialize, Serialize};
use vbs::version::StaticVersion;

use crate::{
auction_results_provider_types::TestAuctionResultsProvider,
Expand All @@ -34,6 +35,12 @@ use crate::{
/// to select our traits
pub struct TestTypes;
impl NodeType for TestTypes {
type Base = StaticVersion<0, 1>;
type Upgrade = StaticVersion<0, 2>;
const UPGRADE_HASH: [u8; 32] = [
1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0,
];
type Time = ViewNumber;
type BlockHeader = TestBlockHeader;
type BlockPayload = TestBlockPayload;
Expand Down
6 changes: 3 additions & 3 deletions crates/hotshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use hotshot_task_impls::{events::HotShotEvent, helpers::broadcast_event, network
pub use hotshot_types::error::HotShotError;
use hotshot_types::{
consensus::{Consensus, ConsensusMetricsValue, View, ViewInner},
constants::{Base, EVENT_CHANNEL_SIZE, EXTERNAL_EVENT_CHANNEL_SIZE},
constants::{EVENT_CHANNEL_SIZE, EXTERNAL_EVENT_CHANNEL_SIZE},
data::{Leaf, QuorumProposal},
event::{EventType, LeafInfo},
message::{DataMessage, Message, MessageKind, Proposal, VersionedMessage},
Expand Down Expand Up @@ -258,7 +258,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>> SystemContext<TYPES, I> {
);

let consensus = Arc::new(RwLock::new(consensus));
let version = Arc::new(RwLock::new(Base::VERSION));
let version = Arc::new(RwLock::new(TYPES::Base::VERSION));

// This makes it so we won't block on broadcasting if there is not a receiver
// Our own copy of the receiver is inactive so it doesn't count.
Expand Down Expand Up @@ -613,7 +613,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>> SystemContext<TYPES, I> {
)
.await;
add_network_event_task(&mut handle, network, vid_membership, network::vid_filter).await;
add_consensus_tasks::<TYPES, I, Base>(&mut handle).await;
add_consensus_tasks::<TYPES, I>(&mut handle).await;
handle
}
}
Expand Down
15 changes: 8 additions & 7 deletions crates/hotshot/src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,19 @@ pub async fn add_network_event_task<
}

/// Adds consensus-related tasks to a `SystemContextHandle`.
pub async fn add_consensus_tasks<
TYPES: NodeType,
I: NodeImplementation<TYPES>,
VERSION: StaticVersionType + 'static,
>(
pub async fn add_consensus_tasks<TYPES: NodeType, I: NodeImplementation<TYPES>>(
handle: &mut SystemContextHandle<TYPES, I>,
) {
handle.add_task(ViewSyncTaskState::<TYPES, I>::create_from(handle).await);
handle.add_task(VidTaskState::<TYPES, I>::create_from(handle).await);
handle.add_task(DaTaskState::<TYPES, I>::create_from(handle).await);
handle.add_task(TransactionTaskState::<TYPES, I, VERSION>::create_from(handle).await);
handle.add_task(UpgradeTaskState::<TYPES, I>::create_from(handle).await);
handle.add_task(TransactionTaskState::<TYPES, I>::create_from(handle).await);

// only spawn the upgrade task if we are actually configured to perform an upgrade.
if TYPES::Base::VERSION < TYPES::Upgrade::VERSION {
handle.add_task(UpgradeTaskState::<TYPES, I>::create_from(handle).await);
}

{
#![cfg(not(feature = "dependency-tasks"))]
handle.add_task(ConsensusTaskState::<TYPES, I>::create_from(handle).await);
Expand Down
9 changes: 3 additions & 6 deletions crates/hotshot/src/tasks/task_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use hotshot_types::traits::{
consensus_api::ConsensusApi,
node_implementation::{ConsensusTime, NodeImplementation, NodeType},
};
use vbs::version::StaticVersionType;

use crate::types::SystemContextHandle;

Expand Down Expand Up @@ -163,12 +162,10 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>> CreateTaskState<TYPES, I>
}

#[async_trait]
impl<TYPES: NodeType, I: NodeImplementation<TYPES>, Ver: StaticVersionType>
CreateTaskState<TYPES, I> for TransactionTaskState<TYPES, I, Ver>
impl<TYPES: NodeType, I: NodeImplementation<TYPES>> CreateTaskState<TYPES, I>
for TransactionTaskState<TYPES, I>
{
async fn create_from(
handle: &SystemContextHandle<TYPES, I>,
) -> TransactionTaskState<TYPES, I, Ver> {
async fn create_from(handle: &SystemContextHandle<TYPES, I>) -> TransactionTaskState<TYPES, I> {
TransactionTaskState {
builder_timeout: handle.builder_timeout(),
output_event_stream: handle.hotshot.external_event_stream.0.clone(),
Expand Down
3 changes: 1 addition & 2 deletions crates/hotshot/src/types/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ use async_std::task::JoinHandle;
use futures::Stream;
use hotshot_task::task::{ConsensusTaskRegistry, NetworkTaskRegistry, Task, TaskState};
use hotshot_task_impls::{events::HotShotEvent, helpers::broadcast_event};
use hotshot_types::traits::network::ConnectedNetwork;
use hotshot_types::{
consensus::Consensus,
data::Leaf,
error::HotShotError,
traits::{election::Membership, node_implementation::NodeType},
traits::{election::Membership, network::ConnectedNetwork, node_implementation::NodeType},
};
#[cfg(async_executor_impl = "tokio")]
use tokio::task::JoinHandle;
Expand Down
21 changes: 12 additions & 9 deletions crates/orchestrator/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use std::{net::SocketAddr, time::Duration};
use async_compatibility_layer::art::async_sleep;
use clap::Parser;
use futures::{Future, FutureExt};
use hotshot_types::{
constants::Base, traits::signature_key::SignatureKey, PeerConfig, ValidatorConfig,
};
use hotshot_types::{traits::signature_key::SignatureKey, PeerConfig, ValidatorConfig};
use libp2p::{Multiaddr, PeerId};
use surf_disco::{error::ClientError, Client};
use tide_disco::Url;
Expand Down Expand Up @@ -223,8 +221,10 @@ impl OrchestratorClient {
});

// Serialize our (possible) libp2p-specific data
let request_body =
vbs::Serializer::<Base>::serialize(&(libp2p_address, libp2p_public_key))?;
let request_body = vbs::Serializer::<OrchestratorVersion>::serialize(&(
libp2p_address,
libp2p_public_key,
))?;

let identity = |client: Client<ClientError, OrchestratorVersion>| {
// We need to clone here to move it into the closure
Expand Down Expand Up @@ -316,7 +316,7 @@ impl OrchestratorClient {
/// if unable to serialize `address`
pub async fn post_builder_addresses(&self, addresses: Vec<Url>) {
let send_builder_f = |client: Client<ClientError, OrchestratorVersion>| {
let request_body = vbs::Serializer::<Base>::serialize(&addresses)
let request_body = vbs::Serializer::<OrchestratorVersion>::serialize(&addresses)
.expect("Failed to serialize request");

async move {
Expand Down Expand Up @@ -382,9 +382,12 @@ impl OrchestratorClient {
let da_requested: bool = validator_config.is_da;

// Serialize our (possible) libp2p-specific data
let request_body =
vbs::Serializer::<Base>::serialize(&(pubkey, libp2p_address, libp2p_public_key))
.expect("failed to serialize request");
let request_body = vbs::Serializer::<OrchestratorVersion>::serialize(&(
pubkey,
libp2p_address,
libp2p_public_key,
))
.expect("failed to serialize request");

// register our public key with the orchestrator
let (node_index, is_da): (u64, bool) = loop {
Expand Down
12 changes: 7 additions & 5 deletions crates/orchestrator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use client::{BenchResults, BenchResultsDownloadConfig};
use config::BuilderType;
use csv::Writer;
use futures::{stream::FuturesUnordered, FutureExt, StreamExt};
use hotshot_types::{constants::Base, traits::signature_key::SignatureKey, PeerConfig};
use hotshot_types::{traits::signature_key::SignatureKey, PeerConfig};
use libp2p::{
identity::{
ed25519::{Keypair as EdKeypair, SecretKey},
Expand Down Expand Up @@ -582,7 +582,7 @@ where

// Decode the libp2p data so we can add to our bootstrap nodes (if supplied)
let Ok((libp2p_address, libp2p_public_key)) =
vbs::Serializer::<Base>::deserialize(&body_bytes)
vbs::Serializer::<OrchestratorVersion>::deserialize(&body_bytes)
else {
return Err(ServerError {
status: tide_disco::StatusCode::BAD_REQUEST,
Expand Down Expand Up @@ -614,7 +614,7 @@ where

// Decode the libp2p data so we can add to our bootstrap nodes (if supplied)
let Ok((mut pubkey, libp2p_address, libp2p_public_key)) =
vbs::Serializer::<Base>::deserialize(&body_bytes)
vbs::Serializer::<OrchestratorVersion>::deserialize(&body_bytes)
else {
return Err(ServerError {
status: tide_disco::StatusCode::BAD_REQUEST,
Expand Down Expand Up @@ -662,7 +662,9 @@ where
let mut body_bytes = req.body_bytes();
body_bytes.drain(..12);

let Ok(urls) = vbs::Serializer::<Base>::deserialize::<Vec<Url>>(&body_bytes) else {
let Ok(urls) =
vbs::Serializer::<OrchestratorVersion>::deserialize::<Vec<Url>>(&body_bytes)
else {
return Err(ServerError {
status: tide_disco::StatusCode::BAD_REQUEST,
message: "Malformed body".to_string(),
Expand All @@ -672,7 +674,7 @@ where
let mut futures = urls
.into_iter()
.map(|url| async {
let client: surf_disco::Client<ServerError, Base> =
let client: surf_disco::Client<ServerError, OrchestratorVersion> =
surf_disco::client::Client::builder(url.clone()).build();
if client.connect(Some(Duration::from_secs(2))).await {
Some(url)
Expand Down
17 changes: 4 additions & 13 deletions crates/task-impls/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use hotshot_types::{
vid::VidCommitment,
};
use tracing::{debug, error, instrument, warn};
use vbs::version::StaticVersionType;

use crate::{
builder::BuilderClient,
Expand Down Expand Up @@ -65,11 +64,7 @@ pub struct BuilderResponses<TYPES: NodeType> {
}

/// Tracks state of a Transaction task
pub struct TransactionTaskState<
TYPES: NodeType,
I: NodeImplementation<TYPES>,
Ver: StaticVersionType,
> {
pub struct TransactionTaskState<TYPES: NodeType, I: NodeImplementation<TYPES>> {
/// The state's api
pub builder_timeout: Duration,

Expand All @@ -89,7 +84,7 @@ pub struct TransactionTaskState<
pub membership: Arc<TYPES::Membership>,

/// Builder API client
pub builder_clients: Vec<BuilderClient<TYPES, Ver>>,
pub builder_clients: Vec<BuilderClient<TYPES, TYPES::Base>>,

/// This Nodes Public Key
pub public_key: TYPES::SignatureKey,
Expand All @@ -103,9 +98,7 @@ pub struct TransactionTaskState<
pub decided_upgrade_certificate: Option<UpgradeCertificate<TYPES>>,
}

impl<TYPES: NodeType, I: NodeImplementation<TYPES>, Ver: StaticVersionType>
TransactionTaskState<TYPES, I, Ver>
{
impl<TYPES: NodeType, I: NodeImplementation<TYPES>> TransactionTaskState<TYPES, I> {
/// main task event handler
#[instrument(skip_all, fields(id = self.id, view = *self.cur_view), name = "Transaction task", level = "error")]
pub async fn handle(
Expand Down Expand Up @@ -524,9 +517,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, Ver: StaticVersionType>

#[async_trait]
/// task state implementation for Transactions Task
impl<TYPES: NodeType, I: NodeImplementation<TYPES>, Ver: StaticVersionType + 'static> TaskState
for TransactionTaskState<TYPES, I, Ver>
{
impl<TYPES: NodeType, I: NodeImplementation<TYPES>> TaskState for TransactionTaskState<TYPES, I> {
type Event = HotShotEvent<TYPES>;

async fn handle_event(
Expand Down
13 changes: 6 additions & 7 deletions crates/task-impls/src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use async_trait::async_trait;
use committable::Committable;
use hotshot_task::task::TaskState;
use hotshot_types::{
constants::{Base, Upgrade, UPGRADE_HASH},
data::UpgradeProposal,
event::{Event, EventType},
message::Proposal,
Expand Down Expand Up @@ -92,9 +91,9 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>> UpgradeTaskState<TYPES, I> {
}

// If the proposal does not match our upgrade target, we immediately exit.
if proposal.data.upgrade_proposal.new_version_hash != UPGRADE_HASH
|| proposal.data.upgrade_proposal.old_version != Base::VERSION
|| proposal.data.upgrade_proposal.new_version != Upgrade::VERSION
if proposal.data.upgrade_proposal.new_version_hash != TYPES::UPGRADE_HASH
|| proposal.data.upgrade_proposal.old_version != TYPES::Base::VERSION
|| proposal.data.upgrade_proposal.new_version != TYPES::Upgrade::VERSION
{
return None;
}
Expand Down Expand Up @@ -228,9 +227,9 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>> UpgradeTaskState<TYPES, I> {
&& self.quorum_membership.leader(view + 5) == self.public_key
{
let upgrade_proposal_data = UpgradeProposalData {
old_version: Base::VERSION,
new_version: Upgrade::VERSION,
new_version_hash: UPGRADE_HASH.to_vec(),
old_version: TYPES::Base::VERSION,
new_version: TYPES::Upgrade::VERSION,
new_version_hash: TYPES::UPGRADE_HASH.to_vec(),
// We schedule the upgrade to begin 15 views in the future
old_version_last_view: TYPES::Time::new(*view + 15),
// and end 20 views in the future
Expand Down
22 changes: 10 additions & 12 deletions crates/testing/src/block_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ use hotshot_builder_api::{
builder::{Error, Options},
data_source::BuilderDataSource,
};
use hotshot_types::{
constants::Base,
traits::{
block_contents::{precompute_vid_commitment, EncodeBytes},
node_implementation::NodeType,
signature_key::BuilderSignatureKey,
},
use hotshot_types::traits::{
block_contents::{precompute_vid_commitment, EncodeBytes},
node_implementation::NodeType,
signature_key::BuilderSignatureKey,
};
use tide_disco::{method::ReadState, App, Url};
use vbs::version::StaticVersionType;
Expand Down Expand Up @@ -75,14 +72,15 @@ pub fn run_builder_source<TYPES, Source>(
{
async_spawn(async move {
let start_builder = |url: Url, source: Source| -> _ {
let builder_api = hotshot_builder_api::builder::define_api::<Source, TYPES, Base>(
&Options::default(),
)
.expect("Failed to construct the builder API");
let builder_api =
hotshot_builder_api::builder::define_api::<Source, TYPES, TYPES::Base>(
&Options::default(),
)
.expect("Failed to construct the builder API");
let mut app: App<Source, Error> = App::with_state(source);
app.register_module("block_info", builder_api)
.expect("Failed to register the builder API");
async_spawn(app.serve(url, Base::instance()))
async_spawn(app.serve(url, TYPES::Base::instance()))
};

let mut handle = Some(start_builder(url.clone(), source.clone()));
Expand Down
16 changes: 8 additions & 8 deletions crates/testing/src/block_builder/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use hotshot_builder_api::{
data_source::BuilderDataSource,
};
use hotshot_types::{
constants::Base,
traits::{
block_contents::BlockHeader, node_implementation::NodeType,
signature_key::BuilderSignatureKey,
Expand Down Expand Up @@ -238,16 +237,17 @@ impl<TYPES: NodeType> SimpleBuilderSource<TYPES> {
where
<TYPES as NodeType>::InstanceState: Default,
{
let builder_api =
hotshot_builder_api::builder::define_api::<SimpleBuilderSource<TYPES>, TYPES, Base>(
&Options::default(),
)
.expect("Failed to construct the builder API");
let builder_api = hotshot_builder_api::builder::define_api::<
SimpleBuilderSource<TYPES>,
TYPES,
TYPES::Base,
>(&Options::default())
.expect("Failed to construct the builder API");
let mut app: App<SimpleBuilderSource<TYPES>, Error> = App::with_state(self);
app.register_module::<Error, Base>("block_info", builder_api)
app.register_module::<Error, TYPES::Base>("block_info", builder_api)
.expect("Failed to register the builder API");

async_spawn(app.serve(url, Base::instance()));
async_spawn(app.serve(url, TYPES::Base::instance()));
}
}

Expand Down
3 changes: 1 addition & 2 deletions crates/testing/tests/tests_1/block_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use hotshot_testing::block_builder::{
BuilderTask, RandomBuilderImplementation, TestBuilderImplementation,
};
use hotshot_types::{
constants::Base,
traits::{
block_contents::vid_commitment, node_implementation::NodeType, signature_key::SignatureKey,
BlockPayload,
Expand Down Expand Up @@ -46,7 +45,7 @@ async fn test_random_block_builder() {

let builder_started = Instant::now();

let client: BuilderClient<TestTypes, Base> = BuilderClient::new(api_url);
let client: BuilderClient<TestTypes, <TestTypes as NodeType>::Base> = BuilderClient::new(api_url);
assert!(client.connect(Duration::from_millis(100)).await);

let (pub_key, private_key) =
Expand Down
Loading

0 comments on commit 0c0a4dd

Please sign in to comment.