Skip to content

Commit

Permalink
[VID Upgrade] add version number to VID interfaces (#4062)
Browse files Browse the repository at this point in the history
* add version information for VID

* fix merge conflict

* advz_scheme & upgrade_lock

* fix

* address comments
  • Loading branch information
mrain authored Jan 29, 2025
1 parent 627365e commit 927084b
Show file tree
Hide file tree
Showing 22 changed files with 285 additions and 125 deletions.
15 changes: 9 additions & 6 deletions crates/hotshot/src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,21 @@ pub async fn add_request_network_task<
pub fn add_response_task<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions>(
handle: &mut SystemContextHandle<TYPES, I, V>,
) {
let state = NetworkResponseState::<TYPES>::new(
let state = NetworkResponseState::<TYPES, V>::new(
handle.hotshot.consensus(),
Arc::clone(&handle.memberships),
handle.public_key().clone(),
handle.private_key().clone(),
handle.hotshot.id,
handle.hotshot.upgrade_lock.clone(),
);
handle.network_registry.register(run_response_task::<TYPES>(
state,
handle.internal_event_stream.1.activate_cloned(),
handle.internal_event_stream.0.clone(),
));
handle
.network_registry
.register(run_response_task::<TYPES, V>(
state,
handle.internal_event_stream.1.activate_cloned(),
handle.internal_event_stream.0.clone(),
));
}

/// Add a task which updates our queue length metric at a set interval
Expand Down
8 changes: 6 additions & 2 deletions crates/task-impls/src/da.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,11 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> DaTaskState<TYP
let num_nodes = membership_reader.total_nodes(epoch_number);
drop(membership_reader);

let version = self.upgrade_lock.version_infallible(view_number).await;

let txns = Arc::clone(&proposal.data.encoded_transactions);
let payload_commitment =
spawn_blocking(move || vid_commitment(&txns, num_nodes)).await;
spawn_blocking(move || vid_commitment::<V>(&txns, num_nodes, version)).await;
let payload_commitment = payload_commitment.unwrap();

self.storage
Expand Down Expand Up @@ -233,12 +235,14 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> DaTaskState<TYP
let pk = self.private_key.clone();
let public_key = self.public_key.clone();
let chan = event_stream.clone();
let upgrade_lock = self.upgrade_lock.clone();
spawn(async move {
Consensus::calculate_and_update_vid(
Consensus::calculate_and_update_vid::<V>(
OuterConsensus::new(Arc::clone(&consensus.inner_consensus)),
view_number,
membership,
&pk,
&upgrade_lock,
)
.await;
if let Some(Some(vid_share)) = consensus
Expand Down
25 changes: 17 additions & 8 deletions crates/task-impls/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ use committable::Committable;
use hotshot_types::{
consensus::{Consensus, LockedConsensusState, OuterConsensus},
data::VidDisperseShare,
message::Proposal,
message::{Proposal, UpgradeLock},
traits::{
election::Membership, network::DataRequest, node_implementation::NodeType,
election::Membership,
network::DataRequest,
node_implementation::{NodeType, Versions},
signature_key::SignatureKey,
},
};
Expand All @@ -29,7 +31,7 @@ const TXNS_TIMEOUT: Duration = Duration::from_millis(100);
/// Task state for the Network Request Task. The task is responsible for handling
/// requests sent to this node by the network. It will validate the sender,
/// parse the request, and try to find the data request in the consensus stores.
pub struct NetworkResponseState<TYPES: NodeType> {
pub struct NetworkResponseState<TYPES: NodeType, V: Versions> {
/// Locked consensus state
consensus: LockedConsensusState<TYPES>,

Expand All @@ -44,23 +46,28 @@ pub struct NetworkResponseState<TYPES: NodeType> {

/// The node's id
id: u64,

/// Lock for a decided upgrade
upgrade_lock: UpgradeLock<TYPES, V>,
}

impl<TYPES: NodeType> NetworkResponseState<TYPES> {
impl<TYPES: NodeType, V: Versions> NetworkResponseState<TYPES, V> {
/// Create the network request state with the info it needs
pub fn new(
consensus: LockedConsensusState<TYPES>,
membership: Arc<RwLock<TYPES::Membership>>,
pub_key: TYPES::SignatureKey,
private_key: <TYPES::SignatureKey as SignatureKey>::PrivateKey,
id: u64,
upgrade_lock: UpgradeLock<TYPES, V>,
) -> Self {
Self {
consensus,
membership,
pub_key,
private_key,
id,
upgrade_lock,
}
}

Expand Down Expand Up @@ -155,22 +162,24 @@ impl<TYPES: NodeType> NetworkResponseState<TYPES> {

drop(consensus_reader);

if Consensus::calculate_and_update_vid(
if Consensus::calculate_and_update_vid::<V>(
OuterConsensus::new(Arc::clone(&self.consensus)),
view,
Arc::clone(&self.membership),
&self.private_key,
&self.upgrade_lock,
)
.await
.is_none()
{
// Sleep in hope we receive txns in the meantime
sleep(TXNS_TIMEOUT).await;
Consensus::calculate_and_update_vid(
Consensus::calculate_and_update_vid::<V>(
OuterConsensus::new(Arc::clone(&self.consensus)),
view,
Arc::clone(&self.membership),
&self.private_key,
&self.upgrade_lock,
)
.await?;
}
Expand Down Expand Up @@ -208,8 +217,8 @@ fn valid_signature<TYPES: NodeType>(
/// Spawn the network response task to handle incoming request for data
/// from other nodes. It will shutdown when it gets `HotshotEvent::Shutdown`
/// on the `event_stream` arg.
pub fn run_response_task<TYPES: NodeType>(
task_state: NetworkResponseState<TYPES>,
pub fn run_response_task<TYPES: NodeType, V: Versions>(
task_state: NetworkResponseState<TYPES, V>,
event_stream: Receiver<Arc<HotShotEvent<TYPES>>>,
sender: Sender<Arc<HotShotEvent<TYPES>>>,
) -> JoinHandle<()> {
Expand Down
6 changes: 4 additions & 2 deletions crates/task-impls/src/vid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,13 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> VidTaskState<TY
);
return None;
}
let vid_disperse = VidDisperse::calculate_vid_disperse(
let vid_disperse = VidDisperse::calculate_vid_disperse::<V>(
&payload,
&Arc::clone(&self.membership),
*view_number,
epoch,
epoch,
&self.upgrade_lock,
)
.await
.ok()?;
Expand Down Expand Up @@ -203,12 +204,13 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> VidTaskState<TY
let payload = Arc::clone(payload);
drop(consensus_reader);

let next_epoch_vid_disperse = VidDisperse::calculate_vid_disperse(
let next_epoch_vid_disperse = VidDisperse::calculate_vid_disperse::<V>(
payload.as_ref(),
&Arc::clone(&self.membership),
proposal_view_number,
target_epoch,
sender_epoch,
&self.upgrade_lock,
)
.await
.ok()?;
Expand Down
11 changes: 7 additions & 4 deletions crates/testing/src/block_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ use hotshot_builder_api::{
use hotshot_types::{
constants::{LEGACY_BUILDER_MODULE, MARKETPLACE_BUILDER_MODULE},
traits::{
block_contents::EncodeBytes, node_implementation::NodeType,
block_contents::EncodeBytes,
node_implementation::{NodeType, Versions},
signature_key::BuilderSignatureKey,
},
};
use tide_disco::{method::ReadState, App, Url};
use tokio::spawn;
use vbs::version::StaticVersionType;
use vbs::version::{StaticVersionType, Version};

use crate::test_builder::BuilderChange;

Expand Down Expand Up @@ -166,11 +167,12 @@ pub fn run_builder_source_0_1<TYPES, Source>(
}

/// Helper function to construct all builder data structures from a list of transactions
async fn build_block<TYPES: NodeType>(
async fn build_block<TYPES: NodeType, V: Versions>(
transactions: Vec<TYPES::Transaction>,
num_storage_nodes: Arc<RwLock<usize>>,
pub_key: TYPES::BuilderSignatureKey,
priv_key: <TYPES::BuilderSignatureKey as BuilderSignatureKey>::BuilderPrivateKey,
version: Version,
) -> BlockEntry<TYPES>
where
<TYPES as NodeType>::InstanceState: Default,
Expand All @@ -185,9 +187,10 @@ where

let commitment = block_payload.builder_commitment(&metadata);

let vid_commitment = hotshot_types::traits::block_contents::vid_commitment(
let vid_commitment = hotshot_types::traits::block_contents::vid_commitment::<V>(
&block_payload.encode(),
*num_storage_nodes.read_arc().await,
version,
);

// Get block size from the encoded payload
Expand Down
19 changes: 13 additions & 6 deletions crates/testing/src/block_builder/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ use hotshot_builder_api::v0_1::{
builder::BuildError,
data_source::BuilderDataSource,
};
use hotshot_example_types::block_types::TestTransaction;
use hotshot_example_types::{block_types::TestTransaction, node_types::TestVersions};
use hotshot_types::{
network::RandomBuilderConfig,
traits::{node_implementation::NodeType, signature_key::BuilderSignatureKey},
traits::{
node_implementation::{NodeType, Versions},
signature_key::BuilderSignatureKey,
},
utils::BuilderCommitment,
vid::VidCommitment,
};
use lru::LruCache;
use rand::{rngs::SmallRng, Rng, RngCore, SeedableRng};
use tide_disco::{method::ReadState, Url};
use tokio::{spawn, time::sleep};
use vbs::version::StaticVersionType;

use super::{
build_block, run_builder_source_0_1, BlockEntry, BuilderTask, TestBuilderImplementation,
Expand Down Expand Up @@ -110,7 +114,7 @@ pub struct RandomBuilderTask<TYPES: NodeType<Transaction = TestTransaction>> {
}

impl<TYPES: NodeType<Transaction = TestTransaction>> RandomBuilderTask<TYPES> {
async fn build_blocks(
async fn build_blocks<V: Versions>(
options: RandomBuilderConfig,
num_nodes: Arc<RwLock<usize>>,
pub_key: <TYPES as NodeType>::BuilderSignatureKey,
Expand All @@ -136,11 +140,14 @@ impl<TYPES: NodeType<Transaction = TestTransaction>> RandomBuilderTask<TYPES> {
})
.collect();

let block = build_block(
// Let new VID scheme ship with Epochs upgrade.
let version = <V as Versions>::Epochs::VERSION;
let block = build_block::<TYPES, V>(
transactions,
num_nodes.clone(),
pub_key.clone(),
priv_key.clone(),
version,
)
.await;

Expand Down Expand Up @@ -171,7 +178,7 @@ where
mut self: Box<Self>,
mut stream: Box<dyn Stream<Item = Event<TYPES>> + std::marker::Unpin + Send + 'static>,
) {
let mut task = Some(spawn(Self::build_blocks(
let mut task = Some(spawn(Self::build_blocks::<TestVersions>(
self.config.clone(),
self.num_nodes.clone(),
self.pub_key.clone(),
Expand All @@ -191,7 +198,7 @@ where
match change {
BuilderChange::Up => {
if task.is_none() {
task = Some(spawn(Self::build_blocks(
task = Some(spawn(Self::build_blocks::<TestVersions>(
self.config.clone(),
self.num_nodes.clone(),
self.pub_key.clone(),
Expand Down
8 changes: 6 additions & 2 deletions crates/testing/src/block_builder/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ use hotshot_builder_api::{
},
v0_99,
};
use hotshot_example_types::node_types::TestVersions;
use hotshot_types::{
bundle::Bundle,
constants::{LEGACY_BUILDER_MODULE, MARKETPLACE_BUILDER_MODULE},
traits::{
block_contents::{BlockHeader, BuilderFee},
node_implementation::NodeType,
node_implementation::{NodeType, Versions},
signature_key::BuilderSignatureKey,
},
utils::BuilderCommitment,
Expand Down Expand Up @@ -246,11 +247,14 @@ where
return Ok(vec![]);
}

let block_entry = build_block(
// Let new VID scheme ships with Epochs upgrade
let version = <TestVersions as Versions>::Epochs::VERSION;
let block_entry = build_block::<TYPES, TestVersions>(
transactions,
self.num_nodes.clone(),
self.pub_key.clone(),
self.priv_key.clone(),
version,
)
.await;

Expand Down
Loading

0 comments on commit 927084b

Please sign in to comment.