1
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
- // Copyright (c) 2009-2014 The Bitcoin developers
2
+ // Copyright (c) 2009-2021 The Bitcoin developers
3
3
// Copyright (c) 2014-2015 The Dash developers
4
- // Copyright (c) 2015-2020 The PIVX developers
4
+ // Copyright (c) 2015-2021 The PIVX developers
5
5
// Distributed under the MIT/X11 software license, see the accompanying
6
6
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
7
7
@@ -100,8 +100,9 @@ bool CWalletDB::EraseTx(uint256 hash)
100
100
101
101
bool CWalletDB::WriteKey (const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata& keyMeta)
102
102
{
103
- if (!WriteIC (std::make_pair (std::string (DBKeys::KEYMETA), vchPubKey), keyMeta, false ))
103
+ if (!WriteIC (std::make_pair (std::string (DBKeys::KEYMETA), vchPubKey), keyMeta, false )) {
104
104
return false ;
105
+ }
105
106
106
107
// hash pubkey/privkey to accelerate wallet load
107
108
std::vector<unsigned char > vchKey;
@@ -113,16 +114,18 @@ bool CWalletDB::WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, c
113
114
}
114
115
115
116
bool CWalletDB::WriteCryptedKey (const CPubKey& vchPubKey,
116
- const std::vector<unsigned char >& vchCryptedSecret,
117
- const CKeyMetadata& keyMeta)
117
+ const std::vector<unsigned char >& vchCryptedSecret,
118
+ const CKeyMetadata& keyMeta)
118
119
{
119
120
const bool fEraseUnencryptedKey = true ;
120
121
121
- if (!WriteIC (std::make_pair (std::string (DBKeys::KEYMETA), vchPubKey), keyMeta))
122
+ if (!WriteIC (std::make_pair (std::string (DBKeys::KEYMETA), vchPubKey), keyMeta)) {
122
123
return false ;
124
+ }
123
125
124
- if (!WriteIC (std::make_pair (std::string (DBKeys::CRYPTED_KEY), vchPubKey), vchCryptedSecret, false ))
126
+ if (!WriteIC (std::make_pair (std::string (DBKeys::CRYPTED_KEY), vchPubKey), vchCryptedSecret, false )) {
125
127
return false ;
128
+ }
126
129
if (fEraseUnencryptedKey ) {
127
130
EraseIC (std::make_pair (std::string (DBKeys::KEY), vchPubKey));
128
131
}
@@ -134,32 +137,34 @@ bool CWalletDB::WriteSaplingZKey(const libzcash::SaplingIncomingViewingKey &ivk,
134
137
const libzcash::SaplingExtendedSpendingKey &key,
135
138
const CKeyMetadata &keyMeta)
136
139
{
137
- if (!WriteIC (std::make_pair (std::string (DBKeys::SAP_KEYMETA), ivk), keyMeta))
140
+ if (!WriteIC (std::make_pair (std::string (DBKeys::SAP_KEYMETA), ivk), keyMeta)) {
138
141
return false ;
142
+ }
139
143
140
144
return WriteIC (std::make_pair (std::string (DBKeys::SAP_KEY), ivk), key, false );
141
145
}
142
146
143
- bool CWalletDB::WriteSaplingPaymentAddress (
144
- const libzcash::SaplingPaymentAddress &addr,
145
- const libzcash::SaplingIncomingViewingKey &ivk)
147
+ bool CWalletDB::WriteSaplingPaymentAddress (const libzcash::SaplingPaymentAddress &addr,
148
+ const libzcash::SaplingIncomingViewingKey &ivk)
146
149
{
147
150
return WriteIC (std::make_pair (std::string (DBKeys::SAP_ADDR), addr), ivk, false );
148
151
}
149
152
150
- bool CWalletDB::WriteCryptedSaplingZKey (
151
- const libzcash::SaplingExtendedFullViewingKey &extfvk,
152
- const std::vector<unsigned char >& vchCryptedSecret,
153
- const CKeyMetadata &keyMeta)
153
+ bool CWalletDB::WriteCryptedSaplingZKey (const libzcash::SaplingExtendedFullViewingKey &extfvk,
154
+ const std::vector<unsigned char >& vchCryptedSecret,
155
+ const CKeyMetadata &keyMeta)
154
156
{
155
157
const bool fEraseUnencryptedKey = true ;
156
158
auto ivk = extfvk.fvk .in_viewing_key ();
157
159
158
- if (!WriteIC (std::make_pair (std::string (DBKeys::SAP_KEYMETA), ivk), keyMeta))
160
+ if (!WriteIC (std::make_pair (std::string (DBKeys::SAP_KEYMETA), ivk), keyMeta)) {
159
161
return false ;
162
+ }
160
163
161
- if (!WriteIC (std::make_pair (std::string (DBKeys::SAP_KEY_CRIPTED), ivk), std::make_pair (extfvk, vchCryptedSecret), false ))
164
+ if (!WriteIC (std::make_pair (std::string (DBKeys::SAP_KEY_CRIPTED), ivk),
165
+ std::make_pair (extfvk, vchCryptedSecret), false )) {
162
166
return false ;
167
+ }
163
168
164
169
if (fEraseUnencryptedKey ) {
165
170
EraseIC (std::make_pair (std::string (DBKeys::SAP_KEY), ivk));
@@ -210,7 +215,9 @@ bool CWalletDB::WriteBestBlock(const CBlockLocator& locator)
210
215
211
216
bool CWalletDB::ReadBestBlock (CBlockLocator& locator)
212
217
{
213
- if (batch.Read (std::string (DBKeys::BESTBLOCK), locator) && !locator.vHave .empty ()) return true ;
218
+ if (batch.Read (std::string (DBKeys::BESTBLOCK), locator) && !locator.vHave .empty ()) {
219
+ return true ;
220
+ }
214
221
return batch.Read (std::string (DBKeys::BESTBLOCK_NOMERKLE), locator);
215
222
}
216
223
@@ -279,7 +286,7 @@ DBErrors CWalletDB::ReorderTransactions(CWallet* pwallet)
279
286
typedef std::multimap<int64_t , CWalletTx*> TxItems;
280
287
TxItems txByTime;
281
288
282
- for (std::map<uint256, CWalletTx>::iterator it = pwallet->mapWallet .begin (); it != pwallet->mapWallet .end (); ++it) {
289
+ for (auto it = pwallet->mapWallet .begin (); it != pwallet->mapWallet .end (); ++it) {
283
290
CWalletTx* wtx = &((*it).second );
284
291
txByTime.insert (std::make_pair (wtx->nTimeReceived , wtx));
285
292
}
@@ -295,8 +302,8 @@ DBErrors CWalletDB::ReorderTransactions(CWallet* pwallet)
295
302
nOrderPos = nOrderPosNext++;
296
303
nOrderPosOffsets.push_back (nOrderPos);
297
304
298
- if (!WriteTx (*pwtx))
299
- return DB_LOAD_FAIL;
305
+ if (!WriteTx (*pwtx)) return DB_LOAD_FAIL;
306
+
300
307
} else {
301
308
int64_t nOrderPosOff = 0 ;
302
309
for (const int64_t & nOffsetStart : nOrderPosOffsets) {
@@ -306,12 +313,10 @@ DBErrors CWalletDB::ReorderTransactions(CWallet* pwallet)
306
313
nOrderPos += nOrderPosOff;
307
314
nOrderPosNext = std::max (nOrderPosNext, nOrderPos + 1 );
308
315
309
- if (!nOrderPosOff)
310
- continue ;
316
+ if (!nOrderPosOff) continue ;
311
317
312
318
// Since we're changing the order, write it back
313
- if (!WriteTx (*pwtx))
314
- return DB_LOAD_FAIL;
319
+ if (!WriteTx (*pwtx)) return DB_LOAD_FAIL;
315
320
}
316
321
}
317
322
WriteOrderPosNext (nOrderPosNext);
@@ -626,8 +631,9 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
626
631
try {
627
632
int nMinVersion = 0 ;
628
633
if (batch.Read ((std::string) DBKeys::MINVERSION, nMinVersion)) {
629
- if (nMinVersion > CLIENT_VERSION)
634
+ if (nMinVersion > CLIENT_VERSION) {
630
635
return DB_TOO_NEW;
636
+ }
631
637
pwallet->LoadMinVersion (nMinVersion);
632
638
}
633
639
@@ -643,9 +649,9 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
643
649
CDataStream ssKey (SER_DISK, CLIENT_VERSION);
644
650
CDataStream ssValue (SER_DISK, CLIENT_VERSION);
645
651
int ret = batch.ReadAtCursor (pcursor, ssKey, ssValue);
646
- if (ret == DB_NOTFOUND)
652
+ if (ret == DB_NOTFOUND) {
647
653
break ;
648
- else if (ret != 0 ) {
654
+ } else if (ret != 0 ) {
649
655
LogPrintf (" Error reading next record from wallet database\n " );
650
656
return DB_CORRUPT;
651
657
}
@@ -655,9 +661,9 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
655
661
if (!ReadKeyValue (pwallet, ssKey, ssValue, wss, strType, strErr)) {
656
662
// losing keys is considered a catastrophic error, anything else
657
663
// we assume the user can live with:
658
- if (IsKeyType (strType) || strType == DBKeys::DEFAULTKEY)
664
+ if (IsKeyType (strType) || strType == DBKeys::DEFAULTKEY) {
659
665
result = DB_CORRUPT;
660
- else {
666
+ } else {
661
667
// Leave other errors alone, if we try to fix them we might make things worse.
662
668
fNoncriticalErrors = true ; // ... but do warn the user there is something wrong.
663
669
if (strType == DBKeys::TX)
@@ -695,18 +701,22 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
695
701
if ((wss.nKeys + wss.nCKeys ) != wss.nKeyMeta )
696
702
pwallet->nTimeFirstKey = 1 ; // 0 would be considered 'no value'
697
703
698
- for (const uint256& hash : wss.vWalletUpgrade )
704
+ for (const uint256& hash : wss.vWalletUpgrade ) {
699
705
WriteTx (pwallet->mapWallet .at (hash));
706
+ }
700
707
701
708
// Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
702
- if (wss.fIsEncrypted && (wss.nFileVersion == 40000 || wss.nFileVersion == 50000 ))
709
+ if (wss.fIsEncrypted && (wss.nFileVersion == 40000 || wss.nFileVersion == 50000 )) {
703
710
return DB_NEED_REWRITE;
711
+ }
704
712
705
- if (wss.nFileVersion < CLIENT_VERSION) // Update
713
+ if (wss.nFileVersion < CLIENT_VERSION) { // Update
706
714
WriteVersion (CLIENT_VERSION);
715
+ }
707
716
708
- if (wss.fAnyUnordered )
717
+ if (wss.fAnyUnordered ) {
709
718
result = ReorderTransactions (pwallet);
719
+ }
710
720
711
721
return result;
712
722
}
@@ -720,8 +730,9 @@ DBErrors CWalletDB::FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash
720
730
LOCK (pwallet->cs_wallet );
721
731
int nMinVersion = 0 ;
722
732
if (batch.Read ((std::string) DBKeys::MINVERSION, nMinVersion)) {
723
- if (nMinVersion > CLIENT_VERSION)
733
+ if (nMinVersion > CLIENT_VERSION) {
724
734
return DB_TOO_NEW;
735
+ }
725
736
pwallet->LoadMinVersion (nMinVersion);
726
737
}
727
738
@@ -737,9 +748,9 @@ DBErrors CWalletDB::FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash
737
748
CDataStream ssKey (SER_DISK, CLIENT_VERSION);
738
749
CDataStream ssValue (SER_DISK, CLIENT_VERSION);
739
750
int ret = batch.ReadAtCursor (pcursor, ssKey, ssValue);
740
- if (ret == DB_NOTFOUND)
751
+ if (ret == DB_NOTFOUND) {
741
752
break ;
742
- else if (ret != 0 ) {
753
+ } else if (ret != 0 ) {
743
754
LogPrintf (" Error reading next record from wallet database\n " );
744
755
return DB_CORRUPT;
745
756
}
@@ -775,13 +786,13 @@ DBErrors CWalletDB::ZapWalletTx(CWallet* pwallet, std::vector<CWalletTx>& vWtx)
775
786
// build list of wallet TXs
776
787
std::vector<uint256> vTxHash;
777
788
DBErrors err = FindWalletTx (pwallet, vTxHash, vWtx);
778
- if (err != DB_LOAD_OK)
789
+ if (err != DB_LOAD_OK) {
779
790
return err;
791
+ }
780
792
781
793
// erase each wallet TX
782
794
for (uint256& hash : vTxHash) {
783
- if (!EraseTx (hash))
784
- return DB_CORRUPT;
795
+ if (!EraseTx (hash)) return DB_CORRUPT;
785
796
}
786
797
787
798
return DB_LOAD_OK;
0 commit comments