Skip to content

Commit

Permalink
Database version upgrade logic
Browse files Browse the repository at this point in the history
  • Loading branch information
someone235 committed Nov 18, 2023
1 parent 099c658 commit 51730e3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
24 changes: 23 additions & 1 deletion consensus/src/consensus/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub enum ConsensusEntryType {
New(ConsensusEntry),
}

#[derive(Serialize, Deserialize, Clone, Default)]
#[derive(Serialize, Deserialize, Clone)]
pub struct MultiConsensusMetadata {
current_consensus_key: Option<u64>,
staging_consensus_key: Option<u64>,
Expand All @@ -54,6 +54,20 @@ pub struct MultiConsensusMetadata {
version: u32,
}

const LATEST_DB_VERSION: u32 = 1;
impl Default for MultiConsensusMetadata {
fn default() -> Self {
Self {
current_consensus_key: Default::default(),
staging_consensus_key: Default::default(),
max_key_used: Default::default(),
is_archival_node: Default::default(),
props: Default::default(),
version: LATEST_DB_VERSION,
}
}
}

#[derive(Clone)]
pub struct MultiConsensusManagementStore {
db: Arc<DB>,
Expand Down Expand Up @@ -190,6 +204,14 @@ impl MultiConsensusManagementStore {
self.metadata.write(BatchDbWriter::new(&mut batch), &metadata).unwrap();
}
}

pub fn should_upgrade(&self) -> StoreResult<bool> {
match self.metadata.read() {
Ok(data) => Ok(data.version != LATEST_DB_VERSION),
Err(StoreError::KeyNotFound(_)) => Ok(false),
Err(err) => Err(err),
}
}
}

pub struct Factory {
Expand Down
7 changes: 5 additions & 2 deletions kaspad/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ do you confirm? (answer y/n or pass --yes to the Kaspad command line to confirm
.unwrap();

// TEMP: upgrade from Alpha version or any version before this one
if meta_db.get_pinned(b"multi-consensus-metadata-key").is_ok_and(|r| r.is_some()) {
if meta_db.get_pinned(b"multi-consensus-metadata-key").is_ok_and(|r| r.is_some())
|| MultiConsensusManagementStore::new(meta_db.clone()).should_upgrade().unwrap()
{
let msg = "Node database is from an older Kaspad version and needs to be fully deleted, do you confirm the delete? (y/n)";
get_user_approval_or_exit(msg, args.yes);

Expand All @@ -269,7 +271,8 @@ do you confirm? (answer y/n or pass --yes to the Kaspad command line to confirm
.unwrap();
}

if !args.archival && MultiConsensusManagementStore::new(meta_db.clone()).is_archival_node().unwrap() {
let meta_store = MultiConsensusManagementStore::new(meta_db.clone());
if !args.archival && meta_store.is_archival_node().unwrap() {
get_user_approval_or_exit("--archival is set to false although the node was previously archival. Proceeding may delete archived data. Do you confirm? (y/n)", args.yes);
}

Expand Down

0 comments on commit 51730e3

Please sign in to comment.