Skip to content

Commit

Permalink
Get instance state from HotShotInitializer, move function parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
shenkeyao committed Jan 31, 2024
1 parent 60b0711 commit 84ea862
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 48 deletions.
1 change: 0 additions & 1 deletion crates/hotshot/examples/infra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@ pub trait RunDA<
memberships,
networks_bundle,
initializer,
TestInstanceState {},
ConsensusMetricsValue::default(),
)
.await
Expand Down
17 changes: 11 additions & 6 deletions crates/hotshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,13 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>> SystemContext<TYPES, I> {
memberships: Memberships<TYPES>,
networks: Networks<TYPES, I>,
initializer: HotShotInitializer<TYPES>,
instance_state: TYPES::InstanceState,
metrics: ConsensusMetricsValue,
) -> Result<Self, HotShotError<TYPES>> {
debug!("Creating a new hotshot");

let consensus_metrics = Arc::new(metrics);
let anchored_leaf = initializer.inner;
let instance_state = initializer.instance_state;

// insert to storage
storage
Expand Down Expand Up @@ -442,7 +442,6 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>> SystemContext<TYPES, I> {
memberships: Memberships<TYPES>,
networks: Networks<TYPES, I>,
initializer: HotShotInitializer<TYPES>,
instance_state: TYPES::InstanceState,
metrics: ConsensusMetricsValue,
) -> Result<
(
Expand All @@ -461,7 +460,6 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>> SystemContext<TYPES, I> {
memberships,
networks,
initializer,
instance_state,
metrics,
)
.await?;
Expand Down Expand Up @@ -749,6 +747,9 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>> ConsensusApi<TYPES, I>
pub struct HotShotInitializer<TYPES: NodeType> {
/// the leaf specified initialization
inner: Leaf<TYPES>,

/// Instance-level state.
instance_state: TYPES::InstanceState,
}

impl<TYPES: NodeType> HotShotInitializer<TYPES> {
Expand All @@ -760,11 +761,15 @@ impl<TYPES: NodeType> HotShotInitializer<TYPES> {
) -> Result<Self, HotShotError<TYPES>> {
Ok(Self {
inner: Leaf::genesis(instance_state),
instance_state: instance_state.clone(),
})
}

/// reload previous state based on most recent leaf
pub fn from_reload(anchor_leaf: Leaf<TYPES>) -> Self {
Self { inner: anchor_leaf }
/// reload previous state based on most recent leaf and the instance-level state.
pub fn from_reload(anchor_leaf: Leaf<TYPES>, instance_state: TYPES::InstanceState) -> Self {
Self {
inner: anchor_leaf,
instance_state,
}
}
}
9 changes: 4 additions & 5 deletions crates/task-impls/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,9 +594,8 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, A: ConsensusApi<TYPES, I> +
};
let Ok(state) = parent_state.validate_and_apply_header(
&consensus.instance_state,
&proposal.data.block_header.clone(),
&parent.block_header.clone(),
&view,
&proposal.data.block_header.clone(),
) else {
error!("Block header doesn't extend the proposal",);
return;
Expand Down Expand Up @@ -1218,11 +1217,11 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, A: ConsensusApi<TYPES, I> +

if let Some(commit_and_metadata) = &self.payload_commitment_and_metadata {
let block_header = TYPES::BlockHeader::new(
commit_and_metadata.commitment,
commit_and_metadata.metadata.clone(),
state,
&consensus.instance_state,
&parent_header,
state,
commit_and_metadata.commitment,
commit_and_metadata.metadata.clone(),
);
let leaf = Leaf {
view_number: view,
Expand Down
6 changes: 3 additions & 3 deletions crates/testing/src/block_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@ impl BlockHeader for TestBlockHeader {
type State = TestValidatedState;

fn new(
payload_commitment: VidCommitment,
_metadata: <Self::Payload as BlockPayload>::Metadata,
_parent_state: &Self::State,
_instance_state: &<Self::State as ValidatedState>::Instance,
parent_header: &Self,
_parent_state: &Self::State,
payload_commitment: VidCommitment,
_metadata: <Self::Payload as BlockPayload>::Metadata,
) -> Self {
Self {
block_number: parent_header.block_number + 1,
Expand Down
18 changes: 2 additions & 16 deletions crates/testing/src/state_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use commit::{Commitment, Committable};
use hotshot_types::{
data::{fake_commitment, BlockError, ViewNumber},
traits::{
node_implementation::ConsensusTime,
states::{InstanceState, TestableState, ValidatedState},
BlockPayload,
},
Expand All @@ -18,7 +17,7 @@ use crate::block_types::{TestBlockHeader, TestBlockPayload};
pub use crate::node_types::TestTypes;

/// Instance-level state implementation for testing purposes.
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct TestInstanceState {}

impl InstanceState for TestInstanceState {}
Expand All @@ -28,8 +27,6 @@ impl InstanceState for TestInstanceState {}
pub struct TestValidatedState {
/// the block height
block_height: u64,
/// the view number
view_number: ViewNumber,
/// the previous state commitment
prev_state_commitment: Commitment<Self>,
}
Expand All @@ -38,7 +35,6 @@ impl Committable for TestValidatedState {
fn commit(&self) -> Commitment<Self> {
commit::RawCommitmentBuilder::new("Test State Commit")
.u64_field("block_height", self.block_height)
.u64_field("view_number", *self.view_number)
.field("prev_state_commitment", self.prev_state_commitment)
.finalize()
}
Expand All @@ -52,7 +48,6 @@ impl Default for TestValidatedState {
fn default() -> Self {
Self {
block_height: 0,
view_number: ViewNumber::genesis(),
prev_state_commitment: fake_commitment(),
}
}
Expand All @@ -72,20 +67,11 @@ impl ValidatedState for TestValidatedState {
fn validate_and_apply_header(
&self,
_instance: &Self::Instance,
_proposed_header: &Self::BlockHeader,
_parent_header: &Self::BlockHeader,
view_number: &Self::Time,
_proposed_header: &Self::BlockHeader,
) -> Result<Self, Self::Error> {
if view_number == &ViewNumber::genesis() {
if &self.view_number != view_number {
return Err(BlockError::InvalidBlockHeader);
}
} else if self.view_number >= *view_number {
return Err(BlockError::InvalidBlockHeader);
}
Ok(TestValidatedState {
block_height: self.block_height + 1,
view_number: *view_number,
prev_state_commitment: self.commit(),
})
}
Expand Down
14 changes: 4 additions & 10 deletions crates/testing/src/task_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ pub async fn build_system_handle(
memberships,
networks_bundle,
initializer,
TestInstanceState {},
ConsensusMetricsValue::default(),
)
.await
Expand Down Expand Up @@ -242,11 +241,11 @@ async fn build_quorum_proposal_and_signature(
let mut parent_state =
<TestValidatedState as ValidatedState>::from_header(&parent_leaf.block_header);
let block_header = TestBlockHeader::new(
payload_commitment,
(),
&parent_state,
&TestInstanceState {},
&parent_leaf.block_header,
&parent_state,
payload_commitment,
(),
);
// current leaf that can be re-assigned everytime when entering a new view
let mut leaf = Leaf {
Expand All @@ -271,12 +270,7 @@ async fn build_quorum_proposal_and_signature(
// Only view 2 is tested, higher views are not tested
for cur_view in 2..=view {
let state_new_view = parent_state
.validate_and_apply_header(
&TestInstanceState {},
&block_header,
&block_header,
&ViewNumber::new(cur_view - 1),
)
.validate_and_apply_header(&TestInstanceState {}, &block_header, &block_header)
.unwrap();
// save states for the previous view to pass all the qc checks
// In the long term, we want to get rid of this, do not manually update consensus state
Expand Down
1 change: 0 additions & 1 deletion crates/testing/src/test_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ where
memberships,
network_bundle,
initializer,
TestInstanceState {},
ConsensusMetricsValue::default(),
)
.await
Expand Down
6 changes: 3 additions & 3 deletions crates/types/src/traits/block_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ pub trait BlockHeader:
/// Build a header with the payload commitment, metadata, instance-level state, parent header,
/// and parent state.
fn new(
payload_commitment: VidCommitment,
metadata: <Self::Payload as BlockPayload>::Metadata,
parent_state: &Self::State,
instance_state: &<Self::State as ValidatedState>::Instance,
parent_header: &Self,
parent_state: &Self::State,
payload_commitment: VidCommitment,
metadata: <Self::Payload as BlockPayload>::Metadata,
) -> Self;

/// Build the genesis header, payload, and metadata.
Expand Down
5 changes: 2 additions & 3 deletions crates/types/src/traits/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde::{de::DeserializeOwned, Serialize};
use std::{error::Error, fmt::Debug, hash::Hash};

/// Instance-level state, which allows us to fetch missing validated state.
pub trait InstanceState: Debug + Send + Sync {}
pub trait InstanceState: Clone + Debug + Send + Sync {}

/// Abstraction over the state that blocks modify
///
Expand Down Expand Up @@ -48,9 +48,8 @@ pub trait ValidatedState:
fn validate_and_apply_header(
&self,
instance: &Self::Instance,
proposed_header: &Self::BlockHeader,
parent_header: &Self::BlockHeader,
view_number: &Self::Time,
proposed_header: &Self::BlockHeader,
) -> Result<Self, Self::Error>;

/// Construct the state with the given block header.
Expand Down

0 comments on commit 84ea862

Please sign in to comment.