{"action":"created","comment":{"url":"https://api.github.com/repos/NebulousLabs/Sia/pulls/comments/25296750","id":25296750,"diff_hunk":"@@ -1,42 +1,153 @@\n package hostdb\n \n import (\n+\t\"crypto/rand\"\n+\t\"errors\"\n+\t\"math/big\"\n+\n \t\"github.com/NebulousLabs/Sia/consensus\"\n \t\"github.com/NebulousLabs/Sia/modules\"\n+\t\"github.com/NebulousLabs/Sia/network\"\n )\n \n-// TODO: Add a bunch of different ways to arrive at weight, which can each be\n-// chosen according to the need at hand. This might also require having a bunch\n-// of different weights at each node in the tree.\n-\n-// entryWeight determines the weight of a specific host, which is:\n-//\n-//\t\tFreeze * Collateral / sqrt(Price).\n-//\n-// Freeze has to be linear, because any non-linear freeze will invite sybil\n-// attacks.\n-//\n-// For now, collateral is also linear because an increased collateral means\n-// increased risk for the host. (Freeze on the other hand has no risk.) It\n-// might be better to make collateral grow sublinearly, such as taking\n-// sqrt(collateral) or collateral^(4/5).\n-//\n-// We take the square of the price to heavily emphasize hosts that have a low\n-// price. This is also a bit simplistic however, because we're not sure what\n-// the host might be charging for bandwidth.\n-func entryWeight(entry modules.HostEntry) consensus.Currency {\n-\t// Catch a divide by 0 error, and let all hosts have at least some weight.\n-\t//\n-\t// TODO: Perhaps there's a better way to do this.\n-\tif entry.Price.Sign() <= 0 {\n-\t\tentry.Price = consensus.NewCurrency64(1)\n-\t}\n-\tif entry.Collateral.Sign() <= 0 {\n-\t\tentry.Collateral = consensus.NewCurrency64(1)\n-\t}\n-\tif entry.Freeze.Sign() <= 0 {\n-\t\tentry.Freeze = consensus.NewCurrency64(1)\n-\t}\n-\n-\treturn entry.Freeze.Mul(entry.Collateral).Div(entry.Price.Sqrt())\n+// entryWeight returns the weight of a host entry according to the internal\n+// metrics of the HostDB. Currently, that means using the weight (10^29 *\n+// entry.Collateral / (entry.Price)^2), where entry.Collateral is adjusted to\n+// be at most twice the price and at least half the price.\n+func entryWeight(entry modules.HostEntry) (weight consensus.Currency) {\n+\t// Find the collateral by enforcing a min and max collateral. The minimum collateral\n+\tmaxCollateral := entry.Price.Mul(consensus.NewCurrency64(2))\n+\tminCollateral := entry.Price.Div(consensus.NewCurrency64(2))\n+\tvar collateral consensus.Currency\n+\tif maxCollateral.Cmp(entry.Collateral) < 0 {\n+\t\tcollateral = maxCollateral\n+\t} else if minCollateral.Cmp(entry.Collateral) > 0 {\n+\t\tcollateral = minCollateral\n+\t} else {\n+\t\tcollateral = entry.Collateral\n+\t}\n+\n+\t// Create a baseline weight of 10^29, which adds precision to the equation\n+\t// and makes sure that all reasonable prices end with a weight that's\n+\t// greater than zero.\n+\tweight = consensus.NewCurrency(new(big.Int).Exp(big.NewInt(10), big.NewInt(29), nil))\n+\tweight = weight.Mul(collateral).Div(entry.Price).Div(entry.Price)","path":"modules/hostdb/entries.go","position":68,"original_position":68,"commit_id":"508d62c20f5c416e92cb5afc634ca0b25053b18b","original_commit_id":"508d62c20f5c416e92cb5afc634ca0b25053b18b","user":{"login":"lukechampine","id":3344749,"avatar_url":"https://avatars.githubusercontent.com/u/3344749?v=3","gravatar_id":"","url":"https://api.github.com/users/lukechampine","html_url":"https://github.com/lukechampine","followers_url":"https://api.github.com/users/lukechampine/followers","following_url":"https://api.github.com/users/lukechampine/following{/other_user}","gists_url":"https://api.github.com/users/lukechampine/gists{/gist_id}","starred_url":"https://api.github.com/users/lukechampine/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lukechampine/subscriptions","organizations_url":"https://api.github.com/users/lukechampine/orgs","repos_url":"https://api.github.com/users/lukechampine/repos","events_url":"https://api.github.com/users/lukechampine/events{/privacy}","received_events_url":"https://api.github.com/users/lukechampine/received_events","type":"User","site_admin":false},"body":"10^29 is a lot smaller than 2^100.\r\nDividing twice is pretty ugly too, I'd rather just add an `Exp` method to `Currency`.","created_at":"2015-02-24T21:54:22Z","updated_at":"2015-02-24T21:54:22Z","html_url":"https://github.com/NebulousLabs/Sia/pull/358#discussion_r25296750","pull_request_url":"https://api.github.com/repos/NebulousLabs/Sia/pulls/358","_links":{"self":{"href":"https://api.github.com/repos/NebulousLabs/Sia/pulls/comments/25296750"},"html":{"href":"https://github.com/NebulousLabs/Sia/pull/358#discussion_r25296750"},"pull_request":{"href":"https://api.github.com/repos/NebulousLabs/Sia/pulls/358"}}},"pull_request":{"url":"https://api.github.com/repos/NebulousLabs/Sia/pulls/358","id":29897909,"html_url":"https://github.com/NebulousLabs/Sia/pull/358","diff_url":"https://github.com/NebulousLabs/Sia/pull/358.diff","patch_url":"https://github.com/NebulousLabs/Sia/pull/358.patch","issue_url":"https://api.github.com/repos/NebulousLabs/Sia/issues/358","number":358,"state":"open","locked":false,"title":"Add HostDB Tests","user":{"login":"DavidVorick","id":2316841,"avatar_url":"https://avatars.githubusercontent.com/u/2316841?v=3","gravatar_id":"","url":"https://api.github.com/users/DavidVorick","html_url":"https://github.com/DavidVorick","followers_url":"https://api.github.com/users/DavidVorick/followers","following_url":"https://api.github.com/users/DavidVorick/following{/other_user}","gists_url":"https://api.github.com/users/DavidVorick/gists{/gist_id}","starred_url":"https://api.github.com/users/DavidVorick/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DavidVorick/subscriptions","organizations_url":"https://api.github.com/users/DavidVorick/orgs","repos_url":"https://api.github.com/users/DavidVorick/repos","events_url":"https://api.github.com/users/DavidVorick/events{/privacy}","received_events_url":"https://api.github.com/users/DavidVorick/received_events","type":"User","site_admin":false},"body":"As usually happens, writing tests for the hostdb helped me to reason about a lot of things that it was doing, so there are a bunch of changes.\r\n\r\nI also moved some things around in the network package to increase the organization. I renamed network.go to tcpserver.go, and I moved the address functions to their own address.go file. None of the functions or types were changed, just which files they are in.\r\n\r\nEntry weight no longer weights according to price. This is of course a pretty big problem. I couldn't think of a good way to write the equation. I think I'd want the weight to increase linearly as the collateral increases, and I'd want the weight to decrease with the square of the price.\r\n\r\nThe problem is that we're stuck with integer weights, so you'll only have positive weights in that scheme if the collateral is equal to the square of the price, which is unreasonable. We should probably also enforce a max and min on the collateral. Max of 2x price, min of 1/2 price. The price would then be the dominating factor.","created_at":"2015-02-24T13:44:40Z","updated_at":"2015-02-24T21:54:22Z","closed_at":null,"merged_at":null,"merge_commit_sha":"618cfcffb15b1d00e78f42b4e8ed06a85b404074","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/NebulousLabs/Sia/pulls/358/commits","review_comments_url":"https://api.github.com/repos/NebulousLabs/Sia/pulls/358/comments","review_comment_url":"https://api.github.com/repos/NebulousLabs/Sia/pulls/comments{/number}","comments_url":"https://api.github.com/repos/NebulousLabs/Sia/issues/358/comments","statuses_url":"https://api.github.com/repos/NebulousLabs/Sia/statuses/508d62c20f5c416e92cb5afc634ca0b25053b18b","head":{"label":"NebulousLabs:renter","ref":"renter","sha":"508d62c20f5c416e92cb5afc634ca0b25053b18b","user":{"login":"NebulousLabs","id":7471422,"avatar_url":"https://avatars.githubusercontent.com/u/7471422?v=3","gravatar_id":"","url":"https://api.github.com/users/NebulousLabs","html_url":"https://github.com/NebulousLabs","followers_url":"https://api.github.com/users/NebulousLabs/followers","following_url":"https://api.github.com/users/NebulousLabs/following{/other_user}","gists_url":"https://api.github.com/users/NebulousLabs/gists{/gist_id}","starred_url":"https://api.github.com/users/NebulousLabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NebulousLabs/subscriptions","organizations_url":"https://api.github.com/users/NebulousLabs/orgs","repos_url":"https://api.github.com/users/NebulousLabs/repos","events_url":"https://api.github.com/users/NebulousLabs/events{/privacy}","received_events_url":"https://api.github.com/users/NebulousLabs/received_events","type":"Organization","site_admin":false},"repo":{"id":25370072,"name":"Sia","full_name":"NebulousLabs/Sia","owner":{"login":"NebulousLabs","id":7471422,"avatar_url":"https://avatars.githubusercontent.com/u/7471422?v=3","gravatar_id":"","url":"https://api.github.com/users/NebulousLabs","html_url":"https://github.com/NebulousLabs","followers_url":"https://api.github.com/users/NebulousLabs/followers","following_url":"https://api.github.com/users/NebulousLabs/following{/other_user}","gists_url":"https://api.github.com/users/NebulousLabs/gists{/gist_id}","starred_url":"https://api.github.com/users/NebulousLabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NebulousLabs/subscriptions","organizations_url":"https://api.github.com/users/NebulousLabs/orgs","repos_url":"https://api.github.com/users/NebulousLabs/repos","events_url":"https://api.github.com/users/NebulousLabs/events{/privacy}","received_events_url":"https://api.github.com/users/NebulousLabs/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/NebulousLabs/Sia","description":"Blockchain-based marketplace for file storage","fork":false,"url":"https://api.github.com/repos/NebulousLabs/Sia","forks_url":"https://api.github.com/repos/NebulousLabs/Sia/forks","keys_url":"https://api.github.com/repos/NebulousLabs/Sia/keys{/key_id}","collaborators_url":"https://api.github.com/repos/NebulousLabs/Sia/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/NebulousLabs/Sia/teams","hooks_url":"https://api.github.com/repos/NebulousLabs/Sia/hooks","issue_events_url":"https://api.github.com/repos/NebulousLabs/Sia/issues/events{/number}","events_url":"https://api.github.com/repos/NebulousLabs/Sia/events","assignees_url":"https://api.github.com/repos/NebulousLabs/Sia/assignees{/user}","branches_url":"https://api.github.com/repos/NebulousLabs/Sia/branches{/branch}","tags_url":"https://api.github.com/repos/NebulousLabs/Sia/tags","blobs_url":"https://api.github.com/repos/NebulousLabs/Sia/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/NebulousLabs/Sia/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/NebulousLabs/Sia/git/refs{/sha}","trees_url":"https://api.github.com/repos/NebulousLabs/Sia/git/trees{/sha}","statuses_url":"https://api.github.com/repos/NebulousLabs/Sia/statuses/{sha}","languages_url":"https://api.github.com/repos/NebulousLabs/Sia/languages","stargazers_url":"https://api.github.com/repos/NebulousLabs/Sia/stargazers","contributors_url":"https://api.github.com/repos/NebulousLabs/Sia/contributors","subscribers_url":"https://api.github.com/repos/NebulousLabs/Sia/subscribers","subscription_url":"https://api.github.com/repos/NebulousLabs/Sia/subscription","commits_url":"https://api.github.com/repos/NebulousLabs/Sia/commits{/sha}","git_commits_url":"https://api.github.com/repos/NebulousLabs/Sia/git/commits{/sha}","comments_url":"https://api.github.com/repos/NebulousLabs/Sia/comments{/number}","issue_comment_url":"https://api.github.com/repos/NebulousLabs/Sia/issues/comments{/number}","contents_url":"https://api.github.com/repos/NebulousLabs/Sia/contents/{+path}","compare_url":"https://api.github.com/repos/NebulousLabs/Sia/compare/{base}...{head}","merges_url":"https://api.github.com/repos/NebulousLabs/Sia/merges","archive_url":"https://api.github.com/repos/NebulousLabs/Sia/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/NebulousLabs/Sia/downloads","issues_url":"https://api.github.com/repos/NebulousLabs/Sia/issues{/number}","pulls_url":"https://api.github.com/repos/NebulousLabs/Sia/pulls{/number}","milestones_url":"https://api.github.com/repos/NebulousLabs/Sia/milestones{/number}","notifications_url":"https://api.github.com/repos/NebulousLabs/Sia/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/NebulousLabs/Sia/labels{/name}","releases_url":"https://api.github.com/repos/NebulousLabs/Sia/releases{/id}","created_at":"2014-10-17T17:50:25Z","updated_at":"2015-02-24T07:57:54Z","pushed_at":"2015-02-24T21:51:22Z","git_url":"git://github.com/NebulousLabs/Sia.git","ssh_url":"git@github.com:NebulousLabs/Sia.git","clone_url":"https://github.com/NebulousLabs/Sia.git","svn_url":"https://github.com/NebulousLabs/Sia","homepage":"http://siacoin.com","size":7967,"stargazers_count":8,"watchers_count":8,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1,"mirror_url":null,"open_issues_count":35,"forks":1,"open_issues":35,"watchers":8,"default_branch":"master"}},"base":{"label":"NebulousLabs:master","ref":"master","sha":"712072fc4793c431a83b065cdadc94a186bdd5f8","user":{"login":"NebulousLabs","id":7471422,"avatar_url":"https://avatars.githubusercontent.com/u/7471422?v=3","gravatar_id":"","url":"https://api.github.com/users/NebulousLabs","html_url":"https://github.com/NebulousLabs","followers_url":"https://api.github.com/users/NebulousLabs/followers","following_url":"https://api.github.com/users/NebulousLabs/following{/other_user}","gists_url":"https://api.github.com/users/NebulousLabs/gists{/gist_id}","starred_url":"https://api.github.com/users/NebulousLabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NebulousLabs/subscriptions","organizations_url":"https://api.github.com/users/NebulousLabs/orgs","repos_url":"https://api.github.com/users/NebulousLabs/repos","events_url":"https://api.github.com/users/NebulousLabs/events{/privacy}","received_events_url":"https://api.github.com/users/NebulousLabs/received_events","type":"Organization","site_admin":false},"repo":{"id":25370072,"name":"Sia","full_name":"NebulousLabs/Sia","owner":{"login":"NebulousLabs","id":7471422,"avatar_url":"https://avatars.githubusercontent.com/u/7471422?v=3","gravatar_id":"","url":"https://api.github.com/users/NebulousLabs","html_url":"https://github.com/NebulousLabs","followers_url":"https://api.github.com/users/NebulousLabs/followers","following_url":"https://api.github.com/users/NebulousLabs/following{/other_user}","gists_url":"https://api.github.com/users/NebulousLabs/gists{/gist_id}","starred_url":"https://api.github.com/users/NebulousLabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NebulousLabs/subscriptions","organizations_url":"https://api.github.com/users/NebulousLabs/orgs","repos_url":"https://api.github.com/users/NebulousLabs/repos","events_url":"https://api.github.com/users/NebulousLabs/events{/privacy}","received_events_url":"https://api.github.com/users/NebulousLabs/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/NebulousLabs/Sia","description":"Blockchain-based marketplace for file storage","fork":false,"url":"https://api.github.com/repos/NebulousLabs/Sia","forks_url":"https://api.github.com/repos/NebulousLabs/Sia/forks","keys_url":"https://api.github.com/repos/NebulousLabs/Sia/keys{/key_id}","collaborators_url":"https://api.github.com/repos/NebulousLabs/Sia/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/NebulousLabs/Sia/teams","hooks_url":"https://api.github.com/repos/NebulousLabs/Sia/hooks","issue_events_url":"https://api.github.com/repos/NebulousLabs/Sia/issues/events{/number}","events_url":"https://api.github.com/repos/NebulousLabs/Sia/events","assignees_url":"https://api.github.com/repos/NebulousLabs/Sia/assignees{/user}","branches_url":"https://api.github.com/repos/NebulousLabs/Sia/branches{/branch}","tags_url":"https://api.github.com/repos/NebulousLabs/Sia/tags","blobs_url":"https://api.github.com/repos/NebulousLabs/Sia/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/NebulousLabs/Sia/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/NebulousLabs/Sia/git/refs{/sha}","trees_url":"https://api.github.com/repos/NebulousLabs/Sia/git/trees{/sha}","statuses_url":"https://api.github.com/repos/NebulousLabs/Sia/statuses/{sha}","languages_url":"https://api.github.com/repos/NebulousLabs/Sia/languages","stargazers_url":"https://api.github.com/repos/NebulousLabs/Sia/stargazers","contributors_url":"https://api.github.com/repos/NebulousLabs/Sia/contributors","subscribers_url":"https://api.github.com/repos/NebulousLabs/Sia/subscribers","subscription_url":"https://api.github.com/repos/NebulousLabs/Sia/subscription","commits_url":"https://api.github.com/repos/NebulousLabs/Sia/commits{/sha}","git_commits_url":"https://api.github.com/repos/NebulousLabs/Sia/git/commits{/sha}","comments_url":"https://api.github.com/repos/NebulousLabs/Sia/comments{/number}","issue_comment_url":"https://api.github.com/repos/NebulousLabs/Sia/issues/comments{/number}","contents_url":"https://api.github.com/repos/NebulousLabs/Sia/contents/{+path}","compare_url":"https://api.github.com/repos/NebulousLabs/Sia/compare/{base}...{head}","merges_url":"https://api.github.com/repos/NebulousLabs/Sia/merges","archive_url":"https://api.github.com/repos/NebulousLabs/Sia/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/NebulousLabs/Sia/downloads","issues_url":"https://api.github.com/repos/NebulousLabs/Sia/issues{/number}","pulls_url":"https://api.github.com/repos/NebulousLabs/Sia/pulls{/number}","milestones_url":"https://api.github.com/repos/NebulousLabs/Sia/milestones{/number}","notifications_url":"https://api.github.com/repos/NebulousLabs/Sia/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/NebulousLabs/Sia/labels{/name}","releases_url":"https://api.github.com/repos/NebulousLabs/Sia/releases{/id}","created_at":"2014-10-17T17:50:25Z","updated_at":"2015-02-24T07:57:54Z","pushed_at":"2015-02-24T21:51:22Z","git_url":"git://github.com/NebulousLabs/Sia.git","ssh_url":"git@github.com:NebulousLabs/Sia.git","clone_url":"https://github.com/NebulousLabs/Sia.git","svn_url":"https://github.com/NebulousLabs/Sia","homepage":"http://siacoin.com","size":7967,"stargazers_count":8,"watchers_count":8,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1,"mirror_url":null,"open_issues_count":35,"forks":1,"open_issues":35,"watchers":8,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/NebulousLabs/Sia/pulls/358"},"html":{"href":"https://github.com/NebulousLabs/Sia/pull/358"},"issue":{"href":"https://api.github.com/repos/NebulousLabs/Sia/issues/358"},"comments":{"href":"https://api.github.com/repos/NebulousLabs/Sia/issues/358/comments"},"review_comments":{"href":"https://api.github.com/repos/NebulousLabs/Sia/pulls/358/comments"},"review_comment":{"href":"https://api.github.com/repos/NebulousLabs/Sia/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/NebulousLabs/Sia/pulls/358/commits"},"statuses":{"href":"https://api.github.com/repos/NebulousLabs/Sia/statuses/508d62c20f5c416e92cb5afc634ca0b25053b18b"}}}}