Skip to content

Commit

Permalink
Block proposal size limits
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-ds committed Nov 16, 2024
1 parent e3b4729 commit ce8e26e
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ View or update the resource control policy
* `--maximum-executed-block-size <MAXIMUM_EXECUTED_BLOCK_SIZE>` — Set the maximum size of an executed block, in bytes
* `--maximum-blob-size <MAXIMUM_BLOB_SIZE>` — Set the maximum size of data blobs, compressed bytecode and other binary blobs, in bytes
* `--maximum-bytecode-size <MAXIMUM_BYTECODE_SIZE>` — Set the maximum size of decompressed contract or service bytecode, in bytes
* `--maximum-block-proposal-size <MAXIMUM_BLOCK_PROPOSAL_SIZE>` — Set the maximum size of a block proposal, in bytes
* `--maximum-bytes-read-per-block <MAXIMUM_BYTES_READ_PER_BLOCK>` — Set the maximum read data per block
* `--maximum-bytes-written-per-block <MAXIMUM_BYTES_WRITTEN_PER_BLOCK>` — Set the maximum write data per block

Expand Down Expand Up @@ -493,6 +494,7 @@ Create genesis configuration for a Linera deployment. Create initial user chains
* `--maximum-executed-block-size <MAXIMUM_EXECUTED_BLOCK_SIZE>` — Set the maximum size of an executed block
* `--maximum-bytecode-size <MAXIMUM_BYTECODE_SIZE>` — Set the maximum size of decompressed contract or service bytecode, in bytes
* `--maximum-blob-size <MAXIMUM_BLOB_SIZE>` — Set the maximum size of data blobs, compressed bytecode and other binary blobs, in bytes
* `--maximum-block-proposal-size <MAXIMUM_BLOCK_PROPOSAL_SIZE>` — Set the maximum size of a block proposal, in bytes
* `--maximum-bytes-read-per-block <MAXIMUM_BYTES_READ_PER_BLOCK>` — Set the maximum read data per block
* `--maximum-bytes-written-per-block <MAXIMUM_BYTES_WRITTEN_PER_BLOCK>` — Set the maximum write data per block
* `--testing-prng-seed <TESTING_PRNG_SEED>` — Force this wallet to generate keys using a PRNG and a given seed. USE FOR TESTING ONLY
Expand Down
10 changes: 10 additions & 0 deletions linera-chain/src/data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{collections::HashSet, fmt};
use async_graphql::SimpleObject;
use custom_debug_derive::Debug;
use linera_base::{
bcs,
crypto::{BcsHashable, BcsSignable, CryptoError, CryptoHash, KeyPair, PublicKey, Signature},
data_types::{Amount, Blob, BlockHeight, OracleResponse, Round, Timestamp},
doc_scalar, ensure, hex_debug,
Expand Down Expand Up @@ -775,6 +776,15 @@ 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
5 changes: 5 additions & 0 deletions linera-chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod test;
pub use chain::ChainStateView;
use data_types::{MessageBundle, Origin, PostedMessage};
use linera_base::{
bcs,
crypto::{CryptoError, CryptoHash},
data_types::{ArithmeticError, BlockHeight, Round, Timestamp},
identifiers::{ApplicationId, BlobId, ChainId},
Expand Down Expand Up @@ -138,6 +139,10 @@ pub enum ChainError {
CertificateSignatureVerificationFailed { error: String },
#[error("Internal error {0}")]
InternalError(String),
#[error("Block proposal is too large")]
BlockProposalTooLarge,
#[error(transparent)]
BcsError(#[from] bcs::Error),
#[error("Insufficient balance to pay the fees")]
InsufficientBalance,
#[error("Invalid owner weights: {0}")]
Expand Down
2 changes: 1 addition & 1 deletion linera-chain/src/unit_tests/chain_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ async fn test_block_size_limit() {
let mut chain = ChainStateView::new(chain_id).await;

// The size of the executed valid block below.
let maximum_executed_block_size = 691;
let maximum_executed_block_size = 699;

// Initialize the chain.
let mut config = make_open_chain_config();
Expand Down
8 changes: 8 additions & 0 deletions linera-client/src/client_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,10 @@ pub enum ClientCommand {
#[arg(long)]
maximum_bytecode_size: Option<u64>,

/// Set the maximum size of a block proposal, in bytes.
#[arg(long)]
maximum_block_proposal_size: Option<u64>,

/// Set the maximum read data per block.
#[arg(long)]
maximum_bytes_read_per_block: Option<u64>,
Expand Down Expand Up @@ -669,6 +673,10 @@ pub enum ClientCommand {
#[arg(long)]
maximum_blob_size: Option<u64>,

/// Set the maximum size of a block proposal, in bytes.
#[arg(long)]
maximum_block_proposal_size: Option<u64>,

/// Set the maximum read data per block.
#[arg(long)]
maximum_bytes_read_per_block: Option<u64>,
Expand Down
3 changes: 2 additions & 1 deletion linera-core/src/chain_worker/state/temporary_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ where
.system
.current_committee()
.expect("chain is active");
let policy = committee.policy().clone();
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
28 changes: 27 additions & 1 deletion linera-core/src/unit_tests/client_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use linera_execution::{
};
use linera_storage::{DbStorage, TestClock};
use linera_views::memory::MemoryStore;
use rand::Rng;
use test_case::test_case;

#[cfg(feature = "dynamodb")]
Expand Down Expand Up @@ -2408,6 +2409,7 @@ where
let large_blob_bytes = b"blob+".to_vec();
let policy = ResourceControlPolicy {
maximum_blob_size: blob_bytes.len() as u64,
maximum_block_proposal_size: (blob_bytes.len() * 100) as u64,
..ResourceControlPolicy::default()
};
let mut builder = TestBuilder::new(storage_builder, 4, 0)
Expand Down Expand Up @@ -2456,8 +2458,32 @@ where
assert_eq!(executed_block.block.incoming_bundles.len(), 1);
assert_eq!(executed_block.required_blob_ids().len(), 1);

// This will go way over the limit, because of the different overheads.
let blob_bytes = (0..100)
.map(|_| {
rand::thread_rng()
.sample_iter(&rand::distributions::Standard)
.take(policy.maximum_blob_size as usize)
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let result = client1.publish_data_blobs(blob_bytes).await;
assert_matches!(
result,
Err(ChainClientError::LocalNodeError(
LocalNodeError::WorkerError(WorkerError::BlockProposalTooLarge)

Check failure on line 2474 in linera-core/src/unit_tests/client_tests.rs

View workflow job for this annotation

GitHub Actions / default-features-and-witty-integration-test

no variant or associated item named `BlockProposalTooLarge` found for enum `WorkerError` in the current scope

Check failure on line 2474 in linera-core/src/unit_tests/client_tests.rs

View workflow job for this annotation

GitHub Actions / ethereum-tests

no variant or associated item named `BlockProposalTooLarge` found for enum `WorkerError` in the current scope

Check failure on line 2474 in linera-core/src/unit_tests/client_tests.rs

View workflow job for this annotation

GitHub Actions / storage-service-tests

no variant or associated item named `BlockProposalTooLarge` found for enum `WorkerError` in the current scope

Check failure on line 2474 in linera-core/src/unit_tests/client_tests.rs

View workflow job for this annotation

GitHub Actions / lint-check-all-features

no variant or associated item named `BlockProposalTooLarge` found for enum `WorkerError` in the current scope
))
);
// TODO(#2906): Remove this once the client properly clears the pending block.
client1.clear_pending_block();

let result = client1.publish_data_blob(large_blob_bytes).await;
assert_matches!(result, Err(ChainClientError::LocalNodeError(_)));
assert_matches!(
result,
Err(ChainClientError::LocalNodeError(
LocalNodeError::WorkerError(WorkerError::BlobTooLarge)
))
);

Ok(())
}
6 changes: 6 additions & 0 deletions linera-execution/src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub struct ResourceControlPolicy {
pub maximum_bytecode_size: u64,
/// The maximum size of a blob.
pub maximum_blob_size: u64,
/// The maximum size of a block proposal.
pub maximum_block_proposal_size: u64,
/// The maximum data to read per block
pub maximum_bytes_read_per_block: u64,
/// The maximum data to write per block
Expand All @@ -71,6 +73,7 @@ impl fmt::Display for ResourceControlPolicy {
maximum_executed_block_size,
maximum_blob_size,
maximum_bytecode_size,
maximum_block_proposal_size,
maximum_bytes_read_per_block,
maximum_bytes_written_per_block,
} = self;
Expand All @@ -92,6 +95,7 @@ impl fmt::Display for ResourceControlPolicy {
{maximum_executed_block_size} maximum size of an executed block\n\
{maximum_blob_size} maximum size of a data blob, bytecode or other binary blob\n\
{maximum_bytecode_size} maximum size of service and contract bytecode\n\
{maximum_block_proposal_size} maximum size of a block proposal\n\
{maximum_bytes_read_per_block} maximum number bytes read per block\n\
{maximum_bytes_written_per_block} maximum number bytes written per block",
)
Expand All @@ -116,6 +120,7 @@ impl Default for ResourceControlPolicy {
maximum_executed_block_size: u64::MAX,
maximum_blob_size: u64::MAX,
maximum_bytecode_size: u64::MAX,
maximum_block_proposal_size: u64::MAX,
maximum_bytes_read_per_block: u64::MAX,
maximum_bytes_written_per_block: u64::MAX,
}
Expand Down Expand Up @@ -237,6 +242,7 @@ impl ResourceControlPolicy {
maximum_executed_block_size: 1_000_000,
maximum_blob_size: 1_000_000,
maximum_bytecode_size: 10_000_000,
maximum_block_proposal_size: 13_000_000,
maximum_bytes_read_per_block: 100_000_000,
maximum_bytes_written_per_block: 10_000_000,
}
Expand Down
5 changes: 3 additions & 2 deletions linera-execution/tests/fee_consumption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ async fn test_fee_consumption(
maximum_executed_block_size: 37,
maximum_blob_size: 41,
maximum_bytecode_size: 43,
maximum_bytes_read_per_block: 47,
maximum_bytes_written_per_block: 53,
maximum_block_proposal_size: 47,
maximum_bytes_read_per_block: 53,
maximum_bytes_written_per_block: 59,
};

let consumed_fees = spends
Expand Down
1 change: 1 addition & 0 deletions linera-rpc/tests/snapshots/format__format.yaml.snap
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ ResourceControlPolicy:
- maximum_executed_block_size: U64
- maximum_bytecode_size: U64
- maximum_blob_size: U64
- maximum_block_proposal_size: U64
- maximum_bytes_read_per_block: U64
- maximum_bytes_written_per_block: U64
Round:
Expand Down
4 changes: 4 additions & 0 deletions linera-service-graphql-client/gql/service_schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,10 @@ input ResourceControlPolicy {
"""
maximumBlobSize: Int!
"""
The maximum size of a block proposal.
"""
maximumBlockProposalSize: Int!
"""
The maximum data to read per block
"""
maximumBytesReadPerBlock: Int!
Expand Down
5 changes: 5 additions & 0 deletions linera-service/src/cli_wrappers/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ impl ClientWrapper {
maximum_executed_block_size,
maximum_blob_size,
maximum_bytecode_size,
maximum_block_proposal_size,
maximum_bytes_read_per_block,
maximum_bytes_written_per_block,
} = policy;
Expand Down Expand Up @@ -286,6 +287,10 @@ impl ClientWrapper {
"--maximum-bytecode-size",
&maximum_bytecode_size.to_string(),
])
.args([
"--maximum-block-proposal-size",
&maximum_block_proposal_size.to_string(),
])
.args([
"--maximum-bytes-read-per-block",
&maximum_bytes_read_per_block.to_string(),
Expand Down
10 changes: 10 additions & 0 deletions linera-service/src/linera/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ impl Runnable for Job {
maximum_executed_block_size,
maximum_blob_size,
maximum_bytecode_size,
maximum_block_proposal_size,
maximum_bytes_read_per_block,
maximum_bytes_written_per_block,
} => {
Expand Down Expand Up @@ -621,6 +622,12 @@ impl Runnable for Job {
if let Some(maximum_blob_size) = maximum_blob_size {
policy.maximum_blob_size = maximum_blob_size;
}
if let Some(maximum_block_proposal_size) =
maximum_block_proposal_size
{
policy.maximum_block_proposal_size =
maximum_block_proposal_size;
}
if let Some(maximum_bytes_read_per_block) =
maximum_bytes_read_per_block
{
Expand Down Expand Up @@ -1385,6 +1392,7 @@ async fn run(options: &ClientOptions) -> Result<i32, anyhow::Error> {
maximum_executed_block_size,
maximum_blob_size,
maximum_bytecode_size,
maximum_block_proposal_size,
maximum_bytes_read_per_block,
maximum_bytes_written_per_block,
testing_prng_seed,
Expand All @@ -1399,6 +1407,7 @@ async fn run(options: &ClientOptions) -> Result<i32, anyhow::Error> {
let maximum_executed_block_size = maximum_executed_block_size.unwrap_or(u64::MAX);
let maximum_blob_size = maximum_blob_size.unwrap_or(u64::MAX);
let maximum_bytecode_size = maximum_bytecode_size.unwrap_or(u64::MAX);
let maximum_block_proposal_size = maximum_block_proposal_size.unwrap_or(u64::MAX);
let policy = ResourceControlPolicy {
block: *block_price,
fuel_unit: *fuel_unit_price,
Expand All @@ -1415,6 +1424,7 @@ async fn run(options: &ClientOptions) -> Result<i32, anyhow::Error> {
maximum_executed_block_size,
maximum_blob_size,
maximum_bytecode_size,
maximum_block_proposal_size,
maximum_bytes_read_per_block,
maximum_bytes_written_per_block,
};
Expand Down

0 comments on commit ce8e26e

Please sign in to comment.