aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.test.include23
-rw-r--r--src/addrman.h2
-rw-r--r--src/arith_uint256.cpp8
-rw-r--r--src/bench/coin_selection.cpp2
-rw-r--r--src/bitcoind.cpp7
-rw-r--r--src/chainparams.cpp1
-rw-r--r--src/chainparamsbase.cpp1
-rw-r--r--src/cuckoocache.h16
-rw-r--r--src/init.cpp3
-rw-r--r--src/interfaces/handler.cpp1
-rw-r--r--src/interfaces/wallet.cpp4
-rw-r--r--src/miner.cpp1
-rw-r--r--src/net.cpp1
-rw-r--r--src/netaddress.h10
-rw-r--r--src/primitives/block.h2
-rw-r--r--src/protocol.h2
-rw-r--r--src/qt/README.md10
-rw-r--r--src/qt/optionsmodel.cpp23
-rw-r--r--src/qt/paymentrequestplus.cpp1
-rw-r--r--src/qt/test/wallettests.cpp2
-rw-r--r--src/random.cpp1
-rw-r--r--src/rpc/client.cpp1
-rw-r--r--src/rpc/mining.cpp1
-rw-r--r--src/script/script.h2
-rw-r--r--src/serialize.h55
-rw-r--r--src/support/lockedpool.cpp1
-rw-r--r--src/test/test_bitcoin.cpp4
-rw-r--r--src/test/uint256_tests.cpp13
-rw-r--r--src/test/util_tests.cpp2
-rw-r--r--src/txdb.h2
-rw-r--r--src/util.cpp4
-rw-r--r--src/wallet/db.cpp145
-rw-r--r--src/wallet/db.h50
-rw-r--r--src/wallet/feebumper.cpp2
-rw-r--r--src/wallet/init.cpp8
-rw-r--r--src/wallet/rpcwallet.cpp201
-rw-r--r--src/wallet/test/coinselector_tests.cpp2
-rw-r--r--src/wallet/test/wallet_test_fixture.cpp3
-rw-r--r--src/wallet/test/wallet_tests.cpp14
-rw-r--r--src/wallet/wallet.cpp252
-rw-r--r--src/wallet/wallet.h35
-rw-r--r--src/wallet/walletdb.cpp139
-rw-r--r--src/wallet/walletdb.h45
43 files changed, 670 insertions, 432 deletions
diff --git a/src/Makefile.test.include b/src/Makefile.test.include
index 76da140b84..c4f18bb371 100644
--- a/src/Makefile.test.include
+++ b/src/Makefile.test.include
@@ -2,7 +2,6 @@
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
-TESTS += test/test_bitcoin
bin_PROGRAMS += test/test_bitcoin
noinst_PROGRAMS += test/test_bitcoin_fuzzy
TEST_SRCDIR = test
@@ -21,6 +20,11 @@ RAW_TEST_FILES =
GENERATED_TEST_FILES = $(JSON_TEST_FILES:.json=.json.h) $(RAW_TEST_FILES:.raw=.raw.h)
+BITCOIN_TEST_SUITE = \
+ test/test_bitcoin_main.cpp \
+ test/test_bitcoin.h \
+ test/test_bitcoin.cpp
+
# test_bitcoin binary #
BITCOIN_TESTS =\
test/arith_uint256_tests.cpp \
@@ -76,9 +80,6 @@ BITCOIN_TESTS =\
test/sigopcount_tests.cpp \
test/skiplist_tests.cpp \
test/streams_tests.cpp \
- test/test_bitcoin.cpp \
- test/test_bitcoin.h \
- test/test_bitcoin_main.cpp \
test/timedata_tests.cpp \
test/torcontrol_tests.cpp \
test/transaction_tests.cpp \
@@ -90,15 +91,17 @@ BITCOIN_TESTS =\
if ENABLE_WALLET
BITCOIN_TESTS += \
- wallet/test/wallet_test_fixture.cpp \
- wallet/test/wallet_test_fixture.h \
wallet/test/accounting_tests.cpp \
wallet/test/wallet_tests.cpp \
wallet/test/wallet_crypto_tests.cpp \
wallet/test/coinselector_tests.cpp
+
+BITCOIN_TEST_SUITE += \
+ wallet/test/wallet_test_fixture.cpp \
+ wallet/test/wallet_test_fixture.h
endif
-test_test_bitcoin_SOURCES = $(BITCOIN_TESTS) $(JSON_TEST_FILES) $(RAW_TEST_FILES)
+test_test_bitcoin_SOURCES = $(BITCOIN_TEST_SUITE) $(BITCOIN_TESTS) $(JSON_TEST_FILES) $(RAW_TEST_FILES)
test_test_bitcoin_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(TESTDEFS) $(EVENT_CFLAGS)
test_test_bitcoin_LDADD =
if ENABLE_WALLET
@@ -150,7 +153,7 @@ bitcoin_test_check: $(TEST_BINARY) FORCE
bitcoin_test_clean : FORCE
rm -f $(CLEAN_BITCOIN_TEST) $(test_test_bitcoin_OBJECTS) $(TEST_BINARY)
-check-local:
+check-local: $(BITCOIN_TESTS:.cpp=.cpp.test)
@echo "Running test/util/bitcoin-util-test.py..."
$(PYTHON) $(top_builddir)/test/util/bitcoin-util-test.py
$(AM_V_at)$(MAKE) $(AM_MAKEFLAGS) -C secp256k1 check
@@ -158,6 +161,10 @@ if EMBEDDED_UNIVALUE
$(AM_V_at)$(MAKE) $(AM_MAKEFLAGS) -C univalue check
endif
+%.cpp.test: %.cpp
+ @echo Running tests: `cat $< | grep "BOOST_FIXTURE_TEST_SUITE(\|BOOST_AUTO_TEST_SUITE(" | cut -d '(' -f 2 | cut -d ',' -f 1 | cut -d ')' -f 1` from $<
+ $(AM_V_at)$(TEST_BINARY) -l test_suite -t "`cat $< | grep "BOOST_FIXTURE_TEST_SUITE(\|BOOST_AUTO_TEST_SUITE(" | cut -d '(' -f 2 | cut -d ',' -f 1 | cut -d ')' -f 1`" > $<.log 2>&1 || (cat $<.log && false)
+
%.json.h: %.json
@$(MKDIR_P) $(@D)
@{ \
diff --git a/src/addrman.h b/src/addrman.h
index 6dec3fe416..a36f7ea100 100644
--- a/src/addrman.h
+++ b/src/addrman.h
@@ -59,7 +59,7 @@ public:
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
- READWRITE(*static_cast<CAddress*>(this));
+ READWRITEAS(CAddress, *this);
READWRITE(source);
READWRITE(nLastSuccess);
READWRITE(nAttempts);
diff --git a/src/arith_uint256.cpp b/src/arith_uint256.cpp
index 65de632306..c7ddb17eb0 100644
--- a/src/arith_uint256.cpp
+++ b/src/arith_uint256.cpp
@@ -69,16 +69,16 @@ base_uint<BITS>& base_uint<BITS>::operator*=(uint32_t b32)
template <unsigned int BITS>
base_uint<BITS>& base_uint<BITS>::operator*=(const base_uint& b)
{
- base_uint<BITS> a = *this;
- *this = 0;
+ base_uint<BITS> a;
for (int j = 0; j < WIDTH; j++) {
uint64_t carry = 0;
for (int i = 0; i + j < WIDTH; i++) {
- uint64_t n = carry + pn[i + j] + (uint64_t)a.pn[j] * b.pn[i];
- pn[i + j] = n & 0xffffffff;
+ uint64_t n = carry + a.pn[i + j] + (uint64_t)pn[j] * b.pn[i];
+ a.pn[i + j] = n & 0xffffffff;
carry = n >> 32;
}
}
+ *this = a;
return *this;
}
diff --git a/src/bench/coin_selection.cpp b/src/bench/coin_selection.cpp
index 4b2a0e72fe..64ec056c4d 100644
--- a/src/bench/coin_selection.cpp
+++ b/src/bench/coin_selection.cpp
@@ -33,7 +33,7 @@ static void addCoin(const CAmount& nValue, const CWallet& wallet, std::vector<CO
// (https://github.com/bitcoin/bitcoin/issues/7883#issuecomment-224807484)
static void CoinSelection(benchmark::State& state)
{
- const CWallet wallet("dummy", CWalletDBWrapper::CreateDummy());
+ const CWallet wallet("dummy", WalletDatabase::CreateDummy());
std::vector<COutput> vCoins;
LOCK(wallet.cs_wallet);
diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp
index 4aa811b86b..83d9719df2 100644
--- a/src/bitcoind.cpp
+++ b/src/bitcoind.cpp
@@ -139,6 +139,10 @@ bool AppInit(int argc, char* argv[])
if (gArgs.GetBoolArg("-daemon", false))
{
#if HAVE_DECL_DAEMON
+#if defined(MAC_OSX)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+#endif
fprintf(stdout, "Bitcoin server starting\n");
// Daemonize
@@ -146,6 +150,9 @@ bool AppInit(int argc, char* argv[])
fprintf(stderr, "Error: daemon() failed: %s\n", strerror(errno));
return false;
}
+#if defined(MAC_OSX)
+#pragma GCC diagnostic pop
+#endif
#else
fprintf(stderr, "Error: -daemon is not supported on this operating system\n");
return false;
diff --git a/src/chainparams.cpp b/src/chainparams.cpp
index adf8e6ae5b..6067503b0b 100644
--- a/src/chainparams.cpp
+++ b/src/chainparams.cpp
@@ -11,7 +11,6 @@
#include <utilstrencodings.h>
#include <assert.h>
-#include <memory>
#include <chainparamsseeds.h>
diff --git a/src/chainparamsbase.cpp b/src/chainparamsbase.cpp
index c51a3fc960..e840a2ed30 100644
--- a/src/chainparamsbase.cpp
+++ b/src/chainparamsbase.cpp
@@ -9,7 +9,6 @@
#include <util.h>
#include <assert.h>
-#include <memory>
const std::string CBaseChainParams::MAIN = "main";
const std::string CBaseChainParams::TESTNET = "test";
diff --git a/src/cuckoocache.h b/src/cuckoocache.h
index d1de712024..15f6873961 100644
--- a/src/cuckoocache.h
+++ b/src/cuckoocache.h
@@ -242,14 +242,14 @@ private:
*/
inline std::array<uint32_t, 8> compute_hashes(const Element& e) const
{
- return {{(uint32_t)((hash_function.template operator()<0>(e) * (uint64_t)size) >> 32),
- (uint32_t)((hash_function.template operator()<1>(e) * (uint64_t)size) >> 32),
- (uint32_t)((hash_function.template operator()<2>(e) * (uint64_t)size) >> 32),
- (uint32_t)((hash_function.template operator()<3>(e) * (uint64_t)size) >> 32),
- (uint32_t)((hash_function.template operator()<4>(e) * (uint64_t)size) >> 32),
- (uint32_t)((hash_function.template operator()<5>(e) * (uint64_t)size) >> 32),
- (uint32_t)((hash_function.template operator()<6>(e) * (uint64_t)size) >> 32),
- (uint32_t)((hash_function.template operator()<7>(e) * (uint64_t)size) >> 32)}};
+ return {{(uint32_t)(((uint64_t)hash_function.template operator()<0>(e) * (uint64_t)size) >> 32),
+ (uint32_t)(((uint64_t)hash_function.template operator()<1>(e) * (uint64_t)size) >> 32),
+ (uint32_t)(((uint64_t)hash_function.template operator()<2>(e) * (uint64_t)size) >> 32),
+ (uint32_t)(((uint64_t)hash_function.template operator()<3>(e) * (uint64_t)size) >> 32),
+ (uint32_t)(((uint64_t)hash_function.template operator()<4>(e) * (uint64_t)size) >> 32),
+ (uint32_t)(((uint64_t)hash_function.template operator()<5>(e) * (uint64_t)size) >> 32),
+ (uint32_t)(((uint64_t)hash_function.template operator()<6>(e) * (uint64_t)size) >> 32),
+ (uint32_t)(((uint64_t)hash_function.template operator()<7>(e) * (uint64_t)size) >> 32)}};
}
/* end
diff --git a/src/init.cpp b/src/init.cpp
index 9684b0c702..9edd93000f 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -47,7 +47,6 @@
#include <walletinitinterface.h>
#include <stdint.h>
#include <stdio.h>
-#include <memory>
#ifndef WIN32
#include <signal.h>
@@ -81,7 +80,7 @@ public:
bool ParameterInteraction() override {return true;}
void RegisterRPC(CRPCTable &) override {}
bool Verify() override {return true;}
- bool Open() override {return true;}
+ bool Open() override {LogPrintf("No wallet support compiled in!\n"); return true;}
void Start(CScheduler& scheduler) override {}
void Flush() override {}
void Stop() override {}
diff --git a/src/interfaces/handler.cpp b/src/interfaces/handler.cpp
index 1443fe9f18..8e45faa2a5 100644
--- a/src/interfaces/handler.cpp
+++ b/src/interfaces/handler.cpp
@@ -7,7 +7,6 @@
#include <util.h>
#include <boost/signals2/connection.hpp>
-#include <memory>
#include <utility>
namespace interfaces {
diff --git a/src/interfaces/wallet.cpp b/src/interfaces/wallet.cpp
index 0244fe70f5..fbf631f7da 100644
--- a/src/interfaces/wallet.cpp
+++ b/src/interfaces/wallet.cpp
@@ -22,8 +22,6 @@
#include <wallet/feebumper.h>
#include <wallet/wallet.h>
-#include <memory>
-
namespace interfaces {
namespace {
@@ -424,7 +422,7 @@ public:
std::unique_ptr<Handler> handleTransactionChanged(TransactionChangedFn fn) override
{
return MakeHandler(m_wallet.NotifyTransactionChanged.connect(
- [fn, this](CWallet*, const uint256& txid, ChangeType status) { fn(txid, status); }));
+ [fn](CWallet*, const uint256& txid, ChangeType status) { fn(txid, status); }));
}
std::unique_ptr<Handler> handleWatchOnlyChanged(WatchOnlyChangedFn fn) override
{
diff --git a/src/miner.cpp b/src/miner.cpp
index d2be393538..0660df928c 100644
--- a/src/miner.cpp
+++ b/src/miner.cpp
@@ -27,7 +27,6 @@
#include <validationinterface.h>
#include <algorithm>
-#include <memory>
#include <queue>
#include <utility>
diff --git a/src/net.cpp b/src/net.cpp
index f45592ab83..342dfbaeb9 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -20,7 +20,6 @@
#include <ui_interface.h>
#include <utilstrencodings.h>
-#include <memory>
#ifdef WIN32
#include <string.h>
#else
diff --git a/src/netaddress.h b/src/netaddress.h
index b3d1407f72..38f8709257 100644
--- a/src/netaddress.h
+++ b/src/netaddress.h
@@ -141,7 +141,7 @@ class CSubNet
class CService : public CNetAddr
{
protected:
- unsigned short port; // host order
+ uint16_t port; // host order
public:
CService();
@@ -168,13 +168,7 @@ class CService : public CNetAddr
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(ip);
-
- // TODO: introduce native support for BE serialization in serialize.h
- unsigned short portN = htons(port);
- READWRITE(Span<unsigned char>((unsigned char*)&portN, 2));
- if (ser_action.ForRead()) {
- port = ntohs(portN);
- }
+ READWRITE(WrapBigEndian(port));
}
};
diff --git a/src/primitives/block.h b/src/primitives/block.h
index 5d6d44ac76..1fca55d910 100644
--- a/src/primitives/block.h
+++ b/src/primitives/block.h
@@ -93,7 +93,7 @@ public:
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
- READWRITE(*static_cast<CBlockHeader*>(this));
+ READWRITEAS(CBlockHeader, *this);
READWRITE(vtx);
}
diff --git a/src/protocol.h b/src/protocol.h
index a07c5ea862..3a9b2d2561 100644
--- a/src/protocol.h
+++ b/src/protocol.h
@@ -349,7 +349,7 @@ public:
uint64_t nServicesInt = nServices;
READWRITE(nServicesInt);
nServices = static_cast<ServiceFlags>(nServicesInt);
- READWRITE(*static_cast<CService*>(this));
+ READWRITEAS(CService, *this);
}
// TODO: make private (improves encapsulation)
diff --git a/src/qt/README.md b/src/qt/README.md
index 7ffea98170..7c23ccadcc 100644
--- a/src/qt/README.md
+++ b/src/qt/README.md
@@ -1,6 +1,6 @@
-This directory contains the BitcoinQT graphical user interface (GUI). It uses the cross platform framework [QT](https://www1.qt.io/developers/).
+This directory contains the BitcoinQT graphical user interface (GUI). It uses the cross-platform framework [Qt](https://www1.qt.io/developers/).
-The current precise version for QT 5 is specified in [qt.mk](/depends/packages/qt.mk). QT 4 is also supported (see [#8263](https://github.com/bitcoin/bitcoin/issues/8263)).
+The current precise version for Qt 5 is specified in [qt.mk](/depends/packages/qt.mk). Qt 4 is also supported (see [#8263](https://github.com/bitcoin/bitcoin/issues/8263)).
## Compile and run
@@ -36,7 +36,7 @@ Represents the main window of the Bitcoin UI.
### \*model.(h/cpp)
-The model. When it has a corresponding controller, it generally inherits from [QAbstractTableModel](http://doc.qt.io/qt-5/qabstracttablemodel.html). Models that are used by controllers as helpers inherit from other QT classes like [QValidator](http://doc.qt.io/qt-5/qvalidator.html).
+The model. When it has a corresponding controller, it generally inherits from [QAbstractTableModel](http://doc.qt.io/qt-5/qabstracttablemodel.html). Models that are used by controllers as helpers inherit from other Qt classes like [QValidator](http://doc.qt.io/qt-5/qvalidator.html).
ClientModel is used by the main application `bitcoingui` and several models like `peertablemodel`.
@@ -69,7 +69,7 @@ Represents the view to a single wallet.
## Contribute
-See [CONTRIBUTING.md](/CONTRIBUTING.md) for general guidelines. Specifically for QT:
+See [CONTRIBUTING.md](/CONTRIBUTING.md) for general guidelines. Specifically for Qt:
* don't change `local/bitcoin_en.ts`; this happens [automatically](/doc/translation_process.md#writing-code-with-translations)
@@ -83,7 +83,7 @@ Uncheck everything except Qt Creator during the installation process.
Instructions for OSX:
-1. Make sure you installed everything through Homebrew mentioned in the [OSX build instructions](/docs/build-osx.md)
+1. Make sure you installed everything through Homebrew mentioned in the [OSX build instructions](/doc/build-osx.md)
2. Use `./configure` with the `--enable-debug` flag
3. In Qt Creator do "New Project" -> Import Project -> Import Existing Project
4. Enter "bitcoin-qt" as project name, enter src/qt as location
diff --git a/src/qt/optionsmodel.cpp b/src/qt/optionsmodel.cpp
index 30c8124c58..cae9dace4c 100644
--- a/src/qt/optionsmodel.cpp
+++ b/src/qt/optionsmodel.cpp
@@ -24,6 +24,8 @@
const char *DEFAULT_GUI_PROXY_HOST = "127.0.0.1";
+static const QString GetDefaultProxyAddress();
+
OptionsModel::OptionsModel(interfaces::Node& node, QObject *parent, bool resetSettings) :
QAbstractListModel(parent), m_node(node)
{
@@ -121,7 +123,7 @@ void OptionsModel::Init(bool resetSettings)
if (!settings.contains("fUseProxy"))
settings.setValue("fUseProxy", false);
if (!settings.contains("addrProxy"))
- settings.setValue("addrProxy", QString("%1:%2").arg(DEFAULT_GUI_PROXY_HOST, DEFAULT_GUI_PROXY_PORT));
+ settings.setValue("addrProxy", GetDefaultProxyAddress());
// Only try to set -proxy, if user has enabled fUseProxy
if (settings.value("fUseProxy").toBool() && !m_node.softSetArg("-proxy", settings.value("addrProxy").toString().toStdString()))
addOverriddenOption("-proxy");
@@ -131,7 +133,7 @@ void OptionsModel::Init(bool resetSettings)
if (!settings.contains("fUseSeparateProxyTor"))
settings.setValue("fUseSeparateProxyTor", false);
if (!settings.contains("addrSeparateProxyTor"))
- settings.setValue("addrSeparateProxyTor", QString("%1:%2").arg(DEFAULT_GUI_PROXY_HOST, DEFAULT_GUI_PROXY_PORT));
+ settings.setValue("addrSeparateProxyTor", GetDefaultProxyAddress());
// Only try to set -onion, if user has enabled fUseSeparateProxyTor
if (settings.value("fUseSeparateProxyTor").toBool() && !m_node.softSetArg("-onion", settings.value("addrSeparateProxyTor").toString().toStdString()))
addOverriddenOption("-onion");
@@ -223,6 +225,11 @@ static void SetProxySetting(QSettings &settings, const QString &name, const Prox
settings.setValue(name, ip_port.ip + ":" + ip_port.port);
}
+static const QString GetDefaultProxyAddress()
+{
+ return QString("%1:%2").arg(DEFAULT_GUI_PROXY_HOST).arg(DEFAULT_GUI_PROXY_PORT);
+}
+
// read QSettings values and return them
QVariant OptionsModel::data(const QModelIndex & index, int role) const
{
@@ -485,4 +492,16 @@ void OptionsModel::checkAndMigrate()
settings.setValue(strSettingsVersionKey, CLIENT_VERSION);
}
+
+ // Overwrite the 'addrProxy' setting in case it has been set to an illegal
+ // default value (see issue #12623; PR #12650).
+ if (settings.contains("addrProxy") && settings.value("addrProxy").toString().endsWith("%2")) {
+ settings.setValue("addrProxy", GetDefaultProxyAddress());
+ }
+
+ // Overwrite the 'addrSeparateProxyTor' setting in case it has been set to an illegal
+ // default value (see issue #12623; PR #12650).
+ if (settings.contains("addrSeparateProxyTor") && settings.value("addrSeparateProxyTor").toString().endsWith("%2")) {
+ settings.setValue("addrSeparateProxyTor", GetDefaultProxyAddress());
+ }
}
diff --git a/src/qt/paymentrequestplus.cpp b/src/qt/paymentrequestplus.cpp
index 357e98a53c..b0ef475b35 100644
--- a/src/qt/paymentrequestplus.cpp
+++ b/src/qt/paymentrequestplus.cpp
@@ -9,7 +9,6 @@
#include <qt/paymentrequestplus.h>
-#include <script/script.h>
#include <util.h>
#include <stdexcept>
diff --git a/src/qt/test/wallettests.cpp b/src/qt/test/wallettests.cpp
index fb86cf5ec9..dcc834c352 100644
--- a/src/qt/test/wallettests.cpp
+++ b/src/qt/test/wallettests.cpp
@@ -158,7 +158,7 @@ void TestGUI()
for (int i = 0; i < 5; ++i) {
test.CreateAndProcessBlock({}, GetScriptForRawPubKey(test.coinbaseKey.GetPubKey()));
}
- CWallet wallet("mock", CWalletDBWrapper::CreateMock());
+ CWallet wallet("mock", WalletDatabase::CreateMock());
bool firstRun;
wallet.LoadWallet(firstRun);
{
diff --git a/src/random.cpp b/src/random.cpp
index a845526d8a..b004dcac39 100644
--- a/src/random.cpp
+++ b/src/random.cpp
@@ -15,7 +15,6 @@
#include <utilstrencodings.h> // for GetTime()
#include <stdlib.h>
-#include <limits>
#include <chrono>
#include <thread>
diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp
index 01f932dbb4..34c41b3b6b 100644
--- a/src/rpc/client.cpp
+++ b/src/rpc/client.cpp
@@ -51,6 +51,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "listreceivedbylabel", 0, "minconf" },
{ "listreceivedbylabel", 1, "include_empty" },
{ "listreceivedbylabel", 2, "include_watchonly" },
+ { "getlabeladdress", 1, "force" },
{ "getbalance", 1, "minconf" },
{ "getbalance", 2, "include_watchonly" },
{ "getblockhash", 0, "height" },
diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp
index 06882c0dfd..75bc983200 100644
--- a/src/rpc/mining.cpp
+++ b/src/rpc/mining.cpp
@@ -235,6 +235,7 @@ UniValue prioritisetransaction(const JSONRPCRequest& request)
"2. dummy (numeric, optional) API-Compatibility for previous API. Must be zero or null.\n"
" DEPRECATED. For forward compatibility use named arguments and omit this parameter.\n"
"3. fee_delta (numeric, required) The fee value (in satoshis) to add (or subtract, if negative).\n"
+ " Note, that this value is not a fee rate. It is a value to modify absolute fee of the TX.\n"
" The fee is not actually paid, only the algorithm for selecting transactions into a block\n"
" considers the transaction as it would have paid a higher (or lower) fee.\n"
"\nResult:\n"
diff --git a/src/script/script.h b/src/script/script.h
index 591777672e..8e5a792c7d 100644
--- a/src/script/script.h
+++ b/src/script/script.h
@@ -415,7 +415,7 @@ public:
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
- READWRITE(static_cast<CScriptBase&>(*this));
+ READWRITEAS(CScriptBase, *this);
}
CScript& operator+=(const CScript& b)
diff --git a/src/serialize.h b/src/serialize.h
index e90b041cc2..e54c7483d2 100644
--- a/src/serialize.h
+++ b/src/serialize.h
@@ -79,6 +79,11 @@ template<typename Stream> inline void ser_writedata16(Stream &s, uint16_t obj)
obj = htole16(obj);
s.write((char*)&obj, 2);
}
+template<typename Stream> inline void ser_writedata16be(Stream &s, uint16_t obj)
+{
+ obj = htobe16(obj);
+ s.write((char*)&obj, 2);
+}
template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
{
obj = htole32(obj);
@@ -101,6 +106,12 @@ template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
s.read((char*)&obj, 2);
return le16toh(obj);
}
+template<typename Stream> inline uint16_t ser_readdata16be(Stream &s)
+{
+ uint16_t obj;
+ s.read((char*)&obj, 2);
+ return be16toh(obj);
+}
template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
{
uint32_t obj;
@@ -155,7 +166,12 @@ enum
SER_GETHASH = (1 << 2),
};
-#define READWRITE(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
+//! Convert the reference base type to X, without changing constness or reference type.
+template<typename X> X& ReadWriteAsHelper(X& x) { return x; }
+template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
+
+#define READWRITE(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
+#define READWRITEAS(type, obj) (::SerReadWriteMany(s, ser_action, ReadWriteAsHelper<type>(obj)))
/**
* Implement three methods for serializable objects. These are actually wrappers over
@@ -411,6 +427,40 @@ public:
}
};
+/** Serialization wrapper class for big-endian integers.
+ *
+ * Use this wrapper around integer types that are stored in memory in native
+ * byte order, but serialized in big endian notation. This is only intended
+ * to implement serializers that are compatible with existing formats, and
+ * its use is not recommended for new data structures.
+ *
+ * Only 16-bit types are supported for now.
+ */
+template<typename I>
+class BigEndian
+{
+protected:
+ I& m_val;
+public:
+ explicit BigEndian(I& val) : m_val(val)
+ {
+ static_assert(std::is_unsigned<I>::value, "BigEndian type must be unsigned integer");
+ static_assert(sizeof(I) == 2 && std::numeric_limits<I>::min() == 0 && std::numeric_limits<I>::max() == std::numeric_limits<uint16_t>::max(), "Unsupported BigEndian size");
+ }
+
+ template<typename Stream>
+ void Serialize(Stream& s) const
+ {
+ ser_writedata16be(s, m_val);
+ }
+
+ template<typename Stream>
+ void Unserialize(Stream& s)
+ {
+ m_val = ser_readdata16be(s);
+ }
+};
+
class CCompactSize
{
protected:
@@ -461,6 +511,9 @@ public:
template<VarIntMode Mode=VarIntMode::DEFAULT, typename I>
CVarInt<Mode, I> WrapVarInt(I& n) { return CVarInt<Mode, I>{n}; }
+template<typename I>
+BigEndian<I> WrapBigEndian(I& n) { return BigEndian<I>(n); }
+
/**
* Forward declarations
*/
diff --git a/src/support/lockedpool.cpp b/src/support/lockedpool.cpp
index 51c337ed2f..f10fd07c63 100644
--- a/src/support/lockedpool.cpp
+++ b/src/support/lockedpool.cpp
@@ -27,7 +27,6 @@
#endif
#include <algorithm>
-#include <memory>
LockedPoolManager* LockedPoolManager::_instance = nullptr;
std::once_flag LockedPoolManager::init_flag;
diff --git a/src/test/test_bitcoin.cpp b/src/test/test_bitcoin.cpp
index 9390a93b99..ff20d4b3d7 100644
--- a/src/test/test_bitcoin.cpp
+++ b/src/test/test_bitcoin.cpp
@@ -17,8 +17,6 @@
#include <rpc/register.h>
#include <script/sigcache.h>
-#include <memory>
-
void CConnmanTest::AddNode(CNode& node)
{
LOCK(g_connman->cs_vNodes);
@@ -68,7 +66,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
RegisterAllCoreRPCCommands(tableRPC);
ClearDatadirCache();
- pathTemp = fs::temp_directory_path() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(100000)));
+ pathTemp = fs::temp_directory_path() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(1 << 30)));
fs::create_directories(pathTemp);
gArgs.ForceSetArg("-datadir", pathTemp.string());
diff --git a/src/test/uint256_tests.cpp b/src/test/uint256_tests.cpp
index ad5478e829..20ed29f59b 100644
--- a/src/test/uint256_tests.cpp
+++ b/src/test/uint256_tests.cpp
@@ -266,4 +266,17 @@ BOOST_AUTO_TEST_CASE( conversion )
BOOST_CHECK(R2L.GetHex() == UintToArith256(R2L).GetHex());
}
+BOOST_AUTO_TEST_CASE( operator_with_self )
+{
+ arith_uint256 v = UintToArith256(uint256S("02"));
+ v *= v;
+ BOOST_CHECK(v == UintToArith256(uint256S("04")));
+ v /= v;
+ BOOST_CHECK(v == UintToArith256(uint256S("01")));
+ v += v;
+ BOOST_CHECK(v == UintToArith256(uint256S("02")));
+ v -= v;
+ BOOST_CHECK(v == UintToArith256(uint256S("0")));
+}
+
BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp
index b4af3617f1..d41c43a795 100644
--- a/src/test/util_tests.cpp
+++ b/src/test/util_tests.cpp
@@ -62,7 +62,7 @@ BOOST_AUTO_TEST_CASE(util_ParseHex)
result = ParseHex("12 34 56 78");
BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
- // Leading space must be supported (used in CDBEnv::Salvage)
+ // Leading space must be supported (used in BerkeleyEnvironment::Salvage)
result = ParseHex(" 89 34 56 78");
BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
diff --git a/src/txdb.h b/src/txdb.h
index ad76b3257d..f3454e7d09 100644
--- a/src/txdb.h
+++ b/src/txdb.h
@@ -47,7 +47,7 @@ struct CDiskTxPos : public CDiskBlockPos
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
- READWRITE(*static_cast<CDiskBlockPos*>(this));
+ READWRITEAS(CDiskBlockPos, *this);
READWRITE(VARINT(nTxOffset));
}
diff --git a/src/util.cpp b/src/util.cpp
index 393cc413d6..f55c9c8c34 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -1064,8 +1064,8 @@ fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific)
int ScheduleBatchPriority(void)
{
#ifdef SCHED_BATCH
- const static sched_param param{.sched_priority = 0};
- if (int ret = pthread_setschedparam(0, SCHED_BATCH, &param)) {
+ const static sched_param param{0};
+ if (int ret = pthread_setschedparam(pthread_self(), SCHED_BATCH, &param)) {
LogPrintf("Failed to pthread_setschedparam: %s\n", strerror(errno));
return ret;
}
diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp
index 553cae4d02..10a06e4b9a 100644
--- a/src/wallet/db.cpp
+++ b/src/wallet/db.cpp
@@ -8,7 +8,6 @@
#include <addrman.h>
#include <hash.h>
#include <protocol.h>
-#include <util.h>
#include <utilstrencodings.h>
#include <wallet/walletutil.h>
@@ -30,14 +29,14 @@ namespace {
//! (https://docs.oracle.com/cd/E17275_01/html/programmer_reference/program_copy.html),
//! so bitcoin should never create different databases with the same fileid, but
//! this error can be triggered if users manually copy database files.
-void CheckUniqueFileid(const CDBEnv& env, const std::string& filename, Db& db)
+void CheckUniqueFileid(const BerkeleyEnvironment& env, const std::string& filename, Db& db)
{
if (env.IsMock()) return;
u_int8_t fileid[DB_FILE_ID_LEN];
int ret = db.get_mpf()->get_fileid(fileid);
if (ret != 0) {
- throw std::runtime_error(strprintf("CDB: Can't open database %s (get_fileid failed with %d)", filename, ret));
+ throw std::runtime_error(strprintf("BerkeleyBatch: Can't open database %s (get_fileid failed with %d)", filename, ret));
}
for (const auto& item : env.mapDb) {
@@ -46,7 +45,7 @@ void CheckUniqueFileid(const CDBEnv& env, const std::string& filename, Db& db)
memcmp(fileid, item_fileid, sizeof(fileid)) == 0) {
const char* item_filename = nullptr;
item.second->get_dbname(&item_filename, nullptr);
- throw std::runtime_error(strprintf("CDB: Can't open database %s (duplicates fileid %s from %s)", filename,
+ throw std::runtime_error(strprintf("BerkeleyBatch: Can't open database %s (duplicates fileid %s from %s)", filename,
HexStr(std::begin(item_fileid), std::end(item_fileid)),
item_filename ? item_filename : "(unknown database)"));
}
@@ -54,10 +53,10 @@ void CheckUniqueFileid(const CDBEnv& env, const std::string& filename, Db& db)
}
CCriticalSection cs_db;
-std::map<std::string, CDBEnv> g_dbenvs; //!< Map from directory name to open db environment.
+std::map<std::string, BerkeleyEnvironment> g_dbenvs; //!< Map from directory name to open db environment.
} // namespace
-CDBEnv* GetWalletEnv(const fs::path& wallet_path, std::string& database_filename)
+BerkeleyEnvironment* GetWalletEnv(const fs::path& wallet_path, std::string& database_filename)
{
fs::path env_directory;
if (fs::is_regular_file(wallet_path)) {
@@ -73,7 +72,7 @@ CDBEnv* GetWalletEnv(const fs::path& wallet_path, std::string& database_filename
database_filename = "wallet.dat";
}
LOCK(cs_db);
- // Note: An ununsed temporary CDBEnv object may be created inside the
+ // Note: An ununsed temporary BerkeleyEnvironment object may be created inside the
// emplace function if the key already exists. This is a little inefficient,
// but not a big concern since the map will be changed in the future to hold
// pointers instead of objects, anyway.
@@ -81,10 +80,10 @@ CDBEnv* GetWalletEnv(const fs::path& wallet_path, std::string& database_filename
}
//
-// CDB
+// BerkeleyBatch
//
-void CDBEnv::Close()
+void BerkeleyEnvironment::Close()
{
if (!fDbEnvInit)
return;
@@ -103,29 +102,29 @@ void CDBEnv::Close()
int ret = dbenv->close(0);
if (ret != 0)
- LogPrintf("CDBEnv::EnvShutdown: Error %d shutting down database environment: %s\n", ret, DbEnv::strerror(ret));
+ LogPrintf("BerkeleyEnvironment::EnvShutdown: Error %d shutting down database environment: %s\n", ret, DbEnv::strerror(ret));
if (!fMockDb)
DbEnv((u_int32_t)0).remove(strPath.c_str(), 0);
}
-void CDBEnv::Reset()
+void BerkeleyEnvironment::Reset()
{
dbenv.reset(new DbEnv(DB_CXX_NO_EXCEPTIONS));
fDbEnvInit = false;
fMockDb = false;
}
-CDBEnv::CDBEnv(const fs::path& dir_path) : strPath(dir_path.string())
+BerkeleyEnvironment::BerkeleyEnvironment(const fs::path& dir_path) : strPath(dir_path.string())
{
Reset();
}
-CDBEnv::~CDBEnv()
+BerkeleyEnvironment::~BerkeleyEnvironment()
{
Close();
}
-bool CDBEnv::Open(bool retry)
+bool BerkeleyEnvironment::Open(bool retry)
{
if (fDbEnvInit)
return true;
@@ -142,7 +141,7 @@ bool CDBEnv::Open(bool retry)
fs::path pathLogDir = pathIn / "database";
TryCreateDirectories(pathLogDir);
fs::path pathErrorFile = pathIn / "db.log";
- LogPrintf("CDBEnv::Open: LogDir=%s ErrorFile=%s\n", pathLogDir.string(), pathErrorFile.string());
+ LogPrintf("BerkeleyEnvironment::Open: LogDir=%s ErrorFile=%s\n", pathLogDir.string(), pathErrorFile.string());
unsigned int nEnvFlags = 0;
if (gArgs.GetBoolArg("-privdb", DEFAULT_WALLET_PRIVDB))
@@ -170,7 +169,7 @@ bool CDBEnv::Open(bool retry)
S_IRUSR | S_IWUSR);
if (ret != 0) {
dbenv->close(0);
- LogPrintf("CDBEnv::Open: Error %d opening database environment: %s\n", ret, DbEnv::strerror(ret));
+ LogPrintf("BerkeleyEnvironment::Open: Error %d opening database environment: %s\n", ret, DbEnv::strerror(ret));
if (retry) {
// try moving the database env out of the way
fs::path pathDatabaseBak = pathIn / strprintf("database.%d.bak", GetTime());
@@ -195,14 +194,14 @@ bool CDBEnv::Open(bool retry)
return true;
}
-void CDBEnv::MakeMock()
+void BerkeleyEnvironment::MakeMock()
{
if (fDbEnvInit)
- throw std::runtime_error("CDBEnv::MakeMock: Already initialized");
+ throw std::runtime_error("BerkeleyEnvironment::MakeMock: Already initialized");
boost::this_thread::interruption_point();
- LogPrint(BCLog::DB, "CDBEnv::MakeMock\n");
+ LogPrint(BCLog::DB, "BerkeleyEnvironment::MakeMock\n");
dbenv->set_cachesize(1, 0, 1);
dbenv->set_lg_bsize(10485760 * 4);
@@ -221,13 +220,13 @@ void CDBEnv::MakeMock()
DB_PRIVATE,
S_IRUSR | S_IWUSR);
if (ret > 0)
- throw std::runtime_error(strprintf("CDBEnv::MakeMock: Error %d opening database environment.", ret));
+ throw std::runtime_error(strprintf("BerkeleyEnvironment::MakeMock: Error %d opening database environment.", ret));
fDbEnvInit = true;
fMockDb = true;
}
-CDBEnv::VerifyResult CDBEnv::Verify(const std::string& strFile, recoverFunc_type recoverFunc, std::string& out_backup_filename)
+BerkeleyEnvironment::VerifyResult BerkeleyEnvironment::Verify(const std::string& strFile, recoverFunc_type recoverFunc, std::string& out_backup_filename)
{
LOCK(cs_db);
assert(mapFileUseCount.count(strFile) == 0);
@@ -244,10 +243,10 @@ CDBEnv::VerifyResult CDBEnv::Verify(const std::string& strFile, recoverFunc_type
return (fRecovered ? VerifyResult::RECOVER_OK : VerifyResult::RECOVER_FAIL);
}
-bool CDB::Recover(const fs::path& file_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& newFilename)
+bool BerkeleyBatch::Recover(const fs::path& file_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& newFilename)
{
std::string filename;
- CDBEnv* env = GetWalletEnv(file_path, filename);
+ BerkeleyEnvironment* env = GetWalletEnv(file_path, filename);
// Recovery procedure:
// move wallet file to walletfilename.timestamp.bak
@@ -269,7 +268,7 @@ bool CDB::Recover(const fs::path& file_path, void *callbackDataIn, bool (*recove
return false;
}
- std::vector<CDBEnv::KeyValPair> salvagedData;
+ std::vector<BerkeleyEnvironment::KeyValPair> salvagedData;
bool fSuccess = env->Salvage(newFilename, true, salvagedData);
if (salvagedData.empty())
{
@@ -292,7 +291,7 @@ bool CDB::Recover(const fs::path& file_path, void *callbackDataIn, bool (*recove
}
DbTxn* ptxn = env->TxnBegin();
- for (CDBEnv::KeyValPair& row : salvagedData)
+ for (BerkeleyEnvironment::KeyValPair& row : salvagedData)
{
if (recoverKVcallback)
{
@@ -313,10 +312,10 @@ bool CDB::Recover(const fs::path& file_path, void *callbackDataIn, bool (*recove
return fSuccess;
}
-bool CDB::VerifyEnvironment(const fs::path& file_path, std::string& errorStr)
+bool BerkeleyBatch::VerifyEnvironment(const fs::path& file_path, std::string& errorStr)
{
std::string walletFile;
- CDBEnv* env = GetWalletEnv(file_path, walletFile);
+ BerkeleyEnvironment* env = GetWalletEnv(file_path, walletFile);
fs::path walletDir = env->Directory();
LogPrintf("Using BerkeleyDB version %s\n", DbEnv::version(0, 0, 0));
@@ -337,17 +336,17 @@ bool CDB::VerifyEnvironment(const fs::path& file_path, std::string& errorStr)
return true;
}
-bool CDB::VerifyDatabaseFile(const fs::path& file_path, std::string& warningStr, std::string& errorStr, CDBEnv::recoverFunc_type recoverFunc)
+bool BerkeleyBatch::VerifyDatabaseFile(const fs::path& file_path, std::string& warningStr, std::string& errorStr, BerkeleyEnvironment::recoverFunc_type recoverFunc)
{
std::string walletFile;
- CDBEnv* env = GetWalletEnv(file_path, walletFile);
+ BerkeleyEnvironment* env = GetWalletEnv(file_path, walletFile);
fs::path walletDir = env->Directory();
if (fs::exists(walletDir / walletFile))
{
std::string backup_filename;
- CDBEnv::VerifyResult r = env->Verify(walletFile, recoverFunc, backup_filename);
- if (r == CDBEnv::VerifyResult::RECOVER_OK)
+ BerkeleyEnvironment::VerifyResult r = env->Verify(walletFile, recoverFunc, backup_filename);
+ if (r == BerkeleyEnvironment::VerifyResult::RECOVER_OK)
{
warningStr = strprintf(_("Warning: Wallet file corrupt, data salvaged!"
" Original %s saved as %s in %s; if"
@@ -355,7 +354,7 @@ bool CDB::VerifyDatabaseFile(const fs::path& file_path, std::string& warningStr,
" restore from a backup."),
walletFile, backup_filename, walletDir);
}
- if (r == CDBEnv::VerifyResult::RECOVER_FAIL)
+ if (r == BerkeleyEnvironment::VerifyResult::RECOVER_FAIL)
{
errorStr = strprintf(_("%s corrupt, salvage failed"), walletFile);
return false;
@@ -370,7 +369,7 @@ static const char *HEADER_END = "HEADER=END";
/* End of key/value data */
static const char *DATA_END = "DATA=END";
-bool CDBEnv::Salvage(const std::string& strFile, bool fAggressive, std::vector<CDBEnv::KeyValPair>& vResult)
+bool BerkeleyEnvironment::Salvage(const std::string& strFile, bool fAggressive, std::vector<BerkeleyEnvironment::KeyValPair>& vResult)
{
LOCK(cs_db);
assert(mapFileUseCount.count(strFile) == 0);
@@ -384,14 +383,14 @@ bool CDBEnv::Salvage(const std::string& strFile, bool fAggressive, std::vector<C
Db db(dbenv.get(), 0);
int result = db.verify(strFile.c_str(), nullptr, &strDump, flags);
if (result == DB_VERIFY_BAD) {
- LogPrintf("CDBEnv::Salvage: Database salvage found errors, all data may not be recoverable.\n");
+ LogPrintf("BerkeleyEnvironment::Salvage: Database salvage found errors, all data may not be recoverable.\n");
if (!fAggressive) {
- LogPrintf("CDBEnv::Salvage: Rerun with aggressive mode to ignore errors and continue.\n");
+ LogPrintf("BerkeleyEnvironment::Salvage: Rerun with aggressive mode to ignore errors and continue.\n");
return false;
}
}
if (result != 0 && result != DB_VERIFY_BAD) {
- LogPrintf("CDBEnv::Salvage: Database salvage failed with result %d.\n", result);
+ LogPrintf("BerkeleyEnvironment::Salvage: Database salvage failed with result %d.\n", result);
return false;
}
@@ -415,7 +414,7 @@ bool CDBEnv::Salvage(const std::string& strFile, bool fAggressive, std::vector<C
break;
getline(strDump, valueHex);
if (valueHex == DATA_END) {
- LogPrintf("CDBEnv::Salvage: WARNING: Number of keys in data does not match number of values.\n");
+ LogPrintf("BerkeleyEnvironment::Salvage: WARNING: Number of keys in data does not match number of values.\n");
break;
}
vResult.push_back(make_pair(ParseHex(keyHex), ParseHex(valueHex)));
@@ -423,7 +422,7 @@ bool CDBEnv::Salvage(const std::string& strFile, bool fAggressive, std::vector<C
}
if (keyHex != DATA_END) {
- LogPrintf("CDBEnv::Salvage: WARNING: Unexpected end of file while reading salvage output.\n");
+ LogPrintf("BerkeleyEnvironment::Salvage: WARNING: Unexpected end of file while reading salvage output.\n");
return false;
}
@@ -431,7 +430,7 @@ bool CDBEnv::Salvage(const std::string& strFile, bool fAggressive, std::vector<C
}
-void CDBEnv::CheckpointLSN(const std::string& strFile)
+void BerkeleyEnvironment::CheckpointLSN(const std::string& strFile)
{
dbenv->txn_checkpoint(0, 0, 0);
if (fMockDb)
@@ -440,15 +439,15 @@ void CDBEnv::CheckpointLSN(const std::string& strFile)
}
-CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr)
+BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr)
{
fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
fFlushOnClose = fFlushOnCloseIn;
- env = dbw.env;
- if (dbw.IsDummy()) {
+ env = database.env;
+ if (database.IsDummy()) {
return;
}
- const std::string &strFilename = dbw.strFile;
+ const std::string &strFilename = database.strFile;
bool fCreate = strchr(pszMode, 'c') != nullptr;
unsigned int nFlags = DB_THREAD;
@@ -458,7 +457,7 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
{
LOCK(cs_db);
if (!env->Open(false /* retry */))
- throw std::runtime_error("CDB: Failed to open database environment.");
+ throw std::runtime_error("BerkeleyBatch: Failed to open database environment.");
pdb = env->mapDb[strFilename];
if (pdb == nullptr) {
@@ -470,7 +469,7 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
DbMpoolFile* mpf = pdb_temp->get_mpf();
ret = mpf->set_flags(DB_MPOOL_NOFILE, 1);
if (ret != 0) {
- throw std::runtime_error(strprintf("CDB: Failed to configure for no temp file backing for database %s", strFilename));
+ throw std::runtime_error(strprintf("BerkeleyBatch: Failed to configure for no temp file backing for database %s", strFilename));
}
}
@@ -482,7 +481,7 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
0);
if (ret != 0) {
- throw std::runtime_error(strprintf("CDB: Error %d, can't open database %s", ret, strFilename));
+ throw std::runtime_error(strprintf("BerkeleyBatch: Error %d, can't open database %s", ret, strFilename));
}
// Call CheckUniqueFileid on the containing BDB environment to
@@ -519,7 +518,7 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
}
}
-void CDB::Flush()
+void BerkeleyBatch::Flush()
{
if (activeTxn)
return;
@@ -532,12 +531,12 @@ void CDB::Flush()
env->dbenv->txn_checkpoint(nMinutes ? gArgs.GetArg("-dblogsize", DEFAULT_WALLET_DBLOGSIZE) * 1024 : 0, nMinutes, 0);
}
-void CWalletDBWrapper::IncrementUpdateCounter()
+void BerkeleyDatabase::IncrementUpdateCounter()
{
++nUpdateCounter;
}
-void CDB::Close()
+void BerkeleyBatch::Close()
{
if (!pdb)
return;
@@ -555,7 +554,7 @@ void CDB::Close()
}
}
-void CDBEnv::CloseDb(const std::string& strFile)
+void BerkeleyEnvironment::CloseDb(const std::string& strFile)
{
{
LOCK(cs_db);
@@ -569,13 +568,13 @@ void CDBEnv::CloseDb(const std::string& strFile)
}
}
-bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
+bool BerkeleyBatch::Rewrite(BerkeleyDatabase& database, const char* pszSkip)
{
- if (dbw.IsDummy()) {
+ if (database.IsDummy()) {
return true;
}
- CDBEnv *env = dbw.env;
- const std::string& strFile = dbw.strFile;
+ BerkeleyEnvironment *env = database.env;
+ const std::string& strFile = database.strFile;
while (true) {
{
LOCK(cs_db);
@@ -586,10 +585,10 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
env->mapFileUseCount.erase(strFile);
bool fSuccess = true;
- LogPrintf("CDB::Rewrite: Rewriting %s...\n", strFile);
+ LogPrintf("BerkeleyBatch::Rewrite: Rewriting %s...\n", strFile);
std::string strFileRes = strFile + ".rewrite";
{ // surround usage of db with extra {}
- CDB db(dbw, "r");
+ BerkeleyBatch db(database, "r");
std::unique_ptr<Db> pdbCopy = MakeUnique<Db>(env->dbenv.get(), 0);
int ret = pdbCopy->open(nullptr, // Txn pointer
@@ -599,7 +598,7 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
DB_CREATE, // Flags
0);
if (ret > 0) {
- LogPrintf("CDB::Rewrite: Can't create database file %s\n", strFileRes);
+ LogPrintf("BerkeleyBatch::Rewrite: Can't create database file %s\n", strFileRes);
fSuccess = false;
}
@@ -649,7 +648,7 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
fSuccess = false;
}
if (!fSuccess)
- LogPrintf("CDB::Rewrite: Failed to rewrite database file %s\n", strFileRes);
+ LogPrintf("BerkeleyBatch::Rewrite: Failed to rewrite database file %s\n", strFileRes);
return fSuccess;
}
}
@@ -658,11 +657,11 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
}
-void CDBEnv::Flush(bool fShutdown)
+void BerkeleyEnvironment::Flush(bool fShutdown)
{
int64_t nStart = GetTimeMillis();
// Flush log data to the actual data file on all files that are not in use
- LogPrint(BCLog::DB, "CDBEnv::Flush: Flush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " database not started");
+ LogPrint(BCLog::DB, "BerkeleyEnvironment::Flush: Flush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " database not started");
if (!fDbEnvInit)
return;
{
@@ -671,21 +670,21 @@ void CDBEnv::Flush(bool fShutdown)
while (mi != mapFileUseCount.end()) {
std::string strFile = (*mi).first;
int nRefCount = (*mi).second;
- LogPrint(BCLog::DB, "CDBEnv::Flush: Flushing %s (refcount = %d)...\n", strFile, nRefCount);
+ LogPrint(BCLog::DB, "BerkeleyEnvironment::Flush: Flushing %s (refcount = %d)...\n", strFile, nRefCount);
if (nRefCount == 0) {
// Move log data to the dat file
CloseDb(strFile);
- LogPrint(BCLog::DB, "CDBEnv::Flush: %s checkpoint\n", strFile);
+ LogPrint(BCLog::DB, "BerkeleyEnvironment::Flush: %s checkpoint\n", strFile);
dbenv->txn_checkpoint(0, 0, 0);
- LogPrint(BCLog::DB, "CDBEnv::Flush: %s detach\n", strFile);
+ LogPrint(BCLog::DB, "BerkeleyEnvironment::Flush: %s detach\n", strFile);
if (!fMockDb)
dbenv->lsn_reset(strFile.c_str(), 0);
- LogPrint(BCLog::DB, "CDBEnv::Flush: %s closed\n", strFile);
+ LogPrint(BCLog::DB, "BerkeleyEnvironment::Flush: %s closed\n", strFile);
mapFileUseCount.erase(mi++);
} else
mi++;
}
- LogPrint(BCLog::DB, "CDBEnv::Flush: Flush(%s)%s took %15dms\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " database not started", GetTimeMillis() - nStart);
+ LogPrint(BCLog::DB, "BerkeleyEnvironment::Flush: Flush(%s)%s took %15dms\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " database not started", GetTimeMillis() - nStart);
if (fShutdown) {
char** listp;
if (mapFileUseCount.empty()) {
@@ -698,14 +697,14 @@ void CDBEnv::Flush(bool fShutdown)
}
}
-bool CDB::PeriodicFlush(CWalletDBWrapper& dbw)
+bool BerkeleyBatch::PeriodicFlush(BerkeleyDatabase& database)
{
- if (dbw.IsDummy()) {
+ if (database.IsDummy()) {
return true;
}
bool ret = false;
- CDBEnv *env = dbw.env;
- const std::string& strFile = dbw.strFile;
+ BerkeleyEnvironment *env = database.env;
+ const std::string& strFile = database.strFile;
TRY_LOCK(cs_db, lockDb);
if (lockDb)
{
@@ -741,12 +740,12 @@ bool CDB::PeriodicFlush(CWalletDBWrapper& dbw)
return ret;
}
-bool CWalletDBWrapper::Rewrite(const char* pszSkip)
+bool BerkeleyDatabase::Rewrite(const char* pszSkip)
{
- return CDB::Rewrite(*this, pszSkip);
+ return BerkeleyBatch::Rewrite(*this, pszSkip);
}
-bool CWalletDBWrapper::Backup(const std::string& strDest)
+bool BerkeleyDatabase::Backup(const std::string& strDest)
{
if (IsDummy()) {
return false;
@@ -787,7 +786,7 @@ bool CWalletDBWrapper::Backup(const std::string& strDest)
}
}
-void CWalletDBWrapper::Flush(bool shutdown)
+void BerkeleyDatabase::Flush(bool shutdown)
{
if (!IsDummy()) {
env->Flush(shutdown);
diff --git a/src/wallet/db.h b/src/wallet/db.h
index 49a9f3f082..5e61280f7a 100644
--- a/src/wallet/db.h
+++ b/src/wallet/db.h
@@ -25,7 +25,7 @@
static const unsigned int DEFAULT_WALLET_DBLOGSIZE = 100;
static const bool DEFAULT_WALLET_PRIVDB = true;
-class CDBEnv
+class BerkeleyEnvironment
{
private:
bool fDbEnvInit;
@@ -39,8 +39,8 @@ public:
std::map<std::string, int> mapFileUseCount;
std::map<std::string, Db*> mapDb;
- CDBEnv(const fs::path& env_directory);
- ~CDBEnv();
+ BerkeleyEnvironment(const fs::path& env_directory);
+ ~BerkeleyEnvironment();
void Reset();
void MakeMock();
@@ -86,23 +86,23 @@ public:
}
};
-/** Get CDBEnv and database filename given a wallet path. */
-CDBEnv* GetWalletEnv(const fs::path& wallet_path, std::string& database_filename);
+/** Get BerkeleyEnvironment and database filename given a wallet path. */
+BerkeleyEnvironment* GetWalletEnv(const fs::path& wallet_path, std::string& database_filename);
/** An instance of this class represents one database.
* For BerkeleyDB this is just a (env, strFile) tuple.
**/
-class CWalletDBWrapper
+class BerkeleyDatabase
{
- friend class CDB;
+ friend class BerkeleyBatch;
public:
/** Create dummy DB handle */
- CWalletDBWrapper() : nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0), env(nullptr)
+ BerkeleyDatabase() : nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0), env(nullptr)
{
}
/** Create DB handle to real database */
- CWalletDBWrapper(const fs::path& wallet_path, bool mock = false) :
+ BerkeleyDatabase(const fs::path& wallet_path, bool mock = false) :
nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0)
{
env = GetWalletEnv(wallet_path, strFile);
@@ -114,21 +114,21 @@ public:
}
/** Return object for accessing database at specified path. */
- static std::unique_ptr<CWalletDBWrapper> Create(const fs::path& path)
+ static std::unique_ptr<BerkeleyDatabase> Create(const fs::path& path)
{
- return MakeUnique<CWalletDBWrapper>(path);
+ return MakeUnique<BerkeleyDatabase>(path);
}
/** Return object for accessing dummy database with no read/write capabilities. */
- static std::unique_ptr<CWalletDBWrapper> CreateDummy()
+ static std::unique_ptr<BerkeleyDatabase> CreateDummy()
{
- return MakeUnique<CWalletDBWrapper>();
+ return MakeUnique<BerkeleyDatabase>();
}
/** Return object for accessing temporary in-memory database. */
- static std::unique_ptr<CWalletDBWrapper> CreateMock()
+ static std::unique_ptr<BerkeleyDatabase> CreateMock()
{
- return MakeUnique<CWalletDBWrapper>("", true /* mock */);
+ return MakeUnique<BerkeleyDatabase>("", true /* mock */);
}
/** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero
@@ -152,7 +152,7 @@ public:
private:
/** BerkeleyDB specific */
- CDBEnv *env;
+ BerkeleyEnvironment *env;
std::string strFile;
/** Return whether this database handle is a dummy for testing.
@@ -164,7 +164,7 @@ private:
/** RAII class that provides access to a Berkeley database */
-class CDB
+class BerkeleyBatch
{
protected:
Db* pdb;
@@ -172,14 +172,14 @@ protected:
DbTxn* activeTxn;
bool fReadOnly;
bool fFlushOnClose;
- CDBEnv *env;
+ BerkeleyEnvironment *env;
public:
- explicit CDB(CWalletDBWrapper& dbw, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
- ~CDB() { Close(); }
+ explicit BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
+ ~BerkeleyBatch() { Close(); }
- CDB(const CDB&) = delete;
- CDB& operator=(const CDB&) = delete;
+ BerkeleyBatch(const BerkeleyBatch&) = delete;
+ BerkeleyBatch& operator=(const BerkeleyBatch&) = delete;
void Flush();
void Close();
@@ -187,11 +187,11 @@ public:
/* flush the wallet passively (TRY_LOCK)
ideal to be called periodically */
- static bool PeriodicFlush(CWalletDBWrapper& dbw);
+ static bool PeriodicFlush(BerkeleyDatabase& database);
/* verifies the database environment */
static bool VerifyEnvironment(const fs::path& file_path, std::string& errorStr);
/* verifies the database file */
- static bool VerifyDatabaseFile(const fs::path& file_path, std::string& warningStr, std::string& errorStr, CDBEnv::recoverFunc_type recoverFunc);
+ static bool VerifyDatabaseFile(const fs::path& file_path, std::string& warningStr, std::string& errorStr, BerkeleyEnvironment::recoverFunc_type recoverFunc);
public:
template <typename K, typename T>
@@ -387,7 +387,7 @@ public:
return Write(std::string("version"), nVersion);
}
- bool static Rewrite(CWalletDBWrapper& dbw, const char* pszSkip = nullptr);
+ bool static Rewrite(BerkeleyDatabase& database, const char* pszSkip = nullptr);
};
#endif // BITCOIN_WALLET_DB_H
diff --git a/src/wallet/feebumper.cpp b/src/wallet/feebumper.cpp
index 82a5017de0..5c34b39ec8 100644
--- a/src/wallet/feebumper.cpp
+++ b/src/wallet/feebumper.cpp
@@ -194,7 +194,7 @@ Result CreateTransaction(const CWallet* wallet, const uint256& txid, const CCoin
// If the output would become dust, discard it (converting the dust to fee)
poutput->nValue -= nDelta;
- if (poutput->nValue <= GetDustThreshold(*poutput, ::dustRelayFee)) {
+ if (poutput->nValue <= GetDustThreshold(*poutput, GetDiscardRate(::feeEstimator))) {
LogPrint(BCLog::RPC, "Bumping fee and discarding dust output\n");
new_fee += poutput->nValue;
mtx.vout.erase(mtx.vout.begin() + nOutput);
diff --git a/src/wallet/init.cpp b/src/wallet/init.cpp
index c860eede05..b6f4a0e1e1 100644
--- a/src/wallet/init.cpp
+++ b/src/wallet/init.cpp
@@ -277,21 +277,21 @@ bool WalletInit::Verify()
}
std::string strError;
- if (!CWalletDB::VerifyEnvironment(wallet_path, strError)) {
+ if (!WalletBatch::VerifyEnvironment(wallet_path, strError)) {
return InitError(strError);
}
if (gArgs.GetBoolArg("-salvagewallet", false)) {
// Recover readable keypairs:
- CWallet dummyWallet("dummy", CWalletDBWrapper::CreateDummy());
+ CWallet dummyWallet("dummy", WalletDatabase::CreateDummy());
std::string backup_filename;
- if (!CWalletDB::Recover(wallet_path, (void *)&dummyWallet, CWalletDB::RecoverKeysOnlyFilter, backup_filename)) {
+ if (!WalletBatch::Recover(wallet_path, (void *)&dummyWallet, WalletBatch::RecoverKeysOnlyFilter, backup_filename)) {
return false;
}
}
std::string strWarning;
- bool dbV = CWalletDB::VerifyDatabaseFile(wallet_path, strWarning, strError);
+ bool dbV = WalletBatch::VerifyDatabaseFile(wallet_path, strWarning, strError);
if (!strWarning.empty()) {
InitWarning(strWarning);
}
diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp
index 29760a7092..6212ea7512 100644
--- a/src/wallet/rpcwallet.cpp
+++ b/src/wallet/rpcwallet.cpp
@@ -189,7 +189,6 @@ UniValue getnewaddress(const JSONRPCRequest& request)
return EncodeDestination(dest);
}
-
CTxDestination GetLabelDestination(CWallet* const pwallet, const std::string& label, bool bForceNew=false)
{
CTxDestination dest;
@@ -207,14 +206,16 @@ UniValue getlabeladdress(const JSONRPCRequest& request)
return NullUniValue;
}
- if (request.fHelp || request.params.size() != 1)
+ if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
throw std::runtime_error(
- "getlabeladdress \"label\"\n"
- "\nReturns the current Bitcoin address for receiving payments to this label.\n"
+ "getlabeladdress \"label\" ( force ) \n"
+ "\nReturns the default receiving address for this label. This will reset to a fresh address once there's a transaction that spends to it.\n"
"\nArguments:\n"
- "1. \"label\" (string, required) The label name for the address. It can also be set to the empty string \"\" to represent the default label. The label does not need to exist, it will be created and a new address created if there is no label by the given name.\n"
+ "1. \"label\" (string, required) The label for the address. It can also be set to the empty string \"\" to represent the default label.\n"
+ "2. \"force\" (bool, optional) Whether the label should be created if it does not yet exist. If False, the RPC will return an error if called with a label that doesn't exist.\n"
+ " Defaults to false (unless the getaccountaddress method alias is being called, in which case defaults to true for backwards compatibility).\n"
"\nResult:\n"
- "\"address\" (string) The label bitcoin address\n"
+ "\"address\" (string) The current receiving address for the label.\n"
"\nExamples:\n"
+ HelpExampleCli("getlabeladdress", "")
+ HelpExampleCli("getlabeladdress", "\"\"")
@@ -226,6 +227,21 @@ UniValue getlabeladdress(const JSONRPCRequest& request)
// Parse the label first so we don't generate a key if there's an error
std::string label = LabelFromValue(request.params[0]);
+ bool force = request.strMethod == "getaccountaddress" ? true : false;
+ if (!request.params[1].isNull()) {
+ force = request.params[1].get_bool();
+ }
+
+ bool label_found = false;
+ for (const std::pair<CTxDestination, CAddressBookData>& item : pwallet->mapAddressBook) {
+ if (item.second.name == label) {
+ label_found = true;
+ break;
+ }
+ }
+ if (!force && !label_found) {
+ throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, std::string("No addresses with label " + label));
+ }
UniValue ret(UniValue::VSTR);
@@ -290,13 +306,13 @@ UniValue setlabel(const JSONRPCRequest& request)
return NullUniValue;
}
- if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
+ if (request.fHelp || request.params.size() != 2)
throw std::runtime_error(
"setlabel \"address\" \"label\"\n"
"\nSets the label associated with the given address.\n"
"\nArguments:\n"
"1. \"address\" (string, required) The bitcoin address to be associated with a label.\n"
- "2. \"label\" (string, required) The label to assign the address to.\n"
+ "2. \"label\" (string, required) The label to assign to the address.\n"
"\nExamples:\n"
+ HelpExampleCli("setlabel", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"tabby\"")
+ HelpExampleRpc("setlabel", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"tabby\"")
@@ -309,23 +325,22 @@ UniValue setlabel(const JSONRPCRequest& request)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
}
- std::string label;
- if (!request.params[1].isNull())
- label = LabelFromValue(request.params[1]);
+ std::string label = LabelFromValue(request.params[1]);
- // Only add the label if the address is yours.
if (IsMine(*pwallet, dest)) {
- // Detect when changing the label of an address that is the 'unused current key' of another label:
+ // Detect when changing the label of an address that is the receiving address of another label:
+ // If so, delete the account record for it. Labels, unlike addresses, can be deleted,
+ // and if we wouldn't do this, the record would stick around forever.
if (pwallet->mapAddressBook.count(dest)) {
std::string old_label = pwallet->mapAddressBook[dest].name;
- if (dest == GetLabelDestination(pwallet, old_label)) {
- GetLabelDestination(pwallet, old_label, true);
+ if (old_label != label && dest == GetLabelDestination(pwallet, old_label)) {
+ pwallet->DeleteLabel(old_label);
}
}
pwallet->SetAddressBook(dest, label, "receive");
+ } else {
+ pwallet->SetAddressBook(dest, label, "send");
}
- else
- throw JSONRPCError(RPC_MISC_ERROR, "setlabel can only be used with own address");
return NullUniValue;
}
@@ -3720,6 +3735,17 @@ UniValue DescribeWalletAddress(CWallet* pwallet, const CTxDestination& dest)
return ret;
}
+/** Convert CAddressBookData to JSON record. */
+static UniValue AddressBookDataToJSON(const CAddressBookData& data, const bool verbose)
+{
+ UniValue ret(UniValue::VOBJ);
+ if (verbose) {
+ ret.pushKV("name", data.name);
+ }
+ ret.pushKV("purpose", data.purpose);
+ return ret;
+}
+
UniValue getaddressinfo(const JSONRPCRequest& request)
{
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
@@ -3759,6 +3785,13 @@ UniValue getaddressinfo(const JSONRPCRequest& request)
" \"timestamp\" : timestamp, (number, optional) The creation time of the key if available in seconds since epoch (Jan 1 1970 GMT)\n"
" \"hdkeypath\" : \"keypath\" (string, optional) The HD keypath if the key is HD and available\n"
" \"hdmasterkeyid\" : \"<hash160>\" (string, optional) The Hash160 of the HD master pubkey\n"
+ " \"labels\" (object) Array of labels associated with the address.\n"
+ " [\n"
+ " { (json object of label data)\n"
+ " \"name\": \"labelname\" (string) The label\n"
+ " \"purpose\": \"string\" (string) Purpose of address (\"send\" for sending address, \"receive\" for receiving address)\n"
+ " },...\n"
+ " ]\n"
"}\n"
"\nExamples:\n"
+ HelpExampleCli("getaddressinfo", "\"1PSSGeFHDnKNxiEyFrD1wcEaHr9hrQDDWc\"")
@@ -3811,6 +3844,112 @@ UniValue getaddressinfo(const JSONRPCRequest& request)
ret.pushKV("hdmasterkeyid", meta->hdMasterKeyID.GetHex());
}
}
+
+ // Currently only one label can be associated with an address, return an array
+ // so the API remains stable if we allow multiple labels to be associated with
+ // an address.
+ UniValue labels(UniValue::VARR);
+ std::map<CTxDestination, CAddressBookData>::iterator mi = pwallet->mapAddressBook.find(dest);
+ if (mi != pwallet->mapAddressBook.end()) {
+ labels.push_back(AddressBookDataToJSON(mi->second, true));
+ }
+ ret.pushKV("labels", std::move(labels));
+
+ return ret;
+}
+
+UniValue getaddressesbylabel(const JSONRPCRequest& request)
+{
+ CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
+ if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
+ return NullUniValue;
+ }
+
+ if (request.fHelp || request.params.size() != 1)
+ throw std::runtime_error(
+ "getaddressesbylabel \"label\"\n"
+ "\nReturns the list of addresses assigned the specified label.\n"
+ "\nArguments:\n"
+ "1. \"label\" (string, required) The label.\n"
+ "\nResult:\n"
+ "{ (json object with addresses as keys)\n"
+ " \"address\": { (json object with information about address)\n"
+ " \"purpose\": \"string\" (string) Purpose of address (\"send\" for sending address, \"receive\" for receiving address)\n"
+ " },...\n"
+ "}\n"
+ "\nExamples:\n"
+ + HelpExampleCli("getaddressesbylabel", "\"tabby\"")
+ + HelpExampleRpc("getaddressesbylabel", "\"tabby\"")
+ );
+
+ LOCK(pwallet->cs_wallet);
+
+ std::string label = LabelFromValue(request.params[0]);
+
+ // Find all addresses that have the given label
+ UniValue ret(UniValue::VOBJ);
+ for (const std::pair<CTxDestination, CAddressBookData>& item : pwallet->mapAddressBook) {
+ if (item.second.name == label) {
+ ret.pushKV(EncodeDestination(item.first), AddressBookDataToJSON(item.second, false));
+ }
+ }
+
+ if (ret.empty()) {
+ throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, std::string("No addresses with label " + label));
+ }
+
+ return ret;
+}
+
+UniValue listlabels(const JSONRPCRequest& request)
+{
+ CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
+ if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
+ return NullUniValue;
+ }
+
+ if (request.fHelp || request.params.size() > 1)
+ throw std::runtime_error(
+ "listlabels ( \"purpose\" )\n"
+ "\nReturns the list of all labels, or labels that are assigned to addresses with a specific purpose.\n"
+ "\nArguments:\n"
+ "1. \"purpose\" (string, optional) Address purpose to list labels for ('send','receive'). An empty string is the same as not providing this argument.\n"
+ "\nResult:\n"
+ "[ (json array of string)\n"
+ " \"label\", (string) Label name\n"
+ " ...\n"
+ "]\n"
+ "\nExamples:\n"
+ "\nList all labels\n"
+ + HelpExampleCli("listlabels", "") +
+ "\nList labels that have receiving addresses\n"
+ + HelpExampleCli("listlabels", "receive") +
+ "\nList labels that have sending addresses\n"
+ + HelpExampleCli("listlabels", "send") +
+ "\nAs json rpc call\n"
+ + HelpExampleRpc("listlabels", "receive")
+ );
+
+ LOCK(pwallet->cs_wallet);
+
+ std::string purpose;
+ if (!request.params[0].isNull()) {
+ purpose = request.params[0].get_str();
+ }
+
+ // Add to a set to sort by label name, then insert into Univalue array
+ std::set<std::string> label_set;
+ for (const std::pair<CTxDestination, CAddressBookData>& entry : pwallet->mapAddressBook) {
+ if (purpose.empty() || entry.second.purpose == purpose) {
+ label_set.insert(entry.second.name);
+ }
+ }
+
+ UniValue ret(UniValue::VARR);
+ for (const std::string& name : label_set) {
+ ret.push_back(name);
+ }
+
return ret;
}
@@ -3840,16 +3979,10 @@ static const CRPCCommand commands[] =
{ "wallet", "dumpprivkey", &dumpprivkey, {"address"} },
{ "wallet", "dumpwallet", &dumpwallet, {"filename"} },
{ "wallet", "encryptwallet", &encryptwallet, {"passphrase"} },
- { "wallet", "getlabeladdress", &getlabeladdress, {"label"} },
- { "wallet", "getaccountaddress", &getlabeladdress, {"account"} },
- { "wallet", "getaccount", &getaccount, {"address"} },
- { "wallet", "getaddressesbyaccount", &getaddressesbyaccount, {"account"} },
{ "wallet", "getaddressinfo", &getaddressinfo, {"address"} },
{ "wallet", "getbalance", &getbalance, {"account","minconf","include_watchonly"} },
{ "wallet", "getnewaddress", &getnewaddress, {"label|account","address_type"} },
{ "wallet", "getrawchangeaddress", &getrawchangeaddress, {"address_type"} },
- { "wallet", "getreceivedbylabel", &getreceivedbylabel, {"label","minconf"} },
- { "wallet", "getreceivedbyaccount", &getreceivedbylabel, {"account","minconf"} },
{ "wallet", "getreceivedbyaddress", &getreceivedbyaddress, {"address","minconf"} },
{ "wallet", "gettransaction", &gettransaction, {"txid","include_watchonly"} },
{ "wallet", "getunconfirmedbalance", &getunconfirmedbalance, {} },
@@ -3861,7 +3994,6 @@ static const CRPCCommand commands[] =
{ "wallet", "importprunedfunds", &importprunedfunds, {"rawtransaction","txoutproof"} },
{ "wallet", "importpubkey", &importpubkey, {"pubkey","label","rescan"} },
{ "wallet", "keypoolrefill", &keypoolrefill, {"newsize"} },
- { "wallet", "listaccounts", &listaccounts, {"minconf","include_watchonly"} },
{ "wallet", "listaddressgroupings", &listaddressgroupings, {} },
{ "wallet", "listlockunspent", &listlockunspent, {} },
{ "wallet", "listreceivedbylabel", &listreceivedbylabel, {"minconf","include_empty","include_watchonly"} },
@@ -3872,12 +4004,9 @@ static const CRPCCommand commands[] =
{ "wallet", "listunspent", &listunspent, {"minconf","maxconf","addresses","include_unsafe","query_options"} },
{ "wallet", "listwallets", &listwallets, {} },
{ "wallet", "lockunspent", &lockunspent, {"unlock","transactions"} },
- { "wallet", "move", &movecmd, {"fromaccount","toaccount","amount","minconf","comment"} },
{ "wallet", "sendfrom", &sendfrom, {"fromaccount","toaddress","amount","minconf","comment","comment_to"} },
{ "wallet", "sendmany", &sendmany, {"fromaccount","amounts","minconf","comment","subtractfeefrom","replaceable","conf_target","estimate_mode"} },
{ "wallet", "sendtoaddress", &sendtoaddress, {"address","amount","comment","comment_to","subtractfeefromamount","replaceable","conf_target","estimate_mode"} },
- { "wallet", "setlabel", &setlabel, {"address","label"} },
- { "wallet", "setaccount", &setlabel, {"address","account"} },
{ "wallet", "settxfee", &settxfee, {"amount"} },
{ "wallet", "signmessage", &signmessage, {"address","message"} },
{ "wallet", "signrawtransactionwithwallet", &signrawtransactionwithwallet, {"hexstring","prevtxs","sighashtype"} },
@@ -3887,6 +4016,24 @@ static const CRPCCommand commands[] =
{ "wallet", "removeprunedfunds", &removeprunedfunds, {"txid"} },
{ "wallet", "rescanblockchain", &rescanblockchain, {"start_height", "stop_height"} },
+ /** Account functions (deprecated) */
+ { "wallet", "getaccountaddress", &getlabeladdress, {"account"} },
+ { "wallet", "getaccount", &getaccount, {"address"} },
+ { "wallet", "getaddressesbyaccount", &getaddressesbyaccount, {"account"} },
+ { "wallet", "getreceivedbyaccount", &getreceivedbylabel, {"account","minconf"} },
+ { "wallet", "listaccounts", &listaccounts, {"minconf","include_watchonly"} },
+ { "wallet", "listreceivedbyaccount", &listreceivedbylabel, {"minconf","include_empty","include_watchonly"} },
+ { "wallet", "setaccount", &setlabel, {"address","account"} },
+ { "wallet", "move", &movecmd, {"fromaccount","toaccount","amount","minconf","comment"} },
+
+ /** Label functions (to replace non-balance account functions) */
+ { "wallet", "getlabeladdress", &getlabeladdress, {"label","force"} },
+ { "wallet", "getaddressesbylabel", &getaddressesbylabel, {"label"} },
+ { "wallet", "getreceivedbylabel", &getreceivedbylabel, {"label","minconf"} },
+ { "wallet", "listlabels", &listlabels, {"purpose"} },
+ { "wallet", "listreceivedbylabel", &listreceivedbylabel, {"minconf","include_empty","include_watchonly"} },
+ { "wallet", "setlabel", &setlabel, {"address","label"} },
+
{ "generating", "generate", &generate, {"nblocks","maxtries"} },
};
diff --git a/src/wallet/test/coinselector_tests.cpp b/src/wallet/test/coinselector_tests.cpp
index 184a8a3f1f..ac47d4448a 100644
--- a/src/wallet/test/coinselector_tests.cpp
+++ b/src/wallet/test/coinselector_tests.cpp
@@ -28,7 +28,7 @@ std::vector<std::unique_ptr<CWalletTx>> wtxn;
typedef std::set<CInputCoin> CoinSet;
static std::vector<COutput> vCoins;
-static CWallet testWallet("dummy", CWalletDBWrapper::CreateDummy());
+static CWallet testWallet("dummy", WalletDatabase::CreateDummy());
static CAmount balance = 0;
CoinEligibilityFilter filter_standard(1, 6, 0);
diff --git a/src/wallet/test/wallet_test_fixture.cpp b/src/wallet/test/wallet_test_fixture.cpp
index 5c550742c8..6129e337ce 100644
--- a/src/wallet/test/wallet_test_fixture.cpp
+++ b/src/wallet/test/wallet_test_fixture.cpp
@@ -6,10 +6,9 @@
#include <rpc/server.h>
#include <wallet/db.h>
-#include <wallet/wallet.h>
WalletTestingSetup::WalletTestingSetup(const std::string& chainName):
- TestingSetup(chainName), m_wallet("mock", CWalletDBWrapper::CreateMock())
+ TestingSetup(chainName), m_wallet("mock", WalletDatabase::CreateMock())
{
bool fFirstRun;
m_wallet.LoadWallet(fFirstRun);
diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp
index 4729b7c319..14c5ad7214 100644
--- a/src/wallet/test/wallet_tests.cpp
+++ b/src/wallet/test/wallet_tests.cpp
@@ -46,7 +46,7 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
// Verify ScanForWalletTransactions picks up transactions in both the old
// and new block files.
{
- CWallet wallet("dummy", CWalletDBWrapper::CreateDummy());
+ CWallet wallet("dummy", WalletDatabase::CreateDummy());
AddKey(wallet, coinbaseKey);
WalletRescanReserver reserver(&wallet);
reserver.reserve();
@@ -61,7 +61,7 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
// Verify ScanForWalletTransactions only picks transactions in the new block
// file.
{
- CWallet wallet("dummy", CWalletDBWrapper::CreateDummy());
+ CWallet wallet("dummy", WalletDatabase::CreateDummy());
AddKey(wallet, coinbaseKey);
WalletRescanReserver reserver(&wallet);
reserver.reserve();
@@ -73,7 +73,7 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
// before the missing block, and success for a key whose creation time is
// after.
{
- CWallet wallet("dummy", CWalletDBWrapper::CreateDummy());
+ CWallet wallet("dummy", WalletDatabase::CreateDummy());
vpwallets.insert(vpwallets.begin(), &wallet);
UniValue keys;
keys.setArray();
@@ -132,7 +132,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
// Import key into wallet and call dumpwallet to create backup file.
{
- CWallet wallet("dummy", CWalletDBWrapper::CreateDummy());
+ CWallet wallet("dummy", WalletDatabase::CreateDummy());
LOCK(wallet.cs_wallet);
wallet.mapKeyMetadata[coinbaseKey.GetPubKey().GetID()].nCreateTime = KEY_TIME;
wallet.AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());
@@ -147,7 +147,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
// Call importwallet RPC and verify all blocks with timestamps >= BLOCK_TIME
// were scanned, and no prior blocks were scanned.
{
- CWallet wallet("dummy", CWalletDBWrapper::CreateDummy());
+ CWallet wallet("dummy", WalletDatabase::CreateDummy());
JSONRPCRequest request;
request.params.setArray();
@@ -177,7 +177,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
// debit functions.
BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup)
{
- CWallet wallet("dummy", CWalletDBWrapper::CreateDummy());
+ CWallet wallet("dummy", WalletDatabase::CreateDummy());
CWalletTx wtx(&wallet, MakeTransactionRef(coinbaseTxns.back()));
LOCK2(cs_main, wallet.cs_wallet);
wtx.hashBlock = chainActive.Tip()->GetBlockHash();
@@ -270,7 +270,7 @@ public:
ListCoinsTestingSetup()
{
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
- wallet = MakeUnique<CWallet>("mock", CWalletDBWrapper::CreateMock());
+ wallet = MakeUnique<CWallet>("mock", WalletDatabase::CreateMock());
bool firstRun;
wallet->LoadWallet(firstRun);
AddKey(*wallet, coinbaseKey);
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
index 894dfb035c..673d91c613 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -8,7 +8,6 @@
#include <checkpoints.h>
#include <chain.h>
#include <wallet/coincontrol.h>
-#include <wallet/coinselection.h>
#include <consensus/consensus.h>
#include <consensus/validation.h>
#include <fs.h>
@@ -26,7 +25,6 @@
#include <scheduler.h>
#include <timedata.h>
#include <txmempool.h>
-#include <util.h>
#include <utilmoneystr.h>
#include <wallet/fees.h>
@@ -131,7 +129,7 @@ const CWalletTx* CWallet::GetWalletTx(const uint256& hash) const
return &(it->second);
}
-CPubKey CWallet::GenerateNewKey(CWalletDB &walletdb, bool internal)
+CPubKey CWallet::GenerateNewKey(WalletBatch &batch, bool internal)
{
AssertLockHeld(cs_wallet); // mapKeyMetadata
bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets
@@ -144,7 +142,7 @@ CPubKey CWallet::GenerateNewKey(CWalletDB &walletdb, bool internal)
// use HD key derivation if HD was enabled during wallet creation
if (IsHDEnabled()) {
- DeriveNewChildKey(walletdb, metadata, secret, (CanSupportFeature(FEATURE_HD_SPLIT) ? internal : false));
+ DeriveNewChildKey(batch, metadata, secret, (CanSupportFeature(FEATURE_HD_SPLIT) ? internal : false));
} else {
secret.MakeNewKey(fCompressed);
}
@@ -160,13 +158,13 @@ CPubKey CWallet::GenerateNewKey(CWalletDB &walletdb, bool internal)
mapKeyMetadata[pubkey.GetID()] = metadata;
UpdateTimeFirstKey(nCreationTime);
- if (!AddKeyPubKeyWithDB(walletdb, secret, pubkey)) {
+ if (!AddKeyPubKeyWithDB(batch, secret, pubkey)) {
throw std::runtime_error(std::string(__func__) + ": AddKey failed");
}
return pubkey;
}
-void CWallet::DeriveNewChildKey(CWalletDB &walletdb, CKeyMetadata& metadata, CKey& secret, bool internal)
+void CWallet::DeriveNewChildKey(WalletBatch &batch, CKeyMetadata& metadata, CKey& secret, bool internal)
{
// for now we use a fixed keypath scheme of m/0'/0'/k
CKey key; //master key seed (256bit)
@@ -208,26 +206,26 @@ void CWallet::DeriveNewChildKey(CWalletDB &walletdb, CKeyMetadata& metadata, CKe
secret = childKey.key;
metadata.hdMasterKeyID = hdChain.masterKeyID;
// update the chain model in the database
- if (!walletdb.WriteHDChain(hdChain))
+ if (!batch.WriteHDChain(hdChain))
throw std::runtime_error(std::string(__func__) + ": Writing HD chain model failed");
}
-bool CWallet::AddKeyPubKeyWithDB(CWalletDB &walletdb, const CKey& secret, const CPubKey &pubkey)
+bool CWallet::AddKeyPubKeyWithDB(WalletBatch &batch, const CKey& secret, const CPubKey &pubkey)
{
AssertLockHeld(cs_wallet); // mapKeyMetadata
// CCryptoKeyStore has no concept of wallet databases, but calls AddCryptedKey
// which is overridden below. To avoid flushes, the database handle is
// tunneled through to it.
- bool needsDB = !pwalletdbEncryption;
+ bool needsDB = !encrypted_batch;
if (needsDB) {
- pwalletdbEncryption = &walletdb;
+ encrypted_batch = &batch;
}
if (!CCryptoKeyStore::AddKeyPubKey(secret, pubkey)) {
- if (needsDB) pwalletdbEncryption = nullptr;
+ if (needsDB) encrypted_batch = nullptr;
return false;
}
- if (needsDB) pwalletdbEncryption = nullptr;
+ if (needsDB) encrypted_batch = nullptr;
// check if we need to remove from watch-only
CScript script;
@@ -241,7 +239,7 @@ bool CWallet::AddKeyPubKeyWithDB(CWalletDB &walletdb, const CKey& secret, const
}
if (!IsCrypted()) {
- return walletdb.WriteKey(pubkey,
+ return batch.WriteKey(pubkey,
secret.GetPrivKey(),
mapKeyMetadata[pubkey.GetID()]);
}
@@ -250,8 +248,8 @@ bool CWallet::AddKeyPubKeyWithDB(CWalletDB &walletdb, const CKey& secret, const
bool CWallet::AddKeyPubKey(const CKey& secret, const CPubKey &pubkey)
{
- CWalletDB walletdb(*dbw);
- return CWallet::AddKeyPubKeyWithDB(walletdb, secret, pubkey);
+ WalletBatch batch(*database);
+ return CWallet::AddKeyPubKeyWithDB(batch, secret, pubkey);
}
bool CWallet::AddCryptedKey(const CPubKey &vchPubKey,
@@ -261,12 +259,12 @@ bool CWallet::AddCryptedKey(const CPubKey &vchPubKey,
return false;
{
LOCK(cs_wallet);
- if (pwalletdbEncryption)
- return pwalletdbEncryption->WriteCryptedKey(vchPubKey,
+ if (encrypted_batch)
+ return encrypted_batch->WriteCryptedKey(vchPubKey,
vchCryptedSecret,
mapKeyMetadata[vchPubKey.GetID()]);
else
- return CWalletDB(*dbw).WriteCryptedKey(vchPubKey,
+ return WalletBatch(*database).WriteCryptedKey(vchPubKey,
vchCryptedSecret,
mapKeyMetadata[vchPubKey.GetID()]);
}
@@ -313,7 +311,7 @@ bool CWallet::AddCScript(const CScript& redeemScript)
{
if (!CCryptoKeyStore::AddCScript(redeemScript))
return false;
- return CWalletDB(*dbw).WriteCScript(Hash160(redeemScript), redeemScript);
+ return WalletBatch(*database).WriteCScript(Hash160(redeemScript), redeemScript);
}
bool CWallet::LoadCScript(const CScript& redeemScript)
@@ -339,7 +337,7 @@ bool CWallet::AddWatchOnly(const CScript& dest)
const CKeyMetadata& meta = m_script_metadata[CScriptID(dest)];
UpdateTimeFirstKey(meta.nCreateTime);
NotifyWatchonlyChanged(true);
- return CWalletDB(*dbw).WriteWatchOnly(dest, meta);
+ return WalletBatch(*database).WriteWatchOnly(dest, meta);
}
bool CWallet::AddWatchOnly(const CScript& dest, int64_t nCreateTime)
@@ -355,7 +353,7 @@ bool CWallet::RemoveWatchOnly(const CScript &dest)
return false;
if (!HaveWatchOnly())
NotifyWatchonlyChanged(false);
- if (!CWalletDB(*dbw).EraseWatchOnly(dest))
+ if (!WalletBatch(*database).EraseWatchOnly(dest))
return false;
return true;
@@ -421,7 +419,7 @@ bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase,
return false;
if (!crypter.Encrypt(_vMasterKey, pMasterKey.second.vchCryptedKey))
return false;
- CWalletDB(*dbw).WriteMasterKey(pMasterKey.first, pMasterKey.second);
+ WalletBatch(*database).WriteMasterKey(pMasterKey.first, pMasterKey.second);
if (fWasLocked)
Lock();
return true;
@@ -434,11 +432,11 @@ bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase,
void CWallet::SetBestChain(const CBlockLocator& loc)
{
- CWalletDB walletdb(*dbw);
- walletdb.WriteBestBlock(loc);
+ WalletBatch batch(*database);
+ batch.WriteBestBlock(loc);
}
-bool CWallet::SetMinVersion(enum WalletFeature nVersion, CWalletDB* pwalletdbIn, bool fExplicit)
+bool CWallet::SetMinVersion(enum WalletFeature nVersion, WalletBatch* batch_in, bool fExplicit)
{
LOCK(cs_wallet); // nWalletVersion
if (nWalletVersion >= nVersion)
@@ -454,11 +452,11 @@ bool CWallet::SetMinVersion(enum WalletFeature nVersion, CWalletDB* pwalletdbIn,
nWalletMaxVersion = nVersion;
{
- CWalletDB* pwalletdb = pwalletdbIn ? pwalletdbIn : new CWalletDB(*dbw);
+ WalletBatch* batch = batch_in ? batch_in : new WalletBatch(*database);
if (nWalletVersion > 40000)
- pwalletdb->WriteMinVersion(nWalletVersion);
- if (!pwalletdbIn)
- delete pwalletdb;
+ batch->WriteMinVersion(nWalletVersion);
+ if (!batch_in)
+ delete batch;
}
return true;
@@ -508,7 +506,7 @@ bool CWallet::HasWalletSpend(const uint256& txid) const
void CWallet::Flush(bool shutdown)
{
- dbw->Flush(shutdown);
+ database->Flush(shutdown);
}
void CWallet::SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator> range)
@@ -631,36 +629,36 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
{
LOCK(cs_wallet);
mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
- assert(!pwalletdbEncryption);
- pwalletdbEncryption = new CWalletDB(*dbw);
- if (!pwalletdbEncryption->TxnBegin()) {
- delete pwalletdbEncryption;
- pwalletdbEncryption = nullptr;
+ assert(!encrypted_batch);
+ encrypted_batch = new WalletBatch(*database);
+ if (!encrypted_batch->TxnBegin()) {
+ delete encrypted_batch;
+ encrypted_batch = nullptr;
return false;
}
- pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
+ encrypted_batch->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
if (!EncryptKeys(_vMasterKey))
{
- pwalletdbEncryption->TxnAbort();
- delete pwalletdbEncryption;
+ encrypted_batch->TxnAbort();
+ delete encrypted_batch;
// We now probably have half of our keys encrypted in memory, and half not...
// die and let the user reload the unencrypted wallet.
assert(false);
}
// Encryption was introduced in version 0.4.0
- SetMinVersion(FEATURE_WALLETCRYPT, pwalletdbEncryption, true);
+ SetMinVersion(FEATURE_WALLETCRYPT, encrypted_batch, true);
- if (!pwalletdbEncryption->TxnCommit()) {
- delete pwalletdbEncryption;
+ if (!encrypted_batch->TxnCommit()) {
+ delete encrypted_batch;
// We now have keys encrypted in memory, but not on disk...
// die to avoid confusion and let the user reload the unencrypted wallet.
assert(false);
}
- delete pwalletdbEncryption;
- pwalletdbEncryption = nullptr;
+ delete encrypted_batch;
+ encrypted_batch = nullptr;
Lock();
Unlock(strWalletPassphrase);
@@ -677,7 +675,7 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
// Need to completely rewrite the wallet file; if we don't, bdb might keep
// bits of the unencrypted private key in slack space in the database file.
- dbw->Rewrite();
+ database->Rewrite();
}
NotifyStatusChanged(this);
@@ -688,7 +686,7 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
DBErrors CWallet::ReorderTransactions()
{
LOCK(cs_wallet);
- CWalletDB walletdb(*dbw);
+ WalletBatch batch(*database);
// Old wallets didn't have any defined order for transactions
// Probably a bad idea to change the output of this
@@ -704,7 +702,7 @@ DBErrors CWallet::ReorderTransactions()
txByTime.insert(std::make_pair(wtx->nTimeReceived, TxPair(wtx, nullptr)));
}
std::list<CAccountingEntry> acentries;
- walletdb.ListAccountCreditDebit("", acentries);
+ batch.ListAccountCreditDebit("", acentries);
for (CAccountingEntry& entry : acentries)
{
txByTime.insert(std::make_pair(entry.nTime, TxPair(nullptr, &entry)));
@@ -725,11 +723,11 @@ DBErrors CWallet::ReorderTransactions()
if (pwtx)
{
- if (!walletdb.WriteTx(*pwtx))
+ if (!batch.WriteTx(*pwtx))
return DBErrors::LOAD_FAIL;
}
else
- if (!walletdb.WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
+ if (!batch.WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
return DBErrors::LOAD_FAIL;
}
else
@@ -749,60 +747,60 @@ DBErrors CWallet::ReorderTransactions()
// Since we're changing the order, write it back
if (pwtx)
{
- if (!walletdb.WriteTx(*pwtx))
+ if (!batch.WriteTx(*pwtx))
return DBErrors::LOAD_FAIL;
}
else
- if (!walletdb.WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
+ if (!batch.WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
return DBErrors::LOAD_FAIL;
}
}
- walletdb.WriteOrderPosNext(nOrderPosNext);
+ batch.WriteOrderPosNext(nOrderPosNext);
return DBErrors::LOAD_OK;
}
-int64_t CWallet::IncOrderPosNext(CWalletDB *pwalletdb)
+int64_t CWallet::IncOrderPosNext(WalletBatch *batch)
{
AssertLockHeld(cs_wallet); // nOrderPosNext
int64_t nRet = nOrderPosNext++;
- if (pwalletdb) {
- pwalletdb->WriteOrderPosNext(nOrderPosNext);
+ if (batch) {
+ batch->WriteOrderPosNext(nOrderPosNext);
} else {
- CWalletDB(*dbw).WriteOrderPosNext(nOrderPosNext);
+ WalletBatch(*database).WriteOrderPosNext(nOrderPosNext);
}
return nRet;
}
bool CWallet::AccountMove(std::string strFrom, std::string strTo, CAmount nAmount, std::string strComment)
{
- CWalletDB walletdb(*dbw);
- if (!walletdb.TxnBegin())
+ WalletBatch batch(*database);
+ if (!batch.TxnBegin())
return false;
int64_t nNow = GetAdjustedTime();
// Debit
CAccountingEntry debit;
- debit.nOrderPos = IncOrderPosNext(&walletdb);
+ debit.nOrderPos = IncOrderPosNext(&batch);
debit.strAccount = strFrom;
debit.nCreditDebit = -nAmount;
debit.nTime = nNow;
debit.strOtherAccount = strTo;
debit.strComment = strComment;
- AddAccountingEntry(debit, &walletdb);
+ AddAccountingEntry(debit, &batch);
// Credit
CAccountingEntry credit;
- credit.nOrderPos = IncOrderPosNext(&walletdb);
+ credit.nOrderPos = IncOrderPosNext(&batch);
credit.strAccount = strTo;
credit.nCreditDebit = nAmount;
credit.nTime = nNow;
credit.strOtherAccount = strFrom;
credit.strComment = strComment;
- AddAccountingEntry(credit, &walletdb);
+ AddAccountingEntry(credit, &batch);
- if (!walletdb.TxnCommit())
+ if (!batch.TxnCommit())
return false;
return true;
@@ -810,10 +808,10 @@ bool CWallet::AccountMove(std::string strFrom, std::string strTo, CAmount nAmoun
bool CWallet::GetLabelDestination(CTxDestination &dest, const std::string& label, bool bForceNew)
{
- CWalletDB walletdb(*dbw);
+ WalletBatch batch(*database);
CAccount account;
- walletdb.ReadAccount(label, account);
+ batch.ReadAccount(label, account);
if (!bForceNew) {
if (!account.vchPubKey.IsValid())
@@ -840,7 +838,7 @@ bool CWallet::GetLabelDestination(CTxDestination &dest, const std::string& label
LearnRelatedScripts(account.vchPubKey, m_default_address_type);
dest = GetDestinationForKey(account.vchPubKey, m_default_address_type);
SetAddressBook(dest, label, "receive");
- walletdb.WriteAccount(label, account);
+ batch.WriteAccount(label, account);
} else {
dest = GetDestinationForKey(account.vchPubKey, m_default_address_type);
}
@@ -873,11 +871,11 @@ bool CWallet::MarkReplaced(const uint256& originalHash, const uint256& newHash)
wtx.mapValue["replaced_by_txid"] = newHash.ToString();
- CWalletDB walletdb(*dbw, "r+");
+ WalletBatch batch(*database, "r+");
bool success = true;
- if (!walletdb.WriteTx(wtx)) {
- LogPrintf("%s: Updating walletdb tx %s failed\n", __func__, wtx.GetHash().ToString());
+ if (!batch.WriteTx(wtx)) {
+ LogPrintf("%s: Updating batch tx %s failed\n", __func__, wtx.GetHash().ToString());
success = false;
}
@@ -890,7 +888,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
{
LOCK(cs_wallet);
- CWalletDB walletdb(*dbw, "r+", fFlushOnClose);
+ WalletBatch batch(*database, "r+", fFlushOnClose);
uint256 hash = wtxIn.GetHash();
@@ -902,7 +900,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
if (fInsertedNew)
{
wtx.nTimeReceived = GetAdjustedTime();
- wtx.nOrderPos = IncOrderPosNext(&walletdb);
+ wtx.nOrderPos = IncOrderPosNext(&batch);
wtxOrdered.insert(std::make_pair(wtx.nOrderPos, TxPair(&wtx, nullptr)));
wtx.nTimeSmart = ComputeTimeSmart(wtx);
AddToSpends(hash);
@@ -949,7 +947,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
// Write to disk
if (fInsertedNew || fUpdated)
- if (!walletdb.WriteTx(wtx))
+ if (!batch.WriteTx(wtx))
return false;
// Break debit/credit balance caches:
@@ -1074,7 +1072,7 @@ bool CWallet::AbandonTransaction(const uint256& hashTx)
{
LOCK2(cs_main, cs_wallet);
- CWalletDB walletdb(*dbw, "r+");
+ WalletBatch batch(*database, "r+");
std::set<uint256> todo;
std::set<uint256> done;
@@ -1106,7 +1104,7 @@ bool CWallet::AbandonTransaction(const uint256& hashTx)
wtx.nIndex = -1;
wtx.setAbandoned();
wtx.MarkDirty();
- walletdb.WriteTx(wtx);
+ batch.WriteTx(wtx);
NotifyTransactionChanged(this, wtx.GetHash(), CT_UPDATED);
// Iterate over all its outputs, and mark transactions in the wallet that spend them abandoned too
TxSpends::const_iterator iter = mapTxSpends.lower_bound(COutPoint(hashTx, 0));
@@ -1148,7 +1146,7 @@ void CWallet::MarkConflicted(const uint256& hashBlock, const uint256& hashTx)
return;
// Do not flush the wallet here for performance reasons
- CWalletDB walletdb(*dbw, "r+", false);
+ WalletBatch batch(*database, "r+", false);
std::set<uint256> todo;
std::set<uint256> done;
@@ -1169,7 +1167,7 @@ void CWallet::MarkConflicted(const uint256& hashBlock, const uint256& hashTx)
wtx.nIndex = -1;
wtx.hashBlock = hashBlock;
wtx.MarkDirty();
- walletdb.WriteTx(wtx);
+ batch.WriteTx(wtx);
// Iterate over all its outputs, and mark transactions in the wallet that spend them conflicted too
TxSpends::const_iterator iter = mapTxSpends.lower_bound(COutPoint(now, 0));
while (iter != mapTxSpends.end() && iter->first.hash == now) {
@@ -1473,7 +1471,7 @@ bool CWallet::SetHDMasterKey(const CPubKey& pubkey)
bool CWallet::SetHDChain(const CHDChain& chain, bool memonly)
{
LOCK(cs_wallet);
- if (!memonly && !CWalletDB(*dbw).WriteHDChain(chain))
+ if (!memonly && !WalletBatch(*database).WriteHDChain(chain))
throw std::runtime_error(std::string(__func__) + ": writing chain failed");
hdChain = chain;
@@ -2231,7 +2229,7 @@ CAmount CWallet::GetLegacyBalance(const isminefilter& filter, int minDepth, cons
}
if (account) {
- balance += CWalletDB(*dbw).GetAccountCreditDebit(*account);
+ balance += WalletBatch(*database).GetAccountCreditDebit(*account);
}
return balance;
@@ -3115,20 +3113,20 @@ bool CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::ve
}
void CWallet::ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& entries) {
- CWalletDB walletdb(*dbw);
- return walletdb.ListAccountCreditDebit(strAccount, entries);
+ WalletBatch batch(*database);
+ return batch.ListAccountCreditDebit(strAccount, entries);
}
bool CWallet::AddAccountingEntry(const CAccountingEntry& acentry)
{
- CWalletDB walletdb(*dbw);
+ WalletBatch batch(*database);
- return AddAccountingEntry(acentry, &walletdb);
+ return AddAccountingEntry(acentry, &batch);
}
-bool CWallet::AddAccountingEntry(const CAccountingEntry& acentry, CWalletDB *pwalletdb)
+bool CWallet::AddAccountingEntry(const CAccountingEntry& acentry, WalletBatch *batch)
{
- if (!pwalletdb->WriteAccountingEntry(++nAccountingEntryNumber, acentry)) {
+ if (!batch->WriteAccountingEntry(++nAccountingEntryNumber, acentry)) {
return false;
}
@@ -3144,10 +3142,10 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
LOCK2(cs_main, cs_wallet);
fFirstRunRet = false;
- DBErrors nLoadWalletRet = CWalletDB(*dbw,"cr+").LoadWallet(this);
+ DBErrors nLoadWalletRet = WalletBatch(*database,"cr+").LoadWallet(this);
if (nLoadWalletRet == DBErrors::NEED_REWRITE)
{
- if (dbw->Rewrite("\x04pool"))
+ if (database->Rewrite("\x04pool"))
{
setInternalKeyPool.clear();
setExternalKeyPool.clear();
@@ -3172,13 +3170,13 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut)
{
AssertLockHeld(cs_wallet); // mapWallet
- DBErrors nZapSelectTxRet = CWalletDB(*dbw,"cr+").ZapSelectTx(vHashIn, vHashOut);
+ DBErrors nZapSelectTxRet = WalletBatch(*database,"cr+").ZapSelectTx(vHashIn, vHashOut);
for (uint256 hash : vHashOut)
mapWallet.erase(hash);
if (nZapSelectTxRet == DBErrors::NEED_REWRITE)
{
- if (dbw->Rewrite("\x04pool"))
+ if (database->Rewrite("\x04pool"))
{
setInternalKeyPool.clear();
setExternalKeyPool.clear();
@@ -3200,10 +3198,10 @@ DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256
DBErrors CWallet::ZapWalletTx(std::vector<CWalletTx>& vWtx)
{
- DBErrors nZapWalletTxRet = CWalletDB(*dbw,"cr+").ZapWalletTx(vWtx);
+ DBErrors nZapWalletTxRet = WalletBatch(*database,"cr+").ZapWalletTx(vWtx);
if (nZapWalletTxRet == DBErrors::NEED_REWRITE)
{
- if (dbw->Rewrite("\x04pool"))
+ if (database->Rewrite("\x04pool"))
{
LOCK(cs_wallet);
setInternalKeyPool.clear();
@@ -3235,9 +3233,9 @@ bool CWallet::SetAddressBook(const CTxDestination& address, const std::string& s
}
NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address) != ISMINE_NO,
strPurpose, (fUpdated ? CT_UPDATED : CT_NEW) );
- if (!strPurpose.empty() && !CWalletDB(*dbw).WritePurpose(EncodeDestination(address), strPurpose))
+ if (!strPurpose.empty() && !WalletBatch(*database).WritePurpose(EncodeDestination(address), strPurpose))
return false;
- return CWalletDB(*dbw).WriteName(EncodeDestination(address), strName);
+ return WalletBatch(*database).WriteName(EncodeDestination(address), strName);
}
bool CWallet::DelAddressBook(const CTxDestination& address)
@@ -3249,15 +3247,15 @@ bool CWallet::DelAddressBook(const CTxDestination& address)
std::string strAddress = EncodeDestination(address);
for (const std::pair<std::string, std::string> &item : mapAddressBook[address].destdata)
{
- CWalletDB(*dbw).EraseDestData(strAddress, item.first);
+ WalletBatch(*database).EraseDestData(strAddress, item.first);
}
mapAddressBook.erase(address);
}
NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address) != ISMINE_NO, "", CT_DELETED);
- CWalletDB(*dbw).ErasePurpose(EncodeDestination(address));
- return CWalletDB(*dbw).EraseName(EncodeDestination(address));
+ WalletBatch(*database).ErasePurpose(EncodeDestination(address));
+ return WalletBatch(*database).EraseName(EncodeDestination(address));
}
const std::string& CWallet::GetLabelName(const CScript& scriptPubKey) const
@@ -3283,15 +3281,15 @@ bool CWallet::NewKeyPool()
{
{
LOCK(cs_wallet);
- CWalletDB walletdb(*dbw);
+ WalletBatch batch(*database);
for (int64_t nIndex : setInternalKeyPool) {
- walletdb.ErasePool(nIndex);
+ batch.ErasePool(nIndex);
}
setInternalKeyPool.clear();
for (int64_t nIndex : setExternalKeyPool) {
- walletdb.ErasePool(nIndex);
+ batch.ErasePool(nIndex);
}
setExternalKeyPool.clear();
@@ -3356,7 +3354,7 @@ bool CWallet::TopUpKeyPool(unsigned int kpSize)
missingInternal = 0;
}
bool internal = false;
- CWalletDB walletdb(*dbw);
+ WalletBatch batch(*database);
for (int64_t i = missingInternal + missingExternal; i--;)
{
if (i < missingInternal) {
@@ -3366,8 +3364,8 @@ bool CWallet::TopUpKeyPool(unsigned int kpSize)
assert(m_max_keypool_index < std::numeric_limits<int64_t>::max()); // How in the hell did you use so many keys?
int64_t index = ++m_max_keypool_index;
- CPubKey pubkey(GenerateNewKey(walletdb, internal));
- if (!walletdb.WritePool(index, CKeyPool(pubkey, internal))) {
+ CPubKey pubkey(GenerateNewKey(batch, internal));
+ if (!batch.WritePool(index, CKeyPool(pubkey, internal))) {
throw std::runtime_error(std::string(__func__) + ": writing generated key failed");
}
@@ -3402,12 +3400,12 @@ void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, bool fRe
if(setKeyPool.empty())
return;
- CWalletDB walletdb(*dbw);
+ WalletBatch batch(*database);
auto it = setKeyPool.begin();
nIndex = *it;
setKeyPool.erase(it);
- if (!walletdb.ReadPool(nIndex, keypool)) {
+ if (!batch.ReadPool(nIndex, keypool)) {
throw std::runtime_error(std::string(__func__) + ": read failed");
}
if (!HaveKey(keypool.vchPubKey.GetID())) {
@@ -3426,8 +3424,8 @@ void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, bool fRe
void CWallet::KeepKey(int64_t nIndex)
{
// Remove from key pool
- CWalletDB walletdb(*dbw);
- walletdb.ErasePool(nIndex);
+ WalletBatch batch(*database);
+ batch.ErasePool(nIndex);
LogPrintf("keypool keep %d\n", nIndex);
}
@@ -3456,8 +3454,8 @@ bool CWallet::GetKeyFromPool(CPubKey& result, bool internal)
if (nIndex == -1)
{
if (IsLocked()) return false;
- CWalletDB walletdb(*dbw);
- result = GenerateNewKey(walletdb, internal);
+ WalletBatch batch(*database);
+ result = GenerateNewKey(batch, internal);
return true;
}
KeepKey(nIndex);
@@ -3466,14 +3464,14 @@ bool CWallet::GetKeyFromPool(CPubKey& result, bool internal)
return true;
}
-static int64_t GetOldestKeyTimeInPool(const std::set<int64_t>& setKeyPool, CWalletDB& walletdb) {
+static int64_t GetOldestKeyTimeInPool(const std::set<int64_t>& setKeyPool, WalletBatch& batch) {
if (setKeyPool.empty()) {
return GetTime();
}
CKeyPool keypool;
int64_t nIndex = *(setKeyPool.begin());
- if (!walletdb.ReadPool(nIndex, keypool)) {
+ if (!batch.ReadPool(nIndex, keypool)) {
throw std::runtime_error(std::string(__func__) + ": read oldest key in keypool failed");
}
assert(keypool.vchPubKey.IsValid());
@@ -3484,12 +3482,12 @@ int64_t CWallet::GetOldestKeyPoolTime()
{
LOCK(cs_wallet);
- CWalletDB walletdb(*dbw);
+ WalletBatch batch(*database);
// load oldest key from keypool, get time and return
- int64_t oldestKey = GetOldestKeyTimeInPool(setExternalKeyPool, walletdb);
+ int64_t oldestKey = GetOldestKeyTimeInPool(setExternalKeyPool, batch);
if (IsHDEnabled() && CanSupportFeature(FEATURE_HD_SPLIT)) {
- oldestKey = std::max(GetOldestKeyTimeInPool(setInternalKeyPool, walletdb), oldestKey);
+ oldestKey = std::max(GetOldestKeyTimeInPool(setInternalKeyPool, batch), oldestKey);
}
return oldestKey;
@@ -3642,6 +3640,12 @@ std::set<CTxDestination> CWallet::GetLabelAddresses(const std::string& label) co
return result;
}
+void CWallet::DeleteLabel(const std::string& label)
+{
+ WalletBatch batch(*database);
+ batch.EraseAccount(label);
+}
+
bool CReserveKey::GetReservedKey(CPubKey& pubkey, bool internal)
{
if (nIndex == -1)
@@ -3685,17 +3689,17 @@ void CWallet::MarkReserveKeysAsUsed(int64_t keypool_id)
std::set<int64_t> *setKeyPool = internal ? &setInternalKeyPool : &setExternalKeyPool;
auto it = setKeyPool->begin();
- CWalletDB walletdb(*dbw);
+ WalletBatch batch(*database);
while (it != std::end(*setKeyPool)) {
const int64_t& index = *(it);
if (index > keypool_id) break; // set*KeyPool is ordered
CKeyPool keypool;
- if (walletdb.ReadPool(index, keypool)) { //TODO: This should be unnecessary
+ if (batch.ReadPool(index, keypool)) { //TODO: This should be unnecessary
m_pool_key_to_index.erase(keypool.vchPubKey.GetID());
}
LearnAllRelatedScripts(keypool.vchPubKey);
- walletdb.ErasePool(index);
+ batch.ErasePool(index);
LogPrintf("keypool index %d removed\n", index);
it = setKeyPool->erase(it);
}
@@ -3872,14 +3876,14 @@ bool CWallet::AddDestData(const CTxDestination &dest, const std::string &key, co
return false;
mapAddressBook[dest].destdata.insert(std::make_pair(key, value));
- return CWalletDB(*dbw).WriteDestData(EncodeDestination(dest), key, value);
+ return WalletBatch(*database).WriteDestData(EncodeDestination(dest), key, value);
}
bool CWallet::EraseDestData(const CTxDestination &dest, const std::string &key)
{
if (!mapAddressBook[dest].destdata.erase(key))
return false;
- return CWalletDB(*dbw).EraseDestData(EncodeDestination(dest), key);
+ return WalletBatch(*database).EraseDestData(EncodeDestination(dest), key);
}
bool CWallet::LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
@@ -3928,7 +3932,7 @@ CWallet* CWallet::CreateWalletFromFile(const std::string& name, const fs::path&
if (gArgs.GetBoolArg("-zapwallettxes", false)) {
uiInterface.InitMessage(_("Zapping all transactions from wallet..."));
- std::unique_ptr<CWallet> tempWallet = MakeUnique<CWallet>(name, CWalletDBWrapper::Create(path));
+ std::unique_ptr<CWallet> tempWallet = MakeUnique<CWallet>(name, WalletDatabase::Create(path));
DBErrors nZapWalletRet = tempWallet->ZapWalletTx(vWtx);
if (nZapWalletRet != DBErrors::LOAD_OK) {
InitError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile));
@@ -3940,7 +3944,7 @@ CWallet* CWallet::CreateWalletFromFile(const std::string& name, const fs::path&
int64_t nStart = GetTimeMillis();
bool fFirstRun = true;
- CWallet *walletInstance = new CWallet(name, CWalletDBWrapper::Create(path));
+ CWallet *walletInstance = new CWallet(name, WalletDatabase::Create(path));
DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun);
if (nLoadWalletRet != DBErrors::LOAD_OK)
{
@@ -4045,9 +4049,9 @@ CWallet* CWallet::CreateWalletFromFile(const std::string& name, const fs::path&
CBlockIndex *pindexRescan = chainActive.Genesis();
if (!gArgs.GetBoolArg("-rescan", false))
{
- CWalletDB walletdb(*walletInstance->dbw);
+ WalletBatch batch(*walletInstance->database);
CBlockLocator locator;
- if (walletdb.ReadBestBlock(locator))
+ if (batch.ReadBestBlock(locator))
pindexRescan = FindForkInGlobalIndex(chainActive, locator);
}
@@ -4091,12 +4095,12 @@ CWallet* CWallet::CreateWalletFromFile(const std::string& name, const fs::path&
}
LogPrintf(" rescan %15dms\n", GetTimeMillis() - nStart);
walletInstance->SetBestChain(chainActive.GetLocator());
- walletInstance->dbw->IncrementUpdateCounter();
+ walletInstance->database->IncrementUpdateCounter();
// Restore wallet transaction metadata after -zapwallettxes=1
if (gArgs.GetBoolArg("-zapwallettxes", false) && gArgs.GetArg("-zapwallettxes", "1") != "2")
{
- CWalletDB walletdb(*walletInstance->dbw);
+ WalletBatch batch(*walletInstance->database);
for (const CWalletTx& wtxOld : vWtx)
{
@@ -4113,7 +4117,7 @@ CWallet* CWallet::CreateWalletFromFile(const std::string& name, const fs::path&
copyTo->fFromMe = copyFrom->fFromMe;
copyTo->strFromAccount = copyFrom->strFromAccount;
copyTo->nOrderPos = copyFrom->nOrderPos;
- walletdb.WriteTx(*copyTo);
+ batch.WriteTx(*copyTo);
}
}
}
@@ -4146,7 +4150,7 @@ void CWallet::postInitProcess(CScheduler& scheduler)
bool CWallet::BackupWallet(const std::string& strDest)
{
- return dbw->Backup(strDest);
+ return database->Backup(strDest);
}
CKeyPool::CKeyPool()
diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h
index 22a0ac2924..b85f374a06 100644
--- a/src/wallet/wallet.h
+++ b/src/wallet/wallet.h
@@ -396,7 +396,7 @@ public:
mapValueCopy["timesmart"] = strprintf("%u", nTimeSmart);
}
- s << *static_cast<const CMerkleTx*>(this);
+ s << static_cast<const CMerkleTx&>(*this);
std::vector<CMerkleTx> vUnused; //!< Used to be vtxPrev
s << vUnused << mapValueCopy << vOrderForm << fTimeReceivedIsTxTime << nTimeReceived << fFromMe << fSpent;
}
@@ -407,7 +407,7 @@ public:
Init(nullptr);
char fSpent;
- s >> *static_cast<CMerkleTx*>(this);
+ s >> static_cast<CMerkleTx&>(*this);
std::vector<CMerkleTx> vUnused; //!< Used to be vtxPrev
s >> vUnused >> mapValue >> vOrderForm >> fTimeReceivedIsTxTime >> nTimeReceived >> fFromMe >> fSpent;
@@ -549,7 +549,7 @@ public:
};
/**
- * Internal transfers.
+ * DEPRECATED Internal transfers.
* Database key is acentry<account><counter>.
*/
class CAccountingEntry
@@ -666,7 +666,7 @@ private:
std::mutex mutexScanning;
friend class WalletRescanReserver;
- CWalletDB *pwalletdbEncryption = nullptr;
+ WalletBatch *encrypted_batch = nullptr;
//! the current wallet version: clients below this version are not able to load the wallet
int nWalletVersion = FEATURE_BASE;
@@ -701,7 +701,7 @@ private:
CHDChain hdChain;
/* HD derive new child key (on internal or external chain) */
- void DeriveNewChildKey(CWalletDB &walletdb, CKeyMetadata& metadata, CKey& secret, bool internal = false);
+ void DeriveNewChildKey(WalletBatch &batch, CKeyMetadata& metadata, CKey& secret, bool internal = false);
std::set<int64_t> setInternalKeyPool;
std::set<int64_t> setExternalKeyPool;
@@ -729,7 +729,7 @@ private:
std::string m_name;
/** Internal database handle. */
- std::unique_ptr<CWalletDBWrapper> dbw;
+ std::unique_ptr<WalletDatabase> database;
/**
* The following is used to keep track of how far behind the wallet is
@@ -753,9 +753,9 @@ public:
/** Get database handle used by this wallet. Ideally this function would
* not be necessary.
*/
- CWalletDBWrapper& GetDBHandle()
+ WalletDatabase& GetDBHandle()
{
- return *dbw;
+ return *database;
}
/**
@@ -783,14 +783,14 @@ public:
unsigned int nMasterKeyMaxID = 0;
/** Construct wallet with specified name and database implementation. */
- CWallet(std::string name, std::unique_ptr<CWalletDBWrapper> dbw) : m_name(std::move(name)), dbw(std::move(dbw))
+ CWallet(std::string name, std::unique_ptr<WalletDatabase> database) : m_name(std::move(name)), database(std::move(database))
{
}
~CWallet()
{
- delete pwalletdbEncryption;
- pwalletdbEncryption = nullptr;
+ delete encrypted_batch;
+ encrypted_batch = nullptr;
}
std::map<uint256, CWalletTx> mapWallet;
@@ -856,10 +856,10 @@ public:
* keystore implementation
* Generate a new key
*/
- CPubKey GenerateNewKey(CWalletDB& walletdb, bool internal = false);
+ CPubKey GenerateNewKey(WalletBatch& batch, bool internal = false);
//! Adds a key to the store, and saves it to disk.
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey) override;
- bool AddKeyPubKeyWithDB(CWalletDB &walletdb,const CKey& key, const CPubKey &pubkey);
+ bool AddKeyPubKeyWithDB(WalletBatch &batch,const CKey& key, const CPubKey &pubkey);
//! Adds a key to the store, without saving it to disk (used by LoadWallet)
bool LoadKey(const CKey& key, const CPubKey &pubkey) { return CCryptoKeyStore::AddKeyPubKey(key, pubkey); }
//! Load metadata (used by LoadWallet)
@@ -907,7 +907,7 @@ public:
* Increment the next transaction order id
* @return next transaction order id
*/
- int64_t IncOrderPosNext(CWalletDB *pwalletdb = nullptr);
+ int64_t IncOrderPosNext(WalletBatch *batch = nullptr);
DBErrors ReorderTransactions();
bool AccountMove(std::string strFrom, std::string strTo, CAmount nAmount, std::string strComment = "");
bool GetLabelDestination(CTxDestination &dest, const std::string& label, bool bForceNew = false);
@@ -955,7 +955,7 @@ public:
void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& entries);
bool AddAccountingEntry(const CAccountingEntry&);
- bool AddAccountingEntry(const CAccountingEntry&, CWalletDB *pwalletdb);
+ bool AddAccountingEntry(const CAccountingEntry&, WalletBatch *batch);
bool DummySignTx(CMutableTransaction &txNew, const std::set<CTxOut> &txouts) const
{
std::vector<CTxOut> v_txouts(txouts.size());
@@ -989,6 +989,7 @@ public:
std::map<CTxDestination, CAmount> GetAddressBalances();
std::set<CTxDestination> GetLabelAddresses(const std::string& label) const;
+ void DeleteLabel(const std::string& label);
isminetype IsMine(const CTxIn& txin) const;
/**
@@ -1039,7 +1040,7 @@ public:
}
//! signify that a particular wallet feature is now used. this may change nWalletVersion and nWalletMaxVersion if those are lower
- bool SetMinVersion(enum WalletFeature, CWalletDB* pwalletdbIn = nullptr, bool fExplicit = false);
+ bool SetMinVersion(enum WalletFeature, WalletBatch* batch_in = nullptr, bool fExplicit = false);
//! change which version we're allowed to upgrade to (note that this does not immediately imply upgrading to that format)
bool SetMaxVersion(int nVersion);
@@ -1184,7 +1185,7 @@ public:
/**
- * Account information.
+ * DEPRECATED Account information.
* Stored in wallet with key "acc"+string account name.
*/
class CAccount
diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp
index 77cdfe7dd0..803cc5f0a0 100644
--- a/src/wallet/walletdb.cpp
+++ b/src/wallet/walletdb.cpp
@@ -21,42 +21,42 @@
#include <boost/thread.hpp>
//
-// CWalletDB
+// WalletBatch
//
-bool CWalletDB::WriteName(const std::string& strAddress, const std::string& strName)
+bool WalletBatch::WriteName(const std::string& strAddress, const std::string& strName)
{
return WriteIC(std::make_pair(std::string("name"), strAddress), strName);
}
-bool CWalletDB::EraseName(const std::string& strAddress)
+bool WalletBatch::EraseName(const std::string& strAddress)
{
// This should only be used for sending addresses, never for receiving addresses,
// receiving addresses must always have an address book entry if they're not change return.
return EraseIC(std::make_pair(std::string("name"), strAddress));
}
-bool CWalletDB::WritePurpose(const std::string& strAddress, const std::string& strPurpose)
+bool WalletBatch::WritePurpose(const std::string& strAddress, const std::string& strPurpose)
{
return WriteIC(std::make_pair(std::string("purpose"), strAddress), strPurpose);
}
-bool CWalletDB::ErasePurpose(const std::string& strAddress)
+bool WalletBatch::ErasePurpose(const std::string& strAddress)
{
return EraseIC(std::make_pair(std::string("purpose"), strAddress));
}
-bool CWalletDB::WriteTx(const CWalletTx& wtx)
+bool WalletBatch::WriteTx(const CWalletTx& wtx)
{
return WriteIC(std::make_pair(std::string("tx"), wtx.GetHash()), wtx);
}
-bool CWalletDB::EraseTx(uint256 hash)
+bool WalletBatch::EraseTx(uint256 hash)
{
return EraseIC(std::make_pair(std::string("tx"), hash));
}
-bool CWalletDB::WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata& keyMeta)
+bool WalletBatch::WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata& keyMeta)
{
if (!WriteIC(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta, false)) {
return false;
@@ -71,7 +71,7 @@ bool CWalletDB::WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, c
return WriteIC(std::make_pair(std::string("key"), vchPubKey), std::make_pair(vchPrivKey, Hash(vchKey.begin(), vchKey.end())), false);
}
-bool CWalletDB::WriteCryptedKey(const CPubKey& vchPubKey,
+bool WalletBatch::WriteCryptedKey(const CPubKey& vchPubKey,
const std::vector<unsigned char>& vchCryptedSecret,
const CKeyMetadata &keyMeta)
{
@@ -87,17 +87,17 @@ bool CWalletDB::WriteCryptedKey(const CPubKey& vchPubKey,
return true;
}
-bool CWalletDB::WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
+bool WalletBatch::WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
{
return WriteIC(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
}
-bool CWalletDB::WriteCScript(const uint160& hash, const CScript& redeemScript)
+bool WalletBatch::WriteCScript(const uint160& hash, const CScript& redeemScript)
{
return WriteIC(std::make_pair(std::string("cscript"), hash), redeemScript, false);
}
-bool CWalletDB::WriteWatchOnly(const CScript &dest, const CKeyMetadata& keyMeta)
+bool WalletBatch::WriteWatchOnly(const CScript &dest, const CKeyMetadata& keyMeta)
{
if (!WriteIC(std::make_pair(std::string("watchmeta"), dest), keyMeta)) {
return false;
@@ -105,7 +105,7 @@ bool CWalletDB::WriteWatchOnly(const CScript &dest, const CKeyMetadata& keyMeta)
return WriteIC(std::make_pair(std::string("watchs"), dest), '1');
}
-bool CWalletDB::EraseWatchOnly(const CScript &dest)
+bool WalletBatch::EraseWatchOnly(const CScript &dest)
{
if (!EraseIC(std::make_pair(std::string("watchmeta"), dest))) {
return false;
@@ -113,60 +113,65 @@ bool CWalletDB::EraseWatchOnly(const CScript &dest)
return EraseIC(std::make_pair(std::string("watchs"), dest));
}
-bool CWalletDB::WriteBestBlock(const CBlockLocator& locator)
+bool WalletBatch::WriteBestBlock(const CBlockLocator& locator)
{
WriteIC(std::string("bestblock"), CBlockLocator()); // Write empty block locator so versions that require a merkle branch automatically rescan
return WriteIC(std::string("bestblock_nomerkle"), locator);
}
-bool CWalletDB::ReadBestBlock(CBlockLocator& locator)
+bool WalletBatch::ReadBestBlock(CBlockLocator& locator)
{
- if (batch.Read(std::string("bestblock"), locator) && !locator.vHave.empty()) return true;
- return batch.Read(std::string("bestblock_nomerkle"), locator);
+ if (m_batch.Read(std::string("bestblock"), locator) && !locator.vHave.empty()) return true;
+ return m_batch.Read(std::string("bestblock_nomerkle"), locator);
}
-bool CWalletDB::WriteOrderPosNext(int64_t nOrderPosNext)
+bool WalletBatch::WriteOrderPosNext(int64_t nOrderPosNext)
{
return WriteIC(std::string("orderposnext"), nOrderPosNext);
}
-bool CWalletDB::ReadPool(int64_t nPool, CKeyPool& keypool)
+bool WalletBatch::ReadPool(int64_t nPool, CKeyPool& keypool)
{
- return batch.Read(std::make_pair(std::string("pool"), nPool), keypool);
+ return m_batch.Read(std::make_pair(std::string("pool"), nPool), keypool);
}
-bool CWalletDB::WritePool(int64_t nPool, const CKeyPool& keypool)
+bool WalletBatch::WritePool(int64_t nPool, const CKeyPool& keypool)
{
return WriteIC(std::make_pair(std::string("pool"), nPool), keypool);
}
-bool CWalletDB::ErasePool(int64_t nPool)
+bool WalletBatch::ErasePool(int64_t nPool)
{
return EraseIC(std::make_pair(std::string("pool"), nPool));
}
-bool CWalletDB::WriteMinVersion(int nVersion)
+bool WalletBatch::WriteMinVersion(int nVersion)
{
return WriteIC(std::string("minversion"), nVersion);
}
-bool CWalletDB::ReadAccount(const std::string& strAccount, CAccount& account)
+bool WalletBatch::ReadAccount(const std::string& strAccount, CAccount& account)
{
account.SetNull();
- return batch.Read(std::make_pair(std::string("acc"), strAccount), account);
+ return m_batch.Read(std::make_pair(std::string("acc"), strAccount), account);
}
-bool CWalletDB::WriteAccount(const std::string& strAccount, const CAccount& account)
+bool WalletBatch::WriteAccount(const std::string& strAccount, const CAccount& account)
{
return WriteIC(std::make_pair(std::string("acc"), strAccount), account);
}
-bool CWalletDB::WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry)
+bool WalletBatch::EraseAccount(const std::string& strAccount)
+{
+ return EraseIC(std::make_pair(std::string("acc"), strAccount));
+}
+
+bool WalletBatch::WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry)
{
return WriteIC(std::make_pair(std::string("acentry"), std::make_pair(acentry.strAccount, nAccEntryNum)), acentry);
}
-CAmount CWalletDB::GetAccountCreditDebit(const std::string& strAccount)
+CAmount WalletBatch::GetAccountCreditDebit(const std::string& strAccount)
{
std::list<CAccountingEntry> entries;
ListAccountCreditDebit(strAccount, entries);
@@ -178,11 +183,11 @@ CAmount CWalletDB::GetAccountCreditDebit(const std::string& strAccount)
return nCreditDebit;
}
-void CWalletDB::ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& entries)
+void WalletBatch::ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& entries)
{
bool fAllAccounts = (strAccount == "*");
- Dbc* pcursor = batch.GetCursor();
+ Dbc* pcursor = m_batch.GetCursor();
if (!pcursor)
throw std::runtime_error(std::string(__func__) + ": cannot create DB cursor");
bool setRange = true;
@@ -193,7 +198,7 @@ void CWalletDB::ListAccountCreditDebit(const std::string& strAccount, std::list<
if (setRange)
ssKey << std::make_pair(std::string("acentry"), std::make_pair((fAllAccounts ? std::string("") : strAccount), uint64_t(0)));
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
- int ret = batch.ReadAtCursor(pcursor, ssKey, ssValue, setRange);
+ int ret = m_batch.ReadAtCursor(pcursor, ssKey, ssValue, setRange);
setRange = false;
if (ret == DB_NOTFOUND)
break;
@@ -512,13 +517,13 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
return true;
}
-bool CWalletDB::IsKeyType(const std::string& strType)
+bool WalletBatch::IsKeyType(const std::string& strType)
{
return (strType== "key" || strType == "wkey" ||
strType == "mkey" || strType == "ckey");
}
-DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
+DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
{
CWalletScanState wss;
bool fNoncriticalErrors = false;
@@ -527,7 +532,7 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
LOCK(pwallet->cs_wallet);
try {
int nMinVersion = 0;
- if (batch.Read((std::string)"minversion", nMinVersion))
+ if (m_batch.Read((std::string)"minversion", nMinVersion))
{
if (nMinVersion > CLIENT_VERSION)
return DBErrors::TOO_NEW;
@@ -535,7 +540,7 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
}
// Get cursor
- Dbc* pcursor = batch.GetCursor();
+ Dbc* pcursor = m_batch.GetCursor();
if (!pcursor)
{
LogPrintf("Error getting wallet database cursor\n");
@@ -547,7 +552,7 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
// Read next record
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
- int ret = batch.ReadAtCursor(pcursor, ssKey, ssValue);
+ int ret = m_batch.ReadAtCursor(pcursor, ssKey, ssValue);
if (ret == DB_NOTFOUND)
break;
else if (ret != 0)
@@ -624,20 +629,20 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
return result;
}
-DBErrors CWalletDB::FindWalletTx(std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx)
+DBErrors WalletBatch::FindWalletTx(std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx)
{
DBErrors result = DBErrors::LOAD_OK;
try {
int nMinVersion = 0;
- if (batch.Read((std::string)"minversion", nMinVersion))
+ if (m_batch.Read((std::string)"minversion", nMinVersion))
{
if (nMinVersion > CLIENT_VERSION)
return DBErrors::TOO_NEW;
}
// Get cursor
- Dbc* pcursor = batch.GetCursor();
+ Dbc* pcursor = m_batch.GetCursor();
if (!pcursor)
{
LogPrintf("Error getting wallet database cursor\n");
@@ -649,7 +654,7 @@ DBErrors CWalletDB::FindWalletTx(std::vector<uint256>& vTxHash, std::vector<CWal
// Read next record
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
- int ret = batch.ReadAtCursor(pcursor, ssKey, ssValue);
+ int ret = m_batch.ReadAtCursor(pcursor, ssKey, ssValue);
if (ret == DB_NOTFOUND)
break;
else if (ret != 0)
@@ -683,7 +688,7 @@ DBErrors CWalletDB::FindWalletTx(std::vector<uint256>& vTxHash, std::vector<CWal
return result;
}
-DBErrors CWalletDB::ZapSelectTx(std::vector<uint256>& vTxHashIn, std::vector<uint256>& vTxHashOut)
+DBErrors WalletBatch::ZapSelectTx(std::vector<uint256>& vTxHashIn, std::vector<uint256>& vTxHashOut)
{
// build list of wallet TXs and hashes
std::vector<uint256> vTxHash;
@@ -721,7 +726,7 @@ DBErrors CWalletDB::ZapSelectTx(std::vector<uint256>& vTxHashIn, std::vector<uin
return DBErrors::LOAD_OK;
}
-DBErrors CWalletDB::ZapWalletTx(std::vector<CWalletTx>& vWtx)
+DBErrors WalletBatch::ZapWalletTx(std::vector<CWalletTx>& vWtx)
{
// build list of wallet TXs
std::vector<uint256> vTxHash;
@@ -749,7 +754,7 @@ void MaybeCompactWalletDB()
}
for (CWalletRef pwallet : vpwallets) {
- CWalletDBWrapper& dbh = pwallet->GetDBHandle();
+ WalletDatabase& dbh = pwallet->GetDBHandle();
unsigned int nUpdateCounter = dbh.nUpdateCounter;
@@ -759,7 +764,7 @@ void MaybeCompactWalletDB()
}
if (dbh.nLastFlushed != nUpdateCounter && GetTime() - dbh.nLastWalletUpdate >= 2) {
- if (CDB::PeriodicFlush(dbh)) {
+ if (BerkeleyBatch::PeriodicFlush(dbh)) {
dbh.nLastFlushed = nUpdateCounter;
}
}
@@ -771,19 +776,19 @@ void MaybeCompactWalletDB()
//
// Try to (very carefully!) recover wallet file if there is a problem.
//
-bool CWalletDB::Recover(const fs::path& wallet_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename)
+bool WalletBatch::Recover(const fs::path& wallet_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename)
{
- return CDB::Recover(wallet_path, callbackDataIn, recoverKVcallback, out_backup_filename);
+ return BerkeleyBatch::Recover(wallet_path, callbackDataIn, recoverKVcallback, out_backup_filename);
}
-bool CWalletDB::Recover(const fs::path& wallet_path, std::string& out_backup_filename)
+bool WalletBatch::Recover(const fs::path& wallet_path, std::string& out_backup_filename)
{
// recover without a key filter callback
// results in recovering all record types
- return CWalletDB::Recover(wallet_path, nullptr, nullptr, out_backup_filename);
+ return WalletBatch::Recover(wallet_path, nullptr, nullptr, out_backup_filename);
}
-bool CWalletDB::RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue)
+bool WalletBatch::RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue)
{
CWallet *dummyWallet = reinterpret_cast<CWallet*>(callbackData);
CWalletScanState dummyWss;
@@ -799,60 +804,60 @@ bool CWalletDB::RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDa
return false;
if (!fReadOK)
{
- LogPrintf("WARNING: CWalletDB::Recover skipping %s: %s\n", strType, strErr);
+ LogPrintf("WARNING: WalletBatch::Recover skipping %s: %s\n", strType, strErr);
return false;
}
return true;
}
-bool CWalletDB::VerifyEnvironment(const fs::path& wallet_path, std::string& errorStr)
+bool WalletBatch::VerifyEnvironment(const fs::path& wallet_path, std::string& errorStr)
{
- return CDB::VerifyEnvironment(wallet_path, errorStr);
+ return BerkeleyBatch::VerifyEnvironment(wallet_path, errorStr);
}
-bool CWalletDB::VerifyDatabaseFile(const fs::path& wallet_path, std::string& warningStr, std::string& errorStr)
+bool WalletBatch::VerifyDatabaseFile(const fs::path& wallet_path, std::string& warningStr, std::string& errorStr)
{
- return CDB::VerifyDatabaseFile(wallet_path, warningStr, errorStr, CWalletDB::Recover);
+ return BerkeleyBatch::VerifyDatabaseFile(wallet_path, warningStr, errorStr, WalletBatch::Recover);
}
-bool CWalletDB::WriteDestData(const std::string &address, const std::string &key, const std::string &value)
+bool WalletBatch::WriteDestData(const std::string &address, const std::string &key, const std::string &value)
{
return WriteIC(std::make_pair(std::string("destdata"), std::make_pair(address, key)), value);
}
-bool CWalletDB::EraseDestData(const std::string &address, const std::string &key)
+bool WalletBatch::EraseDestData(const std::string &address, const std::string &key)
{
return EraseIC(std::make_pair(std::string("destdata"), std::make_pair(address, key)));
}
-bool CWalletDB::WriteHDChain(const CHDChain& chain)
+bool WalletBatch::WriteHDChain(const CHDChain& chain)
{
return WriteIC(std::string("hdchain"), chain);
}
-bool CWalletDB::TxnBegin()
+bool WalletBatch::TxnBegin()
{
- return batch.TxnBegin();
+ return m_batch.TxnBegin();
}
-bool CWalletDB::TxnCommit()
+bool WalletBatch::TxnCommit()
{
- return batch.TxnCommit();
+ return m_batch.TxnCommit();
}
-bool CWalletDB::TxnAbort()
+bool WalletBatch::TxnAbort()
{
- return batch.TxnAbort();
+ return m_batch.TxnAbort();
}
-bool CWalletDB::ReadVersion(int& nVersion)
+bool WalletBatch::ReadVersion(int& nVersion)
{
- return batch.ReadVersion(nVersion);
+ return m_batch.ReadVersion(nVersion);
}
-bool CWalletDB::WriteVersion(int nVersion)
+bool WalletBatch::WriteVersion(int nVersion)
{
- return batch.WriteVersion(nVersion);
+ return m_batch.WriteVersion(nVersion);
}
diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h
index 606b7dace7..a73d727c0c 100644
--- a/src/wallet/walletdb.h
+++ b/src/wallet/walletdb.h
@@ -20,16 +20,13 @@
/**
* Overview of wallet database classes:
*
- * - CDBEnv is an environment in which the database exists (has no analog in dbwrapper.h)
- * - CWalletDBWrapper represents a wallet database (similar to CDBWrapper in dbwrapper.h)
- * - CDB is a low-level database transaction (similar to CDBBatch in dbwrapper.h)
- * - CWalletDB is a modifier object for the wallet, and encapsulates a database
- * transaction as well as methods to act on the database (no analog in
- * dbwrapper.h)
+ * - WalletBatch is an abstract modifier object for the wallet database, and encapsulates a database
+ * batch update as well as methods to act on the database. It should be agnostic to the database implementation.
*
- * The latter two are named confusingly, in contrast to what the names CDB
- * and CWalletDB suggest they are transient transaction objects and don't
- * represent the database itself.
+ * The following classes are implementation specific:
+ * - BerkeleyEnvironment is an environment in which the database exists.
+ * - BerkeleyDatabase represents a wallet database.
+ * - BerkeleyBatch is a low-level database batch update.
*/
static const bool DEFAULT_FLUSHWALLET = true;
@@ -45,6 +42,9 @@ class CWalletTx;
class uint160;
class uint256;
+/** Backend-agnostic database type. */
+using WalletDatabase = BerkeleyDatabase;
+
/** Error statuses for the wallet database */
enum class DBErrors
{
@@ -134,41 +134,41 @@ public:
};
/** Access to the wallet database.
- * This should really be named CWalletDBBatch, as it represents a single transaction at the
+ * This represents a single transaction at the
* database. It will be committed when the object goes out of scope.
* Optionally (on by default) it will flush to disk as well.
*/
-class CWalletDB
+class WalletBatch
{
private:
template <typename K, typename T>
bool WriteIC(const K& key, const T& value, bool fOverwrite = true)
{
- if (!batch.Write(key, value, fOverwrite)) {
+ if (!m_batch.Write(key, value, fOverwrite)) {
return false;
}
- m_dbw.IncrementUpdateCounter();
+ m_database.IncrementUpdateCounter();
return true;
}
template <typename K>
bool EraseIC(const K& key)
{
- if (!batch.Erase(key)) {
+ if (!m_batch.Erase(key)) {
return false;
}
- m_dbw.IncrementUpdateCounter();
+ m_database.IncrementUpdateCounter();
return true;
}
public:
- explicit CWalletDB(CWalletDBWrapper& dbw, const char* pszMode = "r+", bool _fFlushOnClose = true) :
- batch(dbw, pszMode, _fFlushOnClose),
- m_dbw(dbw)
+ explicit WalletBatch(WalletDatabase& database, const char* pszMode = "r+", bool _fFlushOnClose = true) :
+ m_batch(database, pszMode, _fFlushOnClose),
+ m_database(database)
{
}
- CWalletDB(const CWalletDB&) = delete;
- CWalletDB& operator=(const CWalletDB&) = delete;
+ WalletBatch(const WalletBatch&) = delete;
+ WalletBatch& operator=(const WalletBatch&) = delete;
bool WriteName(const std::string& strAddress, const std::string& strName);
bool EraseName(const std::string& strAddress);
@@ -204,6 +204,7 @@ public:
bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
bool ReadAccount(const std::string& strAccount, CAccount& account);
bool WriteAccount(const std::string& strAccount, const CAccount& account);
+ bool EraseAccount(const std::string& strAccount);
/// Write destination data key,value tuple to database
bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
@@ -244,8 +245,8 @@ public:
//! Write wallet version
bool WriteVersion(int nVersion);
private:
- CDB batch;
- CWalletDBWrapper& m_dbw;
+ BerkeleyBatch m_batch;
+ WalletDatabase& m_database;
};
//! Compacts BDB state so that wallet.dat is self-contained (if there are changes)