Skip to content

Commit

Permalink
Save/load InstantSend cache (#2051)
Browse files Browse the repository at this point in the history
* CTxLockCandidate serialization

* COutPointLock serialization

* CInstantSend serialization

* Read/write instantsend.dat file

* Read\write InstantSend cache only if InstantSend is enabled
  • Loading branch information
gladcow authored and UdjinM6 committed Jul 12, 2018
1 parent a527845 commit 2c0d4c9
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ void PrepareShutdown()
flatdb3.Dump(governance);
CFlatDB<CNetFulfilledRequestManager> flatdb4("netfulfilled.dat", "magicFulfilledCache");
flatdb4.Dump(netfulfilledman);
if(fEnableInstantSend)
{
CFlatDB<CInstantSend> flatdb5("instantsend.dat", "magicInstantSendCache");
flatdb5.Dump(instantsend);
}
}

UnregisterNodeSignals(GetNodeSignals());
Expand Down Expand Up @@ -1914,6 +1919,16 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
if(!flatdb4.Load(netfulfilledman)) {
return InitError(_("Failed to load fulfilled requests cache from") + "\n" + (pathDB / strDBName).string());
}

if(fEnableInstantSend)
{
strDBName = "instantsend.dat";
uiInterface.InitMessage(_("Loading InstantSend data cache..."));
CFlatDB<CInstantSend> flatdb5(strDBName, "magicInstantSendCache");
if(!flatdb5.Load(instantsend)) {
return InitError(_("Failed to load InstantSend data cache from") + "\n" + (pathDB / strDBName).string());
}
}
}


Expand Down
18 changes: 17 additions & 1 deletion src/instantx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ int nInstantSendDepth = DEFAULT_INSTANTSEND_DEPTH;
int nCompleteTXLocks;

CInstantSend instantsend;
const std::string CInstantSend::SERIALIZATION_VERSION_STRING = "CInstantSend-Version-1";

// Transaction Locks
//
Expand Down Expand Up @@ -797,6 +798,21 @@ bool CInstantSend::IsInstantSendReadyToLock(const uint256& txHash)
return it != mapTxLockCandidates.end() && it->second.IsAllOutPointsReady();
}

void CInstantSend::Clear()
{
LOCK(cs_instantsend);

mapLockRequestAccepted.clear();
mapLockRequestRejected.clear();
mapTxLockVotes.clear();
mapTxLockVotesOrphan.clear();
mapTxLockCandidates.clear();
mapVotedOutpoints.clear();
mapLockedOutpoints.clear();
mapMasternodeOrphanVotes.clear();
nCachedBlockHeight = 0;
}

bool CInstantSend::IsLockedInstantSendTransaction(const uint256& txHash)
{
if(!fEnableInstantSend || GetfLargeWorkForkFound() || GetfLargeWorkInvalidChainFound() ||
Expand Down Expand Up @@ -928,7 +944,7 @@ void CInstantSend::SyncTransaction(const CTransaction& tx, const CBlockIndex *pi
}
}

std::string CInstantSend::ToString()
std::string CInstantSend::ToString() const
{
LOCK(cs_instantsend);
return strprintf("Lock Candidates: %llu, Votes %llu", mapTxLockCandidates.size(), mapTxLockVotes.size());
Expand Down
62 changes: 60 additions & 2 deletions src/instantx.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ extern int nCompleteTXLocks;
class CInstantSend
{
private:
static const std::string SERIALIZATION_VERSION_STRING;

// Keep track of current block height
int nCachedBlockHeight;

Expand Down Expand Up @@ -91,7 +93,37 @@ class CInstantSend
bool IsInstantSendReadyToLock(const uint256 &txHash);

public:
CCriticalSection cs_instantsend;
mutable CCriticalSection cs_instantsend;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
std::string strVersion;
if(ser_action.ForRead()) {
READWRITE(strVersion);
}
else {
strVersion = SERIALIZATION_VERSION_STRING;
READWRITE(strVersion);
}

READWRITE(mapLockRequestAccepted);
READWRITE(mapLockRequestRejected);
READWRITE(mapTxLockVotes);
READWRITE(mapTxLockVotesOrphan);
READWRITE(mapTxLockCandidates);
READWRITE(mapVotedOutpoints);
READWRITE(mapLockedOutpoints);
READWRITE(mapMasternodeOrphanVotes);
READWRITE(nCachedBlockHeight);

if(ser_action.ForRead() && (strVersion != SERIALIZATION_VERSION_STRING)) {
Clear();
}
}

void Clear();

void ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman);

Expand Down Expand Up @@ -126,7 +158,7 @@ class CInstantSend
void UpdatedBlockTip(const CBlockIndex *pindex);
void SyncTransaction(const CTransaction& tx, const CBlockIndex *pindex, int posInBlock);

std::string ToString();
std::string ToString() const;
};

/**
Expand Down Expand Up @@ -264,13 +296,24 @@ class COutPointLock
static const int SIGNATURES_REQUIRED = 6;
static const int SIGNATURES_TOTAL = 10;

COutPointLock() {}

COutPointLock(const COutPoint& outpointIn) :
outpoint(outpointIn),
mapMasternodeVotes()
{}

COutPoint GetOutpoint() const { return outpoint; }

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(outpoint);
READWRITE(mapMasternodeVotes);
READWRITE(fAttacked);
}

bool AddVote(const CTxLockVote& vote);
std::vector<CTxLockVote> GetVotes() const;
bool HasMasternodeVoted(const COutPoint& outpointMasternodeIn) const;
Expand All @@ -291,6 +334,11 @@ class CTxLockCandidate
int64_t nTimeCreated;

public:
CTxLockCandidate() :
nConfirmedHeight(-1),
nTimeCreated(GetTime())
{}

CTxLockCandidate(const CTxLockRequest& txLockRequestIn) :
nConfirmedHeight(-1),
nTimeCreated(GetTime()),
Expand All @@ -301,6 +349,16 @@ class CTxLockCandidate
CTxLockRequest txLockRequest;
std::map<COutPoint, COutPointLock> mapOutPointLocks;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(txLockRequest);
READWRITE(mapOutPointLocks);
READWRITE(nTimeCreated);
READWRITE(nConfirmedHeight);
}

uint256 GetHash() const { return txLockRequest.GetHash(); }

void AddOutPointLock(const COutPoint& outpoint);
Expand Down

0 comments on commit 2c0d4c9

Please sign in to comment.