Skip to content

Commit

Permalink
Bug/merchantnode sync (#139)
Browse files Browse the repository at this point in the history
* Bumped version to 0.0.19. Updated how merchantnodes are synced. Handling case when connecting blocks which are out of sync with merchantnode sync

* Fixed last commit
  • Loading branch information
durkmurder authored Apr 24, 2019
1 parent 968c8ae commit ace7e7d
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dnl require autoconf 2.60 (AS_ECHO/AS_ECHO_N)
AC_PREREQ([2.60])
define(_CLIENT_VERSION_MAJOR, 1)
define(_CLIENT_VERSION_MINOR, 0)
define(_CLIENT_VERSION_REVISION, 18)
define(_CLIENT_VERSION_REVISION, 19)
define(_CLIENT_VERSION_BUILD, 0)
define(_CLIENT_VERSION_IS_RELEASE, true)
define(_COPYRIGHT_YEAR, 2018)
Expand Down
1 change: 1 addition & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include <tpos/merchantnodeman.h>
#include <netfulfilledman.h>
#include <governance/governance.h>
#include <tpos/merchantnode-sync.h>
#include <flat-database.h>

#ifndef WIN32
Expand Down
1 change: 1 addition & 0 deletions src/net_processing_xsn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ void net_processing_xsn::ThreadProcessExtensions(CConnman *pConnman)
if(nTick % 60 == 0) {
merchantnodeman.ProcessMerchantnodeConnections(connman);
merchantnodeman.CheckAndRemove(connman);
merchantnodeman.AskForMissing(connman);
}
if(fMerchantNode && (nTick % (60 * 5) == 0)) {
merchantnodeman.DoFullVerificationStep(connman);
Expand Down
39 changes: 36 additions & 3 deletions src/tpos/merchantnodeman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,21 @@ bool CMerchantnodeMan::Get(const CKeyID &pubKeyID, CMerchantnode& merchantnodeRe
{
// Theses mutexes are recursive so double locking by the same thread is safe.
LOCK(cs);
for (auto& mnpair : mapMerchantnodes) {
for (auto& mnpair : mapMerchantnodes)
{
CKeyID keyID = mnpair.second.pubKeyMerchantnode.GetID();
if (keyID == pubKeyID) {
merchantnodeRet = mnpair.second;
return true;
}
}

if(mUnknownMerchantnodes.size() < 100)
{
// if it's more than 100, which is very unlikely, someone might try to attack us.
mUnknownMerchantnodes[pubKeyID] += 1;
}

return false;
}

Expand Down Expand Up @@ -438,7 +446,7 @@ void CMerchantnodeMan::ProcessMerchantnodeConnections(CConnman& connman)
//we don't care about this for regtest
if(Params().NetworkIDString() == CBaseChainParams::REGTEST) return;

connman.ForEachNode([](CNode* pnode) {
connman.ForEachNode([&connman](CNode* pnode) {
if(pnode->fMerchantnode) {
LogPrintf("Closing Merchantnode connection: peer=%d, addr=%s\n", pnode->GetId(), pnode->addr.ToString());
pnode->fDisconnect = true;
Expand Down Expand Up @@ -487,6 +495,14 @@ void CMerchantnodeMan::ProcessMessage(CNode* pfrom, const std::string& strComman

if(!merchantnodeSync.IsBlockchainSynced()) return;

{
auto it = mUnknownMerchantnodes.find(mnb.pubKeyMerchantnode.GetID());
if(it != std::end(mUnknownMerchantnodes))
{
mUnknownMerchantnodes.erase(it);
}
}

LogPrint(BCLog::MERCHANTNODE, "MERCHANTNODEANNOUNCE -- Merchantnode announce, merchantnode=%s\n", mnb.pubKeyMerchantnode.GetID().ToString());

int nDos = 0;
Expand Down Expand Up @@ -710,6 +726,23 @@ void CMerchantnodeMan::DoFullVerificationStep(CConnman& connman)
#endif
}

void CMerchantnodeMan::AskForMissing(CConnman &connman)
{
bool shouldAsk = false;

for(auto &&it : mUnknownMerchantnodes)
{
shouldAsk |= (it.second >= 10);
}

if(shouldAsk)
{
connman.ForEachNode([this, &connman](CNode* pnode) {
DsegUpdate(pnode, connman);
});
}
}

// This function tries to find merchantnodes with the same addr,
// find a verified one and ban all the other. If there are many nodes
// with the same addr but none of them is verified yet, then none of them are banned.
Expand Down Expand Up @@ -1138,7 +1171,7 @@ bool CMerchantnodeMan::CheckMnbAndUpdateMerchantnodeList(CNode* pfrom, CMerchant

{
// Need to lock cs_main here to ensure consistent locking order because the SimpleCheck call below locks cs_main
// LOCK(cs_main);
// LOCK(cs_main);
if(!mnb.SimpleCheck(nDos)) {
LogPrint(BCLog::MERCHANTNODE, "CMerchantnodeMan::CheckMnbAndUpdateMerchantnodeList -- SimpleCheck() failed, merchantnode=%s\n",
mnb.pubKeyMerchantnode.GetID().ToString());
Expand Down
3 changes: 3 additions & 0 deletions src/tpos/merchantnodeman.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class CMerchantnodeMan
std::map<uint256, std::pair< int64_t, std::set<CNetAddr> > > mMnbRecoveryRequests;
std::map<uint256, std::vector<CMerchantnodeBroadcast> > mMnbRecoveryGoodReplies;
std::list< std::pair<CService, uint256> > listScheduledMnbRequestConnections;
// map to represent unkown merchantnode and how many times it was asked
std::map<CKeyID, unsigned> mUnknownMerchantnodes;

int64_t nLastWatchdogVoteTime;

Expand Down Expand Up @@ -155,6 +157,7 @@ class CMerchantnodeMan
void ProcessMessage(CNode* pfrom, const string &strCommand, CDataStream& vRecv, CConnman& connman);

void DoFullVerificationStep(CConnman& connman);
void AskForMissing(CConnman& connman);
void CheckSameAddr();
bool SendVerifyRequest(const CAddress& addr, const std::vector<CMerchantnode*>& vSortedByAddr, CConnman& connman);
void SendVerifyReply(CNode* pnode, CMerchantnodeVerification& mnv, CConnman& connman);
Expand Down
1 change: 0 additions & 1 deletion src/tpos/tposutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

static const std::string TPOSEXPORTHEADER("TPOSOWNERINFO");
static const int TPOSEXPORTHEADERWIDTH = 40;

static const int TPOS_CONTRACT_COLATERAL = 1 * COIN;

std::string ParseAddressFromMetadata(std::string str)
Expand Down
2 changes: 1 addition & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2713,7 +2713,7 @@ bool CChainState::ActivateBestChainStep(CValidationState& state, const CChainPar
else
CheckForkWarningConditions();

return true;
return !fInvalidFound;
}

static void NotifyHeaderTip() {
Expand Down

0 comments on commit ace7e7d

Please sign in to comment.