Skip to content

Commit

Permalink
Merge pull request #1937 from EspressoSystems/keyao/block-header
Browse files Browse the repository at this point in the history
Implement block header
  • Loading branch information
shenkeyao authored Oct 31, 2023
2 parents 8ceb257 + 4232453 commit e78b628
Show file tree
Hide file tree
Showing 31 changed files with 623 additions and 639 deletions.
30 changes: 21 additions & 9 deletions crates/hotshot/examples/infra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use hotshot_orchestrator::{
config::{NetworkConfig, NetworkConfigFile, WebServerConfig},
};
use hotshot_task::task::FilterEvent;
use hotshot_types::traits::election::VIDExchange;
use hotshot_types::{block_impl::VIDBlockHeader, traits::election::VIDExchange};
use hotshot_types::{
block_impl::{VIDBlockPayload, VIDTransaction},
certificate::ViewSyncCertificate,
Expand Down Expand Up @@ -231,7 +231,7 @@ pub trait RunDA<
>,
> where
<TYPES as NodeType>::StateType: TestableState,
<TYPES as NodeType>::BlockType: TestableBlock,
<TYPES as NodeType>::BlockPayload: TestableBlock,
Leaf<TYPES>: TestableLeaf,
Self: Sync,
SystemContext<TYPES, NODE>: HotShotType<TYPES, NODE>,
Expand All @@ -250,7 +250,7 @@ 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::BlockType::genesis();
let genesis_block = TYPES::BlockPayload::genesis();
let initializer =
hotshot::HotShotInitializer::<TYPES, Leaf<TYPES>>::from_genesis(genesis_block)
.expect("Couldn't generate genesis block");
Expand Down Expand Up @@ -456,7 +456,11 @@ pub struct WebServerDARun<

#[async_trait]
impl<
TYPES: NodeType<Transaction = VIDTransaction, BlockType = VIDBlockPayload>,
TYPES: NodeType<
Transaction = VIDTransaction,
BlockPayload = VIDBlockPayload,
BlockHeader = VIDBlockHeader,
>,
MEMBERSHIP: Membership<TYPES> + Debug,
NODE: NodeImplementation<
TYPES,
Expand Down Expand Up @@ -507,7 +511,7 @@ impl<
> for WebServerDARun<TYPES, NODE, MEMBERSHIP>
where
<TYPES as NodeType>::StateType: TestableState,
<TYPES as NodeType>::BlockType: TestableBlock,
<TYPES as NodeType>::BlockPayload: TestableBlock,
Leaf<TYPES>: TestableLeaf,
Self: Sync,
{
Expand Down Expand Up @@ -624,7 +628,11 @@ pub struct Libp2pDARun<TYPES: NodeType, I: NodeImplementation<TYPES>, MEMBERSHIP

#[async_trait]
impl<
TYPES: NodeType<Transaction = VIDTransaction, BlockType = VIDBlockPayload>,
TYPES: NodeType<
Transaction = VIDTransaction,
BlockPayload = VIDBlockPayload,
BlockHeader = VIDBlockHeader,
>,
MEMBERSHIP: Membership<TYPES> + Debug,
NODE: NodeImplementation<
TYPES,
Expand Down Expand Up @@ -675,7 +683,7 @@ impl<
> for Libp2pDARun<TYPES, NODE, MEMBERSHIP>
where
<TYPES as NodeType>::StateType: TestableState,
<TYPES as NodeType>::BlockType: TestableBlock,
<TYPES as NodeType>::BlockPayload: TestableBlock,
Leaf<TYPES>: TestableLeaf,
Self: Sync,
{
Expand Down Expand Up @@ -853,7 +861,11 @@ where

/// Main entry point for validators
pub async fn main_entry_point<
TYPES: NodeType<Transaction = VIDTransaction, BlockType = VIDBlockPayload>,
TYPES: NodeType<
Transaction = VIDTransaction,
BlockPayload = VIDBlockPayload,
BlockHeader = VIDBlockHeader,
>,
MEMBERSHIP: Membership<TYPES> + Debug,
DANETWORK: CommunicationChannel<TYPES, Message<TYPES, NODE>, MEMBERSHIP> + Debug,
QUORUMNETWORK: CommunicationChannel<TYPES, Message<TYPES, NODE>, MEMBERSHIP> + Debug,
Expand Down Expand Up @@ -891,7 +903,7 @@ pub async fn main_entry_point<
args: ValidatorArgs,
) where
<TYPES as NodeType>::StateType: TestableState,
<TYPES as NodeType>::BlockType: TestableBlock,
<TYPES as NodeType>::BlockPayload: TestableBlock,
Leaf<TYPES>: TestableLeaf,
{
setup_logging();
Expand Down
48 changes: 17 additions & 31 deletions crates/hotshot/src/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
use crate::traits::election::static_committee::{StaticElectionConfig, StaticVoteToken};
use commit::{Commitment, Committable};
use derivative::Derivative;
use either::Either;
use hotshot_signature_key::bn254::BLSPubKey;
use hotshot_types::{
block_impl::{BlockPayloadError, VIDBlockPayload, VIDTransaction},
block_impl::{BlockPayloadError, VIDBlockHeader, VIDBlockPayload, VIDTransaction},
certificate::{AssembledSignature, QuorumCertificate},
data::{fake_commitment, genesis_proposer_id, random_commitment, Leaf, LeafType, ViewNumber},
data::{fake_commitment, random_commitment, LeafType, ViewNumber},
traits::{
election::Membership,
node_implementation::NodeType,
Expand Down Expand Up @@ -63,24 +62,32 @@ impl Default for DemoState {
impl State for DemoState {
type Error = BlockPayloadError;

type BlockType = VIDBlockPayload;
type BlockHeader = VIDBlockHeader;

type BlockPayload = VIDBlockPayload;

type Time = ViewNumber;

fn validate_block(&self, _block: &Self::BlockType, view_number: &Self::Time) -> bool {
fn validate_block(&self, _block_header: &Self::BlockHeader, view_number: &Self::Time) -> bool {
if view_number == &ViewNumber::genesis() {
&self.view_number == view_number
} else {
self.view_number < *view_number
}
}

fn initialize() -> Self {
let mut state = Self::default();
state.block_height += 1;
state
}

fn append(
&self,
block: &Self::BlockType,
block_header: &Self::BlockHeader,
view_number: &Self::Time,
) -> Result<Self, Self::Error> {
if !self.validate_block(block, view_number) {
if !self.validate_block(block_header, view_number) {
return Err(BlockPayloadError::InvalidBlock);
}

Expand All @@ -99,7 +106,7 @@ impl TestableState for DemoState {
_state: Option<&Self>,
_rng: &mut dyn rand::RngCore,
padding: u64,
) -> <Self::BlockType as BlockPayload>::Transaction {
) -> <Self::BlockPayload as BlockPayload>::Transaction {
/// clippy appeasement for `RANDOM_TX_BASE_SIZE`
const RANDOM_TX_BASE_SIZE: usize = 8;
VIDTransaction(vec![0; RANDOM_TX_BASE_SIZE + (padding as usize)])
Expand All @@ -123,7 +130,8 @@ pub struct DemoTypes;

impl NodeType for DemoTypes {
type Time = ViewNumber;
type BlockType = VIDBlockPayload;
type BlockHeader = VIDBlockHeader;
type BlockPayload = VIDBlockPayload;
type SignatureKey = BLSPubKey;
type VoteTokenType = StaticVoteToken<Self::SignatureKey>;
type Transaction = VIDTransaction;
Expand Down Expand Up @@ -174,31 +182,9 @@ pub fn random_quorum_certificate<TYPES: NodeType, LEAF: LeafType<NodeType = TYPE
rng: &mut dyn rand::RngCore,
) -> QuorumCertificate<TYPES, Commitment<LEAF>> {
QuorumCertificate {
// block_commitment: random_commitment(rng),
leaf_commitment: random_commitment(rng),
view_number: TYPES::Time::new(rng.gen()),
signatures: AssembledSignature::Genesis(),
is_genesis: rng.gen(),
}
}

/// Provides a random [`Leaf`]
pub fn random_leaf<TYPES: NodeType>(
deltas: Either<TYPES::BlockType, Commitment<TYPES::BlockType>>,
rng: &mut dyn rand::RngCore,
) -> Leaf<TYPES> {
let justify_qc = random_quorum_certificate(rng);
// let state = TYPES::StateType::default()
// .append(&deltas, &TYPES::Time::new(42))
// .unwrap_or_default();
Leaf {
view_number: justify_qc.view_number,
height: rng.next_u64(),
justify_qc,
parent_commitment: random_commitment(rng),
deltas,
rejected: Vec::new(),
timestamp: time::OffsetDateTime::now_utc().unix_timestamp_nanos(),
proposer_id: genesis_proposer_id(),
}
}
34 changes: 17 additions & 17 deletions crates/hotshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ use hotshot_types::{
};

use hotshot_types::{
block_impl::{VIDBlockPayload, VIDTransaction},
block_impl::{VIDBlockHeader, VIDBlockPayload, VIDTransaction},
certificate::{DACertificate, ViewSyncCertificate},
consensus::{BlockStore, Consensus, ConsensusMetricsValue, View, ViewInner, ViewQueue},
data::{DAProposal, DeltasType, Leaf, LeafType, QuorumProposal},
consensus::{BlockPayloadStore, Consensus, ConsensusMetricsValue, View, ViewInner, ViewQueue},
data::{DAProposal, Leaf, LeafType, QuorumProposal},
error::StorageSnafu,
message::{
ConsensusMessageType, DataMessage, InternalTrigger, Message, MessageKind,
Expand Down Expand Up @@ -204,10 +204,10 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>> SystemContext<TYPES, I> {
);

let mut saved_leaves = HashMap::new();
let mut saved_blocks = BlockStore::default();
let mut saved_block_payloads = BlockPayloadStore::default();
saved_leaves.insert(anchored_leaf.commit(), anchored_leaf.clone());
if let Ok(block) = anchored_leaf.get_deltas().try_resolve() {
saved_blocks.insert(block);
if let Some(payload) = anchored_leaf.get_block_payload() {
saved_block_payloads.insert(payload);
}

let start_view = anchored_leaf.get_view_number();
Expand All @@ -217,7 +217,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>> SystemContext<TYPES, I> {
cur_view: start_view,
last_decided_view: anchored_leaf.get_view_number(),
saved_leaves,
saved_blocks,
saved_block_payloads,
// TODO this is incorrect
// https://github.com/EspressoSystems/HotShot/issues/560
locked_view: anchored_leaf.get_view_number(),
Expand Down Expand Up @@ -628,7 +628,11 @@ pub trait HotShotType<TYPES: NodeType, I: NodeImplementation<TYPES>> {

#[async_trait]
impl<
TYPES: NodeType<Transaction = VIDTransaction, BlockType = VIDBlockPayload>,
TYPES: NodeType<
BlockHeader = VIDBlockHeader,
BlockPayload = VIDBlockPayload,
Transaction = VIDTransaction,
>,
I: NodeImplementation<
TYPES,
Leaf = Leaf<TYPES>,
Expand All @@ -650,7 +654,7 @@ where
Message<TYPES, I>,
Proposal = DAProposal<TYPES>,
Certificate = DACertificate<TYPES>,
Commitment = Commitment<TYPES::BlockType>,
Commitment = Commitment<TYPES::BlockPayload>,
Membership = MEMBERSHIP,
> + 'static,
ViewSyncEx<TYPES, I>: ConsensusExchange<
Expand All @@ -666,7 +670,7 @@ where
Message<TYPES, I>,
Proposal = VidDisperse<TYPES>,
Certificate = VIDCertificate<TYPES>,
Commitment = Commitment<TYPES::BlockType>,
Commitment = Commitment<TYPES::BlockPayload>,
Membership = MEMBERSHIP,
> + 'static,
TimeoutEx<TYPES, I>: ConsensusExchange<
Expand Down Expand Up @@ -1013,17 +1017,13 @@ 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_block: TYPES::BlockType) -> Result<Self, HotShotError<TYPES>> {
let state = TYPES::StateType::default()
.append(&genesis_block, &TYPES::Time::new(0))
.map_err(|err| HotShotError::Misc {
context: err.to_string(),
})?;
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 = QuorumCertificate::<TYPES, Commitment<LEAF>>::genesis();

Ok(Self {
inner: LEAF::new(time, justify_qc, genesis_block, state),
inner: LEAF::new(time, justify_qc, genesis_payload, state),
})
}

Expand Down
14 changes: 7 additions & 7 deletions crates/hotshot/src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
async_spawn, types::SystemContextHandle, DACertificate, HotShotConsensusApi, QuorumCertificate,
};
use async_compatibility_layer::art::async_sleep;
use commit::{Commitment, CommitmentBounds};
use commit::{Commitment, CommitmentBounds, Committable};
use futures::FutureExt;
use hotshot_task::{
boxed_sync,
Expand Down Expand Up @@ -243,7 +243,7 @@ where
/// # Panics
/// Is unable to panic. This section here is just to satisfy clippy
pub async fn add_consensus_task<
TYPES: NodeType<BlockType = VIDBlockPayload>,
TYPES: NodeType<BlockPayload = VIDBlockPayload, Transaction = VIDTransaction>,
I: NodeImplementation<TYPES, Leaf = Leaf<TYPES>, ConsensusMessage = SequencingMessage<TYPES, I>>,
>(
task_runner: TaskRunner,
Expand All @@ -263,7 +263,7 @@ where
TYPES,
Message<TYPES, I>,
Certificate = DACertificate<TYPES>,
Commitment = Commitment<TYPES::BlockType>,
Commitment = Commitment<TYPES::BlockPayload>,
>,
TimeoutEx<TYPES, I>: ConsensusExchange<
TYPES,
Expand All @@ -284,7 +284,7 @@ where
consensus,
timeout: handle.hotshot.inner.config.next_view_timeout,
cur_view: TYPES::Time::new(0),
block: Some(VIDBlockPayload::genesis()),
payload_commitment: Some(VIDBlockPayload::genesis().commit()),
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 @@ -363,7 +363,7 @@ where
TYPES,
Message<TYPES, I>,
Certificate = VIDCertificate<TYPES>,
Commitment = Commitment<TYPES::BlockType>,
Commitment = Commitment<TYPES::BlockPayload>,
>,
{
// build the vid task
Expand Down Expand Up @@ -429,7 +429,7 @@ where
TYPES,
Message<TYPES, I>,
Certificate = DACertificate<TYPES>,
Commitment = Commitment<TYPES::BlockType>,
Commitment = Commitment<TYPES::BlockPayload>,
>,
{
// build the da task
Expand Down Expand Up @@ -481,7 +481,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, BlockType = VIDBlockPayload>,
TYPES: NodeType<Transaction = VIDTransaction, BlockPayload = VIDBlockPayload>,
I: NodeImplementation<TYPES, Leaf = Leaf<TYPES>, ConsensusMessage = SequencingMessage<TYPES, I>>,
>(
task_runner: TaskRunner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ impl<STATE: StateContents> DualKeyValue for QuorumCertificate<STATE> {
type Key1 = Commitment<STATE::BlockPayload>;
type Key2 = ViewNumber;

const KEY_1_NAME: &'static str = "block_commitment";
const KEY_1_NAME: &'static str = "payload_commitment";
const KEY_2_NAME: &'static str = "view_number";

fn key_1(&self) -> Self::Key1 {
self.block_commitment
self.payload_commitment
}
fn key_2(&self) -> Self::Key2 {
self.view_number
Expand All @@ -203,7 +203,7 @@ where
type Key2 = Commitment<STATE::BlockPayload>;

const KEY_1_NAME: &'static str = "leaf_commitment";
const KEY_2_NAME: &'static str = "block_commitment";
const KEY_2_NAME: &'static str = "payload_commitment";

fn key_1(&self) -> Self::Key1 {
self.commit()
Expand Down
6 changes: 3 additions & 3 deletions crates/hotshot/src/traits/storage/memory_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ mod test {

impl NodeType for DummyTypes {
type Time = ViewNumber;
type BlockType = DummyBlock;
type BlockHeader = DummyBlock;
type BlockPayload = DummyBlock;
type SignatureKey = BLSPubKey;
type VoteTokenType = StaticVoteToken<Self::SignatureKey>;
type Transaction = <DummyBlock as BlockPayload>::Transaction;
Expand All @@ -166,15 +167,14 @@ mod test {
let dummy_leaf_commit = fake_commitment::<ValidatingLeaf<DummyTypes>>();
StoredView::from_qc_block_and_state(
QuorumCertificate {
// block_commitment: dummy_block_commit,
is_genesis: view_number == <DummyTypes as NodeType>::Time::genesis(),
leaf_commitment: dummy_leaf_commit,
signatures: AssembledSignature::Genesis(),
view_number,
},
DummyBlock::random(rng),
Some(DummyBlock::random(rng)),
DummyState::random(rng),
rng.next_u64(),
dummy_leaf_commit,
Vec::new(),
genesis_proposer_id(),
Expand Down
Loading

0 comments on commit e78b628

Please sign in to comment.