Expire confirmed DSTXes after ~1h since confirmation (#1499)

* Expire confirmed DSTXes after ~1h since confirmation

* add missing cs_main lock
This commit is contained in:
UdjinM6 2017-07-10 17:42:09 +03:00 committed by GitHub
parent 70eb83a5ce
commit 739ef9a681
4 changed files with 66 additions and 5 deletions

View File

@ -3,12 +3,12 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "dsnotificationinterface.h"
#include "privatesend-client.h"
#include "instantx.h"
#include "governance.h"
#include "masternodeman.h"
#include "masternode-payments.h"
#include "masternode-sync.h"
#include "privatesend-client.h"
CDSNotificationInterface::CDSNotificationInterface()
{
@ -31,4 +31,5 @@ void CDSNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindex)
void CDSNotificationInterface::SyncTransaction(const CTransaction &tx, const CBlock *pblock)
{
instantsend.SyncTransaction(tx, pblock);
CPrivateSend::SyncTransaction(tx, pblock);
}

View File

@ -1374,6 +1374,8 @@ void CPrivateSendClient::UpdatedBlockTip(const CBlockIndex *pindex)
if(!fLiteMode && masternodeSync.IsMasternodeListSynced()) {
NewBlock();
}
CPrivateSend::CheckDSTXes(pindex->nHeight);
}
//TODO: Rename/move to core

View File

@ -110,6 +110,12 @@ bool CDarksendBroadcastTx::CheckSignature(const CPubKey& pubKeyMasternode)
return true;
}
bool CDarksendBroadcastTx::IsExpired(int nHeight)
{
// expire confirmed DSTXes after ~1h since confirmation
return (nConfirmedHeight != -1) && (nHeight - nConfirmedHeight > 24);
}
void CPrivateSendBase::SetNull()
{
// Both sides
@ -380,6 +386,45 @@ CDarksendBroadcastTx CPrivateSend::GetDSTX(const uint256& hash)
return (it == mapDSTX.end()) ? CDarksendBroadcastTx() : it->second;
}
void CPrivateSend::CheckDSTXes(int nHeight)
{
LOCK(cs_mapdstx);
std::map<uint256, CDarksendBroadcastTx>::iterator it = mapDSTX.begin();
while(it != mapDSTX.end()) {
if (it->second.IsExpired(nHeight)) {
mapDSTX.erase(it++);
} else {
++it;
}
}
LogPrint("privatesend", "CPrivateSend::CheckDSTXes -- mapDSTX.size()=%llu\n", mapDSTX.size());
}
void CPrivateSend::SyncTransaction(const CTransaction& tx, const CBlock* pblock)
{
if (tx.IsCoinBase()) return;
LOCK2(cs_main, cs_mapdstx);
uint256 txHash = tx.GetHash();
if (!mapDSTX.count(txHash)) return;
// When tx is 0-confirmed or conflicted, pblock is NULL and nConfirmedHeight should be set to -1
CBlockIndex* pblockindex = NULL;
if(pblock) {
uint256 blockHash = pblock->GetHash();
BlockMap::iterator mi = mapBlockIndex.find(blockHash);
if(mi == mapBlockIndex.end() || !mi->second) {
// shouldn't happen
LogPrint("privatesend", "CPrivateSendClient::SyncTransaction -- Failed to find block %s\n", blockHash.ToString());
return;
}
pblockindex = mi->second;
}
mapDSTX[txHash].SetConfirmedHeight(pblockindex ? pblockindex->nHeight : -1);
LogPrint("privatesend", "CPrivateSendClient::SyncTransaction -- txid=%s\n", txHash.ToString());
}
//TODO: Rename/move to core
void ThreadCheckPrivateSend()
{

View File

@ -214,6 +214,11 @@ public:
*/
class CDarksendBroadcastTx
{
private:
// memory only
// when corresponding tx is 0-confirmed or conflicted, nConfirmedHeight is -1
int nConfirmedHeight;
public:
CTransaction tx;
CTxIn vin;
@ -221,16 +226,18 @@ public:
int64_t sigTime;
CDarksendBroadcastTx() :
tx(CTransaction()),
vin(CTxIn()),
vchSig(std::vector<unsigned char>()),
nConfirmedHeight(-1),
tx(),
vin(),
vchSig(),
sigTime(0)
{}
CDarksendBroadcastTx(CTransaction tx, CTxIn vin, int64_t sigTime) :
nConfirmedHeight(-1),
tx(tx),
vin(vin),
vchSig(std::vector<unsigned char>()),
vchSig(),
sigTime(sigTime)
{}
@ -259,6 +266,9 @@ public:
bool Sign();
bool CheckSignature(const CPubKey& pubKeyMasternode);
void SetConfirmedHeight(int nConfirmedHeightIn) { nConfirmedHeight = nConfirmedHeightIn; }
bool IsExpired(int nHeight);
};
// base class
@ -337,6 +347,9 @@ public:
static void AddDSTX(const CDarksendBroadcastTx& dstx);
static CDarksendBroadcastTx GetDSTX(const uint256& hash);
static void CheckDSTXes(int nHeight);
static void SyncTransaction(const CTransaction& tx, const CBlock* pblock);
};
void ThreadCheckPrivateSend();