Skip to content

Commit

Permalink
cleanup docs and move migration fns to chain itself
Browse files Browse the repository at this point in the history
  • Loading branch information
antiochp committed Sep 19, 2019
1 parent 2d29870 commit 31ea040
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
23 changes: 18 additions & 5 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl Chain {
)?;
Chain::log_heads(&store)?;

Ok(Chain {
let chain = Chain {
db_root,
store,
adapter,
Expand All @@ -201,7 +201,18 @@ impl Chain {
verifier_cache,
archive_mode,
genesis: genesis.header.clone(),
})
};

// DB migrations to be run prior to the chain being used.
{
// Migrate full blocks to protocol version v2.
chain.migrate_db_v1_v2()?;

// Rebuild height_for_pos index.
chain.rebuild_height_for_pos()?;
}

Ok(chain)
}

/// Return our shared header MMR handle.
Expand Down Expand Up @@ -1245,10 +1256,12 @@ impl Chain {
self.header_pmmr.read().get_header_hash_by_height(height)
}

pub fn migrate_db_v1_v2(&self) -> Result<(), Error> {
/// Migrate our local db from v1 to v2.
/// This covers blocks which themselves contain transactions.
/// Transaction kernels changed in v2 due to "variable size kernels".
fn migrate_db_v1_v2(&self) -> Result<(), Error> {
let store_v1 = self.store.with_version(ProtocolVersion(1));
let batch = store_v1.batch()?;
// Note: the iterator here returns None
for (_, block) in batch.blocks_iter()? {
batch.migrate_block(&block, ProtocolVersion(2))?;
}
Expand All @@ -1258,7 +1271,7 @@ impl Chain {

/// Migrate the index 'commitment -> output_pos' to index 'commitment -> (output_pos, block_height)'
/// Note: should only be called when Node start-up, for database migration from the old version.
pub fn rebuild_height_for_pos(&self) -> Result<(), Error> {
fn rebuild_height_for_pos(&self) -> Result<(), Error> {
let header_pmmr = self.header_pmmr.read();
let txhashset = self.txhashset.read();
let mut outputs_pos = txhashset.get_all_output_pos()?;
Expand Down
6 changes: 6 additions & 0 deletions chain/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ impl ChainStore {
Ok(ChainStore { db })
}

/// Create a new instance of the chain store based on this instance
/// but with the provided protocol version. This is used when migrating
/// data in the db to a different protocol version, reading using one version and
/// writing back to the db with a different version.
pub fn with_version(&self, version: ProtocolVersion) -> ChainStore {
let db_with_version = self.db.with_version(version);
ChainStore {
Expand Down Expand Up @@ -266,6 +270,8 @@ impl<'a> Batch<'a> {
Ok(())
}

/// Migrate a block stored in the db by serializing it using the provided protocol version.
/// Block may have been read using a previous protocol version but we do not actually care.
pub fn migrate_block(&self, b: &Block, version: ProtocolVersion) -> Result<(), Error> {
self.db.put_ser_with_version(
&to_key(BLOCK_PREFIX, &mut b.hash().to_vec())[..],
Expand Down
11 changes: 0 additions & 11 deletions servers/src/grin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,6 @@ impl Server {
archive_mode,
)?);

// TODO - Move this to Chain::init() I think.
//
// DB migrations to be run prior to the chain being used.
{
// migrate full blocks to protocol version v2 as necessary
shared_chain.migrate_db_v1_v2()?;

// rebuild height_for_pos index as necessary
shared_chain.rebuild_height_for_pos()?;
}

pool_adapter.set_chain(shared_chain.clone());

let net_adapter = Arc::new(NetToChainAdapter::new(
Expand Down

0 comments on commit 31ea040

Please sign in to comment.