Skip to content

Commit

Permalink
Rename Proposal back to ProposedBlock. (#3169)
Browse files Browse the repository at this point in the history
## Motivation

The discussion
#3101 (comment)
was never resolved; a proposal has a content, which has a proposal.

## Proposal

Rename `Proposal` to `ProposedBlock`, to distinguish it from the outer
type.

(I also added some destructuring in `Block::new`, because I ran into a
naming conflict and this way the compiler will warn us if we add any
fields and forget them there.)

## Test Plan

Only renaming.

## Release Plan

- Nothing to do / These changes follow the usual release cycle.

## Links

- Related to #3101
- [reviewer
checklist](https://github.com/linera-io/linera-protocol/blob/main/CONTRIBUTING.md#reviewer-checklist)
  • Loading branch information
afck authored Jan 23, 2025
1 parent 903b975 commit 2053b61
Show file tree
Hide file tree
Showing 25 changed files with 192 additions and 160 deletions.
82 changes: 54 additions & 28 deletions linera-chain/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use thiserror::Error;
use crate::{
data_types::{
BlockExecutionOutcome, EventRecord, ExecutedBlock, IncomingBundle, Medium, MessageBundle,
OutgoingMessage, Proposal,
OutgoingMessage, ProposedBlock,
},
ChainError,
};
Expand All @@ -34,7 +34,7 @@ pub struct ValidatedBlock(Hashed<Block>);
impl ValidatedBlock {
/// Creates a new `ValidatedBlock` from an `ExecutedBlock`.
pub fn new(block: ExecutedBlock) -> Self {
Self(Hashed::new(Block::new(block.proposal, block.outcome)))
Self(Hashed::new(Block::new(block.block, block.outcome)))
}

pub fn from_hashed(block: Hashed<Block>) -> Self {
Expand Down Expand Up @@ -95,7 +95,7 @@ impl<'de> BcsHashable<'de> for ConfirmedBlock {}

impl ConfirmedBlock {
pub fn new(block: ExecutedBlock) -> Self {
Self(Hashed::new(Block::new(block.proposal, block.outcome)))
Self(Hashed::new(Block::new(block.block, block.outcome)))
}

pub fn from_hashed(block: Hashed<Block>) -> Self {
Expand Down Expand Up @@ -333,21 +333,21 @@ pub struct BlockBody {
}

impl Block {
pub fn new(proposal: Proposal, outcome: BlockExecutionOutcome) -> Self {
let bundles_hash = hashing::hash_vec(&proposal.incoming_bundles);
pub fn new(block: ProposedBlock, outcome: BlockExecutionOutcome) -> Self {
let bundles_hash = hashing::hash_vec(&block.incoming_bundles);
let messages_hash = hashing::hash_vec_vec(&outcome.messages);
let operations_hash = hashing::hash_vec(&proposal.operations);
let operations_hash = hashing::hash_vec(&block.operations);
let oracle_responses_hash = hashing::hash_vec_vec(&outcome.oracle_responses);
let events_hash = hashing::hash_vec_vec(&outcome.events);

let header = BlockHeader {
chain_id: proposal.chain_id,
epoch: proposal.epoch,
height: proposal.height,
timestamp: proposal.timestamp,
chain_id: block.chain_id,
epoch: block.epoch,
height: block.height,
timestamp: block.timestamp,
state_hash: outcome.state_hash,
previous_block_hash: proposal.previous_block_hash,
authenticated_signer: proposal.authenticated_signer,
previous_block_hash: block.previous_block_hash,
authenticated_signer: block.authenticated_signer,
bundles_hash,
operations_hash,
messages_hash,
Expand All @@ -356,8 +356,8 @@ impl Block {
};

let body = BlockBody {
incoming_bundles: proposal.incoming_bundles,
operations: proposal.operations,
incoming_bundles: block.incoming_bundles,
operations: block.operations,
messages: outcome.messages,
oracle_responses: outcome.oracle_responses,
events: outcome.events,
Expand Down Expand Up @@ -510,25 +510,51 @@ impl Block {

impl From<Block> for ExecutedBlock {
fn from(block: Block) -> Self {
let proposal = Proposal {
chain_id: block.header.chain_id,
epoch: block.header.epoch,
height: block.header.height,
timestamp: block.header.timestamp,
incoming_bundles: block.body.incoming_bundles,
operations: block.body.operations,
authenticated_signer: block.header.authenticated_signer,
previous_block_hash: block.header.previous_block_hash,
let Block {
header:
BlockHeader {
chain_id,
epoch,
height,
timestamp,
state_hash,
previous_block_hash,
authenticated_signer,
bundles_hash: _,
operations_hash: _,
messages_hash: _,
oracle_responses_hash: _,
events_hash: _,
},
body:
BlockBody {
incoming_bundles,
operations,
messages,
oracle_responses,
events,
},
} = block;

let block = ProposedBlock {
chain_id,
epoch,
height,
timestamp,
incoming_bundles,
operations,
authenticated_signer,
previous_block_hash,
};

let outcome = BlockExecutionOutcome {
state_hash: block.header.state_hash,
messages: block.body.messages,
oracle_responses: block.body.oracle_responses,
events: block.body.events,
state_hash,
messages,
oracle_responses,
events,
};

ExecutedBlock { proposal, outcome }
ExecutedBlock { block, outcome }
}
}

Expand Down
16 changes: 8 additions & 8 deletions linera-chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ use serde::{Deserialize, Serialize};
use crate::{
data_types::{
BlockExecutionOutcome, ChainAndHeight, ChannelFullName, EventRecord, IncomingBundle,
MessageAction, MessageBundle, Origin, OutgoingMessage, PostedMessage, Proposal, Target,
Transaction,
MessageAction, MessageBundle, Origin, OutgoingMessage, PostedMessage, ProposedBlock,
Target, Transaction,
},
inbox::{Cursor, InboxError, InboxStateView},
manager::ChainManager,
Expand Down Expand Up @@ -247,7 +247,7 @@ pub struct ChainTipState {
impl ChainTipState {
/// Checks that the proposed block is suitable, i.e. at the expected height and with the
/// expected parent.
pub fn verify_block_chaining(&self, new_block: &Proposal) -> Result<(), ChainError> {
pub fn verify_block_chaining(&self, new_block: &ProposedBlock) -> Result<(), ChainError> {
ensure!(
new_block.height == self.next_block_height,
ChainError::UnexpectedBlockHeight {
Expand Down Expand Up @@ -282,7 +282,7 @@ impl ChainTipState {
/// Checks if the measurement counters would be valid.
pub fn verify_counters(
&self,
new_block: &Proposal,
new_block: &ProposedBlock,
outcome: &BlockExecutionOutcome,
) -> Result<(), ChainError> {
let num_incoming_bundles = u32::try_from(new_block.incoming_bundles.len())
Expand Down Expand Up @@ -664,7 +664,7 @@ where
/// * Returns the outcome of the execution.
pub async fn execute_block(
&mut self,
block: &Proposal,
block: &ProposedBlock,
local_time: Timestamp,
replaying_oracle_responses: Option<Vec<Vec<OracleResponse>>>,
) -> Result<BlockExecutionOutcome, ChainError> {
Expand Down Expand Up @@ -934,7 +934,7 @@ where
message_id: MessageId,
posted_message: &PostedMessage,
incoming_bundle: &IncomingBundle,
block: &Proposal,
block: &ProposedBlock,
txn_index: u32,
local_time: Timestamp,
txn_tracker: &mut TransactionTracker,
Expand Down Expand Up @@ -1247,11 +1247,11 @@ where
#[test]
fn empty_block_size() {
let executed_block = crate::data_types::ExecutedBlock {
proposal: crate::test::make_first_block(ChainId::root(0)),
block: crate::test::make_first_block(ChainId::root(0)),
outcome: crate::data_types::BlockExecutionOutcome::default(),
};
let size = bcs::serialized_size(&crate::block::Block::new(
executed_block.proposal,
executed_block.block,
executed_block.outcome,
))
.unwrap();
Expand Down
40 changes: 20 additions & 20 deletions linera-chain/src/data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mod data_types_tests;
/// received ahead of time in the inbox of the chain.
/// * This constraint does not apply to the execution of confirmed blocks.
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize, SimpleObject)]
pub struct Proposal {
pub struct ProposedBlock {
/// The chain to which this block belongs.
pub chain_id: ChainId,
/// The number identifying the current configuration.
Expand Down Expand Up @@ -76,7 +76,7 @@ pub struct Proposal {
pub previous_block_hash: Option<CryptoHash>,
}

impl Proposal {
impl ProposedBlock {
/// Returns all the published blob IDs in this block's operations.
pub fn published_blob_ids(&self) -> BTreeSet<BlobId> {
let mut blob_ids = BTreeSet::new();
Expand Down Expand Up @@ -400,14 +400,14 @@ impl OutgoingMessage {
}
}

/// A [`Proposal`], together with the outcome from its execution.
/// A [`ProposedBlock`], together with the outcome from its execution.
#[derive(Debug, PartialEq, Eq, Hash, Clone, SimpleObject)]
pub struct ExecutedBlock {
pub proposal: Proposal,
pub block: ProposedBlock,
pub outcome: BlockExecutionOutcome,
}

/// The messages and the state hash resulting from a [`Proposal`]'s execution.
/// The messages and the state hash resulting from a [`ProposedBlock`]'s execution.
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize, SimpleObject)]
#[cfg_attr(with_testing, derive(Default))]
pub struct BlockExecutionOutcome {
Expand Down Expand Up @@ -643,9 +643,9 @@ impl ExecutedBlock {
certificate_hash: CryptoHash,
) -> impl Iterator<Item = (Epoch, MessageBundle)> + 'a {
let mut index = 0u32;
let block_height = self.proposal.height;
let block_timestamp = self.proposal.timestamp;
let block_epoch = self.proposal.epoch;
let block_height = self.block.height;
let block_timestamp = self.block.timestamp;
let block_epoch = self.block.epoch;

(0u32..)
.zip(self.messages())
Expand Down Expand Up @@ -676,7 +676,7 @@ impl ExecutedBlock {
operation_index: usize,
message_index: u32,
) -> Option<MessageId> {
let block = &self.proposal;
let block = &self.block;
let transaction_index = block.incoming_bundles.len().checked_add(operation_index)?;
if message_index
>= u32::try_from(self.outcome.messages.get(transaction_index)?.len()).ok()?
Expand All @@ -702,7 +702,7 @@ impl ExecutedBlock {
height,
index,
} = message_id;
if self.proposal.chain_id != *chain_id || self.proposal.height != *height {
if self.block.chain_id != *chain_id || self.block.height != *height {
return None;
}
let mut index = usize::try_from(*index).ok()?;
Expand All @@ -718,28 +718,28 @@ impl ExecutedBlock {
/// Returns the message ID belonging to the `index`th outgoing message in this block.
pub fn message_id(&self, index: u32) -> MessageId {
MessageId {
chain_id: self.proposal.chain_id,
height: self.proposal.height,
chain_id: self.block.chain_id,
height: self.block.height,
index,
}
}

pub fn required_blob_ids(&self) -> HashSet<BlobId> {
let mut blob_ids = self.outcome.oracle_blob_ids();
blob_ids.extend(self.proposal.published_blob_ids());
blob_ids.extend(self.block.published_blob_ids());
blob_ids
}

pub fn requires_blob(&self, blob_id: &BlobId) -> bool {
self.outcome.oracle_blob_ids().contains(blob_id)
|| self.proposal.published_blob_ids().contains(blob_id)
|| self.block.published_blob_ids().contains(blob_id)
}
}

impl BlockExecutionOutcome {
pub fn with(self, block: Proposal) -> ExecutedBlock {
pub fn with(self, block: ProposedBlock) -> ExecutedBlock {
ExecutedBlock {
proposal: block,
block,
outcome: self,
}
}
Expand Down Expand Up @@ -768,7 +768,7 @@ impl BlockExecutionOutcome {
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct ProposalContent {
/// The proposed block.
pub proposal: Proposal,
pub block: ProposedBlock,
/// The consensus round in which this proposal is made.
pub round: Round,
/// If this is a retry from an earlier round, the execution outcome.
Expand All @@ -779,13 +779,13 @@ pub struct ProposalContent {
impl BlockProposal {
pub fn new_initial(
round: Round,
proposal: Proposal,
block: ProposedBlock,
secret: &KeyPair,
blobs: Vec<Blob>,
) -> Self {
let content = ProposalContent {
round,
proposal,
block,
outcome: None,
};
let signature = Signature::new(&content, secret);
Expand All @@ -809,7 +809,7 @@ impl BlockProposal {
let block = validated_block_certificate.into_inner().into_inner();
let executed_block: ExecutedBlock = block.into();
let content = ProposalContent {
proposal: executed_block.proposal,
block: executed_block.block,
round,
outcome: Some(executed_block.outcome),
};
Expand Down
12 changes: 6 additions & 6 deletions linera-chain/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ use serde::{Deserialize, Serialize};

use crate::{
block::{ConfirmedBlock, Timeout, ValidatedBlock},
data_types::{BlockProposal, ExecutedBlock, LiteVote, Proposal, Vote},
data_types::{BlockProposal, ExecutedBlock, LiteVote, ProposedBlock, Vote},
types::{TimeoutCertificate, ValidatedBlockCertificate},
ChainError,
};
Expand Down Expand Up @@ -131,7 +131,7 @@ impl LockedBlock {

pub fn chain_id(&self) -> ChainId {
match self {
Self::Fast(proposal) => proposal.content.proposal.chain_id,
Self::Fast(proposal) => proposal.content.block.chain_id,
Self::Regular(certificate) => certificate.value().inner().chain_id(),
}
}
Expand Down Expand Up @@ -285,7 +285,7 @@ where

/// Verifies the safety of a proposed block with respect to voting rules.
pub fn check_proposed_block(&self, proposal: &BlockProposal) -> Result<Outcome, ChainError> {
let new_block = &proposal.content.proposal;
let new_block = &proposal.content.block;
if let Some(old_proposal) = self.proposed.get() {
if old_proposal.content == proposal.content {
return Ok(Outcome::Skip); // We already voted for this proposal; nothing to do.
Expand All @@ -304,7 +304,7 @@ where
// validated block certificate, or it must propose the same block.
ensure!(
proposal.validated_block_certificate.is_some()
|| new_block == &old_proposal.content.proposal,
|| new_block == &old_proposal.content.block,
ChainError::HasLockedBlock(new_block.height, Round::Fast)
);
}
Expand Down Expand Up @@ -811,9 +811,9 @@ impl ChainManagerInfo {
}

/// Returns whether a proposal with this content was already handled.
pub fn already_handled_proposal(&self, round: Round, block: &Proposal) -> bool {
pub fn already_handled_proposal(&self, round: Round, block: &ProposedBlock) -> bool {
self.requested_proposed.as_ref().is_some_and(|proposal| {
proposal.content.round == round && proposal.content.proposal == *block
proposal.content.round == round && proposal.content.block == *block
})
}

Expand Down
Loading

0 comments on commit 2053b61

Please sign in to comment.