Skip to content

Commit 2c0d4c9

Browse files
gladcowUdjinM6
authored andcommitted
Save/load InstantSend cache (#2051)
* CTxLockCandidate serialization * COutPointLock serialization * CInstantSend serialization * Read/write instantsend.dat file * Read\write InstantSend cache only if InstantSend is enabled
1 parent a527845 commit 2c0d4c9

File tree

3 files changed

+92
-3
lines changed

3 files changed

+92
-3
lines changed

src/init.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,11 @@ void PrepareShutdown()
251251
flatdb3.Dump(governance);
252252
CFlatDB<CNetFulfilledRequestManager> flatdb4("netfulfilled.dat", "magicFulfilledCache");
253253
flatdb4.Dump(netfulfilledman);
254+
if(fEnableInstantSend)
255+
{
256+
CFlatDB<CInstantSend> flatdb5("instantsend.dat", "magicInstantSendCache");
257+
flatdb5.Dump(instantsend);
258+
}
254259
}
255260

256261
UnregisterNodeSignals(GetNodeSignals());
@@ -1914,6 +1919,16 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
19141919
if(!flatdb4.Load(netfulfilledman)) {
19151920
return InitError(_("Failed to load fulfilled requests cache from") + "\n" + (pathDB / strDBName).string());
19161921
}
1922+
1923+
if(fEnableInstantSend)
1924+
{
1925+
strDBName = "instantsend.dat";
1926+
uiInterface.InitMessage(_("Loading InstantSend data cache..."));
1927+
CFlatDB<CInstantSend> flatdb5(strDBName, "magicInstantSendCache");
1928+
if(!flatdb5.Load(instantsend)) {
1929+
return InitError(_("Failed to load InstantSend data cache from") + "\n" + (pathDB / strDBName).string());
1930+
}
1931+
}
19171932
}
19181933

19191934

src/instantx.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ int nInstantSendDepth = DEFAULT_INSTANTSEND_DEPTH;
3737
int nCompleteTXLocks;
3838

3939
CInstantSend instantsend;
40+
const std::string CInstantSend::SERIALIZATION_VERSION_STRING = "CInstantSend-Version-1";
4041

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

801+
void CInstantSend::Clear()
802+
{
803+
LOCK(cs_instantsend);
804+
805+
mapLockRequestAccepted.clear();
806+
mapLockRequestRejected.clear();
807+
mapTxLockVotes.clear();
808+
mapTxLockVotesOrphan.clear();
809+
mapTxLockCandidates.clear();
810+
mapVotedOutpoints.clear();
811+
mapLockedOutpoints.clear();
812+
mapMasternodeOrphanVotes.clear();
813+
nCachedBlockHeight = 0;
814+
}
815+
800816
bool CInstantSend::IsLockedInstantSendTransaction(const uint256& txHash)
801817
{
802818
if(!fEnableInstantSend || GetfLargeWorkForkFound() || GetfLargeWorkInvalidChainFound() ||
@@ -928,7 +944,7 @@ void CInstantSend::SyncTransaction(const CTransaction& tx, const CBlockIndex *pi
928944
}
929945
}
930946

931-
std::string CInstantSend::ToString()
947+
std::string CInstantSend::ToString() const
932948
{
933949
LOCK(cs_instantsend);
934950
return strprintf("Lock Candidates: %llu, Votes %llu", mapTxLockCandidates.size(), mapTxLockVotes.size());

src/instantx.h

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ extern int nCompleteTXLocks;
5353
class CInstantSend
5454
{
5555
private:
56+
static const std::string SERIALIZATION_VERSION_STRING;
57+
5658
// Keep track of current block height
5759
int nCachedBlockHeight;
5860

@@ -91,7 +93,37 @@ class CInstantSend
9193
bool IsInstantSendReadyToLock(const uint256 &txHash);
9294

9395
public:
94-
CCriticalSection cs_instantsend;
96+
mutable CCriticalSection cs_instantsend;
97+
98+
ADD_SERIALIZE_METHODS;
99+
100+
template <typename Stream, typename Operation>
101+
inline void SerializationOp(Stream& s, Operation ser_action) {
102+
std::string strVersion;
103+
if(ser_action.ForRead()) {
104+
READWRITE(strVersion);
105+
}
106+
else {
107+
strVersion = SERIALIZATION_VERSION_STRING;
108+
READWRITE(strVersion);
109+
}
110+
111+
READWRITE(mapLockRequestAccepted);
112+
READWRITE(mapLockRequestRejected);
113+
READWRITE(mapTxLockVotes);
114+
READWRITE(mapTxLockVotesOrphan);
115+
READWRITE(mapTxLockCandidates);
116+
READWRITE(mapVotedOutpoints);
117+
READWRITE(mapLockedOutpoints);
118+
READWRITE(mapMasternodeOrphanVotes);
119+
READWRITE(nCachedBlockHeight);
120+
121+
if(ser_action.ForRead() && (strVersion != SERIALIZATION_VERSION_STRING)) {
122+
Clear();
123+
}
124+
}
125+
126+
void Clear();
95127

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

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

129-
std::string ToString();
161+
std::string ToString() const;
130162
};
131163

132164
/**
@@ -264,13 +296,24 @@ class COutPointLock
264296
static const int SIGNATURES_REQUIRED = 6;
265297
static const int SIGNATURES_TOTAL = 10;
266298

299+
COutPointLock() {}
300+
267301
COutPointLock(const COutPoint& outpointIn) :
268302
outpoint(outpointIn),
269303
mapMasternodeVotes()
270304
{}
271305

272306
COutPoint GetOutpoint() const { return outpoint; }
273307

308+
ADD_SERIALIZE_METHODS;
309+
310+
template <typename Stream, typename Operation>
311+
inline void SerializationOp(Stream& s, Operation ser_action) {
312+
READWRITE(outpoint);
313+
READWRITE(mapMasternodeVotes);
314+
READWRITE(fAttacked);
315+
}
316+
274317
bool AddVote(const CTxLockVote& vote);
275318
std::vector<CTxLockVote> GetVotes() const;
276319
bool HasMasternodeVoted(const COutPoint& outpointMasternodeIn) const;
@@ -291,6 +334,11 @@ class CTxLockCandidate
291334
int64_t nTimeCreated;
292335

293336
public:
337+
CTxLockCandidate() :
338+
nConfirmedHeight(-1),
339+
nTimeCreated(GetTime())
340+
{}
341+
294342
CTxLockCandidate(const CTxLockRequest& txLockRequestIn) :
295343
nConfirmedHeight(-1),
296344
nTimeCreated(GetTime()),
@@ -301,6 +349,16 @@ class CTxLockCandidate
301349
CTxLockRequest txLockRequest;
302350
std::map<COutPoint, COutPointLock> mapOutPointLocks;
303351

352+
ADD_SERIALIZE_METHODS;
353+
354+
template <typename Stream, typename Operation>
355+
inline void SerializationOp(Stream& s, Operation ser_action) {
356+
READWRITE(txLockRequest);
357+
READWRITE(mapOutPointLocks);
358+
READWRITE(nTimeCreated);
359+
READWRITE(nConfirmedHeight);
360+
}
361+
304362
uint256 GetHash() const { return txLockRequest.GetHash(); }
305363

306364
void AddOutPointLock(const COutPoint& outpoint);

0 commit comments

Comments
 (0)