@@ -22,6 +22,7 @@ use std::sync::Arc;
2222use chainstate:: InitializationError ;
2323use chainstate_storage:: { BlockchainStorageRead , BlockchainStorageWrite } ;
2424use storage_lmdb:: resize_callback:: MapResizeCallback ;
25+ use utils:: ensure;
2526
2627// Some useful reexports
2728pub use chainstate:: {
@@ -47,6 +48,8 @@ fn make_chainstate_and_storage_impl<B: 'static + storage::Backend>(
4748 . map_err ( |e| Error :: FailedToInitializeChainstate ( e. into ( ) ) ) ?;
4849
4950 check_storage_version ( & mut storage) ?;
51+ check_magic_bytes ( & mut storage, chain_config. as_ref ( ) ) ?;
52+ check_chain_type ( & mut storage, chain_config. as_ref ( ) ) ?;
5053
5154 let chainstate = chainstate:: make_chainstate (
5255 chain_config,
@@ -70,7 +73,7 @@ fn check_storage_version<B: 'static + storage::Backend>(
7073 . set_storage_version ( CURRENT_CHAINSTATE_STORAGE_VERSION )
7174 . map_err ( InitializationError :: StorageError ) ?;
7275 } else {
73- utils :: ensure!(
76+ ensure ! (
7477 storage_version == CURRENT_CHAINSTATE_STORAGE_VERSION ,
7578 InitializationError :: ChainstateStorageVersionMismatch (
7679 storage_version,
@@ -81,6 +84,53 @@ fn check_storage_version<B: 'static + storage::Backend>(
8184 Ok ( ( ) )
8285}
8386
87+ fn check_magic_bytes < B : ' static + storage:: Backend > (
88+ storage : & mut chainstate_storage:: Store < B > ,
89+ chain_config : & ChainConfig ,
90+ ) -> Result < ( ) , Error > {
91+ let storage_magic_bytes =
92+ storage. get_magic_bytes ( ) . map_err ( InitializationError :: StorageError ) ?;
93+ let chain_config_magic_bytes = chain_config. magic_bytes ( ) ;
94+
95+ match storage_magic_bytes {
96+ Some ( storage_magic_bytes) => ensure ! (
97+ & storage_magic_bytes == chain_config_magic_bytes,
98+ InitializationError :: ChainConfigMagicBytesMismatch (
99+ storage_magic_bytes,
100+ chain_config_magic_bytes. to_owned( )
101+ )
102+ ) ,
103+ None => storage
104+ . set_magic_bytes ( chain_config_magic_bytes)
105+ . map_err ( InitializationError :: StorageError ) ?,
106+ } ;
107+
108+ Ok ( ( ) )
109+ }
110+
111+ fn check_chain_type < B : ' static + storage:: Backend > (
112+ storage : & mut chainstate_storage:: Store < B > ,
113+ chain_config : & ChainConfig ,
114+ ) -> Result < ( ) , Error > {
115+ let storage_chain_type = storage. get_chain_type ( ) . map_err ( InitializationError :: StorageError ) ?;
116+ let chain_config_type = chain_config. chain_type ( ) . name ( ) ;
117+
118+ match storage_chain_type {
119+ Some ( storage_chain_type) => ensure ! (
120+ storage_chain_type == chain_config_type,
121+ InitializationError :: ChainTypeMismatch (
122+ storage_chain_type,
123+ chain_config_type. to_owned( )
124+ )
125+ ) ,
126+ None => storage
127+ . set_chain_type ( chain_config_type)
128+ . map_err ( InitializationError :: StorageError ) ?,
129+ } ;
130+
131+ Ok ( ( ) )
132+ }
133+
84134/// Create chainstate together with its storage
85135pub fn make_chainstate (
86136 datadir : & std:: path:: Path ,
0 commit comments