Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

Commit

Permalink
A different approach to avoid premature VID computation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyospe committed May 20, 2024
1 parent 4dc6959 commit 6198d76
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
28 changes: 23 additions & 5 deletions src/builder_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ use crate::service::GlobalState;
use async_broadcast::broadcast;
use async_broadcast::Receiver as BroadcastReceiver;
use async_broadcast::Sender as BroadcastSender;
use async_compatibility_layer::art::async_sleep;
use async_compatibility_layer::channel::{unbounded, UnboundedSender};
use async_compatibility_layer::channel::{oneshot, unbounded, UnboundedSender};
use async_compatibility_layer::{art::async_sleep, channel::OneShotSender};
use async_compatibility_layer::{art::async_spawn, channel::UnboundedReceiver};
use async_lock::RwLock;
use async_trait::async_trait;
use core::panic;
use futures::StreamExt;

#[cfg(async_executor_impl = "async-std")]
use async_std::task::spawn_blocking;
#[cfg(async_executor_impl = "tokio")]
use tokio::task::{spawn_blocking, JoinHandle};

use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt::Debug;
use std::sync::Arc;
Expand Down Expand Up @@ -74,6 +79,11 @@ pub struct RequestMessage {
pub requested_view_number: u64,
pub response_channel: UnboundedSender<ResponseMessage>,
}
pub enum TriggerStatus {
Start,
Exit,
}

/// Response Message to be put on the response channel
#[derive(Debug)]
pub struct BuildBlockInfo<TYPES: NodeType> {
Expand All @@ -82,6 +92,7 @@ pub struct BuildBlockInfo<TYPES: NodeType> {
pub offered_fee: u64,
pub block_payload: TYPES::BlockPayload,
pub metadata: <<TYPES as NodeType>::BlockPayload as BlockPayload>::Metadata,
pub vid_trigger: OneShotSender<TriggerStatus>,
pub vid_receiver: UnboundedReceiver<(VidCommitment, VidPrecomputeData)>,
}

Expand Down Expand Up @@ -702,14 +713,20 @@ impl<TYPES: NodeType> BuilderProgress<TYPES> for BuilderState<TYPES> {
// stored while processing the DA Proposal
let vid_num_nodes = self.total_nodes.get();

let (trigger_send, trigger_recv) = oneshot();

// spawn a task to calculate the VID commitment, and pass the handle to the global state
// later global state can await on it before replying to the proposer
let (unbounded_sender, unbounded_receiver) = unbounded();
#[allow(unused_must_use)]
async_spawn(async move {
let (vidc, pre_compute_data) =
precompute_vid_commitment(&encoded_txns, vid_num_nodes);
unbounded_sender.send((vidc, pre_compute_data)).await;
if let Ok(TriggerStatus::Start) = trigger_recv.recv().await {
let (vidc, pre_compute_data) = spawn_blocking(move || {
precompute_vid_commitment(&encoded_txns, vid_num_nodes)
})
.await;
unbounded_sender.send((vidc, pre_compute_data)).await;
}
});

tracing::info!(
Expand All @@ -725,6 +742,7 @@ impl<TYPES: NodeType> BuilderProgress<TYPES> for BuilderState<TYPES> {
offered_fee,
block_payload: payload,
metadata,
vid_trigger: trigger_send,
vid_receiver: unbounded_receiver,
})
} else {
Expand Down
12 changes: 10 additions & 2 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ use hotshot_types::{

use crate::builder_state::{
BuildBlockInfo, DaProposalMessage, DecideMessage, QCMessage, TransactionMessage,
TransactionSource,
TransactionSource, TriggerStatus,
};
use crate::builder_state::{MessageType, RequestMessage, ResponseMessage};
use crate::WaitAndKeep;
use anyhow::anyhow;
use async_broadcast::Sender as BroadcastSender;
pub use async_broadcast::{broadcast, RecvError, TryRecvError};
use async_compatibility_layer::{art::async_timeout, channel::unbounded};
use async_compatibility_layer::{
art::async_timeout,
channel::{unbounded, OneShotSender},
};
use async_lock::RwLock;
use async_trait::async_trait;
use committable::{Commitment, Committable};
Expand All @@ -57,6 +60,7 @@ use tide_disco::{method::ReadState, Url};
pub struct BlockInfo<Types: NodeType> {
pub block_payload: Types::BlockPayload,
pub metadata: <<Types as NodeType>::BlockPayload as BlockPayload>::Metadata,
pub vid_trigger: Arc<RwLock<Option<OneShotSender<TriggerStatus>>>>,
pub vid_receiver: Arc<RwLock<WaitAndKeep<(VidCommitment, VidPrecomputeData)>>>,
pub offered_fee: u64,
}
Expand Down Expand Up @@ -161,6 +165,7 @@ impl<Types: NodeType> GlobalState<Types> {
.or_insert_with(|| BlockInfo {
block_payload: build_block_info.block_payload,
metadata: build_block_info.metadata,
vid_trigger: Arc::new(RwLock::new(Some(build_block_info.vid_trigger))),
vid_receiver: Arc::new(RwLock::new(WaitAndKeep::Wait(
build_block_info.vid_receiver,
))),
Expand Down Expand Up @@ -520,6 +525,9 @@ where
.block_hash_to_block
.get(&(block_hash.clone(), view_num))
{
if let Some(trigger_writer) = block_info.vid_trigger.write().await.take() {
trigger_writer.send(TriggerStatus::Start);
}
// sign over the builder commitment, as the proposer can computer it based on provide block_payload
// and the metata data
let response_block_hash = block_info
Expand Down

0 comments on commit 6198d76

Please sign in to comment.