Skip to content

Commit

Permalink
Add max epoch duration (#630)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop authored Oct 6, 2023
1 parent feee1e4 commit 9a71483
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
15 changes: 15 additions & 0 deletions src/task_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ pub struct Options {
#[clap(long, env, default_value = "100")]
pub min_batch_deletion_size: usize,

/// The parameter to control the delay between mining a deletion batch and
/// inserting the recovery identities
///
/// The sequencer will insert the recovery identities after
/// max_epoch_duration_seconds + root_history_expiry) seconds have passed
///
/// By default the value is set to 0 so the sequencer will only use
/// root_history_expiry
#[clap(long, env, default_value = "0")]
pub max_epoch_duration_seconds: u64,

/// How many identities can be held in the API insertion queue at any given
/// time Past this limit the API request will block until the queue has
/// space for the insertion.
Expand Down Expand Up @@ -122,6 +133,7 @@ pub struct TaskMonitor {
// Finalization params
scanning_window_size: u64,
time_between_scans: Duration,
max_epoch_duration: Duration,
// TODO: docs
batch_deletion_timeout_seconds: i64,
// TODO: docs
Expand All @@ -142,6 +154,7 @@ impl TaskMonitor {
batch_deletion_timeout_seconds: _,
min_batch_deletion_size: _,
insert_identities_capacity: _,
max_epoch_duration_seconds,
} = *options;

Self {
Expand All @@ -154,6 +167,7 @@ impl TaskMonitor {
time_between_scans: Duration::from_secs(time_between_scans_seconds),
batch_deletion_timeout_seconds: options.batch_deletion_timeout_seconds,
min_batch_deletion_size: options.min_batch_deletion_size,
max_epoch_duration: Duration::from_secs(max_epoch_duration_seconds),
}
}

Expand Down Expand Up @@ -183,6 +197,7 @@ impl TaskMonitor {
self.tree_state.get_mined_tree(),
self.scanning_window_size,
self.time_between_scans,
self.max_epoch_duration,
);

let finalize_identities_handle = crate::utils::spawn_monitored_with_backoff(
Expand Down
42 changes: 31 additions & 11 deletions src/task_monitor/tasks/finalize_identities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use std::time::Duration;

use anyhow::{Context, Result as AnyhowResult};
use chrono::{DateTime, Utc};
use chrono::Utc;
use ethers::abi::RawLog;
use ethers::contract::EthEvent;
use ethers::providers::Middleware;
Expand All @@ -25,6 +25,7 @@ pub struct FinalizeRoots {

scanning_window_size: u64,
time_between_scans: Duration,
max_epoch_duration: Duration,
}

impl FinalizeRoots {
Expand All @@ -35,6 +36,7 @@ impl FinalizeRoots {
finalized_tree: TreeVersion<Canonical>,
scanning_window_size: u64,
time_between_scans: Duration,
max_epoch_duration: Duration,
) -> Arc<Self> {
Arc::new(Self {
database,
Expand All @@ -43,6 +45,7 @@ impl FinalizeRoots {
finalized_tree,
scanning_window_size,
time_between_scans,
max_epoch_duration,
})
}

Expand All @@ -54,6 +57,7 @@ impl FinalizeRoots {
&self.finalized_tree,
self.scanning_window_size,
self.time_between_scans,
self.max_epoch_duration,
)
.await
}
Expand All @@ -66,6 +70,7 @@ async fn finalize_roots_loop(
finalized_tree: &TreeVersion<Canonical>,
scanning_window_size: u64,
time_between_scans: Duration,
max_epoch_duration: Duration,
) -> AnyhowResult<()> {
let mainnet_abi = identity_manager.abi();
let secondary_abis = identity_manager.secondary_abis();
Expand All @@ -80,7 +85,14 @@ async fn finalize_roots_loop(
loop {
let mainnet_logs = fetch_mainnet_logs(&mut mainnet_scanner, mainnet_address).await?;

finalize_mainnet_roots(database, identity_manager, processed_tree, &mainnet_logs).await?;
finalize_mainnet_roots(
database,
identity_manager,
processed_tree,
&mainnet_logs,
max_epoch_duration,
)
.await?;

let mut roots = extract_roots_from_mainnet_logs(mainnet_logs);
roots.extend(fetch_secondary_logs(&mut secondary_scanners).await?);
Expand Down Expand Up @@ -150,6 +162,7 @@ async fn finalize_mainnet_roots(
identity_manager: &IdentityManager,
processed_tree: &TreeVersion<Intermediate>,
logs: &[Log],
max_epoch_duration: Duration,
) -> Result<(), anyhow::Error> {
for log in logs {
let Some(event) = raw_log_to_tree_changed(log) else {
Expand All @@ -175,7 +188,14 @@ async fn finalize_mainnet_roots(
// NOTE: We must do this before updating the tree
// because we fetch commitments from the processed tree
// before they are deleted
update_eligible_recoveries(database, identity_manager, processed_tree, &log).await?;
update_eligible_recoveries(
database,
identity_manager,
processed_tree,
&log,

Check warning on line 195 in src/task_monitor/tasks/finalize_identities.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> src/task_monitor/tasks/finalize_identities.rs:195:17 | 195 | &log, | ^^^^ help: change this to: `log` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow note: the lint level is defined here --> src/lib.rs:2:9 | 2 | #![warn(clippy::all, clippy::pedantic, clippy::cargo)] | ^^^^^^^^^^^ = note: `#[warn(clippy::needless_borrow)]` implied by `#[warn(clippy::all)]`

Check warning on line 195 in src/task_monitor/tasks/finalize_identities.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> src/task_monitor/tasks/finalize_identities.rs:195:17 | 195 | &log, | ^^^^ help: change this to: `log` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow note: the lint level is defined here --> src/lib.rs:2:9 | 2 | #![warn(clippy::all, clippy::pedantic, clippy::cargo)] | ^^^^^^^^^^^ = note: `#[warn(clippy::needless_borrow)]` implied by `#[warn(clippy::all)]`

Check warning on line 195 in src/task_monitor/tasks/finalize_identities.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> src/task_monitor/tasks/finalize_identities.rs:195:17 | 195 | &log, | ^^^^ help: change this to: `log` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow note: the lint level is defined here --> src/lib.rs:2:9 | 2 | #![warn(clippy::all, clippy::pedantic, clippy::cargo)] | ^^^^^^^^^^^ = note: `#[warn(clippy::needless_borrow)]` implied by `#[warn(clippy::all)]`
max_epoch_duration,
)
.await?;
}

let updates_count = processed_tree.apply_updates_up_to(post_root.into());
Expand Down Expand Up @@ -274,6 +294,7 @@ async fn update_eligible_recoveries(
identity_manager: &IdentityManager,
processed_tree: &TreeVersion<Intermediate>,
log: &Log,
max_epoch_duration: Duration,
) -> anyhow::Result<()> {
let tx_hash = log.transaction_hash.context("Missing tx hash")?;
let commitments = identity_manager
Expand All @@ -299,14 +320,13 @@ async fn update_eligible_recoveries(

// Use the root history expiry to calcuate the eligibility timestamp for the new
// insertion
let eligibility_timestamp = DateTime::from_utc(
chrono::NaiveDateTime::from_timestamp_opt(
Utc::now().timestamp() + root_history_expiry.as_u64() as i64,
0,
)
.context("Could not convert eligibility timestamp to NaiveDateTime")?,
Utc,
);
let root_history_expiry_duration =
chrono::Duration::seconds(root_history_expiry.as_u64() as i64);

Check warning on line 324 in src/task_monitor/tasks/finalize_identities.rs

View workflow job for this annotation

GitHub Actions / clippy

casting `u64` to `i64` may wrap around the value

warning: casting `u64` to `i64` may wrap around the value --> src/task_monitor/tasks/finalize_identities.rs:324:35 | 324 | chrono::Duration::seconds(root_history_expiry.as_u64() as i64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(clippy::cast_possible_wrap)]` implied by `#[warn(clippy::pedantic)]`

Check warning on line 324 in src/task_monitor/tasks/finalize_identities.rs

View workflow job for this annotation

GitHub Actions / clippy

casting `u64` to `i64` may wrap around the value

warning: casting `u64` to `i64` may wrap around the value --> src/task_monitor/tasks/finalize_identities.rs:324:35 | 324 | chrono::Duration::seconds(root_history_expiry.as_u64() as i64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(clippy::cast_possible_wrap)]` implied by `#[warn(clippy::pedantic)]`

Check warning on line 324 in src/task_monitor/tasks/finalize_identities.rs

View workflow job for this annotation

GitHub Actions / clippy

casting `u64` to `i64` may wrap around the value

warning: casting `u64` to `i64` may wrap around the value --> src/task_monitor/tasks/finalize_identities.rs:324:35 | 324 | chrono::Duration::seconds(root_history_expiry.as_u64() as i64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(clippy::cast_possible_wrap)]` implied by `#[warn(clippy::pedantic)]`
let max_epoch_duration = chrono::Duration::from_std(max_epoch_duration)?;

let delay = root_history_expiry_duration + max_epoch_duration;

let eligibility_timestamp = Utc::now() + delay;

// For each deletion, if there is a corresponding recovery, insert a new
// identity with the specified eligibility timestamp
Expand Down

0 comments on commit 9a71483

Please sign in to comment.