-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Block producer selects da height to never exceed u64::MAX - 1 transactions from L1 #2189
Changes from 8 commits
ad1d2b1
4c527aa
87efdde
cec811a
f355d25
5b48295
6b5008d
f45d770
ca016df
4a5c9e9
03f4160
7949399
9a91abe
3252f59
7767d32
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,13 +139,16 @@ impl fuel_core_producer::ports::Relayer for MaybeRelayerAdapter { | |
} | ||
} | ||
|
||
async fn get_cost_for_block(&self, height: &DaBlockHeight) -> anyhow::Result<u64> { | ||
async fn get_cost_and_transactions_number_for_block( | ||
&self, | ||
height: &DaBlockHeight, | ||
) -> anyhow::Result<(u64, u64)> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer more structure with fields instead of tuple |
||
#[cfg(feature = "relayer")] | ||
{ | ||
if let Some(sync) = self.relayer_synced.as_ref() { | ||
get_gas_cost_for_height(**height, sync) | ||
get_gas_cost_and_transactions_number_for_height(**height, sync) | ||
} else { | ||
Ok(0) | ||
Ok((0, 0)) | ||
} | ||
} | ||
#[cfg(not(feature = "relayer"))] | ||
|
@@ -155,29 +158,38 @@ impl fuel_core_producer::ports::Relayer for MaybeRelayerAdapter { | |
"Cannot have a da height above zero without a relayer" | ||
); | ||
// If the relayer is not enabled, then all blocks are zero. | ||
Ok(0) | ||
Ok((0, 0)) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "relayer")] | ||
fn get_gas_cost_for_height( | ||
fn get_gas_cost_and_transactions_number_for_height( | ||
height: u64, | ||
sync: &fuel_core_relayer::SharedState< | ||
crate::database::Database< | ||
crate::database::database_description::relayer::Relayer, | ||
>, | ||
>, | ||
) -> anyhow::Result<u64> { | ||
) -> anyhow::Result<(u64, u64)> { | ||
let da_height = DaBlockHeight(height); | ||
let cost = sync | ||
let cost_with_transactions = sync | ||
.database() | ||
.storage::<fuel_core_relayer::storage::EventsHistory>() | ||
.get(&da_height)? | ||
.unwrap_or_default() | ||
.iter() | ||
.fold(0u64, |acc, event| acc.saturating_add(event.cost())); | ||
Ok(cost) | ||
.fold((0u64, 0u64), |(cost, transactions), event| { | ||
let cost = cost.saturating_add(event.cost()); | ||
let transactions = match event { | ||
fuel_core_types::services::relayer::Event::Message(_) => transactions, | ||
fuel_core_types::services::relayer::Event::Transaction(_) => { | ||
transactions.saturating_add(1) | ||
} | ||
}; | ||
(cost, transactions) | ||
}); | ||
Ok(cost_with_transactions) | ||
} | ||
|
||
impl fuel_core_producer::ports::BlockProducerDatabase for OnChainIterableKeyValueView { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -974,7 +974,7 @@ where | |
return Err(ForcedTransactionFailure::InsufficientMaxGas { | ||
claimed_max_gas, | ||
actual_max_gas, | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: remove this ";" |
||
}) | ||
xgreenx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
Ok(()) | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -390,6 +390,52 @@ mod produce_and_execute_block_txpool { | |||||
assert_eq!(expected, actual); | ||||||
} | ||||||
|
||||||
#[tokio::test] | ||||||
async fn will_only_advance_da_height_if_enough_transactions_remaining() { | ||||||
// given | ||||||
let prev_da_height = 100; | ||||||
let prev_height = 1u32.into(); | ||||||
// 0 + 15_000 + 15_000 + 15_000 + 21_000 = 66_000 > 65_535 | ||||||
let latest_blocks_with_transaction_numbers = vec![ | ||||||
(prev_da_height, 0u64), | ||||||
(prev_da_height + 1, 15_000), | ||||||
(prev_da_height + 2, 15_000), | ||||||
(prev_da_height + 3, 15_000), | ||||||
(prev_da_height + 4, 21_000), | ||||||
] | ||||||
.into_iter() | ||||||
.map(|(height, gas_cost)| (DaBlockHeight(height), gas_cost)); | ||||||
|
||||||
let ctx = TestContextBuilder::new() | ||||||
.with_latest_block_height((prev_da_height + 4u64).into()) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
.with_latest_blocks_with_transactions(latest_blocks_with_transaction_numbers) | ||||||
.with_prev_da_height(prev_da_height.into()) | ||||||
.with_prev_height(prev_height) | ||||||
.build(); | ||||||
|
||||||
let producer = ctx.producer(); | ||||||
let next_height = prev_height | ||||||
.succ() | ||||||
.expect("The block height should be valid"); | ||||||
|
||||||
// when | ||||||
let res = producer | ||||||
.produce_and_execute_block_txpool(next_height, Tai64::now()) | ||||||
.await | ||||||
.unwrap(); | ||||||
|
||||||
// then | ||||||
let expected = prev_da_height + 3; | ||||||
let actual: u64 = res | ||||||
.into_result() | ||||||
.block | ||||||
.header() | ||||||
.application() | ||||||
.da_height | ||||||
.into(); | ||||||
assert_eq!(expected, actual); | ||||||
} | ||||||
|
||||||
#[tokio::test] | ||||||
async fn if_each_block_is_full_then_only_advance_one_at_a_time() { | ||||||
// given | ||||||
|
@@ -800,7 +846,7 @@ impl<Executor> TestContext<Executor> { | |||||
|
||||||
struct TestContextBuilder { | ||||||
latest_block_height: DaBlockHeight, | ||||||
blocks_with_gas_costs: HashMap<DaBlockHeight, u64>, | ||||||
blocks_with_gas_costs_and_transactions_number: HashMap<DaBlockHeight, (u64, u64)>, | ||||||
prev_da_height: DaBlockHeight, | ||||||
block_gas_limit: Option<u64>, | ||||||
prev_height: BlockHeight, | ||||||
|
@@ -810,7 +856,7 @@ impl TestContextBuilder { | |||||
fn new() -> Self { | ||||||
Self { | ||||||
latest_block_height: 0u64.into(), | ||||||
blocks_with_gas_costs: HashMap::new(), | ||||||
blocks_with_gas_costs_and_transactions_number: HashMap::new(), | ||||||
prev_da_height: 1u64.into(), | ||||||
block_gas_limit: None, | ||||||
prev_height: 0u32.into(), | ||||||
|
@@ -822,12 +868,47 @@ impl TestContextBuilder { | |||||
self | ||||||
} | ||||||
|
||||||
fn with_latest_blocks_with_gas_costs_and_transactions_number( | ||||||
mut self, | ||||||
latest_blocks_with_gas_costs_and_transactions: impl Iterator< | ||||||
Item = (DaBlockHeight, (u64, u64)), | ||||||
>, | ||||||
) -> Self { | ||||||
self.blocks_with_gas_costs_and_transactions_number | ||||||
.extend(latest_blocks_with_gas_costs_and_transactions); | ||||||
self | ||||||
} | ||||||
|
||||||
// Helper function that can be used in tests where transaction numbers in a da block are irrelevant | ||||||
fn with_latest_blocks_with_gas_costs( | ||||||
mut self, | ||||||
latest_blocks_with_gas_costs: impl Iterator<Item = (DaBlockHeight, u64)>, | ||||||
) -> Self { | ||||||
self.blocks_with_gas_costs | ||||||
.extend(latest_blocks_with_gas_costs); | ||||||
let latest_blocks_with_gas_costs_and_transactions_number = | ||||||
latest_blocks_with_gas_costs | ||||||
.into_iter() | ||||||
.map(|(da_block_height, gas_costs)| (da_block_height, (gas_costs, 0))); | ||||||
// Assigning `self` necessary to avoid the compiler complaining about the mutability of `self` | ||||||
self = self.with_latest_blocks_with_gas_costs_and_transactions_number( | ||||||
latest_blocks_with_gas_costs_and_transactions_number, | ||||||
); | ||||||
self | ||||||
} | ||||||
|
||||||
// Helper function that can be used in tests where gas costs in a da block are irrelevant | ||||||
fn with_latest_blocks_with_transactions( | ||||||
mut self, | ||||||
latest_blocks_with_transactions: impl Iterator<Item = (DaBlockHeight, u64)>, | ||||||
) -> Self { | ||||||
let latest_blocks_with_gas_costs_and_transactions_number = | ||||||
latest_blocks_with_transactions.into_iter().map( | ||||||
|(da_block_height, transactions)| (da_block_height, (0, transactions)), | ||||||
); | ||||||
|
||||||
// Assigning `self` necessary to avoid the compiler complaining about the mutability of `self` | ||||||
self = self.with_latest_blocks_with_gas_costs_and_transactions_number( | ||||||
latest_blocks_with_gas_costs_and_transactions_number, | ||||||
); | ||||||
self | ||||||
} | ||||||
|
||||||
|
@@ -877,7 +958,9 @@ impl TestContextBuilder { | |||||
|
||||||
let mock_relayer = MockRelayer { | ||||||
latest_block_height: self.latest_block_height, | ||||||
latest_da_blocks_with_costs: self.blocks_with_gas_costs.clone(), | ||||||
latest_da_blocks_with_costs_and_transactions_number: self | ||||||
.blocks_with_gas_costs_and_transactions_number | ||||||
.clone(), | ||||||
..Default::default() | ||||||
}; | ||||||
|
||||||
|
@@ -919,7 +1002,9 @@ impl TestContextBuilder { | |||||
|
||||||
let mock_relayer = MockRelayer { | ||||||
latest_block_height: self.latest_block_height, | ||||||
latest_da_blocks_with_costs: self.blocks_with_gas_costs.clone(), | ||||||
latest_da_blocks_with_costs_and_transactions_number: self | ||||||
.blocks_with_gas_costs_and_transactions_number | ||||||
.clone(), | ||||||
..Default::default() | ||||||
}; | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to move this into
unreleased
section because of release=)