aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bench/base58.cpp27
-rw-r--r--src/bench/verify_script.cpp10
-rw-r--r--src/bitcoin-cli.cpp10
-rw-r--r--src/blockencodings.cpp2
-rw-r--r--src/consensus/consensus.h2
-rw-r--r--src/core_memusage.h2
-rw-r--r--src/httpserver.cpp1
-rw-r--r--src/netbase.cpp19
-rw-r--r--src/netbase.h1
-rw-r--r--src/policy/fees.cpp6
-rw-r--r--src/primitives/transaction.h4
-rw-r--r--src/protocol.h2
-rw-r--r--src/random.cpp19
-rw-r--r--src/rpc/blockchain.cpp2
-rw-r--r--src/script/interpreter.cpp4
-rw-r--r--src/script/script.h8
-rw-r--r--src/test/dbwrapper_tests.cpp29
-rw-r--r--src/test/netbase_tests.cpp1
-rw-r--r--src/test/txvalidationcache_tests.cpp2
-rw-r--r--src/txdb.cpp2
-rw-r--r--src/util.h6
-rw-r--r--src/utilstrencodings.cpp19
-rw-r--r--src/utilstrencodings.h1
-rw-r--r--src/wallet/rpcwallet.cpp4
-rw-r--r--src/wallet/rpcwallet.h2
-rw-r--r--src/wallet/wallet.cpp211
-rw-r--r--src/wallet/wallet.h19
-rw-r--r--src/wallet/walletdb.cpp16
28 files changed, 228 insertions, 203 deletions
diff --git a/src/bench/base58.cpp b/src/bench/base58.cpp
index 3319c179bf..65e27a615d 100644
--- a/src/bench/base58.cpp
+++ b/src/bench/base58.cpp
@@ -7,34 +7,37 @@
#include "validation.h"
#include "base58.h"
+#include <array>
#include <vector>
#include <string>
static void Base58Encode(benchmark::State& state)
{
- unsigned char buff[32] = {
- 17, 79, 8, 99, 150, 189, 208, 162, 22, 23, 203, 163, 36, 58, 147,
- 227, 139, 2, 215, 100, 91, 38, 11, 141, 253, 40, 117, 21, 16, 90,
- 200, 24
+ static const std::array<unsigned char, 32> buff = {
+ {
+ 17, 79, 8, 99, 150, 189, 208, 162, 22, 23, 203, 163, 36, 58, 147,
+ 227, 139, 2, 215, 100, 91, 38, 11, 141, 253, 40, 117, 21, 16, 90,
+ 200, 24
+ }
};
- unsigned char* b = buff;
while (state.KeepRunning()) {
- EncodeBase58(b, b + 32);
+ EncodeBase58(buff.begin(), buff.end());
}
}
static void Base58CheckEncode(benchmark::State& state)
{
- unsigned char buff[32] = {
- 17, 79, 8, 99, 150, 189, 208, 162, 22, 23, 203, 163, 36, 58, 147,
- 227, 139, 2, 215, 100, 91, 38, 11, 141, 253, 40, 117, 21, 16, 90,
- 200, 24
+ static const std::array<unsigned char, 32> buff = {
+ {
+ 17, 79, 8, 99, 150, 189, 208, 162, 22, 23, 203, 163, 36, 58, 147,
+ 227, 139, 2, 215, 100, 91, 38, 11, 141, 253, 40, 117, 21, 16, 90,
+ 200, 24
+ }
};
- unsigned char* b = buff;
std::vector<unsigned char> vch;
- vch.assign(b, b + 32);
+ vch.assign(buff.begin(), buff.end());
while (state.KeepRunning()) {
EncodeBase58Check(vch);
}
diff --git a/src/bench/verify_script.cpp b/src/bench/verify_script.cpp
index 23bbadc88d..ef7381c120 100644
--- a/src/bench/verify_script.cpp
+++ b/src/bench/verify_script.cpp
@@ -11,6 +11,8 @@
#include "script/sign.h"
#include "streams.h"
+#include <array>
+
// FIXME: Dedup with BuildCreditingTransaction in test/script_tests.cpp.
static CMutableTransaction BuildCreditingTransaction(const CScript& scriptPubKey)
{
@@ -55,8 +57,12 @@ static void VerifyScriptBench(benchmark::State& state)
// Keypair.
CKey key;
- const unsigned char vchKey[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
- key.Set(vchKey, vchKey + 32, false);
+ static const std::array<unsigned char, 32> vchKey = {
+ {
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
+ }
+ };
+ key.Set(vchKey.begin(), vchKey.end(), false);
CPubKey pubkey = key.GetPubKey();
uint160 pubkeyHash;
CHash160().Write(pubkey.begin(), pubkey.size()).Finalize(pubkeyHash.begin());
diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp
index 885b787b4d..8b48c7f8e4 100644
--- a/src/bitcoin-cli.cpp
+++ b/src/bitcoin-cli.cpp
@@ -191,8 +191,14 @@ static void http_error_cb(enum evhttp_request_error err, void *ctx)
UniValue CallRPC(const std::string& strMethod, const UniValue& params)
{
- std::string host = GetArg("-rpcconnect", DEFAULT_RPCCONNECT);
- int port = GetArg("-rpcport", BaseParams().RPCPort());
+ std::string host;
+ // In preference order, we choose the following for the port:
+ // 1. -rpcport
+ // 2. port in -rpcconnect (ie following : in ipv4 or ]: in ipv6)
+ // 3. default port for chain
+ int port = BaseParams().RPCPort();
+ SplitHostPort(GetArg("-rpcconnect", DEFAULT_RPCCONNECT), port, host);
+ port = GetArg("-rpcport", port);
// Obtain event base
raii_event_base base = obtain_event_base();
diff --git a/src/blockencodings.cpp b/src/blockencodings.cpp
index 7adab586e8..6f27b7b9dc 100644
--- a/src/blockencodings.cpp
+++ b/src/blockencodings.cpp
@@ -48,7 +48,7 @@ uint64_t CBlockHeaderAndShortTxIDs::GetShortID(const uint256& txhash) const {
ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& cmpctblock, const std::vector<std::pair<uint256, CTransactionRef>>& extra_txn) {
if (cmpctblock.header.IsNull() || (cmpctblock.shorttxids.empty() && cmpctblock.prefilledtxn.empty()))
return READ_STATUS_INVALID;
- if (cmpctblock.shorttxids.size() + cmpctblock.prefilledtxn.size() > MAX_BLOCK_WEIGHT / MIN_SERIALIZEABLE_TRANSACTION_WEIGHT)
+ if (cmpctblock.shorttxids.size() + cmpctblock.prefilledtxn.size() > MAX_BLOCK_WEIGHT / MIN_SERIALIZABLE_TRANSACTION_WEIGHT)
return READ_STATUS_INVALID;
assert(header.IsNull() && txn_available.empty());
diff --git a/src/consensus/consensus.h b/src/consensus/consensus.h
index a1bb99a802..ddd4ee9fab 100644
--- a/src/consensus/consensus.h
+++ b/src/consensus/consensus.h
@@ -21,7 +21,7 @@ static const int COINBASE_MATURITY = 100;
static const int WITNESS_SCALE_FACTOR = 4;
static const size_t MIN_TRANSACTION_WEIGHT = WITNESS_SCALE_FACTOR * 60; // 60 is the lower bound for the size of a valid serialized CTransaction
-static const size_t MIN_SERIALIZEABLE_TRANSACTION_WEIGHT = WITNESS_SCALE_FACTOR * 10; // 10 is the lower bound for the size of a serialized CTransaction
+static const size_t MIN_SERIALIZABLE_TRANSACTION_WEIGHT = WITNESS_SCALE_FACTOR * 10; // 10 is the lower bound for the size of a serialized CTransaction
/** Flags for nSequence and nLockTime locks */
enum {
diff --git a/src/core_memusage.h b/src/core_memusage.h
index e4ccd54c42..f038e7b154 100644
--- a/src/core_memusage.h
+++ b/src/core_memusage.h
@@ -10,7 +10,7 @@
#include "memusage.h"
static inline size_t RecursiveDynamicUsage(const CScript& script) {
- return memusage::DynamicUsage(*static_cast<const CScriptBase*>(&script));
+ return memusage::DynamicUsage(script);
}
static inline size_t RecursiveDynamicUsage(const COutPoint& out) {
diff --git a/src/httpserver.cpp b/src/httpserver.cpp
index 1c53d8d49d..290a2efca2 100644
--- a/src/httpserver.cpp
+++ b/src/httpserver.cpp
@@ -7,6 +7,7 @@
#include "chainparamsbase.h"
#include "compat.h"
#include "util.h"
+#include "utilstrencodings.h"
#include "netbase.h"
#include "rpc/protocol.h" // For HTTP status codes
#include "sync.h"
diff --git a/src/netbase.cpp b/src/netbase.cpp
index 3fb1c18f03..1f668a5d4c 100644
--- a/src/netbase.cpp
+++ b/src/netbase.cpp
@@ -58,25 +58,6 @@ std::string GetNetworkName(enum Network net) {
}
}
-void SplitHostPort(std::string in, int &portOut, std::string &hostOut) {
- size_t colon = in.find_last_of(':');
- // if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
- bool fHaveColon = colon != in.npos;
- bool fBracketed = fHaveColon && (in[0]=='[' && in[colon-1]==']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe
- bool fMultiColon = fHaveColon && (in.find_last_of(':',colon-1) != in.npos);
- if (fHaveColon && (colon==0 || fBracketed || !fMultiColon)) {
- int32_t n;
- if (ParseInt32(in.substr(colon + 1), &n) && n > 0 && n < 0x10000) {
- in = in.substr(0, colon);
- portOut = n;
- }
- }
- if (in.size()>0 && in[0] == '[' && in[in.size()-1] == ']')
- hostOut = in.substr(1, in.size()-2);
- else
- hostOut = in;
-}
-
bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)
{
vIP.clear();
diff --git a/src/netbase.h b/src/netbase.h
index c9d108aadd..fd4b34c8f1 100644
--- a/src/netbase.h
+++ b/src/netbase.h
@@ -39,7 +39,6 @@ public:
enum Network ParseNetwork(std::string net);
std::string GetNetworkName(enum Network net);
-void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
bool SetProxy(enum Network net, const proxyType &addrProxy);
bool GetProxy(enum Network net, proxyType &proxyInfoOut);
bool IsProxy(const CNetAddr &addr);
diff --git a/src/policy/fees.cpp b/src/policy/fees.cpp
index 45f9765231..0f186fa845 100644
--- a/src/policy/fees.cpp
+++ b/src/policy/fees.cpp
@@ -117,7 +117,7 @@ public:
* Create new TxConfirmStats. This is called by BlockPolicyEstimator's
* constructor with default values.
* @param defaultBuckets contains the upper limits for the bucket boundaries
- * @param maxConfirms max number of confirms to track
+ * @param maxPeriods max number of periods to track
* @param decay how much to decay the historical moving average per block
*/
TxConfirmStats(const std::vector<double>& defaultBuckets, const std::map<double, unsigned int>& defaultBucketMap,
@@ -698,7 +698,7 @@ CFeeRate CBlockPolicyEstimator::estimateRawFee(int confTarget, double successThr
break;
}
default: {
- throw std::out_of_range("CBlockPoicyEstimator::estimateRawFee unknown FeeEstimateHorizon");
+ throw std::out_of_range("CBlockPolicyEstimator::estimateRawFee unknown FeeEstimateHorizon");
}
}
@@ -730,7 +730,7 @@ unsigned int CBlockPolicyEstimator::HighestTargetTracked(FeeEstimateHorizon hori
return longStats->GetMaxConfirms();
}
default: {
- throw std::out_of_range("CBlockPoicyEstimator::HighestTargetTracked unknown FeeEstimateHorizon");
+ throw std::out_of_range("CBlockPolicyEstimator::HighestTargetTracked unknown FeeEstimateHorizon");
}
}
}
diff --git a/src/primitives/transaction.h b/src/primitives/transaction.h
index f1dd51b9fd..041034bb8b 100644
--- a/src/primitives/transaction.h
+++ b/src/primitives/transaction.h
@@ -106,7 +106,7 @@ public:
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(prevout);
- READWRITE(*(CScriptBase*)(&scriptSig));
+ READWRITE(scriptSig);
READWRITE(nSequence);
}
@@ -146,7 +146,7 @@ public:
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(nValue);
- READWRITE(*(CScriptBase*)(&scriptPubKey));
+ READWRITE(scriptPubKey);
}
void SetNull()
diff --git a/src/protocol.h b/src/protocol.h
index eba39ab1e5..7890bb627d 100644
--- a/src/protocol.h
+++ b/src/protocol.h
@@ -163,7 +163,7 @@ extern const char *PONG;
/**
* The notfound message is a reply to a getdata message which requested an
* object the receiving node does not have available for relay.
- * @ince protocol version 70001.
+ * @since protocol version 70001.
* @see https://bitcoin.org/en/developer-reference#notfound
*/
extern const char *NOTFOUND;
diff --git a/src/random.cpp b/src/random.cpp
index 67efc7d945..1ee6158e4b 100644
--- a/src/random.cpp
+++ b/src/random.cpp
@@ -36,6 +36,10 @@
#include <mutex>
+#if defined(__x86_64__) || defined(__amd64__) || defined(__i386__)
+#include <cpuid.h>
+#endif
+
#include <openssl/err.h>
#include <openssl/rand.h>
@@ -72,18 +76,9 @@ static bool rdrand_supported = false;
static constexpr uint32_t CPUID_F1_ECX_RDRAND = 0x40000000;
static void RDRandInit()
{
- uint32_t eax, ecx, edx;
-#if defined(__i386__) && ( defined(__PIC__) || defined(__PIE__))
- // Avoid clobbering ebx, as that is used for PIC on x86.
- uint32_t tmp;
- __asm__ ("mov %%ebx, %1; cpuid; mov %1, %%ebx": "=a"(eax), "=g"(tmp), "=c"(ecx), "=d"(edx) : "a"(1));
-#else
- uint32_t ebx;
- __asm__ ("cpuid": "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(1));
-#endif
- //! When calling cpuid function #1, ecx register will have this set if RDRAND is available.
- if (ecx & CPUID_F1_ECX_RDRAND) {
- LogPrintf("Using RdRand as entropy source\n");
+ uint32_t eax, ebx, ecx, edx;
+ if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) && (ecx & CPUID_F1_ECX_RDRAND)) {
+ LogPrintf("Using RdRand as an additional entropy source\n");
rdrand_supported = true;
}
hwrand_initialized.store(true);
diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp
index c17ca2fa3a..6178a1c7ab 100644
--- a/src/rpc/blockchain.cpp
+++ b/src/rpc/blockchain.cpp
@@ -798,7 +798,7 @@ static void ApplyStats(CCoinsStats &stats, CHashWriter& ss, const uint256& hash,
stats.nTransactions++;
for (const auto output : outputs) {
ss << VARINT(output.first + 1);
- ss << *(const CScriptBase*)(&output.second.out.scriptPubKey);
+ ss << output.second.out.scriptPubKey;
ss << VARINT(output.second.out.nValue);
stats.nTransactionOutputs++;
stats.nTotalAmount += output.second.out.nValue;
diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp
index 7149c938fc..8a121774a0 100644
--- a/src/script/interpreter.cpp
+++ b/src/script/interpreter.cpp
@@ -1099,7 +1099,7 @@ public:
// Serialize the script
if (nInput != nIn)
// Blank out other inputs' signatures
- ::Serialize(s, CScriptBase());
+ ::Serialize(s, CScript());
else
SerializeScriptCode(s);
// Serialize the nSequence
@@ -1207,7 +1207,7 @@ uint256 SignatureHash(const CScript& scriptCode, const CTransaction& txTo, unsig
// The prevout may already be contained in hashPrevout, and the nSequence
// may already be contain in hashSequence.
ss << txTo.vin[nIn].prevout;
- ss << static_cast<const CScriptBase&>(scriptCode);
+ ss << scriptCode;
ss << amount;
ss << txTo.vin[nIn].nSequence;
// Outputs (none/one/all, depending on flags)
diff --git a/src/script/script.h b/src/script/script.h
index bbb37f049e..d16bfd0e00 100644
--- a/src/script/script.h
+++ b/src/script/script.h
@@ -8,6 +8,7 @@
#include "crypto/common.h"
#include "prevector.h"
+#include "serialize.h"
#include <assert.h>
#include <climits>
@@ -404,6 +405,13 @@ public:
CScript(std::vector<unsigned char>::const_iterator pbegin, std::vector<unsigned char>::const_iterator pend) : CScriptBase(pbegin, pend) { }
CScript(const unsigned char* pbegin, const unsigned char* pend) : CScriptBase(pbegin, pend) { }
+ ADD_SERIALIZE_METHODS;
+
+ template <typename Stream, typename Operation>
+ inline void SerializationOp(Stream& s, Operation ser_action) {
+ READWRITE(static_cast<CScriptBase&>(*this));
+ }
+
CScript& operator+=(const CScript& b)
{
insert(end(), b.begin(), b.end());
diff --git a/src/test/dbwrapper_tests.cpp b/src/test/dbwrapper_tests.cpp
index be631ce7a6..6ed6e7744e 100644
--- a/src/test/dbwrapper_tests.cpp
+++ b/src/test/dbwrapper_tests.cpp
@@ -24,8 +24,7 @@ BOOST_FIXTURE_TEST_SUITE(dbwrapper_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(dbwrapper)
{
// Perform tests both obfuscated and non-obfuscated.
- for (int i = 0; i < 2; i++) {
- bool obfuscate = (bool)i;
+ for (bool obfuscate : {false, true}) {
fs::path ph = fs::temp_directory_path() / fs::unique_path();
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
char key = 'k';
@@ -45,8 +44,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
BOOST_AUTO_TEST_CASE(dbwrapper_batch)
{
// Perform tests both obfuscated and non-obfuscated.
- for (int i = 0; i < 2; i++) {
- bool obfuscate = (bool)i;
+ for (bool obfuscate : {false, true}) {
fs::path ph = fs::temp_directory_path() / fs::unique_path();
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
@@ -82,8 +80,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_batch)
BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
{
// Perform tests both obfuscated and non-obfuscated.
- for (int i = 0; i < 2; i++) {
- bool obfuscate = (bool)i;
+ for (bool obfuscate : {false, true}) {
fs::path ph = fs::temp_directory_path() / fs::unique_path();
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
@@ -95,7 +92,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
uint256 in2 = InsecureRand256();
BOOST_CHECK(dbw.Write(key2, in2));
- std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper*>(&dbw)->NewIterator());
+ std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
// Be sure to seek past the obfuscation key (if it exists)
it->Seek(key);
@@ -210,13 +207,8 @@ BOOST_AUTO_TEST_CASE(iterator_ordering)
BOOST_CHECK(dbw.Write(key, value));
}
- std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper*>(&dbw)->NewIterator());
- for (int c=0; c<2; ++c) {
- int seek_start;
- if (c == 0)
- seek_start = 0x00;
- else
- seek_start = 0x80;
+ std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
+ for (int seek_start : {0x00, 0x80}) {
it->Seek((uint8_t)seek_start);
for (int x=seek_start; x<256; ++x) {
uint8_t key;
@@ -286,13 +278,8 @@ BOOST_AUTO_TEST_CASE(iterator_string_ordering)
}
}
- std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper*>(&dbw)->NewIterator());
- for (int c=0; c<2; ++c) {
- int seek_start;
- if (c == 0)
- seek_start = 0;
- else
- seek_start = 5;
+ std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
+ for (int seek_start : {0, 5}) {
snprintf(buf, sizeof(buf), "%d", seek_start);
StringContentsSerializer seek_key(buf);
it->Seek(seek_key);
diff --git a/src/test/netbase_tests.cpp b/src/test/netbase_tests.cpp
index b45a7fcc57..1baf7643e5 100644
--- a/src/test/netbase_tests.cpp
+++ b/src/test/netbase_tests.cpp
@@ -4,6 +4,7 @@
#include "netbase.h"
#include "test/test_bitcoin.h"
+#include "utilstrencodings.h"
#include <string>
diff --git a/src/test/txvalidationcache_tests.cpp b/src/test/txvalidationcache_tests.cpp
index a74f40251a..f609cb1af4 100644
--- a/src/test/txvalidationcache_tests.cpp
+++ b/src/test/txvalidationcache_tests.cpp
@@ -196,8 +196,8 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, TestChain100Setup)
// Test that invalidity under a set of flags doesn't preclude validity
// under other (eg consensus) flags.
// spend_tx is invalid according to DERSIG
- CValidationState state;
{
+ CValidationState state;
PrecomputedTransactionData ptd_spend_tx(spend_tx);
BOOST_CHECK(!CheckInputs(spend_tx, state, pcoinsTip, true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_DERSIG, true, true, ptd_spend_tx, nullptr));
diff --git a/src/txdb.cpp b/src/txdb.cpp
index 002f6550bc..aa0b73a417 100644
--- a/src/txdb.cpp
+++ b/src/txdb.cpp
@@ -172,7 +172,7 @@ bool CBlockTreeDB::ReadLastBlockFile(int &nFile) {
CCoinsViewCursor *CCoinsViewDB::Cursor() const
{
- CCoinsViewDBCursor *i = new CCoinsViewDBCursor(const_cast<CDBWrapper*>(&db)->NewIterator(), GetBestBlock());
+ CCoinsViewDBCursor *i = new CCoinsViewDBCursor(const_cast<CDBWrapper&>(db).NewIterator(), GetBestBlock());
/* It seems that there are no "const iterators" for LevelDB. Since we
only need read operations on it, use a const-cast to get around
that restriction. */
diff --git a/src/util.h b/src/util.h
index 824ad51ac4..e1bdfb1988 100644
--- a/src/util.h
+++ b/src/util.h
@@ -215,7 +215,7 @@ public:
* Return string argument or default value
*
* @param strArg Argument to get (e.g. "-foo")
- * @param default (e.g. "1")
+ * @param strDefault (e.g. "1")
* @return command-line argument or default value
*/
std::string GetArg(const std::string& strArg, const std::string& strDefault);
@@ -224,7 +224,7 @@ public:
* Return integer argument or default value
*
* @param strArg Argument to get (e.g. "-foo")
- * @param default (e.g. 1)
+ * @param nDefault (e.g. 1)
* @return command-line argument (0 if invalid number) or default value
*/
int64_t GetArg(const std::string& strArg, int64_t nDefault);
@@ -233,7 +233,7 @@ public:
* Return boolean argument or default value
*
* @param strArg Argument to get (e.g. "-foo")
- * @param default (true or false)
+ * @param fDefault (true or false)
* @return command-line argument or default value
*/
bool GetBoolArg(const std::string& strArg, bool fDefault);
diff --git a/src/utilstrencodings.cpp b/src/utilstrencodings.cpp
index 2fabb8cf7a..9ee14070a2 100644
--- a/src/utilstrencodings.cpp
+++ b/src/utilstrencodings.cpp
@@ -91,6 +91,25 @@ std::vector<unsigned char> ParseHex(const std::string& str)
return ParseHex(str.c_str());
}
+void SplitHostPort(std::string in, int &portOut, std::string &hostOut) {
+ size_t colon = in.find_last_of(':');
+ // if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
+ bool fHaveColon = colon != in.npos;
+ bool fBracketed = fHaveColon && (in[0]=='[' && in[colon-1]==']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe
+ bool fMultiColon = fHaveColon && (in.find_last_of(':',colon-1) != in.npos);
+ if (fHaveColon && (colon==0 || fBracketed || !fMultiColon)) {
+ int32_t n;
+ if (ParseInt32(in.substr(colon + 1), &n) && n > 0 && n < 0x10000) {
+ in = in.substr(0, colon);
+ portOut = n;
+ }
+ }
+ if (in.size()>0 && in[0] == '[' && in[in.size()-1] == ']')
+ hostOut = in.substr(1, in.size()-2);
+ else
+ hostOut = in;
+}
+
std::string EncodeBase64(const unsigned char* pch, size_t len)
{
static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
diff --git a/src/utilstrencodings.h b/src/utilstrencodings.h
index 8b37fe12e0..707fdaad16 100644
--- a/src/utilstrencodings.h
+++ b/src/utilstrencodings.h
@@ -48,6 +48,7 @@ std::string DecodeBase32(const std::string& str);
std::string EncodeBase32(const unsigned char* pch, size_t len);
std::string EncodeBase32(const std::string& str);
+void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
std::string i64tostr(int64_t n);
std::string itostr(int n);
int64_t atoi64(const char* psz);
diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp
index f983a61a9f..ee8c7548fc 100644
--- a/src/wallet/rpcwallet.cpp
+++ b/src/wallet/rpcwallet.cpp
@@ -740,9 +740,9 @@ UniValue getbalance(const JSONRPCRequest& request)
"\nResult:\n"
"amount (numeric) The total amount in " + CURRENCY_UNIT + " received for this account.\n"
"\nExamples:\n"
- "\nThe total amount in the wallet\n"
+ "\nThe total amount in the wallet with 1 or more confirmations\n"
+ HelpExampleCli("getbalance", "") +
- "\nThe total amount in the wallet at least 5 blocks confirmed\n"
+ "\nThe total amount in the wallet at least 6 blocks confirmed\n"
+ HelpExampleCli("getbalance", "\"*\" 6") +
"\nAs a json rpc call\n"
+ HelpExampleRpc("getbalance", "\"*\", 6")
diff --git a/src/wallet/rpcwallet.h b/src/wallet/rpcwallet.h
index bd5dad18ca..31e2f6a699 100644
--- a/src/wallet/rpcwallet.h
+++ b/src/wallet/rpcwallet.h
@@ -16,7 +16,7 @@ void RegisterWalletRPCCommands(CRPCTable &t);
* @param[in] request JSONRPCRequest that wishes to access a wallet
* @return NULL if no wallet should be used, or a pointer to the CWallet
*/
-CWallet *GetWalletForJSONRPCRequest(const JSONRPCRequest&);
+CWallet *GetWalletForJSONRPCRequest(const JSONRPCRequest& request);
std::string HelpRequiringPassphrase(CWallet *);
void EnsureWalletIsUnlocked(CWallet *);
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
index 8c4f637306..13da7bb2c2 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -2991,7 +2991,8 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
if (dbw->Rewrite("\x04pool"))
{
LOCK(cs_wallet);
- setKeyPool.clear();
+ setInternalKeyPool.clear();
+ setExternalKeyPool.clear();
// Note: can't top-up keypool here, because wallet is locked.
// User will be prompted to unlock wallet the next operation
// that requires a new key.
@@ -3019,7 +3020,8 @@ DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256
{
if (dbw->Rewrite("\x04pool"))
{
- setKeyPool.clear();
+ setInternalKeyPool.clear();
+ setExternalKeyPool.clear();
// Note: can't top-up keypool here, because wallet is locked.
// User will be prompted to unlock wallet the next operation
// that requires a new key.
@@ -3044,7 +3046,8 @@ DBErrors CWallet::ZapWalletTx(std::vector<CWalletTx>& vWtx)
if (dbw->Rewrite("\x04pool"))
{
LOCK(cs_wallet);
- setKeyPool.clear();
+ setInternalKeyPool.clear();
+ setExternalKeyPool.clear();
// Note: can't top-up keypool here, because wallet is locked.
// User will be prompted to unlock wallet the next operation
// that requires a new key.
@@ -3128,9 +3131,16 @@ bool CWallet::NewKeyPool()
{
LOCK(cs_wallet);
CWalletDB walletdb(*dbw);
- for (int64_t nIndex : setKeyPool)
+
+ for (int64_t nIndex : setInternalKeyPool) {
+ walletdb.ErasePool(nIndex);
+ }
+ setInternalKeyPool.clear();
+
+ for (int64_t nIndex : setExternalKeyPool) {
walletdb.ErasePool(nIndex);
- setKeyPool.clear();
+ }
+ setExternalKeyPool.clear();
if (!TopUpKeyPool()) {
return false;
@@ -3142,25 +3152,8 @@ bool CWallet::NewKeyPool()
size_t CWallet::KeypoolCountExternalKeys()
{
- AssertLockHeld(cs_wallet); // setKeyPool
-
- // immediately return setKeyPool's size if HD or HD_SPLIT is disabled or not supported
- if (!IsHDEnabled() || !CanSupportFeature(FEATURE_HD_SPLIT))
- return setKeyPool.size();
-
- CWalletDB walletdb(*dbw);
-
- // count amount of external keys
- size_t amountE = 0;
- for(const int64_t& id : setKeyPool)
- {
- CKeyPool tmpKeypool;
- if (!walletdb.ReadPool(id, tmpKeypool))
- throw std::runtime_error(std::string(__func__) + ": read failed");
- amountE += !tmpKeypool.fInternal;
- }
-
- return amountE;
+ AssertLockHeld(cs_wallet); // setExternalKeyPool
+ return setExternalKeyPool.size();
}
bool CWallet::TopUpKeyPool(unsigned int kpSize)
@@ -3180,10 +3173,8 @@ bool CWallet::TopUpKeyPool(unsigned int kpSize)
// count amount of available keys (internal, external)
// make sure the keypool of external and internal keys fits the user selected target (-keypool)
- int64_t amountExternal = KeypoolCountExternalKeys();
- int64_t amountInternal = setKeyPool.size() - amountExternal;
- int64_t missingExternal = std::max(std::max((int64_t) nTargetSize, (int64_t) 1) - amountExternal, (int64_t) 0);
- int64_t missingInternal = std::max(std::max((int64_t) nTargetSize, (int64_t) 1) - amountInternal, (int64_t) 0);
+ int64_t missingExternal = std::max(std::max((int64_t) nTargetSize, (int64_t) 1) - (int64_t)setExternalKeyPool.size(), (int64_t) 0);
+ int64_t missingInternal = std::max(std::max((int64_t) nTargetSize, (int64_t) 1) - (int64_t)setInternalKeyPool.size(), (int64_t) 0);
if (!IsHDEnabled() || !CanSupportFeature(FEATURE_HD_SPLIT))
{
@@ -3195,20 +3186,32 @@ bool CWallet::TopUpKeyPool(unsigned int kpSize)
for (int64_t i = missingInternal + missingExternal; i--;)
{
int64_t nEnd = 1;
- if (i < missingInternal)
+ if (i < missingInternal) {
internal = true;
- if (!setKeyPool.empty())
- nEnd = *(--setKeyPool.end()) + 1;
+ }
+
+ if (!setInternalKeyPool.empty()) {
+ nEnd = *(setInternalKeyPool.rbegin()) + 1;
+ }
+ if (!setExternalKeyPool.empty()) {
+ nEnd = std::max(nEnd, *(setExternalKeyPool.rbegin()) + 1);
+ }
+
if (!walletdb.WritePool(nEnd, CKeyPool(GenerateNewKey(internal), internal)))
throw std::runtime_error(std::string(__func__) + ": writing generated key failed");
- setKeyPool.insert(nEnd);
- LogPrintf("keypool added key %d, size=%u, internal=%d\n", nEnd, setKeyPool.size(), internal);
+
+ if (internal) {
+ setInternalKeyPool.insert(nEnd);
+ } else {
+ setExternalKeyPool.insert(nEnd);
+ }
+ LogPrintf("keypool added key %d, size=%u (%u internal), new key is %s\n", nEnd, setInternalKeyPool.size() + setExternalKeyPool.size(), setInternalKeyPool.size(), internal ? "internal" : "external");
}
}
return true;
}
-void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, bool internal)
+void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, bool fRequestedInternal)
{
nIndex = -1;
keypool.vchPubKey = CPubKey();
@@ -3218,30 +3221,30 @@ void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, bool int
if (!IsLocked())
TopUpKeyPool();
+ bool fReturningInternal = IsHDEnabled() && CanSupportFeature(FEATURE_HD_SPLIT) && fRequestedInternal;
+ std::set<int64_t>& setKeyPool = fReturningInternal ? setInternalKeyPool : setExternalKeyPool;
+
// Get the oldest key
if(setKeyPool.empty())
return;
CWalletDB walletdb(*dbw);
- // try to find a key that matches the internal/external filter
- for(const int64_t& id : setKeyPool)
- {
- CKeyPool tmpKeypool;
- if (!walletdb.ReadPool(id, tmpKeypool))
- throw std::runtime_error(std::string(__func__) + ": read failed");
- if (!HaveKey(tmpKeypool.vchPubKey.GetID()))
- throw std::runtime_error(std::string(__func__) + ": unknown key in key pool");
- if (!IsHDEnabled() || !CanSupportFeature(FEATURE_HD_SPLIT) || tmpKeypool.fInternal == internal)
- {
- nIndex = id;
- keypool = tmpKeypool;
- setKeyPool.erase(id);
- assert(keypool.vchPubKey.IsValid());
- LogPrintf("keypool reserve %d\n", nIndex);
- return;
- }
+ auto it = setKeyPool.begin();
+ nIndex = *it;
+ setKeyPool.erase(it);
+ if (!walletdb.ReadPool(nIndex, keypool)) {
+ throw std::runtime_error(std::string(__func__) + ": read failed");
+ }
+ if (!HaveKey(keypool.vchPubKey.GetID())) {
+ throw std::runtime_error(std::string(__func__) + ": unknown key in key pool");
}
+ if (keypool.fInternal != fReturningInternal) {
+ throw std::runtime_error(std::string(__func__) + ": keypool entry misclassified");
+ }
+
+ assert(keypool.vchPubKey.IsValid());
+ LogPrintf("keypool reserve %d\n", nIndex);
}
}
@@ -3253,12 +3256,16 @@ void CWallet::KeepKey(int64_t nIndex)
LogPrintf("keypool keep %d\n", nIndex);
}
-void CWallet::ReturnKey(int64_t nIndex)
+void CWallet::ReturnKey(int64_t nIndex, bool fInternal)
{
// Return to key pool
{
LOCK(cs_wallet);
- setKeyPool.insert(nIndex);
+ if (fInternal) {
+ setInternalKeyPool.insert(nIndex);
+ } else {
+ setExternalKeyPool.insert(nIndex);
+ }
}
LogPrintf("keypool return %d\n", nIndex);
}
@@ -3282,48 +3289,35 @@ bool CWallet::GetKeyFromPool(CPubKey& result, bool internal)
return true;
}
-int64_t CWallet::GetOldestKeyPoolTime()
-{
- LOCK(cs_wallet);
-
- // if the keypool is empty, return <NOW>
- if (setKeyPool.empty())
+static int64_t GetOldestKeyTimeInPool(const std::set<int64_t>& setKeyPool, CWalletDB& walletdb) {
+ if (setKeyPool.empty()) {
return GetTime();
+ }
CKeyPool keypool;
- CWalletDB walletdb(*dbw);
-
- if (IsHDEnabled() && CanSupportFeature(FEATURE_HD_SPLIT))
- {
- // if HD & HD Chain Split is enabled, response max(oldest-internal-key, oldest-external-key)
- int64_t now = GetTime();
- int64_t oldest_external = now, oldest_internal = now;
-
- for(const int64_t& id : setKeyPool)
- {
- if (!walletdb.ReadPool(id, keypool)) {
- throw std::runtime_error(std::string(__func__) + ": read failed");
- }
- if (keypool.fInternal && keypool.nTime < oldest_internal) {
- oldest_internal = keypool.nTime;
- }
- else if (!keypool.fInternal && keypool.nTime < oldest_external) {
- oldest_external = keypool.nTime;
- }
- if (oldest_internal != now && oldest_external != now) {
- break;
- }
- }
- return std::max(oldest_internal, oldest_external);
- }
- // load oldest key from keypool, get time and return
int64_t nIndex = *(setKeyPool.begin());
- if (!walletdb.ReadPool(nIndex, keypool))
+ if (!walletdb.ReadPool(nIndex, keypool)) {
throw std::runtime_error(std::string(__func__) + ": read oldest key in keypool failed");
+ }
assert(keypool.vchPubKey.IsValid());
return keypool.nTime;
}
+int64_t CWallet::GetOldestKeyPoolTime()
+{
+ LOCK(cs_wallet);
+
+ CWalletDB walletdb(*dbw);
+
+ // load oldest key from keypool, get time and return
+ int64_t oldestKey = GetOldestKeyTimeInPool(setExternalKeyPool, walletdb);
+ if (IsHDEnabled() && CanSupportFeature(FEATURE_HD_SPLIT)) {
+ oldestKey = std::max(GetOldestKeyTimeInPool(setInternalKeyPool, walletdb), oldestKey);
+ }
+
+ return oldestKey;
+}
+
std::map<CTxDestination, CAmount> CWallet::GetAddressBalances()
{
std::map<CTxDestination, CAmount> balances;
@@ -3482,6 +3476,7 @@ bool CReserveKey::GetReservedKey(CPubKey& pubkey, bool internal)
else {
return false;
}
+ fInternal = keypool.fInternal;
}
assert(vchPubKey.IsValid());
pubkey = vchPubKey;
@@ -3498,19 +3493,14 @@ void CReserveKey::KeepKey()
void CReserveKey::ReturnKey()
{
- if (nIndex != -1)
- pwallet->ReturnKey(nIndex);
+ if (nIndex != -1) {
+ pwallet->ReturnKey(nIndex, fInternal);
+ }
nIndex = -1;
vchPubKey = CPubKey();
}
-void CWallet::GetAllReserveKeys(std::set<CKeyID>& setAddress) const
-{
- setAddress.clear();
-
- CWalletDB walletdb(*dbw);
-
- LOCK2(cs_main, cs_wallet);
+static void LoadReserveKeysToSet(std::set<CKeyID>& setAddress, const std::set<int64_t>& setKeyPool, CWalletDB& walletdb) {
for (const int64_t& id : setKeyPool)
{
CKeyPool keypool;
@@ -3518,12 +3508,27 @@ void CWallet::GetAllReserveKeys(std::set<CKeyID>& setAddress) const
throw std::runtime_error(std::string(__func__) + ": read failed");
assert(keypool.vchPubKey.IsValid());
CKeyID keyID = keypool.vchPubKey.GetID();
- if (!HaveKey(keyID))
- throw std::runtime_error(std::string(__func__) + ": unknown key in key pool");
setAddress.insert(keyID);
}
}
+void CWallet::GetAllReserveKeys(std::set<CKeyID>& setAddress) const
+{
+ setAddress.clear();
+
+ CWalletDB walletdb(*dbw);
+
+ LOCK2(cs_main, cs_wallet);
+ LoadReserveKeysToSet(setAddress, setInternalKeyPool, walletdb);
+ LoadReserveKeysToSet(setAddress, setExternalKeyPool, walletdb);
+
+ for (const CKeyID& keyID : setAddress) {
+ if (!HaveKey(keyID)) {
+ throw std::runtime_error(std::string(__func__) + ": unknown key in key pool");
+ }
+ }
+}
+
void CWallet::GetScriptForMining(std::shared_ptr<CReserveScript> &script)
{
std::shared_ptr<CReserveKey> rKey = std::make_shared<CReserveKey>(this);
@@ -4057,13 +4062,19 @@ bool CWallet::ParameterInteraction()
}
}
- // -zapwallettx implies a rescan
- if (GetBoolArg("-zapwallettxes", false)) {
+ int zapwallettxes = GetArg("-zapwallettxes", 0);
+ // -zapwallettxes implies dropping the mempool on startup
+ if (zapwallettxes != 0 && SoftSetBoolArg("-persistmempool", false)) {
+ LogPrintf("%s: parameter interaction: -zapwallettxes=%s -> setting -persistmempool=0\n", __func__, zapwallettxes);
+ }
+
+ // -zapwallettxes implies a rescan
+ if (zapwallettxes != 0) {
if (is_multiwallet) {
return InitError(strprintf("%s is only allowed with a single wallet file", "-zapwallettxes"));
}
if (SoftSetBoolArg("-rescan", true)) {
- LogPrintf("%s: parameter interaction: -zapwallettxes=<mode> -> setting -rescan=1\n", __func__);
+ LogPrintf("%s: parameter interaction: -zapwallettxes=%s -> setting -rescan=1\n", __func__, zapwallettxes);
}
}
diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h
index bb9d146a20..b716e46a7d 100644
--- a/src/wallet/wallet.h
+++ b/src/wallet/wallet.h
@@ -699,7 +699,8 @@ private:
/* HD derive new child key (on internal or external chain) */
void DeriveNewChildKey(CKeyMetadata& metadata, CKey& secret, bool internal = false);
- std::set<int64_t> setKeyPool;
+ std::set<int64_t> setInternalKeyPool;
+ std::set<int64_t> setExternalKeyPool;
int64_t nTimeFirstKey;
@@ -744,7 +745,11 @@ public:
void LoadKeyPool(int nIndex, const CKeyPool &keypool)
{
- setKeyPool.insert(nIndex);
+ if (keypool.fInternal) {
+ setInternalKeyPool.insert(nIndex);
+ } else {
+ setExternalKeyPool.insert(nIndex);
+ }
// If no metadata exists yet, create a default with the pool key's
// creation time. Note that this may be overwritten by actually
@@ -974,9 +979,9 @@ public:
bool NewKeyPool();
size_t KeypoolCountExternalKeys();
bool TopUpKeyPool(unsigned int kpSize = 0);
- void ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, bool internal);
+ void ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, bool fRequestedInternal);
void KeepKey(int64_t nIndex);
- void ReturnKey(int64_t nIndex);
+ void ReturnKey(int64_t nIndex, bool fInternal);
bool GetKeyFromPool(CPubKey &key, bool internal = false);
int64_t GetOldestKeyPoolTime();
void GetAllReserveKeys(std::set<CKeyID>& setAddress) const;
@@ -1030,8 +1035,8 @@ public:
unsigned int GetKeyPoolSize()
{
- AssertLockHeld(cs_wallet); // setKeyPool
- return setKeyPool.size();
+ AssertLockHeld(cs_wallet); // set{Ex,In}ternalKeyPool
+ return setInternalKeyPool.size() + setExternalKeyPool.size();
}
bool SetDefaultKey(const CPubKey &vchPubKey);
@@ -1135,11 +1140,13 @@ protected:
CWallet* pwallet;
int64_t nIndex;
CPubKey vchPubKey;
+ bool fInternal;
public:
CReserveKey(CWallet* pwalletIn)
{
nIndex = -1;
pwallet = pwalletIn;
+ fInternal = false;
}
CReserveKey() = default;
diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp
index 2a33dca240..65a28af46d 100644
--- a/src/wallet/walletdb.cpp
+++ b/src/wallet/walletdb.cpp
@@ -94,23 +94,23 @@ bool CWalletDB::WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
bool CWalletDB::WriteCScript(const uint160& hash, const CScript& redeemScript)
{
- return WriteIC(std::make_pair(std::string("cscript"), hash), *(const CScriptBase*)(&redeemScript), false);
+ return WriteIC(std::make_pair(std::string("cscript"), hash), redeemScript, false);
}
bool CWalletDB::WriteWatchOnly(const CScript &dest, const CKeyMetadata& keyMeta)
{
- if (!WriteIC(std::make_pair(std::string("watchmeta"), *(const CScriptBase*)(&dest)), keyMeta)) {
+ if (!WriteIC(std::make_pair(std::string("watchmeta"), dest), keyMeta)) {
return false;
}
- return WriteIC(std::make_pair(std::string("watchs"), *(const CScriptBase*)(&dest)), '1');
+ return WriteIC(std::make_pair(std::string("watchs"), dest), '1');
}
bool CWalletDB::EraseWatchOnly(const CScript &dest)
{
- if (!EraseIC(std::make_pair(std::string("watchmeta"), *(const CScriptBase*)(&dest)))) {
+ if (!EraseIC(std::make_pair(std::string("watchmeta"), dest))) {
return false;
}
- return EraseIC(std::make_pair(std::string("watchs"), *(const CScriptBase*)(&dest)));
+ return EraseIC(std::make_pair(std::string("watchs"), dest));
}
bool CWalletDB::WriteBestBlock(const CBlockLocator& locator)
@@ -323,7 +323,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
{
wss.nWatchKeys++;
CScript script;
- ssKey >> *(CScriptBase*)(&script);
+ ssKey >> script;
char fYes;
ssValue >> fYes;
if (fYes == '1')
@@ -440,7 +440,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
else if (strType == "watchmeta")
{
CScript script;
- ssKey >> *(CScriptBase*)(&script);
+ ssKey >> script;
keyID = CScriptID(script);
}
@@ -474,7 +474,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
uint160 hash;
ssKey >> hash;
CScript script;
- ssValue >> *(CScriptBase*)(&script);
+ ssValue >> script;
if (!pwallet->LoadCScript(script))
{
strErr = "Error reading wallet database: LoadCScript failed";