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

Improve error handling during validation #2224

Merged
merged 5 commits into from Jan 30, 2013
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -187,9 +187,9 @@ bool AppInit(int argc, char* argv[])
fRet = AppInit2();
}
catch (std::exception& e) {
PrintException(&e, "AppInit()");
PrintExceptionContinue(&e, "AppInit()");
} catch (...) {
PrintException(NULL, "AppInit()");
PrintExceptionContinue(NULL, "AppInit()");
}
if (!fRet)
Shutdown(NULL);
@@ -936,7 +936,8 @@ bool AppInit2()

// scan for better chains in the block chain database, that are not yet connected in the active best chain
uiInterface.InitMessage(_("Importing blocks from block database..."));
if (!ConnectBestBlock())
CValidationState state;
if (!ConnectBestBlock(state))
strErrors << "Failed to connect best block";

CImportData *pimport = new CImportData();
@@ -12,6 +12,18 @@

#include <boost/filesystem.hpp>

void HandleError(const leveldb::Status &status) throw(leveldb_error) {
if (status.ok())
return;
if (status.IsCorruption())
throw leveldb_error("Database corrupted");
if (status.IsIOError())
throw leveldb_error("Database I/O error");
if (status.IsNotFound())
throw leveldb_error("Database entry missing");
throw leveldb_error("Unknown database error");
}

static leveldb::Options GetOptions(size_t nCacheSize) {
leveldb::Options options;
options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
@@ -57,12 +69,12 @@ CLevelDB::~CLevelDB() {
options.env = NULL;
}

bool CLevelDB::WriteBatch(CLevelDBBatch &batch, bool fSync) {
bool CLevelDB::WriteBatch(CLevelDBBatch &batch, bool fSync) throw(leveldb_error) {
leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
if (!status.ok()) {
printf("LevelDB write failure: %s\n", status.ToString().c_str());
HandleError(status);
return false;
}
return true;
}

@@ -11,6 +11,14 @@

#include <boost/filesystem/path.hpp>

class leveldb_error : public std::runtime_error
{
public:
leveldb_error(const std::string &msg) : std::runtime_error(msg) {}
};

void HandleError(const leveldb::Status &status) throw(leveldb_error);

// Batch of changes queued to be written to a CLevelDB
class CLevelDBBatch
{
@@ -72,7 +80,7 @@ class CLevelDB
CLevelDB(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory = false, bool fWipe = false);
~CLevelDB();

template<typename K, typename V> bool Read(const K& key, V& value) {
template<typename K, typename V> bool Read(const K& key, V& value) throw(leveldb_error) {
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(ssKey.GetSerializeSize(key));
ssKey << key;
@@ -84,6 +92,7 @@ class CLevelDB
if (status.IsNotFound())
return false;
printf("LevelDB read failure: %s\n", status.ToString().c_str());
HandleError(status);
}
try {
CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(), SER_DISK, CLIENT_VERSION);
@@ -94,13 +103,13 @@ class CLevelDB
return true;
}

template<typename K, typename V> bool Write(const K& key, const V& value, bool fSync = false) {
template<typename K, typename V> bool Write(const K& key, const V& value, bool fSync = false) throw(leveldb_error) {
CLevelDBBatch batch;
batch.Write(key, value);
return WriteBatch(batch, fSync);
}

template<typename K> bool Exists(const K& key) {
template<typename K> bool Exists(const K& key) throw(leveldb_error) {
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(ssKey.GetSerializeSize(key));
ssKey << key;
@@ -112,24 +121,25 @@ class CLevelDB
if (status.IsNotFound())
return false;
printf("LevelDB read failure: %s\n", status.ToString().c_str());
HandleError(status);
}
return true;
}

template<typename K> bool Erase(const K& key, bool fSync = false) {
template<typename K> bool Erase(const K& key, bool fSync = false) throw(leveldb_error) {
CLevelDBBatch batch;
batch.Erase(key);
return WriteBatch(batch, fSync);
}

bool WriteBatch(CLevelDBBatch &batch, bool fSync = false);
bool WriteBatch(CLevelDBBatch &batch, bool fSync = false) throw(leveldb_error);

// not available for LevelDB; provide for compatibility with BDB
bool Flush() {
return true;
}

bool Sync() {
bool Sync() throw(leveldb_error) {
CLevelDBBatch batch;
return WriteBatch(batch, true);
}
@@ -141,4 +151,3 @@ class CLevelDB
};

#endif // BITCOIN_LEVELDB_H

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.