Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit 403dc90

Browse files
authored
Merge pull request #5827 from ethereum/minorversion
Write database minor version to disk and fix rebuild on minor version mismatch bug
2 parents dba2a7e + 038a75f commit 403dc90

File tree

5 files changed

+40
-30
lines changed

5 files changed

+40
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- Fixed: [#5811](https://github.com/ethereum/aleth/pull/5811) RPC methods querying transactions (`eth_getTransactionByHash`, `eth_getBlockByNumber`) return correct `v` value.
1515
- Fixed: [#5821](https://github.com/ethereum/aleth/pull/5821) `test_setChainParams` correctly initializes custom configuration of precompiled contracts.
1616
- Fixed: [#5826](https://github.com/ethereum/aleth/pull/5826) Fix blocking bug in database rebuild functionality - users can now rebuild their databases via Aleth's '-R' switch.
17+
- Fixed: [#5827](https://github.com/ethereum/aleth/pull/5827) Detect database upgrades and automatically rebuild the database when they occur.
1718

1819
## [1.7.0] - 2019-11-14
1920

libethcore/Common.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ namespace eth
2323

2424
const unsigned c_protocolVersion = 63;
2525
#if ETH_FATDB
26-
const unsigned c_minorProtocolVersion = 3;
26+
const unsigned c_databaseMinorVersion = 3;
2727
const unsigned c_databaseBaseVersion = 9;
2828
const unsigned c_databaseVersionModifier = 1;
2929
#else
30-
const unsigned c_minorProtocolVersion = 2;
30+
const unsigned c_databaseMinorVersion = 2;
3131
const unsigned c_databaseBaseVersion = 9;
3232
const unsigned c_databaseVersionModifier = 0;
3333
#endif

libethcore/Common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ namespace eth
2323
/// Current protocol version.
2424
extern const unsigned c_protocolVersion;
2525

26-
/// Current minor protocol version.
27-
extern const unsigned c_minorProtocolVersion;
26+
/// Current minor database version (for the extras database).
27+
extern const unsigned c_databaseMinorVersion;
2828

2929
/// Current database version.
3030
extern const unsigned c_databaseVersion;

libethereum/BlockChain.cpp

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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

291300
void 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

libethereum/BlockChain.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ class BlockChain
314314

315315
/// Initialise everything and ready for openning the database.
316316
void init(ChainParams const& _p);
317-
/// Open the database.
318-
unsigned open(boost::filesystem::path const& _path, WithExisting _we);
317+
/// Open the database. Returns whether or not the database needs to be rebuilt.
318+
bool open(boost::filesystem::path const& _path, WithExisting _we);
319319
/// Open the database, rebuilding if necessary.
320320
void open(boost::filesystem::path const& _path, WithExisting _we, ProgressCallback const& _pc);
321321
/// Finalise everything and close the database.

0 commit comments

Comments
 (0)