Skip to content

Commit

Permalink
Combine genesis and normal block, remove previous_state, rename SDemo…
Browse files Browse the repository at this point in the history
…Types.
  • Loading branch information
shenkeyao committed Sep 18, 2023
1 parent 44abc15 commit e11b70e
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 132 deletions.
4 changes: 2 additions & 2 deletions crates/hotshot/examples/web-server-da/multi-validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use async_compatibility_layer::{
logging::{setup_backtrace, setup_logging},
};
use clap::Parser;
use hotshot::demo::SDemoTypes;
use hotshot::demo::DemoTypes;
use hotshot_orchestrator::client::ValidatorArgs;
use std::net::IpAddr;
use tracing::instrument;
Expand Down Expand Up @@ -49,7 +49,7 @@ async fn main() {
for _ in 0..args.num_nodes {
let node = async_spawn(async move {
infra_da::main_entry_point::<
SDemoTypes,
DemoTypes,
ThisMembership,
DANetwork,
QuorumNetwork,
Expand Down
8 changes: 4 additions & 4 deletions crates/hotshot/examples/web-server-da/multi-web-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use async_compatibility_layer::{art::async_spawn, channel::oneshot};
use clap::Parser;
use hotshot::demo::SDemoTypes;
use hotshot::demo::DemoTypes;
use tracing::error;

#[derive(Parser, Debug)]
Expand All @@ -27,7 +27,7 @@ async fn main() {

let cdn_server = async_spawn(async move {
if let Err(e) = hotshot_web_server::run_web_server::<
<SDemoTypes as hotshot_types::traits::node_implementation::NodeType>::SignatureKey,
<DemoTypes as hotshot_types::traits::node_implementation::NodeType>::SignatureKey,
>(Some(server_shutdown_cdn), args.cdn_port)
.await
{
Expand All @@ -37,7 +37,7 @@ async fn main() {
});
let da_server = async_spawn(async move {
if let Err(e) = hotshot_web_server::run_web_server::<
<SDemoTypes as hotshot_types::traits::node_implementation::NodeType>::SignatureKey,
<DemoTypes as hotshot_types::traits::node_implementation::NodeType>::SignatureKey,
>(Some(server_shutdown_da), args.da_port)
.await
{
Expand All @@ -47,7 +47,7 @@ async fn main() {
});
let vs_server = async_spawn(async move {
if let Err(e) = hotshot_web_server::run_web_server::<
<SDemoTypes as hotshot_types::traits::node_implementation::NodeType>::SignatureKey,
<DemoTypes as hotshot_types::traits::node_implementation::NodeType>::SignatureKey,
>(Some(server_shutdown_view_sync), args.view_sync_port)
.await
{
Expand Down
4 changes: 2 additions & 2 deletions crates/hotshot/examples/web-server-da/orchestrator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod types;

use async_compatibility_layer::logging::{setup_backtrace, setup_logging};
use clap::Parser;
use hotshot::demo::SDemoTypes;
use hotshot::demo::DemoTypes;
use tracing::instrument;
use types::ThisMembership;

Expand All @@ -29,7 +29,7 @@ async fn main() {
let args = OrchestratorArgs::parse();

run_orchestrator_da::<
SDemoTypes,
DemoTypes,
ThisMembership,
DANetwork,
QuorumNetwork,
Expand Down
54 changes: 27 additions & 27 deletions crates/hotshot/examples/web-server-da/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::infra_da::WebServerDARun;
use hotshot::{
demo::SDemoTypes,
demo::DemoTypes,
traits::{
election::static_committee::GeneralStaticCommittee,
implementations::{MemoryStorage, WebCommChannel},
Expand All @@ -22,54 +22,54 @@ use std::fmt::Debug;
#[derive(Clone, Debug, Deserialize, Serialize, Hash, PartialEq, Eq)]
pub struct NodeImpl {}

pub type ThisLeaf = SequencingLeaf<SDemoTypes>;
pub type ThisLeaf = SequencingLeaf<DemoTypes>;
pub type ThisMembership =
GeneralStaticCommittee<SDemoTypes, ThisLeaf, <SDemoTypes as NodeType>::SignatureKey>;
pub type DANetwork = WebCommChannel<SDemoTypes, NodeImpl, ThisMembership>;
pub type QuorumNetwork = WebCommChannel<SDemoTypes, NodeImpl, ThisMembership>;
pub type ViewSyncNetwork = WebCommChannel<SDemoTypes, NodeImpl, ThisMembership>;
GeneralStaticCommittee<DemoTypes, ThisLeaf, <DemoTypes as NodeType>::SignatureKey>;
pub type DANetwork = WebCommChannel<DemoTypes, NodeImpl, ThisMembership>;
pub type QuorumNetwork = WebCommChannel<DemoTypes, NodeImpl, ThisMembership>;
pub type ViewSyncNetwork = WebCommChannel<DemoTypes, NodeImpl, ThisMembership>;

pub type ThisDAProposal = DAProposal<SDemoTypes>;
pub type ThisDAVote = DAVote<SDemoTypes>;
pub type ThisDAProposal = DAProposal<DemoTypes>;
pub type ThisDAVote = DAVote<DemoTypes>;

pub type ThisQuorumProposal = QuorumProposal<SDemoTypes, ThisLeaf>;
pub type ThisQuorumVote = QuorumVote<SDemoTypes, ThisLeaf>;
pub type ThisQuorumProposal = QuorumProposal<DemoTypes, ThisLeaf>;
pub type ThisQuorumVote = QuorumVote<DemoTypes, ThisLeaf>;

pub type ThisViewSyncProposal = ViewSyncCertificate<SDemoTypes>;
pub type ThisViewSyncVote = ViewSyncVote<SDemoTypes>;
pub type ThisViewSyncProposal = ViewSyncCertificate<DemoTypes>;
pub type ThisViewSyncVote = ViewSyncVote<DemoTypes>;

impl NodeImplementation<SDemoTypes> for NodeImpl {
type Storage = MemoryStorage<SDemoTypes, Self::Leaf>;
type Leaf = SequencingLeaf<SDemoTypes>;
impl NodeImplementation<DemoTypes> for NodeImpl {
type Storage = MemoryStorage<DemoTypes, Self::Leaf>;
type Leaf = SequencingLeaf<DemoTypes>;
type Exchanges = SequencingExchanges<
SDemoTypes,
Message<SDemoTypes, Self>,
DemoTypes,
Message<DemoTypes, Self>,
QuorumExchange<
SDemoTypes,
DemoTypes,
Self::Leaf,
ThisQuorumProposal,
ThisMembership,
QuorumNetwork,
Message<SDemoTypes, Self>,
Message<DemoTypes, Self>,
>,
CommitteeExchange<SDemoTypes, ThisMembership, DANetwork, Message<SDemoTypes, Self>>,
CommitteeExchange<DemoTypes, ThisMembership, DANetwork, Message<DemoTypes, Self>>,
ViewSyncExchange<
SDemoTypes,
DemoTypes,
ThisViewSyncProposal,
ThisMembership,
ViewSyncNetwork,
Message<SDemoTypes, Self>,
Message<DemoTypes, Self>,
>,
>;
type ConsensusMessage = SequencingMessage<SDemoTypes, Self>;
type ConsensusMessage = SequencingMessage<DemoTypes, Self>;

fn new_channel_maps(
start_view: <SDemoTypes as NodeType>::Time,
start_view: <DemoTypes as NodeType>::Time,
) -> (
ChannelMaps<SDemoTypes, Self>,
Option<ChannelMaps<SDemoTypes, Self>>,
ChannelMaps<DemoTypes, Self>,
Option<ChannelMaps<DemoTypes, Self>>,
) {
(ChannelMaps::new(start_view), None)
}
}
pub type ThisRun = WebServerDARun<SDemoTypes, NodeImpl, ThisMembership>;
pub type ThisRun = WebServerDARun<DemoTypes, NodeImpl, ThisMembership>;
4 changes: 2 additions & 2 deletions crates/hotshot/examples/web-server-da/validator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use async_compatibility_layer::logging::{setup_backtrace, setup_logging};
use clap::Parser;
use hotshot::demo::SDemoTypes;
use hotshot::demo::DemoTypes;
use tracing::{info, instrument};

use crate::types::{DANetwork, NodeImpl, QuorumNetwork, ThisMembership, ThisRun, ViewSyncNetwork};
Expand Down Expand Up @@ -29,7 +29,7 @@ async fn main() {
args.host, args.port
);
infra_da::main_entry_point::<
SDemoTypes,
DemoTypes,
ThisMembership,
DANetwork,
QuorumNetwork,
Expand Down
4 changes: 2 additions & 2 deletions crates/hotshot/examples/web-server-da/web-server.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use hotshot::demo::SDemoTypes;
use hotshot::demo::DemoTypes;
use std::sync::Arc;

use async_compatibility_layer::{
Expand All @@ -23,7 +23,7 @@ async fn main() {
let (server_shutdown_sender, server_shutdown) = oneshot();
let _sender = Arc::new(server_shutdown_sender);
let _result = hotshot_web_server::run_web_server::<
<SDemoTypes as hotshot_types::traits::node_implementation::NodeType>::SignatureKey,
<DemoTypes as hotshot_types::traits::node_implementation::NodeType>::SignatureKey,
>(Some(server_shutdown), args.port)
.await;
}
75 changes: 13 additions & 62 deletions crates/hotshot/src/block_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,48 +63,21 @@ pub enum BlockPayloadError {
GenesisFailed,
/// Genesis reencountered after initialization
GenesisAfterStart,
/// no transasctions added to genesis
GenesisCantHaveTransactions,
/// invalid block
InvalidBlock,
}

/// genesis block
/// A [`BlockPayload`] that contains a list of `VIDTransaction`.
#[derive(PartialEq, Eq, Hash, Serialize, Deserialize, Clone, Debug)]
pub struct GenesisBlockPayload {}

/// Any block after genesis
#[derive(PartialEq, Eq, Hash, Serialize, Deserialize, Clone, Debug)]
pub struct NormalBlockPayload {
/// [`BlockPayload`] state commitment
pub previous_state: (),
/// [`VIDTransaction`] vector
pub transactions: Vec<VIDTransaction>,
}

/// The block for the sequencing demo
#[derive(PartialEq, Eq, Hash, Serialize, Deserialize, Clone, Debug)]
pub enum VIDBlockPayload {
/// genesis block payload
Genesis(GenesisBlockPayload),
/// normal block payload
Normal(NormalBlockPayload),
}
pub struct VIDBlockPayload(pub Vec<VIDTransaction>);

impl Committable for VIDBlockPayload {
fn commit(&self) -> Commitment<Self> {
match &self {
VIDBlockPayload::Genesis(_) => {
commit::RawCommitmentBuilder::new("Genesis Comm").finalize()
}
VIDBlockPayload::Normal(block) => {
let mut builder = commit::RawCommitmentBuilder::new("Normal Comm");
for txn in &block.transactions {
builder = builder.u64_field("transaction", **txn);
}
builder.finalize()
}
let mut builder = commit::RawCommitmentBuilder::new("Normal Comm");
for txn in &self.0 {
builder = builder.u64_field("transaction", **txn);
}
builder.finalize()
}

fn tag() -> String {
Expand All @@ -114,27 +87,17 @@ impl Committable for VIDBlockPayload {

impl Display for VIDBlockPayload {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
VIDBlockPayload::Genesis(_) => {
write!(f, "Genesis BlockPayload")
}
VIDBlockPayload::Normal(block) => {
write!(f, "Normal BlockPayload #txns={}", block.transactions.len())
}
}
write!(f, "BlockPayload #txns={}", self.0.len())
}
}

impl TestableBlock for VIDBlockPayload {
fn genesis() -> Self {
VIDBlockPayload::Genesis(GenesisBlockPayload {})
VIDBlockPayload(Vec::new())
}

fn txn_count(&self) -> u64 {
match self {
VIDBlockPayload::Genesis(_) => 0,
VIDBlockPayload::Normal(n) => n.transactions.len() as u64,
}
self.0.len() as u64
}
}

Expand All @@ -151,24 +114,12 @@ impl BlockPayload for VIDBlockPayload {
&self,
tx: &Self::Transaction,
) -> std::result::Result<Self, Self::Error> {
match self {
VIDBlockPayload::Genesis(_) => Err(BlockPayloadError::GenesisCantHaveTransactions),
VIDBlockPayload::Normal(n) => {
let mut new = n.clone();
new.transactions.push(tx.clone());
Ok(VIDBlockPayload::Normal(new))
}
}
let mut new = self.0.clone();
new.push(tx.clone());
Ok(VIDBlockPayload(new))
}

fn contained_transactions(&self) -> HashSet<Commitment<Self::Transaction>> {
match self {
VIDBlockPayload::Genesis(_) => HashSet::new(),
VIDBlockPayload::Normal(n) => n
.transactions
.iter()
.map(commit::Committable::commit)
.collect(),
}
self.0.iter().map(commit::Committable::commit).collect()
}
}
30 changes: 13 additions & 17 deletions crates/hotshot/src/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! These implementations are useful in examples and integration testing, but are not suitable for
//! production use.
use crate::{
block_impl::{BlockPayloadError, NormalBlockPayload, VIDBlockPayload, VIDTransaction},
block_impl::{BlockPayloadError, VIDBlockPayload, VIDTransaction},
traits::election::static_committee::{StaticElectionConfig, StaticVoteToken},
};
use commit::{Commitment, Committable};
Expand Down Expand Up @@ -73,18 +73,14 @@ impl State for SDemoState {
type Time = ViewNumber;

fn next_block(_state: Option<Self>) -> Self::BlockType {
VIDBlockPayload::Normal(NormalBlockPayload {
previous_state: (),
transactions: Vec::new(),
})
VIDBlockPayload(Vec::new())
}

fn validate_block(&self, block: &Self::BlockType, view_number: &Self::Time) -> bool {
match block {
VIDBlockPayload::Genesis(_) => {
view_number == &ViewNumber::genesis() && view_number == &self.view_number
}
VIDBlockPayload::Normal(_n) => self.view_number < *view_number,
fn validate_block(&self, _block: &Self::BlockType, view_number: &Self::Time) -> bool {
if view_number == &ViewNumber::genesis() {
&self.view_number == view_number
} else {
self.view_number < *view_number
}
}

Expand Down Expand Up @@ -133,9 +129,9 @@ impl TestableState for SDemoState {
serde::Serialize,
serde::Deserialize,
)]
pub struct SDemoTypes;
pub struct DemoTypes;

impl NodeType for SDemoTypes {
impl NodeType for DemoTypes {
type Time = ViewNumber;
type BlockType = VIDBlockPayload;
type SignatureKey = BLSPubKey;
Expand All @@ -150,11 +146,11 @@ impl NodeType for SDemoTypes {
#[derivative(Clone(bound = ""))]
pub struct SDemoNode<MEMBERSHIP>(PhantomData<MEMBERSHIP>)
where
MEMBERSHIP: Membership<SDemoTypes> + std::fmt::Debug;
MEMBERSHIP: Membership<DemoTypes> + std::fmt::Debug;

impl<MEMBERSHIP> SDemoNode<MEMBERSHIP>
where
MEMBERSHIP: Membership<SDemoTypes> + std::fmt::Debug,
MEMBERSHIP: Membership<DemoTypes> + std::fmt::Debug,
{
/// Create a new `SDemoNode`
#[must_use]
Expand All @@ -165,7 +161,7 @@ where

impl<MEMBERSHIP> Debug for SDemoNode<MEMBERSHIP>
where
MEMBERSHIP: Membership<SDemoTypes> + std::fmt::Debug,
MEMBERSHIP: Membership<DemoTypes> + std::fmt::Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SDemoNode")
Expand All @@ -176,7 +172,7 @@ where

impl<MEMBERSHIP> Default for SDemoNode<MEMBERSHIP>
where
MEMBERSHIP: Membership<SDemoTypes> + std::fmt::Debug,
MEMBERSHIP: Membership<DemoTypes> + std::fmt::Debug,
{
fn default() -> Self {
Self::new()
Expand Down
Loading

0 comments on commit e11b70e

Please sign in to comment.