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 2 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
208 changes: 104 additions & 104 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion 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::SDemoTypes;
shenkeyao marked this conversation as resolved.
Show resolved Hide resolved
use hotshot_orchestrator::client::ValidatorArgs;
use std::net::IpAddr;
use tracing::instrument;
Expand Down
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::SDemoTypes;
use tracing::error;

#[derive(Parser, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion 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::SDemoTypes;
use tracing::instrument;
use types::ThisMembership;

Expand Down
2 changes: 1 addition & 1 deletion 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::SDemoTypes,
traits::{
election::static_committee::GeneralStaticCommittee,
implementations::{MemoryStorage, WebCommChannel},
Expand Down
2 changes: 1 addition & 1 deletion 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::SDemoTypes;
use tracing::{info, instrument};

use crate::types::{DANetwork, NodeImpl, QuorumNetwork, ThisMembership, ThisRun, ViewSyncNetwork};
Expand Down
2 changes: 1 addition & 1 deletion 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::SDemoTypes;
use std::sync::Arc;

use async_compatibility_layer::{
Expand Down
174 changes: 174 additions & 0 deletions crates/hotshot/src/block_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
//! 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},
ops::Deref,
};

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

/// The transaction in a [`VIDBlockPayload`].
#[derive(PartialEq, Eq, Hash, Serialize, Deserialize, Clone, Debug)]
pub struct VIDTransaction {
/// identifier for the transaction
pub id: u64,
/// padding to add to txn (to make it larger and thereby more realistic)
pub padding: Vec<u8>,
shenkeyao marked this conversation as resolved.
Show resolved Hide resolved
}

impl Deref for VIDTransaction {
type Target = u64;

fn deref(&self) -> &Self::Target {
&self.id
}
}

impl Committable for VIDTransaction {
fn commit(&self) -> Commitment<Self> {
commit::RawCommitmentBuilder::new("SDemo Txn Comm")
.u64_field("id", self.id)
.finalize()
}

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

impl Transaction for VIDTransaction {}

impl VIDTransaction {
/// create a new transaction
#[must_use]
pub fn new(id: u64) -> Self {
Self {
id,
padding: vec![],
}
}
}

/// 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,
/// no transasctions added to genesis
GenesisCantHaveTransactions,
/// invalid block
InvalidBlock,
}

/// genesis block
#[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: (),
shenkeyao marked this conversation as resolved.
Show resolved Hide resolved
/// [`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),
}

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()
}
}
}

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

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())
}
}
}
}

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

fn txn_count(&self) -> u64 {
match self {
VIDBlockPayload::Genesis(_) => 0,
VIDBlockPayload::Normal(n) => n.transactions.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> {
match self {
VIDBlockPayload::Genesis(_) => Err(BlockPayloadError::GenesisCantHaveTransactions),
VIDBlockPayload::Normal(n) => {
let mut new = n.clone();
new.transactions.push(tx.clone());
Ok(VIDBlockPayload::Normal(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(),
}
}
}
Loading
Loading