Skip to content

Commit 4cbf18c

Browse files
authored
Merge pull request #1824 from cyrossignol/proto-version
net: Update protocol version and clean up net messaging
2 parents 9b1ac5b + 222d7c7 commit 4cbf18c

File tree

4 files changed

+113
-160
lines changed

4 files changed

+113
-160
lines changed

src/main.cpp

Lines changed: 75 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@
3434
#include <ctime>
3535
#include <math.h>
3636

37-
extern std::string NodeAddress(CNode* pfrom);
3837
extern bool WalletOutOfSync();
3938
extern bool AskForOutstandingBlocks(uint256 hashStart);
4039
extern void ResetTimerMain(std::string timer_name);
4140
extern bool GridcoinServices();
42-
std::string GetCommandNonce(std::string command);
4341

4442
unsigned int nNodeLifespan;
4543

@@ -3740,7 +3738,7 @@ bool AskForOutstandingBlocks(uint256 hashStart)
37403738
LOCK(cs_vNodes);
37413739
for (auto const& pNode : vNodes)
37423740
{
3743-
if (!pNode->fClient && !pNode->fOneShot && (pNode->nStartingHeight > (nBestHeight - 144)) && (pNode->nVersion < NOBLKS_VERSION_START || pNode->nVersion >= NOBLKS_VERSION_END) )
3741+
if (!pNode->fClient && !pNode->fOneShot && (pNode->nStartingHeight > (nBestHeight - 144)))
37443742
{
37453743
if (hashStart==uint256())
37463744
{
@@ -4376,19 +4374,6 @@ bool static AlreadyHave(CTxDB& txdb, const CInv& inv)
43764374
// a large 4-byte int at any alignment.
43774375
unsigned char pchMessageStart[4] = { 0x70, 0x35, 0x22, 0x05 };
43784376

4379-
4380-
std::string NodeAddress(CNode* pfrom)
4381-
{
4382-
std::string ip = pfrom->addr.ToString();
4383-
return ip;
4384-
}
4385-
4386-
bool SecurityTest(CNode* pfrom, bool acid_test)
4387-
{
4388-
if (pfrom->nStartingHeight > (nBestHeight*.5) && acid_test) return true;
4389-
return false;
4390-
}
4391-
43924377
bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, int64_t nTimeReceived)
43934378
{
43944379
RandAddSeedPerfmon();
@@ -4427,40 +4412,35 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
44274412
CAddress addrMe;
44284413
CAddress addrFrom;
44294414
uint64_t nNonce = 1;
4430-
std::string legacy_dummy;
4431-
4432-
vRecv >> pfrom->nVersion
4433-
>> legacy_dummy // pfrom->boinchashnonce
4434-
>> legacy_dummy // pfrom->boinchashpw
4435-
>> legacy_dummy // pfrom->cpid
4436-
>> legacy_dummy // pfrom->enccpid
4437-
>> legacy_dummy // acid
4438-
>> pfrom->nServices
4439-
>> nTime
4440-
>> addrMe;
4415+
4416+
vRecv >> pfrom->nVersion;
4417+
4418+
// In the version following 180324 (mandatory v5.0.0 - Fern), we can finally
4419+
// drop the garbage legacy fields added to the version message:
4420+
//
4421+
if (pfrom->nVersion <= 180324) {
4422+
std::string legacy_dummy;
4423+
vRecv >> legacy_dummy // pfrom->boinchashnonce
4424+
>> legacy_dummy // pfrom->boinchashpw
4425+
>> legacy_dummy // pfrom->cpid
4426+
>> legacy_dummy // pfrom->enccpid
4427+
>> legacy_dummy; // acid
4428+
}
4429+
4430+
vRecv >> pfrom->nServices >> nTime >> addrMe;
44414431

44424432
LogPrint(BCLog::LogFlags::NOISY, "received aries version %i ...", pfrom->nVersion);
44434433

44444434
int64_t timedrift = std::abs(GetAdjustedTime() - nTime);
44454435

4446-
if (timedrift > (8*60))
4447-
{
4436+
if (timedrift > (8*60))
4437+
{
44484438
LogPrint(BCLog::LogFlags::NOISY, "Disconnecting unauthorized peer with Network Time so far off by %" PRId64 " seconds!", timedrift);
44494439
pfrom->Misbehaving(100);
44504440
pfrom->fDisconnect = true;
44514441
return false;
44524442
}
44534443

4454-
4455-
// Ensure testnet users are running latest version as of 12-3-2015 (works in conjunction with block spamming)
4456-
if (pfrom->nVersion < 180321 && fTestNet)
4457-
{
4458-
// disconnect from peers older than this proto version
4459-
LogPrint(BCLog::LogFlags::NOISY, "Testnet partner %s using obsolete version %i; disconnecting", pfrom->addr.ToString(), pfrom->nVersion);
4460-
pfrom->fDisconnect = true;
4461-
return false;
4462-
}
4463-
44644444
if (pfrom->nVersion < MIN_PEER_PROTO_VERSION)
44654445
{
44664446
// disconnect from peers older than this proto version
@@ -4469,37 +4449,16 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
44694449
return false;
44704450
}
44714451

4472-
if (pfrom->nVersion < 180323 && !fTestNet && pindexBest->nHeight > 860500)
4473-
{
4474-
// disconnect from peers older than this proto version - Enforce Beacon Age - 3-26-2017
4475-
LogPrint(BCLog::LogFlags::NOISY, "partner %s using obsolete version %i (before enforcing beacon age); disconnecting", pfrom->addr.ToString(), pfrom->nVersion);
4476-
pfrom->fDisconnect = true;
4477-
return false;
4478-
}
4479-
4480-
if (!fTestNet && pfrom->nVersion < 180314)
4481-
{
4482-
// disconnect from peers older than this proto version
4483-
LogPrint(BCLog::LogFlags::NOISY, "ResearchAge: partner %s using obsolete version %i; disconnecting", pfrom->addr.ToString(), pfrom->nVersion);
4484-
pfrom->fDisconnect = true;
4485-
return false;
4486-
}
4487-
4488-
if (pfrom->nVersion == 10300)
4489-
pfrom->nVersion = 300;
44904452
if (!vRecv.empty())
44914453
vRecv >> addrFrom >> nNonce;
44924454
if (!vRecv.empty())
44934455
vRecv >> pfrom->strSubVer;
4494-
44954456
if (!vRecv.empty())
44964457
vRecv >> pfrom->nStartingHeight;
4458+
44974459
// 12-5-2015 - Append Trust fields
44984460
pfrom->nTrust = 0;
44994461

4500-
if (!vRecv.empty()) vRecv >> legacy_dummy; // pfrom->sGRCAddress;
4501-
4502-
45034462
// Allow newbies to connect easily with 0 blocks
45044463
if (GetArgument("autoban","true") == "true")
45054464
{
@@ -4563,23 +4522,16 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
45634522
}
45644523

45654524
// Get recent addresses
4566-
if (pfrom->fOneShot || pfrom->nVersion >= CADDR_TIME_VERSION || addrman.size() < 1000)
4567-
{
4568-
pfrom->PushMessage("getaddr");
4569-
pfrom->fGetAddr = true;
4570-
}
4525+
pfrom->PushMessage("getaddr");
4526+
pfrom->fGetAddr = true;
45714527
addrman.Good(pfrom->addr);
45724528
}
45734529
else
45744530
{
45754531
if (((CNetAddr)pfrom->addr) == (CNetAddr)addrFrom)
45764532
{
4577-
if (SecurityTest(pfrom,true))
4578-
{
4579-
//Dont store the peer unless it passes the test
4580-
addrman.Add(addrFrom, addrFrom);
4581-
addrman.Good(addrFrom);
4582-
}
4533+
addrman.Add(addrFrom, addrFrom);
4534+
addrman.Good(addrFrom);
45834535
}
45844536
}
45854537

@@ -4588,8 +4540,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
45884540
static int nAskedForBlocks = 0;
45894541
if (!pfrom->fClient && !pfrom->fOneShot &&
45904542
(pfrom->nStartingHeight > (nBestHeight - 144)) &&
4591-
(pfrom->nVersion < NOBLKS_VERSION_START ||
4592-
pfrom->nVersion >= NOBLKS_VERSION_END) &&
45934543
(nAskedForBlocks < 1 || (vNodes.size() <= 1 && nAskedForBlocks < 1)))
45944544
{
45954545
nAskedForBlocks++;
@@ -4619,7 +4569,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
46194569
else if (pfrom->nVersion == 0)
46204570
{
46214571
// Must have a version message before anything else 1-10-2015 Halford
4622-
LogPrintf("Hack attempt from %s - %s (banned) ",pfrom->addrName, NodeAddress(pfrom));
4572+
LogPrintf("Hack attempt from %s - %s (banned) ", pfrom->addrName, pfrom->addr.ToString());
46234573
pfrom->Misbehaving(100);
46244574
pfrom->fDisconnect=true;
46254575
return false;
@@ -4634,9 +4584,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
46344584
vector<CAddress> vAddr;
46354585
vRecv >> vAddr;
46364586

4637-
// Don't want addr from older versions unless seeding
4638-
if (pfrom->nVersion < CADDR_TIME_VERSION && addrman.size() > 1000)
4639-
return true;
46404587
if (vAddr.size() > 1000)
46414588
{
46424589
pfrom->Misbehaving(10);
@@ -4678,8 +4625,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
46784625
multimap<uint256, CNode*> mapMix;
46794626
for (auto const& pnode : vNodes)
46804627
{
4681-
if (pnode->nVersion < CADDR_TIME_VERSION)
4682-
continue;
46834628
unsigned int nPointer;
46844629
memcpy(&nPointer, &pnode, sizeof(nPointer));
46854630
uint256 hashKey = ArithToUint256(UintToArith256(hashRand) ^ nPointer);
@@ -4794,9 +4739,16 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
47944739
{
47954740
CBlock block;
47964741
block.ReadFromDisk((*mi).second);
4797-
//HALFORD 12-26-2014
4798-
std::string acid = GetCommandNonce("encrypt");
4799-
pfrom->PushMessage("encrypt", block, acid);
4742+
4743+
// TODO: drop legacy "command nonce" removal transition in the next
4744+
// release after the mandatory version:
4745+
//
4746+
if (pfrom->nVersion >= PROTOCOL_VERSION) {
4747+
pfrom->PushMessage("encrypt", block);
4748+
} else {
4749+
std::string acid;
4750+
pfrom->PushMessage("encrypt", block, acid);
4751+
}
48004752

48014753
// Trigger them to send a getblocks request for the next batch of inventory
48024754
if (inv.hash == pfrom->hashContinue)
@@ -5020,8 +4972,16 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
50204972
//Response from getblocks, message = block
50214973

50224974
CBlock block;
5023-
std::string acid = "";
5024-
vRecv >> block >> acid;
4975+
vRecv >> block;
4976+
4977+
// TODO: drop legacy "command nonce" removal transition in the next
4978+
// release after the mandatory version:
4979+
//
4980+
if (pfrom->nVersion < PROTOCOL_VERSION) {
4981+
std::string acid;
4982+
vRecv >> acid;
4983+
}
4984+
50254985
uint256 hashBlock = block.GetHash(true);
50264986

50274987
LogPrintf(" Received block %s; ", hashBlock.ToString());
@@ -5095,25 +5055,29 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
50955055
}
50965056
else if (strCommand == "ping")
50975057
{
5098-
std::string acid = "";
5099-
if (pfrom->nVersion > BIP0031_VERSION)
5100-
{
5101-
uint64_t nonce = 0;
5102-
vRecv >> nonce >> acid;
5058+
uint64_t nonce = 0;
5059+
vRecv >> nonce;
51035060

5104-
// Echo the message back with the nonce. This allows for two useful features:
5105-
//
5106-
// 1) A remote node can quickly check if the connection is operational
5107-
// 2) Remote nodes can measure the latency of the network thread. If this node
5108-
// is overloaded it won't respond to pings quickly and the remote node can
5109-
// avoid sending us more work, like chain download requests.
5110-
//
5111-
// The nonce stops the remote getting confused between different pings: without
5112-
// it, if the remote node sends a ping once per second and this node takes 5
5113-
// seconds to respond to each, the 5th ping the remote sends would appear to
5114-
// return very quickly.
5115-
pfrom->PushMessage("pong", nonce);
5061+
// TODO: drop legacy "command nonce" removal transition in the next
5062+
// release after the mandatory version:
5063+
//
5064+
if (pfrom->nVersion < PROTOCOL_VERSION) {
5065+
std::string acid;
5066+
vRecv >> acid;
51165067
}
5068+
5069+
// Echo the message back with the nonce. This allows for two useful features:
5070+
//
5071+
// 1) A remote node can quickly check if the connection is operational
5072+
// 2) Remote nodes can measure the latency of the network thread. If this node
5073+
// is overloaded it won't respond to pings quickly and the remote node can
5074+
// avoid sending us more work, like chain download requests.
5075+
//
5076+
// The nonce stops the remote getting confused between different pings: without
5077+
// it, if the remote node sends a ping once per second and this node takes 5
5078+
// seconds to respond to each, the 5th ping the remote sends would appear to
5079+
// return very quickly.
5080+
pfrom->PushMessage("pong", nonce);
51175081
}
51185082
else if (strCommand == "pong")
51195083
{
@@ -5392,16 +5356,16 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
53925356
}
53935357
pto->fPingQueued = false;
53945358
pto->nPingUsecStart = GetTimeMicros();
5395-
if (pto->nVersion > BIP0031_VERSION)
5396-
{
5397-
pto->nPingNonceSent = nonce;
5398-
std::string acid = GetCommandNonce("ping");
5359+
pto->nPingNonceSent = nonce;
5360+
5361+
// TODO: drop legacy "command nonce" removal transition in the next
5362+
// release after the mandatory version:
5363+
//
5364+
if (pto->nVersion >= PROTOCOL_VERSION) {
5365+
pto->PushMessage("ping", nonce);
5366+
} else {
5367+
std::string acid;
53995368
pto->PushMessage("ping", nonce, acid);
5400-
} else
5401-
{
5402-
// Peer is too old to support ping command with nonce, pong will never arrive.
5403-
pto->nPingNonceSent = 0;
5404-
pto->PushMessage("ping");
54055369
}
54065370
}
54075371

0 commit comments

Comments
 (0)