Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Associate block with metadata and fix transaction encoding #2045

Merged
merged 14 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions crates/hotshot/examples/infra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,8 @@ pub trait RunDA<
/// get the anchored view
/// Note: sequencing leaf does not have state, so does not return state
async fn initialize_state_and_hotshot(&self) -> SystemContextHandle<TYPES, NODE> {
let genesis_block = TYPES::BlockPayload::genesis();
let initializer =
hotshot::HotShotInitializer::<TYPES, Leaf<TYPES>>::from_genesis(genesis_block)
.expect("Couldn't generate genesis block");
let initializer = hotshot::HotShotInitializer::<TYPES, Leaf<TYPES>>::from_genesis()
.expect("Couldn't generate genesis block");

let config = self.get_config();

Expand Down
6 changes: 3 additions & 3 deletions crates/hotshot/src/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use derivative::Derivative;

use hotshot_signature_key::bn254::BLSPubKey;
use hotshot_types::{
block_impl::{BlockPayloadError, VIDBlockHeader, VIDBlockPayload, VIDTransaction},
block_impl::{BlockError, VIDBlockHeader, VIDBlockPayload, VIDTransaction},
data::{fake_commitment, ViewNumber},
traits::{
election::Membership,
Expand Down Expand Up @@ -60,7 +60,7 @@ impl Default for DemoState {
}

impl State for DemoState {
type Error = BlockPayloadError;
type Error = BlockError;

type BlockHeader = VIDBlockHeader;

Expand Down Expand Up @@ -88,7 +88,7 @@ impl State for DemoState {
view_number: &Self::Time,
) -> Result<Self, Self::Error> {
if !self.validate_block(block_header, view_number) {
return Err(BlockPayloadError::InvalidBlock);
return Err(BlockError::InvalidBlockHeader);
}

Ok(DemoState {
Expand Down
16 changes: 3 additions & 13 deletions crates/hotshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ use hotshot_types::{
};

use hotshot_types::{
block_impl::{VIDBlockHeader, VIDBlockPayload, VIDTransaction},
certificate::ViewSyncCertificate,
consensus::{BlockPayloadStore, Consensus, ConsensusMetricsValue, View, ViewInner, ViewQueue},
data::{DAProposal, Leaf, LeafType, QuorumProposal},
Expand All @@ -77,7 +76,6 @@ use hotshot_types::{
signature_key::SignatureKey,
state::ConsensusTime,
storage::StoredView,
State,
},
vote::ViewSyncData,
HotShotConfig,
Expand Down Expand Up @@ -616,11 +614,7 @@ pub trait HotShotType<TYPES: NodeType, I: NodeImplementation<TYPES>> {

#[async_trait]
impl<
TYPES: NodeType<
BlockHeader = VIDBlockHeader,
BlockPayload = VIDBlockPayload,
Transaction = VIDTransaction,
>,
TYPES: NodeType,
I: NodeImplementation<
TYPES,
Leaf = Leaf<TYPES>,
Expand Down Expand Up @@ -1001,13 +995,9 @@ impl<TYPES: NodeType, LEAF: LeafType<NodeType = TYPES>> HotShotInitializer<TYPES
/// initialize from genesis
/// # Errors
/// If we are unable to apply the genesis block to the default state
pub fn from_genesis(genesis_payload: TYPES::BlockPayload) -> Result<Self, HotShotError<TYPES>> {
let state = TYPES::StateType::initialize();
let time = TYPES::Time::genesis();
let justify_qc = QuorumCertificate2::<TYPES, LEAF>::genesis();

pub fn from_genesis() -> Result<Self, HotShotError<TYPES>> {
Ok(Self {
inner: LEAF::new(time, justify_qc, genesis_payload, state),
inner: LEAF::genesis(),
})
}

Expand Down
9 changes: 5 additions & 4 deletions crates/hotshot/src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use hotshot_task_impls::{
view_sync::{ViewSyncTaskState, ViewSyncTaskStateTypes},
};
use hotshot_types::{
block_impl::{VIDBlockPayload, VIDTransaction},
certificate::ViewSyncCertificate,
data::{Leaf, ProposalType, QuorumProposal},
event::Event,
Expand All @@ -38,6 +37,7 @@ use hotshot_types::{
ViewSyncEx,
},
state::ConsensusTime,
BlockPayload,
},
vote::ViewSyncData,
};
Expand Down Expand Up @@ -227,7 +227,7 @@ where
/// # Panics
/// Is unable to panic. This section here is just to satisfy clippy
pub async fn add_consensus_task<
TYPES: NodeType<BlockPayload = VIDBlockPayload, Transaction = VIDTransaction>,
TYPES: NodeType,
I: NodeImplementation<TYPES, Leaf = Leaf<TYPES>, ConsensusMessage = SequencingMessage<TYPES, I>>,
>(
task_runner: TaskRunner,
Expand Down Expand Up @@ -256,13 +256,14 @@ where
inner: handle.hotshot.inner.clone(),
};
let registry = task_runner.registry.clone();
let (payload, metadata) = <TYPES::BlockPayload as BlockPayload>::genesis();
// build the consensus task
let consensus_state = ConsensusTaskState {
registry: registry.clone(),
consensus,
timeout: handle.hotshot.inner.config.next_view_timeout,
cur_view: TYPES::Time::new(0),
payload_commitment: Some(VIDBlockPayload::genesis().commit()),
payload_commitment_and_metadata: Some((payload.commit(), metadata)),
quorum_exchange: c_api.inner.exchanges.quorum_exchange().clone().into(),
timeout_exchange: c_api.inner.exchanges.timeout_exchange().clone().into(),
api: c_api.clone(),
Expand Down Expand Up @@ -450,7 +451,7 @@ where
/// # Panics
/// Is unable to panic. This section here is just to satisfy clippy
pub async fn add_transaction_task<
TYPES: NodeType<Transaction = VIDTransaction, BlockPayload = VIDBlockPayload>,
TYPES: NodeType,
I: NodeImplementation<TYPES, Leaf = Leaf<TYPES>, ConsensusMessage = SequencingMessage<TYPES, I>>,
>(
task_runner: TaskRunner,
Expand Down
36 changes: 20 additions & 16 deletions crates/task-impls/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use hotshot_task::{
task_impls::{HSTWithEvent, TaskBuilder},
};
use hotshot_types::{
block_impl::{VIDBlockPayload, VIDTransaction},
consensus::{Consensus, View},
data::{Leaf, LeafType, ProposalType, QuorumProposal},
event::{Event, EventType},
Expand Down Expand Up @@ -57,10 +56,13 @@ use tracing::{debug, error, info, instrument};
#[derive(Snafu, Debug)]
pub struct ConsensusTaskError {}

/// Alias for the block payload commitment and the associated metadata.
type CommitmentAndMetadata<PAYLOAD> = (Commitment<PAYLOAD>, <PAYLOAD as BlockPayload>::Metadata);

/// The state for the consensus task. Contains all of the information for the implementation
/// of consensus
pub struct ConsensusTaskState<
TYPES: NodeType<Transaction = VIDTransaction>,
TYPES: NodeType,
I: NodeImplementation<TYPES, Leaf = Leaf<TYPES>, ConsensusMessage = SequencingMessage<TYPES, I>>,
A: ConsensusApi<TYPES, Leaf<TYPES>, I> + 'static,
> where
Expand Down Expand Up @@ -88,8 +90,8 @@ pub struct ConsensusTaskState<
/// View number this view is executing in.
pub cur_view: TYPES::Time,

/// The commitment to the current block payload submitted to DA
pub payload_commitment: Option<Commitment<TYPES::BlockPayload>>,
/// The commitment to the current block payload and its metadata submitted to DA.
pub payload_commitment_and_metadata: Option<CommitmentAndMetadata<TYPES::BlockPayload>>,

/// the quorum exchange
pub quorum_exchange: Arc<QuorumEx<TYPES, I>>,
Expand Down Expand Up @@ -331,7 +333,7 @@ where
}

impl<
TYPES: NodeType<BlockPayload = VIDBlockPayload, Transaction = VIDTransaction>,
TYPES: NodeType,
I: NodeImplementation<
TYPES,
Leaf = Leaf<TYPES>,
Expand All @@ -340,7 +342,6 @@ impl<
A: ConsensusApi<TYPES, Leaf<TYPES>, I> + 'static,
> ConsensusTaskState<TYPES, I, A>
where
TYPES::BlockHeader: BlockHeader<Payload = VIDBlockPayload>,
QuorumEx<TYPES, I>: ConsensusExchange<
TYPES,
Message<TYPES, I>,
Expand Down Expand Up @@ -1237,8 +1238,8 @@ where
let consensus = self.consensus.read().await;
consensus.metrics.number_of_timeouts.add(1);
}
HotShotEvent::SendPayloadCommitment(payload_commitment) => {
self.payload_commitment = Some(payload_commitment);
HotShotEvent::SendPayloadCommitmentAndMetadata(payload_commitment, metadata) => {
self.payload_commitment_and_metadata = Some((payload_commitment, metadata));
}
_ => {}
}
Expand Down Expand Up @@ -1314,12 +1315,16 @@ where
// TODO do some sort of sanity check on the view number that it matches decided
}

if let Some(payload_commitment) = &self.payload_commitment {
if let Some((payload_commitment, metadata)) = &self.payload_commitment_and_metadata {
let leaf = Leaf {
view_number: view,
justify_qc: consensus.high_qc.clone(),
parent_commitment: parent_leaf.commit(),
block_header: TYPES::BlockHeader::new(*payload_commitment, &parent_header),
block_header: TYPES::BlockHeader::new(
*payload_commitment,
metadata.clone(),
&parent_header,
),
block_payload: None,
rejected: vec![],
timestamp: time::OffsetDateTime::now_utc().unix_timestamp_nanos(),
Expand All @@ -1331,7 +1336,7 @@ where
.sign_validating_or_commitment_proposal::<I>(&leaf.commit());
// TODO: DA cert is sent as part of the proposal here, we should split this out so we don't have to wait for it.
let proposal = QuorumProposal {
block_header: TYPES::BlockHeader::new(*payload_commitment, &parent_header),
block_header: leaf.block_header.clone(),
view_number: leaf.view_number,
justify_qc: consensus.high_qc.clone(),
timeout_certificate: timeout_certificate.or_else(|| None),
Expand All @@ -1352,7 +1357,7 @@ where
self.quorum_exchange.public_key().clone(),
))
.await;
self.payload_commitment = None;
self.payload_commitment_and_metadata = None;
return true;
}
debug!("Self block was None");
Expand All @@ -1361,7 +1366,7 @@ where
}

impl<
TYPES: NodeType<Transaction = VIDTransaction>,
TYPES: NodeType,
I: NodeImplementation<
TYPES,
Leaf = Leaf<TYPES>,
Expand Down Expand Up @@ -1405,7 +1410,7 @@ pub type ConsensusTaskTypes<TYPES, I, A> = HSTWithEvent<

/// Event handle for consensus
pub async fn sequencing_consensus_handle<
TYPES: NodeType<BlockPayload = VIDBlockPayload, Transaction = VIDTransaction>,
TYPES: NodeType,
I: NodeImplementation<TYPES, Leaf = Leaf<TYPES>, ConsensusMessage = SequencingMessage<TYPES, I>>,
A: ConsensusApi<TYPES, Leaf<TYPES>, I> + 'static,
>(
Expand All @@ -1416,7 +1421,6 @@ pub async fn sequencing_consensus_handle<
ConsensusTaskState<TYPES, I, A>,
)
where
TYPES::BlockHeader: BlockHeader<Payload = VIDBlockPayload>,
QuorumEx<TYPES, I>: ConsensusExchange<
TYPES,
Message<TYPES, I>,
Expand Down Expand Up @@ -1451,7 +1455,7 @@ pub fn consensus_event_filter<TYPES: NodeType, I: NodeImplementation<TYPES>>(
| HotShotEvent::QCFormed(_)
| HotShotEvent::DACRecv(_)
| HotShotEvent::ViewChange(_)
| HotShotEvent::SendPayloadCommitment(_)
| HotShotEvent::SendPayloadCommitmentAndMetadata(_, _)
| HotShotEvent::Timeout(_)
| HotShotEvent::TimeoutVoteRecv(_)
| HotShotEvent::Shutdown,
Expand Down
13 changes: 8 additions & 5 deletions crates/task-impls/src/da.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,18 +394,18 @@ where

return None;
}
HotShotEvent::BlockReady(block, view) => {
HotShotEvent::BlockReady(payload, metadata, view) => {
self.committee_exchange
.network()
.inject_consensus_info(ConsensusIntentEvent::CancelPollForTransactions(*view))
.await;

let payload_commitment = block.commit();
let payload_commitment = payload.commit();
let signature = self
.committee_exchange
.sign_da_proposal(&payload_commitment);
let data: DAProposal<TYPES> = DAProposal {
block_payload: block.clone(),
block_payload: payload.clone(),
jbearer marked this conversation as resolved.
Show resolved Hide resolved
// Upon entering a new view we want to send a DA Proposal for the next view -> Is it always the case that this is cur_view + 1?
view_number: view,
};
Expand All @@ -414,7 +414,10 @@ where
let message = Proposal { data, signature };

self.event_stream
.publish(HotShotEvent::SendPayloadCommitment(payload_commitment))
.publish(HotShotEvent::SendPayloadCommitmentAndMetadata(
payload_commitment,
metadata,
))
.await;
self.event_stream
.publish(HotShotEvent::DAProposalSend(
Expand Down Expand Up @@ -449,7 +452,7 @@ where
HotShotEvent::DAProposalRecv(_, _)
| HotShotEvent::DAVoteRecv(_)
| HotShotEvent::Shutdown
| HotShotEvent::BlockReady(_, _)
| HotShotEvent::BlockReady(_, _, _)
| HotShotEvent::Timeout(_)
| HotShotEvent::ViewChange(_)
)
Expand Down
22 changes: 16 additions & 6 deletions crates/task-impls/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ use hotshot_types::{
DACertificate2, QuorumCertificate2, TimeoutCertificate2, VIDCertificate2,
},
simple_vote::{DAVote2, QuorumVote, TimeoutVote2, VIDVote2},
traits::node_implementation::{
CommitteeMembership, NodeImplementation, NodeType, QuorumMembership, QuorumProposalType,
VIDMembership, ViewSyncProposalType,
traits::{
node_implementation::{
CommitteeMembership, NodeImplementation, NodeType, QuorumMembership,
QuorumProposalType, VIDMembership, ViewSyncProposalType,
},
BlockPayload,
},
vote::ViewSyncVote,
};
Expand Down Expand Up @@ -70,10 +73,17 @@ pub enum HotShotEvent<TYPES: NodeType, I: NodeImplementation<TYPES>> {
TransactionsRecv(Vec<TYPES::Transaction>),
/// Send transactions to the network
TransactionSend(TYPES::Transaction, TYPES::SignatureKey),
/// Event to send block payload commitment from DA leader to the quorum; internal event only
SendPayloadCommitment(Commitment<TYPES::BlockPayload>),
/// Event to send block payload commitment and metadata from DA leader to the quorum; internal event only
SendPayloadCommitmentAndMetadata(
Commitment<TYPES::BlockPayload>,
<TYPES::BlockPayload as BlockPayload>::Metadata,
),
/// Event when the transactions task has a block formed
BlockReady(TYPES::BlockPayload, TYPES::Time),
BlockReady(
TYPES::BlockPayload,
<TYPES::BlockPayload as BlockPayload>::Metadata,
TYPES::Time,
),
/// Event when consensus decided on a leaf
LeafDecided(Vec<I::Leaf>),
/// Send VID shares to VID storage nodes; emitted by the DA leader
Expand Down
Loading
Loading