| 510 | } |
| 511 | |
| 512 | bool ProcessGetHeadersMessage(CNode *pFrom, CDataStream &vRecv) { |
| 513 | CBlockLocator locator; |
| 514 | uint256 hashStop; |
| 515 | vRecv >> locator >> hashStop; |
| 516 | |
| 517 | LOCK(cs_main); |
| 518 | |
| 519 | CBlockIndex *pIndex = nullptr; |
| 520 | if (locator.IsNull()) { |
| 521 | // If locator is null, return the hashStop block |
| 522 | map<uint256, CBlockIndex *>::iterator mi = mapBlockIndex.find(hashStop); |
| 523 | if (mi == mapBlockIndex.end()) |
| 524 | return true; |
| 525 | |
| 526 | pIndex = (*mi).second; |
| 527 | } else { |
| 528 | // Find the last block the caller has in the main chain |
| 529 | pIndex = chainActive.FindFork(mapBlockIndex, locator); |
| 530 | if (pIndex) |
| 531 | pIndex = chainActive.Next(pIndex); |
| 532 | } |
| 533 | |
| 534 | // We must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end |
| 535 | vector<CBlock> vHeaders; |
| 536 | int32_t nLimit = 2000; |
| 537 | LogPrint(BCLog::NET, "getheaders %d to %s from peer %s\n", (pIndex ? pIndex->height : -1), hashStop.ToString(), |
| 538 | pFrom->addr.ToString()); |
| 539 | |
| 540 | for (; pIndex; pIndex = chainActive.Next(pIndex)) { |
| 541 | CBlockHeader blockHeader; |
| 542 | if (!GetBlockHeader(pIndex, blockHeader)) |
| 543 | return ERRORMSG("get block=%s index failed", pIndex->GetIdString()); |
| 544 | vHeaders.push_back(blockHeader); |
| 545 | |
| 546 | if (--nLimit <= 0 || pIndex->GetBlockHash() == hashStop) |
| 547 | break; |
| 548 | } |
| 549 | pFrom->PushMessage("headers", vHeaders); |
| 550 | |
| 551 | return false; |
| 552 | } |
| 553 | |
| 554 | void ProcessGetBlocksMessage(CNode *pFrom, CDataStream &vRecv) { |
| 555 | CBlockLocator locator; |
no test coverage detected