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

Add a tool to reset persistent storage #888

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ jobs:
target/release/sequencer
target/release/cli
target/release/commitment-task
target/release/reset-storage

build-arm:
runs-on: [self-hosted, ARM64]
Expand Down Expand Up @@ -101,6 +102,7 @@ jobs:
target/release/sequencer
target/release/cli
target/release/commitment-task
target/release/reset-storage

build-dockers:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build_static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ jobs:
${{ env.CARGO_TARGET_DIR }}/${{ env.TARGET_TRIPLET }}/release/sequencer
${{ env.CARGO_TARGET_DIR }}/${{ env.TARGET_TRIPLET }}/release/cli
${{ env.CARGO_TARGET_DIR }}/${{ env.TARGET_TRIPLET }}/release/commitment-task
${{ env.CARGO_TARGET_DIR }}/${{ env.TARGET_TRIPLET }}/release/reset-storage

static-dockers:
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions docker/sequencer.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ ENTRYPOINT ["tini", "--"]
COPY target/$TARGETARCH/release/sequencer /bin/sequencer
RUN chmod +x /bin/sequencer

COPY target/$TARGETARCH/release/reset-storage /bin/reset-storage
RUN chmod +x /bin/reset-storage

# Set a path to save the consensus config on startup.
#
# Upon restart, the config will be loaded from this file and the node will be able to resume
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-docker-images
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ for ARCH in "amd64" "arm64"; do
;;
esac
mkdir -p ${WORKDIR}/target/$ARCH/release
for binary in "orchestrator" "web-server" "sequencer" "commitment-task"; do
for binary in "orchestrator" "web-server" "sequencer" "commitment-task" "reset-storage"; do
cp -v "${CARGO_TARGET_DIR}/${TARGET}/release/$binary" ${WORKDIR}/target/$ARCH/release
done
done
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-docker-images-native
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ cleanup(){
}

mkdir -p ${WORKDIR}/target/$ARCH/release
for binary in "orchestrator" "web-server" "sequencer" "commitment-task"; do
for binary in "orchestrator" "web-server" "sequencer" "commitment-task" "reset-storage"; do
cp -v "${CARGO_TARGET_DIR}/release/$binary" ${WORKDIR}/target/$ARCH/release
# Patch the interpreter for running without nix inside the ubuntu based docker image.
if [ $KERNEL == "linux" ]; then
Expand Down
2 changes: 1 addition & 1 deletion sequencer/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use data_source::SubmitDataSource;
use hotshot::types::SystemContextHandle;
use hotshot_query_service::data_source::ExtensibleDataSource;

mod data_source;
pub mod data_source;
pub mod endpoints;
pub mod fs;
pub mod options;
Expand Down
11 changes: 8 additions & 3 deletions sequencer/src/api/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@ use hotshot_query_service::{
QueryResult,
};

pub trait DataSourceOptions {
type DataSource: SequencerDataSource<Options = Self>;
fn reset_storage(&mut self);
}

/// A data source with sequencer-specific functionality.
///
/// This trait extends the generic [`AvailabilityDataSource`] with some additional data needed to
/// provided sequencer-specific endpoints.
#[async_trait]
pub(crate) trait SequencerDataSource:
pub trait SequencerDataSource:
AvailabilityDataSource<SeqTypes>
+ StatusDataSource
+ UpdateDataSource<SeqTypes>
+ VersionedDataSource
+ Sized
{
type Options;
type Options: DataSourceOptions<DataSource = Self>;

/// Instantiate a data source from command line options.
async fn create(opt: Self::Options) -> anyhow::Result<Self>;
Expand All @@ -42,7 +47,7 @@ pub(crate) trait SequencerDataSource:
ID: Into<BlockId<SeqTypes>> + Send + Sync;
}

pub(crate) trait SubmitDataSource<N: network::Type> {
pub trait SubmitDataSource<N: network::Type> {
fn handle(&self) -> &SystemContextHandle<SeqTypes, Node<N>>;
}

Expand Down
22 changes: 20 additions & 2 deletions sequencer/src/api/options.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Sequencer-specific API options and initialization.

use super::{
data_source::SequencerDataSource, endpoints, fs, sql, update::update_loop, AppState, Consensus,
NodeIndex, SequencerNode,
data_source::{DataSourceOptions, SequencerDataSource},
endpoints, fs, sql,
update::update_loop,
AppState, Consensus, NodeIndex, SequencerNode,
};
use crate::network;
use async_std::{
Expand Down Expand Up @@ -190,6 +192,14 @@ pub struct Sql {
pub reset_store: bool,
}

impl DataSourceOptions for Sql {
type DataSource = sql::DataSource;

fn reset_storage(&mut self) {
self.reset_store = true;
}
}

/// Options for the query API module backed by the file system.
#[derive(Parser, Clone, Debug)]
pub struct Fs {
Expand All @@ -202,6 +212,14 @@ pub struct Fs {
pub reset_store: bool,
}

impl DataSourceOptions for Fs {
type DataSource = fs::DataSource;

fn reset_storage(&mut self) {
self.reset_store = true;
}
}

async fn init_with_query_module<N, D>(
opt: Options,
mod_opt: D::Options,
Expand Down
34 changes: 34 additions & 0 deletions sequencer/src/bin/reset-storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use clap::Parser;
use hotshot_query_service::data_source::VersionedDataSource;
use sequencer::api::{
data_source::{DataSourceOptions, SequencerDataSource},
options,
};

/// Reset the persistent storage of a sequencer.
///
/// This will remove all the persistent storage of a sequencer node, effectively resetting it to
/// its genesis state. Do not run this program while the sequencer is running.
#[derive(Clone, Debug, Parser)]
enum Options {
/// Reset file system storage.
Fs(options::Fs),
/// Reset SQL storage.
Sql(options::Sql),
}

#[async_std::main]
async fn main() -> anyhow::Result<()> {
let opt = Options::parse();
match opt {
Options::Fs(opt) => reset_storage(opt).await,
Options::Sql(opt) => reset_storage(opt).await,
}
}

async fn reset_storage<O: DataSourceOptions>(mut opt: O) -> anyhow::Result<()> {
opt.reset_storage();
let mut ds = O::DataSource::create(opt).await?;
ds.commit().await?;
Ok(())
}