{"action":"created","comment":{"url":"https://api.github.com/repos/bitcoin/bitcoin/pulls/comments/44813816","id":44813816,"diff_hunk":"@@ -4970,6 +5080,92 @@ bool SendMessages(CNode* pto, bool fSendTrickle)\n         }\n \n         //\n+        // Try sending block announcements via headers\n+        //\n+        {\n+            // If we have less than MAX_BLOCKS_TO_ANNOUNCE in our\n+            // list of block hashes we're relaying, and our peer wants\n+            // headers announcements, then find the first header\n+            // not yet known to our peer but would connect, and send.\n+            // If no header would connect, or if we have too many\n+            // blocks, or if the peer doesn't want headers, just\n+            // add all to the inv queue.\n+            LOCK(pto->cs_inventory);\n+            vector<CBlock> vHeaders;\n+            bool fRevertToInv = (!state.fPreferHeaders || pto->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE);\n+            CBlockIndex *pBestIndex = NULL; // last header queued for delivery\n+            ProcessBlockAvailability(pto->id); // ensure pindexBestKnownBlock is up-to-date\n+\n+            if (!fRevertToInv) {\n+                bool fFoundStartingHeader = false;\n+                // Try to find first header that our peer doesn't have, and\n+                // then send all headers past that one.  If we come across any\n+                // headers that aren't on chainActive, give up.\n+                BOOST_FOREACH(const uint256 &hash, pto->vBlockHashesToAnnounce) {\n+                    BlockMap::iterator mi = mapBlockIndex.find(hash);\n+                    assert(mi != mapBlockIndex.end());\n+                    CBlockIndex *pindex = mi->second;\n+                    if (chainActive[pindex->nHeight] != pindex) {\n+                        // Bail out if we reorged away from this block","path":"src/main.cpp","position":298,"original_position":297,"commit_id":"3e663cc7a390a5a8c206b407dd61e170dc43943e","original_commit_id":"83602363a391633f0fdb9a836c5d40e94ec8db98","user":{"login":"sipa","id":548488,"avatar_url":"https://avatars.githubusercontent.com/u/548488?v=3","gravatar_id":"","url":"https://api.github.com/users/sipa","html_url":"https://github.com/sipa","followers_url":"https://api.github.com/users/sipa/followers","following_url":"https://api.github.com/users/sipa/following{/other_user}","gists_url":"https://api.github.com/users/sipa/gists{/gist_id}","starred_url":"https://api.github.com/users/sipa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sipa/subscriptions","organizations_url":"https://api.github.com/users/sipa/orgs","repos_url":"https://api.github.com/users/sipa/repos","events_url":"https://api.github.com/users/sipa/events{/privacy}","received_events_url":"https://api.github.com/users/sipa/received_events","type":"User","site_admin":false},"body":"I think it's harmless, as the current code in this PR mimicks the old behaviour. But if we're doing the check anyway, I think we can skip the entry.","created_at":"2015-11-13T18:09:19Z","updated_at":"2015-11-13T18:09:19Z","html_url":"https://github.com/bitcoin/bitcoin/pull/6494#discussion_r44813816","pull_request_url":"https://api.github.com/repos/bitcoin/bitcoin/pulls/6494","_links":{"self":{"href":"https://api.github.com/repos/bitcoin/bitcoin/pulls/comments/44813816"},"html":{"href":"https://github.com/bitcoin/bitcoin/pull/6494#discussion_r44813816"},"pull_request":{"href":"https://api.github.com/repos/bitcoin/bitcoin/pulls/6494"}}},"pull_request":{"url":"https://api.github.com/repos/bitcoin/bitcoin/pulls/6494","id":41275766,"html_url":"https://github.com/bitcoin/bitcoin/pull/6494","diff_url":"https://github.com/bitcoin/bitcoin/pull/6494.diff","patch_url":"https://github.com/bitcoin/bitcoin/pull/6494.patch","issue_url":"https://api.github.com/repos/bitcoin/bitcoin/issues/6494","number":6494,"state":"open","locked":false,"title":"Allow block announcements with headers","user":{"login":"sdaftuar","id":7463573,"avatar_url":"https://avatars.githubusercontent.com/u/7463573?v=3","gravatar_id":"","url":"https://api.github.com/users/sdaftuar","html_url":"https://github.com/sdaftuar","followers_url":"https://api.github.com/users/sdaftuar/followers","following_url":"https://api.github.com/users/sdaftuar/following{/other_user}","gists_url":"https://api.github.com/users/sdaftuar/gists{/gist_id}","starred_url":"https://api.github.com/users/sdaftuar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sdaftuar/subscriptions","organizations_url":"https://api.github.com/users/sdaftuar/orgs","repos_url":"https://api.github.com/users/sdaftuar/repos","events_url":"https://api.github.com/users/sdaftuar/events{/privacy}","received_events_url":"https://api.github.com/users/sdaftuar/received_events","type":"User","site_admin":false},"body":"This is an implementation of #5982 (and based off @sipa's commit in #5307, which has been squashed into this commit).\r\n\r\nCurrently new blocks are announced via ```inv``` messages.  Since headers-first download was introduced in 0.10, blocks are not processed unless the headers for the block connect to known, valid headers, so peers receiving ```inv``` messages respond with a ```getheaders``` and a ```getdata```: the expectation is that the announcing peer will send the headers for the block and any parents unknown to the peer before delivering the new block.\r\n\r\nIn the case of a reorg, nodes currently announce only the new tip, and not the blocks that lead to the tip.  This means that an extra round-trip is required to relay a reorg, because the peer receiving the ```inv``` for the tip must wait for the response to the ```getheaders``` message before being able to request the missing blocks.  (For additional reasons discussed in https://github.com/bitcoin/bitcoin/pull/5307#issuecomment-93785348, it's not optimal to ```inv``` all the blocks added in the reorg to a headers-first peer, because this results in needless traffic due to the way ```getheaders``` requests are generated when direct-fetching ```inv```'ed blocks.)\r\n\r\nThis pull implements a new ```sendheaders``` p2p message, which a node sends to its peers to indicate that the node prefers to receive new block announcements via a ```headers``` message rather than an inv.  This implementation does a few things:\r\n\r\n* When announcing a new block to a peer that prefers headers, try to announce all the blocks that are being added, back to the last fork point, by sending headers for each.\r\n* Avoid sending headers for blocks that are already known to the peer (determined by checking ```pindexBestKnownBlock``` and ```pindexBestHeaderSent``` and their ancestors).\r\n  * pindexBestKnownBlock is updated based on ```inv``` messages, ```headers``` sent from that peer, and ```getheaders``` messages sent from the peer.\r\n  * ```pindexBestHeaderSent``` is a new variable that tracks the best header we have sent to the peer\r\n* Avoid sending more than 8 headers to the peer.  This code is designed to be optimized for relaying at the tip, so a large reorg is some kind of error condition; in that case avoid spamming the peer with a lot of headers and instead fall back to the old ```inv``` mechanism.\r\n* If the headers to be announced aren't known to connect to headers that the peer has, then revert to sending an ```inv``` at the tip instead.  This is designed to avoid DoS points from sending headers that don't connect.  Since every new block should generally be announced by one peer in any pair of peers, it's expected that once headers-announcement begins on a link (ie once two nodes are known to be synced), it should be able to continue.\r\n* After #6148 is merged, this would allow pruning nodes to generally be able to relay reorgs to peers via headers announcements.  The code that implements direct fetching upon receipt of a headers message will send ```getdata``` requests to any peer that announces a block via a headers message.\r\n\r\nThis pull includes a new python p2p test exercising the new functionality.\r\n\r\nBIP 130 describes the proposed new ```sendheaders``` p2p message here:\r\nhttps://github.com/bitcoin/bips/blob/master/bip-0130.mediawiki ","created_at":"2015-07-30T20:03:49Z","updated_at":"2015-11-13T18:09:20Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d63f4533a24deb0d16868333d16bd54a3d151f00","assignee":null,"milestone":{"url":"https://api.github.com/repos/bitcoin/bitcoin/milestones/19","html_url":"https://github.com/bitcoin/bitcoin/milestones/0.12.0","labels_url":"https://api.github.com/repos/bitcoin/bitcoin/milestones/19/labels","id":1092458,"number":19,"title":"0.12.0","description":"","creator":{"login":"laanwj","id":126646,"avatar_url":"https://avatars.githubusercontent.com/u/126646?v=3","gravatar_id":"","url":"https://api.github.com/users/laanwj","html_url":"https://github.com/laanwj","followers_url":"https://api.github.com/users/laanwj/followers","following_url":"https://api.github.com/users/laanwj/following{/other_user}","gists_url":"https://api.github.com/users/laanwj/gists{/gist_id}","starred_url":"https://api.github.com/users/laanwj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/laanwj/subscriptions","organizations_url":"https://api.github.com/users/laanwj/orgs","repos_url":"https://api.github.com/users/laanwj/repos","events_url":"https://api.github.com/users/laanwj/events{/privacy}","received_events_url":"https://api.github.com/users/laanwj/received_events","type":"User","site_admin":false},"open_issues":9,"closed_issues":13,"state":"open","created_at":"2015-05-01T12:31:35Z","updated_at":"2015-11-13T15:52:00Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/bitcoin/bitcoin/pulls/6494/commits","review_comments_url":"https://api.github.com/repos/bitcoin/bitcoin/pulls/6494/comments","review_comment_url":"https://api.github.com/repos/bitcoin/bitcoin/pulls/comments{/number}","comments_url":"https://api.github.com/repos/bitcoin/bitcoin/issues/6494/comments","statuses_url":"https://api.github.com/repos/bitcoin/bitcoin/statuses/3e663cc7a390a5a8c206b407dd61e170dc43943e","head":{"label":"sdaftuar:direct-headers-announcement","ref":"direct-headers-announcement","sha":"3e663cc7a390a5a8c206b407dd61e170dc43943e","user":{"login":"sdaftuar","id":7463573,"avatar_url":"https://avatars.githubusercontent.com/u/7463573?v=3","gravatar_id":"","url":"https://api.github.com/users/sdaftuar","html_url":"https://github.com/sdaftuar","followers_url":"https://api.github.com/users/sdaftuar/followers","following_url":"https://api.github.com/users/sdaftuar/following{/other_user}","gists_url":"https://api.github.com/users/sdaftuar/gists{/gist_id}","starred_url":"https://api.github.com/users/sdaftuar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sdaftuar/subscriptions","organizations_url":"https://api.github.com/users/sdaftuar/orgs","repos_url":"https://api.github.com/users/sdaftuar/repos","events_url":"https://api.github.com/users/sdaftuar/events{/privacy}","received_events_url":"https://api.github.com/users/sdaftuar/received_events","type":"User","site_admin":false},"repo":{"id":28761781,"name":"bitcoin","full_name":"sdaftuar/bitcoin","owner":{"login":"sdaftuar","id":7463573,"avatar_url":"https://avatars.githubusercontent.com/u/7463573?v=3","gravatar_id":"","url":"https://api.github.com/users/sdaftuar","html_url":"https://github.com/sdaftuar","followers_url":"https://api.github.com/users/sdaftuar/followers","following_url":"https://api.github.com/users/sdaftuar/following{/other_user}","gists_url":"https://api.github.com/users/sdaftuar/gists{/gist_id}","starred_url":"https://api.github.com/users/sdaftuar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sdaftuar/subscriptions","organizations_url":"https://api.github.com/users/sdaftuar/orgs","repos_url":"https://api.github.com/users/sdaftuar/repos","events_url":"https://api.github.com/users/sdaftuar/events{/privacy}","received_events_url":"https://api.github.com/users/sdaftuar/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/sdaftuar/bitcoin","description":"Bitcoin Core integration/staging tree","fork":true,"url":"https://api.github.com/repos/sdaftuar/bitcoin","forks_url":"https://api.github.com/repos/sdaftuar/bitcoin/forks","keys_url":"https://api.github.com/repos/sdaftuar/bitcoin/keys{/key_id}","collaborators_url":"https://api.github.com/repos/sdaftuar/bitcoin/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/sdaftuar/bitcoin/teams","hooks_url":"https://api.github.com/repos/sdaftuar/bitcoin/hooks","issue_events_url":"https://api.github.com/repos/sdaftuar/bitcoin/issues/events{/number}","events_url":"https://api.github.com/repos/sdaftuar/bitcoin/events","assignees_url":"https://api.github.com/repos/sdaftuar/bitcoin/assignees{/user}","branches_url":"https://api.github.com/repos/sdaftuar/bitcoin/branches{/branch}","tags_url":"https://api.github.com/repos/sdaftuar/bitcoin/tags","blobs_url":"https://api.github.com/repos/sdaftuar/bitcoin/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/sdaftuar/bitcoin/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/sdaftuar/bitcoin/git/refs{/sha}","trees_url":"https://api.github.com/repos/sdaftuar/bitcoin/git/trees{/sha}","statuses_url":"https://api.github.com/repos/sdaftuar/bitcoin/statuses/{sha}","languages_url":"https://api.github.com/repos/sdaftuar/bitcoin/languages","stargazers_url":"https://api.github.com/repos/sdaftuar/bitcoin/stargazers","contributors_url":"https://api.github.com/repos/sdaftuar/bitcoin/contributors","subscribers_url":"https://api.github.com/repos/sdaftuar/bitcoin/subscribers","subscription_url":"https://api.github.com/repos/sdaftuar/bitcoin/subscription","commits_url":"https://api.github.com/repos/sdaftuar/bitcoin/commits{/sha}","git_commits_url":"https://api.github.com/repos/sdaftuar/bitcoin/git/commits{/sha}","comments_url":"https://api.github.com/repos/sdaftuar/bitcoin/comments{/number}","issue_comment_url":"https://api.github.com/repos/sdaftuar/bitcoin/issues/comments{/number}","contents_url":"https://api.github.com/repos/sdaftuar/bitcoin/contents/{+path}","compare_url":"https://api.github.com/repos/sdaftuar/bitcoin/compare/{base}...{head}","merges_url":"https://api.github.com/repos/sdaftuar/bitcoin/merges","archive_url":"https://api.github.com/repos/sdaftuar/bitcoin/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/sdaftuar/bitcoin/downloads","issues_url":"https://api.github.com/repos/sdaftuar/bitcoin/issues{/number}","pulls_url":"https://api.github.com/repos/sdaftuar/bitcoin/pulls{/number}","milestones_url":"https://api.github.com/repos/sdaftuar/bitcoin/milestones{/number}","notifications_url":"https://api.github.com/repos/sdaftuar/bitcoin/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/sdaftuar/bitcoin/labels{/name}","releases_url":"https://api.github.com/repos/sdaftuar/bitcoin/releases{/id}","created_at":"2015-01-04T02:52:13Z","updated_at":"2015-04-24T18:52:36Z","pushed_at":"2015-11-13T18:01:06Z","git_url":"git://github.com/sdaftuar/bitcoin.git","ssh_url":"git@github.com:sdaftuar/bitcoin.git","clone_url":"https://github.com/sdaftuar/bitcoin.git","svn_url":"https://github.com/sdaftuar/bitcoin","homepage":"https://bitcoin.org/en/download","size":57713,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"bitcoin:master","ref":"master","sha":"d3565604e3d94c074ee3f79b61dee9610b870a4c","user":{"login":"bitcoin","id":528860,"avatar_url":"https://avatars.githubusercontent.com/u/528860?v=3","gravatar_id":"","url":"https://api.github.com/users/bitcoin","html_url":"https://github.com/bitcoin","followers_url":"https://api.github.com/users/bitcoin/followers","following_url":"https://api.github.com/users/bitcoin/following{/other_user}","gists_url":"https://api.github.com/users/bitcoin/gists{/gist_id}","starred_url":"https://api.github.com/users/bitcoin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bitcoin/subscriptions","organizations_url":"https://api.github.com/users/bitcoin/orgs","repos_url":"https://api.github.com/users/bitcoin/repos","events_url":"https://api.github.com/users/bitcoin/events{/privacy}","received_events_url":"https://api.github.com/users/bitcoin/received_events","type":"Organization","site_admin":false},"repo":{"id":1181927,"name":"bitcoin","full_name":"bitcoin/bitcoin","owner":{"login":"bitcoin","id":528860,"avatar_url":"https://avatars.githubusercontent.com/u/528860?v=3","gravatar_id":"","url":"https://api.github.com/users/bitcoin","html_url":"https://github.com/bitcoin","followers_url":"https://api.github.com/users/bitcoin/followers","following_url":"https://api.github.com/users/bitcoin/following{/other_user}","gists_url":"https://api.github.com/users/bitcoin/gists{/gist_id}","starred_url":"https://api.github.com/users/bitcoin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bitcoin/subscriptions","organizations_url":"https://api.github.com/users/bitcoin/orgs","repos_url":"https://api.github.com/users/bitcoin/repos","events_url":"https://api.github.com/users/bitcoin/events{/privacy}","received_events_url":"https://api.github.com/users/bitcoin/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/bitcoin/bitcoin","description":"Bitcoin Core integration/staging tree","fork":false,"url":"https://api.github.com/repos/bitcoin/bitcoin","forks_url":"https://api.github.com/repos/bitcoin/bitcoin/forks","keys_url":"https://api.github.com/repos/bitcoin/bitcoin/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bitcoin/bitcoin/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bitcoin/bitcoin/teams","hooks_url":"https://api.github.com/repos/bitcoin/bitcoin/hooks","issue_events_url":"https://api.github.com/repos/bitcoin/bitcoin/issues/events{/number}","events_url":"https://api.github.com/repos/bitcoin/bitcoin/events","assignees_url":"https://api.github.com/repos/bitcoin/bitcoin/assignees{/user}","branches_url":"https://api.github.com/repos/bitcoin/bitcoin/branches{/branch}","tags_url":"https://api.github.com/repos/bitcoin/bitcoin/tags","blobs_url":"https://api.github.com/repos/bitcoin/bitcoin/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bitcoin/bitcoin/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bitcoin/bitcoin/git/refs{/sha}","trees_url":"https://api.github.com/repos/bitcoin/bitcoin/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bitcoin/bitcoin/statuses/{sha}","languages_url":"https://api.github.com/repos/bitcoin/bitcoin/languages","stargazers_url":"https://api.github.com/repos/bitcoin/bitcoin/stargazers","contributors_url":"https://api.github.com/repos/bitcoin/bitcoin/contributors","subscribers_url":"https://api.github.com/repos/bitcoin/bitcoin/subscribers","subscription_url":"https://api.github.com/repos/bitcoin/bitcoin/subscription","commits_url":"https://api.github.com/repos/bitcoin/bitcoin/commits{/sha}","git_commits_url":"https://api.github.com/repos/bitcoin/bitcoin/git/commits{/sha}","comments_url":"https://api.github.com/repos/bitcoin/bitcoin/comments{/number}","issue_comment_url":"https://api.github.com/repos/bitcoin/bitcoin/issues/comments{/number}","contents_url":"https://api.github.com/repos/bitcoin/bitcoin/contents/{+path}","compare_url":"https://api.github.com/repos/bitcoin/bitcoin/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bitcoin/bitcoin/merges","archive_url":"https://api.github.com/repos/bitcoin/bitcoin/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bitcoin/bitcoin/downloads","issues_url":"https://api.github.com/repos/bitcoin/bitcoin/issues{/number}","pulls_url":"https://api.github.com/repos/bitcoin/bitcoin/pulls{/number}","milestones_url":"https://api.github.com/repos/bitcoin/bitcoin/milestones{/number}","notifications_url":"https://api.github.com/repos/bitcoin/bitcoin/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bitcoin/bitcoin/labels{/name}","releases_url":"https://api.github.com/repos/bitcoin/bitcoin/releases{/id}","created_at":"2010-12-19T15:16:43Z","updated_at":"2015-11-13T17:23:11Z","pushed_at":"2015-11-13T18:01:07Z","git_url":"git://github.com/bitcoin/bitcoin.git","ssh_url":"git@github.com:bitcoin/bitcoin.git","clone_url":"https://github.com/bitcoin/bitcoin.git","svn_url":"https://github.com/bitcoin/bitcoin","homepage":"https://bitcoin.org/en/download","size":142106,"stargazers_count":7802,"watchers_count":7802,"language":"C++","has_issues":true,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":5370,"mirror_url":null,"open_issues_count":414,"forks":5370,"open_issues":414,"watchers":7802,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/bitcoin/bitcoin/pulls/6494"},"html":{"href":"https://github.com/bitcoin/bitcoin/pull/6494"},"issue":{"href":"https://api.github.com/repos/bitcoin/bitcoin/issues/6494"},"comments":{"href":"https://api.github.com/repos/bitcoin/bitcoin/issues/6494/comments"},"review_comments":{"href":"https://api.github.com/repos/bitcoin/bitcoin/pulls/6494/comments"},"review_comment":{"href":"https://api.github.com/repos/bitcoin/bitcoin/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/bitcoin/bitcoin/pulls/6494/commits"},"statuses":{"href":"https://api.github.com/repos/bitcoin/bitcoin/statuses/3e663cc7a390a5a8c206b407dd61e170dc43943e"}}}}