Skip to content

Commit 159bbb6

Browse files
authored
Merge pull request duality-solutions#359 from duality-solutions/v2.4.22-keypools
Lower size of keypools
2 parents a747da8 + c3b7106 commit 159bbb6

File tree

4 files changed

+127
-15
lines changed

4 files changed

+127
-15
lines changed

src/privatesend-client.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ static const bool DEFAULT_PRIVATESEND_MULTISESSION = false;
3333

3434
// Warn user if mixing in gui or try to create backup if mixing in daemon mode
3535
// when we have only this many keys left
36-
static const int PRIVATESEND_KEYS_THRESHOLD_WARNING = 100;
36+
static const int PRIVATESEND_KEYS_THRESHOLD_WARNING = 50;
3737
// Stop mixing completely, it's too dangerous to continue when we have only this many keys left
38-
static const int PRIVATESEND_KEYS_THRESHOLD_STOP = 50;
38+
static const int PRIVATESEND_KEYS_THRESHOLD_STOP = 25;
3939

4040
// The main object for accessing mixing
4141
extern CPrivateSendClientManager privateSendClient;

src/validation.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -626,10 +626,6 @@ static bool IsCurrentForFeeEstimation()
626626
// Check if BDAP entry is valid
627627
bool ValidateBDAPInputs(const CTransactionRef& tx, CValidationState& state, const CCoinsViewCache& inputs, const CBlock& block, bool fJustCheck, int nHeight, bool bSanity)
628628
{
629-
// Do not check while wallet is loading
630-
if (!fLoaded)
631-
return true;
632-
633629
if (!CheckDomainEntryDB())
634630
return true;
635631

@@ -1946,7 +1942,7 @@ static DisconnectResult DisconnectBlock(const CBlock& block, CValidationState& s
19461942
uint256 hash = tx.GetHash();
19471943
bool is_coinbase = tx.IsCoinBase();
19481944
bool fIsBDAP = tx.nVersion == BDAP_TX_VERSION;
1949-
if (fIsBDAP) {
1945+
if (fIsBDAP && !fReindex) {
19501946
LogPrintf("%s -- BDAP tx found. Hash %s\n", __func__, hash.ToString());
19511947
// get BDAP object
19521948
CScript scriptBDAPOp;

src/wallet/wallet.cpp

Lines changed: 112 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,40 @@ bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlockIndex
14411441
}
14421442
}
14431443
}
1444-
}
1444+
} // if tx.nversion END
1445+
else {
1446+
BOOST_FOREACH (const CTxOut& txout, tx.vout) {
1447+
CScript scriptPubKey = txout.scriptPubKey;
1448+
CTxDestination address1;
1449+
bool ReserveKey = true;
1450+
1451+
if (!ExtractDestination(scriptPubKey, address1)) {
1452+
ReserveKey = false;
1453+
}
1454+
1455+
CDynamicAddress address2(address1);
1456+
CKeyID keyID;
1457+
1458+
if (!address2.GetKeyID(keyID)) {
1459+
ReserveKey = false;
1460+
}
1461+
CKey vchSecret;
1462+
if (!GetKey(keyID, vchSecret)) {
1463+
ReserveKey = false;
1464+
}
1465+
1466+
CPubKey retrievePubKey;
1467+
1468+
if (ReserveKey) {
1469+
retrievePubKey = vchSecret.GetPubKey();
1470+
if (ReserveKeyForTransactions(retrievePubKey)) {
1471+
TopUpKeyPoolCombo(0,true);
1472+
fNeedToRescanTransactions = true;
1473+
}
1474+
}
1475+
1476+
} //BOOST_FOREACH END
1477+
}
14451478

14461479
CWalletTx wtx(this, ptx);
14471480

@@ -1595,7 +1628,7 @@ void CWallet::SyncTransaction(const CTransaction& tx, const CBlockIndex* pindex,
15951628
fAnonymizableTallyCachedNonDenom = false;
15961629

15971630
if (fNeedToUpdateKeyPools) {
1598-
TopUpKeyPoolCombo();
1631+
TopUpKeyPoolCombo(0,true);
15991632
fNeedToUpdateKeyPools = false;
16001633
}
16011634

@@ -2119,10 +2152,15 @@ CBlockIndex* CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool f
21192152
CBlock block;
21202153
if (ReadBlockFromDisk(block, pindex, Params().GetConsensus())) {
21212154
for (size_t posInBlock = 0; posInBlock < block.vtx.size(); ++posInBlock) {
2122-
AddToWalletIfInvolvingMe(*block.vtx[posInBlock], pindex, posInBlock, fUpdate);
2155+
AddToWalletIfInvolvingMe(*block.vtx[posInBlock], pindex, posInBlock, fUpdate);
2156+
2157+
if (SaveRescanIndex) {
2158+
rescan_index = pindex;
2159+
SaveRescanIndex = false;
2160+
}
21232161

21242162
if (fNeedToUpdateKeyPools) {
2125-
TopUpKeyPoolCombo();
2163+
TopUpKeyPoolCombo(0,true);
21262164
fNeedToUpdateKeyPools = false;
21272165
}
21282166

@@ -4581,7 +4619,7 @@ bool CWallet::SyncEdKeyPool()
45814619

45824620
} //SyncEdKeyPool
45834621

4584-
bool CWallet::TopUpKeyPoolCombo(unsigned int kpSize)
4622+
bool CWallet::TopUpKeyPoolCombo(unsigned int kpSize, bool fIncreaseSize)
45854623
{
45864624
{
45874625
LOCK(cs_wallet);
@@ -4593,8 +4631,9 @@ bool CWallet::TopUpKeyPoolCombo(unsigned int kpSize)
45934631
unsigned int nTargetSize;
45944632
if (kpSize > 0)
45954633
nTargetSize = kpSize;
4596-
else
4634+
else {
45974635
nTargetSize = std::max(GetArg("-keypool", DEFAULT_KEYPOOL_SIZE), (int64_t)0);
4636+
}
45984637

45994638
// count amount of available keys (internal, external)
46004639
// make sure the keypool of external and internal keys fits the user selected target (-keypool)
@@ -4603,6 +4642,11 @@ bool CWallet::TopUpKeyPoolCombo(unsigned int kpSize)
46034642
int64_t missingExternal = std::max(std::max((int64_t)nTargetSize, (int64_t)1) - amountExternal, (int64_t)0);
46044643
int64_t missingInternal = std::max(std::max((int64_t)nTargetSize, (int64_t)1) - amountInternal, (int64_t)0);
46054644

4645+
if (fIncreaseSize) {
4646+
missingExternal = missingExternal + 2;
4647+
missingInternal = missingInternal + 2;
4648+
}
4649+
46064650
if (!IsHDEnabled()) {
46074651
// don't create extra internal keys
46084652
missingInternal = 0;
@@ -4661,6 +4705,9 @@ bool CWallet::TopUpKeyPoolCombo(unsigned int kpSize)
46614705

46624706
void CWallet::UpdateKeyPoolsFromTransactions(const std::string& strOpType, const std::vector<std::vector<unsigned char>>& vvchOpParameters)
46634707
{
4708+
if (vvchOpParameters.size() <= 1)
4709+
return;
4710+
46644711
std::vector<unsigned char> key0 = vvchOpParameters[0];
46654712
std::vector<unsigned char> key1 = vvchOpParameters[1];
46664713

@@ -4748,6 +4795,7 @@ void CWallet::ReserveEdKeyForTransactions(const std::vector<unsigned char>& pubK
47484795
fNeedToUpdateKeyPools = true;
47494796
EraseIndex = true;
47504797
IndexToErase = nIndex;
4798+
ReserveKeyCount++;
47514799
}
47524800
it++;
47534801
}
@@ -4763,6 +4811,50 @@ void CWallet::ReserveEdKeyForTransactions(const std::vector<unsigned char>& pubK
47634811

47644812
} //ReserveEdKeyForTransactions
47654813

4814+
bool CWallet::ReserveKeyForTransactions(const CPubKey pubKeyToReserve)
4815+
{
4816+
bool foundPubKey = false;
4817+
4818+
CWalletDB walletdb(strWalletFile);
4819+
CKeyPool keypool;
4820+
CPubKey PubKey;
4821+
std::vector<int64_t> keypoolIndexes;
4822+
bool EraseIndex = false;
4823+
int64_t IndexToErase = 0;
4824+
int64_t nIndex = 0;
4825+
std::set<std::int64_t>::iterator it = setExternalKeyPool.begin();
4826+
4827+
while ((it != setExternalKeyPool.end()) && (!EraseIndex)) {
4828+
nIndex = *it;
4829+
if (!walletdb.ReadPool(nIndex, keypool)) {
4830+
throw std::runtime_error(std::string(__func__) + ": read failed");
4831+
}
4832+
PubKey = keypool.vchPubKey;
4833+
4834+
if(pubKeyToReserve == PubKey) {
4835+
foundPubKey = true;
4836+
KeepKey(nIndex);
4837+
EraseIndex = true;
4838+
IndexToErase = nIndex;
4839+
ReserveKeyCount++;
4840+
SaveRescanIndex = true;
4841+
}
4842+
it++;
4843+
}
4844+
4845+
if (EraseIndex) {
4846+
std::set<int64_t>::iterator eraseIndexEd = setExternalEdKeyPool.find(IndexToErase);
4847+
std::set<int64_t>::iterator eraseIndex = setExternalKeyPool.find(IndexToErase);
4848+
if (eraseIndexEd != setExternalKeyPool.end())
4849+
setExternalEdKeyPool.erase(eraseIndexEd);
4850+
if (eraseIndex != setExternalKeyPool.end())
4851+
setExternalKeyPool.erase(eraseIndex);
4852+
}
4853+
4854+
return foundPubKey;
4855+
4856+
} //ReserveKeyForTransactions
4857+
47664858
void CWallet::KeepKey(int64_t nIndex)
47674859
{
47684860
// Remove from key pool
@@ -5537,6 +5629,20 @@ CWallet* CWallet::CreateWalletFromFile(const std::string walletFile, const bool
55375629
LogPrintf("Rescanning last %i blocks (from block %i)...\n", chainActive.Height() - pindexRescan->nHeight, pindexRescan->nHeight);
55385630
nStart = GetTimeMillis();
55395631
walletInstance->ScanForWalletTransactions(pindexRescan, true);
5632+
5633+
//rescan if boolean is set. go back 100 transactions from most recent transaction involving me.
5634+
while ((walletInstance->fNeedToRescanTransactions) && (walletInstance->ReserveKeyCount > DEFAULT_RESCAN_THRESHOLD)) {
5635+
//initialize values
5636+
walletInstance->fNeedToRescanTransactions = false;
5637+
walletInstance->ReserveKeyCount = 0;
5638+
5639+
CBlockIndex* computed_rescan_index = walletInstance->rescan_index;
5640+
if (computed_rescan_index->nHeight > 100) {
5641+
computed_rescan_index = chainActive[computed_rescan_index->nHeight - 100];
5642+
}
5643+
walletInstance->ScanForWalletTransactions(computed_rescan_index, true);
5644+
}
5645+
55405646
LogPrintf(" rescan %15dms\n", GetTimeMillis() - nStart);
55415647
walletInstance->SetBestChain(chainActive.GetLocator());
55425648
CWalletDB::IncrementUpdateCounter();

src/wallet/wallet.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ extern unsigned int nTxConfirmTarget;
4949
extern bool bSpendZeroConfChange;
5050
extern bool fSendFreeTransactions;
5151

52-
static const unsigned int DEFAULT_KEYPOOL_SIZE = 1000;
52+
//Set the following 2 constants together
53+
static const unsigned int DEFAULT_KEYPOOL_SIZE = 200;
54+
static const int DEFAULT_RESCAN_THRESHOLD = 150;
55+
5356
//! -paytxfee default
5457
static const CAmount DEFAULT_TRANSACTION_FEE = 0;
5558
//! -fallbackfee default
@@ -789,6 +792,8 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
789792

790793
void ReserveEdKeyForTransactions(const std::vector<unsigned char>& pubKeyToReserve);
791794

795+
bool ReserveKeyForTransactions(const CPubKey pubKeyToReserve);
796+
792797
std::array<char, 32> ConvertSecureVector32ToArray(const std::vector<unsigned char, secure_allocator<unsigned char> >& vIn);
793798

794799
bool fFileBacked;
@@ -826,6 +831,11 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
826831

827832
const std::string strWalletFile;
828833

834+
bool fNeedToRescanTransactions = false;
835+
CBlockIndex* rescan_index = nullptr;
836+
int ReserveKeyCount = 0;
837+
bool SaveRescanIndex = false;
838+
829839
bool WalletNeedsUpgrading()
830840
{
831841
return fNeedToUpgradeWallet;
@@ -1148,7 +1158,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
11481158
size_t EdKeypoolCountExternalKeys();
11491159
size_t EdKeypoolCountInternalKeys();
11501160
bool SyncEdKeyPool();
1151-
bool TopUpKeyPoolCombo(unsigned int kpSize = 0);
1161+
bool TopUpKeyPoolCombo(unsigned int kpSize = 0, bool fIncreaseSize = false);
11521162
void ReserveKeysFromKeyPools(int64_t& nIndex, CKeyPool& keypool, CEdKeyPool& edkeypool, bool fInternal);
11531163
void KeepKey(int64_t nIndex);
11541164
void ReturnKey(int64_t nIndex, bool fInternal);

0 commit comments

Comments
 (0)