1414// limitations under the License.
1515
1616use chainstate:: StorageCompatibilityCheckError ;
17- use chainstate_storage:: { BlockchainStorageRead , BlockchainStorageWrite } ;
17+ use chainstate_storage:: { BlockchainStorageRead , ChainstateStorageVersion } ;
1818use common:: chain:: ChainConfig ;
1919use utils:: ensure;
2020
21- const CHAINSTATE_STORAGE_VERSION_UNINITIALIZED : u32 = 0 ;
22- const CHAINSTATE_STORAGE_VERSION_V1 : u32 = 1 ;
23- const CURRENT_CHAINSTATE_STORAGE_VERSION : u32 = CHAINSTATE_STORAGE_VERSION_V1 ;
24-
25- pub fn check_storage_compatibility < B : ' static + storage:: Backend > (
26- storage : & mut chainstate_storage:: Store < B > ,
21+ pub fn check_storage_compatibility (
22+ storage : & impl BlockchainStorageRead ,
2723 chain_config : & ChainConfig ,
2824) -> Result < ( ) , StorageCompatibilityCheckError > {
2925 check_storage_version ( storage) ?;
@@ -33,74 +29,64 @@ pub fn check_storage_compatibility<B: 'static + storage::Backend>(
3329 Ok ( ( ) )
3430}
3531
36- fn check_storage_version < B : ' static + storage :: Backend > (
37- storage : & mut chainstate_storage :: Store < B > ,
32+ fn check_storage_version (
33+ storage : & impl BlockchainStorageRead ,
3834) -> Result < ( ) , StorageCompatibilityCheckError > {
3935 let storage_version = storage
4036 . get_storage_version ( )
41- . map_err ( StorageCompatibilityCheckError :: StorageError ) ?;
37+ . map_err ( StorageCompatibilityCheckError :: StorageError ) ?
38+ . ok_or ( StorageCompatibilityCheckError :: StorageVersionMissing ) ?;
39+ let storage_version = ChainstateStorageVersion :: new ( storage_version)
40+ . ok_or ( StorageCompatibilityCheckError :: StorageVersionConversionError ) ?;
41+
42+ ensure ! (
43+ storage_version == ChainstateStorageVersion :: CURRENT ,
44+ StorageCompatibilityCheckError :: ChainstateStorageVersionMismatch (
45+ storage_version,
46+ ChainstateStorageVersion :: CURRENT
47+ )
48+ ) ;
4249
43- if storage_version == CHAINSTATE_STORAGE_VERSION_UNINITIALIZED {
44- storage
45- . set_storage_version ( CURRENT_CHAINSTATE_STORAGE_VERSION )
46- . map_err ( StorageCompatibilityCheckError :: StorageError ) ?;
47- } else {
48- ensure ! (
49- storage_version == CURRENT_CHAINSTATE_STORAGE_VERSION ,
50- StorageCompatibilityCheckError :: ChainstateStorageVersionMismatch (
51- storage_version,
52- CURRENT_CHAINSTATE_STORAGE_VERSION
53- )
54- ) ;
55- }
5650 Ok ( ( ) )
5751}
5852
59- fn check_magic_bytes < B : ' static + storage :: Backend > (
60- storage : & mut chainstate_storage :: Store < B > ,
53+ fn check_magic_bytes (
54+ storage : & impl BlockchainStorageRead ,
6155 chain_config : & ChainConfig ,
6256) -> Result < ( ) , StorageCompatibilityCheckError > {
6357 let storage_magic_bytes = storage
6458 . get_magic_bytes ( )
65- . map_err ( StorageCompatibilityCheckError :: StorageError ) ?;
66- let chain_config_magic_bytes = chain_config . magic_bytes ( ) ;
59+ . map_err ( StorageCompatibilityCheckError :: StorageError ) ?
60+ . ok_or ( StorageCompatibilityCheckError :: MagicBytesMissing ) ? ;
6761
68- match storage_magic_bytes {
69- Some ( storage_magic_bytes) => ensure ! (
70- & storage_magic_bytes == chain_config_magic_bytes,
71- StorageCompatibilityCheckError :: ChainConfigMagicBytesMismatch (
72- storage_magic_bytes,
73- chain_config_magic_bytes. to_owned( )
74- )
75- ) ,
76- None => storage
77- . set_magic_bytes ( chain_config_magic_bytes)
78- . map_err ( StorageCompatibilityCheckError :: StorageError ) ?,
79- } ;
62+ ensure ! (
63+ & storage_magic_bytes == chain_config. magic_bytes( ) ,
64+ StorageCompatibilityCheckError :: ChainConfigMagicBytesMismatch (
65+ storage_magic_bytes,
66+ chain_config. magic_bytes( ) . to_owned( )
67+ )
68+ ) ;
8069
8170 Ok ( ( ) )
8271}
8372
84- fn check_chain_type < B : ' static + storage :: Backend > (
85- storage : & mut chainstate_storage :: Store < B > ,
73+ fn check_chain_type (
74+ storage : & impl BlockchainStorageRead ,
8675 chain_config : & ChainConfig ,
8776) -> Result < ( ) , StorageCompatibilityCheckError > {
88- let storage_chain_type =
89- storage. get_chain_type ( ) . map_err ( StorageCompatibilityCheckError :: StorageError ) ?;
77+ let storage_chain_type = storage
78+ . get_chain_type ( )
79+ . map_err ( StorageCompatibilityCheckError :: StorageError ) ?
80+ . ok_or ( StorageCompatibilityCheckError :: ChainTypeMissing ) ?;
9081 let chain_config_type = chain_config. chain_type ( ) . name ( ) ;
9182
92- match storage_chain_type {
93- Some ( storage_chain_type) => ensure ! (
94- storage_chain_type == chain_config_type,
95- StorageCompatibilityCheckError :: ChainTypeMismatch (
96- storage_chain_type,
97- chain_config_type. to_owned( )
98- )
99- ) ,
100- None => storage
101- . set_chain_type ( chain_config_type)
102- . map_err ( StorageCompatibilityCheckError :: StorageError ) ?,
103- } ;
83+ ensure ! (
84+ storage_chain_type == chain_config_type,
85+ StorageCompatibilityCheckError :: ChainTypeMismatch (
86+ storage_chain_type,
87+ chain_config_type. to_owned( )
88+ )
89+ ) ;
10490
10591 Ok ( ( ) )
10692}
0 commit comments