Skip to content
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

fix: save every new checkpoint #413

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions ethereum/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
pub finalized_block_recv: Option<watch::Receiver<Option<Block<Transaction>>>>,
pub checkpoint_recv: watch::Receiver<Option<B256>>,
genesis_time: u64,
db: DB,
db: Arc<DB>,

Check warning on line 46 in ethereum/src/consensus.rs

View workflow job for this annotation

GitHub Actions / check

field `db` is never read

Check warning on line 46 in ethereum/src/consensus.rs

View workflow job for this annotation

GitHub Actions / test

field `db` is never read
config: Arc<Config>,
phantom: PhantomData<(S, R)>,
}
Expand Down Expand Up @@ -80,11 +80,6 @@
}

fn shutdown(&self) -> Result<()> {
let checkpoint = self.checkpoint_recv.borrow();
if let Some(checkpoint) = checkpoint.as_ref() {
self.db.save_checkpoint(*checkpoint)?;
}

Ok(())
}
}
Expand All @@ -98,7 +93,7 @@
let config_clone = config.clone();
let rpc = rpc.to_string();
let genesis_time = config.chain.genesis_time;
let db = DB::new(&config)?;
let db = Arc::new(DB::new(&config)?);
let initial_checkpoint = config
.checkpoint
.unwrap_or_else(|| db.load_checkpoint().unwrap_or(config.default_checkpoint));
Expand Down Expand Up @@ -160,6 +155,8 @@
}
});

save_new_checkpoints(checkpoint_recv.clone(), db.clone(), initial_checkpoint);

Ok(ConsensusClient {
block_recv: Some(block_recv),
finalized_block_recv: Some(finalized_block_recv),
Expand All @@ -178,6 +175,39 @@
}
}

fn save_new_checkpoints<DB: Database>(
mut checkpoint_recv: watch::Receiver<Option<B256>>,
db: Arc<DB>,
initial_checkpoint: B256,
) {
#[cfg(not(target_arch = "wasm32"))]
let run = tokio::spawn;

#[cfg(target_arch = "wasm32")]
let run = wasm_bindgen_futures::spawn_local;

run(async move {
let mut last_saved_checkpoint = initial_checkpoint;
loop {
let new_checkpoint = *checkpoint_recv.borrow_and_update();
if let Some(new_checkpoint) = new_checkpoint.as_ref() {
if *new_checkpoint != last_saved_checkpoint {
// There is a more recent checkpoint to save
if db.save_checkpoint(*new_checkpoint).is_err() {
warn!(target: "helios::consensus", "failed to save checkpoint");
} else {
info!(target: "helios::consensus", "saved checkpoint to DB: 0x{}", hex::encode(*new_checkpoint));
last_saved_checkpoint = *new_checkpoint;
}
}
}
if checkpoint_recv.changed().await.is_err() {
break;
}
}
});
}

async fn sync_fallback<S: ConsensusSpec, R: ConsensusRpc<S>>(
inner: &mut Inner<S, R>,
fallback: &str,
Expand Down Expand Up @@ -675,10 +705,10 @@
consensus::calc_sync_period,
consensus::Inner,
constants::MAX_REQUEST_LIGHT_CLIENT_UPDATES,
rpc::{mock_rpc::MockRpc, ConsensusRpc},

Check warning on line 708 in ethereum/src/consensus.rs

View workflow job for this annotation

GitHub Actions / test

unused import: `ConsensusRpc`
};

async fn get_client(strict_checkpoint_age: bool, sync: bool) -> Inner<MockRpc> {

Check failure on line 711 in ethereum/src/consensus.rs

View workflow job for this annotation

GitHub Actions / test

struct takes 2 generic arguments but 1 generic argument was supplied
let base_config = networks::mainnet();
let config = Config {
consensus_rpc: String::new(),
Expand Down Expand Up @@ -728,7 +758,7 @@

#[tokio::test]
async fn test_verify_update_invalid_committee() {
let client = get_client(false, false).await;

Check failure on line 761 in ethereum/src/consensus.rs

View workflow job for this annotation

GitHub Actions / test

the trait bound `MockRpc: ConsensusSpec` is not satisfied
let period = calc_sync_period(client.store.finalized_header.beacon.slot.into());
let updates = client
.rpc
Expand All @@ -748,7 +778,7 @@

#[tokio::test]
async fn test_verify_update_invalid_finality() {
let client = get_client(false, false).await;

Check failure on line 781 in ethereum/src/consensus.rs

View workflow job for this annotation

GitHub Actions / test

the trait bound `MockRpc: ConsensusSpec` is not satisfied
let period = calc_sync_period(client.store.finalized_header.beacon.slot.into());
let updates = client
.rpc
Expand All @@ -768,7 +798,7 @@

#[tokio::test]
async fn test_verify_update_invalid_sig() {
let client = get_client(false, false).await;

Check failure on line 801 in ethereum/src/consensus.rs

View workflow job for this annotation

GitHub Actions / test

the trait bound `MockRpc: ConsensusSpec` is not satisfied
let period = calc_sync_period(client.store.finalized_header.beacon.slot.into());
let updates = client
.rpc
Expand All @@ -788,7 +818,7 @@

#[tokio::test]
async fn test_verify_finality() {
let client = get_client(false, true).await;

Check failure on line 821 in ethereum/src/consensus.rs

View workflow job for this annotation

GitHub Actions / test

the trait bound `MockRpc: ConsensusSpec` is not satisfied

let update = client.rpc.get_finality_update().await.unwrap();

Expand All @@ -797,7 +827,7 @@

#[tokio::test]
async fn test_verify_finality_invalid_finality() {
let client = get_client(false, true).await;

Check failure on line 830 in ethereum/src/consensus.rs

View workflow job for this annotation

GitHub Actions / test

the trait bound `MockRpc: ConsensusSpec` is not satisfied

let mut update = client.rpc.get_finality_update().await.unwrap();
update.finalized_header = LightClientHeader::default();
Expand All @@ -811,7 +841,7 @@

#[tokio::test]
async fn test_verify_finality_invalid_sig() {
let client = get_client(false, true).await;

Check failure on line 844 in ethereum/src/consensus.rs

View workflow job for this annotation

GitHub Actions / test

the trait bound `MockRpc: ConsensusSpec` is not satisfied

let mut update = client.rpc.get_finality_update().await.unwrap();
update.sync_aggregate.sync_committee_signature = Signature::default();
Expand All @@ -826,6 +856,6 @@
#[tokio::test]
#[should_panic]
async fn test_verify_checkpoint_age_invalid() {
get_client(true, false).await;

Check failure on line 859 in ethereum/src/consensus.rs

View workflow job for this annotation

GitHub Actions / test

the trait bound `MockRpc: ConsensusSpec` is not satisfied
}
}
Loading