Skip to content

Commit

Permalink
Implemented inventory system for IX messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Duffield committed Feb 6, 2015
1 parent dfb3da5 commit 012f0e5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 23 deletions.
46 changes: 25 additions & 21 deletions src/instantx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using namespace boost;

std::map<uint256, CTransaction> mapTxLockReq;
std::map<uint256, CTransaction> mapTxLockReqRejected;
std::map<uint256, int> mapTxLockVote;
std::map<uint256, CConsensusVote> mapTxLockVote;
std::map<uint256, CTransactionLock> mapTxLocks;
std::map<COutPoint, uint256> mapLockedInputs;
std::map<uint256, int64_t> mapUnknownVotes; //track votes with no tx for DOS
Expand Down Expand Up @@ -70,7 +70,12 @@ void ProcessMessageInstantX(CNode* pfrom, std::string& strCommand, CDataStream&

if (AcceptToMemoryPool(mempool, state, tx, true, &fMissingInputs))
{
RelayTransactionLockReq(tx, tx.GetHash());
vector<CInv> vInv;
vInv.push_back(inv);
LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes)
pnode->PushMessage("inv", vInv);

DoConsensusVote(tx, nBlockHeight);

mapTxLockReq.insert(make_pair(tx.GetHash(), tx));
Expand All @@ -87,8 +92,6 @@ void ProcessMessageInstantX(CNode* pfrom, std::string& strCommand, CDataStream&

// can we get the conflicting transaction as proof?

RelayTransactionLockReq(tx, inv.hash);

LogPrintf("ProcessMessageInstantX::txlreq - Transaction Lock Request: %s %s : rejected %s\n",
pfrom->addr.ToString().c_str(), pfrom->cleanSubVer.c_str(),
tx.GetHash().ToString().c_str()
Expand Down Expand Up @@ -130,7 +133,7 @@ void ProcessMessageInstantX(CNode* pfrom, std::string& strCommand, CDataStream&
return;
}

mapTxLockVote.insert(make_pair(ctx.GetHash(), 1));
mapTxLockVote.insert(make_pair(ctx.GetHash(), ctx));

if(ProcessConsensusVote(ctx)){
//Spam/Dos protection
Expand All @@ -155,15 +158,12 @@ void ProcessMessageInstantX(CNode* pfrom, std::string& strCommand, CDataStream&
mapUnknownVotes[ctx.vinMasternode.prevout.hash] = GetTime()+(60*10);
}
}

vector<CInv> vInv;
vInv.push_back(inv);
LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes)
{/*
if(!pnode->fRelayTxes)
continue;*/
pnode->PushMessage("inv", vInv);

pnode->PushMessage("txlvote", ctx);
}
}

return;
Expand Down Expand Up @@ -253,20 +253,20 @@ void DoConsensusVote(CTransaction& tx, int64_t nBlockHeight)

if(n == -1)
{
LogPrintf("InstantX::DoConsensusVote - Unknown Masternode\n");
if(fDebug) LogPrintf("InstantX::DoConsensusVote - Unknown Masternode\n");
return;
}

if(n > INSTANTX_SIGNATURES_TOTAL)
{
LogPrintf("InstantX::DoConsensusVote - Masternode not in the top %d (%d)\n", INSTANTX_SIGNATURES_TOTAL, n);
if(fDebug) LogPrintf("InstantX::DoConsensusVote - Masternode not in the top %d (%d)\n", INSTANTX_SIGNATURES_TOTAL, n);
return;
}
/*
nBlockHeight calculated from the transaction is the authoritive source
*/

LogPrintf("InstantX::DoConsensusVote - In the top %d (%d)\n", INSTANTX_SIGNATURES_TOTAL, n);
if(fDebug) LogPrintf("InstantX::DoConsensusVote - In the top %d (%d)\n", INSTANTX_SIGNATURES_TOTAL, n);

CConsensusVote ctx;
ctx.vinMasternode = activeMasternode.vin;
Expand All @@ -281,13 +281,17 @@ void DoConsensusVote(CTransaction& tx, int64_t nBlockHeight)
return;
}

LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes)
{
//LogPrintf("%s::check -- %s %s\n", vecMasternodes[winner].addr.ToString().c_str(), pnode->addr.ToString().c_str());
mapTxLockVote[ctx.GetHash()] = ctx;

CInv inv(MSG_TXLOCK_VOTE, ctx.GetHash());

pnode->PushMessage("txlvote", ctx);
vector<CInv> vInv;
vInv.push_back(inv);
LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes){
pnode->PushMessage("inv", vInv);
}

}

//received a consensus vote
Expand All @@ -309,7 +313,7 @@ bool ProcessConsensusVote(CConsensusVote& ctx)

if(n > INSTANTX_SIGNATURES_TOTAL)
{
LogPrintf("InstantX::ProcessConsensusVote - Masternode not in the top %d (%d) - %s\n", INSTANTX_SIGNATURES_TOTAL, n, ctx.GetHash().ToString().c_str());
if(fDebug) LogPrintf("InstantX::ProcessConsensusVote - Masternode not in the top %d (%d) - %s\n", INSTANTX_SIGNATURES_TOTAL, n, ctx.GetHash().ToString().c_str());
return false;
}

Expand Down Expand Up @@ -555,7 +559,7 @@ bool CTransactionLock::SignaturesValid()
return true;
}

void CTransactionLock::AddSignature(CConsensusVote cv)
void CTransactionLock::AddSignature(CConsensusVote& cv)
{
vecConsensusVotes.push_back(cv);
}
Expand Down
4 changes: 3 additions & 1 deletion src/instantx.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class CTransactionLock;
static const int MIN_INSTANTX_PROTO_VERSION = 70062;

extern map<uint256, CTransaction> mapTxLockReq;
extern map<uint256, CTransaction> mapTxLockReqRejected;
extern map<uint256, CConsensusVote> mapTxLockVote;
extern map<uint256, CTransactionLock> mapTxLocks;
extern std::map<COutPoint, uint256> mapLockedInputs;
extern int nCompleteTXLocks;
Expand Down Expand Up @@ -84,7 +86,7 @@ class CTransactionLock

bool SignaturesValid();
int CountSignatures();
void AddSignature(CConsensusVote cv);
void AddSignature(CConsensusVote& cv);

uint256 GetHash()
{
Expand Down
25 changes: 25 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3857,6 +3857,11 @@ bool static AlreadyHave(const CInv& inv)
case MSG_BLOCK:
return mapBlockIndex.count(inv.hash) ||
mapOrphanBlocks.count(inv.hash);
case MSG_TXLOCK_REQUEST:
return mapTxLockReq.count(inv.hash) ||
mapTxLockReqRejected.count(inv.hash);
case MSG_TXLOCK_VOTE:
return mapTxLockVote.count(inv.hash);
}
// Don't know what it is, just say we already got one
return true;
Expand Down Expand Up @@ -3956,6 +3961,7 @@ void static ProcessGetData(CNode* pfrom)
pushed = true;
}
}

if (!pushed && inv.type == MSG_TX) {

if(mapDarksendBroadcastTxes.count(inv.hash)){
Expand All @@ -3980,6 +3986,25 @@ void static ProcessGetData(CNode* pfrom)
}
}
}
if (!pushed && inv.type == MSG_TXLOCK_VOTE) {
if(mapTxLockVote.count(inv.hash)){
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss.reserve(1000);
ss << mapTxLockVote[inv.hash];
pfrom->PushMessage("txlvote", ss);
pushed = true;
}
}
if (!pushed && inv.type == MSG_TXLOCK_REQUEST) {
if(mapTxLockReq.count(inv.hash)){
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss.reserve(1000);
ss << mapTxLockReq[inv.hash];
pfrom->PushMessage("txlreq", ss);
pushed = true;
}
}

if (!pushed) {
vNotFound.push_back(inv);
}
Expand Down
4 changes: 3 additions & 1 deletion src/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ static const char* ppszTypeName[] =
"ERROR",
"tx",
"block",
"filtered block"
"filtered block",
"tx lock request",
"tx lock vote"
};

CMessageHeader::CMessageHeader()
Expand Down

0 comments on commit 012f0e5

Please sign in to comment.