Skip to content

Commit

Permalink
Fix l1 fee rate selection and error handling in sequencer (#565)
Browse files Browse the repository at this point in the history
* fix l1 fee rate selection and error handling in sequencer

* Update crates/soft-confirmation-rule-enforcer/src/query.rs

Co-authored-by: Roman <[email protected]>

* Refactor + clamp

---------

Co-authored-by: Roman <[email protected]>
Co-authored-by: otaliptus <[email protected]>
  • Loading branch information
3 people authored May 14, 2024
1 parent 822a5b3 commit d147e1c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bin/citrea/tests/e2e/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1843,7 +1843,7 @@ async fn sequencer_crash_and_replace_full_node() -> Result<(), anyhow::Error> {

#[tokio::test]
async fn transaction_failing_on_l1_is_removed_from_mempool() -> Result<(), anyhow::Error> {
citrea::initialize_logging();
// citrea::initialize_logging();

let (seq_test_client, full_node_test_client, seq_task, full_node_task, _) =
initialize_test(Default::default()).await;
Expand Down
7 changes: 5 additions & 2 deletions crates/sequencer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ schnellru = "0.2.1"
sov-rollup-interface = { path = "../sovereign-sdk/rollup-interface", features = [
"native",
] }
citrea-evm = { path = "../evm" }
citrea-evm = { path = "../evm", features = ["native"] }
sov-db = { path = "../sovereign-sdk/full-node/db/sov-db", version = "0.3" }

sov-stf-runner = { path = "../sovereign-sdk/full-node/sov-stf-runner" }
Expand All @@ -57,6 +57,10 @@ sov-modules-api = { path = "../sovereign-sdk/module-system/sov-modules-api", def
sov-accounts = { path = "../sovereign-sdk/module-system/module-implementations/sov-accounts", default-features = false }
sov-state = { path = "../sovereign-sdk/module-system/sov-state" }
sov-mock-da = { path = "../sovereign-sdk/adapters/mock-da", default-features = false }
soft-confirmation-rule-enforcer = { path = "../soft-confirmation-rule-enforcer", features = [
"native",
] }

hex = { workspace = true }

shared-backup-db = { path = "../shared-backup-db" }
Expand All @@ -68,4 +72,3 @@ tempfile = { workspace = true }
[features]
default = []
local = []
native = ["citrea-stf/native", "citrea-evm/native"]
22 changes: 22 additions & 0 deletions crates/sequencer/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use reth_primitives::{FromRecoveredPooledTransaction, IntoRecoveredTransaction};
use reth_provider::BlockReaderIdExt;
use reth_transaction_pool::{BestTransactionsAttributes, EthPooledTransaction, PoolTransaction};
use shared_backup_db::{CommitmentStatus, PostgresConnector, SharedBackupDbConfig};
use soft_confirmation_rule_enforcer::SoftConfirmationRuleEnforcer;
use sov_accounts::Accounts;
use sov_accounts::Response::{AccountEmpty, AccountExists};
use sov_db::ledger_db::{LedgerDB, SlotCommit};
Expand Down Expand Up @@ -74,6 +75,7 @@ where
state_root: StateRoot<Stf, Vm, Da::Spec>,
sequencer_pub_key: Vec<u8>,
rpc_config: RpcConfig,
soft_confirmation_rule_enforcer: SoftConfirmationRuleEnforcer<C, Da::Spec>,
}

enum L2BlockMode {
Expand Down Expand Up @@ -138,6 +140,9 @@ where
let sov_tx_signer_priv_key =
C::PrivateKey::try_from(&hex::decode(&config.private_key).unwrap()).unwrap();

let soft_confirmation_rule_enforcer =
SoftConfirmationRuleEnforcer::<C, <Da as DaService>::Spec>::default();

Ok(Self {
da_service,
mempool: Arc::new(pool),
Expand All @@ -154,6 +159,7 @@ where
state_root: prev_state_root,
sequencer_pub_key: public_keys.sequencer_public_key,
rpc_config,
soft_confirmation_rule_enforcer,
})
}

Expand Down Expand Up @@ -424,6 +430,10 @@ where
err
);
batch_workspace.revert();
return Err(anyhow!(
"Failed to apply begin soft confirmation hook: {:?}",
err
));
}
}
Ok(())
Expand Down Expand Up @@ -464,12 +474,16 @@ where
last_finalized_height
);

let fee_rate_range = self.get_l1_fee_rate_range()?;

let l1_fee_rate = self
.da_service
.get_fee_rate()
.await
.map_err(|e| anyhow!(e))?;

let l1_fee_rate = l1_fee_rate.clamp(*fee_rate_range.start(), *fee_rate_range.end());

let new_da_block = match last_finalized_height.cmp(&prev_l1_height) {
Ordering::Less => {
panic!("DA L1 height is less than Ledger finalized height");
Expand Down Expand Up @@ -862,4 +876,12 @@ where
None => Ok(()),
}
}

fn get_l1_fee_rate_range(&self) -> Result<RangeInclusive<u128>, anyhow::Error> {
let mut working_set = WorkingSet::<C>::new(self.storage.clone());

self.soft_confirmation_rule_enforcer
.get_next_min_max_l1_fee_rate(&mut working_set)
.map_err(|e| anyhow::anyhow!("Error reading min max l1 fee rate: {}", e))
}
}
28 changes: 28 additions & 0 deletions crates/soft-confirmation-rule-enforcer/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ops::RangeInclusive;

use jsonrpsee::core::RpcResult;
use sov_modules_api::macros::rpc_gen;
use sov_modules_api::{Context, DaSpec, StateMapAccessor, StateValueAccessor, WorkingSet};
Expand Down Expand Up @@ -52,4 +54,30 @@ impl<C: Context, Da: DaSpec> SoftConfirmationRuleEnforcer<C, Da> {
pub fn get_last_timestamp(&self, working_set: &mut WorkingSet<C>) -> RpcResult<u64> {
Ok(self.last_timestamp.get(working_set).unwrap_or(0))
}

/// function to get min and max for next L1 fee rate
pub fn get_next_min_max_l1_fee_rate(
&self,
working_set: &mut WorkingSet<C>,
) -> RpcResult<RangeInclusive<u128>> {
let last_l1_fee_rate = self.last_l1_fee_rate.get(working_set).unwrap_or(0);

if last_l1_fee_rate == 0 {
// on the first soft confirmation, we don't have a last fee rate
return Ok(0..=u128::MAX);
}

let l1_fee_rate_change_percentage = self
.l1_fee_rate_change_percentage
.get(working_set)
.expect("L1 fee rate change should be set");

let min = last_l1_fee_rate
.saturating_sub((last_l1_fee_rate * l1_fee_rate_change_percentage) / 100);

let max = last_l1_fee_rate
.saturating_add((last_l1_fee_rate * l1_fee_rate_change_percentage) / 100);

Ok(min..=max)
}
}

0 comments on commit d147e1c

Please sign in to comment.