Permalink
Browse files

Merge branch 'master' into renter-pools

  • Loading branch information...
2 parents 9f94d2d + 558fa8c commit 3decc2df97f6fcc17eb3ffa8d16846015f978a31 @DavidVorick DavidVorick committed Dec 8, 2016
Showing with 269 additions and 328 deletions.
  1. +22 −1 build/var.go
  2. +64 −0 build/var_test.go
  3. +23 −48 modules/consensus/synchronize.go
  4. +80 −87 modules/gateway.go
  5. +75 −180 modules/gateway/consts.go
  6. +5 −12 modules/negotiate.go
View
@@ -1,19 +1,40 @@
package build
+import "reflect"
+
// A Var represents a variable whose value depends on which Release is being
// compiled. None of the fields may be nil, and all fields must have the same
-// underlying type.
+// type.
type Var struct {
Standard interface{}
Dev interface{}
Testing interface{}
+ // prevent unkeyed literals
+ _ struct{}
}
// Select returns the field of v that corresponds to the current Release.
+//
+// Since the caller typically makes a type assertion on the result, it is
+// important to point out that type assertions are stricter than conversions.
+// Specifically, you cannot write:
+//
+// type myint int
+// Select(Var{0, 0, 0}).(myint)
+//
+// Because 0 will be interpreted as an int, which is not assignable to myint.
+// Instead, you must explicitly cast each field in the Var, or cast the return
+// value of Select after the type assertion. The former is preferred.
func Select(v Var) interface{} {
if v.Standard == nil || v.Dev == nil || v.Testing == nil {
panic("nil value in build variable")
}
+ st, dt, tt := reflect.TypeOf(v.Standard), reflect.TypeOf(v.Dev), reflect.TypeOf(v.Testing)
+ if !dt.AssignableTo(st) || !tt.AssignableTo(st) {
+ // NOTE: we use AssignableTo instead of the more lenient ConvertibleTo
+ // because type assertions require the former.
+ panic("build variables must have a single type")
+ }
switch Release {
case "standard":
return v.Standard
View
@@ -0,0 +1,64 @@
+package build
+
+import "testing"
+
+// didPanic returns true if fn panicked.
+func didPanic(fn func()) (p bool) {
+ defer func() {
+ p = (recover() != nil)
+ }()
+ fn()
+ return
+}
+
+// TestSelect tests the Select function. Since we can't change the Release
+// constant during testing, we can only test the "testing" branches.
+func TestSelect(t *testing.T) {
+ var v Var
+ if !didPanic(func() { Select(v) }) {
+ t.Error("Select should panic with all nil fields")
+ }
+
+ v.Standard = 0
+ if !didPanic(func() { Select(v) }) {
+ t.Error("Select should panic with some nil fields")
+ }
+
+ v = Var{
+ Standard: 0,
+ Dev: 0,
+ Testing: 0,
+ }
+ if didPanic(func() { Select(v) }) {
+ t.Error("Select should not panic with valid fields")
+ }
+
+ if !didPanic(func() { _ = Select(v).(string) }) {
+ t.Error("improper type assertion should panic")
+ }
+ // should fail even if types are convertible
+ type myint int
+ if !didPanic(func() { _ = Select(v).(myint) }) {
+ t.Error("improper type assertion should panic")
+ }
+
+ v.Standard = "foo"
+ if !didPanic(func() { Select(v) }) {
+ t.Error("Select should panic if field types do not match")
+ }
+
+ // Even though myint is convertible to int, it is not *assignable*. That
+ // means that this code will panic, as checked in a previous test:
+ //
+ // _ = Select(v).(myint)
+ //
+ // This is important because users of Select may assume that type
+ // assertions only require convertibility. To guard against this, we
+ // enforce that all Var fields must be assignable to each other; otherwise
+ // a type assertion may succeed for certain Release constants and fail for
+ // others.
+ v.Standard = myint(0)
+ if !didPanic(func() { Select(v) }) {
+ t.Error("Select should panic if field types are not mutually assignable")
+ }
+}
@@ -25,63 +25,38 @@ var (
// MaxCatchUpBlocks is the maxiumum number of blocks that can be given to
// the consensus set in a single iteration during the initial blockchain
// download.
- MaxCatchUpBlocks = func() types.BlockHeight {
- switch build.Release {
- case "dev":
- return 50
- case "standard":
- return 10
- case "testing":
- return 3
- default:
- panic("unrecognized build.Release")
- }
- }()
+ MaxCatchUpBlocks = build.Select(build.Var{
+ Standard: types.BlockHeight(10),
+ Dev: types.BlockHeight(50),
+ Testing: types.BlockHeight(3),
+ }).(types.BlockHeight)
+
// sendBlocksTimeout is the timeout for the SendBlocks RPC.
- sendBlocksTimeout = func() time.Duration {
- switch build.Release {
- case "dev":
- return 40 * time.Second
- case "standard":
- return 5 * time.Minute
- case "testing":
- return 5 * time.Second
- default:
- panic("unrecognized build.Release")
- }
- }()
+ sendBlocksTimeout = build.Select(build.Var{
+ Standard: 5 * time.Minute,
+ Dev: 40 * time.Second,
+ Testing: 5 * time.Second,
+ }).(time.Duration)
+
// minIBDWaitTime is the time threadedInitialBlockchainDownload waits before
// exiting if there are >= 1 and <= minNumOutbound peers synced. This timeout
// will primarily affect miners who have multiple nodes daisy chained off each
// other. Those nodes will likely have to wait minIBDWaitTime on every startup
// before IBD is done.
- minIBDWaitTime = func() time.Duration {
- switch build.Release {
- case "dev":
- return 80 * time.Second
- case "standard":
- return 90 * time.Minute
- case "testing":
- return 10 * time.Second
- default:
- panic("unrecognized build.Release")
- }
- }()
+ minIBDWaitTime = build.Select(build.Var{
+ Standard: 90 * time.Minute,
+ Dev: 80 * time.Second,
+ Testing: 10 * time.Second,
+ }).(time.Duration)
+
// ibdLoopDelay is the time that threadedInitialBlockchainDownload waits
// between attempts to synchronize with the network if the last attempt
// failed.
- ibdLoopDelay = func() time.Duration {
- switch build.Release {
- case "dev":
- return 1 * time.Second
- case "standard":
- return 10 * time.Second
- case "testing":
- return 100 * time.Millisecond
- default:
- panic("unrecognized build.Release")
- }
- }()
+ ibdLoopDelay = build.Select(build.Var{
+ Standard: 10 * time.Second,
+ Dev: 1 * time.Second,
+ Testing: 100 * time.Millisecond,
+ }).(time.Duration)
errEarlyStop = errors.New("initial blockchain download did not complete by the time shutdown was issued")
errSendBlocksStalled = errors.New("SendBlocks RPC timed and never received any blocks")
View
@@ -19,93 +19,86 @@ var (
// bootstrap point. While the bootstrap point could be a central service,
// it can also be a list of peers that are known to be stable. We have
// chosen to hardcode known-stable peers.
- BootstrapPeers = func() []NetAddress {
- switch build.Release {
- case "dev":
- return nil
- case "standard":
- return []NetAddress{
- "101.200.214.115:9981",
- "104.223.98.174:9981",
- "109.172.42.157:9981",
- "109.206.33.225:9981",
- "109.71.42.163:9981",
- "109.71.42.164:9981",
- "113.98.98.164:9981",
- "115.187.229.102:9981",
- "120.25.198.251:9981",
- "138.201.13.159:9981",
- "139.162.152.204:9981",
- "141.105.11.33:9981",
- "142.4.209.72:9981",
- "148.251.221.163:9981",
- "158.69.120.71:9981",
- "162.210.249.170:9981",
- "162.222.23.93:9981",
- "176.9.59.110:9981",
- "176.9.72.2:9981",
- "180.167.17.236:9981",
- "18.239.0.53:9981",
- "183.86.218.232:9981",
- "188.166.61.155:9981",
- "188.166.61.157:9981",
- "188.166.61.158:9981",
- "188.166.61.159:9981",
- "188.166.61.163:9981",
- "188.61.177.92:9981",
- "190.10.8.173:9981",
- "193.198.102.34:9981",
- "194.135.90.38:9981",
- "195.154.243.233:9981",
- "202.63.55.79:9981",
- "210.14.155.90:9981",
- "213.251.158.199:9981",
- "217.65.8.75:9981",
- "222.187.224.89:9981",
- "222.187.224.93:9981",
- "23.239.14.98:9971",
- "23.239.14.98:9981",
- "23.239.14.98:9981",
- "24.91.0.62:9981",
- "31.178.227.21:9981",
- "37.139.1.78:9981",
- "37.139.28.207:9981",
- "43.227.113.131:9981",
- "45.79.132.35:9981",
- "45.79.159.167:9981",
- "46.105.118.15:9981",
- "52.205.238.6:9981",
- "62.107.201.170:9981",
- "62.210.207.79:9981",
- "62.212.77.12:9981",
- "64.31.31.106:9981",
- "68.55.10.144:9981",
- "73.73.50.191:33721",
- "76.164.234.13:9981",
- "78.119.218.13:9981",
- "79.172.204.10:9981",
- "79.51.183.132:9981",
- "80.234.37.94:9981",
- "82.196.11.170:9981",
- "82.196.5.50:9981",
- "82.220.99.82:9981",
- "83.76.19.197:10981",
- "85.255.197.69:9981",
- "87.236.27.155:12487",
- "87.98.216.46:9981",
- "88.109.8.173:9981",
- "91.121.183.171:9981",
- "95.211.203.138:9981",
- "95.85.14.54:9981",
- "95.85.15.69:9981",
- "95.85.15.71:9981",
- }
- case "testing":
- return nil
- default:
- panic("unrecognized build.Release constant in BootstrapPeers")
- }
- }()
+ BootstrapPeers = build.Select(build.Var{
+ Standard: []NetAddress{
+ "101.200.214.115:9981",
+ "104.223.98.174:9981",
+ "109.172.42.157:9981",
+ "109.206.33.225:9981",
+ "109.71.42.163:9981",
+ "109.71.42.164:9981",
+ "113.98.98.164:9981",
+ "115.187.229.102:9981",
+ "120.25.198.251:9981",
+ "138.201.13.159:9981",
+ "139.162.152.204:9981",
+ "141.105.11.33:9981",
+ "142.4.209.72:9981",
+ "148.251.221.163:9981",
+ "158.69.120.71:9981",
+ "162.210.249.170:9981",
+ "162.222.23.93:9981",
+ "176.9.59.110:9981",
+ "176.9.72.2:9981",
+ "180.167.17.236:9981",
+ "18.239.0.53:9981",
+ "183.86.218.232:9981",
+ "188.166.61.155:9981",
+ "188.166.61.157:9981",
+ "188.166.61.158:9981",
+ "188.166.61.159:9981",
+ "188.166.61.163:9981",
+ "188.61.177.92:9981",
+ "190.10.8.173:9981",
+ "193.198.102.34:9981",
+ "194.135.90.38:9981",
+ "195.154.243.233:9981",
+ "202.63.55.79:9981",
+ "210.14.155.90:9981",
+ "213.251.158.199:9981",
+ "217.65.8.75:9981",
+ "222.187.224.89:9981",
+ "222.187.224.93:9981",
+ "23.239.14.98:9971",
+ "23.239.14.98:9981",
+ "23.239.14.98:9981",
+ "24.91.0.62:9981",
+ "31.178.227.21:9981",
+ "37.139.1.78:9981",
+ "37.139.28.207:9981",
+ "43.227.113.131:9981",
+ "45.79.132.35:9981",
+ "45.79.159.167:9981",
+ "46.105.118.15:9981",
+ "52.205.238.6:9981",
+ "62.107.201.170:9981",
+ "62.210.207.79:9981",
+ "62.212.77.12:9981",
+ "64.31.31.106:9981",
+ "68.55.10.144:9981",
+ "73.73.50.191:33721",
+ "76.164.234.13:9981",
+ "78.119.218.13:9981",
+ "79.172.204.10:9981",
+ "79.51.183.132:9981",
+ "80.234.37.94:9981",
+ "82.196.11.170:9981",
+ "82.196.5.50:9981",
+ "82.220.99.82:9981",
+ "83.76.19.197:10981",
+ "85.255.197.69:9981",
+ "87.236.27.155:12487",
+ "87.98.216.46:9981",
+ "88.109.8.173:9981",
+ "91.121.183.171:9981",
+ "95.211.203.138:9981",
+ "95.85.14.54:9981",
+ "95.85.15.69:9981",
+ "95.85.15.71:9981",
+ },
+ Dev: []NetAddress(nil),
+ Testing: []NetAddress(nil),
+ }).([]NetAddress)
)
type (
Oops, something went wrong.

0 comments on commit 3decc2d

Please sign in to comment.