]>
Commit | Line | Data |
---|---|---|
b2120e22 | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
57702541 | 2 | // Copyright (c) 2009-2014 The Bitcoin developers |
e8ef3da7 | 3 | // Distributed under the MIT/X11 software license, see the accompanying |
3a25a2b9 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
e8ef3da7 | 5 | |
9eace6b1 | 6 | #include "wallet.h" |
51ed9ec9 | 7 | |
10254401 | 8 | #include "base58.h" |
3e1cf9b6 | 9 | #include "checkpoints.h" |
6a86c24d | 10 | #include "coincontrol.h" |
0689f46c | 11 | #include "net.h" |
51ed9ec9 | 12 | |
cae686d3 | 13 | #include <boost/algorithm/string/replace.hpp> |
51ed9ec9 | 14 | #include <openssl/rand.h> |
e8ef3da7 WL |
15 | |
16 | using namespace std; | |
17 | ||
cd7fa8bb | 18 | // Settings |
c6cb21d1 | 19 | CFeeRate payTxFee(DEFAULT_TRANSACTION_FEE); |
1bbca249 | 20 | bool bSpendZeroConfChange = true; |
e8ef3da7 | 21 | |
e8ef3da7 WL |
22 | ////////////////////////////////////////////////////////////////////////////// |
23 | // | |
24 | // mapWallet | |
25 | // | |
26 | ||
d650f96d CM |
27 | struct CompareValueOnly |
28 | { | |
51ed9ec9 BD |
29 | bool operator()(const pair<int64_t, pair<const CWalletTx*, unsigned int> >& t1, |
30 | const pair<int64_t, pair<const CWalletTx*, unsigned int> >& t2) const | |
d650f96d CM |
31 | { |
32 | return t1.first < t2.first; | |
33 | } | |
34 | }; | |
35 | ||
93a18a36 GA |
36 | const CWalletTx* CWallet::GetWalletTx(const uint256& hash) const |
37 | { | |
38 | LOCK(cs_wallet); | |
39 | std::map<uint256, CWalletTx>::const_iterator it = mapWallet.find(hash); | |
40 | if (it == mapWallet.end()) | |
41 | return NULL; | |
42 | return &(it->second); | |
43 | } | |
44 | ||
fd61d6f5 | 45 | CPubKey CWallet::GenerateNewKey() |
9976cf07 | 46 | { |
95691680 | 47 | AssertLockHeld(cs_wallet); // mapKeyMetadata |
439e1497 | 48 | bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets |
38067c18 | 49 | |
9976cf07 | 50 | RandAddSeedPerfmon(); |
dfa23b94 PW |
51 | CKey secret; |
52 | secret.MakeNewKey(fCompressed); | |
38067c18 PW |
53 | |
54 | // Compressed public keys were introduced in version 0.6.0 | |
55 | if (fCompressed) | |
439e1497 | 56 | SetMinVersion(FEATURE_COMPRPUBKEY); |
38067c18 | 57 | |
dfa23b94 | 58 | CPubKey pubkey = secret.GetPubKey(); |
4addb2c0 PW |
59 | |
60 | // Create new metadata | |
51ed9ec9 | 61 | int64_t nCreationTime = GetTime(); |
4addb2c0 PW |
62 | mapKeyMetadata[pubkey.GetID()] = CKeyMetadata(nCreationTime); |
63 | if (!nTimeFirstKey || nCreationTime < nTimeFirstKey) | |
64 | nTimeFirstKey = nCreationTime; | |
65 | ||
dfa23b94 | 66 | if (!AddKeyPubKey(secret, pubkey)) |
9976cf07 | 67 | throw std::runtime_error("CWallet::GenerateNewKey() : AddKey failed"); |
dfa23b94 | 68 | return pubkey; |
9976cf07 | 69 | } |
e8ef3da7 | 70 | |
4addb2c0 | 71 | bool CWallet::AddKeyPubKey(const CKey& secret, const CPubKey &pubkey) |
e8ef3da7 | 72 | { |
95691680 | 73 | AssertLockHeld(cs_wallet); // mapKeyMetadata |
dfa23b94 | 74 | if (!CCryptoKeyStore::AddKeyPubKey(secret, pubkey)) |
acd65016 | 75 | return false; |
e8ef3da7 WL |
76 | if (!fFileBacked) |
77 | return true; | |
dfa23b94 | 78 | if (!IsCrypted()) { |
3869fb89 JG |
79 | return CWalletDB(strWalletFile).WriteKey(pubkey, |
80 | secret.GetPrivKey(), | |
4addb2c0 | 81 | mapKeyMetadata[pubkey.GetID()]); |
dfa23b94 | 82 | } |
84c3c2eb | 83 | return true; |
4e87d341 MC |
84 | } |
85 | ||
3869fb89 | 86 | bool CWallet::AddCryptedKey(const CPubKey &vchPubKey, |
4addb2c0 | 87 | const vector<unsigned char> &vchCryptedSecret) |
4e87d341 MC |
88 | { |
89 | if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret)) | |
90 | return false; | |
91 | if (!fFileBacked) | |
92 | return true; | |
96f34cd5 | 93 | { |
f8dcd5ca | 94 | LOCK(cs_wallet); |
96f34cd5 | 95 | if (pwalletdbEncryption) |
3869fb89 JG |
96 | return pwalletdbEncryption->WriteCryptedKey(vchPubKey, |
97 | vchCryptedSecret, | |
4addb2c0 | 98 | mapKeyMetadata[vchPubKey.GetID()]); |
96f34cd5 | 99 | else |
3869fb89 JG |
100 | return CWalletDB(strWalletFile).WriteCryptedKey(vchPubKey, |
101 | vchCryptedSecret, | |
4addb2c0 | 102 | mapKeyMetadata[vchPubKey.GetID()]); |
96f34cd5 | 103 | } |
0767e691 | 104 | return false; |
4e87d341 MC |
105 | } |
106 | ||
4addb2c0 PW |
107 | bool CWallet::LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &meta) |
108 | { | |
95691680 | 109 | AssertLockHeld(cs_wallet); // mapKeyMetadata |
4addb2c0 PW |
110 | if (meta.nCreateTime && (!nTimeFirstKey || meta.nCreateTime < nTimeFirstKey)) |
111 | nTimeFirstKey = meta.nCreateTime; | |
112 | ||
113 | mapKeyMetadata[pubkey.GetID()] = meta; | |
114 | return true; | |
115 | } | |
116 | ||
2f15e86a GA |
117 | bool CWallet::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret) |
118 | { | |
119 | return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret); | |
120 | } | |
121 | ||
922e8e29 | 122 | bool CWallet::AddCScript(const CScript& redeemScript) |
e679ec96 | 123 | { |
922e8e29 | 124 | if (!CCryptoKeyStore::AddCScript(redeemScript)) |
e679ec96 GA |
125 | return false; |
126 | if (!fFileBacked) | |
127 | return true; | |
922e8e29 | 128 | return CWalletDB(strWalletFile).WriteCScript(Hash160(redeemScript), redeemScript); |
e679ec96 GA |
129 | } |
130 | ||
18116b06 WL |
131 | bool CWallet::LoadCScript(const CScript& redeemScript) |
132 | { | |
133 | /* A sanity check was added in pull #3843 to avoid adding redeemScripts | |
134 | * that never can be redeemed. However, old wallets may still contain | |
135 | * these. Do not add them to the wallet and warn. */ | |
136 | if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE) | |
137 | { | |
138 | std::string strAddr = CBitcoinAddress(redeemScript.GetID()).ToString(); | |
139 | 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", | |
140 | __func__, redeemScript.size(), MAX_SCRIPT_ELEMENT_SIZE, strAddr); | |
141 | return true; | |
142 | } | |
143 | ||
144 | return CCryptoKeyStore::AddCScript(redeemScript); | |
145 | } | |
146 | ||
94f778bd | 147 | bool CWallet::Unlock(const SecureString& strWalletPassphrase) |
4e87d341 | 148 | { |
6cc4a62c GA |
149 | CCrypter crypter; |
150 | CKeyingMaterial vMasterKey; | |
4e87d341 | 151 | |
f8dcd5ca PW |
152 | { |
153 | LOCK(cs_wallet); | |
4e87d341 MC |
154 | BOOST_FOREACH(const MasterKeyMap::value_type& pMasterKey, mapMasterKeys) |
155 | { | |
156 | if(!crypter.SetKeyFromPassphrase(strWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod)) | |
157 | return false; | |
158 | if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey)) | |
92f2c1fe | 159 | continue; // try another master key |
4e87d341 MC |
160 | if (CCryptoKeyStore::Unlock(vMasterKey)) |
161 | return true; | |
162 | } | |
f8dcd5ca | 163 | } |
4e87d341 MC |
164 | return false; |
165 | } | |
166 | ||
94f778bd | 167 | bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase) |
4e87d341 | 168 | { |
6cc4a62c | 169 | bool fWasLocked = IsLocked(); |
4e87d341 | 170 | |
6cc4a62c | 171 | { |
f8dcd5ca | 172 | LOCK(cs_wallet); |
4e87d341 MC |
173 | Lock(); |
174 | ||
175 | CCrypter crypter; | |
176 | CKeyingMaterial vMasterKey; | |
177 | BOOST_FOREACH(MasterKeyMap::value_type& pMasterKey, mapMasterKeys) | |
178 | { | |
179 | if(!crypter.SetKeyFromPassphrase(strOldWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod)) | |
180 | return false; | |
6cc4a62c | 181 | if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey)) |
4e87d341 MC |
182 | return false; |
183 | if (CCryptoKeyStore::Unlock(vMasterKey)) | |
184 | { | |
51ed9ec9 | 185 | int64_t nStartTime = GetTimeMillis(); |
ddebdd9a MC |
186 | crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod); |
187 | pMasterKey.second.nDeriveIterations = pMasterKey.second.nDeriveIterations * (100 / ((double)(GetTimeMillis() - nStartTime))); | |
188 | ||
189 | nStartTime = GetTimeMillis(); | |
190 | crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod); | |
191 | pMasterKey.second.nDeriveIterations = (pMasterKey.second.nDeriveIterations + pMasterKey.second.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2; | |
192 | ||
193 | if (pMasterKey.second.nDeriveIterations < 25000) | |
194 | pMasterKey.second.nDeriveIterations = 25000; | |
195 | ||
881a85a2 | 196 | LogPrintf("Wallet passphrase changed to an nDeriveIterations of %i\n", pMasterKey.second.nDeriveIterations); |
ddebdd9a | 197 | |
4e87d341 MC |
198 | if (!crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod)) |
199 | return false; | |
200 | if (!crypter.Encrypt(vMasterKey, pMasterKey.second.vchCryptedKey)) | |
201 | return false; | |
202 | CWalletDB(strWalletFile).WriteMasterKey(pMasterKey.first, pMasterKey.second); | |
203 | if (fWasLocked) | |
204 | Lock(); | |
205 | return true; | |
206 | } | |
207 | } | |
208 | } | |
6cc4a62c | 209 | |
4e87d341 MC |
210 | return false; |
211 | } | |
212 | ||
ed6d0b5f PW |
213 | void CWallet::SetBestChain(const CBlockLocator& loc) |
214 | { | |
215 | CWalletDB walletdb(strWalletFile); | |
216 | walletdb.WriteBestBlock(loc); | |
217 | } | |
7414733b | 218 | |
439e1497 | 219 | bool CWallet::SetMinVersion(enum WalletFeature nVersion, CWalletDB* pwalletdbIn, bool fExplicit) |
0b807a41 | 220 | { |
ca4cf5cf | 221 | LOCK(cs_wallet); // nWalletVersion |
0b807a41 PW |
222 | if (nWalletVersion >= nVersion) |
223 | return true; | |
224 | ||
439e1497 PW |
225 | // when doing an explicit upgrade, if we pass the max version permitted, upgrade all the way |
226 | if (fExplicit && nVersion > nWalletMaxVersion) | |
227 | nVersion = FEATURE_LATEST; | |
228 | ||
0b807a41 PW |
229 | nWalletVersion = nVersion; |
230 | ||
439e1497 PW |
231 | if (nVersion > nWalletMaxVersion) |
232 | nWalletMaxVersion = nVersion; | |
233 | ||
0b807a41 PW |
234 | if (fFileBacked) |
235 | { | |
236 | CWalletDB* pwalletdb = pwalletdbIn ? pwalletdbIn : new CWalletDB(strWalletFile); | |
0b807a41 PW |
237 | if (nWalletVersion > 40000) |
238 | pwalletdb->WriteMinVersion(nWalletVersion); | |
239 | if (!pwalletdbIn) | |
240 | delete pwalletdb; | |
241 | } | |
242 | ||
243 | return true; | |
244 | } | |
245 | ||
439e1497 PW |
246 | bool CWallet::SetMaxVersion(int nVersion) |
247 | { | |
ca4cf5cf | 248 | LOCK(cs_wallet); // nWalletVersion, nWalletMaxVersion |
439e1497 PW |
249 | // cannot downgrade below current version |
250 | if (nWalletVersion > nVersion) | |
251 | return false; | |
252 | ||
253 | nWalletMaxVersion = nVersion; | |
254 | ||
255 | return true; | |
256 | } | |
257 | ||
731b89b8 GA |
258 | set<uint256> CWallet::GetConflicts(const uint256& txid) const |
259 | { | |
260 | set<uint256> result; | |
261 | AssertLockHeld(cs_wallet); | |
262 | ||
263 | std::map<uint256, CWalletTx>::const_iterator it = mapWallet.find(txid); | |
264 | if (it == mapWallet.end()) | |
265 | return result; | |
266 | const CWalletTx& wtx = it->second; | |
267 | ||
93a18a36 | 268 | std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range; |
731b89b8 GA |
269 | |
270 | BOOST_FOREACH(const CTxIn& txin, wtx.vin) | |
271 | { | |
93a18a36 GA |
272 | if (mapTxSpends.count(txin.prevout) <= 1) |
273 | continue; // No conflict if zero or one spends | |
274 | range = mapTxSpends.equal_range(txin.prevout); | |
275 | for (TxSpends::const_iterator it = range.first; it != range.second; ++it) | |
731b89b8 GA |
276 | result.insert(it->second); |
277 | } | |
278 | return result; | |
279 | } | |
280 | ||
93a18a36 | 281 | void CWallet::SyncMetaData(pair<TxSpends::iterator, TxSpends::iterator> range) |
731b89b8 GA |
282 | { |
283 | // We want all the wallet transactions in range to have the same metadata as | |
284 | // the oldest (smallest nOrderPos). | |
285 | // So: find smallest nOrderPos: | |
286 | ||
287 | int nMinOrderPos = std::numeric_limits<int>::max(); | |
288 | const CWalletTx* copyFrom = NULL; | |
93a18a36 | 289 | for (TxSpends::iterator it = range.first; it != range.second; ++it) |
731b89b8 GA |
290 | { |
291 | const uint256& hash = it->second; | |
292 | int n = mapWallet[hash].nOrderPos; | |
293 | if (n < nMinOrderPos) | |
294 | { | |
295 | nMinOrderPos = n; | |
296 | copyFrom = &mapWallet[hash]; | |
297 | } | |
298 | } | |
299 | // Now copy data from copyFrom to rest: | |
93a18a36 | 300 | for (TxSpends::iterator it = range.first; it != range.second; ++it) |
731b89b8 GA |
301 | { |
302 | const uint256& hash = it->second; | |
303 | CWalletTx* copyTo = &mapWallet[hash]; | |
304 | if (copyFrom == copyTo) continue; | |
305 | copyTo->mapValue = copyFrom->mapValue; | |
306 | copyTo->vOrderForm = copyFrom->vOrderForm; | |
307 | // fTimeReceivedIsTxTime not copied on purpose | |
308 | // nTimeReceived not copied on purpose | |
309 | copyTo->nTimeSmart = copyFrom->nTimeSmart; | |
310 | copyTo->fFromMe = copyFrom->fFromMe; | |
311 | copyTo->strFromAccount = copyFrom->strFromAccount; | |
731b89b8 GA |
312 | // nOrderPos not copied on purpose |
313 | // cached members not copied on purpose | |
314 | } | |
315 | } | |
316 | ||
93a18a36 GA |
317 | // Outpoint is spent if any non-conflicted transaction |
318 | // spends it: | |
319 | bool CWallet::IsSpent(const uint256& hash, unsigned int n) const | |
731b89b8 | 320 | { |
93a18a36 GA |
321 | const COutPoint outpoint(hash, n); |
322 | pair<TxSpends::const_iterator, TxSpends::const_iterator> range; | |
323 | range = mapTxSpends.equal_range(outpoint); | |
731b89b8 | 324 | |
93a18a36 | 325 | for (TxSpends::const_iterator it = range.first; it != range.second; ++it) |
731b89b8 | 326 | { |
93a18a36 GA |
327 | const uint256& wtxid = it->second; |
328 | std::map<uint256, CWalletTx>::const_iterator mit = mapWallet.find(wtxid); | |
329 | if (mit != mapWallet.end() && mit->second.GetDepthInMainChain() >= 0) | |
330 | return true; // Spent | |
731b89b8 | 331 | } |
93a18a36 GA |
332 | return false; |
333 | } | |
334 | ||
335 | void CWallet::AddToSpends(const COutPoint& outpoint, const uint256& wtxid) | |
336 | { | |
337 | mapTxSpends.insert(make_pair(outpoint, wtxid)); | |
338 | ||
339 | pair<TxSpends::iterator, TxSpends::iterator> range; | |
340 | range = mapTxSpends.equal_range(outpoint); | |
341 | SyncMetaData(range); | |
342 | } | |
343 | ||
344 | ||
345 | void CWallet::AddToSpends(const uint256& wtxid) | |
346 | { | |
347 | assert(mapWallet.count(wtxid)); | |
348 | CWalletTx& thisTx = mapWallet[wtxid]; | |
349 | if (thisTx.IsCoinBase()) // Coinbases don't spend anything! | |
350 | return; | |
351 | ||
352 | BOOST_FOREACH(const CTxIn& txin, thisTx.vin) | |
353 | AddToSpends(txin.prevout, wtxid); | |
731b89b8 GA |
354 | } |
355 | ||
94f778bd | 356 | bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase) |
4e87d341 | 357 | { |
6cc4a62c GA |
358 | if (IsCrypted()) |
359 | return false; | |
4e87d341 | 360 | |
6cc4a62c GA |
361 | CKeyingMaterial vMasterKey; |
362 | RandAddSeedPerfmon(); | |
4e87d341 | 363 | |
6cc4a62c GA |
364 | vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE); |
365 | RAND_bytes(&vMasterKey[0], WALLET_CRYPTO_KEY_SIZE); | |
4e87d341 | 366 | |
6cc4a62c | 367 | CMasterKey kMasterKey; |
4e87d341 | 368 | |
6cc4a62c GA |
369 | RandAddSeedPerfmon(); |
370 | kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE); | |
371 | RAND_bytes(&kMasterKey.vchSalt[0], WALLET_CRYPTO_SALT_SIZE); | |
4e87d341 | 372 | |
6cc4a62c | 373 | CCrypter crypter; |
51ed9ec9 | 374 | int64_t nStartTime = GetTimeMillis(); |
6cc4a62c GA |
375 | crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, 25000, kMasterKey.nDerivationMethod); |
376 | kMasterKey.nDeriveIterations = 2500000 / ((double)(GetTimeMillis() - nStartTime)); | |
ddebdd9a | 377 | |
6cc4a62c GA |
378 | nStartTime = GetTimeMillis(); |
379 | crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod); | |
380 | kMasterKey.nDeriveIterations = (kMasterKey.nDeriveIterations + kMasterKey.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2; | |
ddebdd9a | 381 | |
6cc4a62c GA |
382 | if (kMasterKey.nDeriveIterations < 25000) |
383 | kMasterKey.nDeriveIterations = 25000; | |
ddebdd9a | 384 | |
881a85a2 | 385 | LogPrintf("Encrypting Wallet with an nDeriveIterations of %i\n", kMasterKey.nDeriveIterations); |
ddebdd9a | 386 | |
6cc4a62c GA |
387 | if (!crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod)) |
388 | return false; | |
389 | if (!crypter.Encrypt(vMasterKey, kMasterKey.vchCryptedKey)) | |
390 | return false; | |
4e87d341 | 391 | |
6cc4a62c | 392 | { |
f8dcd5ca | 393 | LOCK(cs_wallet); |
4e87d341 MC |
394 | mapMasterKeys[++nMasterKeyMaxID] = kMasterKey; |
395 | if (fFileBacked) | |
396 | { | |
96f34cd5 | 397 | pwalletdbEncryption = new CWalletDB(strWalletFile); |
0fb78eae JG |
398 | if (!pwalletdbEncryption->TxnBegin()) |
399 | return false; | |
96f34cd5 | 400 | pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey); |
4e87d341 MC |
401 | } |
402 | ||
403 | if (!EncryptKeys(vMasterKey)) | |
96f34cd5 MC |
404 | { |
405 | if (fFileBacked) | |
406 | pwalletdbEncryption->TxnAbort(); | |
407 | exit(1); //We now probably have half of our keys encrypted in memory, and half not...die and let the user reload their unencrypted wallet. | |
408 | } | |
409 | ||
0b807a41 | 410 | // Encryption was introduced in version 0.4.0 |
439e1497 | 411 | SetMinVersion(FEATURE_WALLETCRYPT, pwalletdbEncryption, true); |
0b807a41 | 412 | |
96f34cd5 MC |
413 | if (fFileBacked) |
414 | { | |
415 | if (!pwalletdbEncryption->TxnCommit()) | |
416 | exit(1); //We now have keys encrypted in memory, but no on disk...die to avoid confusion and let the user reload their unencrypted wallet. | |
417 | ||
fcfd7ff8 | 418 | delete pwalletdbEncryption; |
96f34cd5 MC |
419 | pwalletdbEncryption = NULL; |
420 | } | |
4e87d341 | 421 | |
37971fcc GA |
422 | Lock(); |
423 | Unlock(strWalletPassphrase); | |
424 | NewKeyPool(); | |
4e87d341 | 425 | Lock(); |
6cc4a62c | 426 | |
d764d916 GA |
427 | // Need to completely rewrite the wallet file; if we don't, bdb might keep |
428 | // bits of the unencrypted private key in slack space in the database file. | |
b2d3b2d6 | 429 | CDB::Rewrite(strWalletFile); |
fe4a6550 | 430 | |
d764d916 | 431 | } |
ab1b288f | 432 | NotifyStatusChanged(this); |
9e9869d0 | 433 | |
4e87d341 | 434 | return true; |
e8ef3da7 WL |
435 | } |
436 | ||
51ed9ec9 | 437 | int64_t CWallet::IncOrderPosNext(CWalletDB *pwalletdb) |
da7b8c12 | 438 | { |
95691680 | 439 | AssertLockHeld(cs_wallet); // nOrderPosNext |
51ed9ec9 | 440 | int64_t nRet = nOrderPosNext++; |
4291e8fe PW |
441 | if (pwalletdb) { |
442 | pwalletdb->WriteOrderPosNext(nOrderPosNext); | |
443 | } else { | |
444 | CWalletDB(strWalletFile).WriteOrderPosNext(nOrderPosNext); | |
445 | } | |
da7b8c12 LD |
446 | return nRet; |
447 | } | |
448 | ||
ddb709e9 | 449 | CWallet::TxItems CWallet::OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount) |
c3f95ef1 | 450 | { |
95691680 | 451 | AssertLockHeld(cs_wallet); // mapWallet |
c3f95ef1 LD |
452 | CWalletDB walletdb(strWalletFile); |
453 | ||
454 | // First: get all CWalletTx and CAccountingEntry into a sorted-by-order multimap. | |
455 | TxItems txOrdered; | |
456 | ||
457 | // Note: maintaining indices in the database of (account,time) --> txid and (account, time) --> acentry | |
458 | // would make this much faster for applications that do this a lot. | |
459 | for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) | |
460 | { | |
461 | CWalletTx* wtx = &((*it).second); | |
462 | txOrdered.insert(make_pair(wtx->nOrderPos, TxPair(wtx, (CAccountingEntry*)0))); | |
463 | } | |
ddb709e9 | 464 | acentries.clear(); |
c3f95ef1 LD |
465 | walletdb.ListAccountCreditDebit(strAccount, acentries); |
466 | BOOST_FOREACH(CAccountingEntry& entry, acentries) | |
467 | { | |
468 | txOrdered.insert(make_pair(entry.nOrderPos, TxPair((CWalletTx*)0, &entry))); | |
469 | } | |
470 | ||
471 | return txOrdered; | |
472 | } | |
473 | ||
95d888a6 PW |
474 | void CWallet::MarkDirty() |
475 | { | |
95d888a6 | 476 | { |
f8dcd5ca | 477 | LOCK(cs_wallet); |
95d888a6 PW |
478 | BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet) |
479 | item.second.MarkDirty(); | |
480 | } | |
481 | } | |
482 | ||
731b89b8 | 483 | bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet) |
e8ef3da7 WL |
484 | { |
485 | uint256 hash = wtxIn.GetHash(); | |
731b89b8 GA |
486 | |
487 | if (fFromLoadWallet) | |
488 | { | |
489 | mapWallet[hash] = wtxIn; | |
09ec3af1 | 490 | mapWallet[hash].BindWallet(this); |
93a18a36 | 491 | AddToSpends(hash); |
731b89b8 GA |
492 | } |
493 | else | |
e8ef3da7 | 494 | { |
f8dcd5ca | 495 | LOCK(cs_wallet); |
e8ef3da7 WL |
496 | // Inserts only if not already there, returns tx inserted or tx found |
497 | pair<map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(make_pair(hash, wtxIn)); | |
498 | CWalletTx& wtx = (*ret.first).second; | |
4c6e2295 | 499 | wtx.BindWallet(this); |
e8ef3da7 WL |
500 | bool fInsertedNew = ret.second; |
501 | if (fInsertedNew) | |
9c7722b7 | 502 | { |
e8ef3da7 | 503 | wtx.nTimeReceived = GetAdjustedTime(); |
da7b8c12 | 504 | wtx.nOrderPos = IncOrderPosNext(); |
c3f95ef1 LD |
505 | |
506 | wtx.nTimeSmart = wtx.nTimeReceived; | |
507 | if (wtxIn.hashBlock != 0) | |
508 | { | |
509 | if (mapBlockIndex.count(wtxIn.hashBlock)) | |
510 | { | |
511 | unsigned int latestNow = wtx.nTimeReceived; | |
512 | unsigned int latestEntry = 0; | |
513 | { | |
514 | // Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future | |
51ed9ec9 | 515 | int64_t latestTolerated = latestNow + 300; |
ddb709e9 LD |
516 | std::list<CAccountingEntry> acentries; |
517 | TxItems txOrdered = OrderedTxItems(acentries); | |
c3f95ef1 LD |
518 | for (TxItems::reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it) |
519 | { | |
520 | CWalletTx *const pwtx = (*it).second.first; | |
521 | if (pwtx == &wtx) | |
522 | continue; | |
523 | CAccountingEntry *const pacentry = (*it).second.second; | |
51ed9ec9 | 524 | int64_t nSmartTime; |
c3f95ef1 LD |
525 | if (pwtx) |
526 | { | |
527 | nSmartTime = pwtx->nTimeSmart; | |
528 | if (!nSmartTime) | |
529 | nSmartTime = pwtx->nTimeReceived; | |
530 | } | |
531 | else | |
532 | nSmartTime = pacentry->nTime; | |
533 | if (nSmartTime <= latestTolerated) | |
534 | { | |
535 | latestEntry = nSmartTime; | |
536 | if (nSmartTime > latestNow) | |
537 | latestNow = nSmartTime; | |
538 | break; | |
539 | } | |
540 | } | |
541 | } | |
542 | ||
543 | unsigned int& blocktime = mapBlockIndex[wtxIn.hashBlock]->nTime; | |
544 | wtx.nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow)); | |
545 | } | |
546 | else | |
881a85a2 | 547 | LogPrintf("AddToWallet() : found %s in block %s not in index\n", |
7d9d134b WL |
548 | wtxIn.GetHash().ToString(), |
549 | wtxIn.hashBlock.ToString()); | |
c3f95ef1 | 550 | } |
93a18a36 | 551 | AddToSpends(hash); |
9c7722b7 | 552 | } |
e8ef3da7 WL |
553 | |
554 | bool fUpdated = false; | |
555 | if (!fInsertedNew) | |
556 | { | |
557 | // Merge | |
558 | if (wtxIn.hashBlock != 0 && wtxIn.hashBlock != wtx.hashBlock) | |
559 | { | |
560 | wtx.hashBlock = wtxIn.hashBlock; | |
561 | fUpdated = true; | |
562 | } | |
563 | if (wtxIn.nIndex != -1 && (wtxIn.vMerkleBranch != wtx.vMerkleBranch || wtxIn.nIndex != wtx.nIndex)) | |
564 | { | |
565 | wtx.vMerkleBranch = wtxIn.vMerkleBranch; | |
566 | wtx.nIndex = wtxIn.nIndex; | |
567 | fUpdated = true; | |
568 | } | |
569 | if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe) | |
570 | { | |
571 | wtx.fFromMe = wtxIn.fFromMe; | |
572 | fUpdated = true; | |
573 | } | |
e8ef3da7 WL |
574 | } |
575 | ||
576 | //// debug print | |
7d9d134b | 577 | LogPrintf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : "")); |
e8ef3da7 WL |
578 | |
579 | // Write to disk | |
580 | if (fInsertedNew || fUpdated) | |
581 | if (!wtx.WriteToDisk()) | |
582 | return false; | |
ee4b170c | 583 | |
93a18a36 GA |
584 | // Break debit/credit balance caches: |
585 | wtx.MarkDirty(); | |
e8ef3da7 | 586 | |
fe4a6550 WL |
587 | // Notify UI of new or updated transaction |
588 | NotifyTransactionChanged(this, hash, fInsertedNew ? CT_NEW : CT_UPDATED); | |
cae686d3 | 589 | |
590 | // notify an external script when a wallet transaction comes in or is updated | |
591 | std::string strCmd = GetArg("-walletnotify", ""); | |
592 | ||
593 | if ( !strCmd.empty()) | |
594 | { | |
595 | boost::replace_all(strCmd, "%s", wtxIn.GetHash().GetHex()); | |
596 | boost::thread t(runCommand, strCmd); // thread runs free | |
597 | } | |
598 | ||
fe4a6550 | 599 | } |
e8ef3da7 WL |
600 | return true; |
601 | } | |
602 | ||
d825e6a3 PW |
603 | // Add a transaction to the wallet, or update it. |
604 | // pblock is optional, but should be provided if the transaction is known to be in a block. | |
605 | // If fUpdate is true, existing transactions will be updated. | |
00588c3f | 606 | bool CWallet::AddToWalletIfInvolvingMe(const uint256 &hash, const CTransaction& tx, const CBlock* pblock, bool fUpdate) |
e8ef3da7 | 607 | { |
e8ef3da7 | 608 | { |
53d56881 | 609 | AssertLockHeld(cs_wallet); |
6cc4a62c GA |
610 | bool fExisted = mapWallet.count(hash); |
611 | if (fExisted && !fUpdate) return false; | |
612 | if (fExisted || IsMine(tx) || IsFromMe(tx)) | |
613 | { | |
614 | CWalletTx wtx(this,tx); | |
615 | // Get merkle branch if transaction was found in a block | |
616 | if (pblock) | |
617 | wtx.SetMerkleBranch(pblock); | |
618 | return AddToWallet(wtx); | |
619 | } | |
e8ef3da7 | 620 | } |
e8ef3da7 WL |
621 | return false; |
622 | } | |
623 | ||
93a18a36 GA |
624 | void CWallet::SyncTransaction(const uint256 &hash, const CTransaction& tx, const CBlock* pblock) |
625 | { | |
55a1db4f | 626 | LOCK2(cs_main, cs_wallet); |
53d56881 | 627 | if (!AddToWalletIfInvolvingMe(hash, tx, pblock, true)) |
93a18a36 GA |
628 | return; // Not one of ours |
629 | ||
630 | // If a transaction changes 'conflicted' state, that changes the balance | |
631 | // available of the outputs it spends. So force those to be | |
632 | // recomputed, also: | |
633 | BOOST_FOREACH(const CTxIn& txin, tx.vin) | |
634 | { | |
635 | if (mapWallet.count(txin.prevout.hash)) | |
636 | mapWallet[txin.prevout.hash].MarkDirty(); | |
637 | } | |
00588c3f PW |
638 | } |
639 | ||
640 | void CWallet::EraseFromWallet(const uint256 &hash) | |
e8ef3da7 WL |
641 | { |
642 | if (!fFileBacked) | |
00588c3f | 643 | return; |
e8ef3da7 | 644 | { |
f8dcd5ca | 645 | LOCK(cs_wallet); |
e8ef3da7 WL |
646 | if (mapWallet.erase(hash)) |
647 | CWalletDB(strWalletFile).EraseTx(hash); | |
648 | } | |
00588c3f | 649 | return; |
e8ef3da7 WL |
650 | } |
651 | ||
652 | ||
653 | bool CWallet::IsMine(const CTxIn &txin) const | |
654 | { | |
e8ef3da7 | 655 | { |
f8dcd5ca | 656 | LOCK(cs_wallet); |
e8ef3da7 WL |
657 | map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash); |
658 | if (mi != mapWallet.end()) | |
659 | { | |
660 | const CWalletTx& prev = (*mi).second; | |
661 | if (txin.prevout.n < prev.vout.size()) | |
662 | if (IsMine(prev.vout[txin.prevout.n])) | |
663 | return true; | |
664 | } | |
665 | } | |
666 | return false; | |
667 | } | |
668 | ||
51ed9ec9 | 669 | int64_t CWallet::GetDebit(const CTxIn &txin) const |
e8ef3da7 | 670 | { |
e8ef3da7 | 671 | { |
f8dcd5ca | 672 | LOCK(cs_wallet); |
e8ef3da7 WL |
673 | map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash); |
674 | if (mi != mapWallet.end()) | |
675 | { | |
676 | const CWalletTx& prev = (*mi).second; | |
677 | if (txin.prevout.n < prev.vout.size()) | |
678 | if (IsMine(prev.vout[txin.prevout.n])) | |
679 | return prev.vout[txin.prevout.n].nValue; | |
680 | } | |
681 | } | |
682 | return 0; | |
683 | } | |
684 | ||
e679ec96 GA |
685 | bool CWallet::IsChange(const CTxOut& txout) const |
686 | { | |
10254401 | 687 | CTxDestination address; |
2a45a494 GA |
688 | |
689 | // TODO: fix handling of 'change' outputs. The assumption is that any | |
690 | // payment to a TX_PUBKEYHASH that is mine but isn't in the address book | |
691 | // is change. That assumption is likely to break when we implement multisignature | |
692 | // wallets that return change back into a multi-signature-protected address; | |
693 | // a better way of identifying which outputs are 'the send' and which are | |
694 | // 'the change' will need to be implemented (maybe extend CWalletTx to remember | |
695 | // which output, if any, was change). | |
10254401 | 696 | if (ExtractDestination(txout.scriptPubKey, address) && ::IsMine(*this, address)) |
f8dcd5ca PW |
697 | { |
698 | LOCK(cs_wallet); | |
699 | if (!mapAddressBook.count(address)) | |
700 | return true; | |
701 | } | |
e679ec96 GA |
702 | return false; |
703 | } | |
704 | ||
51ed9ec9 | 705 | int64_t CWalletTx::GetTxTime() const |
e8ef3da7 | 706 | { |
51ed9ec9 | 707 | int64_t n = nTimeSmart; |
c3f95ef1 | 708 | return n ? n : nTimeReceived; |
e8ef3da7 WL |
709 | } |
710 | ||
711 | int CWalletTx::GetRequestCount() const | |
712 | { | |
713 | // Returns -1 if it wasn't being tracked | |
714 | int nRequests = -1; | |
e8ef3da7 | 715 | { |
f8dcd5ca | 716 | LOCK(pwallet->cs_wallet); |
e8ef3da7 WL |
717 | if (IsCoinBase()) |
718 | { | |
719 | // Generated block | |
720 | if (hashBlock != 0) | |
721 | { | |
722 | map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock); | |
723 | if (mi != pwallet->mapRequestCount.end()) | |
724 | nRequests = (*mi).second; | |
725 | } | |
726 | } | |
727 | else | |
728 | { | |
729 | // Did anyone request this transaction? | |
730 | map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(GetHash()); | |
731 | if (mi != pwallet->mapRequestCount.end()) | |
732 | { | |
733 | nRequests = (*mi).second; | |
734 | ||
735 | // How about the block it's in? | |
736 | if (nRequests == 0 && hashBlock != 0) | |
737 | { | |
738 | map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock); | |
739 | if (mi != pwallet->mapRequestCount.end()) | |
740 | nRequests = (*mi).second; | |
741 | else | |
742 | nRequests = 1; // If it's in someone else's block it must have got out | |
743 | } | |
744 | } | |
745 | } | |
746 | } | |
747 | return nRequests; | |
748 | } | |
749 | ||
51ed9ec9 BD |
750 | void CWalletTx::GetAmounts(list<pair<CTxDestination, int64_t> >& listReceived, |
751 | list<pair<CTxDestination, int64_t> >& listSent, int64_t& nFee, string& strSentAccount) const | |
e8ef3da7 | 752 | { |
e07c8e91 | 753 | nFee = 0; |
e8ef3da7 WL |
754 | listReceived.clear(); |
755 | listSent.clear(); | |
756 | strSentAccount = strFromAccount; | |
757 | ||
e8ef3da7 | 758 | // Compute fee: |
51ed9ec9 | 759 | int64_t nDebit = GetDebit(); |
e8ef3da7 WL |
760 | if (nDebit > 0) // debit>0 means we signed/sent this transaction |
761 | { | |
0733c1bd | 762 | int64_t nValueOut = GetValueOut(); |
e8ef3da7 WL |
763 | nFee = nDebit - nValueOut; |
764 | } | |
765 | ||
e679ec96 | 766 | // Sent/received. |
e8ef3da7 WL |
767 | BOOST_FOREACH(const CTxOut& txout, vout) |
768 | { | |
96ed6821 LD |
769 | bool fIsMine; |
770 | // Only need to handle txouts if AT LEAST one of these is true: | |
771 | // 1) they debit from us (sent) | |
772 | // 2) the output is to us (received) | |
773 | if (nDebit > 0) | |
774 | { | |
775 | // Don't report 'change' txouts | |
776 | if (pwallet->IsChange(txout)) | |
777 | continue; | |
778 | fIsMine = pwallet->IsMine(txout); | |
779 | } | |
780 | else if (!(fIsMine = pwallet->IsMine(txout))) | |
781 | continue; | |
782 | ||
783 | // In either case, we need to get the destination address | |
10254401 | 784 | CTxDestination address; |
10254401 | 785 | if (!ExtractDestination(txout.scriptPubKey, address)) |
e8ef3da7 | 786 | { |
881a85a2 | 787 | LogPrintf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n", |
7d9d134b | 788 | this->GetHash().ToString()); |
96ed6821 | 789 | address = CNoDestination(); |
e8ef3da7 WL |
790 | } |
791 | ||
96ed6821 | 792 | // If we are debited by the transaction, add the output as a "sent" entry |
e8ef3da7 WL |
793 | if (nDebit > 0) |
794 | listSent.push_back(make_pair(address, txout.nValue)); | |
795 | ||
96ed6821 LD |
796 | // If we are receiving the output, add it as a "received" entry |
797 | if (fIsMine) | |
e8ef3da7 WL |
798 | listReceived.push_back(make_pair(address, txout.nValue)); |
799 | } | |
800 | ||
801 | } | |
802 | ||
51ed9ec9 BD |
803 | void CWalletTx::GetAccountAmounts(const string& strAccount, int64_t& nReceived, |
804 | int64_t& nSent, int64_t& nFee) const | |
e8ef3da7 | 805 | { |
e07c8e91 | 806 | nReceived = nSent = nFee = 0; |
e8ef3da7 | 807 | |
51ed9ec9 | 808 | int64_t allFee; |
e8ef3da7 | 809 | string strSentAccount; |
51ed9ec9 BD |
810 | list<pair<CTxDestination, int64_t> > listReceived; |
811 | list<pair<CTxDestination, int64_t> > listSent; | |
e07c8e91 | 812 | GetAmounts(listReceived, listSent, allFee, strSentAccount); |
e8ef3da7 | 813 | |
e8ef3da7 WL |
814 | if (strAccount == strSentAccount) |
815 | { | |
51ed9ec9 | 816 | BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64_t)& s, listSent) |
e8ef3da7 WL |
817 | nSent += s.second; |
818 | nFee = allFee; | |
819 | } | |
e8ef3da7 | 820 | { |
f8dcd5ca | 821 | LOCK(pwallet->cs_wallet); |
51ed9ec9 | 822 | BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64_t)& r, listReceived) |
e8ef3da7 WL |
823 | { |
824 | if (pwallet->mapAddressBook.count(r.first)) | |
825 | { | |
61885513 GA |
826 | map<CTxDestination, CAddressBookData>::const_iterator mi = pwallet->mapAddressBook.find(r.first); |
827 | if (mi != pwallet->mapAddressBook.end() && (*mi).second.name == strAccount) | |
e8ef3da7 WL |
828 | nReceived += r.second; |
829 | } | |
830 | else if (strAccount.empty()) | |
831 | { | |
832 | nReceived += r.second; | |
833 | } | |
834 | } | |
835 | } | |
836 | } | |
837 | ||
722fa283 | 838 | |
e8ef3da7 WL |
839 | bool CWalletTx::WriteToDisk() |
840 | { | |
841 | return CWalletDB(pwallet->strWalletFile).WriteTx(GetHash(), *this); | |
842 | } | |
843 | ||
d825e6a3 PW |
844 | // Scan the block chain (starting in pindexStart) for transactions |
845 | // from or to us. If fUpdate is true, found transactions that already | |
846 | // exist in the wallet will be updated. | |
e8ef3da7 WL |
847 | int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate) |
848 | { | |
849 | int ret = 0; | |
75b8953a | 850 | int64_t nNow = GetTime(); |
e8ef3da7 WL |
851 | |
852 | CBlockIndex* pindex = pindexStart; | |
e8ef3da7 | 853 | { |
55a1db4f | 854 | LOCK2(cs_main, cs_wallet); |
39278369 CL |
855 | |
856 | // no need to read and scan block, if block was created before | |
857 | // our wallet birthday (as adjusted for block time variability) | |
858 | while (pindex && nTimeFirstKey && (pindex->nTime < (nTimeFirstKey - 7200))) | |
859 | pindex = chainActive.Next(pindex); | |
860 | ||
861 | ShowProgress(_("Rescanning..."), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup | |
862 | double dProgressStart = Checkpoints::GuessVerificationProgress(pindex, false); | |
863 | double dProgressTip = Checkpoints::GuessVerificationProgress(chainActive.Tip(), false); | |
e8ef3da7 WL |
864 | while (pindex) |
865 | { | |
39278369 CL |
866 | if (pindex->nHeight % 100 == 0 && dProgressTip - dProgressStart > 0.0) |
867 | ShowProgress(_("Rescanning..."), std::max(1, std::min(99, (int)((Checkpoints::GuessVerificationProgress(pindex, false) - dProgressStart) / (dProgressTip - dProgressStart) * 100)))); | |
8da9dd07 | 868 | |
e8ef3da7 | 869 | CBlock block; |
7db120d5 | 870 | ReadBlockFromDisk(block, pindex); |
e8ef3da7 WL |
871 | BOOST_FOREACH(CTransaction& tx, block.vtx) |
872 | { | |
64dd46fd | 873 | if (AddToWalletIfInvolvingMe(tx.GetHash(), tx, &block, fUpdate)) |
e8ef3da7 WL |
874 | ret++; |
875 | } | |
4c6d41b8 | 876 | pindex = chainActive.Next(pindex); |
75b8953a B |
877 | if (GetTime() >= nNow + 60) { |
878 | nNow = GetTime(); | |
879 | LogPrintf("Still rescanning. At block %d. Progress=%f\n", pindex->nHeight, Checkpoints::GuessVerificationProgress(pindex)); | |
880 | } | |
e8ef3da7 | 881 | } |
39278369 | 882 | ShowProgress(_("Rescanning..."), 100); // hide progress dialog in GUI |
e8ef3da7 WL |
883 | } |
884 | return ret; | |
885 | } | |
886 | ||
887 | void CWallet::ReacceptWalletTransactions() | |
888 | { | |
55a1db4f | 889 | LOCK2(cs_main, cs_wallet); |
93a18a36 | 890 | BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet) |
e8ef3da7 | 891 | { |
93a18a36 GA |
892 | const uint256& wtxid = item.first; |
893 | CWalletTx& wtx = item.second; | |
894 | assert(wtx.GetHash() == wtxid); | |
e8ef3da7 | 895 | |
93a18a36 GA |
896 | int nDepth = wtx.GetDepthInMainChain(); |
897 | ||
898 | if (!wtx.IsCoinBase() && nDepth < 0) | |
e8ef3da7 | 899 | { |
93a18a36 GA |
900 | // Try to add to memory pool |
901 | LOCK(mempool.cs); | |
902 | wtx.AcceptToMemoryPool(false); | |
e8ef3da7 WL |
903 | } |
904 | } | |
905 | } | |
906 | ||
ae8bfd12 | 907 | void CWalletTx::RelayWalletTransaction() |
e8ef3da7 | 908 | { |
e8ef3da7 WL |
909 | if (!IsCoinBase()) |
910 | { | |
5eaf91a4 PW |
911 | if (GetDepthInMainChain() == 0) { |
912 | uint256 hash = GetHash(); | |
7d9d134b | 913 | LogPrintf("Relaying wtx %s\n", hash.ToString()); |
269d9c64 | 914 | RelayTransaction((CTransaction)*this, hash); |
e8ef3da7 WL |
915 | } |
916 | } | |
917 | } | |
918 | ||
731b89b8 GA |
919 | set<uint256> CWalletTx::GetConflicts() const |
920 | { | |
921 | set<uint256> result; | |
922 | if (pwallet != NULL) | |
923 | { | |
924 | uint256 myHash = GetHash(); | |
925 | result = pwallet->GetConflicts(myHash); | |
926 | result.erase(myHash); | |
927 | } | |
928 | return result; | |
929 | } | |
930 | ||
e8ef3da7 WL |
931 | void CWallet::ResendWalletTransactions() |
932 | { | |
933 | // Do this infrequently and randomly to avoid giving away | |
934 | // that these are our transactions. | |
203d1ae6 | 935 | if (GetTime() < nNextResend) |
e8ef3da7 | 936 | return; |
203d1ae6 LD |
937 | bool fFirst = (nNextResend == 0); |
938 | nNextResend = GetTime() + GetRand(30 * 60); | |
e8ef3da7 WL |
939 | if (fFirst) |
940 | return; | |
941 | ||
942 | // Only do it if there's been a new block since last time | |
203d1ae6 | 943 | if (nTimeBestReceived < nLastResend) |
e8ef3da7 | 944 | return; |
203d1ae6 | 945 | nLastResend = GetTime(); |
e8ef3da7 WL |
946 | |
947 | // Rebroadcast any of our txes that aren't in a block yet | |
881a85a2 | 948 | LogPrintf("ResendWalletTransactions()\n"); |
e8ef3da7 | 949 | { |
f8dcd5ca | 950 | LOCK(cs_wallet); |
e8ef3da7 WL |
951 | // Sort them in chronological order |
952 | multimap<unsigned int, CWalletTx*> mapSorted; | |
953 | BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet) | |
954 | { | |
955 | CWalletTx& wtx = item.second; | |
956 | // Don't rebroadcast until it's had plenty of time that | |
957 | // it should have gotten in already by now. | |
51ed9ec9 | 958 | if (nTimeBestReceived - (int64_t)wtx.nTimeReceived > 5 * 60) |
e8ef3da7 WL |
959 | mapSorted.insert(make_pair(wtx.nTimeReceived, &wtx)); |
960 | } | |
961 | BOOST_FOREACH(PAIRTYPE(const unsigned int, CWalletTx*)& item, mapSorted) | |
962 | { | |
963 | CWalletTx& wtx = *item.second; | |
ae8bfd12 | 964 | wtx.RelayWalletTransaction(); |
e8ef3da7 WL |
965 | } |
966 | } | |
967 | } | |
968 | ||
969 | ||
970 | ||
971 | ||
972 | ||
973 | ||
974 | ////////////////////////////////////////////////////////////////////////////// | |
975 | // | |
976 | // Actions | |
977 | // | |
978 | ||
979 | ||
51ed9ec9 | 980 | int64_t CWallet::GetBalance() const |
e8ef3da7 | 981 | { |
51ed9ec9 | 982 | int64_t nTotal = 0; |
e8ef3da7 | 983 | { |
55a1db4f | 984 | LOCK2(cs_main, cs_wallet); |
e8ef3da7 WL |
985 | for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) |
986 | { | |
987 | const CWalletTx* pcoin = &(*it).second; | |
0542619d | 988 | if (pcoin->IsTrusted()) |
8fdb7e10 | 989 | nTotal += pcoin->GetAvailableCredit(); |
e8ef3da7 WL |
990 | } |
991 | } | |
992 | ||
e8ef3da7 WL |
993 | return nTotal; |
994 | } | |
995 | ||
51ed9ec9 | 996 | int64_t CWallet::GetUnconfirmedBalance() const |
df5ccbd2 | 997 | { |
51ed9ec9 | 998 | int64_t nTotal = 0; |
df5ccbd2 | 999 | { |
55a1db4f | 1000 | LOCK2(cs_main, cs_wallet); |
df5ccbd2 WL |
1001 | for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) |
1002 | { | |
1003 | const CWalletTx* pcoin = &(*it).second; | |
731b89b8 | 1004 | if (!IsFinalTx(*pcoin) || (!pcoin->IsTrusted() && pcoin->GetDepthInMainChain() == 0)) |
8fdb7e10 | 1005 | nTotal += pcoin->GetAvailableCredit(); |
1006 | } | |
1007 | } | |
1008 | return nTotal; | |
1009 | } | |
1010 | ||
51ed9ec9 | 1011 | int64_t CWallet::GetImmatureBalance() const |
8fdb7e10 | 1012 | { |
51ed9ec9 | 1013 | int64_t nTotal = 0; |
8fdb7e10 | 1014 | { |
55a1db4f | 1015 | LOCK2(cs_main, cs_wallet); |
8fdb7e10 | 1016 | for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) |
1017 | { | |
966a0e8c PK |
1018 | const CWalletTx* pcoin = &(*it).second; |
1019 | nTotal += pcoin->GetImmatureCredit(); | |
df5ccbd2 WL |
1020 | } |
1021 | } | |
1022 | return nTotal; | |
1023 | } | |
e8ef3da7 | 1024 | |
831f59ce | 1025 | // populate vCoins with vector of spendable COutputs |
6a86c24d | 1026 | void CWallet::AvailableCoins(vector<COutput>& vCoins, bool fOnlyConfirmed, const CCoinControl *coinControl) const |
9b0369c7 CM |
1027 | { |
1028 | vCoins.clear(); | |
1029 | ||
1030 | { | |
ea3acaf3 | 1031 | LOCK2(cs_main, cs_wallet); |
9b0369c7 CM |
1032 | for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) |
1033 | { | |
93a18a36 | 1034 | const uint256& wtxid = it->first; |
9b0369c7 CM |
1035 | const CWalletTx* pcoin = &(*it).second; |
1036 | ||
05df3fc6 | 1037 | if (!IsFinalTx(*pcoin)) |
a2709fad GA |
1038 | continue; |
1039 | ||
0542619d | 1040 | if (fOnlyConfirmed && !pcoin->IsTrusted()) |
9b0369c7 CM |
1041 | continue; |
1042 | ||
1043 | if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0) | |
1044 | continue; | |
1045 | ||
2b72d46f GA |
1046 | int nDepth = pcoin->GetDepthInMainChain(); |
1047 | if (nDepth < 0) | |
1048 | continue; | |
1049 | ||
fdbb537d | 1050 | for (unsigned int i = 0; i < pcoin->vout.size(); i++) { |
93a18a36 | 1051 | if (!(IsSpent(wtxid, i)) && IsMine(pcoin->vout[i]) && |
6a86c24d CL |
1052 | !IsLockedCoin((*it).first, i) && pcoin->vout[i].nValue > 0 && |
1053 | (!coinControl || !coinControl->HasSelected() || coinControl->IsSelected((*it).first, i))) | |
2b72d46f | 1054 | vCoins.push_back(COutput(pcoin, i, nDepth)); |
fdbb537d | 1055 | } |
9b0369c7 CM |
1056 | } |
1057 | } | |
1058 | } | |
1059 | ||
51ed9ec9 BD |
1060 | static void ApproximateBestSubset(vector<pair<int64_t, pair<const CWalletTx*,unsigned int> > >vValue, int64_t nTotalLower, int64_t nTargetValue, |
1061 | vector<char>& vfBest, int64_t& nBest, int iterations = 1000) | |
831f59ce CM |
1062 | { |
1063 | vector<char> vfIncluded; | |
1064 | ||
1065 | vfBest.assign(vValue.size(), true); | |
1066 | nBest = nTotalLower; | |
1067 | ||
907a2aa4 GM |
1068 | seed_insecure_rand(); |
1069 | ||
831f59ce CM |
1070 | for (int nRep = 0; nRep < iterations && nBest != nTargetValue; nRep++) |
1071 | { | |
1072 | vfIncluded.assign(vValue.size(), false); | |
51ed9ec9 | 1073 | int64_t nTotal = 0; |
831f59ce CM |
1074 | bool fReachedTarget = false; |
1075 | for (int nPass = 0; nPass < 2 && !fReachedTarget; nPass++) | |
1076 | { | |
1077 | for (unsigned int i = 0; i < vValue.size(); i++) | |
1078 | { | |
907a2aa4 GM |
1079 | //The solver here uses a randomized algorithm, |
1080 | //the randomness serves no real security purpose but is just | |
1081 | //needed to prevent degenerate behavior and it is important | |
1082 | //that the rng fast. We do not use a constant random sequence, | |
1083 | //because there may be some privacy improvement by making | |
1084 | //the selection random. | |
1085 | if (nPass == 0 ? insecure_rand()&1 : !vfIncluded[i]) | |
831f59ce CM |
1086 | { |
1087 | nTotal += vValue[i].first; | |
1088 | vfIncluded[i] = true; | |
1089 | if (nTotal >= nTargetValue) | |
1090 | { | |
1091 | fReachedTarget = true; | |
1092 | if (nTotal < nBest) | |
1093 | { | |
1094 | nBest = nTotal; | |
1095 | vfBest = vfIncluded; | |
1096 | } | |
1097 | nTotal -= vValue[i].first; | |
1098 | vfIncluded[i] = false; | |
1099 | } | |
1100 | } | |
1101 | } | |
1102 | } | |
1103 | } | |
1104 | } | |
1105 | ||
51ed9ec9 BD |
1106 | bool CWallet::SelectCoinsMinConf(int64_t nTargetValue, int nConfMine, int nConfTheirs, vector<COutput> vCoins, |
1107 | set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet) const | |
e8ef3da7 WL |
1108 | { |
1109 | setCoinsRet.clear(); | |
1110 | nValueRet = 0; | |
1111 | ||
1112 | // List of values less than target | |
51ed9ec9 BD |
1113 | pair<int64_t, pair<const CWalletTx*,unsigned int> > coinLowestLarger; |
1114 | coinLowestLarger.first = std::numeric_limits<int64_t>::max(); | |
e8ef3da7 | 1115 | coinLowestLarger.second.first = NULL; |
51ed9ec9 BD |
1116 | vector<pair<int64_t, pair<const CWalletTx*,unsigned int> > > vValue; |
1117 | int64_t nTotalLower = 0; | |
e8ef3da7 | 1118 | |
e333ab56 CM |
1119 | random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt); |
1120 | ||
9b0369c7 | 1121 | BOOST_FOREACH(COutput output, vCoins) |
e8ef3da7 | 1122 | { |
9b0369c7 | 1123 | const CWalletTx *pcoin = output.tx; |
e8ef3da7 | 1124 | |
9b0369c7 CM |
1125 | if (output.nDepth < (pcoin->IsFromMe() ? nConfMine : nConfTheirs)) |
1126 | continue; | |
e8ef3da7 | 1127 | |
9b0369c7 | 1128 | int i = output.i; |
51ed9ec9 | 1129 | int64_t n = pcoin->vout[i].nValue; |
e8ef3da7 | 1130 | |
51ed9ec9 | 1131 | pair<int64_t,pair<const CWalletTx*,unsigned int> > coin = make_pair(n,make_pair(pcoin, i)); |
e8ef3da7 | 1132 | |
9b0369c7 CM |
1133 | if (n == nTargetValue) |
1134 | { | |
1135 | setCoinsRet.insert(coin.second); | |
1136 | nValueRet += coin.first; | |
1137 | return true; | |
1138 | } | |
1139 | else if (n < nTargetValue + CENT) | |
1140 | { | |
1141 | vValue.push_back(coin); | |
1142 | nTotalLower += n; | |
1143 | } | |
1144 | else if (n < coinLowestLarger.first) | |
1145 | { | |
1146 | coinLowestLarger = coin; | |
e8ef3da7 WL |
1147 | } |
1148 | } | |
1149 | ||
831f59ce | 1150 | if (nTotalLower == nTargetValue) |
e8ef3da7 | 1151 | { |
c376ac35 | 1152 | for (unsigned int i = 0; i < vValue.size(); ++i) |
e8ef3da7 WL |
1153 | { |
1154 | setCoinsRet.insert(vValue[i].second); | |
1155 | nValueRet += vValue[i].first; | |
1156 | } | |
1157 | return true; | |
1158 | } | |
1159 | ||
831f59ce | 1160 | if (nTotalLower < nTargetValue) |
e8ef3da7 WL |
1161 | { |
1162 | if (coinLowestLarger.second.first == NULL) | |
1163 | return false; | |
1164 | setCoinsRet.insert(coinLowestLarger.second); | |
1165 | nValueRet += coinLowestLarger.first; | |
1166 | return true; | |
1167 | } | |
1168 | ||
e8ef3da7 | 1169 | // Solve subset sum by stochastic approximation |
d650f96d | 1170 | sort(vValue.rbegin(), vValue.rend(), CompareValueOnly()); |
831f59ce | 1171 | vector<char> vfBest; |
51ed9ec9 | 1172 | int64_t nBest; |
e8ef3da7 | 1173 | |
831f59ce CM |
1174 | ApproximateBestSubset(vValue, nTotalLower, nTargetValue, vfBest, nBest, 1000); |
1175 | if (nBest != nTargetValue && nTotalLower >= nTargetValue + CENT) | |
1176 | ApproximateBestSubset(vValue, nTotalLower, nTargetValue + CENT, vfBest, nBest, 1000); | |
e8ef3da7 | 1177 | |
831f59ce CM |
1178 | // If we have a bigger coin and (either the stochastic approximation didn't find a good solution, |
1179 | // or the next bigger coin is closer), return the bigger coin | |
1180 | if (coinLowestLarger.second.first && | |
1181 | ((nBest != nTargetValue && nBest < nTargetValue + CENT) || coinLowestLarger.first <= nBest)) | |
e8ef3da7 WL |
1182 | { |
1183 | setCoinsRet.insert(coinLowestLarger.second); | |
1184 | nValueRet += coinLowestLarger.first; | |
1185 | } | |
1186 | else { | |
c376ac35 | 1187 | for (unsigned int i = 0; i < vValue.size(); i++) |
e8ef3da7 WL |
1188 | if (vfBest[i]) |
1189 | { | |
1190 | setCoinsRet.insert(vValue[i].second); | |
1191 | nValueRet += vValue[i].first; | |
1192 | } | |
1193 | ||
faaeae1e | 1194 | LogPrint("selectcoins", "SelectCoins() best subset: "); |
c376ac35 | 1195 | for (unsigned int i = 0; i < vValue.size(); i++) |
e8ef3da7 | 1196 | if (vfBest[i]) |
7d9d134b WL |
1197 | LogPrint("selectcoins", "%s ", FormatMoney(vValue[i].first)); |
1198 | LogPrint("selectcoins", "total %s\n", FormatMoney(nBest)); | |
e8ef3da7 WL |
1199 | } |
1200 | ||
1201 | return true; | |
1202 | } | |
1203 | ||
6a86c24d | 1204 | bool CWallet::SelectCoins(int64_t nTargetValue, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet, const CCoinControl* coinControl) const |
e8ef3da7 | 1205 | { |
9b0369c7 | 1206 | vector<COutput> vCoins; |
6a86c24d CL |
1207 | AvailableCoins(vCoins, true, coinControl); |
1208 | ||
1209 | // coin control -> return all selected outputs (we want all selected to go into the transaction for sure) | |
1210 | if (coinControl && coinControl->HasSelected()) | |
1211 | { | |
1212 | BOOST_FOREACH(const COutput& out, vCoins) | |
1213 | { | |
1214 | nValueRet += out.tx->vout[out.i].nValue; | |
1215 | setCoinsRet.insert(make_pair(out.tx, out.i)); | |
1216 | } | |
1217 | return (nValueRet >= nTargetValue); | |
1218 | } | |
9b0369c7 CM |
1219 | |
1220 | return (SelectCoinsMinConf(nTargetValue, 1, 6, vCoins, setCoinsRet, nValueRet) || | |
1221 | SelectCoinsMinConf(nTargetValue, 1, 1, vCoins, setCoinsRet, nValueRet) || | |
1bbca249 | 1222 | (bSpendZeroConfChange && SelectCoinsMinConf(nTargetValue, 0, 1, vCoins, setCoinsRet, nValueRet))); |
e8ef3da7 WL |
1223 | } |
1224 | ||
1225 | ||
1226 | ||
1227 | ||
51ed9ec9 | 1228 | bool CWallet::CreateTransaction(const vector<pair<CScript, int64_t> >& vecSend, |
6a86c24d | 1229 | CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, std::string& strFailReason, const CCoinControl* coinControl) |
e8ef3da7 | 1230 | { |
51ed9ec9 BD |
1231 | int64_t nValue = 0; |
1232 | BOOST_FOREACH (const PAIRTYPE(CScript, int64_t)& s, vecSend) | |
e8ef3da7 WL |
1233 | { |
1234 | if (nValue < 0) | |
1f00f4e9 GA |
1235 | { |
1236 | strFailReason = _("Transaction amounts must be positive"); | |
e8ef3da7 | 1237 | return false; |
1f00f4e9 | 1238 | } |
e8ef3da7 WL |
1239 | nValue += s.second; |
1240 | } | |
1241 | if (vecSend.empty() || nValue < 0) | |
1f00f4e9 GA |
1242 | { |
1243 | strFailReason = _("Transaction amounts must be positive"); | |
e8ef3da7 | 1244 | return false; |
1f00f4e9 | 1245 | } |
e8ef3da7 | 1246 | |
4c6e2295 | 1247 | wtxNew.BindWallet(this); |
4949004d | 1248 | CMutableTransaction txNew; |
e8ef3da7 | 1249 | |
e8ef3da7 | 1250 | { |
f8dcd5ca | 1251 | LOCK2(cs_main, cs_wallet); |
e8ef3da7 | 1252 | { |
c6cb21d1 | 1253 | nFeeRet = payTxFee.GetFeePerK(); |
050d2e95 | 1254 | while (true) |
e8ef3da7 | 1255 | { |
4949004d PW |
1256 | txNew.vin.clear(); |
1257 | txNew.vout.clear(); | |
e8ef3da7 WL |
1258 | wtxNew.fFromMe = true; |
1259 | ||
51ed9ec9 | 1260 | int64_t nTotalValue = nValue + nFeeRet; |
e8ef3da7 WL |
1261 | double dPriority = 0; |
1262 | // vouts to the payees | |
51ed9ec9 | 1263 | BOOST_FOREACH (const PAIRTYPE(CScript, int64_t)& s, vecSend) |
8de9bb53 GA |
1264 | { |
1265 | CTxOut txout(s.second, s.first); | |
c6cb21d1 | 1266 | if (txout.IsDust(CTransaction::minRelayTxFee)) |
1f00f4e9 GA |
1267 | { |
1268 | strFailReason = _("Transaction amount too small"); | |
8de9bb53 | 1269 | return false; |
1f00f4e9 | 1270 | } |
4949004d | 1271 | txNew.vout.push_back(txout); |
8de9bb53 | 1272 | } |
e8ef3da7 WL |
1273 | |
1274 | // Choose coins to use | |
1275 | set<pair<const CWalletTx*,unsigned int> > setCoins; | |
51ed9ec9 | 1276 | int64_t nValueIn = 0; |
6a86c24d | 1277 | if (!SelectCoins(nTotalValue, setCoins, nValueIn, coinControl)) |
1f00f4e9 GA |
1278 | { |
1279 | strFailReason = _("Insufficient funds"); | |
e8ef3da7 | 1280 | return false; |
1f00f4e9 | 1281 | } |
e8ef3da7 WL |
1282 | BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int) pcoin, setCoins) |
1283 | { | |
51ed9ec9 | 1284 | int64_t nCredit = pcoin.first->vout[pcoin.second].nValue; |
d7836552 GM |
1285 | //The priority after the next block (depth+1) is used instead of the current, |
1286 | //reflecting an assumption the user would accept a bit more delay for | |
1287 | //a chance at a free transaction. | |
1288 | dPriority += (double)nCredit * (pcoin.first->GetDepthInMainChain()+1); | |
e8ef3da7 WL |
1289 | } |
1290 | ||
51ed9ec9 | 1291 | int64_t nChange = nValueIn - nValue - nFeeRet; |
a7dd11c6 PW |
1292 | |
1293 | if (nChange > 0) | |
e8ef3da7 | 1294 | { |
bf798734 GA |
1295 | // Fill a vout to ourself |
1296 | // TODO: pass in scriptChange instead of reservekey so | |
1297 | // change transaction isn't always pay-to-bitcoin-address | |
e8ef3da7 | 1298 | CScript scriptChange; |
6a86c24d CL |
1299 | |
1300 | // coin control: send change to custom address | |
1301 | if (coinControl && !boost::get<CNoDestination>(&coinControl->destChange)) | |
1302 | scriptChange.SetDestination(coinControl->destChange); | |
1303 | ||
1304 | // no coin control: send change to newly generated address | |
1305 | else | |
1306 | { | |
1307 | // Note: We use a new key here to keep it from being obvious which side is the change. | |
1308 | // The drawback is that by not reusing a previous key, the change may be lost if a | |
1309 | // backup is restored, if the backup doesn't have the new private key for the change. | |
1310 | // If we reused the old key, it would be possible to add code to look for and | |
1311 | // rediscover unknown transactions that were written with keys of ours to recover | |
1312 | // post-backup change. | |
1313 | ||
1314 | // Reserve a new key pair from key pool | |
1315 | CPubKey vchPubKey; | |
9b59e3bd GM |
1316 | bool ret; |
1317 | ret = reservekey.GetReservedKey(vchPubKey); | |
1318 | assert(ret); // should never fail, as we just unlocked | |
6a86c24d CL |
1319 | |
1320 | scriptChange.SetDestination(vchPubKey.GetID()); | |
1321 | } | |
e8ef3da7 | 1322 | |
8de9bb53 GA |
1323 | CTxOut newTxOut(nChange, scriptChange); |
1324 | ||
1325 | // Never create dust outputs; if we would, just | |
1326 | // add the dust to the fee. | |
c6cb21d1 | 1327 | if (newTxOut.IsDust(CTransaction::minRelayTxFee)) |
8de9bb53 GA |
1328 | { |
1329 | nFeeRet += nChange; | |
1330 | reservekey.ReturnKey(); | |
1331 | } | |
1332 | else | |
1333 | { | |
1334 | // Insert change txn at random position: | |
4949004d PW |
1335 | vector<CTxOut>::iterator position = txNew.vout.begin()+GetRandInt(txNew.vout.size()+1); |
1336 | txNew.vout.insert(position, newTxOut); | |
8de9bb53 | 1337 | } |
e8ef3da7 WL |
1338 | } |
1339 | else | |
1340 | reservekey.ReturnKey(); | |
1341 | ||
1342 | // Fill vin | |
1343 | BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins) | |
4949004d | 1344 | txNew.vin.push_back(CTxIn(coin.first->GetHash(),coin.second)); |
e8ef3da7 WL |
1345 | |
1346 | // Sign | |
1347 | int nIn = 0; | |
1348 | BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins) | |
4949004d | 1349 | if (!SignSignature(*this, *coin.first, txNew, nIn++)) |
1f00f4e9 GA |
1350 | { |
1351 | strFailReason = _("Signing transaction failed"); | |
e8ef3da7 | 1352 | return false; |
1f00f4e9 | 1353 | } |
e8ef3da7 | 1354 | |
4949004d PW |
1355 | // Embed the constructed transaction data in wtxNew. |
1356 | *static_cast<CTransaction*>(&wtxNew) = CTransaction(txNew); | |
1357 | ||
e8ef3da7 | 1358 | // Limit size |
6b6aaa16 | 1359 | unsigned int nBytes = ::GetSerializeSize(*(CTransaction*)&wtxNew, SER_NETWORK, PROTOCOL_VERSION); |
41e1a0d7 | 1360 | if (nBytes >= MAX_STANDARD_TX_SIZE) |
1f00f4e9 GA |
1361 | { |
1362 | strFailReason = _("Transaction too large"); | |
e8ef3da7 | 1363 | return false; |
1f00f4e9 | 1364 | } |
4d707d51 | 1365 | dPriority = wtxNew.ComputePriority(dPriority, nBytes); |
e8ef3da7 WL |
1366 | |
1367 | // Check that enough fee is included | |
c6cb21d1 | 1368 | int64_t nPayFee = payTxFee.GetFee(nBytes); |
05df3fc6 | 1369 | bool fAllowFree = AllowFree(dPriority); |
8dfd8c62 | 1370 | int64_t nMinFee = GetMinFee(wtxNew, nBytes, fAllowFree, GMF_SEND); |
e8ef3da7 WL |
1371 | if (nFeeRet < max(nPayFee, nMinFee)) |
1372 | { | |
1373 | nFeeRet = max(nPayFee, nMinFee); | |
1374 | continue; | |
1375 | } | |
1376 | ||
e8ef3da7 WL |
1377 | wtxNew.fTimeReceivedIsTxTime = true; |
1378 | ||
1379 | break; | |
1380 | } | |
1381 | } | |
1382 | } | |
1383 | return true; | |
1384 | } | |
1385 | ||
51ed9ec9 | 1386 | bool CWallet::CreateTransaction(CScript scriptPubKey, int64_t nValue, |
6a86c24d | 1387 | CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, std::string& strFailReason, const CCoinControl* coinControl) |
e8ef3da7 | 1388 | { |
51ed9ec9 | 1389 | vector< pair<CScript, int64_t> > vecSend; |
e8ef3da7 | 1390 | vecSend.push_back(make_pair(scriptPubKey, nValue)); |
6a86c24d | 1391 | return CreateTransaction(vecSend, wtxNew, reservekey, nFeeRet, strFailReason, coinControl); |
e8ef3da7 WL |
1392 | } |
1393 | ||
1394 | // Call after CreateTransaction unless you want to abort | |
1395 | bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey) | |
1396 | { | |
e8ef3da7 | 1397 | { |
f8dcd5ca | 1398 | LOCK2(cs_main, cs_wallet); |
7d9d134b | 1399 | LogPrintf("CommitTransaction:\n%s", wtxNew.ToString()); |
e8ef3da7 WL |
1400 | { |
1401 | // This is only to keep the database open to defeat the auto-flush for the | |
1402 | // duration of this scope. This is the only place where this optimization | |
1403 | // maybe makes sense; please don't do it anywhere else. | |
1404 | CWalletDB* pwalletdb = fFileBacked ? new CWalletDB(strWalletFile,"r") : NULL; | |
1405 | ||
1406 | // Take key pair from key pool so it won't be used again | |
1407 | reservekey.KeepKey(); | |
1408 | ||
1409 | // Add tx to wallet, because if it has change it's also ours, | |
1410 | // otherwise just for transaction history. | |
1411 | AddToWallet(wtxNew); | |
1412 | ||
93a18a36 | 1413 | // Notify that old coins are spent |
e8ef3da7 WL |
1414 | set<CWalletTx*> setCoins; |
1415 | BOOST_FOREACH(const CTxIn& txin, wtxNew.vin) | |
1416 | { | |
1417 | CWalletTx &coin = mapWallet[txin.prevout.hash]; | |
4c6e2295 | 1418 | coin.BindWallet(this); |
fe4a6550 | 1419 | NotifyTransactionChanged(this, coin.GetHash(), CT_UPDATED); |
e8ef3da7 WL |
1420 | } |
1421 | ||
1422 | if (fFileBacked) | |
1423 | delete pwalletdb; | |
1424 | } | |
1425 | ||
1426 | // Track how many getdata requests our transaction gets | |
6cc4a62c | 1427 | mapRequestCount[wtxNew.GetHash()] = 0; |
e8ef3da7 WL |
1428 | |
1429 | // Broadcast | |
b1f15b21 | 1430 | if (!wtxNew.AcceptToMemoryPool(false)) |
e8ef3da7 WL |
1431 | { |
1432 | // This must not fail. The transaction has already been signed and recorded. | |
881a85a2 | 1433 | LogPrintf("CommitTransaction() : Error: Transaction not valid"); |
e8ef3da7 WL |
1434 | return false; |
1435 | } | |
1436 | wtxNew.RelayWalletTransaction(); | |
1437 | } | |
e8ef3da7 WL |
1438 | return true; |
1439 | } | |
1440 | ||
1441 | ||
1442 | ||
1443 | ||
ca2c83da | 1444 | string CWallet::SendMoney(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew) |
e8ef3da7 WL |
1445 | { |
1446 | CReserveKey reservekey(this); | |
51ed9ec9 | 1447 | int64_t nFeeRequired; |
6cc4a62c GA |
1448 | |
1449 | if (IsLocked()) | |
e8ef3da7 | 1450 | { |
6b3783a9 | 1451 | string strError = _("Error: Wallet locked, unable to create transaction!"); |
7d9d134b | 1452 | LogPrintf("SendMoney() : %s", strError); |
6cc4a62c GA |
1453 | return strError; |
1454 | } | |
1f00f4e9 GA |
1455 | string strError; |
1456 | if (!CreateTransaction(scriptPubKey, nValue, wtxNew, reservekey, nFeeRequired, strError)) | |
6cc4a62c | 1457 | { |
6cc4a62c | 1458 | if (nValue + nFeeRequired > GetBalance()) |
7d9d134b WL |
1459 | strError = strprintf(_("Error: This transaction requires a transaction fee of at least %s because of its amount, complexity, or use of recently received funds!"), FormatMoney(nFeeRequired)); |
1460 | LogPrintf("SendMoney() : %s\n", strError); | |
6cc4a62c | 1461 | return strError; |
e8ef3da7 WL |
1462 | } |
1463 | ||
e8ef3da7 | 1464 | if (!CommitTransaction(wtxNew, reservekey)) |
6b3783a9 | 1465 | return _("Error: The transaction was rejected! This might happen if some of the coins in your wallet were already spent, such as if you used a copy of wallet.dat and coins were spent in the copy but not marked as spent here."); |
e8ef3da7 | 1466 | |
e8ef3da7 WL |
1467 | return ""; |
1468 | } | |
1469 | ||
1470 | ||
1471 | ||
ca2c83da | 1472 | string CWallet::SendMoneyToDestination(const CTxDestination& address, int64_t nValue, CWalletTx& wtxNew) |
e8ef3da7 WL |
1473 | { |
1474 | // Check amount | |
1475 | if (nValue <= 0) | |
1476 | return _("Invalid amount"); | |
c6cb21d1 | 1477 | if (nValue > GetBalance()) |
e8ef3da7 WL |
1478 | return _("Insufficient funds"); |
1479 | ||
ff0ee876 | 1480 | // Parse Bitcoin address |
e8ef3da7 | 1481 | CScript scriptPubKey; |
10254401 | 1482 | scriptPubKey.SetDestination(address); |
e8ef3da7 | 1483 | |
ca2c83da | 1484 | return SendMoney(scriptPubKey, nValue, wtxNew); |
e8ef3da7 WL |
1485 | } |
1486 | ||
1487 | ||
1488 | ||
1489 | ||
eed1785f | 1490 | DBErrors CWallet::LoadWallet(bool& fFirstRunRet) |
e8ef3da7 WL |
1491 | { |
1492 | if (!fFileBacked) | |
4f76be1d | 1493 | return DB_LOAD_OK; |
e8ef3da7 | 1494 | fFirstRunRet = false; |
eed1785f | 1495 | DBErrors nLoadWalletRet = CWalletDB(strWalletFile,"cr+").LoadWallet(this); |
d764d916 | 1496 | if (nLoadWalletRet == DB_NEED_REWRITE) |
9e9869d0 | 1497 | { |
d764d916 GA |
1498 | if (CDB::Rewrite(strWalletFile, "\x04pool")) |
1499 | { | |
012ca1c9 | 1500 | LOCK(cs_wallet); |
d764d916 GA |
1501 | setKeyPool.clear(); |
1502 | // Note: can't top-up keypool here, because wallet is locked. | |
1503 | // User will be prompted to unlock wallet the next operation | |
1504 | // the requires a new key. | |
1505 | } | |
9e9869d0 PW |
1506 | } |
1507 | ||
7ec55267 MC |
1508 | if (nLoadWalletRet != DB_LOAD_OK) |
1509 | return nLoadWalletRet; | |
fd61d6f5 | 1510 | fFirstRunRet = !vchDefaultKey.IsValid(); |
e8ef3da7 | 1511 | |
39278369 CL |
1512 | uiInterface.LoadWallet(this); |
1513 | ||
116df55e | 1514 | return DB_LOAD_OK; |
e8ef3da7 WL |
1515 | } |
1516 | ||
ae3d0aba | 1517 | |
518f3bda JG |
1518 | DBErrors CWallet::ZapWalletTx() |
1519 | { | |
1520 | if (!fFileBacked) | |
1521 | return DB_LOAD_OK; | |
1522 | DBErrors nZapWalletTxRet = CWalletDB(strWalletFile,"cr+").ZapWalletTx(this); | |
1523 | if (nZapWalletTxRet == DB_NEED_REWRITE) | |
1524 | { | |
1525 | if (CDB::Rewrite(strWalletFile, "\x04pool")) | |
1526 | { | |
1527 | LOCK(cs_wallet); | |
1528 | setKeyPool.clear(); | |
1529 | // Note: can't top-up keypool here, because wallet is locked. | |
1530 | // User will be prompted to unlock wallet the next operation | |
1531 | // the requires a new key. | |
1532 | } | |
1533 | } | |
1534 | ||
1535 | if (nZapWalletTxRet != DB_LOAD_OK) | |
1536 | return nZapWalletTxRet; | |
1537 | ||
1538 | return DB_LOAD_OK; | |
1539 | } | |
1540 | ||
1541 | ||
a41d5fe0 | 1542 | bool CWallet::SetAddressBook(const CTxDestination& address, const string& strName, const string& strPurpose) |
ae3d0aba | 1543 | { |
ca4cf5cf GA |
1544 | bool fUpdated = false; |
1545 | { | |
1546 | LOCK(cs_wallet); // mapAddressBook | |
1547 | std::map<CTxDestination, CAddressBookData>::iterator mi = mapAddressBook.find(address); | |
1548 | fUpdated = mi != mapAddressBook.end(); | |
1549 | mapAddressBook[address].name = strName; | |
1550 | if (!strPurpose.empty()) /* update purpose only if requested */ | |
1551 | mapAddressBook[address].purpose = strPurpose; | |
1552 | } | |
dcd0b077 | 1553 | NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address), |
ca4cf5cf | 1554 | strPurpose, (fUpdated ? CT_UPDATED : CT_NEW) ); |
ae3d0aba WL |
1555 | if (!fFileBacked) |
1556 | return false; | |
a41d5fe0 GA |
1557 | if (!strPurpose.empty() && !CWalletDB(strWalletFile).WritePurpose(CBitcoinAddress(address).ToString(), strPurpose)) |
1558 | return false; | |
10254401 | 1559 | return CWalletDB(strWalletFile).WriteName(CBitcoinAddress(address).ToString(), strName); |
ae3d0aba WL |
1560 | } |
1561 | ||
a41d5fe0 | 1562 | bool CWallet::DelAddressBook(const CTxDestination& address) |
ae3d0aba | 1563 | { |
b10e1470 | 1564 | { |
ca4cf5cf GA |
1565 | LOCK(cs_wallet); // mapAddressBook |
1566 | ||
1567 | if(fFileBacked) | |
b10e1470 | 1568 | { |
ca4cf5cf GA |
1569 | // Delete destdata tuples associated with address |
1570 | std::string strAddress = CBitcoinAddress(address).ToString(); | |
1571 | BOOST_FOREACH(const PAIRTYPE(string, string) &item, mapAddressBook[address].destdata) | |
1572 | { | |
1573 | CWalletDB(strWalletFile).EraseDestData(strAddress, item.first); | |
1574 | } | |
b10e1470 | 1575 | } |
ca4cf5cf | 1576 | mapAddressBook.erase(address); |
b10e1470 WL |
1577 | } |
1578 | ||
dcd0b077 | 1579 | NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address), "", CT_DELETED); |
ca4cf5cf | 1580 | |
ae3d0aba WL |
1581 | if (!fFileBacked) |
1582 | return false; | |
a41d5fe0 | 1583 | CWalletDB(strWalletFile).ErasePurpose(CBitcoinAddress(address).ToString()); |
10254401 | 1584 | return CWalletDB(strWalletFile).EraseName(CBitcoinAddress(address).ToString()); |
ae3d0aba WL |
1585 | } |
1586 | ||
fd61d6f5 | 1587 | bool CWallet::SetDefaultKey(const CPubKey &vchPubKey) |
ae3d0aba WL |
1588 | { |
1589 | if (fFileBacked) | |
1590 | { | |
1591 | if (!CWalletDB(strWalletFile).WriteDefaultKey(vchPubKey)) | |
1592 | return false; | |
1593 | } | |
1594 | vchDefaultKey = vchPubKey; | |
1595 | return true; | |
1596 | } | |
1597 | ||
37971fcc GA |
1598 | // |
1599 | // Mark old keypool keys as used, | |
1600 | // and generate all new keys | |
1601 | // | |
1602 | bool CWallet::NewKeyPool() | |
1603 | { | |
37971fcc | 1604 | { |
f8dcd5ca | 1605 | LOCK(cs_wallet); |
37971fcc | 1606 | CWalletDB walletdb(strWalletFile); |
51ed9ec9 | 1607 | BOOST_FOREACH(int64_t nIndex, setKeyPool) |
37971fcc GA |
1608 | walletdb.ErasePool(nIndex); |
1609 | setKeyPool.clear(); | |
1610 | ||
1611 | if (IsLocked()) | |
1612 | return false; | |
1613 | ||
51ed9ec9 | 1614 | int64_t nKeys = max(GetArg("-keypool", 100), (int64_t)0); |
37971fcc GA |
1615 | for (int i = 0; i < nKeys; i++) |
1616 | { | |
51ed9ec9 | 1617 | int64_t nIndex = i+1; |
37971fcc GA |
1618 | walletdb.WritePool(nIndex, CKeyPool(GenerateNewKey())); |
1619 | setKeyPool.insert(nIndex); | |
1620 | } | |
f48742c2 | 1621 | LogPrintf("CWallet::NewKeyPool wrote %d new keys\n", nKeys); |
37971fcc GA |
1622 | } |
1623 | return true; | |
1624 | } | |
1625 | ||
13dd2d09 | 1626 | bool CWallet::TopUpKeyPool(unsigned int kpSize) |
e8ef3da7 | 1627 | { |
e8ef3da7 | 1628 | { |
f8dcd5ca PW |
1629 | LOCK(cs_wallet); |
1630 | ||
4e87d341 MC |
1631 | if (IsLocked()) |
1632 | return false; | |
1633 | ||
e8ef3da7 WL |
1634 | CWalletDB walletdb(strWalletFile); |
1635 | ||
1636 | // Top up key pool | |
13dd2d09 JG |
1637 | unsigned int nTargetSize; |
1638 | if (kpSize > 0) | |
1639 | nTargetSize = kpSize; | |
1640 | else | |
51ed9ec9 | 1641 | nTargetSize = max(GetArg("-keypool", 100), (int64_t) 0); |
13dd2d09 | 1642 | |
faf705a4 | 1643 | while (setKeyPool.size() < (nTargetSize + 1)) |
e8ef3da7 | 1644 | { |
51ed9ec9 | 1645 | int64_t nEnd = 1; |
e8ef3da7 WL |
1646 | if (!setKeyPool.empty()) |
1647 | nEnd = *(--setKeyPool.end()) + 1; | |
1648 | if (!walletdb.WritePool(nEnd, CKeyPool(GenerateNewKey()))) | |
4e87d341 | 1649 | throw runtime_error("TopUpKeyPool() : writing generated key failed"); |
e8ef3da7 | 1650 | setKeyPool.insert(nEnd); |
783b182c | 1651 | LogPrintf("keypool added key %d, size=%u\n", nEnd, setKeyPool.size()); |
e8ef3da7 | 1652 | } |
4e87d341 MC |
1653 | } |
1654 | return true; | |
1655 | } | |
1656 | ||
51ed9ec9 | 1657 | void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool) |
4e87d341 MC |
1658 | { |
1659 | nIndex = -1; | |
fd61d6f5 | 1660 | keypool.vchPubKey = CPubKey(); |
4e87d341 | 1661 | { |
f8dcd5ca PW |
1662 | LOCK(cs_wallet); |
1663 | ||
4e87d341 MC |
1664 | if (!IsLocked()) |
1665 | TopUpKeyPool(); | |
e8ef3da7 WL |
1666 | |
1667 | // Get the oldest key | |
4e87d341 MC |
1668 | if(setKeyPool.empty()) |
1669 | return; | |
1670 | ||
1671 | CWalletDB walletdb(strWalletFile); | |
1672 | ||
e8ef3da7 WL |
1673 | nIndex = *(setKeyPool.begin()); |
1674 | setKeyPool.erase(setKeyPool.begin()); | |
1675 | if (!walletdb.ReadPool(nIndex, keypool)) | |
1676 | throw runtime_error("ReserveKeyFromKeyPool() : read failed"); | |
fd61d6f5 | 1677 | if (!HaveKey(keypool.vchPubKey.GetID())) |
e8ef3da7 | 1678 | throw runtime_error("ReserveKeyFromKeyPool() : unknown key in key pool"); |
fd61d6f5 | 1679 | assert(keypool.vchPubKey.IsValid()); |
f48742c2 | 1680 | LogPrintf("keypool reserve %d\n", nIndex); |
e8ef3da7 WL |
1681 | } |
1682 | } | |
1683 | ||
51ed9ec9 | 1684 | void CWallet::KeepKey(int64_t nIndex) |
e8ef3da7 WL |
1685 | { |
1686 | // Remove from key pool | |
1687 | if (fFileBacked) | |
1688 | { | |
1689 | CWalletDB walletdb(strWalletFile); | |
6cc4a62c | 1690 | walletdb.ErasePool(nIndex); |
e8ef3da7 | 1691 | } |
f48742c2 | 1692 | LogPrintf("keypool keep %d\n", nIndex); |
e8ef3da7 WL |
1693 | } |
1694 | ||
51ed9ec9 | 1695 | void CWallet::ReturnKey(int64_t nIndex) |
e8ef3da7 WL |
1696 | { |
1697 | // Return to key pool | |
f8dcd5ca PW |
1698 | { |
1699 | LOCK(cs_wallet); | |
e8ef3da7 | 1700 | setKeyPool.insert(nIndex); |
f8dcd5ca | 1701 | } |
f48742c2 | 1702 | LogPrintf("keypool return %d\n", nIndex); |
e8ef3da7 WL |
1703 | } |
1704 | ||
71ac5052 | 1705 | bool CWallet::GetKeyFromPool(CPubKey& result) |
e8ef3da7 | 1706 | { |
51ed9ec9 | 1707 | int64_t nIndex = 0; |
e8ef3da7 | 1708 | CKeyPool keypool; |
7db3b75b | 1709 | { |
f8dcd5ca | 1710 | LOCK(cs_wallet); |
ed02c95d GA |
1711 | ReserveKeyFromKeyPool(nIndex, keypool); |
1712 | if (nIndex == -1) | |
7db3b75b | 1713 | { |
ed02c95d GA |
1714 | if (IsLocked()) return false; |
1715 | result = GenerateNewKey(); | |
7db3b75b GA |
1716 | return true; |
1717 | } | |
ed02c95d GA |
1718 | KeepKey(nIndex); |
1719 | result = keypool.vchPubKey; | |
7db3b75b | 1720 | } |
7db3b75b | 1721 | return true; |
e8ef3da7 WL |
1722 | } |
1723 | ||
51ed9ec9 | 1724 | int64_t CWallet::GetOldestKeyPoolTime() |
e8ef3da7 | 1725 | { |
51ed9ec9 | 1726 | int64_t nIndex = 0; |
e8ef3da7 WL |
1727 | CKeyPool keypool; |
1728 | ReserveKeyFromKeyPool(nIndex, keypool); | |
4e87d341 MC |
1729 | if (nIndex == -1) |
1730 | return GetTime(); | |
e8ef3da7 WL |
1731 | ReturnKey(nIndex); |
1732 | return keypool.nTime; | |
1733 | } | |
1734 | ||
51ed9ec9 | 1735 | std::map<CTxDestination, int64_t> CWallet::GetAddressBalances() |
22dfd735 | 1736 | { |
51ed9ec9 | 1737 | map<CTxDestination, int64_t> balances; |
22dfd735 | 1738 | |
1739 | { | |
1740 | LOCK(cs_wallet); | |
1741 | BOOST_FOREACH(PAIRTYPE(uint256, CWalletTx) walletEntry, mapWallet) | |
1742 | { | |
1743 | CWalletTx *pcoin = &walletEntry.second; | |
1744 | ||
0542619d | 1745 | if (!IsFinalTx(*pcoin) || !pcoin->IsTrusted()) |
22dfd735 | 1746 | continue; |
1747 | ||
1748 | if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0) | |
1749 | continue; | |
1750 | ||
1751 | int nDepth = pcoin->GetDepthInMainChain(); | |
1752 | if (nDepth < (pcoin->IsFromMe() ? 0 : 1)) | |
1753 | continue; | |
1754 | ||
b1093efa | 1755 | for (unsigned int i = 0; i < pcoin->vout.size(); i++) |
22dfd735 | 1756 | { |
b1093efa | 1757 | CTxDestination addr; |
22dfd735 | 1758 | if (!IsMine(pcoin->vout[i])) |
1759 | continue; | |
b1093efa GM |
1760 | if(!ExtractDestination(pcoin->vout[i].scriptPubKey, addr)) |
1761 | continue; | |
22dfd735 | 1762 | |
93a18a36 | 1763 | int64_t n = IsSpent(walletEntry.first, i) ? 0 : pcoin->vout[i].nValue; |
22dfd735 | 1764 | |
22dfd735 | 1765 | if (!balances.count(addr)) |
1766 | balances[addr] = 0; | |
1767 | balances[addr] += n; | |
1768 | } | |
1769 | } | |
1770 | } | |
1771 | ||
1772 | return balances; | |
1773 | } | |
1774 | ||
b1093efa | 1775 | set< set<CTxDestination> > CWallet::GetAddressGroupings() |
22dfd735 | 1776 | { |
95691680 | 1777 | AssertLockHeld(cs_wallet); // mapWallet |
b1093efa GM |
1778 | set< set<CTxDestination> > groupings; |
1779 | set<CTxDestination> grouping; | |
22dfd735 | 1780 | |
1781 | BOOST_FOREACH(PAIRTYPE(uint256, CWalletTx) walletEntry, mapWallet) | |
1782 | { | |
1783 | CWalletTx *pcoin = &walletEntry.second; | |
1784 | ||
a3fad211 | 1785 | if (pcoin->vin.size() > 0) |
22dfd735 | 1786 | { |
a3fad211 | 1787 | bool any_mine = false; |
22dfd735 | 1788 | // group all input addresses with each other |
1789 | BOOST_FOREACH(CTxIn txin, pcoin->vin) | |
b1093efa GM |
1790 | { |
1791 | CTxDestination address; | |
a3fad211 GM |
1792 | if(!IsMine(txin)) /* If this input isn't mine, ignore it */ |
1793 | continue; | |
b1093efa GM |
1794 | if(!ExtractDestination(mapWallet[txin.prevout.hash].vout[txin.prevout.n].scriptPubKey, address)) |
1795 | continue; | |
1796 | grouping.insert(address); | |
a3fad211 | 1797 | any_mine = true; |
b1093efa | 1798 | } |
22dfd735 | 1799 | |
1800 | // group change with input addresses | |
a3fad211 GM |
1801 | if (any_mine) |
1802 | { | |
1803 | BOOST_FOREACH(CTxOut txout, pcoin->vout) | |
1804 | if (IsChange(txout)) | |
1805 | { | |
1806 | CTxDestination txoutAddr; | |
1807 | if(!ExtractDestination(txout.scriptPubKey, txoutAddr)) | |
1808 | continue; | |
1809 | grouping.insert(txoutAddr); | |
1810 | } | |
1811 | } | |
1812 | if (grouping.size() > 0) | |
1813 | { | |
1814 | groupings.insert(grouping); | |
1815 | grouping.clear(); | |
1816 | } | |
22dfd735 | 1817 | } |
1818 | ||
1819 | // group lone addrs by themselves | |
b1093efa | 1820 | for (unsigned int i = 0; i < pcoin->vout.size(); i++) |
22dfd735 | 1821 | if (IsMine(pcoin->vout[i])) |
1822 | { | |
b1093efa GM |
1823 | CTxDestination address; |
1824 | if(!ExtractDestination(pcoin->vout[i].scriptPubKey, address)) | |
1825 | continue; | |
1826 | grouping.insert(address); | |
22dfd735 | 1827 | groupings.insert(grouping); |
1828 | grouping.clear(); | |
1829 | } | |
1830 | } | |
1831 | ||
b1093efa GM |
1832 | set< set<CTxDestination>* > uniqueGroupings; // a set of pointers to groups of addresses |
1833 | map< CTxDestination, set<CTxDestination>* > setmap; // map addresses to the unique group containing it | |
1834 | BOOST_FOREACH(set<CTxDestination> grouping, groupings) | |
22dfd735 | 1835 | { |
1836 | // make a set of all the groups hit by this new group | |
b1093efa GM |
1837 | set< set<CTxDestination>* > hits; |
1838 | map< CTxDestination, set<CTxDestination>* >::iterator it; | |
1839 | BOOST_FOREACH(CTxDestination address, grouping) | |
22dfd735 | 1840 | if ((it = setmap.find(address)) != setmap.end()) |
1841 | hits.insert((*it).second); | |
1842 | ||
1843 | // merge all hit groups into a new single group and delete old groups | |
b1093efa GM |
1844 | set<CTxDestination>* merged = new set<CTxDestination>(grouping); |
1845 | BOOST_FOREACH(set<CTxDestination>* hit, hits) | |
22dfd735 | 1846 | { |
1847 | merged->insert(hit->begin(), hit->end()); | |
1848 | uniqueGroupings.erase(hit); | |
1849 | delete hit; | |
1850 | } | |
1851 | uniqueGroupings.insert(merged); | |
1852 | ||
1853 | // update setmap | |
b1093efa | 1854 | BOOST_FOREACH(CTxDestination element, *merged) |
22dfd735 | 1855 | setmap[element] = merged; |
1856 | } | |
1857 | ||
b1093efa GM |
1858 | set< set<CTxDestination> > ret; |
1859 | BOOST_FOREACH(set<CTxDestination>* uniqueGrouping, uniqueGroupings) | |
22dfd735 | 1860 | { |
1861 | ret.insert(*uniqueGrouping); | |
1862 | delete uniqueGrouping; | |
1863 | } | |
1864 | ||
1865 | return ret; | |
1866 | } | |
1867 | ||
3624356e GA |
1868 | set<CTxDestination> CWallet::GetAccountAddresses(string strAccount) const |
1869 | { | |
95691680 | 1870 | AssertLockHeld(cs_wallet); // mapWallet |
3624356e GA |
1871 | set<CTxDestination> result; |
1872 | BOOST_FOREACH(const PAIRTYPE(CTxDestination, CAddressBookData)& item, mapAddressBook) | |
1873 | { | |
1874 | const CTxDestination& address = item.first; | |
1875 | const string& strName = item.second.name; | |
1876 | if (strName == strAccount) | |
1877 | result.insert(address); | |
1878 | } | |
1879 | return result; | |
1880 | } | |
1881 | ||
360cfe14 | 1882 | bool CReserveKey::GetReservedKey(CPubKey& pubkey) |
e8ef3da7 WL |
1883 | { |
1884 | if (nIndex == -1) | |
1885 | { | |
1886 | CKeyPool keypool; | |
1887 | pwallet->ReserveKeyFromKeyPool(nIndex, keypool); | |
0d7b28e5 MC |
1888 | if (nIndex != -1) |
1889 | vchPubKey = keypool.vchPubKey; | |
360cfe14 PW |
1890 | else { |
1891 | if (pwallet->vchDefaultKey.IsValid()) { | |
881a85a2 | 1892 | LogPrintf("CReserveKey::GetReservedKey(): Warning: Using default key instead of a new key, top up your keypool!"); |
360cfe14 PW |
1893 | vchPubKey = pwallet->vchDefaultKey; |
1894 | } else | |
1895 | return false; | |
cee69980 | 1896 | } |
e8ef3da7 | 1897 | } |
fd61d6f5 | 1898 | assert(vchPubKey.IsValid()); |
360cfe14 PW |
1899 | pubkey = vchPubKey; |
1900 | return true; | |
e8ef3da7 WL |
1901 | } |
1902 | ||
1903 | void CReserveKey::KeepKey() | |
1904 | { | |
1905 | if (nIndex != -1) | |
1906 | pwallet->KeepKey(nIndex); | |
1907 | nIndex = -1; | |
fd61d6f5 | 1908 | vchPubKey = CPubKey(); |
e8ef3da7 WL |
1909 | } |
1910 | ||
1911 | void CReserveKey::ReturnKey() | |
1912 | { | |
1913 | if (nIndex != -1) | |
1914 | pwallet->ReturnKey(nIndex); | |
1915 | nIndex = -1; | |
fd61d6f5 | 1916 | vchPubKey = CPubKey(); |
e8ef3da7 | 1917 | } |
ae3d0aba | 1918 | |
434e4273 | 1919 | void CWallet::GetAllReserveKeys(set<CKeyID>& setAddress) const |
30ab2c9c PW |
1920 | { |
1921 | setAddress.clear(); | |
1922 | ||
1923 | CWalletDB walletdb(strWalletFile); | |
1924 | ||
f8dcd5ca | 1925 | LOCK2(cs_main, cs_wallet); |
51ed9ec9 | 1926 | BOOST_FOREACH(const int64_t& id, setKeyPool) |
30ab2c9c PW |
1927 | { |
1928 | CKeyPool keypool; | |
1929 | if (!walletdb.ReadPool(id, keypool)) | |
1930 | throw runtime_error("GetAllReserveKeyHashes() : read failed"); | |
fd61d6f5 | 1931 | assert(keypool.vchPubKey.IsValid()); |
10254401 PW |
1932 | CKeyID keyID = keypool.vchPubKey.GetID(); |
1933 | if (!HaveKey(keyID)) | |
30ab2c9c | 1934 | throw runtime_error("GetAllReserveKeyHashes() : unknown key in key pool"); |
10254401 | 1935 | setAddress.insert(keyID); |
30ab2c9c PW |
1936 | } |
1937 | } | |
fe4a6550 WL |
1938 | |
1939 | void CWallet::UpdatedTransaction(const uint256 &hashTx) | |
1940 | { | |
1941 | { | |
1942 | LOCK(cs_wallet); | |
1943 | // Only notify UI if this transaction is in this wallet | |
1944 | map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(hashTx); | |
1945 | if (mi != mapWallet.end()) | |
1946 | NotifyTransactionChanged(this, hashTx, CT_UPDATED); | |
1947 | } | |
1948 | } | |
fdbb537d JG |
1949 | |
1950 | void CWallet::LockCoin(COutPoint& output) | |
1951 | { | |
95691680 | 1952 | AssertLockHeld(cs_wallet); // setLockedCoins |
fdbb537d JG |
1953 | setLockedCoins.insert(output); |
1954 | } | |
1955 | ||
1956 | void CWallet::UnlockCoin(COutPoint& output) | |
1957 | { | |
95691680 | 1958 | AssertLockHeld(cs_wallet); // setLockedCoins |
fdbb537d JG |
1959 | setLockedCoins.erase(output); |
1960 | } | |
1961 | ||
1962 | void CWallet::UnlockAllCoins() | |
1963 | { | |
95691680 | 1964 | AssertLockHeld(cs_wallet); // setLockedCoins |
fdbb537d JG |
1965 | setLockedCoins.clear(); |
1966 | } | |
1967 | ||
1968 | bool CWallet::IsLockedCoin(uint256 hash, unsigned int n) const | |
1969 | { | |
95691680 | 1970 | AssertLockHeld(cs_wallet); // setLockedCoins |
fdbb537d JG |
1971 | COutPoint outpt(hash, n); |
1972 | ||
1973 | return (setLockedCoins.count(outpt) > 0); | |
1974 | } | |
1975 | ||
1976 | void CWallet::ListLockedCoins(std::vector<COutPoint>& vOutpts) | |
1977 | { | |
95691680 | 1978 | AssertLockHeld(cs_wallet); // setLockedCoins |
fdbb537d JG |
1979 | for (std::set<COutPoint>::iterator it = setLockedCoins.begin(); |
1980 | it != setLockedCoins.end(); it++) { | |
1981 | COutPoint outpt = (*it); | |
1982 | vOutpts.push_back(outpt); | |
1983 | } | |
1984 | } | |
1985 | ||
51ed9ec9 | 1986 | void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const { |
95691680 | 1987 | AssertLockHeld(cs_wallet); // mapKeyMetadata |
434e4273 PW |
1988 | mapKeyBirth.clear(); |
1989 | ||
1990 | // get birth times for keys with metadata | |
1991 | for (std::map<CKeyID, CKeyMetadata>::const_iterator it = mapKeyMetadata.begin(); it != mapKeyMetadata.end(); it++) | |
1992 | if (it->second.nCreateTime) | |
1993 | mapKeyBirth[it->first] = it->second.nCreateTime; | |
1994 | ||
1995 | // map in which we'll infer heights of other keys | |
4c6d41b8 | 1996 | CBlockIndex *pindexMax = chainActive[std::max(0, chainActive.Height() - 144)]; // the tip can be reorganised; use a 144-block safety margin |
434e4273 PW |
1997 | std::map<CKeyID, CBlockIndex*> mapKeyFirstBlock; |
1998 | std::set<CKeyID> setKeys; | |
1999 | GetKeys(setKeys); | |
2000 | BOOST_FOREACH(const CKeyID &keyid, setKeys) { | |
2001 | if (mapKeyBirth.count(keyid) == 0) | |
2002 | mapKeyFirstBlock[keyid] = pindexMax; | |
2003 | } | |
2004 | setKeys.clear(); | |
2005 | ||
2006 | // if there are no such keys, we're done | |
2007 | if (mapKeyFirstBlock.empty()) | |
2008 | return; | |
2009 | ||
2010 | // find first block that affects those keys, if there are any left | |
2011 | std::vector<CKeyID> vAffected; | |
2012 | for (std::map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); it++) { | |
2013 | // iterate over all wallet transactions... | |
2014 | const CWalletTx &wtx = (*it).second; | |
2015 | std::map<uint256, CBlockIndex*>::const_iterator blit = mapBlockIndex.find(wtx.hashBlock); | |
4c6d41b8 | 2016 | if (blit != mapBlockIndex.end() && chainActive.Contains(blit->second)) { |
434e4273 PW |
2017 | // ... which are already in a block |
2018 | int nHeight = blit->second->nHeight; | |
2019 | BOOST_FOREACH(const CTxOut &txout, wtx.vout) { | |
2020 | // iterate over all their outputs | |
2021 | ::ExtractAffectedKeys(*this, txout.scriptPubKey, vAffected); | |
2022 | BOOST_FOREACH(const CKeyID &keyid, vAffected) { | |
2023 | // ... and all their affected keys | |
2024 | std::map<CKeyID, CBlockIndex*>::iterator rit = mapKeyFirstBlock.find(keyid); | |
2025 | if (rit != mapKeyFirstBlock.end() && nHeight < rit->second->nHeight) | |
2026 | rit->second = blit->second; | |
2027 | } | |
2028 | vAffected.clear(); | |
2029 | } | |
2030 | } | |
2031 | } | |
2032 | ||
2033 | // Extract block timestamps for those keys | |
2034 | for (std::map<CKeyID, CBlockIndex*>::const_iterator it = mapKeyFirstBlock.begin(); it != mapKeyFirstBlock.end(); it++) | |
2035 | mapKeyBirth[it->first] = it->second->nTime - 7200; // block times can be 2h off | |
2036 | } | |
b10e1470 WL |
2037 | |
2038 | bool CWallet::AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value) | |
2039 | { | |
8476d5d4 CL |
2040 | if (boost::get<CNoDestination>(&dest)) |
2041 | return false; | |
2042 | ||
b10e1470 WL |
2043 | mapAddressBook[dest].destdata.insert(std::make_pair(key, value)); |
2044 | if (!fFileBacked) | |
2045 | return true; | |
2046 | return CWalletDB(strWalletFile).WriteDestData(CBitcoinAddress(dest).ToString(), key, value); | |
2047 | } | |
2048 | ||
2049 | bool CWallet::EraseDestData(const CTxDestination &dest, const std::string &key) | |
2050 | { | |
2051 | if (!mapAddressBook[dest].destdata.erase(key)) | |
2052 | return false; | |
2053 | if (!fFileBacked) | |
2054 | return true; | |
2055 | return CWalletDB(strWalletFile).EraseDestData(CBitcoinAddress(dest).ToString(), key); | |
2056 | } | |
2057 | ||
2058 | bool CWallet::LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value) | |
2059 | { | |
2060 | mapAddressBook[dest].destdata.insert(std::make_pair(key, value)); | |
2061 | return true; | |
2062 | } | |
2063 | ||
2064 | bool CWallet::GetDestData(const CTxDestination &dest, const std::string &key, std::string *value) const | |
2065 | { | |
2066 | std::map<CTxDestination, CAddressBookData>::const_iterator i = mapAddressBook.find(dest); | |
2067 | if(i != mapAddressBook.end()) | |
2068 | { | |
2069 | CAddressBookData::StringMap::const_iterator j = i->second.destdata.find(key); | |
2070 | if(j != i->second.destdata.end()) | |
2071 | { | |
2072 | if(value) | |
2073 | *value = j->second; | |
2074 | return true; | |
2075 | } | |
2076 | } | |
2077 | return false; | |
2078 | } |