Skip to content

Commit

Permalink
Use transaction to init storage
Browse files Browse the repository at this point in the history
  • Loading branch information
azarovh committed Aug 1, 2023
1 parent db2e40a commit 552e5da
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions chainstate/storage/src/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,25 @@ pub struct Store<B: storage::Backend>(storage::Storage<B, Schema>);
impl<B: storage::Backend> Store<B> {
/// Create a new chainstate storage
pub fn new(backend: B, chain_config: &ChainConfig) -> crate::Result<Self> {
let mut storage = Self::from_backend(backend)?;
let storage = Self::from_backend(backend)?;

// Set defaults if missing
let mut db_tx = storage.transaction_rw(None)?;

if storage.get_storage_version()?.is_none() {
storage.set_storage_version(ChainstateStorageVersion::CURRENT)?;
if db_tx.get_storage_version()?.is_none() {
db_tx.set_storage_version(ChainstateStorageVersion::CURRENT)?;
}

if storage.get_magic_bytes()?.is_none() {
storage.set_magic_bytes(chain_config.magic_bytes())?;
if db_tx.get_magic_bytes()?.is_none() {
db_tx.set_magic_bytes(chain_config.magic_bytes())?;
}

if storage.get_chain_type()?.is_none() {
storage.set_chain_type(chain_config.chain_type().name())?;
if db_tx.get_chain_type()?.is_none() {
db_tx.set_chain_type(chain_config.chain_type().name())?;
}

db_tx.commit()?;

Ok(storage)
}

Expand Down

0 comments on commit 552e5da

Please sign in to comment.