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

Move chainstate storage init inside constructor #1118

Merged
merged 4 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions chainstate/launcher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ fn make_chainstate_and_storage_impl<B: 'static + storage::Backend>(
chain_config: Arc<ChainConfig>,
chainstate_config: ChainstateConfig,
) -> Result<Box<dyn ChainstateInterface>, Error> {
let mut storage = chainstate_storage::Store::new(storage_backend)
let storage = chainstate_storage::Store::new(storage_backend, &chain_config)
.map_err(|e| Error::FailedToInitializeChainstate(e.into()))?;

storage_compatibility::check_storage_compatibility(&mut storage, chain_config.as_ref())
storage_compatibility::check_storage_compatibility(&storage, chain_config.as_ref())
.map_err(InitializationError::StorageCompatibilityCheckError)?;

let chainstate = chainstate::make_chainstate(
Expand Down
94 changes: 39 additions & 55 deletions chainstate/launcher/src/storage_compatibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@
// limitations under the License.

use chainstate::StorageCompatibilityCheckError;
use chainstate_storage::{BlockchainStorageRead, BlockchainStorageWrite};
use chainstate_storage::{BlockchainStorageRead, ChainstateStorageVersion};
use common::chain::ChainConfig;
use utils::ensure;

const CHAINSTATE_STORAGE_VERSION_UNINITIALIZED: u32 = 0;
const CHAINSTATE_STORAGE_VERSION_V1: u32 = 1;
const CURRENT_CHAINSTATE_STORAGE_VERSION: u32 = CHAINSTATE_STORAGE_VERSION_V1;

pub fn check_storage_compatibility<B: 'static + storage::Backend>(
storage: &mut chainstate_storage::Store<B>,
pub fn check_storage_compatibility(
storage: &impl BlockchainStorageRead,
chain_config: &ChainConfig,
) -> Result<(), StorageCompatibilityCheckError> {
check_storage_version(storage)?;
Expand All @@ -33,74 +29,62 @@ pub fn check_storage_compatibility<B: 'static + storage::Backend>(
Ok(())
}

fn check_storage_version<B: 'static + storage::Backend>(
storage: &mut chainstate_storage::Store<B>,
fn check_storage_version(
storage: &impl BlockchainStorageRead,
) -> Result<(), StorageCompatibilityCheckError> {
let storage_version = storage
.get_storage_version()
.map_err(StorageCompatibilityCheckError::StorageError)?;
.map_err(StorageCompatibilityCheckError::StorageError)?
.ok_or(StorageCompatibilityCheckError::StorageVersionMissing)?;

ensure!(
storage_version == ChainstateStorageVersion::CURRENT,
StorageCompatibilityCheckError::ChainstateStorageVersionMismatch(
storage_version,
ChainstateStorageVersion::CURRENT
)
);

if storage_version == CHAINSTATE_STORAGE_VERSION_UNINITIALIZED {
storage
.set_storage_version(CURRENT_CHAINSTATE_STORAGE_VERSION)
.map_err(StorageCompatibilityCheckError::StorageError)?;
} else {
ensure!(
storage_version == CURRENT_CHAINSTATE_STORAGE_VERSION,
StorageCompatibilityCheckError::ChainstateStorageVersionMismatch(
storage_version,
CURRENT_CHAINSTATE_STORAGE_VERSION
)
);
}
Ok(())
}

fn check_magic_bytes<B: 'static + storage::Backend>(
storage: &mut chainstate_storage::Store<B>,
fn check_magic_bytes(
storage: &impl BlockchainStorageRead,
chain_config: &ChainConfig,
) -> Result<(), StorageCompatibilityCheckError> {
let storage_magic_bytes = storage
.get_magic_bytes()
.map_err(StorageCompatibilityCheckError::StorageError)?;
let chain_config_magic_bytes = chain_config.magic_bytes();
.map_err(StorageCompatibilityCheckError::StorageError)?
.ok_or(StorageCompatibilityCheckError::MagicBytesMissing)?;

match storage_magic_bytes {
Some(storage_magic_bytes) => ensure!(
&storage_magic_bytes == chain_config_magic_bytes,
StorageCompatibilityCheckError::ChainConfigMagicBytesMismatch(
storage_magic_bytes,
chain_config_magic_bytes.to_owned()
)
),
None => storage
.set_magic_bytes(chain_config_magic_bytes)
.map_err(StorageCompatibilityCheckError::StorageError)?,
};
ensure!(
&storage_magic_bytes == chain_config.magic_bytes(),
StorageCompatibilityCheckError::ChainConfigMagicBytesMismatch(
storage_magic_bytes,
chain_config.magic_bytes().to_owned()
)
);

Ok(())
}

fn check_chain_type<B: 'static + storage::Backend>(
storage: &mut chainstate_storage::Store<B>,
fn check_chain_type(
storage: &impl BlockchainStorageRead,
chain_config: &ChainConfig,
) -> Result<(), StorageCompatibilityCheckError> {
let storage_chain_type =
storage.get_chain_type().map_err(StorageCompatibilityCheckError::StorageError)?;
let storage_chain_type = storage
.get_chain_type()
.map_err(StorageCompatibilityCheckError::StorageError)?
.ok_or(StorageCompatibilityCheckError::ChainTypeMissing)?;
let chain_config_type = chain_config.chain_type().name();

match storage_chain_type {
Some(storage_chain_type) => ensure!(
storage_chain_type == chain_config_type,
StorageCompatibilityCheckError::ChainTypeMismatch(
storage_chain_type,
chain_config_type.to_owned()
)
),
None => storage
.set_chain_type(chain_config_type)
.map_err(StorageCompatibilityCheckError::StorageError)?,
};
ensure!(
storage_chain_type == chain_config_type,
StorageCompatibilityCheckError::ChainTypeMismatch(
storage_chain_type,
chain_config_type.to_owned()
)
);

Ok(())
}
11 changes: 9 additions & 2 deletions chainstate/src/detail/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use super::{
storage::TransactionVerifierStorageError,
},
};
use chainstate_storage::ChainstateStorageVersion;
use chainstate_types::{GetAncestorError, PropertyQueryError};
use common::{
chain::{
Expand Down Expand Up @@ -214,10 +215,16 @@ pub enum InitializationError {
pub enum StorageCompatibilityCheckError {
#[error("Block storage error: `{0}`")]
StorageError(#[from] chainstate_storage::Error),
#[error("Storage version is missing in the db")]
StorageVersionMissing,
#[error("Magic bytes are missing in the db")]
MagicBytesMissing,
#[error("Chain type is missing in the db")]
ChainTypeMissing,
#[error(
"Node cannot load chainstate database because the versions mismatch: db `{0}`, app `{1}`"
"Node cannot load chainstate database because the versions mismatch: db `{0:?}`, app `{1:?}`"
iljakuklic marked this conversation as resolved.
Show resolved Hide resolved
)]
ChainstateStorageVersionMismatch(u32, u32),
ChainstateStorageVersionMismatch(ChainstateStorageVersion, ChainstateStorageVersion),
#[error(
"Chain's config magic bytes do not match the one from database : expected `{0:?}`, actual `{1:?}`"
)]
Expand Down
2 changes: 2 additions & 0 deletions chainstate/storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ utxo = { path = '../../utxo' }

mockall = { workspace = true, optional = true }

parity-scale-codec.workspace = true

[dev-dependencies]
crypto = { path = '../../crypto' }
test-utils = {path = '../../test-utils'}
Expand Down
38 changes: 32 additions & 6 deletions chainstate/storage/src/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use common::{
config::EpochIndex,
tokens::{TokenAuxiliaryData, TokenId},
transaction::{Transaction, TxMainChainIndex, TxMainChainPosition},
AccountNonce, AccountType, Block, DelegationId, GenBlock, OutPointSourceId, PoolId,
UtxoOutPoint,
AccountNonce, AccountType, Block, ChainConfig, DelegationId, GenBlock, OutPointSourceId,
PoolId, UtxoOutPoint,
},
primitives::{Amount, BlockHeight, Id},
};
Expand All @@ -42,12 +42,38 @@ use crate::{
mod store_tx;
pub use store_tx::{StoreTxRo, StoreTxRw};

mod version;
pub use version::ChainstateStorageVersion;

/// Store for blockchain data, parametrized over the backend B
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) -> crate::Result<Self> {
pub fn new(backend: B, chain_config: &ChainConfig) -> crate::Result<Self> {
let storage = Self::from_backend(backend)?;

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

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

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

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

db_tx.commit()?;

Ok(storage)
}

fn from_backend(backend: B) -> crate::Result<Self> {
let storage = Self(storage::Storage::new(backend).map_err(crate::Error::from)?);
Ok(storage)
}
Expand Down Expand Up @@ -146,7 +172,7 @@ impl<B: storage::Backend> Store<B> {
impl<B: Default + storage::Backend> Store<B> {
/// Create a default storage (mostly for testing, may want to remove this later)
pub fn new_empty() -> crate::Result<Self> {
Self::new(B::default())
Self::from_backend(B::default())
}
}

Expand Down Expand Up @@ -201,7 +227,7 @@ macro_rules! delegate_to_transaction {

impl<B: storage::Backend> BlockchainStorageRead for Store<B> {
delegate_to_transaction! {
fn get_storage_version(&self) -> crate::Result<u32>;
fn get_storage_version(&self) -> crate::Result<Option<ChainstateStorageVersion>>;
fn get_magic_bytes(&self) -> crate::Result<Option<[u8; 4]>>;
fn get_chain_type(&self) -> crate::Result<Option<String>>;
fn get_best_block_id(&self) -> crate::Result<Option<Id<GenBlock>>>;
Expand Down Expand Up @@ -356,7 +382,7 @@ impl<B: storage::Backend> PoSAccountingStorageRead<SealedStorageTag> for Store<B

impl<B: storage::Backend> BlockchainStorageWrite for Store<B> {
delegate_to_transaction! {
fn set_storage_version(&mut self, version: u32) -> crate::Result<()>;
fn set_storage_version(&mut self, version: ChainstateStorageVersion) -> crate::Result<()>;
fn set_magic_bytes(&mut self, bytes: &[u8; 4]) -> crate::Result<()>;
fn set_chain_type(&mut self, chain: &str) -> crate::Result<()>;
fn set_best_block_id(&mut self, id: &Id<GenBlock>) -> crate::Result<()>;
Expand Down
13 changes: 7 additions & 6 deletions chainstate/storage/src/internal/store_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ use utxo::{Utxo, UtxosBlockUndo, UtxosStorageRead, UtxosStorageWrite};

use crate::{
schema::{self as db, Schema},
BlockchainStorageRead, BlockchainStorageWrite, SealedStorageTag, TipStorageTag,
BlockchainStorageRead, BlockchainStorageWrite, ChainstateStorageVersion, SealedStorageTag,
TipStorageTag,
};

mod well_known {
use super::{Codec, GenBlock, Id};
use super::{ChainstateStorageVersion, Codec, GenBlock, Id};

/// Pre-defined database keys
pub trait Entry {
Expand All @@ -61,7 +62,7 @@ mod well_known {
};
}

declare_entry!(StoreVersion: u32);
declare_entry!(StoreVersion: ChainstateStorageVersion);
declare_entry!(BestBlockId: Id<GenBlock>);
declare_entry!(UtxosBestBlockId: Id<GenBlock>);
declare_entry!(TxIndexEnabled: bool);
Expand All @@ -79,8 +80,8 @@ macro_rules! impl_read_ops {
($TxType:ident) => {
/// Blockchain data storage transaction
impl<'st, B: storage::Backend> BlockchainStorageRead for $TxType<'st, B> {
fn get_storage_version(&self) -> crate::Result<u32> {
self.read_value::<well_known::StoreVersion>().map(|v| v.unwrap_or_default())
fn get_storage_version(&self) -> crate::Result<Option<ChainstateStorageVersion>> {
self.read_value::<well_known::StoreVersion>()
}

fn get_magic_bytes(&self) -> crate::Result<Option<[u8; 4]>> {
Expand Down Expand Up @@ -394,7 +395,7 @@ impl_read_ops!(StoreTxRo);
impl_read_ops!(StoreTxRw);

impl<'st, B: storage::Backend> BlockchainStorageWrite for StoreTxRw<'st, B> {
fn set_storage_version(&mut self, version: u32) -> crate::Result<()> {
fn set_storage_version(&mut self, version: ChainstateStorageVersion) -> crate::Result<()> {
self.write_value::<well_known::StoreVersion>(&version)
}

Expand Down
Loading
Loading