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

Commit

Permalink
Humane IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinI committed Jul 26, 2024
1 parent 40ea9ff commit 77fefc9
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 200 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ tide-disco = "0.9"
tokio = "1"
tracing = "0.1"
vbs = "0.1"
lru = "0.12.3"
hex = "0.4.3"

[dev-dependencies]
hotshot-example-types = { git = "https://github.com/EspressoSystems/HotShot.git", tag = "rc-0.5.64" }
59 changes: 29 additions & 30 deletions src/builder_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use hotshot_types::{

use committable::{Commitment, Committable};

use crate::service::{GlobalState, ReceivedTransaction};
use crate::{
service::{GlobalState, ReceivedTransaction},
BlockId, BuilderStateId,
};
use async_broadcast::broadcast;
use async_broadcast::Receiver as BroadcastReceiver;
use async_broadcast::Sender as BroadcastSender;
Expand Down Expand Up @@ -65,9 +68,8 @@ pub struct QCMessage<TYPES: NodeType> {
}
/// Request Message to be put on the request channel
#[derive(Clone, Debug)]
pub struct RequestMessage {
pub requested_vid_commitment: VidCommitment,
pub requested_view_number: u64,
pub struct RequestMessage<Types: NodeType> {
pub state_id: BuilderStateId<Types>,
pub response_channel: UnboundedSender<ResponseMessage>,
}
pub enum TriggerStatus {
Expand All @@ -78,7 +80,7 @@ pub enum TriggerStatus {
/// Response Message to be put on the response channel
#[derive(Debug)]
pub struct BuildBlockInfo<TYPES: NodeType> {
pub builder_hash: BuilderCommitment,
pub id: BlockId<TYPES>,
pub block_size: u64,
pub offered_fee: u64,
pub block_payload: TYPES::BlockPayload,
Expand Down Expand Up @@ -457,8 +459,10 @@ impl<TYPES: NodeType> BuilderState<TYPES> {

// register the spawned builder state to spawned_builder_states in the global state
self.global_state.write_arc().await.register_builder_state(
self.built_from_proposed_block.vid_commitment,
self.built_from_proposed_block.view_number,
BuilderStateId {
parent_commitment: self.built_from_proposed_block.vid_commitment,
view: self.built_from_proposed_block.view_number,
},
req_sender,
);

Expand All @@ -470,8 +474,7 @@ impl<TYPES: NodeType> BuilderState<TYPES> {
fields(builder_built_from_proposed_block = %self.built_from_proposed_block))]
async fn build_block(
&mut self,
matching_vid: VidCommitment,
requested_view_number: TYPES::Time,
state_id: BuilderStateId<TYPES>,
) -> Option<BuildBlockInfo<TYPES>> {
let timeout_after = Instant::now() + self.maximize_txn_capture_timeout;
let sleep_interval = self.maximize_txn_capture_timeout / 10;
Expand Down Expand Up @@ -501,9 +504,9 @@ impl<TYPES: NodeType> BuilderState<TYPES> {

// insert the recently built block into the builder commitments
self.builder_commitments.insert((
matching_vid,
state_id.parent_commitment,
builder_hash.clone(),
requested_view_number,
state_id.view,
));

let encoded_txns: Vec<u8> = payload.encode().to_vec();
Expand Down Expand Up @@ -541,7 +544,10 @@ impl<TYPES: NodeType> BuilderState<TYPES> {
);

Some(BuildBlockInfo {
builder_hash,
id: BlockId {
view: self.built_from_proposed_block.view_number,
hash: builder_hash,
},
block_size,
offered_fee,
block_payload: payload,
Expand All @@ -555,46 +561,39 @@ impl<TYPES: NodeType> BuilderState<TYPES> {
}
}

async fn process_block_request(&mut self, req: RequestMessage) {
let requested_vid_commitment = req.requested_vid_commitment;
let requested_view_number =
<<TYPES as NodeType>::Time as ConsensusTime>::new(req.requested_view_number);
async fn process_block_request(&mut self, req: RequestMessage<TYPES>) {
// If a spawned clone is active then it will handle the request, otherwise the highest view num builder will handle
if (requested_vid_commitment == self.built_from_proposed_block.vid_commitment
&& requested_view_number == self.built_from_proposed_block.view_number)
if (req.state_id.parent_commitment == self.built_from_proposed_block.vid_commitment
&& req.state_id.view == self.built_from_proposed_block.view_number)
|| (self.built_from_proposed_block.view_number.u64()
== self
.global_state
.read_arc()
.await
.highest_view_num_builder_id
.1
.view
.u64())
{
tracing::info!(
"Request handled by builder with view {:?} for (parent {:?}, view_num: {:?})",
"Request for parent {} handled by builder with view {:?}",
req.state_id,
self.built_from_proposed_block.view_number,
requested_vid_commitment,
requested_view_number
);
let response = self
.build_block(requested_vid_commitment, requested_view_number)
.await;
let response = self.build_block(req.state_id.clone()).await;

match response {
Some(response) => {
// form the response message
let response_msg = ResponseMessage {
builder_hash: response.builder_hash.clone(),
builder_hash: response.id.hash.clone(),
block_size: response.block_size,
offered_fee: response.offered_fee,
};

let builder_hash = response.builder_hash.clone();
let builder_hash = response.id.hash.clone();
self.global_state.write_arc().await.update_global_state(
req.state_id.clone(),
response,
requested_vid_commitment,
requested_view_number,
response_msg.clone(),
);

Expand Down Expand Up @@ -736,7 +735,7 @@ pub enum MessageType<TYPES: NodeType> {
DecideMessage(DecideMessage<TYPES>),
DaProposalMessage(DaProposalMessage<TYPES>),
QCMessage(QCMessage<TYPES>),
RequestMessage(RequestMessage),
RequestMessage(RequestMessage<TYPES>),
}

#[allow(clippy::too_many_arguments)]
Expand Down
35 changes: 34 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ pub mod service;
pub mod testing;

use async_compatibility_layer::channel::UnboundedReceiver;
use hotshot_builder_api::v0_2::builder::BuildError;
use hotshot_builder_api::v0_1::builder::BuildError;
use hotshot_types::{
traits::node_implementation::NodeType, utils::BuilderCommitment, vid::VidCommitment,
};

#[derive(Debug)]
pub enum WaitAndKeep<T> {
Keep(T),
Expand All @@ -45,3 +49,32 @@ impl<T: Clone> WaitAndKeep<T> {
}
}
}

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct BlockId<Types: NodeType> {
hash: BuilderCommitment,
view: Types::Time,
}

impl<Types: NodeType> std::fmt::Display for BlockId<Types> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Block({}@{})",
hex::encode(self.hash.as_ref()),
*self.view
)
}
}

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct BuilderStateId<Types: NodeType> {
parent_commitment: VidCommitment,
view: Types::Time,
}

impl<Types: NodeType> std::fmt::Display for BuilderStateId<Types> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "BuilderState({}@{})", self.parent_commitment, *self.view)
}
}
Loading

0 comments on commit 77fefc9

Please sign in to comment.