Skip to content

Commit

Permalink
Migration for TrieRefcountAddition/Subtraction.
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-near committed Nov 1, 2023
1 parent b5980cc commit 5385426
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 25 deletions.
40 changes: 24 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/store/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pub type DbVersion = u32;

/// Current version of the database.
pub const DB_VERSION: DbVersion = 38;
pub const DB_VERSION: DbVersion = 39;

/// Database version at which point DbKind was introduced.
const DB_VERSION_WITH_KIND: DbVersion = 34;
Expand Down
60 changes: 59 additions & 1 deletion core/store/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::metadata::DbKind;
use crate::{DBCol, Store, StoreUpdate};
use crate::trie::{TrieRefcountAddition, TrieRefcountSubtraction};
use crate::{DBCol, Store, StoreUpdate, TrieChanges};
use borsh::{BorshDeserialize, BorshSerialize};
use near_primitives::hash::CryptoHash;
use near_primitives::state::FlatStateValue;
use near_primitives::transaction::{ExecutionOutcomeWithIdAndProof, ExecutionOutcomeWithProof};
use near_primitives::types::StateRoot;
use near_primitives::utils::get_outcome_id_block_hash;
use std::collections::HashMap;
use tracing::info;
Expand Down Expand Up @@ -229,3 +232,58 @@ pub fn migrate_37_to_38(store: &Store) -> anyhow::Result<()> {
update.commit()?;
Ok(())
}

// Migrates the database from version 38 to 39.
//
// Rewrites TrieChanges to use TrieRefcountAddition and TrieRefcountSubtraction.
// TrieRefcountSubtraction no longer stores the value behind the refcount,
// because subtracting the refcount does not require specifying the value.
pub fn migrate_38_to_39(store: &Store) -> anyhow::Result<()> {
#[derive(borsh::BorshDeserialize)]
struct LegacyTrieRefcountChange {
trie_node_or_value_hash: CryptoHash,
trie_node_or_value: Vec<u8>,
rc: std::num::NonZeroU32,
}

#[derive(borsh::BorshDeserialize)]
struct LegacyTrieChanges {
old_root: StateRoot,
new_root: StateRoot,
insertions: Vec<LegacyTrieRefcountChange>,
deletions: Vec<LegacyTrieRefcountChange>,
}

let mut update = store.store_update();
update.delete_all(DBCol::TrieChanges);
for result in store.iter(DBCol::TrieChanges) {
let (key, old_value) = result?;
let LegacyTrieChanges { old_root, new_root, insertions, deletions } =
borsh::from_slice(&old_value)?;
let new_value = TrieChanges {
old_root,
new_root,
insertions: insertions
.into_iter()
.map(
|LegacyTrieRefcountChange {
trie_node_or_value_hash,
trie_node_or_value,
rc,
}| {
TrieRefcountAddition { trie_node_or_value_hash, trie_node_or_value, rc }
},
)
.collect(),
deletions: deletions
.into_iter()
.map(|LegacyTrieRefcountChange { trie_node_or_value_hash, rc, .. }| {
TrieRefcountSubtraction { trie_node_or_value_hash, rc }
})
.collect(),
};
update.set(DBCol::TrieChanges, &key, &borsh::to_vec(&new_value)?);
}
update.commit()?;
Ok(())
}
14 changes: 7 additions & 7 deletions core/store/src/trie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,22 +369,22 @@ pub trait TrieAccess {
pub struct TrieRefcountAddition {
/// Hash of trie_node_or_value and part of the DB key.
/// Used for uniting with shard id to get actual DB key.
trie_node_or_value_hash: CryptoHash,
pub(crate) trie_node_or_value_hash: CryptoHash,
/// DB value. Can be either serialized RawTrieNodeWithSize or value corresponding to
/// some TrieKey.
trie_node_or_value: Vec<u8>,
pub(crate) trie_node_or_value: Vec<u8>,
/// Reference count difference which will be added to the total refcount.
rc: std::num::NonZeroU32,
pub(crate) rc: std::num::NonZeroU32,
}

/// Stores reference count subtraction for some key in DB.
#[derive(BorshSerialize, BorshDeserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct TrieRefcountSubtraction {
/// Hash of trie_node_or_value and part of the DB key.
/// Used for uniting with shard id to get actual DB key.
trie_node_or_value_hash: CryptoHash,
pub(crate) trie_node_or_value_hash: CryptoHash,
/// Reference count difference which will be subtracted to the total refcount.
rc: std::num::NonZeroU32,
pub(crate) rc: std::num::NonZeroU32,
}

impl TrieRefcountAddition {
Expand Down Expand Up @@ -477,8 +477,8 @@ impl TrieRefcountDeltaMap {
pub struct TrieChanges {
pub old_root: StateRoot,
pub new_root: StateRoot,
insertions: Vec<TrieRefcountAddition>,
deletions: Vec<TrieRefcountSubtraction>,
pub(crate) insertions: Vec<TrieRefcountAddition>,
pub(crate) deletions: Vec<TrieRefcountSubtraction>,
}

impl TrieChanges {
Expand Down
1 change: 1 addition & 0 deletions nearcore/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl<'a> near_store::StoreMigrator for Migrator<'a> {
}
36 => near_store::migrations::migrate_36_to_37(store),
37 => near_store::migrations::migrate_37_to_38(store),
38 => near_store::migrations::migrate_38_to_39(store),
DB_VERSION.. => unreachable!(),
}
}
Expand Down

0 comments on commit 5385426

Please sign in to comment.