Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save/load InstantSend cache #2051

Merged
merged 5 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -36,6 +36,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 @@ -795,6 +796,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 @@ -926,7 +942,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 @@ -46,6 +46,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 @@ -84,7 +86,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 @@ -119,7 +151,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;
};

class CTxLockRequest
Expand Down Expand Up @@ -242,13 +274,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 @@ -266,6 +309,11 @@ class CTxLockCandidate
int64_t nTimeCreated;

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

CTxLockCandidate(const CTxLockRequest& txLockRequestIn) :
nConfirmedHeight(-1),
nTimeCreated(GetTime()),
Expand All @@ -276,6 +324,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