forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 717
/
wallet.h
1319 lines (1100 loc) · 52.9 KB
/
wallet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2021 The Bitcoin developers
// Copyright (c) 2014-2015 The Dash developers
// Copyright (c) 2015-2021 The PIVX developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef PIVX_WALLET_H
#define PIVX_WALLET_H
#include "addressbook.h"
#include "amount.h"
#include "consensus/tx_verify.h"
#include "consensus/validation.h"
#include "crypter.h"
#include "destination_io.h"
#include "kernel.h"
#include "key.h"
#include "key_io.h"
#include "keystore.h"
#include "pairresult.h"
#include "policy/feerate.h"
#include "primitives/block.h"
#include "primitives/transaction.h"
#include "sapling/address.h"
#include "guiinterface.h"
#include "util/system.h"
#include "util/memory.h"
#include "utilstrencodings.h"
#include "validationinterface.h"
#include "script/ismine.h"
#include "wallet/scriptpubkeyman.h"
#include "sapling/saplingscriptpubkeyman.h"
#include "validation.h"
#include "wallet/walletdb.h"
#include <algorithm>
#include <atomic>
#include <map>
#include <set>
#include <stdexcept>
#include <stdint.h>
#include <string>
#include <utility>
#include <vector>
typedef CWallet* CWalletRef;
extern std::vector<CWalletRef> vpwallets;
/**
* Settings
*/
extern CFeeRate payTxFee;
extern CAmount maxTxFee;
extern unsigned int nTxConfirmTarget;
extern bool bSpendZeroConfChange;
extern bool bdisableSystemnotifications;
extern bool fPayAtLeastCustomFee;
//! -paytxfee default
static const CAmount DEFAULT_TRANSACTION_FEE = 0;
//! -paytxfee will warn if called with a higher fee than this amount (in satoshis) per KB
static const CAmount nHighTransactionFeeWarning = 0.1 * COIN;
//! -maxtxfee default
static const CAmount DEFAULT_TRANSACTION_MAXFEE = 1 * COIN;
//! -maxtxfee will warn if called with a higher fee than this amount (in satoshis)
static const CAmount nHighTransactionMaxFeeWarning = 100 * nHighTransactionFeeWarning;
//! -minstakesplit default
static const CAmount DEFAULT_MIN_STAKE_SPLIT_THRESHOLD = 100 * COIN;
//! Default for -spendzeroconfchange
static const bool DEFAULT_SPEND_ZEROCONF_CHANGE = true;
//! Default for -staking
static const bool DEFAULT_STAKING = true;
//! Default for -coldstaking
static const bool DEFAULT_COLDSTAKING = true;
//! Defaults for -gen and -genproclimit
static const bool DEFAULT_GENERATE = false;
static const unsigned int DEFAULT_GENERATE_PROCLIMIT = 1;
//! Default for -createwalletbackups
static const unsigned int DEFAULT_CREATEWALLETBACKUPS = 10;
//! Default for -disablewallet
static const bool DEFAULT_DISABLE_WALLET = false;
static const int64_t TIMESTAMP_MIN = 0;
class CAddressBookIterator;
class CCoinControl;
class COutput;
class CStakeableOutput;
class CReserveKey;
class CScript;
class CScheduler;
class ScriptPubKeyMan;
class SaplingScriptPubKeyMan;
class SaplingNoteData;
struct SaplingNoteEntry;
class CDeterministicMNList;
/** (client) version numbers for particular wallet features */
enum WalletFeature {
FEATURE_BASE = 10500, // the earliest version new wallets supports (only useful for getinfo's clientversion output)
FEATURE_WALLETCRYPT = 40000, // wallet encryption
FEATURE_COMPRPUBKEY = 60000, // compressed public keys
FEATURE_PRE_PIVX = 61000, // inherited version..
// The following features were implemented in BTC but not in our wallet, we can simply skip them.
// FEATURE_HD = 130000, Hierarchical key derivation after BIP32 (HD Wallet)
// FEATURE_HD_SPLIT = 139900, // Wallet with HD chain split (change outputs will use m/0'/1'/k)
FEATURE_PRE_SPLIT_KEYPOOL = 169900, // Upgraded to HD SPLIT and can have a pre-split keypool
FEATURE_SAPLING = 170000, // Upgraded to Saplings key manager.
FEATURE_LATEST = FEATURE_SAPLING
};
/** A key pool entry */
class CKeyPool
{
public:
//! The time at which the key was generated. Set in AddKeypoolPubKeyWithDB
int64_t nTime;
//! The public key
CPubKey vchPubKey;
//! Whether this keypool entry is in the internal, external or staking keypool.
uint8_t type;
//! Whether this key was generated for a keypool before the wallet was upgraded to HD-split
bool m_pre_split;
CKeyPool();
CKeyPool(const CPubKey& vchPubKeyIn, const uint8_t& type);
bool IsInternal() const { return type == HDChain::ChangeType::INTERNAL; }
bool IsExternal() const { return type == HDChain::ChangeType::EXTERNAL; }
bool IsStaking() const { return type == HDChain::ChangeType::STAKING; }
template<typename Stream>
void Serialize(Stream& s) const
{
int nVersion = s.GetVersion();
if (!(s.GetType() & SER_GETHASH)) {
s << nVersion;
}
s << nTime << vchPubKey << Span<unsigned char>((unsigned char*)&type, 1) << m_pre_split;
}
template<typename Stream>
void Unserialize(Stream& s)
{
int nVersion = s.GetVersion();
if (!(s.GetType() & SER_GETHASH)) {
s >> nVersion;
}
s >> nTime >> vchPubKey;
try {
s >> Span<unsigned char>((unsigned char*)&type, 1);
} catch (std::ios_base::failure&) {
/* flag as external address if we can't read the internal boolean
(this will be the case for any wallet before the HD chain split version) */
type = HDChain::ChangeType::EXTERNAL;
}
try {
s >> m_pre_split;
} catch (std::ios_base::failure&) {
/* flag as pre-split address if we can't read the m_pre_split boolean
(this will be the case for any wallet prior to the HD chain upgrade) */
m_pre_split = true;
}
}
};
/** Record info about last stake attempt:
* - tipBlock index of the block on top of which last stake attempt was made
* - nTime time slot of last attempt
* - nTries number of UTXOs hashed during last attempt
* - nCoins number of stakeable utxos during last attempt
**/
class CStakerStatus
{
private:
const CBlockIndex* tipBlock{nullptr};
int64_t nTime{0};
int nTries{0};
int nCoins{0};
public:
// Get
const CBlockIndex* GetLastTip() const { return tipBlock; }
uint256 GetLastHash() const { return (GetLastTip() == nullptr ? UINT256_ZERO : GetLastTip()->GetBlockHash()); }
int GetLastHeight() const { return (GetLastTip() == nullptr ? 0 : GetLastTip()->nHeight); }
int GetLastCoins() const { return nCoins; }
int GetLastTries() const { return nTries; }
int64_t GetLastTime() const { return nTime; }
// Set
void SetLastCoins(const int coins) { nCoins = coins; }
void SetLastTries(const int tries) { nTries = tries; }
void SetLastTip(const CBlockIndex* lastTip) { tipBlock = lastTip; }
void SetLastTime(const uint64_t lastTime) { nTime = lastTime; }
void SetNull()
{
SetLastCoins(0);
SetLastTries(0);
SetLastTip(nullptr);
SetLastTime(0);
}
// Check whether staking status is active (last attempt earlier than 30 seconds ago)
bool IsActive() const { return (nTime + 30) >= GetTime(); }
};
class CRecipientBase {
public:
CAmount nAmount;
bool fSubtractFeeFromAmount;
CRecipientBase(const CAmount& _nAmount, bool _fSubtractFeeFromAmount) :
nAmount(_nAmount), fSubtractFeeFromAmount(_fSubtractFeeFromAmount) {}
virtual bool isTransparent() const { return true; };
virtual Optional<CScript> getScript() const { return nullopt; }
virtual Optional<libzcash::SaplingPaymentAddress> getSapPaymentAddr() const { return nullopt; }
virtual std::string getMemo() const { return ""; }
};
class CRecipient final : public CRecipientBase
{
public:
CScript scriptPubKey;
CRecipient(const CScript& _scriptPubKey, const CAmount& _nAmount, bool _fSubtractFeeFromAmount) :
CRecipientBase(_nAmount, _fSubtractFeeFromAmount), scriptPubKey(_scriptPubKey) {}
bool isTransparent() const override { return true; }
Optional<CScript> getScript() const override { return {scriptPubKey}; }
};
class CAddressBookIterator
{
public:
explicit CAddressBookIterator(std::map<CWDestination, AddressBook::CAddressBookData>& _map) : map(_map), it(_map.begin()), itEnd(_map.end()) {}
const CWDestination* GetDestKey();
const CTxDestination* GetCTxDestKey();
const libzcash::SaplingPaymentAddress* GetShieldedDestKey();
AddressBook::CAddressBookData GetValue() { return it->second; }
bool IsValid() { return it != itEnd; }
bool Next() {
if (!IsValid()) return false;
it++;
return IsValid();
}
void SetFilter(CTxDestination& filter)
{
it = map.find(filter);
if (it != itEnd) {
itEnd = std::next(it);
}
}
private:
std::map<CWDestination, AddressBook::CAddressBookData>& map;
std::map<CWDestination, AddressBook::CAddressBookData>::iterator it;
std::map<CWDestination, AddressBook::CAddressBookData>::iterator itEnd;
};
template <class T>
using TxSpendMap = std::multimap<T, uint256>;
typedef std::map<SaplingOutPoint, SaplingNoteData> mapSaplingNoteData_t;
typedef std::map<std::string, std::string> mapValue_t;
static inline void ReadOrderPos(int64_t& nOrderPos, mapValue_t& mapValue)
{
if (!mapValue.count("n")) {
nOrderPos = -1; // TODO: calculate elsewhere
return;
}
nOrderPos = atoi64(mapValue["n"].c_str());
}
static inline void WriteOrderPos(const int64_t& nOrderPos, mapValue_t& mapValue)
{
if (nOrderPos == -1)
return;
mapValue["n"] = i64tostr(nOrderPos);
}
struct COutputEntry {
CTxDestination destination;
CAmount amount;
int vout;
};
/** Legacy class used for deserializing vtxPrev for backwards compatibility.
* vtxPrev was removed in commit 93a18a3650292afbb441a47d1fa1b94aeb0164e3,
* but old wallet.dat files may still contain vtxPrev vectors of CMerkleTxs.
* These need to get deserialized for field alignment when deserializing
* a CWalletTx, but the deserialized values are discarded.**/
class CMerkleTx
{
private:
public:
template<typename Stream>
void Unserialize(Stream& s)
{
CTransactionRef tx;
uint256 hashBlock;
std::vector<uint256> vMerkleBranch;
int nIndex;
s >> tx >> hashBlock >> vMerkleBranch >> nIndex;
}
};
/**
* A transaction with a bunch of additional info that only the owner cares about.
* It includes any unrecorded transactions needed to link it back to the block chain.
*/
class CWalletTx
{
private:
const CWallet* pwallet;
/** Constant used in hashBlock to indicate tx has been abandoned, only used at
* serialization/deserialization to avoid ambiguity with conflicted.
*/
static const uint256 ABANDON_HASH;
public:
mapValue_t mapValue;
mapSaplingNoteData_t mapSaplingNoteData;
std::vector<std::pair<std::string, std::string> > vOrderForm;
unsigned int fTimeReceivedIsTxTime;
unsigned int nTimeReceived; //! time received by this node
/**
* Stable timestamp representing the block time, for a transaction included in a block,
* or else the time when the transaction was received if it isn't yet part of a block.
*/
unsigned int nTimeSmart;
char fFromMe;
int64_t nOrderPos; //! position in ordered transaction list
// memory only
enum AmountType { DEBIT, CREDIT, IMMATURE_CREDIT, AVAILABLE_CREDIT, AMOUNTTYPE_ENUM_ELEMENTS };
CAmount GetCachableAmount(AmountType type, const isminefilter& filter, bool recalculate = false) const;
bool IsAmountCached(AmountType type, const isminefilter& filter) const; // Only used in unit tests
mutable CachableAmount m_amounts[AMOUNTTYPE_ENUM_ELEMENTS];
mutable bool fStakeDelegationVoided;
mutable bool fChangeCached;
mutable bool fInMempool;
mutable CAmount nChangeCached;
mutable bool fShieldedChangeCached;
mutable CAmount nShieldedChangeCached;
CWalletTx(const CWallet* pwalletIn, CTransactionRef arg);
void Init(const CWallet* pwalletIn);
CTransactionRef tx;
/* New transactions start as UNCONFIRMED. At BlockConnected,
* they will transition to CONFIRMED. In case of reorg, at BlockDisconnected,
* they roll back to UNCONFIRMED. If we detect a conflicting transaction at
* block connection, we update conflicted tx and its dependencies as CONFLICTED.
* If tx isn't confirmed and outside of mempool, the user may switch it to ABANDONED
* by using the abandontransaction call. This last status may be override by a CONFLICTED
* or CONFIRMED transition.
*/
enum Status {
UNCONFIRMED,
CONFIRMED,
CONFLICTED,
ABANDONED
};
/* Confirmation includes tx status and a triplet of {block height/block hash/tx index in block}
* at which tx has been confirmed. All three are set to 0 if tx is unconfirmed or abandoned.
* Meaning of these fields changes with CONFLICTED state where they instead point to block hash
* and block height of the deepest conflicting tx.
*/
struct Confirmation {
Status status;
int block_height;
uint256 hashBlock;
int nIndex;
Confirmation(Status s = UNCONFIRMED, int b = 0, const uint256& h = UINT256_ZERO, int i = 0) : status(s), block_height(b), hashBlock(h), nIndex(i) {}
};
Confirmation m_confirm;
template<typename Stream>
void Serialize(Stream& s) const
{
mapValue_t mapValueCopy = mapValue;
mapValueCopy["fromaccount"] = "";
WriteOrderPos(nOrderPos, mapValueCopy);
if (nTimeSmart) {
mapValueCopy["timesmart"] = strprintf("%u", nTimeSmart);
}
std::vector<char> dummy_vector1; //!< Used to be vMerkleBranch
std::vector<char> dummy_vector2; //!< Used to be vtxPrev
char dummy_char = false; //!< Used to be fSpent
uint256 serializedHash = isAbandoned() ? ABANDON_HASH : m_confirm.hashBlock;
int serializedIndex = isAbandoned() || isConflicted() ? -1 : m_confirm.nIndex;
s << tx << serializedHash << dummy_vector1 << serializedIndex << dummy_vector2 << mapValueCopy << vOrderForm << fTimeReceivedIsTxTime << nTimeReceived << fFromMe << dummy_char;
if (this->tx->isSaplingVersion()) {
s << mapSaplingNoteData;
}
}
template<typename Stream>
void Unserialize(Stream& s)
{
Init(nullptr);
std::vector<uint256> dummy_vector1; //!< Used to be vMerkleBranch
std::vector<CMerkleTx> dummy_vector2; //!< Used to be vtxPrev
char dummy_char; //! Used to be fSpent
int serializedIndex;
s >> tx >> m_confirm.hashBlock >> dummy_vector1 >> serializedIndex >> dummy_vector2 >> mapValue >> vOrderForm >> fTimeReceivedIsTxTime >> nTimeReceived >> fFromMe >> dummy_char;
if (this->tx->isSaplingVersion()) {
s >> mapSaplingNoteData;
}
/* At serialization/deserialization, an nIndex == -1 means that hashBlock refers to
* the earliest block in the chain we know this or any in-wallet ancestor conflicts
* with. If nIndex == -1 and hashBlock is ABANDON_HASH, it means transaction is abandoned.
* In same context, an nIndex >= 0 refers to a confirmed transaction (if hashBlock set) or
* unconfirmed one. Older clients interpret nIndex == -1 as unconfirmed for backward
* compatibility (pre-commit 9ac63d6).
*/
if (serializedIndex == -1 && m_confirm.hashBlock == ABANDON_HASH) {
setAbandoned();
} else if (serializedIndex == -1) {
setConflicted();
} else if (!m_confirm.hashBlock.IsNull()) {
m_confirm.nIndex = serializedIndex;
setConfirmed();
}
ReadOrderPos(nOrderPos, mapValue);
nTimeSmart = mapValue.count("timesmart") ? (unsigned int)atoi64(mapValue["timesmart"]) : 0;
mapValue.erase("fromaccount");
mapValue.erase("version");
mapValue.erase("spent");
mapValue.erase("n");
mapValue.erase("timesmart");
}
void SetTx(CTransactionRef arg) { tx = std::move(arg); }
//! make sure balances are recalculated
void MarkDirty();
void BindWallet(CWallet* pwalletIn);
void SetSaplingNoteData(mapSaplingNoteData_t& noteData);
Optional<std::pair<
libzcash::SaplingNotePlaintext,
libzcash::SaplingPaymentAddress>> DecryptSaplingNote(const SaplingOutPoint& op) const;
Optional<std::pair<
libzcash::SaplingNotePlaintext,
libzcash::SaplingPaymentAddress>> RecoverSaplingNote(const SaplingOutPoint& op, const std::set<uint256>& ovks) const;
//! checks whether a tx has P2CS inputs or not
bool HasP2CSInputs() const;
//! Store a comment
void SetComment(const std::string& comment) { mapValue["comment"] = comment; }
std::string GetComment() const {
const auto& it = mapValue.find("comment");
return it != mapValue.end() ? it->second : "";
}
int GetDepthAndMempool(bool& fConflicted) const;
//! filter decides which addresses will count towards the debit
CAmount GetDebit(const isminefilter& filter) const;
CAmount GetCredit(const isminefilter& filter, bool recalculate = false) const;
CAmount GetImmatureCredit(bool fUseCache = true, const isminefilter& filter = ISMINE_SPENDABLE_ALL) const;
CAmount GetAvailableCredit(bool fUseCache = true, const isminefilter& filter=ISMINE_SPENDABLE) const;
// Return sum of locked coins
CAmount GetLockedCredit() const;
CAmount GetImmatureWatchOnlyCredit(const bool& fUseCache = true) const;
CAmount GetAvailableWatchOnlyCredit(const bool& fUseCache = true) const;
CAmount GetChange() const;
// Shielded credit/debit/change
CAmount GetShieldedChange() const;
CAmount GetShieldedAvailableCredit(bool fUseCache = true) const;
// Cold staking contracts credit/debit
CAmount GetColdStakingCredit(bool fUseCache = true) const;
CAmount GetColdStakingDebit(bool fUseCache = true) const;
CAmount GetStakeDelegationCredit(bool fUseCache = true) const;
CAmount GetStakeDelegationDebit(bool fUseCache = true) const;
void GetAmounts(std::list<COutputEntry>& listReceived,
std::list<COutputEntry>& listSent,
CAmount& nFee,
const isminefilter& filter) const;
bool IsFromMe(const isminefilter& filter) const;
bool InMempool() const;
// True if only scriptSigs are different
bool IsEquivalentTo(const CWalletTx& tx) const;
bool IsTrusted() const;
bool IsTrusted(int& nDepth, bool& fConflicted) const;
int64_t GetTxTime() const;
void UpdateTimeSmart();
void RelayWalletTransaction(CConnman* connman);
std::set<uint256> GetConflicts() const;
/**
* Return depth of transaction in blockchain:
* <0 : conflicts with a transaction this deep in the blockchain
* 0 : in memory pool, waiting to be included in a block
* >=1 : this many blocks deep in the main chain
*/
// TODO: Remove "NO_THREAD_SAFETY_ANALYSIS" and replace it with the correct
// annotation "EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)". The annotation
// "NO_THREAD_SAFETY_ANALYSIS" was temporarily added to avoid having to
// resolve the issue of member access into incomplete type CWallet. Note
// that we still have the runtime check "AssertLockHeld(pwallet->cs_wallet)"
// in place.
int GetDepthInMainChain() const NO_THREAD_SAFETY_ANALYSIS;
bool IsInMainChainImmature() const;
int GetBlocksToMaturity() const;
bool isAbandoned() const { return m_confirm.status == CWalletTx::ABANDONED; }
void setAbandoned()
{
m_confirm.status = CWalletTx::ABANDONED;
m_confirm.hashBlock = UINT256_ZERO;
m_confirm.block_height = 0;
m_confirm.nIndex = 0;
}
bool isConflicted() const { return m_confirm.status == CWalletTx::CONFLICTED; }
void setConflicted() { m_confirm.status = CWalletTx::CONFLICTED; }
bool isUnconfirmed() const { return m_confirm.status == CWalletTx::UNCONFIRMED; }
void setUnconfirmed() { m_confirm.status = CWalletTx::UNCONFIRMED; }
bool isConfirmed() const { return m_confirm.status == CWalletTx::CONFIRMED; }
void setConfirmed() { m_confirm.status = CWalletTx::CONFIRMED; }
const uint256& GetHash() const { return tx->GetHash(); }
bool IsCoinBase() const { return tx->IsCoinBase(); }
bool IsCoinStake() const { return tx->IsCoinStake(); }
/** Pass this transaction to the mempool. Fails if absolute fee exceeds absurd fee. */
bool AcceptToMemoryPool(CValidationState& state);
};
class WalletRescanReserver; //forward declarations for ScanForWalletTransactions/RescanFromTime
/**
* A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,
* and provides the ability to create new transactions.
*/
class CWallet : public CCryptoKeyStore, public CValidationInterface
{
private:
static std::atomic<bool> fFlushScheduled;
std::atomic<bool> fAbortRescan;
std::atomic<bool> fScanningWallet; //controlled by WalletRescanReserver
std::mutex mutexScanning;
friend class WalletRescanReserver;
//! keeps track of whether Unlock has run a thorough check before
bool fDecryptionThoroughlyChecked{false};
//! Key manager //
std::unique_ptr<ScriptPubKeyMan> m_spk_man = MakeUnique<ScriptPubKeyMan>(this);
std::unique_ptr<SaplingScriptPubKeyMan> m_sspk_man = MakeUnique<SaplingScriptPubKeyMan>(this);
//! the current wallet version: clients below this version are not able to load the wallet
int nWalletVersion;
//! the maximum wallet format version: memory-only variable that specifies to what version this wallet may be upgraded
int nWalletMaxVersion;
/**
* Wallet filename from wallet=<path> command line or config option.
* Used in debug logs and to send RPCs to the right wallet instance when
* more than one wallet is loaded.
*/
std::string m_name;
/** Internal database handle. */
std::unique_ptr<CWalletDBWrapper> dbw;
/**
* The following is used to keep track of how far behind the wallet is
* from the chain sync, and to allow clients to block on us being caught up.
*
* Note that this is *not* how far we've processed, we may need some rescan
* to have seen all transactions in the chain, but is only used to track
* live BlockConnected callbacks.
*
* Protected by cs_main (see BlockUntilSyncedToCurrentChain)
*/
uint256 m_last_block_processed GUARDED_BY(cs_wallet) = UINT256_ZERO;
/* Height of last block processed is used by wallet to know depth of transactions
* without relying on Chain interface beyond asynchronous updates. For safety, we
* initialize it to -1. Height is a pointer on node's tip and doesn't imply
* that the wallet has scanned sequentially all blocks up to this one.
*/
int m_last_block_processed_height GUARDED_BY(cs_wallet) = -1;
int64_t m_last_block_processed_time GUARDED_BY(cs_wallet) = 0;
int64_t nNextResend;
int64_t nLastResend;
/**
* Used to keep track of spent outpoints, and
* detect and report conflicts (double-spends or
* mutated transactions where the mutant gets mined).
*/
typedef TxSpendMap<COutPoint> TxSpends;
TxSpends mapTxSpends;
void AddToSpends(const COutPoint& outpoint, const uint256& wtxid);
void AddToSpends(const uint256& wtxid);
/* Mark a transaction (and its in-wallet descendants) as conflicting with a particular block. */
void MarkConflicted(const uint256& hashBlock, int conflicting_height, const uint256& hashTx);
template <class T>
void SyncMetaData(std::pair<typename TxSpendMap<T>::iterator, typename TxSpendMap<T>::iterator> range);
void ChainTipAdded(const CBlockIndex *pindex, const CBlock *pblock, SaplingMerkleTree saplingTree);
/* Used by TransactionAddedToMemorypool/BlockConnected/Disconnected */
void SyncTransaction(const CTransactionRef& tx, const CWalletTx::Confirmation& confirm);
bool IsKeyUsed(const CPubKey& vchPubKey);
struct OutputAvailabilityResult
{
bool available{false};
bool solvable{false};
bool spendable{false};
};
OutputAvailabilityResult CheckOutputAvailability(const CTxOut& output,
const unsigned int outIndex,
const uint256& wtxid,
const CCoinControl* coinControl,
const bool fCoinsSelected,
const bool fIncludeColdStaking,
const bool fIncludeDelegated,
const bool fIncludeLocked) const;
//! Destination --> label/purpose mapping.
std::map<CWDestination, AddressBook::CAddressBookData> mapAddressBook;
public:
static const CAmount DEFAULT_STAKE_SPLIT_THRESHOLD = 500 * COIN;
//! Generates hd wallet //
bool SetupSPKM(bool newKeypool = true, bool memOnly = false);
//! Whether the wallet is hd or not //
bool IsHDEnabled() const;
//! Whether the wallet supports Sapling or not //
bool IsSaplingUpgradeEnabled() const;
/** Get last block processed height */
int GetLastBlockHeight() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
{
AssertLockHeld(cs_wallet);
assert(m_last_block_processed_height >= 0);
return m_last_block_processed_height;
};
/** Set last block processed height, currently only use in unit test */
void SetLastBlockProcessed(const CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
{
AssertLockHeld(cs_wallet);
m_last_block_processed_height = pindex->nHeight;
m_last_block_processed = pindex->GetBlockHash();
m_last_block_processed_time = pindex->GetBlockTime();
};
/* SPKM Helpers */
const CKeyingMaterial& GetEncryptionKey() const;
bool HasEncryptionKeys() const;
//! Get spkm
ScriptPubKeyMan* GetScriptPubKeyMan() const;
SaplingScriptPubKeyMan* GetSaplingScriptPubKeyMan() const { return m_sspk_man.get(); }
bool HasSaplingSPKM() const;
/*
* Main wallet lock.
* This lock protects all the fields added by CWallet.
*/
mutable RecursiveMutex cs_wallet;
bool fWalletUnlockStaking;
CWalletDB* pwalletdbEncryption;
std::map<CKeyID, CKeyMetadata> mapKeyMetadata;
typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
MasterKeyMap mapMasterKeys;
unsigned int nMasterKeyMaxID;
// Stake split threshold
CAmount nStakeSplitThreshold;
// minimum value allowed for nStakeSplitThreshold (customizable with -minstakesplit flag)
static CAmount minStakeSplitThreshold;
// Staker status (last hashed block and time)
CStakerStatus* pStakerStatus = nullptr;
// User-defined fee PIV/kb
bool fUseCustomFee;
CAmount nCustomFee;
//Auto Combine Inputs
bool fCombineDust;
CAmount nAutoCombineThreshold;
/** Get database handle used by this wallet. Ideally this function would
* not be necessary.
*/
CWalletDBWrapper* GetDBHandlePtr() const { return dbw.get(); }
CWalletDBWrapper& GetDBHandle() const { return *dbw; }
/** Get a name for this wallet for logging/debugging purposes.
*/
const std::string& GetName() const { return m_name; }
/** Get the path to the wallet's db file */
fs::path GetPathToDBFile() { return dbw->GetPathToFile(); }
/** Construct wallet with specified name and database implementation. */
CWallet(std::string name, std::unique_ptr<CWalletDBWrapper> dbw_in);
~CWallet();
void SetNull();
std::map<uint256, CWalletTx> mapWallet;
typedef std::multimap<int64_t, CWalletTx*> TxItems;
TxItems wtxOrdered;
int64_t nOrderPosNext;
std::set<COutPoint> setLockedCoins;
int64_t nTimeFirstKey;
// Public SyncMetadata interface used for the sapling spent nullifier map.
void SyncMetaDataN(std::pair<TxSpendMap<uint256>::iterator, TxSpendMap<uint256>::iterator> range);
const CWalletTx* GetWalletTx(const uint256& hash) const;
std::vector<CWalletTx> getWalletTxs();
std::string GetUniqueWalletBackupName() const;
//! check whether we are allowed to upgrade (or already support) to the named feature
bool CanSupportFeature(enum WalletFeature wf);
struct AvailableCoinsFilter {
public:
AvailableCoinsFilter() {}
AvailableCoinsFilter(bool _fIncludeDelegated,
bool _fIncludeColdStaking,
bool _fOnlySafe,
bool _fOnlySpendable,
std::set<CTxDestination>* _onlyFilteredDest,
int _minDepth,
bool _fIncludeLocked = false,
CAmount _nMaxOutValue = 0) :
fIncludeDelegated(_fIncludeDelegated),
fIncludeColdStaking(_fIncludeColdStaking),
fOnlySafe(_fOnlySafe),
fOnlySpendable(_fOnlySpendable),
onlyFilteredDest(_onlyFilteredDest),
minDepth(_minDepth),
fIncludeLocked(_fIncludeLocked),
nMaxOutValue(_nMaxOutValue) {}
bool fIncludeDelegated{true};
bool fIncludeColdStaking{false};
bool fOnlySafe{true};
bool fOnlySpendable{false};
std::set<CTxDestination>* onlyFilteredDest{nullptr};
int minDepth{0};
bool fIncludeLocked{false};
// Select outputs with value <= nMaxOutValue
CAmount nMaxOutValue{0}; // 0 means not active
CAmount nMinOutValue{0}; // 0 means not active
CAmount nMinimumSumAmount{0}; // 0 means not active
unsigned int nMaximumCount{0}; // 0 means not active
};
//! >> Available coins (generic)
bool AvailableCoins(std::vector<COutput>* pCoins, // --> populates when != nullptr
const CCoinControl* coinControl = nullptr,
AvailableCoinsFilter coinsFilter = AvailableCoinsFilter()
) const;
//! >> Available coins (spending)
bool SelectCoinsToSpend(const std::vector<COutput>& vAvailableCoins, const CAmount& nTargetValue, std::set<std::pair<const CWalletTx*, unsigned int> >& setCoinsRet, CAmount& nValueRet, const CCoinControl* coinControl = nullptr) const;
bool SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int nConfTheirs, std::vector<COutput> vCoins, std::set<std::pair<const CWalletTx*, unsigned int> >& setCoinsRet, CAmount& nValueRet) const;
//! >> Available coins (staking)
bool StakeableCoins(std::vector<CStakeableOutput>* pCoins = nullptr);
//! >> Available coins (P2CS)
void GetAvailableP2CSCoins(std::vector<COutput>& vCoins) const;
std::map<CTxDestination, std::vector<COutput> > AvailableCoinsByAddress(bool fConfirmed, CAmount maxCoinValue, bool fIncludeColdStaking);
/**
* Return list of available coins and locked coins grouped by non-change output address.
* PIVX: group coins by pair <CTxDestination, Optional<CTxDestination>>. The optional destination
* is reserved for the staker address in case of P2CS.
*/
std::map<std::pair<CTxDestination, Optional<CTxDestination>>, std::vector<COutput>> ListCoins() const;
/**
* Return list of available shield notes grouped by sapling address.
*/
std::map<libzcash::SaplingPaymentAddress, std::vector<SaplingNoteEntry>> ListNotes() const;
/// Get 10000 PIV output and keys which can be used for the Masternode
bool GetMasternodeVinAndKeys(CTxIn& txinRet, CPubKey& pubKeyRet,
CKey& keyRet, std::string strTxHash, std::string strOutputIndex, std::string& strError);
/// Extract txin information and keys from output
bool GetVinAndKeysFromOutput(COutput out, CTxIn& txinRet, CPubKey& pubKeyRet, CKey& keyRet, bool fColdStake = false);
bool IsSpent(const COutPoint& outpoint) const;
bool IsSpent(const uint256& hash, unsigned int n) const;
bool IsLockedCoin(const uint256& hash, unsigned int n) const;
void LockCoin(const COutPoint& output);
void UnlockCoin(const COutPoint& output);
void UnlockAllCoins();
std::set<COutPoint> ListLockedCoins();
/*
* Rescan abort properties
*/
void AbortRescan() { fAbortRescan = true; }
bool IsAbortingRescan() { return fAbortRescan; }
bool IsScanning() { return fScanningWallet; }
/*
* Stake Split threshold
*/
bool SetStakeSplitThreshold(const CAmount sst);
CAmount GetStakeSplitThreshold() const { LOCK(cs_wallet); return nStakeSplitThreshold; }
/*
* Requires cs_wallet lock.
* Lock for spending the coin c, if it's owned by the wallet, it's unspent, and:
* -- If ptx is not null, c is one of the outputs of *ptx
* -- If ptx is null, c is the output of a transaction in mapWallet
*/
void LockOutpointIfMine(const CTransactionRef& ptx, const COutPoint& c);
/*
* Locks cs_wallet
* Called during Init. If a DMN collateral is found in the wallet,
* lock the corresponding coin, to prevent accidental spending.
*/
void ScanMasternodeCollateralsAndLock(const CDeterministicMNList& mnList);
/*
* Requires cs_wallet lock.
* Called from AddToWalletIfInvolvingMe. If ptx is a ProRegTx, and the
* collateral (either referenced or created) is owned by this wallet,
* lock the corresponding coin, to prevent accidental spending.
*/
void LockIfMyCollateral(const CTransactionRef& ptx);
// keystore implementation
PairResult getNewAddress(CTxDestination& ret, const std::string addressLabel, const std::string purpose,
const CChainParams::Base58Type addrType = CChainParams::PUBKEY_ADDRESS);
PairResult getNewAddress(CTxDestination& ret, std::string label);
PairResult getNewStakingAddress(CTxDestination& ret, std::string label);
int64_t GetKeyCreationTime(const CWDestination& dest);
int64_t GetKeyCreationTime(CPubKey pubkey);
int64_t GetKeyCreationTime(const CTxDestination& address);
int64_t GetKeyCreationTime(const libzcash::SaplingPaymentAddress& address);
//////////// Sapling //////////////////
// Search for notes and addresses from this wallet in the tx, and add the addresses --> IVK mapping to the keystore if missing.
bool FindNotesDataAndAddMissingIVKToKeystore(const CTransaction& tx, Optional<mapSaplingNoteData_t>& saplingNoteData);
// Decrypt sapling output notes with the inputs ovk and updates saplingNoteDataMap
void AddExternalNotesDataToTx(CWalletTx& wtx) const;
//! Generates new Sapling key
libzcash::SaplingPaymentAddress GenerateNewSaplingZKey(std::string label = "");
//! pindex is the new tip being connected.
void IncrementNoteWitnesses(const CBlockIndex* pindex,
const CBlock* pblock,
SaplingMerkleTree& saplingTree);
//! pindex is the old tip being disconnected.
void DecrementNoteWitnesses(const CBlockIndex* pindex);
//! Adds Sapling spending key to the store, and saves it to disk
bool AddSaplingZKey(const libzcash::SaplingExtendedSpendingKey &key);
bool AddSaplingIncomingViewingKeyW(
const libzcash::SaplingIncomingViewingKey &ivk,
const libzcash::SaplingPaymentAddress &addr);
bool AddCryptedSaplingSpendingKeyW(
const libzcash::SaplingExtendedFullViewingKey &extfvk,
const std::vector<unsigned char> &vchCryptedSecret);
//! Returns true if the wallet contains the spending key
bool HaveSpendingKeyForPaymentAddress(const libzcash::SaplingPaymentAddress &zaddr) const;
//! Adds spending key to the store, without saving it to disk (used by LoadWallet)
bool LoadSaplingZKey(const libzcash::SaplingExtendedSpendingKey &key);
//! Load spending key metadata (used by LoadWallet)
bool LoadSaplingZKeyMetadata(const libzcash::SaplingIncomingViewingKey &ivk, const CKeyMetadata &meta);
//! Adds a Sapling payment address -> incoming viewing key map entry,
//! without saving it to disk (used by LoadWallet)
bool LoadSaplingPaymentAddress(
const libzcash::SaplingPaymentAddress &addr,
const libzcash::SaplingIncomingViewingKey &ivk);
//! Adds an encrypted spending key to the store, without saving it to disk (used by LoadWallet)
bool LoadCryptedSaplingZKey(const libzcash::SaplingExtendedFullViewingKey &extfvk,
const std::vector<unsigned char> &vchCryptedSecret);
//////////// End Sapling //////////////
//! Adds a key to the store, and saves it to disk.
bool AddKeyPubKey(const CKey& key, const CPubKey& pubkey) override;
//! Adds a key to the store, without saving it to disk (used by LoadWallet)
bool LoadKey(const CKey& key, const CPubKey& pubkey) { return CCryptoKeyStore::AddKeyPubKey(key, pubkey); }
//! Load metadata (used by LoadWallet)
bool LoadKeyMetadata(const CPubKey& pubkey, const CKeyMetadata& metadata);
bool LoadMinVersion(int nVersion);
void UpdateTimeFirstKey(int64_t nCreateTime);
//! Adds an encrypted key to the store, and saves it to disk.
bool AddCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret) override;
//! Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
bool LoadCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret);
bool AddCScript(const CScript& redeemScript) override;
bool LoadCScript(const CScript& redeemScript);
//! Adds a destination data tuple to the store, and saves it to disk
bool AddDestData(const CTxDestination& dest, const std::string& key, const std::string& value);
//! Erases a destination data tuple in the store and on disk
bool EraseDestData(const CTxDestination& dest, const std::string& key);
//! Adds a destination data tuple to the store, without saving it to disk
bool LoadDestData(const CTxDestination& dest, const std::string& key, const std::string& value);
//! Get all destination values matching a prefix.
std::vector<std::string> GetDestValues(const std::string& prefix) const;
//! Adds a watch-only address to the store, and saves it to disk.
bool AddWatchOnly(const CScript& dest) override;
bool RemoveWatchOnly(const CScript& dest) override;
//! Adds a watch-only address to the store, without saving it to disk (used by LoadWallet)
bool LoadWatchOnly(const CScript& dest);
//! Lock Wallet
//! Holds a timestamp at which point the wallet is scheduled (externally) to be relocked. Caller must arrange for actual relocking to occur via Lock().
int64_t nRelockTime;
bool Lock();
bool Unlock(const SecureString& strWalletPassphrase, bool anonimizeOnly = false);
bool Unlock(const CKeyingMaterial& vMasterKeyIn);
bool ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase);
bool EncryptWallet(const SecureString& strWalletPassphrase);
std::vector<CKeyID> GetAffectedKeys(const CScript& spk);
void GetKeyBirthTimes(std::map<CKeyID, int64_t>& mapKeyBirth) const;
/**
* Increment the next transaction order id
* @return next transaction order id
*/
int64_t IncOrderPosNext(CWalletDB* pwalletdb = NULL);
void MarkDirty();
bool AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose = true);
bool LoadToWallet(CWalletTx& wtxIn);
void TransactionAddedToMempool(const CTransactionRef& tx) override;
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex *pindex) override;
void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const uint256& blockHash, int nBlockHeight, int64_t blockTime) override;