Skip to content

Commit

Permalink
Add coin cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar committed Dec 6, 2018
1 parent 70cb02c commit 1dc60fd
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "key.h"
#include "keystore.h"
#include "main.h"
#include "miner.h"
#include "net.h"
#include "policy/policy.h"
#include "primitives/block.h"
Expand Down Expand Up @@ -46,6 +47,11 @@ bool fWalletUnlockStakingOnly = false;
const char * DEFAULT_WALLET_DAT = "wallet.dat";
const uint32_t BIP32_HARDENED_KEY_LIMIT = 0x80000000;

set<pair<const CWalletTx*, unsigned int> > setCoinsCache;
int64_t nCoinsCacheValue = 0;
unsigned int nCoinsCacheTime = 0;
const unsigned int nCoinsCacheInterval = 600; /* 10 minutes */

/**
* Fees smaller than this (in satoshi) are considered zero fee (for transaction creation)
* Override with -mintxfee
Expand Down Expand Up @@ -2588,6 +2594,14 @@ bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey)
}
}
}

/* Clear if staking is active */
if (GetStaking()) {
setCoinsCache.clear();
nCoinsCacheValue = 0;
nCoinsCacheTime = 0;
}

return true;
}

Expand Down Expand Up @@ -3951,7 +3965,7 @@ bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int

/* Fail if the minimal stake amount not reached */
if (nCredit < MIN_STAKE_AMOUNT)
return error("CreateCoinStake() : stake amount below the minimum");
return error("%s: stake amount below the minimum", __func__);

nCredit += nStakeReward;

Expand Down Expand Up @@ -3986,6 +4000,11 @@ bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int
if (nBytes >= MAX_BLOCK_SIZE_GEN / 5)
return error("%s: exceeded coinstake size limit", __func__);

/* Clear inputs cached */
setCoinsCache.clear();
nCoinsCacheValue = 0;
nCoinsCacheTime = 0;

// Successfully generated coinstake
return true;
}
Expand All @@ -3994,6 +4013,15 @@ bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int
bool CWallet::SelectCoinsForStaking(int64_t nTargetValue, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet) const
{
unsigned int nCurrentTime = GetTime();
if (nCurrentTime < (nCoinsCacheTime + nCoinsCacheInterval)) {
/* Try to re-use inputs cached */
if (setCoinsCache.size()) {
setCoinsRet = setCoinsCache;
nValueRet = nCoinsCacheValue;
return true;
}
}

int nDepth;
vector<COutput> vCoins;
vCoins.clear();
Expand Down Expand Up @@ -4061,5 +4089,11 @@ bool CWallet::SelectCoinsForStaking(int64_t nTargetValue, set<pair<const CWallet
}
}

/* Clear and reload the cache */
setCoinsCache.clear();
setCoinsCache = setCoinsRet;
nCoinsCacheValue = nValueRet;
nCoinsCacheTime = nCurrentTime;

return true;
}

0 comments on commit 1dc60fd

Please sign in to comment.