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

Write database version file to disk #2034

Merged
merged 1 commit into from
Dec 18, 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
23 changes: 22 additions & 1 deletion zilliqa/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
collections::BTreeMap,
fmt::Debug,
fs::{self, File},
fs::{self, File, OpenOptions},
io::{BufReader, BufWriter, Read, Write},
ops::Range,
path::{Path, PathBuf},
Expand Down Expand Up @@ -179,6 +179,11 @@ enum BlockFilter {

const CHECKPOINT_HEADER_BYTES: [u8; 8] = *b"ZILCHKPT";

/// Version string that is written to disk along with the persisted database. This should be bumped whenever we make a
/// backwards incompatible change to our database format. This should be done rarely, since it forces all node
/// operators to re-sync.
const CURRENT_DB_VERSION: &str = "1";

#[derive(Debug)]
pub struct Db {
db: Arc<Mutex<Connection>>,
Expand All @@ -195,6 +200,22 @@ impl Db {
Some(path) => {
let path = path.as_ref().join(shard_id.to_string());
fs::create_dir_all(&path).context(format!("Unable to create {path:?}"))?;

let mut version_file = OpenOptions::new()
.create(true)
.truncate(false)
.read(true)
.write(true)
.open(path.join("version"))?;
let mut version = String::new();
version_file.read_to_string(&mut version)?;

if !version.is_empty() && version != CURRENT_DB_VERSION {
return Err(anyhow!("data is incompatible with this version - please delete the data and re-sync"));
}

version_file.write_all(CURRENT_DB_VERSION.as_bytes())?;

let db_path = path.join("db.sqlite3");
(
Connection::open(&db_path)
Expand Down
Loading