{"action":"created","comment":{"url":"https://api.github.com/repos/btcsuite/btcd/pulls/comments/38878428","id":38878428,"diff_hunk":"@@ -742,16 +715,65 @@ func (b *BlockChain) getReorganizeNodes(node *blockNode) (*list.List, *list.List\n \n // connectBlock handles connecting the passed node/block to the end of the main\n // (best) chain.\n+//\n+// This function MUST be called with the chain state lock held (for writes).\n func (b *BlockChain) connectBlock(node *blockNode, block *btcutil.Block) error {\n \t// Make sure it's extending the end of the best chain.\n \tprevHash := &block.MsgBlock().Header.PrevBlock\n-\tif b.bestChain != nil && !prevHash.IsEqual(b.bestChain.hash) {\n+\tif b.bestNode != nil && !prevHash.IsEqual(b.bestNode.hash) {\n \t\treturn fmt.Errorf(\"connectBlock must be called with a block \" +\n \t\t\t\"that extends the main chain\")\n \t}\n \n-\t// Insert the block into the database which houses the main chain.\n-\t_, err := b.db.InsertBlock(block)\n+\t// Generate a new best state snapshot that will be used to update the\n+\t// database and later memory if all database updates are successful.\n+\tb.stateLock.RLock()\n+\tcurTotalTxns := b.stateSnapshot.TotalTxns\n+\tb.stateLock.RUnlock()\n+\tnumTxns := uint64(len(block.MsgBlock().Transactions))\n+\tblockSize := uint64(block.MsgBlock().SerializeSize())\n+\tstate := newBestState(node, blockSize, numTxns, curTotalTxns+numTxns)\n+\n+\t// Atomically insert info into the database.\n+\terr := b.db.Update(func(dbTx database.Tx) error {\n+\t\t// Update best block state.\n+\t\terr := dbPutBestState(dbTx, state, node.workSum)\n+\t\tif err != nil {\n+\t\t\treturn err\n+\t\t}\n+\n+\t\t// Add the block hash and height to the block index which tracks\n+\t\t// the main chain.\n+\t\terr = dbPutBlockIndex(dbTx, block.Sha(), node.height)\n+\t\tif err != nil {\n+\t\t\treturn err\n+\t\t}\n+\n+\t\t// Add transaction index entries for all of the transactions in\n+\t\t// the block being connected.\n+\t\terr = dbAddTxIndexEntries(dbTx, block)","path":"blockchain/chain.go","position":500,"original_position":500,"commit_id":"14e4f1df8a34e41901151383467cd88c0fdc1cbd","original_commit_id":"14e4f1df8a34e41901151383467cd88c0fdc1cbd","user":{"login":"Roasbeef","id":998190,"avatar_url":"https://avatars.githubusercontent.com/u/998190?v=3","gravatar_id":"","url":"https://api.github.com/users/Roasbeef","html_url":"https://github.com/Roasbeef","followers_url":"https://api.github.com/users/Roasbeef/followers","following_url":"https://api.github.com/users/Roasbeef/following{/other_user}","gists_url":"https://api.github.com/users/Roasbeef/gists{/gist_id}","starred_url":"https://api.github.com/users/Roasbeef/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Roasbeef/subscriptions","organizations_url":"https://api.github.com/users/Roasbeef/orgs","repos_url":"https://api.github.com/users/Roasbeef/repos","events_url":"https://api.github.com/users/Roasbeef/events{/privacy}","received_events_url":"https://api.github.com/users/Roasbeef/received_events","type":"User","site_admin":false},"body":"Once the pruned utxo set has been implemented this can be made optional to a degree with a `--txidx` flag. And as we discussed on IRC it would be wise to consolidate all indexing operations (`tx\\addr`) in a single batched atomic write. \r\n\r\nLeaving this comment here as a reminder. ","created_at":"2015-09-07T20:09:13Z","updated_at":"2015-09-07T20:09:13Z","html_url":"https://github.com/btcsuite/btcd/pull/491#discussion_r38878428","pull_request_url":"https://api.github.com/repos/btcsuite/btcd/pulls/491","_links":{"self":{"href":"https://api.github.com/repos/btcsuite/btcd/pulls/comments/38878428"},"html":{"href":"https://github.com/btcsuite/btcd/pull/491#discussion_r38878428"},"pull_request":{"href":"https://api.github.com/repos/btcsuite/btcd/pulls/491"}}},"pull_request":{"url":"https://api.github.com/repos/btcsuite/btcd/pulls/491","id":43364610,"html_url":"https://github.com/btcsuite/btcd/pull/491","diff_url":"https://github.com/btcsuite/btcd/pull/491.diff","patch_url":"https://github.com/btcsuite/btcd/pull/491.patch","issue_url":"https://api.github.com/repos/btcsuite/btcd/issues/491","number":491,"state":"open","locked":false,"title":"WIP: blockchain: Rework to use new db interface.","user":{"login":"davecgh","id":2115102,"avatar_url":"https://avatars.githubusercontent.com/u/2115102?v=3","gravatar_id":"","url":"https://api.github.com/users/davecgh","html_url":"https://github.com/davecgh","followers_url":"https://api.github.com/users/davecgh/followers","following_url":"https://api.github.com/users/davecgh/following{/other_user}","gists_url":"https://api.github.com/users/davecgh/gists{/gist_id}","starred_url":"https://api.github.com/users/davecgh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davecgh/subscriptions","organizations_url":"https://api.github.com/users/davecgh/orgs","repos_url":"https://api.github.com/users/davecgh/repos","events_url":"https://api.github.com/users/davecgh/events{/privacy}","received_events_url":"https://api.github.com/users/davecgh/received_events","type":"User","site_admin":false},"body":"**This pull request requires #380**\r\n\r\nThis pull request is the first stage of several that are planned to convert the blockchain package into a concurrent safe package that will ultimately allow support for multi-peer download and concurrent chain processing.  The goal is to update btcd proper after each step so it can take advantage of the enhancements as they are developed.\r\n\r\nIn addition to the aforementioned benefit, this staged approach has been chosen since it is absolutely critical to maintain consensus.  Separating the changes into several stages makes it easier for reviewers to logically follow what is happening and therefore helps prevent consensus bugs.  Naturally there are significant automated tests to help prevent consensus issues as well.\r\n\r\nThe main focus of this stage is to convert the blockchain package to use the new database interface and implement the chain-related functionality which it no longer handles.  It also aims to improve efficiency in\r\nvarious areas by making use of the new database and chain capabilities.\r\n\r\nThe following is an overview of the chain changes:\r\n\r\n- Update to use the new database interface\r\n- Add chain-related functionality that the old database used to handle\r\n  - Main chain structure and state\r\n  - Transaction spend tracking\r\n  - Transaction indexing\r\n- Replace the old TxStore type with a new UtxoStore which paves the way\r\n  towards ultimately supporting a separate pruned utxo set without\r\n  reliance on a transaction index\r\n- Make all exported functions concurrent safe\r\n  - Currently using a single chain state lock as it provides a straight forward and easy to review path forward however this can be improved with more fine grained locking\r\n- Optimize various cases where full blocks were being loaded when only\r\n  the header is needed to help reduce the I/O load\r\n- Add the ability for callers to get a snapshot of the current best chain stats in a concurrent safe fashion\r\n  - Does not block callers while new blocks are being processed\r\n- Make error messages that reference transaction outputs consistently use `<transaction hash>:<output index>`\r\n- Update tests and examples to reflect the changes\r\n\r\nThe following is an overview of the btcd changes:\r\n\r\n- Update to use the new database and chain interfaces\r\n- Temporarily remove all code related to the address index\r\n- Convert all code that uses transaction stores to use the new utxo\r\n  store\r\n- Rework several calls that required the block manager for safe\r\n  concurrency to use the chain package directly now that it is\r\n  concurrent safe\r\n- Change all calls to obtain the best hash to use the new best state\r\n  snapshot capability from the chain package\r\n- Remove workaround for limits on fetching height ranges since the new\r\n  database interface no longer imposes them\r\n- Correct the gettxout RPC handler to return the best chain hash as\r\n  opposed the hash the txout was found in\r\n- Optimize various RPC handlers:\r\n  - Change several of the RPC handlers to use the new chain snapshot\r\n    capability to avoid needlessly loading data\r\n  - Update several handlers to use new functionality to avoid accessing\r\n    the block manager so they are able to return the data without\r\n    blocking when the server is busy processing blocks\r\n  - Update non-verbose getblock to avoid deserialization and\r\n    serialization overhead\r\n  - Update getblockheader to request the block height directly from\r\n    chain and only load the header\r\n  - Update getdifficulty to use the new cached data from chain\r\n  - Update getmininginfo to use the new cached data from chain\r\n  - Update non-verbose getrawtransaction to avoid deserialization and\r\n    serialization overhead\r\n  - Update gettxout to use the new utxo store versus loading\r\n    full transactions using the transaction index\r\n\r\nThe following is an overview of the utility changes:\r\n- Update addblock to use the new database and chain interfaces\r\n- Update findcheckpoint to use the new database and chain interfaces\r\n- Remove the dropafter utility which is no longer supported\r\n\r\n**NOTE:** In order to get testing started on this code this commit is being pushed via a pull request before everything in done although it should be stable.  Before it can be merged to master, the following additional\r\nthings are needed:\r\n\r\n- [ ] Implement a proper pruned utxo set and reorg journal\r\n- [ ] Reimplement the address index using the new database interface including implementing the functionality that used to live in the old database, but will now be needed in btcd proper\r\n- [ ] Increase test coverage","created_at":"2015-08-26T05:09:23Z","updated_at":"2015-09-07T20:09:13Z","closed_at":null,"merged_at":null,"merge_commit_sha":"113497fd4ca5d37e6ad96b1d1334c2dc41c3e700","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/btcsuite/btcd/pulls/491/commits","review_comments_url":"https://api.github.com/repos/btcsuite/btcd/pulls/491/comments","review_comment_url":"https://api.github.com/repos/btcsuite/btcd/pulls/comments{/number}","comments_url":"https://api.github.com/repos/btcsuite/btcd/issues/491/comments","statuses_url":"https://api.github.com/repos/btcsuite/btcd/statuses/14e4f1df8a34e41901151383467cd88c0fdc1cbd","head":{"label":"davecgh:blockchain_database2","ref":"blockchain_database2","sha":"14e4f1df8a34e41901151383467cd88c0fdc1cbd","user":{"login":"davecgh","id":2115102,"avatar_url":"https://avatars.githubusercontent.com/u/2115102?v=3","gravatar_id":"","url":"https://api.github.com/users/davecgh","html_url":"https://github.com/davecgh","followers_url":"https://api.github.com/users/davecgh/followers","following_url":"https://api.github.com/users/davecgh/following{/other_user}","gists_url":"https://api.github.com/users/davecgh/gists{/gist_id}","starred_url":"https://api.github.com/users/davecgh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davecgh/subscriptions","organizations_url":"https://api.github.com/users/davecgh/orgs","repos_url":"https://api.github.com/users/davecgh/repos","events_url":"https://api.github.com/users/davecgh/events{/privacy}","received_events_url":"https://api.github.com/users/davecgh/received_events","type":"User","site_admin":false},"repo":{"id":16972952,"name":"btcd","full_name":"davecgh/btcd","owner":{"login":"davecgh","id":2115102,"avatar_url":"https://avatars.githubusercontent.com/u/2115102?v=3","gravatar_id":"","url":"https://api.github.com/users/davecgh","html_url":"https://github.com/davecgh","followers_url":"https://api.github.com/users/davecgh/followers","following_url":"https://api.github.com/users/davecgh/following{/other_user}","gists_url":"https://api.github.com/users/davecgh/gists{/gist_id}","starred_url":"https://api.github.com/users/davecgh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davecgh/subscriptions","organizations_url":"https://api.github.com/users/davecgh/orgs","repos_url":"https://api.github.com/users/davecgh/repos","events_url":"https://api.github.com/users/davecgh/events{/privacy}","received_events_url":"https://api.github.com/users/davecgh/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/davecgh/btcd","description":"An alternative full node bitcoin implementation written in Go (golang)","fork":true,"url":"https://api.github.com/repos/davecgh/btcd","forks_url":"https://api.github.com/repos/davecgh/btcd/forks","keys_url":"https://api.github.com/repos/davecgh/btcd/keys{/key_id}","collaborators_url":"https://api.github.com/repos/davecgh/btcd/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/davecgh/btcd/teams","hooks_url":"https://api.github.com/repos/davecgh/btcd/hooks","issue_events_url":"https://api.github.com/repos/davecgh/btcd/issues/events{/number}","events_url":"https://api.github.com/repos/davecgh/btcd/events","assignees_url":"https://api.github.com/repos/davecgh/btcd/assignees{/user}","branches_url":"https://api.github.com/repos/davecgh/btcd/branches{/branch}","tags_url":"https://api.github.com/repos/davecgh/btcd/tags","blobs_url":"https://api.github.com/repos/davecgh/btcd/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/davecgh/btcd/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/davecgh/btcd/git/refs{/sha}","trees_url":"https://api.github.com/repos/davecgh/btcd/git/trees{/sha}","statuses_url":"https://api.github.com/repos/davecgh/btcd/statuses/{sha}","languages_url":"https://api.github.com/repos/davecgh/btcd/languages","stargazers_url":"https://api.github.com/repos/davecgh/btcd/stargazers","contributors_url":"https://api.github.com/repos/davecgh/btcd/contributors","subscribers_url":"https://api.github.com/repos/davecgh/btcd/subscribers","subscription_url":"https://api.github.com/repos/davecgh/btcd/subscription","commits_url":"https://api.github.com/repos/davecgh/btcd/commits{/sha}","git_commits_url":"https://api.github.com/repos/davecgh/btcd/git/commits{/sha}","comments_url":"https://api.github.com/repos/davecgh/btcd/comments{/number}","issue_comment_url":"https://api.github.com/repos/davecgh/btcd/issues/comments{/number}","contents_url":"https://api.github.com/repos/davecgh/btcd/contents/{+path}","compare_url":"https://api.github.com/repos/davecgh/btcd/compare/{base}...{head}","merges_url":"https://api.github.com/repos/davecgh/btcd/merges","archive_url":"https://api.github.com/repos/davecgh/btcd/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/davecgh/btcd/downloads","issues_url":"https://api.github.com/repos/davecgh/btcd/issues{/number}","pulls_url":"https://api.github.com/repos/davecgh/btcd/pulls{/number}","milestones_url":"https://api.github.com/repos/davecgh/btcd/milestones{/number}","notifications_url":"https://api.github.com/repos/davecgh/btcd/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/davecgh/btcd/labels{/name}","releases_url":"https://api.github.com/repos/davecgh/btcd/releases{/id}","created_at":"2014-02-19T03:45:51Z","updated_at":"2015-04-21T20:13:13Z","pushed_at":"2015-09-07T18:13:26Z","git_url":"git://github.com/davecgh/btcd.git","ssh_url":"git@github.com:davecgh/btcd.git","clone_url":"https://github.com/davecgh/btcd.git","svn_url":"https://github.com/davecgh/btcd","homepage":null,"size":9777,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":false,"has_downloads":true,"has_wiki":true,"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":"btcsuite:master","ref":"master","sha":"0f9fc42a0618490037cc5f076c2224cb5c801011","user":{"login":"btcsuite","id":10235229,"avatar_url":"https://avatars.githubusercontent.com/u/10235229?v=3","gravatar_id":"","url":"https://api.github.com/users/btcsuite","html_url":"https://github.com/btcsuite","followers_url":"https://api.github.com/users/btcsuite/followers","following_url":"https://api.github.com/users/btcsuite/following{/other_user}","gists_url":"https://api.github.com/users/btcsuite/gists{/gist_id}","starred_url":"https://api.github.com/users/btcsuite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/btcsuite/subscriptions","organizations_url":"https://api.github.com/users/btcsuite/orgs","repos_url":"https://api.github.com/users/btcsuite/repos","events_url":"https://api.github.com/users/btcsuite/events{/privacy}","received_events_url":"https://api.github.com/users/btcsuite/received_events","type":"Organization","site_admin":false},"repo":{"id":11931032,"name":"btcd","full_name":"btcsuite/btcd","owner":{"login":"btcsuite","id":10235229,"avatar_url":"https://avatars.githubusercontent.com/u/10235229?v=3","gravatar_id":"","url":"https://api.github.com/users/btcsuite","html_url":"https://github.com/btcsuite","followers_url":"https://api.github.com/users/btcsuite/followers","following_url":"https://api.github.com/users/btcsuite/following{/other_user}","gists_url":"https://api.github.com/users/btcsuite/gists{/gist_id}","starred_url":"https://api.github.com/users/btcsuite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/btcsuite/subscriptions","organizations_url":"https://api.github.com/users/btcsuite/orgs","repos_url":"https://api.github.com/users/btcsuite/repos","events_url":"https://api.github.com/users/btcsuite/events{/privacy}","received_events_url":"https://api.github.com/users/btcsuite/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/btcsuite/btcd","description":"An alternative full node bitcoin implementation written in Go (golang)","fork":false,"url":"https://api.github.com/repos/btcsuite/btcd","forks_url":"https://api.github.com/repos/btcsuite/btcd/forks","keys_url":"https://api.github.com/repos/btcsuite/btcd/keys{/key_id}","collaborators_url":"https://api.github.com/repos/btcsuite/btcd/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/btcsuite/btcd/teams","hooks_url":"https://api.github.com/repos/btcsuite/btcd/hooks","issue_events_url":"https://api.github.com/repos/btcsuite/btcd/issues/events{/number}","events_url":"https://api.github.com/repos/btcsuite/btcd/events","assignees_url":"https://api.github.com/repos/btcsuite/btcd/assignees{/user}","branches_url":"https://api.github.com/repos/btcsuite/btcd/branches{/branch}","tags_url":"https://api.github.com/repos/btcsuite/btcd/tags","blobs_url":"https://api.github.com/repos/btcsuite/btcd/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/btcsuite/btcd/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/btcsuite/btcd/git/refs{/sha}","trees_url":"https://api.github.com/repos/btcsuite/btcd/git/trees{/sha}","statuses_url":"https://api.github.com/repos/btcsuite/btcd/statuses/{sha}","languages_url":"https://api.github.com/repos/btcsuite/btcd/languages","stargazers_url":"https://api.github.com/repos/btcsuite/btcd/stargazers","contributors_url":"https://api.github.com/repos/btcsuite/btcd/contributors","subscribers_url":"https://api.github.com/repos/btcsuite/btcd/subscribers","subscription_url":"https://api.github.com/repos/btcsuite/btcd/subscription","commits_url":"https://api.github.com/repos/btcsuite/btcd/commits{/sha}","git_commits_url":"https://api.github.com/repos/btcsuite/btcd/git/commits{/sha}","comments_url":"https://api.github.com/repos/btcsuite/btcd/comments{/number}","issue_comment_url":"https://api.github.com/repos/btcsuite/btcd/issues/comments{/number}","contents_url":"https://api.github.com/repos/btcsuite/btcd/contents/{+path}","compare_url":"https://api.github.com/repos/btcsuite/btcd/compare/{base}...{head}","merges_url":"https://api.github.com/repos/btcsuite/btcd/merges","archive_url":"https://api.github.com/repos/btcsuite/btcd/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/btcsuite/btcd/downloads","issues_url":"https://api.github.com/repos/btcsuite/btcd/issues{/number}","pulls_url":"https://api.github.com/repos/btcsuite/btcd/pulls{/number}","milestones_url":"https://api.github.com/repos/btcsuite/btcd/milestones{/number}","notifications_url":"https://api.github.com/repos/btcsuite/btcd/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/btcsuite/btcd/labels{/name}","releases_url":"https://api.github.com/repos/btcsuite/btcd/releases{/id}","created_at":"2013-08-06T18:10:52Z","updated_at":"2015-09-06T18:15:32Z","pushed_at":"2015-09-07T18:13:27Z","git_url":"git://github.com/btcsuite/btcd.git","ssh_url":"git@github.com:btcsuite/btcd.git","clone_url":"https://github.com/btcsuite/btcd.git","svn_url":"https://github.com/btcsuite/btcd","homepage":"https://github.com/btcsuite/btcd/blob/master/docs/README.md","size":19881,"stargazers_count":669,"watchers_count":669,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":139,"mirror_url":null,"open_issues_count":51,"forks":139,"open_issues":51,"watchers":669,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/btcsuite/btcd/pulls/491"},"html":{"href":"https://github.com/btcsuite/btcd/pull/491"},"issue":{"href":"https://api.github.com/repos/btcsuite/btcd/issues/491"},"comments":{"href":"https://api.github.com/repos/btcsuite/btcd/issues/491/comments"},"review_comments":{"href":"https://api.github.com/repos/btcsuite/btcd/pulls/491/comments"},"review_comment":{"href":"https://api.github.com/repos/btcsuite/btcd/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/btcsuite/btcd/pulls/491/commits"},"statuses":{"href":"https://api.github.com/repos/btcsuite/btcd/statuses/14e4f1df8a34e41901151383467cd88c0fdc1cbd"}}}}