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

Refactor demo and block payload implementations #1770

Merged
merged 7 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
209 changes: 105 additions & 104 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/hotshot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ nll = { workspace = true }
rand = { workspace = true }
rand_chacha = { workspace = true }
serde = { workspace = true, features = ["rc"] }
sha3 = "^0.10"
snafu = { workspace = true }
surf-disco = { workspace = true }
time = { workspace = true }
Expand Down
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::demos::sdemo::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::demos::sdemo::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::demos::sdemo::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::{
demos::sdemo::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::demos::sdemo::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::demos::sdemo::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;
}
110 changes: 110 additions & 0 deletions crates/hotshot/src/block_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//! This module provides an implementation of the `HotShot` suite of traits.
shenkeyao marked this conversation as resolved.
Show resolved Hide resolved
use std::{
collections::HashSet,
fmt::{Debug, Display},
};

use commit::{Commitment, Committable};
use hotshot_types::traits::{block_contents::Transaction, state::TestableBlock, BlockPayload};
use serde::{Deserialize, Serialize};
use sha3::{Digest, Keccak256};
use snafu::Snafu;

/// The transaction in a [`VIDBlockPayload`].
#[derive(Default, PartialEq, Eq, Hash, Serialize, Deserialize, Clone, Debug)]
pub struct VIDTransaction(pub Vec<u8>);

impl Committable for VIDTransaction {
fn commit(&self) -> Commitment<Self> {
let builder = commit::RawCommitmentBuilder::new("Txn Comm");
let mut hasher = Keccak256::new();
hasher.update(&self.0);
let generic_array = hasher.finalize();
builder.generic_byte_array(&generic_array).finalize()
}

fn tag() -> String {
"SEQUENCING_TXN".to_string()
}
}

impl Transaction for VIDTransaction {}

impl VIDTransaction {
/// create a new transaction
#[must_use]
pub fn new() -> Self {
Self(Vec::new())
}
}

/// The error type for block payload.
#[derive(Snafu, Debug)]
pub enum BlockPayloadError {
/// Previous state commitment does not match
PreviousStateMismatch,
/// Nonce was reused
ReusedTxn,
/// Genesis failure
GenesisFailed,
/// Genesis reencountered after initialization
GenesisAfterStart,
/// invalid block
InvalidBlock,
}

/// A [`BlockPayload`] that contains a list of `VIDTransaction`.
#[derive(PartialEq, Eq, Hash, Serialize, Deserialize, Clone, Debug)]
pub struct VIDBlockPayload(pub Vec<VIDTransaction>);

impl Committable for VIDBlockPayload {
fn commit(&self) -> Commitment<Self> {
// TODO: Use use VID block commitment.
// <https://github.com/EspressoSystems/HotShot/issues/1730>
let builder = commit::RawCommitmentBuilder::new("BlockPayload Comm");
builder.finalize()
}

fn tag() -> String {
"VID_BLOCK_PAYLOAD".to_string()
}
}

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

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

fn txn_count(&self) -> u64 {
self.0.len() as u64
}
}

impl BlockPayload for VIDBlockPayload {
type Error = BlockPayloadError;

type Transaction = VIDTransaction;

fn new() -> Self {
<Self as TestableBlock>::genesis()
}

fn add_transaction_raw(
&self,
tx: &Self::Transaction,
) -> std::result::Result<Self, Self::Error> {
let mut new = self.0.clone();
new.push(tx.clone());
Ok(VIDBlockPayload(new))
}

fn contained_transactions(&self) -> HashSet<Commitment<Self::Transaction>> {
self.0.iter().map(commit::Committable::commit).collect()
}
}
Loading
Loading