Skip to content

Commit

Permalink
Merge pull request bitcoin#587 from ptschip/dev_timer2
Browse files Browse the repository at this point in the history
thinblocksinflight timer updates
  • Loading branch information
gandrewstone committed May 24, 2017
2 parents c1554bb + 0d5d1d5 commit 8a0cb38
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 38 deletions.
37 changes: 17 additions & 20 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7198,31 +7198,28 @@ bool SendMessages(CNode *pto)
}
}

if (pto->ThinBlockCapable())
{
// Check to see if there are any thinblocks in flight that have gone beyond the timeout interval.
// If so then we need to disconnect them so that the thinblock data is nullified. We coud null
// the thinblock data here but that would possible cause a node to be baneed later if the thinblock
// finally did show up. Better to just disconnect this slow node instead.
if (pto->mapThinBlocksInFlight.size() > 0)
// Check to see if there are any thinblocks in flight that have gone beyond the timeout interval.
// If so then we need to disconnect them so that the thinblock data is nullified. We coud null
// the thinblock data here but that would possible cause a node to be baneed later if the thinblock
// finally did show up. Better to just disconnect this slow node instead.
if (pto->mapThinBlocksInFlight.size() > 0)
{
LOCK(pto->cs_mapthinblocksinflight);
std::map<uint256, CNode::CThinBlockInFlight>::iterator iter = pto->mapThinBlocksInFlight.begin();
while (iter != pto->mapThinBlocksInFlight.end())
{
LOCK(pto->cs_mapthinblocksinflight);
std::map<uint256, int64_t>::iterator iter = pto->mapThinBlocksInFlight.begin();
while (iter != pto->mapThinBlocksInFlight.end())
if (!(*iter).second.fReceived && (GetTime() - (*iter).second.nRequestTime) > THINBLOCK_DOWNLOAD_TIMEOUT)
{
if ((*iter).second != -1 && (GetTime() - (*iter).second) > THINBLOCK_DOWNLOAD_TIMEOUT)
if (!pto->fWhitelisted && Params().NetworkIDString() != "regtest")
{
if (!pto->fWhitelisted && Params().NetworkIDString() != "regtest")
{
LogPrint("thin", "ERROR: Disconnecting peer=%d due to download timeout exceeded "
"(%d secs)\n",
pto->GetId(), (GetTime() - (*iter).second));
pto->fDisconnect = true;
break;
}
LogPrint("thin", "ERROR: Disconnecting peer=%d due to download timeout exceeded "
"(%d secs)\n",
pto->GetId(), (GetTime() - (*iter).second.nRequestTime));
pto->fDisconnect = true;
break;
}
iter++;
}
iter++;
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,18 @@ class CNetCleanup
class CNode
{
public:
struct CThinBlockInFlight
{
int64_t nRequestTime;
bool fReceived;

CThinBlockInFlight()
{
nRequestTime = GetTime();
fReceived = false;
}
};

// socket
uint64_t nServices;
SOCKET hSocket;
Expand Down Expand Up @@ -416,7 +428,7 @@ class CNode
int nSizeThinBlock; // Original on-wire size of the block. Just used for reporting
int thinBlockWaitingForTxns; // if -1 then not currently waiting
CCriticalSection cs_mapthinblocksinflight; // lock mapThinBlocksInFlight
std::map<uint256, int64_t> mapThinBlocksInFlight; // thin blocks in flight and the time requested.
std::map<uint256, CThinBlockInFlight> mapThinBlocksInFlight; // thin blocks in flight and the time requested.
double nGetXBlockTxCount; // Count how many get_xblocktx requests are made
uint64_t nGetXBlockTxLastTime; // The last time a get_xblocktx request was made
double nGetXthinCount; // Count how many get_xthin requests are made
Expand Down
10 changes: 4 additions & 6 deletions src/parallel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,15 +496,13 @@ void HandleBlockMessageThread(CNode *pfrom, const string &strCommand, const CBlo
CValidationState state;
uint64_t nSizeBlock = ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION);

// At this point we have either a block or a fully reconstructed thinblock but we still need to
// maintain a mapThinBlocksInFlight entry so that we don't re-request a full block from
// the same node while the block is processing. Furthermore by setting the time = -1 we prevent
// the timeout from triggering and inadvertently disconnecting the node in the event that the block
// takes a longer time to process than the THINBLOCK_DOWNLOAD_TIMEOUT interval.
// Indicate that the thinblock was fully received. At this point we have either a block or a fully reconstructed
// thinblock but we still need to maintain a mapThinBlocksInFlight entry so that we don't re-request a full block
// from the same node while the block is processing.
{
LOCK(pfrom->cs_mapthinblocksinflight);
if (pfrom->mapThinBlocksInFlight.count(inv.hash))
pfrom->mapThinBlocksInFlight[inv.hash] = -1;
pfrom->mapThinBlocksInFlight[inv.hash].fReceived = true;
}


Expand Down
6 changes: 4 additions & 2 deletions src/requestManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,8 @@ bool RequestBlock(CNode *pfrom, CInv obj)
{ // We can only send one thinblock per peer at a time
{
LOCK(pfrom->cs_mapthinblocksinflight);
pfrom->mapThinBlocksInFlight[inv2.hash] = GetTime();
pfrom->mapThinBlocksInFlight.insert(
std::pair<uint256, CNode::CThinBlockInFlight>(inv2.hash, CNode::CThinBlockInFlight()));
}
inv2.type = MSG_XTHINBLOCK;
std::vector<uint256> vOrphanHashes;
Expand Down Expand Up @@ -402,7 +403,8 @@ bool RequestBlock(CNode *pfrom, CInv obj)
{
{
LOCK(pfrom->cs_mapthinblocksinflight);
pfrom->mapThinBlocksInFlight[inv2.hash] = GetTime();
pfrom->mapThinBlocksInFlight.insert(
std::pair<uint256, CNode::CThinBlockInFlight>(inv2.hash, CNode::CThinBlockInFlight()));
}
inv2.type = MSG_XTHINBLOCK;
std::vector<uint256> vOrphanHashes;
Expand Down
18 changes: 9 additions & 9 deletions src/test/exploit_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ BOOST_AUTO_TEST_CASE(thinblock_tests)
excessiveBlockSize = 234;

// Add the node to vNodes and also we need a thinblockinflight entry
dummyNode6.mapThinBlocksInFlight[TestBlock1().GetHash()] = GetTime();
dummyNode6.mapThinBlocksInFlight[TestBlock1().GetHash()].nRequestTime = GetTime();
vNodes.push_back(&dummyNode6);

// Process an xthinblock
Expand Down Expand Up @@ -755,13 +755,13 @@ BOOST_AUTO_TEST_CASE(thinblock_tests)
excessiveBlockSize = 234;

// Add the node to vNodes and also we need a thinblockinflight entry
dummyNode6.mapThinBlocksInFlight[TestBlock1().GetHash()] = GetTime();
dummyNode6.mapThinBlocksInFlight[TestBlock1().GetHash()].nRequestTime = GetTime();
vNodes.push_back(&dummyNode6);
dummyNode7.mapThinBlocksInFlight[TestBlock1().GetHash()] = GetTime();
dummyNode7.mapThinBlocksInFlight[TestBlock1().GetHash()].nRequestTime = GetTime();
vNodes.push_back(&dummyNode7);
dummyNode8.mapThinBlocksInFlight[TestBlock1().GetHash()] = GetTime();
dummyNode8.mapThinBlocksInFlight[TestBlock1().GetHash()].nRequestTime = GetTime();
vNodes.push_back(&dummyNode8);
dummyNode9.mapThinBlocksInFlight[TestBlock1().GetHash()] = GetTime();
dummyNode9.mapThinBlocksInFlight[TestBlock1().GetHash()].nRequestTime = GetTime();
vNodes.push_back(&dummyNode9);

// manually set the nLocalThinBlockBytes to be lower than the actual bytes of the thinblock that we will
Expand Down Expand Up @@ -816,13 +816,13 @@ BOOST_AUTO_TEST_CASE(thinblock_tests)
excessiveBlockSize = 234;

// Add the node to vNodes and also we need a thinblockinflight entry
dummyNode6.mapThinBlocksInFlight[TestBlock1().GetHash()] = GetTime();
dummyNode6.mapThinBlocksInFlight[TestBlock1().GetHash()].nRequestTime = GetTime();
vNodes.push_back(&dummyNode6);
dummyNode7.mapThinBlocksInFlight[TestBlock1().GetHash()] = GetTime();
dummyNode7.mapThinBlocksInFlight[TestBlock1().GetHash()].nRequestTime = GetTime();
vNodes.push_back(&dummyNode7);
dummyNode8.mapThinBlocksInFlight[TestBlock1().GetHash()] = GetTime();
dummyNode8.mapThinBlocksInFlight[TestBlock1().GetHash()].nRequestTime = GetTime();
vNodes.push_back(&dummyNode8);
dummyNode9.mapThinBlocksInFlight[TestBlock1().GetHash()] = GetTime();
dummyNode9.mapThinBlocksInFlight[TestBlock1().GetHash()].nRequestTime = GetTime();
vNodes.push_back(&dummyNode9);

// manually set two of the nLocalThinBlockBytes to be higher than the actual bytes of the thinblock that we will
Expand Down

0 comments on commit 8a0cb38

Please sign in to comment.