Skip to content

Commit

Permalink
nearcore: run node in archival mode if database is an archive (#7752)
Browse files Browse the repository at this point in the history
Previously, if neard wasn’t configured to run as an archival node
(i.e. `archive` setting in `config.json` was false) but the database
was an archive, the node would fail.  Change this behaviour so that it
warns and continues running as archival node.

The grand plan is to get rid of the `archive` setting and have type of
the node be determined by the database.  This will reduce number of
special cases to be handled when opening a database.
  • Loading branch information
mina86 authored and nikurt committed Nov 9, 2022
1 parent 7f97723 commit a39445b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
40 changes: 26 additions & 14 deletions nearcore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,17 @@ pub fn get_default_home() -> PathBuf {
}

/// Opens node’s storage performing migrations and checks when necessary.
fn open_storage(home_dir: &Path, near_config: &NearConfig) -> anyhow::Result<NodeStorage> {
///
/// If opened storage is an RPC store and `near_config.config.archive` is true,
/// converts the storage to archival node. Otherwise, if opening archival node
/// with that field being false, prints a warning and sets the field to `true`.
/// In other words, once store is archival, the node will act as archival nod
/// regardless of settings in `config.json`.
///
/// The end goal is to get rid of `archive` option in `config.json` file and
/// have the type of the node be determined purely based on kind of database
/// being opened.
fn open_storage(home_dir: &Path, near_config: &mut NearConfig) -> anyhow::Result<NodeStorage> {
let migrator = migrations::Migrator::new(near_config);
let opener = NodeStorage::opener(home_dir, &near_config.config.store).with_migrator(&migrator);
let res = match opener.open() {
Expand Down Expand Up @@ -110,20 +120,22 @@ fn open_storage(home_dir: &Path, near_config: &NearConfig) -> anyhow::Result<Nod
res.with_context(|| format!("unable to open database at {}", opener.path().display()))?;

// Check if the storage is an archive and if it is make sure we are too.
// If the store is not marked as archive but we are an archival node that is
// fine and we just need to mark the store as archival.
let hot = storage.get_store(Temperature::Hot);
let store_is_archive: bool =
hot.get_ser(DBCol::BlockMisc, near_store::db::IS_ARCHIVE_KEY)?.unwrap_or_default();
let client_is_archive = near_config.client_config.archive;
anyhow::ensure!(
!store_is_archive || client_is_archive,
"The node is configured as non-archival but is using database of an archival node."
);
if !store_is_archive && client_is_archive {
let mut update = hot.store_update();
update.set_ser(DBCol::BlockMisc, near_store::db::IS_ARCHIVE_KEY, &true)?;
update.commit()?;
if store_is_archive != near_config.client_config.archive {
if store_is_archive {
tracing::info!(target: "neard", "Opening an archival database.");
tracing::warn!(target: "neard", "Ignoring `archive` client configuration and forcing the node to run in archive mode.");
near_config.client_config.archive = true;
} else {
tracing::info!(target: "neard", "Running node in archival mode (as per `archive` client configuration).");
tracing::info!(target: "neard", "Marking database as archival.");
tracing::warn!(target: "neard", "Starting node in non-archival mode will no longer be possible with this database.");
let mut update = hot.store_update();
update.set_ser(DBCol::BlockMisc, near_store::db::IS_ARCHIVE_KEY, &true)?;
update.commit()?;
}
}

Ok(storage)
Expand All @@ -142,12 +154,12 @@ pub fn start_with_config(home_dir: &Path, config: NearConfig) -> anyhow::Result<

pub fn start_with_config_and_synchronization(
home_dir: &Path,
config: NearConfig,
mut config: NearConfig,
// 'shutdown_signal' will notify the corresponding `oneshot::Receiver` when an instance of
// `ClientActor` gets dropped.
shutdown_signal: Option<oneshot::Sender<()>>,
) -> anyhow::Result<NearNode> {
let store = open_storage(home_dir, &config)?;
let store = open_storage(home_dir, &mut config)?;

let runtime = Arc::new(NightshadeRuntime::from_config(
home_dir,
Expand Down
4 changes: 3 additions & 1 deletion neard/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ impl InitCmd {

#[derive(Parser)]
pub(super) struct RunCmd {
/// Keep old blocks in the storage (default false).
/// Configure node to run as archival node which prevents deletion of old
/// blocks. This is a persistent setting; once client is started as
/// archival node, it cannot be run in non-archival mode.
#[clap(long)]
archive: bool,
/// Set the boot nodes to bootstrap network from.
Expand Down

0 comments on commit a39445b

Please sign in to comment.