Skip to content

Commit

Permalink
Don't handle proposal before setting the pending block. (#2942)
Browse files Browse the repository at this point in the history
* Don't handle proposal before setting the pending block.

* Fix match assertion in the test.
  • Loading branch information
afck authored Nov 22, 2024
1 parent 860dfe3 commit 274332d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 35 deletions.
22 changes: 13 additions & 9 deletions linera-chain/src/data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ impl Block {
let config = posted_message.message.matches_open_chain()?;
Some((in_bundle, posted_message, config))
}

pub fn check_proposal_size(
&self,
maximum_block_proposal_size: u64,
blobs: &[Blob],
) -> Result<(), ChainError> {
let size = bcs::serialized_size(&(self, blobs))?;
ensure!(
size <= usize::try_from(maximum_block_proposal_size).unwrap_or(usize::MAX),
ChainError::BlockProposalTooLarge
);
Ok(())
}
}

/// A transaction in a block: incoming messages or an operation.
Expand Down Expand Up @@ -776,15 +789,6 @@ impl BlockProposal {
}
}

pub fn check_size(&self, maximum_block_proposal_size: u64) -> Result<(), ChainError> {
let size = bcs::serialized_size(&self)?;
ensure!(
size <= usize::try_from(maximum_block_proposal_size).unwrap_or(usize::MAX),
ChainError::BlockProposalTooLarge
);
Ok(())
}

pub fn check_signature(&self, public_key: PublicKey) -> Result<(), CryptoError> {
self.signature.check(&self.content, public_key)
}
Expand Down
2 changes: 1 addition & 1 deletion linera-core/src/chain_worker/state/temporary_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ where
.expect("chain is active");
check_block_epoch(epoch, block)?;
let policy = committee.policy().clone();
proposal.check_size(policy.maximum_block_proposal_size)?;
// Check the authentication of the block.
let public_key = self
.0
Expand Down Expand Up @@ -224,6 +223,7 @@ where
for blob in blobs {
Self::check_blob_size(blob.content(), &policy)?;
}
block.check_proposal_size(policy.maximum_block_proposal_size, blobs)?;

let local_time = self.0.storage.clock().current_time();
ensure!(
Expand Down
28 changes: 6 additions & 22 deletions linera-core/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1997,13 +1997,8 @@ where

let incoming_bundles = self.pending_message_bundles().await?;
let identity = self.identity().await?;
let info = self.chain_info_with_manager_values().await?;
let round = match Self::round_for_new_proposal(&info, &identity, None)? {
Either::Left(round) => round,
Either::Right(timeout) => return Ok(ExecuteBlockOutcome::WaitForTimeout(timeout)),
};
let confirmed_value = self
.new_pending_block(round, incoming_bundles, operations, identity)
.new_pending_block(incoming_bundles, operations, identity)
.await?;

match self.process_pending_block_without_prepare().await? {
Expand Down Expand Up @@ -2031,7 +2026,6 @@ where
#[instrument(level = "trace", skip(incoming_bundles, operations))]
async fn new_pending_block(
&self,
round: Round,
incoming_bundles: Vec<IncomingBundle>,
operations: Vec<Operation>,
identity: Owner,
Expand Down Expand Up @@ -2066,22 +2060,12 @@ where
let (executed_block, _) = self
.stage_block_execution_and_discard_failing_messages(block)
.await?;
let block = executed_block.block.clone();
let block = &executed_block.block;
let blobs = self.read_local_blobs(block.published_blob_ids()).await?;
let key_pair = self.key_pair().await?;

let proposal = Box::new(BlockProposal::new_initial(
round,
block.clone(),
&key_pair,
blobs,
));
// Check the final block proposal. This will be cheaper after #1401.
self.client
.local_node
.handle_block_proposal(*proposal.clone())
.await?;
self.state_mut().set_pending_block(block);
let committee = self.local_committee().await?;
let max_size = committee.policy().maximum_block_proposal_size;
block.check_proposal_size(max_size, &blobs)?;
self.state_mut().set_pending_block(block.clone());

Ok(HashedCertificateValue::new_confirmed(executed_block))
}
Expand Down
6 changes: 3 additions & 3 deletions linera-core/src/unit_tests/client_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2470,9 +2470,9 @@ where
let result = client1.publish_data_blobs(blob_bytes).await;
assert_matches!(
result,
Err(ChainClientError::LocalNodeError(
LocalNodeError::WorkerError(WorkerError::ChainError(chain_error))
)) if matches!(*chain_error, ChainError::BlockProposalTooLarge)
Err(ChainClientError::ChainError(
ChainError::BlockProposalTooLarge
))
);

let result = client1.publish_data_blob(large_blob_bytes).await;
Expand Down

0 comments on commit 274332d

Please sign in to comment.