aboutsummaryrefslogtreecommitdiff
path: root/src/wallet.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/wallet.cpp')
-rw-r--r--src/wallet.cpp125
1 files changed, 64 insertions, 61 deletions
diff --git a/src/wallet.cpp b/src/wallet.cpp
index df1eb549a..7664d6c25 100644
--- a/src/wallet.cpp
+++ b/src/wallet.cpp
@@ -6,9 +6,9 @@
#include "wallet.h"
#include "base58.h"
+#include "checkpoints.h"
#include "coincontrol.h"
#include "net.h"
-#include "checkpoints.h"
#include <boost/algorithm/string/replace.hpp>
#include <openssl/rand.h>
@@ -16,7 +16,7 @@
using namespace std;
// Settings
-int64_t nTransactionFee = 0;
+CFeeRate payTxFee(DEFAULT_TRANSACTION_FEE);
bool bSpendZeroConfChange = true;
//////////////////////////////////////////////////////////////////////////////
@@ -128,6 +128,22 @@ bool CWallet::AddCScript(const CScript& redeemScript)
return CWalletDB(strWalletFile).WriteCScript(Hash160(redeemScript), redeemScript);
}
+bool CWallet::LoadCScript(const CScript& redeemScript)
+{
+ /* A sanity check was added in pull #3843 to avoid adding redeemScripts
+ * that never can be redeemed. However, old wallets may still contain
+ * these. Do not add them to the wallet and warn. */
+ if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
+ {
+ std::string strAddr = CBitcoinAddress(redeemScript.GetID()).ToString();
+ LogPrintf("%s: Warning: This wallet contains a redeemScript of size %i which exceeds maximum size %i thus can never be redeemed. Do not use address %s.\n",
+ __func__, redeemScript.size(), MAX_SCRIPT_ELEMENT_SIZE, strAddr);
+ return true;
+ }
+
+ return CCryptoKeyStore::AddCScript(redeemScript);
+}
+
bool CWallet::Unlock(const SecureString& strWalletPassphrase)
{
CCrypter crypter;
@@ -471,6 +487,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet)
if (fFromLoadWallet)
{
mapWallet[hash] = wtxIn;
+ mapWallet[hash].BindWallet(this);
AddToSpends(hash);
}
else
@@ -586,11 +603,11 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet)
// Add a transaction to the wallet, or update it.
// pblock is optional, but should be provided if the transaction is known to be in a block.
// If fUpdate is true, existing transactions will be updated.
-bool CWallet::AddToWalletIfInvolvingMe(const uint256 &hash, const CTransaction& tx, const CBlock* pblock, bool fUpdate)
+bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate)
{
{
AssertLockHeld(cs_wallet);
- bool fExisted = mapWallet.count(hash);
+ bool fExisted = mapWallet.count(tx.GetHash());
if (fExisted && !fUpdate) return false;
if (fExisted || IsMine(tx) || IsFromMe(tx))
{
@@ -604,10 +621,10 @@ bool CWallet::AddToWalletIfInvolvingMe(const uint256 &hash, const CTransaction&
return false;
}
-void CWallet::SyncTransaction(const uint256 &hash, const CTransaction& tx, const CBlock* pblock)
+void CWallet::SyncTransaction(const CTransaction& tx, const CBlock* pblock)
{
- LOCK(cs_wallet);
- if (!AddToWalletIfInvolvingMe(hash, tx, pblock, true))
+ LOCK2(cs_main, cs_wallet);
+ if (!AddToWalletIfInvolvingMe(tx, pblock, true))
return; // Not one of ours
// If a transaction changes 'conflicted' state, that changes the balance
@@ -834,21 +851,26 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
CBlockIndex* pindex = pindexStart;
{
- LOCK(cs_wallet);
+ LOCK2(cs_main, cs_wallet);
+
+ // no need to read and scan block, if block was created before
+ // our wallet birthday (as adjusted for block time variability)
+ while (pindex && nTimeFirstKey && (pindex->nTime < (nTimeFirstKey - 7200)))
+ pindex = chainActive.Next(pindex);
+
+ ShowProgress(_("Rescanning..."), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup
+ double dProgressStart = Checkpoints::GuessVerificationProgress(pindex, false);
+ double dProgressTip = Checkpoints::GuessVerificationProgress(chainActive.Tip(), false);
while (pindex)
{
- // no need to read and scan block, if block was created before
- // our wallet birthday (as adjusted for block time variability)
- if (nTimeFirstKey && (pindex->nTime < (nTimeFirstKey - 7200))) {
- pindex = chainActive.Next(pindex);
- continue;
- }
+ if (pindex->nHeight % 100 == 0 && dProgressTip - dProgressStart > 0.0)
+ ShowProgress(_("Rescanning..."), std::max(1, std::min(99, (int)((Checkpoints::GuessVerificationProgress(pindex, false) - dProgressStart) / (dProgressTip - dProgressStart) * 100))));
CBlock block;
ReadBlockFromDisk(block, pindex);
BOOST_FOREACH(CTransaction& tx, block.vtx)
{
- if (AddToWalletIfInvolvingMe(tx.GetHash(), tx, &block, fUpdate))
+ if (AddToWalletIfInvolvingMe(tx, &block, fUpdate))
ret++;
}
pindex = chainActive.Next(pindex);
@@ -857,13 +879,14 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
LogPrintf("Still rescanning. At block %d. Progress=%f\n", pindex->nHeight, Checkpoints::GuessVerificationProgress(pindex));
}
}
+ ShowProgress(_("Rescanning..."), 100); // hide progress dialog in GUI
}
return ret;
}
void CWallet::ReacceptWalletTransactions()
{
- LOCK(cs_wallet);
+ LOCK2(cs_main, cs_wallet);
BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
{
const uint256& wtxid = item.first;
@@ -886,9 +909,8 @@ void CWalletTx::RelayWalletTransaction()
if (!IsCoinBase())
{
if (GetDepthInMainChain() == 0) {
- uint256 hash = GetHash();
- LogPrintf("Relaying wtx %s\n", hash.ToString());
- RelayTransaction((CTransaction)*this, hash);
+ LogPrintf("Relaying wtx %s\n", GetHash().ToString());
+ RelayTransaction((CTransaction)*this);
}
}
}
@@ -958,7 +980,7 @@ int64_t CWallet::GetBalance() const
{
int64_t nTotal = 0;
{
- LOCK(cs_wallet);
+ LOCK2(cs_main, cs_wallet);
for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
{
const CWalletTx* pcoin = &(*it).second;
@@ -974,7 +996,7 @@ int64_t CWallet::GetUnconfirmedBalance() const
{
int64_t nTotal = 0;
{
- LOCK(cs_wallet);
+ LOCK2(cs_main, cs_wallet);
for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
{
const CWalletTx* pcoin = &(*it).second;
@@ -989,7 +1011,7 @@ int64_t CWallet::GetImmatureBalance() const
{
int64_t nTotal = 0;
{
- LOCK(cs_wallet);
+ LOCK2(cs_main, cs_wallet);
for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
{
const CWalletTx* pcoin = &(*it).second;
@@ -1005,7 +1027,7 @@ void CWallet::AvailableCoins(vector<COutput>& vCoins, bool fOnlyConfirmed, const
vCoins.clear();
{
- LOCK(cs_wallet);
+ LOCK2(cs_main, cs_wallet);
for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
{
const uint256& wtxid = it->first;
@@ -1222,15 +1244,16 @@ bool CWallet::CreateTransaction(const vector<pair<CScript, int64_t> >& vecSend,
}
wtxNew.BindWallet(this);
+ CMutableTransaction txNew;
{
LOCK2(cs_main, cs_wallet);
{
- nFeeRet = nTransactionFee;
+ nFeeRet = payTxFee.GetFeePerK();
while (true)
{
- wtxNew.vin.clear();
- wtxNew.vout.clear();
+ txNew.vin.clear();
+ txNew.vout.clear();
wtxNew.fFromMe = true;
int64_t nTotalValue = nValue + nFeeRet;
@@ -1239,12 +1262,12 @@ bool CWallet::CreateTransaction(const vector<pair<CScript, int64_t> >& vecSend,
BOOST_FOREACH (const PAIRTYPE(CScript, int64_t)& s, vecSend)
{
CTxOut txout(s.second, s.first);
- if (txout.IsDust(CTransaction::nMinRelayTxFee))
+ if (txout.IsDust(CTransaction::minRelayTxFee))
{
strFailReason = _("Transaction amount too small");
return false;
}
- wtxNew.vout.push_back(txout);
+ txNew.vout.push_back(txout);
}
// Choose coins to use
@@ -1265,16 +1288,6 @@ bool CWallet::CreateTransaction(const vector<pair<CScript, int64_t> >& vecSend,
}
int64_t nChange = nValueIn - nValue - nFeeRet;
- // The following if statement should be removed once enough miners
- // have upgraded to the 0.9 GetMinFee() rules. Until then, this avoids
- // creating free transactions that have change outputs less than
- // CENT bitcoins.
- if (nFeeRet < CTransaction::nMinTxFee && nChange > 0 && nChange < CENT)
- {
- int64_t nMoveToFee = min(nChange, CTransaction::nMinTxFee - nFeeRet);
- nChange -= nMoveToFee;
- nFeeRet += nMoveToFee;
- }
if (nChange > 0)
{
@@ -1310,7 +1323,7 @@ bool CWallet::CreateTransaction(const vector<pair<CScript, int64_t> >& vecSend,
// Never create dust outputs; if we would, just
// add the dust to the fee.
- if (newTxOut.IsDust(CTransaction::nMinRelayTxFee))
+ if (newTxOut.IsDust(CTransaction::minRelayTxFee))
{
nFeeRet += nChange;
reservekey.ReturnKey();
@@ -1318,8 +1331,8 @@ bool CWallet::CreateTransaction(const vector<pair<CScript, int64_t> >& vecSend,
else
{
// Insert change txn at random position:
- vector<CTxOut>::iterator position = wtxNew.vout.begin()+GetRandInt(wtxNew.vout.size()+1);
- wtxNew.vout.insert(position, newTxOut);
+ vector<CTxOut>::iterator position = txNew.vout.begin()+GetRandInt(txNew.vout.size()+1);
+ txNew.vout.insert(position, newTxOut);
}
}
else
@@ -1327,17 +1340,20 @@ bool CWallet::CreateTransaction(const vector<pair<CScript, int64_t> >& vecSend,
// Fill vin
BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins)
- wtxNew.vin.push_back(CTxIn(coin.first->GetHash(),coin.second));
+ txNew.vin.push_back(CTxIn(coin.first->GetHash(),coin.second));
// Sign
int nIn = 0;
BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins)
- if (!SignSignature(*this, *coin.first, wtxNew, nIn++))
+ if (!SignSignature(*this, *coin.first, txNew, nIn++))
{
strFailReason = _("Signing transaction failed");
return false;
}
+ // Embed the constructed transaction data in wtxNew.
+ *static_cast<CTransaction*>(&wtxNew) = CTransaction(txNew);
+
// Limit size
unsigned int nBytes = ::GetSerializeSize(*(CTransaction*)&wtxNew, SER_NETWORK, PROTOCOL_VERSION);
if (nBytes >= MAX_STANDARD_TX_SIZE)
@@ -1348,7 +1364,7 @@ bool CWallet::CreateTransaction(const vector<pair<CScript, int64_t> >& vecSend,
dPriority = wtxNew.ComputePriority(dPriority, nBytes);
// Check that enough fee is included
- int64_t nPayFee = nTransactionFee * (1 + (int64_t)nBytes / 1000);
+ int64_t nPayFee = payTxFee.GetFee(nBytes);
bool fAllowFree = AllowFree(dPriority);
int64_t nMinFee = GetMinFee(wtxNew, nBytes, fAllowFree, GMF_SEND);
if (nFeeRet < max(nPayFee, nMinFee))
@@ -1457,7 +1473,7 @@ string CWallet::SendMoneyToDestination(const CTxDestination& address, int64_t nV
// Check amount
if (nValue <= 0)
return _("Invalid amount");
- if (nValue + nTransactionFee > GetBalance())
+ if (nValue > GetBalance())
return _("Insufficient funds");
// Parse Bitcoin address
@@ -1492,6 +1508,8 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
return nLoadWalletRet;
fFirstRunRet = !vchDefaultKey.IsValid();
+ uiInterface.LoadWallet(this);
+
return DB_LOAD_OK;
}
@@ -1629,7 +1647,7 @@ bool CWallet::TopUpKeyPool(unsigned int kpSize)
if (!walletdb.WritePool(nEnd, CKeyPool(GenerateNewKey())))
throw runtime_error("TopUpKeyPool() : writing generated key failed");
setKeyPool.insert(nEnd);
- LogPrintf("keypool added key %d, size=%"PRIszu"\n", nEnd, setKeyPool.size());
+ LogPrintf("keypool added key %d, size=%u\n", nEnd, setKeyPool.size());
}
}
return true;
@@ -1662,21 +1680,6 @@ void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool)
}
}
-int64_t CWallet::AddReserveKey(const CKeyPool& keypool)
-{
- {
- LOCK2(cs_main, cs_wallet);
- CWalletDB walletdb(strWalletFile);
-
- int64_t nIndex = 1 + *(--setKeyPool.end());
- if (!walletdb.WritePool(nIndex, keypool))
- throw runtime_error("AddReserveKey() : writing added key failed");
- setKeyPool.insert(nIndex);
- return nIndex;
- }
- return -1;
-}
-
void CWallet::KeepKey(int64_t nIndex)
{
// Remove from key pool