Skip to content

Commit 1990d92

Browse files
Merge pull request #390 from aguycalled/fix-slow-sync
fixes #388: cache cfund votes and add bench log
2 parents 0f777df + f1154b4 commit 1990d92

File tree

2 files changed

+45
-23
lines changed

2 files changed

+45
-23
lines changed

src/main.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3858,6 +3858,9 @@ bool static ConnectTip(CValidationState& state, const CChainParams& chainparams,
38583858
return true;
38593859
}
38603860

3861+
std::map<uint256, std::pair<int, int>> vCacheProposalsToUpdate;
3862+
std::map<uint256, std::pair<int, int>> vCachePaymentRequestToUpdate;
3863+
38613864
void CountVotes(CValidationState& state, CBlockIndex *pindexNew, bool fUndo)
38623865
{
38633866
int64_t nTimeStart = GetTimeMicros();
@@ -3866,13 +3869,16 @@ void CountVotes(CValidationState& state, CBlockIndex *pindexNew, bool fUndo)
38663869
int nBlocks = (pindexNew->nHeight % Params().GetConsensus().nBlocksPerVotingCycle) + 1;
38673870
CBlockIndex* pindexblock = pindexNew;
38683871

3869-
std::map<uint256, std::pair<int, int>> vCacheProposalsToUpdate;
3870-
std::map<uint256, std::pair<int, int>> vCachePaymentRequestToUpdate;
38713872
std::map<uint256, bool> vSeen;
38723873

3873-
vCacheProposalsToUpdate.clear();
3874-
vCachePaymentRequestToUpdate.clear();
3874+
if (fUndo || nBlocks == 1 || vCacheProposalsToUpdate.empty() || vCachePaymentRequestToUpdate.empty()) {
3875+
vCacheProposalsToUpdate.clear();
3876+
vCachePaymentRequestToUpdate.clear();
3877+
} else {
3878+
nBlocks = 1;
3879+
}
38753880

3881+
int64_t nTimeStart2 = GetTimeMicros();
38763882
while(nBlocks > 0 && pindexblock != NULL) {
38773883
vSeen.clear();
38783884
for(unsigned int i = 0; i < pindexblock->vProposalVotes.size(); i++) {
@@ -3911,9 +3917,13 @@ void CountVotes(CValidationState& state, CBlockIndex *pindexNew, bool fUndo)
39113917
pindexblock = pindexblock->pprev;
39123918
nBlocks--;
39133919
}
3920+
int64_t nTimeEnd2 = GetTimeMicros();
3921+
LogPrint("bench-cfund", " - CFund count votes from headers: %.2fms\n", (nTimeEnd2 - nTimeStart2) * 0.001);
3922+
39143923

39153924
vSeen.clear();
39163925

3926+
int64_t nTimeStart3 = GetTimeMicros();
39173927
std::map<uint256, std::pair<int, int>>::iterator it;
39183928
std::vector<std::pair<uint256, CFund::CProposal>> vecProposalsToUpdate;
39193929
std::vector<std::pair<uint256, CFund::CPaymentRequest>> vecPaymentRequestsToUpdate;
@@ -3941,9 +3951,12 @@ void CountVotes(CValidationState& state, CBlockIndex *pindexNew, bool fUndo)
39413951
if (!pblocktree->UpdateProposalIndex(vecProposalsToUpdate)) {
39423952
AbortNode(state, "Failed to write proposal index");
39433953
}
3954+
int64_t nTimeEnd3 = GetTimeMicros();
3955+
LogPrint("bench-cfund", " - CFund update votes: %.2fms\n", (nTimeEnd3 - nTimeStart3) * 0.001);
39443956

39453957
std::vector<CFund::CPaymentRequest> vecPaymentRequest;
39463958

3959+
int64_t nTimeStart4 = GetTimeMicros();
39473960
if(pblocktree->GetPaymentRequestIndex(vecPaymentRequest)){
39483961
for(unsigned int i = 0; i < vecPaymentRequest.size(); i++) {
39493962
vecPaymentRequestsToUpdate.clear();
@@ -4026,9 +4039,12 @@ void CountVotes(CValidationState& state, CBlockIndex *pindexNew, bool fUndo)
40264039
} else {
40274040
AbortNode(state, "Failed to read payment request index, please restart with -reindex-chainstate");
40284041
}
4042+
int64_t nTimeEnd4 = GetTimeMicros();
4043+
LogPrint("bench-cfund", " - CFund update payment request status: %.2fms\n", (nTimeEnd4 - nTimeStart4) * 0.001);
40294044

40304045
std::vector<CFund::CProposal> vecProposal;
40314046

4047+
int64_t nTimeStart5 = GetTimeMicros();
40324048
if(pblocktree->GetProposalIndex(vecProposal)){
40334049
for(unsigned int i = 0; i < vecProposal.size(); i++) {
40344050
bool fUpdate = false;
@@ -4116,6 +4132,9 @@ void CountVotes(CValidationState& state, CBlockIndex *pindexNew, bool fUndo)
41164132
} else {
41174133
AbortNode(state, "Failed to read proposal index, please restart with -reindex-chainstate");
41184134
}
4135+
int64_t nTimeEnd5 = GetTimeMicros();
4136+
4137+
LogPrint("bench-cfund", " - CFund update proposal status: %.2fms\n", (nTimeEnd5 - nTimeStart5) * 0.001);
41194138

41204139
if (!pblocktree->UpdatePaymentRequestIndex(vecPaymentRequestsToUpdate)) {
41214140
AbortNode(state, "Failed to write payment request index");
@@ -4126,7 +4145,7 @@ void CountVotes(CValidationState& state, CBlockIndex *pindexNew, bool fUndo)
41264145
}
41274146

41284147
int64_t nTimeEnd = GetTimeMicros();
4129-
LogPrint("bench", "- CFund count votes: %.2fms\n", (nTimeEnd - nTimeStart) * 0.001);
4148+
LogPrint("bench", "- CFund total CountVotes() function: %.2fms\n", (nTimeEnd - nTimeStart) * 0.001);
41304149
}
41314150

41324151
/**

src/rpc/blockchain.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -965,47 +965,50 @@ UniValue cfundstats(const UniValue& params, bool fHelp)
965965
+ HelpExampleRpc("cfundstats", "")
966966
);
967967

968-
int nBlocks = (pindexBestHeader->nHeight % Params().GetConsensus().nBlocksPerVotingCycle);
969-
CBlockIndex* pindexblock = pindexBestHeader;
968+
CFund::CProposal proposal; CFund::CPaymentRequest prequest;
970969

971-
std::map<uint256, std::pair<int, int>> vCacheProposals;
972-
std::map<uint256, std::pair<int, int>> vCachePaymentRequest;
970+
int nBlocks = (chainActive.Tip()->nHeight % Params().GetConsensus().nBlocksPerVotingCycle) + 1;
971+
CBlockIndex* pindexblock = chainActive.Tip();
972+
973+
std::map<uint256, bool> vSeen;
974+
std::map<uint256, std::pair<int, int>> vCacheProposalsRPC;
975+
std::map<uint256, std::pair<int, int>> vCachePaymentRequestRPC;
976+
977+
vCacheProposalsRPC.clear();
978+
vCachePaymentRequestRPC.clear();
973979

974980
while(nBlocks > 0 && pindexblock != NULL) {
975-
std::map<uint256, bool> vSeen;
981+
vSeen.clear();
976982
for(unsigned int i = 0; i < pindexblock->vProposalVotes.size(); i++) {
977-
CFund::CProposal proposal;
978983
if(!CFund::FindProposal(pindexblock->vProposalVotes[i].first, proposal))
979984
continue;
980985
if(vSeen.count(pindexblock->vProposalVotes[i].first) == 0) {
981-
if(vCacheProposals.count(pindexblock->vProposalVotes[i].first) == 0)
982-
vCacheProposals[pindexblock->vProposalVotes[i].first] = make_pair(0, 0);
986+
if(vCacheProposalsRPC.count(pindexblock->vProposalVotes[i].first) == 0)
987+
vCacheProposalsRPC[pindexblock->vProposalVotes[i].first] = make_pair(0, 0);
983988
if(pindexblock->vProposalVotes[i].second)
984-
vCacheProposals[pindexblock->vProposalVotes[i].first].first += 1;
989+
vCacheProposalsRPC[pindexblock->vProposalVotes[i].first].first += 1;
985990
else
986-
vCacheProposals[pindexblock->vProposalVotes[i].first].second += 1;
991+
vCacheProposalsRPC[pindexblock->vProposalVotes[i].first].second += 1;
987992
vSeen[pindexblock->vProposalVotes[i].first]=true;
988993
}
989994
}
990995
for(unsigned int i = 0; i < pindexblock->vPaymentRequestVotes.size(); i++) {
991-
CFund::CPaymentRequest prequest; CFund::CProposal proposal;
992996
if(!CFund::FindPaymentRequest(pindexblock->vPaymentRequestVotes[i].first, prequest))
993997
continue;
994998
if(!CFund::FindProposal(prequest.proposalhash, proposal))
995999
continue;
9961000
if (mapBlockIndex.count(proposal.blockhash) == 0)
9971001
continue;
998-
9991002
CBlockIndex* pindexblockparent = mapBlockIndex[proposal.blockhash];
10001003
if(pindexblockparent == NULL)
10011004
continue;
10021005
if(vSeen.count(pindexblock->vPaymentRequestVotes[i].first) == 0) {
1003-
if(vCachePaymentRequest.count(pindexblock->vPaymentRequestVotes[i].first) == 0)
1004-
vCachePaymentRequest[pindexblock->vPaymentRequestVotes[i].first] = make_pair(0, 0);
1006+
if(vCachePaymentRequestRPC.count(pindexblock->vPaymentRequestVotes[i].first) == 0)
1007+
vCachePaymentRequestRPC[pindexblock->vPaymentRequestVotes[i].first] = make_pair(0, 0);
10051008
if(pindexblock->vPaymentRequestVotes[i].second)
1006-
vCachePaymentRequest[pindexblock->vPaymentRequestVotes[i].first].first += 1;
1009+
vCachePaymentRequestRPC[pindexblock->vPaymentRequestVotes[i].first].first += 1;
10071010
else
1008-
vCachePaymentRequest[pindexblock->vPaymentRequestVotes[i].first].second += 1;
1011+
vCachePaymentRequestRPC[pindexblock->vPaymentRequestVotes[i].first].second += 1;
10091012
vSeen[pindexblock->vPaymentRequestVotes[i].first]=true;
10101013
}
10111014
}
@@ -1043,7 +1046,7 @@ UniValue cfundstats(const UniValue& params, bool fHelp)
10431046
UniValue votesPaymentRequests(UniValue::VARR);
10441047

10451048
std::map<uint256, std::pair<int, int>>::iterator it;
1046-
for(it = vCacheProposals.begin(); it != vCacheProposals.end(); it++) {
1049+
for(it = vCacheProposalsRPC.begin(); it != vCacheProposalsRPC.end(); it++) {
10471050
CFund::CProposal proposal;
10481051
if(!CFund::FindProposal(it->first, proposal))
10491052
continue;
@@ -1055,7 +1058,7 @@ UniValue cfundstats(const UniValue& params, bool fHelp)
10551058
op.push_back(Pair("no", it->second.second));
10561059
votesProposals.push_back(op);
10571060
}
1058-
for(it = vCachePaymentRequest.begin(); it != vCachePaymentRequest.end(); it++) {
1061+
for(it = vCachePaymentRequestRPC.begin(); it != vCachePaymentRequestRPC.end(); it++) {
10591062
CFund::CPaymentRequest prequest; CFund::CProposal proposal;
10601063
if(!CFund::FindPaymentRequest(it->first, prequest))
10611064
continue;

0 commit comments

Comments
 (0)