wallet: Remove confusing double return value ret+success

Also, remove redundant comments
This commit is contained in:
MarcoFalke 2020-06-20 08:44:42 -04:00
parent d4f9ae0025
commit fa021e9a5b
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
2 changed files with 6 additions and 28 deletions

View file

@ -809,10 +809,8 @@ bool BerkeleyBatch::ReadKey(CDataStream& key, CDataStream& value)
if (!pdb)
return false;
// Key
SafeDbt datKey(key.data(), key.size());
// Read
SafeDbt datValue;
int ret = pdb->get(activeTxn, datKey, datValue, 0);
if (ret == 0 && datValue.get_data() != nullptr) {
@ -829,13 +827,10 @@ bool BerkeleyBatch::WriteKey(CDataStream& key, CDataStream& value, bool overwrit
if (fReadOnly)
assert(!"Write called on database in read-only mode");
// Key
SafeDbt datKey(key.data(), key.size());
// Value
SafeDbt datValue(value.data(), value.size());
// Write
int ret = pdb->put(activeTxn, datKey, datValue, (overwrite ? 0 : DB_NOOVERWRITE));
return (ret == 0);
}
@ -847,10 +842,8 @@ bool BerkeleyBatch::EraseKey(CDataStream& key)
if (fReadOnly)
assert(!"Erase called on database in read-only mode");
// Key
SafeDbt datKey(key.data(), key.size());
// Erase
int ret = pdb->del(activeTxn, datKey, 0);
return (ret == 0 || ret == DB_NOTFOUND);
}
@ -860,10 +853,8 @@ bool BerkeleyBatch::HasKey(CDataStream& key)
if (!pdb)
return false;
// Key
SafeDbt datKey(key.data(), key.size());
// Exists
int ret = pdb->exists(activeTxn, datKey, 0);
return ret == 0;
}

View file

@ -223,64 +223,51 @@ public:
template <typename K, typename T>
bool Read(const K& key, T& value)
{
// Key
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(1000);
ssKey << key;
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
bool success = false;
bool ret = ReadKey(ssKey, ssValue);
if (ret) {
// Unserialize value
try {
ssValue >> value;
success = true;
} catch (const std::exception&) {
// In this case success remains 'false'
}
if (!ReadKey(ssKey, ssValue)) return false;
try {
ssValue >> value;
return true;
} catch (const std::exception&) {
return false;
}
return ret && success;
}
template <typename K, typename T>
bool Write(const K& key, const T& value, bool fOverwrite = true)
{
// Key
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(1000);
ssKey << key;
// Value
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
ssValue.reserve(10000);
ssValue << value;
// Write
return WriteKey(ssKey, ssValue, fOverwrite);
}
template <typename K>
bool Erase(const K& key)
{
// Key
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(1000);
ssKey << key;
// Erase
return EraseKey(ssKey);
}
template <typename K>
bool Exists(const K& key)
{
// Key
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(1000);
ssKey << key;
// Exists
return HasKey(ssKey);
}