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

a bunch of changes #11

Closed
wants to merge 13 commits into from
21 changes: 20 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ std::string HelpMessage()
" -datadir=<dir> " + _("Specify data directory") + "\n" +
" -dbcache=<n> " + _("Set database cache size in megabytes (default: 25)") + "\n" +
" -timeout=<n> " + _("Specify connection timeout in milliseconds (default: 5000)") + "\n" +
" -proxy=<ip:port> " + _("Connect through socks proxy") + "\n" +
" -proxy=<ip:port> " + _("Exclusively connect through socks proxy") + "\n" +
" -proxytoo=<ip:port> " + _("Also connect through socks proxy") + "\n" +
" -socks=<n> " + _("Select the version of socks proxy to use (4-5, default: 5)") + "\n" +
" -tor=<ip:port> " + _("Use proxy to reach tor hidden services (default: same as -proxy)") + "\n"
" -dns " + _("Allow DNS lookups for -addnode, -seednode and -connect") + "\n" +
Expand Down Expand Up @@ -582,6 +583,7 @@ bool AppInit2(boost::thread_group& threadGroup)
fPrintToConsole = GetBoolArg("-printtoconsole");
fPrintToDebugger = GetBoolArg("-printtodebugger");
fLogTimestamps = GetBoolArg("-logtimestamps", true);
fMasternodes = GetBoolArg("-masternodes");
bool fDisableWallet = GetBoolArg("-disablewallet", false);

if (mapArgs.count("-timeout"))
Expand Down Expand Up @@ -780,6 +782,23 @@ bool AppInit2(boost::thread_group& threadGroup)
SetReachable(NET_TOR);
}

if (mapArgs.count("-proxytoo")) {
fProxyToo = true;
CService addrProxy = CService(mapArgs["-proxytoo"], 9050);
if (!addrProxy.IsValid())
return InitError(strprintf(_("Invalid -proxytoo address: '%s'"), mapArgs["-proxytoo"].c_str()));

if (!IsLimited(NET_IPV4))
SetProxy(NET_IPV4, addrProxy, nSocksVersion);
if (nSocksVersion > 4) {
#ifdef USE_IPV6
if (!IsLimited(NET_IPV6))
SetProxy(NET_IPV6, addrProxy, nSocksVersion);
#endif
SetNameProxy(addrProxy, nSocksVersion);
}
}

// see Step 2: parameter interactions for more information about these
fNoListen = !GetBoolArg("-listen", true);
fDiscover = GetBoolArg("-discover", true);
Expand Down
48 changes: 33 additions & 15 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ uint256 hashBestChain = 0;
CBlockIndex* pindexBest = NULL;
set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexValid; // may contain all CBlockIndex*'s that have validness >=BLOCK_VALID_TRANSACTIONS, and must contain those who aren't failed
int64 nTimeBestReceived = 0;
int nAskedForBlocks = 0;
int nScriptCheckThreads = 0;
bool fImporting = false;
bool fReindex = false;
Expand Down Expand Up @@ -2616,6 +2617,7 @@ bool CBlock::CheckBlock(CValidationState &state, bool fCheckPOW, bool fCheckMerk
} else {
if(nTime > START_MASTERNODE_PAYMENTS) MasternodePayments = true;
}
if (fMasternodes) MasternodePayments = true;

if(MasternodePayments)
{
Expand Down Expand Up @@ -2644,7 +2646,7 @@ bool CBlock::CheckBlock(CValidationState &state, bool fCheckPOW, bool fCheckMerk
printf("CheckBlock() : loading prev orphan block %s\n", hashPrevBlock.ToString().c_str());
blockLast = *mapOrphanBlocks[hashPrevBlock];
} else {
state.DoS(100, error("CheckBlock() : Couldn't load previous block"));
printf("CheckBlock() : Couldn't load previous block %s\n", hashPrevBlock.ToString().c_str());
}

if (pindexPrev != NULL && fCheckVotes && !fIsInitialDownload){
Expand Down Expand Up @@ -2955,7 +2957,8 @@ bool ProcessBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBl
mapOrphanBlocksByPrev.insert(make_pair(pblock2->hashPrevBlock, pblock2));

// Ask this guy to fill in what we're missing
pfrom->PushGetBlocks(pindexBest, GetOrphanRoot(pblock2));
if (pfrom->PushGetBlocks(pindexBest, GetOrphanRoot(pblock2)))
printf("send fill-in getblocks for %s peer=%d\n", hash.ToString().c_str(), pfrom->id);
}
return true;
}
Expand Down Expand Up @@ -2988,7 +2991,6 @@ bool ProcessBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBl
//might need to reset pool
darkSendPool.NewBlock();

printf("ProcessBlock: ACCEPTED\n");
return true;
}

Expand Down Expand Up @@ -3983,7 +3985,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)

pfrom->fSuccessfullyConnected = true;

printf("receive version message: %s: version %d, blocks=%d, us=%s, them=%s, peer=%s\n", pfrom->cleanSubVer.c_str(), pfrom->nVersion, pfrom->nStartingHeight, addrMe.ToString().c_str(), addrFrom.ToString().c_str(), pfrom->addr.ToString().c_str());
printf("receive version message: %s: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", pfrom->cleanSubVer.c_str(), pfrom->nVersion, pfrom->nStartingHeight, addrMe.ToString().c_str(), addrFrom.ToString().c_str(), pfrom->id);

cPeerBlockCounts.input(pfrom->nStartingHeight);
}
Expand All @@ -4002,6 +4004,12 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
pfrom->SetRecvVersion(min(pfrom->nVersion, PROTOCOL_VERSION));
}

else if (strCommand == "misbehave") {
int howmuch;
vRecv >> howmuch;
printf("peer=%d says we are misbehaving %d\n", howmuch);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops... sorry about the bug here - somehow the fixup commit that fixed this got missed from this branch.

}

else if (strCommand == "dseg") { //DarkSend Election Get
if (pfrom->nVersion != darkSendPool.MIN_PEER_PROTO_VERSION) {
return false;
Expand Down Expand Up @@ -4102,6 +4110,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
return false;
}

addrman.Add(CAddress(addr), pfrom->addr, 2*60*60);

CMasterNode mn(addr, vin, pubkey, vchSig, sigTime, pubkey2);
mn.UpdateLastSeen(lastUpdated);
Expand Down Expand Up @@ -4236,8 +4245,10 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
addrman.Add(vAddrOk, pfrom->addr, 2 * 60 * 60);
if (vAddr.size() < 1000)
pfrom->fGetAddr = false;
if (pfrom->fOneShot)
if (pfrom->fOneShot) {
printf("OneShot. Disconnecting\n");
pfrom->fDisconnect = true;
}
}


Expand Down Expand Up @@ -4274,12 +4285,14 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
if (!fImporting && !fReindex)
pfrom->AskFor(inv);
} else if (inv.type == MSG_BLOCK && mapOrphanBlocks.count(inv.hash)) {
pfrom->PushGetBlocks(pindexBest, GetOrphanRoot(mapOrphanBlocks[inv.hash]));
if (pfrom->PushGetBlocks(pindexBest, GetOrphanRoot(mapOrphanBlocks[inv.hash])))
printf("send getblocks for %s peer=%d\n", inv.hash.ToString().c_str(), pfrom->id);
} else if (nInv == nLastBlock) {
// In case we are on a very long side-chain, it is possible that we already have
// the last block in an inv bundle sent in response to getblocks. Try to detect
// this situation and push another getblocks to continue.
pfrom->PushGetBlocks(mapBlockIndex[inv.hash], uint256(0));
if (pfrom->PushGetBlocks(mapBlockIndex[inv.hash], uint256(0)))
printf("send last getblocks for %s peer=%d\n", inv.hash.ToString().c_str(), pfrom->id);
if (fDebug)
printf("force request: %s\n", inv.ToString().c_str());
}
Expand All @@ -4301,10 +4314,10 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
}

if (fDebugNet || (vInv.size() != 1))
printf("received getdata (%"PRIszu" invsz)\n", vInv.size());
printf("received getdata (%"PRIszu" invsz) peer=%d\n", vInv.size(), pfrom->id);

if ((fDebugNet && vInv.size() > 0) || (vInv.size() == 1))
printf("received getdata for: %s\n", vInv[0].ToString().c_str());
printf("received getdata for: %s peer=%d\n", vInv[0].ToString().c_str(), pfrom->id);

pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end());
ProcessGetData(pfrom);
Expand All @@ -4324,7 +4337,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
if (pindex)
pindex = pindex->pnext;
int nLimit = 500;
printf("getblocks %d to %s limit %d\n", (pindex ? pindex->nHeight : -1), hashStop.ToString().c_str(), nLimit);
printf("getblocks %d to %s limit %d peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop==uint256(0) ? "0" : hashStop.ToString().c_str(), nLimit, pfrom->id);
for (; pindex; pindex = pindex->pnext)
{
if (pindex->GetBlockHash() == hashStop)
Expand Down Expand Up @@ -4468,7 +4481,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
CBlock block;
vRecv >> block;

printf("received block %s\n", block.GetHash().ToString().c_str());
printf("received block %s peer=%d\n", block.GetHash().ToString().c_str(), pfrom->id);
// block.print();

CInv inv(MSG_BLOCK, block.GetHash());
Expand Down Expand Up @@ -4784,9 +4797,14 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
}

// Start block sync
if (pto->fStartSync && !fImporting && !fReindex) {
pto->fStartSync = false;
pto->PushGetBlocks(pindexBest, uint256(0));
if (!pto->fAskedForBlocks && !fImporting && !fReindex && !pto->fClient && !pto->fOneShot &&
!pto->fDisconnect && pto->fSuccessfullyConnected &&
(pto->nStartingHeight > (nBestHeight - 144)) &&
(pto->nVersion < NOBLKS_VERSION_START || pto->nVersion >= NOBLKS_VERSION_END)) {
nAskedForBlocks++;
pto->fAskedForBlocks = true;
if (pto->PushGetBlocks(pindexBest, uint256(0)))
printf("send initial getblocks peer=%d\n", pto->id);
}

// Resend wallet transactions that haven't gotten in a block yet
Expand Down Expand Up @@ -4917,7 +4935,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
if (!AlreadyHave(inv))
{
if (fDebugNet)
printf("sending getdata: %s\n", inv.ToString().c_str());
printf("sending getdata: %s peer=%d\n", inv.ToString().c_str(), pto->id);
vGetData.push_back(inv);
if (vGetData.size() >= 1000)
{
Expand Down
1 change: 1 addition & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ extern bool fImporting;
extern bool fReindex;
extern bool fBenchmark;
extern int nScriptCheckThreads;
extern int nAskedForBlocks; // Nodes sent a getblocks 0
extern bool fTxIndex;
extern unsigned int nCoinCacheSize;
extern CDarkSendPool darkSendPool;
Expand Down
15 changes: 10 additions & 5 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ CCriticalSection cs_setservAddNodeAddresses;
vector<std::string> vAddedNodes;
CCriticalSection cs_vAddedNodes;

NodeId nLastNodeId = 0;
CCriticalSection cs_nLastNodeId;

static CSemaphore *semOutbound = NULL;

void AddOneShot(string strDest)
Expand All @@ -82,15 +85,16 @@ unsigned short GetListenPort()
return (unsigned short)(GetArg("-port", GetDefaultPort()));
}

void CNode::PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd)
bool CNode::PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd)
{
// Filter out duplicate requests
if (pindexBegin == pindexLastGetBlocksBegin && hashEnd == hashLastGetBlocksEnd)
return;
return false;
pindexLastGetBlocksBegin = pindexBegin;
hashLastGetBlocksEnd = hashEnd;

PushMessage("getblocks", CBlockLocator(pindexBegin), hashEnd);
return true;
}

// find 'best' local address for a particular peer
Expand Down Expand Up @@ -471,9 +475,9 @@ CNode* ConnectNode(CAddress addrConnect, const char *pszDest)


/// debug print
printf("trying connection %s lastseen=%.1fhrs\n",
printf("trying connection %s lastseen=%.1fdays\n",
pszDest ? pszDest : addrConnect.ToString().c_str(),
pszDest ? 0 : (double)(GetAdjustedTime() - addrConnect.nTime)/3600.0);
pszDest ? 0 : (double)(GetAdjustedTime() - addrConnect.nTime)/86400.0);

// Connect
SOCKET hSocket;
Expand Down Expand Up @@ -544,7 +548,7 @@ void CNode::PushVersion()
CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService("0.0.0.0",0)));
CAddress addrMe = GetLocalAddress(&addr);
RAND_bytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
printf("send version message: version %d, blocks=%d, us=%s, them=%s, peer=%s\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString().c_str(), addrYou.ToString().c_str(), addr.ToString().c_str());
printf("send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString().c_str(), addrYou.ToString().c_str(), id);
PushMessage("version", PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe,
nLocalHostNonce, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector<string>()), nBestHeight);
}
Expand Down Expand Up @@ -586,6 +590,7 @@ bool CNode::Misbehaving(int howmuch)
}

nMisbehavior += howmuch;
PushMessage("misbehave", howmuch);
if (nMisbehavior >= GetArg("-banscore", 100))
{
int64 banTime = GetTime()+GetArg("-bantime", 60*60*24); // Default 24-hour ban
Expand Down
15 changes: 13 additions & 2 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ void StartNode(boost::thread_group& threadGroup);
bool StopNode();
void SocketSendData(CNode *pnode);

typedef int NodeId;

enum
{
LOCAL_NONE, // unknown
Expand Down Expand Up @@ -87,7 +89,8 @@ extern limitedmap<CInv, int64> mapAlreadyAskedFor;
extern std::vector<std::string> vAddedNodes;
extern CCriticalSection cs_vAddedNodes;


extern NodeId nLastNodeId;
extern CCriticalSection cs_nLastNodeId;


class CNodeStats
Expand Down Expand Up @@ -191,6 +194,7 @@ class CNode
bool fNetworkNode;
bool fSuccessfullyConnected;
bool fDisconnect;
bool fAskedForBlocks; // true when getblocks 0 sent
// We use fRelayTxes for two purposes -
// a) it allows us to not relay tx invs before receiving the peer's version message
// b) the peer may tell us in their version message that we should not relay tx invs
Expand All @@ -200,6 +204,7 @@ class CNode
CCriticalSection cs_filter;
CBloomFilter* pfilter;
int nRefCount;
NodeId id;
protected:

// Denial-of-service detection/prevention
Expand Down Expand Up @@ -249,6 +254,7 @@ class CNode
fNetworkNode = false;
fSuccessfullyConnected = false;
fDisconnect = false;
fAskedForBlocks = false;
nRefCount = 0;
nSendSize = 0;
nSendOffset = 0;
Expand All @@ -263,6 +269,11 @@ class CNode
setInventoryKnown.max_size(SendBufferSize() / 1000);
pfilter = new CBloomFilter();

{
LOCK(cs_nLastNodeId);
id = nLastNodeId++;
}

// Be shy and don't send version until we hear
if (hSocket != INVALID_SOCKET && !fInbound)
PushVersion();
Expand Down Expand Up @@ -608,7 +619,7 @@ class CNode
}
}

void PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd);
bool PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd);
bool IsSubscribed(unsigned int nChannel);
void Subscribe(unsigned int nChannel, unsigned int nHops=0);
void CancelSubscribe(unsigned int nChannel);
Expand Down
3 changes: 2 additions & 1 deletion src/netbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ using namespace std;
static proxyType proxyInfo[NET_MAX];
static proxyType nameproxyInfo;
static CCriticalSection cs_proxyInfos;
int fProxyToo = false;
int nConnectTimeout = 5000;
bool fNameLookup = false;

Expand Down Expand Up @@ -475,7 +476,7 @@ bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout)
proxyType proxy;

// no proxy needed
if (!GetProxy(addrDest.GetNetwork(), proxy))
if (!GetProxy(addrDest.GetNetwork(), proxy) || (fProxyToo && rand() %2 == 0))
return ConnectSocketDirectly(addrDest, hSocketRet, nTimeout);

SOCKET hSocket = INVALID_SOCKET;
Expand Down
2 changes: 2 additions & 0 deletions src/netbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,6 @@ bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0);
bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nConnectTimeout);
bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault = 0, int nTimeout = nConnectTimeout);

// Settings
extern int fProxyToo;
#endif
1 change: 1 addition & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ bool fTestNet = false;
bool fBloomFilters = true;
bool fNoListen = false;
bool fLogTimestamps = false;
bool fMasternodes = false;
CMedianFilter<int64> vTimeOffsets(200,0);
volatile bool fReopenDebugLog = false;
bool fCachedPath[2] = {false, false};
Expand Down
1 change: 1 addition & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ extern bool fTestNet;
extern bool fBloomFilters;
extern bool fNoListen;
extern bool fLogTimestamps;
extern bool fMasternodes;
extern volatile bool fReopenDebugLog;

void RandAddSeed();
Expand Down