~ubuntu-branches/debian/stretch/bitcoin/stretch

« back to all changes in this revision

Viewing changes to src/dbwrapper.cpp

  • Committer: Package Import Robot
  • Author(s): Anthony Towns
  • Date: 2016-10-21 17:13:13 UTC
  • mfrom: (1.3.2)
  • Revision ID: package-import@ubuntu.com-20161021171313-7eu2ltpbk0xag3q1
Tags: 0.13.0-0.1
* Non-maintainer upload.
* New upstream release.
* Allow compilation with gcc/g++ 6. (Closes: Bug#835963)
* Additional fixes for openssl 1.1 compatibility. (See Bug#828248)
* Check if -latomic is needed (it is on mips*).
* Remove reproducible build patch, since leveldb build system is
  no longer used in 0.13. (See Bug#791834)
* Update description since the blockchain is much more than "several GB"
  now. (Closes: Bug#835809)

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
#include <memenv.h>
16
16
#include <stdint.h>
17
17
 
18
 
void HandleError(const leveldb::Status& status) throw(dbwrapper_error)
19
 
{
20
 
    if (status.ok())
21
 
        return;
22
 
    LogPrintf("%s\n", status.ToString());
23
 
    if (status.IsCorruption())
24
 
        throw dbwrapper_error("Database corrupted");
25
 
    if (status.IsIOError())
26
 
        throw dbwrapper_error("Database I/O error");
27
 
    if (status.IsNotFound())
28
 
        throw dbwrapper_error("Database entry missing");
29
 
    throw dbwrapper_error("Unknown database error");
30
 
}
31
 
 
32
18
static leveldb::Options GetOptions(size_t nCacheSize)
33
19
{
34
20
    leveldb::Options options;
61
47
        if (fWipe) {
62
48
            LogPrintf("Wiping LevelDB in %s\n", path.string());
63
49
            leveldb::Status result = leveldb::DestroyDB(path.string(), options);
64
 
            HandleError(result);
 
50
            dbwrapper_private::HandleError(result);
65
51
        }
66
52
        TryCreateDirectory(path);
67
53
        LogPrintf("Opening LevelDB in %s\n", path.string());
68
54
    }
69
55
    leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
70
 
    HandleError(status);
 
56
    dbwrapper_private::HandleError(status);
71
57
    LogPrintf("Opened LevelDB successfully\n");
72
58
 
73
59
    // The base-case obfuscation key, which is a noop.
84
70
        Write(OBFUSCATE_KEY_KEY, new_key);
85
71
        obfuscate_key = new_key;
86
72
 
87
 
        LogPrintf("Wrote new obfuscate key for %s: %s\n", path.string(), GetObfuscateKeyHex());
 
73
        LogPrintf("Wrote new obfuscate key for %s: %s\n", path.string(), HexStr(obfuscate_key));
88
74
    }
89
75
 
90
 
    LogPrintf("Using obfuscation key for %s: %s\n", path.string(), GetObfuscateKeyHex());
 
76
    LogPrintf("Using obfuscation key for %s: %s\n", path.string(), HexStr(obfuscate_key));
91
77
}
92
78
 
93
79
CDBWrapper::~CDBWrapper()
102
88
    options.env = NULL;
103
89
}
104
90
 
105
 
bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync) throw(dbwrapper_error)
 
91
bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
106
92
{
107
93
    leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
108
 
    HandleError(status);
 
94
    dbwrapper_private::HandleError(status);
109
95
    return true;
110
96
}
111
97
 
136
122
    return !(it->Valid());
137
123
}
138
124
 
139
 
const std::vector<unsigned char>& CDBWrapper::GetObfuscateKey() const
140
 
{
141
 
    return obfuscate_key;
142
 
}
143
 
 
144
 
std::string CDBWrapper::GetObfuscateKeyHex() const
145
 
{
146
 
    return HexStr(obfuscate_key);
147
 
}
148
 
 
149
125
CDBIterator::~CDBIterator() { delete piter; }
150
126
bool CDBIterator::Valid() { return piter->Valid(); }
151
127
void CDBIterator::SeekToFirst() { piter->SeekToFirst(); }
152
128
void CDBIterator::Next() { piter->Next(); }
 
129
 
 
130
namespace dbwrapper_private {
 
131
 
 
132
void HandleError(const leveldb::Status& status)
 
133
{
 
134
    if (status.ok())
 
135
        return;
 
136
    LogPrintf("%s\n", status.ToString());
 
137
    if (status.IsCorruption())
 
138
        throw dbwrapper_error("Database corrupted");
 
139
    if (status.IsIOError())
 
140
        throw dbwrapper_error("Database I/O error");
 
141
    if (status.IsNotFound())
 
142
        throw dbwrapper_error("Database entry missing");
 
143
    throw dbwrapper_error("Unknown database error");
 
144
}
 
145
 
 
146
const std::vector<unsigned char>& GetObfuscateKey(const CDBWrapper &w)
 
147
{
 
148
    return w.obfuscate_key;
 
149
}
 
150
 
 
151
};