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

feat: Header pruning #351

Merged
merged 7 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ celestia-tendermint-proto = "0.32.1"

[patch.crates-io]
# Uncomment to apply local changes
#beetswap = { path = "../beetswap" }
#blockstore = { path = "../blockstore" }
#celestia-tendermint = { path = "../celestia-tendermint-rs/tendermint" }
#celestia-tendermint-proto = { path = "../celestia-tendermint-rs/proto" }
Expand Down
22 changes: 21 additions & 1 deletion node/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,18 @@ pub enum NodeEvent {
error: String,
},

/// Pruned headers up to and including specified height.
PrunedHeaders {
/// Last header height that was pruned
to_height: u64,
},

/// Pruning fatal error.
FatalPrunerError {
/// A human readable error.
error: String,
},

/// Network was compromised.
///
/// This happens when a valid bad encoding fraud proof is received.
Expand All @@ -296,6 +308,7 @@ impl NodeEvent {
match self {
NodeEvent::FatalDaserError { .. }
| NodeEvent::FatalSyncerError { .. }
| NodeEvent::FatalPrunerError { .. }
| NodeEvent::FetchingHeadersFailed { .. }
| NodeEvent::NetworkCompromised => true,
NodeEvent::ConnectingToBootnodes
Expand All @@ -308,7 +321,8 @@ impl NodeEvent {
| NodeEvent::FetchingHeadHeaderStarted
| NodeEvent::FetchingHeadHeaderFinished { .. }
| NodeEvent::FetchingHeadersStarted { .. }
| NodeEvent::FetchingHeadersFinished { .. } => false,
| NodeEvent::FetchingHeadersFinished { .. }
| NodeEvent::PrunedHeaders { .. } => false,
}
}
}
Expand Down Expand Up @@ -413,6 +427,12 @@ impl fmt::Display for NodeEvent {
NodeEvent::FatalSyncerError { error } => {
write!(f, "Syncer stopped because of a fatal error: {error}")
}
Self::PrunedHeaders { to_height } => {
write!(f, "Pruned headers up to and including {to_height}")
}
NodeEvent::FatalPrunerError { error } => {
write!(f, "Pruner stopped because of a fatal error: {error}")
}
NodeEvent::NetworkCompromised => {
write!(f, "The network is compromised and should not be trusted. ")?;
write!(f, "Node stopped synchronizing and sampling, but you can still make some queries to the network.")
Expand Down
1 change: 1 addition & 0 deletions node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod network;
pub mod node;
mod p2p;
mod peer_tracker;
mod pruner;
pub mod store;
mod syncer;
#[cfg(any(test, feature = "test-utils"))]
Expand Down
13 changes: 12 additions & 1 deletion node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::daser::{Daser, DaserArgs};
use crate::events::{EventChannel, EventSubscriber, NodeEvent};
use crate::executor::spawn;
use crate::p2p::{P2p, P2pArgs};
use crate::pruner::{Pruner, PrunerArgs};
use crate::store::{SamplingMetadata, Store, StoreError};
use crate::syncer::{Syncer, SyncerArgs};

Expand Down Expand Up @@ -90,6 +91,7 @@ where
store: Arc<S>,
syncer: Arc<Syncer<S>>,
_daser: Arc<Daser>,
_pruner: Arc<Pruner>,
tasks_cancellation_token: CancellationToken,
}

Expand Down Expand Up @@ -125,7 +127,7 @@ where
local_keypair: config.p2p_local_keypair,
bootnodes: config.p2p_bootnodes,
listen_on: config.p2p_listen_on,
blockstore,
blockstore: blockstore.clone(),
store: store.clone(),
event_pub: event_channel.publisher(),
})
Expand All @@ -145,13 +147,20 @@ where
event_pub: event_channel.publisher(),
})?);

let pruner = Arc::new(Pruner::start(PrunerArgs {
store: store.clone(),
blockstore,
event_pub: event_channel.publisher(),
}));

// spawn the task that will stop the services when the fraud is detected
let network_compromised_token = p2p.get_network_compromised_token().await?;
let tasks_cancellation_token = CancellationToken::new();

spawn({
let syncer = syncer.clone();
let daser = daser.clone();
let pruner = pruner.clone();
let tasks_cancellation_token = tasks_cancellation_token.child_token();
let event_pub = event_channel.publisher();

Expand All @@ -161,6 +170,7 @@ where
_ = network_compromised_token.cancelled() => {
syncer.stop();
daser.stop();
pruner.stop();

if event_pub.has_subscribers() {
event_pub.send(NodeEvent::NetworkCompromised);
Expand All @@ -180,6 +190,7 @@ where
store,
syncer,
_daser: daser,
_pruner: pruner,
tasks_cancellation_token,
};

Expand Down
Loading
Loading