Skip to content

Commit

Permalink
fix(sequencer): respect max_tx_bytes when preparing proposals
Browse files Browse the repository at this point in the history
  • Loading branch information
Lilyjjo committed Apr 3, 2024
1 parent f033435 commit 9ad128f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
35 changes: 33 additions & 2 deletions crates/astria-sequencer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ use crate::{
ibc::component::IbcComponent,
proposal::commitment::{
generate_rollup_datas_commitment,
Example,
GeneratedCommitments,
},
state_ext::{
Expand Down Expand Up @@ -237,7 +238,17 @@ impl App {
self.is_proposer = true;
self.update_state_for_new_round(&storage);

let (signed_txs, txs_to_include) = self.execute_block_data(prepare_proposal.txs).await;
let remaining_tx_bytes = prepare_proposal
.max_tx_bytes
.checked_sub(
i64::try_from(GeneratedCommitments::example().commitments_size())
.expect("failed to turn commitment's usize into i64"),
)
.expect("commitment size should not be larger than the prepare proposal's max bytes");

let (signed_txs, txs_to_include) = self
.execute_block_data(prepare_proposal.txs, Some(remaining_tx_bytes))
.await;

let deposits = self
.state
Expand Down Expand Up @@ -295,7 +306,7 @@ impl App {

let expected_txs_len = txs.len();

let (signed_txs, txs_to_include) = self.execute_block_data(txs.into()).await;
let (signed_txs, txs_to_include) = self.execute_block_data(txs.into(), None).await;

// all txs in the proposal should be deserializable and executable
// if any txs were not deserializeable or executable, they would not have been
Expand Down Expand Up @@ -344,6 +355,7 @@ impl App {
async fn execute_block_data(
&mut self,
txs: Vec<bytes::Bytes>,
mut remaining_cometbft_bytes: Option<i64>,
) -> (Vec<SignedTransaction>, Vec<bytes::Bytes>) {
let mut signed_txs = Vec::with_capacity(txs.len());
let mut validated_txs = Vec::with_capacity(txs.len());
Expand Down Expand Up @@ -387,9 +399,28 @@ impl App {
continue;
}

// Don't include tx if it would make the cometbft block too large
if let Some(remaining) = remaining_cometbft_bytes {
if remaining < i64::try_from(tx.len()).expect("failed to turn tx length into i64") {
debug!(
transaction_hash = %telemetry::display::hex(&tx_hash),
remaining_cometbft_bytes = remaining,
tx_len = tx.len(),
"excluding transaction: max cometbft data limit reached"
);
excluded_tx_count += 1;
continue;
}
}

// store transaction execution result, indexed by tx hash
match self.deliver_tx(signed_tx.clone()).await {
Ok(events) => {
if let Some(mut remaining) = remaining_cometbft_bytes {
remaining -=
i64::try_from(tx.len()).expect("failed to turn tx length into i64");
remaining_cometbft_bytes = Some(remaining);
}
self.execution_result.insert(tx_hash.into(), Ok(events));
signed_txs.push(signed_tx);
validated_txs.push(tx);
Expand Down
19 changes: 19 additions & 0 deletions crates/astria-sequencer/src/proposal/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ impl GeneratedCommitments {
}
}

pub(crate) trait Example {
fn example() -> Self;
fn commitments_size(self) -> usize;
}

impl Example for GeneratedCommitments {
fn example() -> Self {
GeneratedCommitments {
rollup_datas_root: [0; 32],
rollup_ids_root: [0; 32],
}
}

// used for accounting for commitments' data size in cometbft block
fn commitments_size(self) -> usize {
self.rollup_datas_root.len() + self.rollup_ids_root.len()
}
}

/// Called when we receive a `PrepareProposal` or `ProcessProposal` consensus message.
///
/// In the case of `PrepareProposal`, we use this function to generate the `rollup_datas_commitment`
Expand Down

0 comments on commit 9ad128f

Please sign in to comment.