{"action":"created","comment":{"url":"https://api.github.com/repos/pipermerriam/py-evm/pulls/comments/147639831","pull_request_review_id":72761552,"id":147639831,"diff_hunk":"@@ -56,19 +57,56 @@ def get_canonical_head(self):\n             raise CanonicalHeadNotFound(\"No canonical head set for this chain\")\n         return self.get_block_header_by_hash(self.db.get(CANONICAL_HEAD_HASH_DB_KEY))\n \n-    def set_canonical_head(self, head_hash):\n-        try:\n-            self.get_block_header_by_hash(head_hash)\n-        except BlockNotFound:\n-            raise ValueError(\"Cannot use unknown block hash as canonical head: {}\".format(\n-                head_hash))\n-        self.db.set(CANONICAL_HEAD_HASH_DB_KEY, head_hash)\n+    def get_canonical_block_header_by_number(self, block_number):\n+        \"\"\"\n+        Returns the block header with the given number in the canonical chain.\n+\n+        Raises BlockNotFound if there's no block header with the given number in the\n+        canonical chain.\n+        \"\"\"\n+        validate_uint256(block_number, title=\"Block Number\")\n+        return self.get_block_header_by_hash(self.lookup_block_hash(block_number))\n \n     def get_score(self, block_hash):\n         return rlp.decode(\n             self.db.get(make_block_hash_to_score_lookup_key(block_hash)),\n             sedes=rlp.sedes.big_endian_int)\n \n+    def set_as_canonical_chain_head(self, header):\n+        \"\"\"\n+        Sets the header as the canonical chain HEAD.\n+        \"\"\"\n+        for h in reversed(self.find_common_ancestor(header)):\n+            self.add_block_number_to_hash_lookup(h)\n+\n+        try:\n+            self.get_block_header_by_hash(header.hash)\n+        except BlockNotFound:\n+            raise ValueError(\"Cannot use unknown block hash as canonical head: {}\".format(\n+                header.hash))\n+        self.db.set(CANONICAL_HEAD_HASH_DB_KEY, header.hash)\n+\n+    @to_tuple\n+    def find_common_ancestor(self, header):\n+        \"\"\"\n+        Returns the chain leading up from the given header until the first ancestor it has in\n+        common with our canonical chain.\n+        \"\"\"\n+        h = header\n+        while True:\n+            yield h\n+            if h.parent_hash == GENESIS_PARENT_HASH:\n+                break\n+            try:\n+                orig = self.get_canonical_block_header_by_number(h.block_number)\n+                if orig.hash == h.hash:","path":"evm/db/chain.py","position":61,"original_position":61,"commit_id":"014989d9b859976f2ba70f0a25aa6cac77322dc1","original_commit_id":"014989d9b859976f2ba70f0a25aa6cac77322dc1","user":{"login":"gsalgado","id":412274,"avatar_url":"https://avatars2.githubusercontent.com/u/412274?v=4","gravatar_id":"","url":"https://api.github.com/users/gsalgado","html_url":"https://github.com/gsalgado","followers_url":"https://api.github.com/users/gsalgado/followers","following_url":"https://api.github.com/users/gsalgado/following{/other_user}","gists_url":"https://api.github.com/users/gsalgado/gists{/gist_id}","starred_url":"https://api.github.com/users/gsalgado/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gsalgado/subscriptions","organizations_url":"https://api.github.com/users/gsalgado/orgs","repos_url":"https://api.github.com/users/gsalgado/repos","events_url":"https://api.github.com/users/gsalgado/events{/privacy}","received_events_url":"https://api.github.com/users/gsalgado/received_events","type":"User","site_admin":false},"body":"Yep, sure","created_at":"2017-10-30T08:37:23Z","updated_at":"2017-10-30T08:37:23Z","html_url":"https://github.com/pipermerriam/py-evm/pull/129#discussion_r147639831","pull_request_url":"https://api.github.com/repos/pipermerriam/py-evm/pulls/129","author_association":"COLLABORATOR","_links":{"self":{"href":"https://api.github.com/repos/pipermerriam/py-evm/pulls/comments/147639831"},"html":{"href":"https://github.com/pipermerriam/py-evm/pull/129#discussion_r147639831"},"pull_request":{"href":"https://api.github.com/repos/pipermerriam/py-evm/pulls/129"}},"in_reply_to_id":147470274},"pull_request":{"url":"https://api.github.com/repos/pipermerriam/py-evm/pulls/129","id":149173378,"html_url":"https://github.com/pipermerriam/py-evm/pull/129","diff_url":"https://github.com/pipermerriam/py-evm/pull/129.diff","patch_url":"https://github.com/pipermerriam/py-evm/pull/129.patch","issue_url":"https://api.github.com/repos/pipermerriam/py-evm/issues/129","number":129,"state":"open","locked":false,"title":"Update canonical chain info whenever we store a header in the DB","user":{"login":"gsalgado","id":412274,"avatar_url":"https://avatars2.githubusercontent.com/u/412274?v=4","gravatar_id":"","url":"https://api.github.com/users/gsalgado","html_url":"https://github.com/gsalgado","followers_url":"https://api.github.com/users/gsalgado/followers","following_url":"https://api.github.com/users/gsalgado/following{/other_user}","gists_url":"https://api.github.com/users/gsalgado/gists{/gist_id}","starred_url":"https://api.github.com/users/gsalgado/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gsalgado/subscriptions","organizations_url":"https://api.github.com/users/gsalgado/orgs","repos_url":"https://api.github.com/users/gsalgado/repos","events_url":"https://api.github.com/users/gsalgado/events{/privacy}","received_events_url":"https://api.github.com/users/gsalgado/received_events","type":"User","site_admin":false},"body":"The Chain class used to do that, but now it's done as part of BaseChainDB.persist_header_to_db().\r\n\r\nI needed this on another branch I'm working on (which changes the LightChain so that it can handle possibly diverging data from multiple peers), but this seemed like a worthwhile refactoring in itself so I'm submitting it separately","created_at":"2017-10-27T12:13:04Z","updated_at":"2017-10-30T08:37:23Z","closed_at":null,"merged_at":null,"merge_commit_sha":"11b02a3a022eebf62838b68cd101baa6f675c04c","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/pipermerriam/py-evm/pulls/129/commits","review_comments_url":"https://api.github.com/repos/pipermerriam/py-evm/pulls/129/comments","review_comment_url":"https://api.github.com/repos/pipermerriam/py-evm/pulls/comments{/number}","comments_url":"https://api.github.com/repos/pipermerriam/py-evm/issues/129/comments","statuses_url":"https://api.github.com/repos/pipermerriam/py-evm/statuses/014989d9b859976f2ba70f0a25aa6cac77322dc1","head":{"label":"gsalgado:canonical-head-refactoring","ref":"canonical-head-refactoring","sha":"014989d9b859976f2ba70f0a25aa6cac77322dc1","user":{"login":"gsalgado","id":412274,"avatar_url":"https://avatars2.githubusercontent.com/u/412274?v=4","gravatar_id":"","url":"https://api.github.com/users/gsalgado","html_url":"https://github.com/gsalgado","followers_url":"https://api.github.com/users/gsalgado/followers","following_url":"https://api.github.com/users/gsalgado/following{/other_user}","gists_url":"https://api.github.com/users/gsalgado/gists{/gist_id}","starred_url":"https://api.github.com/users/gsalgado/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gsalgado/subscriptions","organizations_url":"https://api.github.com/users/gsalgado/orgs","repos_url":"https://api.github.com/users/gsalgado/repos","events_url":"https://api.github.com/users/gsalgado/events{/privacy}","received_events_url":"https://api.github.com/users/gsalgado/received_events","type":"User","site_admin":false},"repo":{"id":94995992,"name":"py-evm","full_name":"gsalgado/py-evm","owner":{"login":"gsalgado","id":412274,"avatar_url":"https://avatars2.githubusercontent.com/u/412274?v=4","gravatar_id":"","url":"https://api.github.com/users/gsalgado","html_url":"https://github.com/gsalgado","followers_url":"https://api.github.com/users/gsalgado/followers","following_url":"https://api.github.com/users/gsalgado/following{/other_user}","gists_url":"https://api.github.com/users/gsalgado/gists{/gist_id}","starred_url":"https://api.github.com/users/gsalgado/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gsalgado/subscriptions","organizations_url":"https://api.github.com/users/gsalgado/orgs","repos_url":"https://api.github.com/users/gsalgado/repos","events_url":"https://api.github.com/users/gsalgado/events{/privacy}","received_events_url":"https://api.github.com/users/gsalgado/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/gsalgado/py-evm","description":"An alternative Ethereum Virtual Machine implementation","fork":true,"url":"https://api.github.com/repos/gsalgado/py-evm","forks_url":"https://api.github.com/repos/gsalgado/py-evm/forks","keys_url":"https://api.github.com/repos/gsalgado/py-evm/keys{/key_id}","collaborators_url":"https://api.github.com/repos/gsalgado/py-evm/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/gsalgado/py-evm/teams","hooks_url":"https://api.github.com/repos/gsalgado/py-evm/hooks","issue_events_url":"https://api.github.com/repos/gsalgado/py-evm/issues/events{/number}","events_url":"https://api.github.com/repos/gsalgado/py-evm/events","assignees_url":"https://api.github.com/repos/gsalgado/py-evm/assignees{/user}","branches_url":"https://api.github.com/repos/gsalgado/py-evm/branches{/branch}","tags_url":"https://api.github.com/repos/gsalgado/py-evm/tags","blobs_url":"https://api.github.com/repos/gsalgado/py-evm/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/gsalgado/py-evm/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/gsalgado/py-evm/git/refs{/sha}","trees_url":"https://api.github.com/repos/gsalgado/py-evm/git/trees{/sha}","statuses_url":"https://api.github.com/repos/gsalgado/py-evm/statuses/{sha}","languages_url":"https://api.github.com/repos/gsalgado/py-evm/languages","stargazers_url":"https://api.github.com/repos/gsalgado/py-evm/stargazers","contributors_url":"https://api.github.com/repos/gsalgado/py-evm/contributors","subscribers_url":"https://api.github.com/repos/gsalgado/py-evm/subscribers","subscription_url":"https://api.github.com/repos/gsalgado/py-evm/subscription","commits_url":"https://api.github.com/repos/gsalgado/py-evm/commits{/sha}","git_commits_url":"https://api.github.com/repos/gsalgado/py-evm/git/commits{/sha}","comments_url":"https://api.github.com/repos/gsalgado/py-evm/comments{/number}","issue_comment_url":"https://api.github.com/repos/gsalgado/py-evm/issues/comments{/number}","contents_url":"https://api.github.com/repos/gsalgado/py-evm/contents/{+path}","compare_url":"https://api.github.com/repos/gsalgado/py-evm/compare/{base}...{head}","merges_url":"https://api.github.com/repos/gsalgado/py-evm/merges","archive_url":"https://api.github.com/repos/gsalgado/py-evm/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/gsalgado/py-evm/downloads","issues_url":"https://api.github.com/repos/gsalgado/py-evm/issues{/number}","pulls_url":"https://api.github.com/repos/gsalgado/py-evm/pulls{/number}","milestones_url":"https://api.github.com/repos/gsalgado/py-evm/milestones{/number}","notifications_url":"https://api.github.com/repos/gsalgado/py-evm/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/gsalgado/py-evm/labels{/name}","releases_url":"https://api.github.com/repos/gsalgado/py-evm/releases{/id}","deployments_url":"https://api.github.com/repos/gsalgado/py-evm/deployments","created_at":"2017-06-21T11:16:04Z","updated_at":"2017-06-21T11:16:06Z","pushed_at":"2017-10-27T17:24:07Z","git_url":"git://github.com/gsalgado/py-evm.git","ssh_url":"git@github.com:gsalgado/py-evm.git","clone_url":"https://github.com/gsalgado/py-evm.git","svn_url":"https://github.com/gsalgado/py-evm","homepage":"","size":892,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"pipermerriam:master","ref":"master","sha":"f103c98a5a7c902534f1c68db8ec153b5300ba1e","user":{"login":"pipermerriam","id":824194,"avatar_url":"https://avatars3.githubusercontent.com/u/824194?v=4","gravatar_id":"","url":"https://api.github.com/users/pipermerriam","html_url":"https://github.com/pipermerriam","followers_url":"https://api.github.com/users/pipermerriam/followers","following_url":"https://api.github.com/users/pipermerriam/following{/other_user}","gists_url":"https://api.github.com/users/pipermerriam/gists{/gist_id}","starred_url":"https://api.github.com/users/pipermerriam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pipermerriam/subscriptions","organizations_url":"https://api.github.com/users/pipermerriam/orgs","repos_url":"https://api.github.com/users/pipermerriam/repos","events_url":"https://api.github.com/users/pipermerriam/events{/privacy}","received_events_url":"https://api.github.com/users/pipermerriam/received_events","type":"User","site_admin":false},"repo":{"id":77478378,"name":"py-evm","full_name":"pipermerriam/py-evm","owner":{"login":"pipermerriam","id":824194,"avatar_url":"https://avatars3.githubusercontent.com/u/824194?v=4","gravatar_id":"","url":"https://api.github.com/users/pipermerriam","html_url":"https://github.com/pipermerriam","followers_url":"https://api.github.com/users/pipermerriam/followers","following_url":"https://api.github.com/users/pipermerriam/following{/other_user}","gists_url":"https://api.github.com/users/pipermerriam/gists{/gist_id}","starred_url":"https://api.github.com/users/pipermerriam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pipermerriam/subscriptions","organizations_url":"https://api.github.com/users/pipermerriam/orgs","repos_url":"https://api.github.com/users/pipermerriam/repos","events_url":"https://api.github.com/users/pipermerriam/events{/privacy}","received_events_url":"https://api.github.com/users/pipermerriam/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/pipermerriam/py-evm","description":"A Python implementation of the Ethereum Virtual Machine","fork":false,"url":"https://api.github.com/repos/pipermerriam/py-evm","forks_url":"https://api.github.com/repos/pipermerriam/py-evm/forks","keys_url":"https://api.github.com/repos/pipermerriam/py-evm/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pipermerriam/py-evm/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pipermerriam/py-evm/teams","hooks_url":"https://api.github.com/repos/pipermerriam/py-evm/hooks","issue_events_url":"https://api.github.com/repos/pipermerriam/py-evm/issues/events{/number}","events_url":"https://api.github.com/repos/pipermerriam/py-evm/events","assignees_url":"https://api.github.com/repos/pipermerriam/py-evm/assignees{/user}","branches_url":"https://api.github.com/repos/pipermerriam/py-evm/branches{/branch}","tags_url":"https://api.github.com/repos/pipermerriam/py-evm/tags","blobs_url":"https://api.github.com/repos/pipermerriam/py-evm/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pipermerriam/py-evm/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pipermerriam/py-evm/git/refs{/sha}","trees_url":"https://api.github.com/repos/pipermerriam/py-evm/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pipermerriam/py-evm/statuses/{sha}","languages_url":"https://api.github.com/repos/pipermerriam/py-evm/languages","stargazers_url":"https://api.github.com/repos/pipermerriam/py-evm/stargazers","contributors_url":"https://api.github.com/repos/pipermerriam/py-evm/contributors","subscribers_url":"https://api.github.com/repos/pipermerriam/py-evm/subscribers","subscription_url":"https://api.github.com/repos/pipermerriam/py-evm/subscription","commits_url":"https://api.github.com/repos/pipermerriam/py-evm/commits{/sha}","git_commits_url":"https://api.github.com/repos/pipermerriam/py-evm/git/commits{/sha}","comments_url":"https://api.github.com/repos/pipermerriam/py-evm/comments{/number}","issue_comment_url":"https://api.github.com/repos/pipermerriam/py-evm/issues/comments{/number}","contents_url":"https://api.github.com/repos/pipermerriam/py-evm/contents/{+path}","compare_url":"https://api.github.com/repos/pipermerriam/py-evm/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pipermerriam/py-evm/merges","archive_url":"https://api.github.com/repos/pipermerriam/py-evm/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pipermerriam/py-evm/downloads","issues_url":"https://api.github.com/repos/pipermerriam/py-evm/issues{/number}","pulls_url":"https://api.github.com/repos/pipermerriam/py-evm/pulls{/number}","milestones_url":"https://api.github.com/repos/pipermerriam/py-evm/milestones{/number}","notifications_url":"https://api.github.com/repos/pipermerriam/py-evm/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pipermerriam/py-evm/labels{/name}","releases_url":"https://api.github.com/repos/pipermerriam/py-evm/releases{/id}","deployments_url":"https://api.github.com/repos/pipermerriam/py-evm/deployments","created_at":"2016-12-27T19:36:00Z","updated_at":"2017-10-29T20:53:37Z","pushed_at":"2017-10-27T17:24:08Z","git_url":"git://github.com/pipermerriam/py-evm.git","ssh_url":"git@github.com:pipermerriam/py-evm.git","clone_url":"https://github.com/pipermerriam/py-evm.git","svn_url":"https://github.com/pipermerriam/py-evm","homepage":"","size":777,"stargazers_count":71,"watchers_count":71,"language":"Python","has_issues":true,"has_projects":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":23,"mirror_url":null,"archived":false,"open_issues_count":11,"forks":23,"open_issues":11,"watchers":71,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/pipermerriam/py-evm/pulls/129"},"html":{"href":"https://github.com/pipermerriam/py-evm/pull/129"},"issue":{"href":"https://api.github.com/repos/pipermerriam/py-evm/issues/129"},"comments":{"href":"https://api.github.com/repos/pipermerriam/py-evm/issues/129/comments"},"review_comments":{"href":"https://api.github.com/repos/pipermerriam/py-evm/pulls/129/comments"},"review_comment":{"href":"https://api.github.com/repos/pipermerriam/py-evm/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/pipermerriam/py-evm/pulls/129/commits"},"statuses":{"href":"https://api.github.com/repos/pipermerriam/py-evm/statuses/014989d9b859976f2ba70f0a25aa6cac77322dc1"}},"author_association":"COLLABORATOR"}}
{"actor":{"display_login":"gsalgado"}}