@@ -192,42 +192,50 @@ void BlockChain::init(ChainParams const& _p)
192192 genesis ();
193193}
194194
195- unsigned BlockChain::open (fs::path const & _path, WithExisting _we)
195+ bool BlockChain::open (fs::path const & _path, WithExisting _we)
196196{
197197 auto const path = _path.empty () ? db::databasePath () : _path;
198198 auto const chainPath = path / fs::path (toHex (m_genesisHash.ref ().cropped (0 , 4 )));
199199 auto const chainSubPathBlocks = chainPath / fs::path (" blocks" );
200200 auto const extrasPath = chainPath / fs::path (toString (c_databaseVersion));
201201 auto const extrasSubPathExtras = extrasPath / fs::path (" extras" );
202- unsigned lastMinor = c_minorProtocolVersion;
202+ unsigned lastMinor = c_databaseMinorVersion;
203+ bool rebuildNeeded = false ;
203204
204205 if (db::isDiskDatabase ())
205206 {
206- fs::create_directories (extrasPath);
207- DEV_IGNORE_EXCEPTIONS (fs::permissions (extrasPath, fs::owner_all));
208-
209- auto const extrasSubPathMinor = extrasPath / fs::path (" minor" );
210- bytes const status = contents (extrasSubPathMinor);
211- if (!status.empty ())
212- DEV_IGNORE_EXCEPTIONS (lastMinor = (unsigned )RLP (status));
213- if (c_minorProtocolVersion != lastMinor)
214- {
215- cnote << " Killing extras database " << extrasPath << " (DB minor version:" << lastMinor
216- << " != our minor version: " << c_minorProtocolVersion << " )." ;
217- DEV_IGNORE_EXCEPTIONS (fs::remove_all (extrasPath / fs::path (" details.old" )));
218- fs::rename (extrasSubPathExtras, extrasPath / fs::path (" extras.old" ));
219- fs::remove_all (extrasPath / fs::path (" state" ));
220- writeFile (extrasSubPathMinor, rlp (c_minorProtocolVersion));
221- lastMinor = (unsigned )RLP (status);
222- }
223-
224207 if (_we == WithExisting::Kill)
225208 {
226209 cnote << " Killing blockchain (" << chainSubPathBlocks << " ) & extras ("
227210 << extrasSubPathExtras << " ) databases (WithExisting::Kill)." ;
228211 fs::remove_all (chainSubPathBlocks);
229212 fs::remove_all (extrasSubPathExtras);
230213 }
214+
215+ fs::create_directories (extrasPath);
216+ DEV_IGNORE_EXCEPTIONS (fs::permissions (extrasPath, fs::owner_all));
217+
218+ auto const extrasSubPathMinor = extrasPath / fs::path (" minor" );
219+ bytes const minorVersionBytes = contents (extrasSubPathMinor);
220+ bool writeMinorVersion = false ;
221+ if (!minorVersionBytes.empty ())
222+ {
223+ DEV_IGNORE_EXCEPTIONS (lastMinor = (unsigned )RLP (minorVersionBytes));
224+ if (c_databaseMinorVersion != lastMinor)
225+ {
226+ rebuildNeeded = true ;
227+ LOG (m_loggerInfo) << " Database minor version change detected, the extras and state "
228+ " databases will be rebuilt." ;
229+ LOG (m_loggerInfo) << " Version from " << extrasSubPathMinor << " (" << lastMinor
230+ << " ) != Aleth's version (" << c_databaseMinorVersion << " )" ;
231+ writeMinorVersion = true ;
232+ lastMinor = (unsigned )RLP (minorVersionBytes);
233+ }
234+ }
235+ else
236+ writeMinorVersion = true ;
237+ if (writeMinorVersion)
238+ writeFile (extrasSubPathMinor, rlp (c_databaseMinorVersion));
231239 }
232240
233241 try
@@ -268,7 +276,7 @@ unsigned BlockChain::open(fs::path const& _path, WithExisting _we)
268276 throw ;
269277 }
270278
271- if (_we != WithExisting::Verify && !details (m_genesisHash))
279+ if (_we != WithExisting::Verify && !rebuildNeeded && ! details (m_genesisHash))
272280 {
273281 BlockHeader gb (m_params.genesisBlock ());
274282 // Insert details of genesis block.
@@ -284,13 +292,14 @@ unsigned BlockChain::open(fs::path const& _path, WithExisting _we)
284292
285293 m_lastBlockNumber = number (m_lastBlockHash);
286294
287- ctrace << " Opened blockchain DB. Latest: " << currentHash () << (lastMinor == c_minorProtocolVersion ? " (rebuild not needed)" : " *** REBUILD NEEDED ***" );
288- return lastMinor;
295+ ctrace << " Opened blockchain DB. Latest: " << currentHash ()
296+ << (!rebuildNeeded ? " (rebuild not needed)" : " *** REBUILD NEEDED ***" );
297+ return rebuildNeeded;
289298}
290299
291300void BlockChain::open (fs::path const & _path, WithExisting _we, ProgressCallback const & _pc)
292301{
293- if (open (_path, _we) != c_minorProtocolVersion || _we == WithExisting::Verify)
302+ if (open (_path, _we) || _we == WithExisting::Verify)
294303 rebuild (_path, _pc);
295304}
296305
0 commit comments