Skip to content

Commit

Permalink
[VALIDATED_STATE] - Give proposal and validation functions access to …
Browse files Browse the repository at this point in the history
…parent leaf (#2674)

* Replace parent header with leaf

* Remove associated types, remove clone, add to justfile
  • Loading branch information
shenkeyao authored Feb 28, 2024
1 parent 5cfeb01 commit eeaf829
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 94 deletions.
38 changes: 21 additions & 17 deletions crates/example-types/src/block_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ use std::{
mem::size_of,
};

use crate::node_types::TestTypes;
use commit::{Commitment, Committable, RawCommitmentBuilder};
use hotshot_types::{
data::BlockError,
data::{BlockError, Leaf},
traits::{
block_contents::{BlockHeader, TestableBlock, Transaction},
node_implementation::NodeType,
BlockPayload, ValidatedState,
},
utils::BuilderCommitment,
Expand All @@ -16,8 +18,6 @@ use hotshot_types::{
use serde::{Deserialize, Serialize};
use sha3::{Digest, Keccak256};

use crate::state_types::TestValidatedState;

/// The transaction in a [`TestBlockPayload`].
#[derive(Default, PartialEq, Eq, Hash, Serialize, Deserialize, Clone, Debug)]
pub struct TestTransaction(pub Vec<u8>);
Expand Down Expand Up @@ -180,27 +180,24 @@ pub struct TestBlockHeader {
pub payload_commitment: VidCommitment,
}

impl BlockHeader for TestBlockHeader {
type Payload = TestBlockPayload;
type State = TestValidatedState;

impl<TYPES: NodeType<BlockPayload = TestBlockPayload>> BlockHeader<TYPES> for TestBlockHeader {
async fn new(
_parent_state: &Self::State,
_instance_state: &<Self::State as ValidatedState>::Instance,
parent_header: &Self,
_parent_state: &TYPES::ValidatedState,
_instance_state: &<TYPES::ValidatedState as ValidatedState<TYPES>>::Instance,
parent_leaf: &Leaf<TYPES>,
payload_commitment: VidCommitment,
_metadata: <Self::Payload as BlockPayload>::Metadata,
_metadata: <TYPES::BlockPayload as BlockPayload>::Metadata,
) -> Self {
Self {
block_number: parent_header.block_number + 1,
block_number: parent_leaf.block_header.block_number() + 1,
payload_commitment,
}
}

fn genesis(
_instance_state: &<Self::State as ValidatedState>::Instance,
_instance_state: &<TYPES::ValidatedState as ValidatedState<TYPES>>::Instance,
payload_commitment: VidCommitment,
_metadata: <Self::Payload as BlockPayload>::Metadata,
_metadata: <TYPES::BlockPayload as BlockPayload>::Metadata,
) -> Self {
Self {
block_number: 0,
Expand All @@ -216,17 +213,24 @@ impl BlockHeader for TestBlockHeader {
self.payload_commitment
}

fn metadata(&self) -> &<Self::Payload as BlockPayload>::Metadata {
fn metadata(&self) -> &<TYPES::BlockPayload as BlockPayload>::Metadata {
&()
}
}

impl Committable for TestBlockHeader {
fn commit(&self) -> Commitment<Self> {
RawCommitmentBuilder::new("Header Comm")
.u64_field("block number", self.block_number())
.u64_field(
"block number",
<TestBlockHeader as BlockHeader<TestTypes>>::block_number(self),
)
.constant_str("payload commitment")
.fixed_size_bytes(self.payload_commitment().as_ref().as_ref())
.fixed_size_bytes(
<TestBlockHeader as BlockHeader<TestTypes>>::payload_commitment(self)
.as_ref()
.as_ref(),
)
.finalize()
}

Expand Down
25 changes: 11 additions & 14 deletions crates/example-types/src/state_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
use commit::{Commitment, Committable};

use hotshot_types::{
data::{fake_commitment, BlockError, ViewNumber},
data::{fake_commitment, BlockError, Leaf, ViewNumber},
traits::{
block_contents::BlockHeader,
node_implementation::NodeType,
states::{InstanceState, TestableState, ValidatedState},
BlockPayload,
},
Expand All @@ -12,8 +14,7 @@ use hotshot_types::{
use serde::{Deserialize, Serialize};
use std::fmt::Debug;

use crate::block_types::TestTransaction;
use crate::block_types::{TestBlockHeader, TestBlockPayload};
use crate::block_types::{TestBlockPayload, TestTransaction};
pub use crate::node_types::TestTypes;

/// Instance-level state implementation for testing purposes.
Expand Down Expand Up @@ -53,32 +54,28 @@ impl Default for TestValidatedState {
}
}

impl ValidatedState for TestValidatedState {
impl<TYPES: NodeType> ValidatedState<TYPES> for TestValidatedState {
type Error = BlockError;

type Instance = TestInstanceState;

type BlockHeader = TestBlockHeader;

type BlockPayload = TestBlockPayload;

type Time = ViewNumber;

async fn validate_and_apply_header(
&self,
_instance: &Self::Instance,
_parent_header: &Self::BlockHeader,
_proposed_header: &Self::BlockHeader,
_parent_leaf: &Leaf<TYPES>,
_proposed_header: &TYPES::BlockHeader,
) -> Result<Self, Self::Error> {
Ok(TestValidatedState {
block_height: self.block_height + 1,
prev_state_commitment: self.commit(),
})
}

fn from_header(block_header: &Self::BlockHeader) -> Self {
fn from_header(block_header: &TYPES::BlockHeader) -> Self {
Self {
block_height: block_header.block_number,
block_height: block_header.block_number(),
..Default::default()
}
}
Expand All @@ -90,12 +87,12 @@ impl ValidatedState for TestValidatedState {
}
}

impl TestableState for TestValidatedState {
impl<TYPES: NodeType<BlockPayload = TestBlockPayload>> TestableState<TYPES> for TestValidatedState {
fn create_random_transaction(
_state: Option<&Self>,
_rng: &mut dyn rand::RngCore,
padding: u64,
) -> <Self::BlockPayload as BlockPayload>::Transaction {
) -> <TYPES::BlockPayload as BlockPayload>::Transaction {
/// clippy appeasement for `RANDOM_TX_BASE_SIZE`
const RANDOM_TX_BASE_SIZE: usize = 8;
TestTransaction(vec![
Expand Down
10 changes: 5 additions & 5 deletions crates/examples/infra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ pub trait RunDA<
Storage = MemoryStorage<TYPES>,
>,
> where
<TYPES as NodeType>::ValidatedState: TestableState,
<TYPES as NodeType>::ValidatedState: TestableState<TYPES>,
<TYPES as NodeType>::BlockPayload: TestableBlock,
TYPES: NodeType<Transaction = TestTransaction>,
Leaf<TYPES>: TestableLeaf,
Expand Down Expand Up @@ -514,7 +514,7 @@ impl<
>,
> RunDA<TYPES, WebServerNetwork<TYPES>, WebServerNetwork<TYPES>, NODE> for WebServerDARun<TYPES>
where
<TYPES as NodeType>::ValidatedState: TestableState,
<TYPES as NodeType>::ValidatedState: TestableState<TYPES>,
<TYPES as NodeType>::BlockPayload: TestableBlock,
Leaf<TYPES>: TestableLeaf,
Self: Sync,
Expand Down Expand Up @@ -594,7 +594,7 @@ impl<
NODE,
> for Libp2pDARun<TYPES>
where
<TYPES as NodeType>::ValidatedState: TestableState,
<TYPES as NodeType>::ValidatedState: TestableState<TYPES>,
<TYPES as NodeType>::BlockPayload: TestableBlock,
Leaf<TYPES>: TestableLeaf,
Self: Sync,
Expand Down Expand Up @@ -658,7 +658,7 @@ impl<
>,
> RunDA<TYPES, CombinedNetworks<TYPES>, CombinedNetworks<TYPES>, NODE> for CombinedDARun<TYPES>
where
<TYPES as NodeType>::ValidatedState: TestableState,
<TYPES as NodeType>::ValidatedState: TestableState<TYPES>,
<TYPES as NodeType>::BlockPayload: TestableBlock,
Leaf<TYPES>: TestableLeaf,
Self: Sync,
Expand Down Expand Up @@ -746,7 +746,7 @@ pub async fn main_entry_point<
>(
args: ValidatorArgs,
) where
<TYPES as NodeType>::ValidatedState: TestableState,
<TYPES as NodeType>::ValidatedState: TestableState<TYPES>,
<TYPES as NodeType>::BlockPayload: TestableBlock,
Leaf<TYPES>: TestableLeaf,
{
Expand Down
13 changes: 7 additions & 6 deletions crates/task-impls/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,11 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, A: ConsensusApi<TYPES, I> +
block_payload: None,
proposer_id: sender,
};
let state = Arc::new(<TYPES::ValidatedState as ValidatedState>::from_header(
&proposal.data.block_header,
));
let state = Arc::new(
<TYPES::ValidatedState as ValidatedState<TYPES>>::from_header(
&proposal.data.block_header,
),
);

consensus.validated_state_map.insert(
view,
Expand Down Expand Up @@ -619,7 +621,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, A: ConsensusApi<TYPES, I> +
let Ok(state) = parent_state
.validate_and_apply_header(
&consensus.instance_state,
&parent_leaf.block_header.clone(),
&parent_leaf,
&proposal.data.block_header.clone(),
)
.await
Expand Down Expand Up @@ -1275,7 +1277,6 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, A: ConsensusApi<TYPES, I> +
}

let parent_leaf = leaf.clone();
let parent_header = parent_leaf.block_header.clone();

let original_parent_hash = parent_leaf.commit();

Expand All @@ -1298,7 +1299,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, A: ConsensusApi<TYPES, I> +
let block_header = TYPES::BlockHeader::new(
state,
&consensus.instance_state,
&parent_header,
&parent_leaf,
commit_and_metadata.commitment,
commit_and_metadata.metadata.clone(),
)
Expand Down
10 changes: 5 additions & 5 deletions crates/testing/src/task_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,13 @@ async fn build_quorum_proposal_and_signature(
&block.encode().unwrap().collect(),
handle.hotshot.memberships.quorum_membership.total_nodes(),
);
let mut parent_state = Arc::new(<TestValidatedState as ValidatedState>::from_header(
&parent_leaf.block_header,
));
let mut parent_state = Arc::new(
<TestValidatedState as ValidatedState<TestTypes>>::from_header(&parent_leaf.block_header),
);
let block_header = TestBlockHeader::new(
&*parent_state,
&TestInstanceState {},
&parent_leaf.block_header,
&parent_leaf,
payload_commitment,
(),
)
Expand Down Expand Up @@ -269,7 +269,7 @@ async fn build_quorum_proposal_and_signature(
for cur_view in 2..=view {
let state_new_view = Arc::new(
parent_state
.validate_and_apply_header(&TestInstanceState {}, &block_header, &block_header)
.validate_and_apply_header(&TestInstanceState {}, &parent_leaf, &block_header)
.await
.unwrap(),
);
Expand Down
10 changes: 2 additions & 8 deletions crates/types/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
election::Membership,
node_implementation::{ConsensusTime, NodeType},
signature_key::SignatureKey,
states::{TestableState, ValidatedState},
states::TestableState,
storage::StoredView,
BlockPayload,
},
Expand Down Expand Up @@ -104,12 +104,6 @@ impl std::ops::Sub<u64> for ViewNumber {
}
}

/// The `Transaction` type associated with a `ValidatedState`, as a syntactic shortcut
pub type Transaction<STATE> =
<<STATE as ValidatedState>::BlockPayload as BlockPayload>::Transaction;
/// `Commitment` to the `Transaction` type associated with a `ValidatedState`, as a syntactic shortcut
pub type TxnCommitment<STATE> = Commitment<Transaction<STATE>>;

/// A proposal to start providing data availability for a block.
#[derive(custom_debug::Debug, Serialize, Deserialize, Clone, Eq, PartialEq, Hash)]
pub struct DAProposal<TYPES: NodeType> {
Expand Down Expand Up @@ -414,7 +408,7 @@ impl<TYPES: NodeType> Leaf<TYPES> {

impl<TYPES: NodeType> TestableLeaf for Leaf<TYPES>
where
TYPES::ValidatedState: TestableState,
TYPES::ValidatedState: TestableState<TYPES>,
TYPES::BlockPayload: TestableBlock,
{
type NodeType = TYPES;
Expand Down
29 changes: 12 additions & 17 deletions crates/types/src/traits/block_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
//! describe the behaviors that a block is expected to have.
use crate::{
traits::ValidatedState,
data::Leaf,
traits::{node_implementation::NodeType, ValidatedState},
utils::BuilderCommitment,
vid::{vid_scheme, VidCommitment, VidSchemeType},
};
Expand Down Expand Up @@ -114,30 +115,24 @@ pub fn vid_commitment(
pub const GENESIS_VID_NUM_STORAGE_NODES: usize = 1;

/// Header of a block, which commits to a [`BlockPayload`].
pub trait BlockHeader:
pub trait BlockHeader<TYPES: NodeType>:
Serialize + Clone + Debug + Hash + PartialEq + Eq + Send + Sync + DeserializeOwned + Committable
{
/// Block payload associated with the commitment.
type Payload: BlockPayload;

/// Validated state.
type State: ValidatedState<BlockHeader = Self>;

/// Build a header with the payload commitment, metadata, instance-level state, parent header,
/// and parent state.
/// Build a header with the parent validate state, instance-level state, parent leaf, payload
/// commitment, and metadata.
fn new(
parent_state: &Self::State,
instance_state: &<Self::State as ValidatedState>::Instance,
parent_header: &Self,
parent_state: &TYPES::ValidatedState,
instance_state: &<TYPES::ValidatedState as ValidatedState<TYPES>>::Instance,
parent_leaf: &Leaf<TYPES>,
payload_commitment: VidCommitment,
metadata: <Self::Payload as BlockPayload>::Metadata,
metadata: <TYPES::BlockPayload as BlockPayload>::Metadata,
) -> impl Future<Output = Self> + Send;

/// Build the genesis header, payload, and metadata.
fn genesis(
instance_state: &<Self::State as ValidatedState>::Instance,
instance_state: &<TYPES::ValidatedState as ValidatedState<TYPES>>::Instance,
payload_commitment: VidCommitment,
metadata: <Self::Payload as BlockPayload>::Metadata,
metadata: <TYPES::BlockPayload as BlockPayload>::Metadata,
) -> Self;

/// Get the block number.
Expand All @@ -147,5 +142,5 @@ pub trait BlockHeader:
fn payload_commitment(&self) -> VidCommitment;

/// Get the metadata.
fn metadata(&self) -> &<Self::Payload as BlockPayload>::Metadata;
fn metadata(&self) -> &<TYPES::BlockPayload as BlockPayload>::Metadata;
}
Loading

0 comments on commit eeaf829

Please sign in to comment.