From 1a0e7452f3e7367b30d3d62cab23dcd4e0bb6d8c Mon Sep 17 00:00:00 2001 From: David Hill Date: Thu, 5 May 2016 15:16:42 -0400 Subject: [PATCH 1/4] btcd: handle signal SIGTERM (#688) When an OS reboots or shuts down, it sends all processes SIGTERM before sending SIGKILL. This allows btcd to do a proper shutdown which most importantly closes the database. --- signal.go | 15 ++++++++++----- signalsigterm.go | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 signalsigterm.go diff --git a/signal.go b/signal.go index 0c527b27..64cb900e 100644 --- a/signal.go +++ b/signal.go @@ -16,6 +16,10 @@ var interruptChannel chan os.Signal // to be invoked on SIGINT (Ctrl+C) signals. var addHandlerChannel = make(chan func()) +// signals defines the default signals to catch in order to do a proper +// shutdown. +var signals = []os.Signal{os.Interrupt} + // mainInterruptHandler listens for SIGINT (Ctrl+C) signals on the // interruptChannel and invokes the registered interruptCallbacks accordingly. // It also listens for callback registration. It must be run as a goroutine. @@ -32,16 +36,17 @@ func mainInterruptHandler() { for { select { - case <-interruptChannel: + case sig := <-interruptChannel: // Ignore more than one shutdown signal. if isShutdown { - btcdLog.Infof("Received SIGINT (Ctrl+C). " + - "Already shutting down...") + btcdLog.Infof("Received signal (%s). "+ + "Already shutting down...", sig) continue } isShutdown = true - btcdLog.Infof("Received SIGINT (Ctrl+C). Shutting down...") + btcdLog.Infof("Received signal (%s). Shutting down...", + sig) // Run handlers in LIFO order. for i := range interruptCallbacks { @@ -74,7 +79,7 @@ func addInterruptHandler(handler func()) { // all other callbacks and exits if not already done. if interruptChannel == nil { interruptChannel = make(chan os.Signal, 1) - signal.Notify(interruptChannel, os.Interrupt) + signal.Notify(interruptChannel, signals...) go mainInterruptHandler() } diff --git a/signalsigterm.go b/signalsigterm.go new file mode 100644 index 00000000..7aaa39a7 --- /dev/null +++ b/signalsigterm.go @@ -0,0 +1,16 @@ +// Copyright (c) 2016 The btcsuite developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + +// +build darwin dragonfly freebsd linux netbsd openbsd solaris + +package main + +import ( + "os" + "syscall" +) + +func init() { + signals = []os.Signal{os.Interrupt, syscall.SIGTERM} +} From 128366734f0307d55bdaca0ed6955054cd3114e1 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 5 May 2016 14:16:58 -0500 Subject: [PATCH 2/4] main: Limit garbage collection percentage. (#686) This reduces the target ratio of freshly allocated data to live data to 10% in order to limit excessive overallocations by the garbage collector during data bursts such as processing complex blocks or rapidly receiving a lot of large transactions. --- btcd.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/btcd.go b/btcd.go index e1fdf04b..10b2a957 100644 --- a/btcd.go +++ b/btcd.go @@ -11,6 +11,7 @@ import ( _ "net/http/pprof" "os" "runtime" + "runtime/debug" "runtime/pprof" "github.com/btcsuite/btcd/blockchain/indexers" @@ -150,6 +151,12 @@ func main() { // Use all processor cores. runtime.GOMAXPROCS(runtime.NumCPU()) + // Block and transaction processing can cause bursty allocations. This + // limits the garbage collector from excessively overallocating during + // bursts. This value was arrived at with the help of profiling live + // usage. + debug.SetGCPercent(10) + // Up some limits. if err := limits.SetLimits(); err != nil { fmt.Fprintf(os.Stderr, "failed to set limits: %v\n", err) From 2554caee5919958c0d4b41db8035f0e12ce6f624 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Fri, 6 May 2016 10:47:53 -0500 Subject: [PATCH 3/4] build: Convert project to use glide. (#689) This converts the project to allow btcd to be used with the glide package manager in order to provide stable and reproducible builds without the user having to jump through all of the hoops as they do today. It consists of adding a glide.yaml file which identifies the project dependencies and locations along with a glide.lock file which contains the complete dependency tree pinned to specific versions. Glide uses these files to download the packages (or updates) to a local vendor directory and checkout the correct pinned versions. The go tool, in turn, is used to build/install btcd and will use the pinned versions in the vendor directory. This also updates TravisCI to build using glide, removes some of the exceptions in the lint checks which are no longer required, and updates the README.md with the new instructions needed to build the project with glide. --- .gitignore | 1 + .travis.yml | 5 +++-- README.md | 28 +++++++++++++++++--------- glide.lock | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ glide.yaml | 35 ++++++++++++++++++++++++++++++++ goclean.sh | 21 ++++++++++---------- 6 files changed, 125 insertions(+), 22 deletions(-) create mode 100644 glide.lock create mode 100644 glide.yaml diff --git a/.gitignore b/.gitignore index 5713f689..72fb9416 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,7 @@ btcd.db # Folders _obj _test +vendor # Architecture specific extensions/prefixes *.[568vq] diff --git a/.travis.yml b/.travis.yml index d003fea9..d00b407e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,10 +6,11 @@ sudo: false before_install: - gotools=golang.org/x/tools install: - - go get -d -t -v ./... + - go get -v github.com/Masterminds/glide + - glide install - go get -v $gotools/cmd/cover - - go get -v github.com/bradfitz/goimports - go get -v github.com/golang/lint/golint script: - export PATH=$PATH:$HOME/gopath/bin + - export GO15VENDOREXPERIMENT=1 - ./goclean.sh diff --git a/README.md b/README.md index 8525bb49..7970aad6 100644 --- a/README.md +++ b/README.md @@ -55,18 +55,26 @@ $ go env GOROOT GOPATH NOTE: The `GOROOT` and `GOPATH` above must not be the same path. It is recommended that `GOPATH` is set to a directory in your home directory such as -`~/goprojects` to avoid write permission issues. +`~/goprojects` to avoid write permission issues. It is also recommended to add +`$GOPATH/bin` to your `PATH` at this point. -- Run the following command to obtain btcd, all dependencies, and install it: +**NOTE:** If you are using Go 1.5, you must manually enable the vendor +experiment by setting the `GO15VENDOREXPERIMENT` environment variable to `1`. +This step is not required for Go 1.6. + +- Run the following commands to obtain btcd, all dependencies, and install it: ```bash -$ go get -u github.com/btcsuite/btcd/... +$ go get -u github.com/Masterminds/glide +$ git clone https://github.com/btcsuite/btcd $GOPATH/src/github.com/btcsuite/btcd +$ cd $GOPATH/src/github.com/btcsuite/btcd +$ glide install +$ go install . ./cmd/... ``` -- btcd (and utilities) will now be installed in either ```$GOROOT/bin``` or - ```$GOPATH/bin``` depending on your configuration. If you did not already - add the bin directory to your system path during Go installation, we - recommend you do so now. +- btcd (and utilities) will now be installed in ```$GOPATH/bin```. If you did + not already add the bin directory to your system path during Go installation, + we recommend you do so now. ## Updating @@ -76,10 +84,12 @@ Install a newer MSI #### Linux/BSD/MacOSX/POSIX - Build from Source -- Run the following command to update btcd, all dependencies, and install it: +- Run the following commands to update btcd, all dependencies, and install it: ```bash -$ go get -u -v github.com/btcsuite/btcd/... +$ cd $GOPATH/src/github.com/btcsuite/btcd +$ git pull && glide install +$ go install . ./cmd/... ``` ## Getting Started diff --git a/glide.lock b/glide.lock new file mode 100644 index 00000000..18a0f1a4 --- /dev/null +++ b/glide.lock @@ -0,0 +1,57 @@ +hash: 9b8ff781a12daad991983e9a421a320e905eecc4e9ff0b0643e790f472bc78c8 +updated: 2016-05-06T10:10:16.348595-05:00 +imports: +- name: github.com/btcsuite/btclog + version: f96df2375f37300305f329b8e5258764b4f19a7f +- name: github.com/btcsuite/btcutil + version: 2c26dd81a59fd671a2218d63ad138a6dd4d693bd + subpackages: + - . + - bloom + - base58 +- name: github.com/btcsuite/fastsha256 + version: 302ad4db268b46f9ebda3078f6f7397f96047735 +- name: github.com/btcsuite/go-flags + version: 6c288d648c1cc1befcb90cb5511dcacf64ae8e61 +- name: github.com/btcsuite/go-socks + version: cfe8b59e565c1a5bd4e2005d77cd9aa8b2e14524 + subpackages: + - socks +- name: github.com/btcsuite/golangcrypto + version: 53f62d9b43e87a6c56975cf862af7edf33a8d0df + subpackages: + - ripemd160 +- name: github.com/btcsuite/goleveldb + version: 7834afc9e8cd15233b6c3d97e12674a31ca24602 + subpackages: + - leveldb + - leveldb/comparer + - leveldb/errors + - leveldb/filter + - leveldb/iterator + - leveldb/opt + - leveldb/util + - leveldb/cache + - leveldb/journal + - leveldb/memdb + - leveldb/storage + - leveldb/table +- name: github.com/btcsuite/seelog + version: 313961b101eb55f65ae0f03ddd4e322731763b6c +- name: github.com/btcsuite/snappy-go + version: 0bdef8d067237991ddaa1bb6072a740bc40601ba +- name: github.com/btcsuite/websocket + version: 31079b6807923eb23992c421b114992b95131b55 +- name: github.com/btcsuite/winsvc + version: f8fb11f83f7e860e3769a08e6811d1b399a43722 + subpackages: + - eventlog + - mgr + - svc + - registry + - winapi +- name: github.com/davecgh/go-spew + version: 5215b55f46b2b919f50a1df0eaa5886afe4e3b3d + subpackages: + - spew +devImports: [] diff --git a/glide.yaml b/glide.yaml new file mode 100644 index 00000000..5298a276 --- /dev/null +++ b/glide.yaml @@ -0,0 +1,35 @@ +package: github.com/btcsuite/btcd +import: +- package: github.com/btcsuite/btclog +- package: github.com/btcsuite/btcutil + subpackages: + - . + - bloom + - base58 +- package: github.com/btcsuite/fastsha256 +- package: github.com/btcsuite/go-flags +- package: github.com/btcsuite/go-socks + subpackages: + - socks +- package: github.com/btcsuite/golangcrypto + subpackages: + - ripemd160 +- package: github.com/btcsuite/goleveldb + subpackages: + - leveldb + - leveldb/comparer + - leveldb/errors + - leveldb/filter + - leveldb/iterator + - leveldb/opt + - leveldb/util +- package: github.com/btcsuite/seelog +- package: github.com/btcsuite/websocket +- package: github.com/btcsuite/winsvc + subpackages: + - eventlog + - mgr + - svc +- package: github.com/davecgh/go-spew + subpackages: + - spew diff --git a/goclean.sh b/goclean.sh index eefbdb91..8a4e8379 100755 --- a/goclean.sh +++ b/goclean.sh @@ -1,20 +1,18 @@ #!/bin/bash # The script does automatic checking on a Go package and its sub-packages, including: # 1. gofmt (http://golang.org/cmd/gofmt/) -# 2. goimports (https://github.com/bradfitz/goimports) -# 3. golint (https://github.com/golang/lint) -# 4. go vet (http://golang.org/cmd/vet) -# 5. race detector (http://blog.golang.org/race-detector) -# 6. test coverage (http://blog.golang.org/cover) +# 2. golint (https://github.com/golang/lint) +# 3. go vet (http://golang.org/cmd/vet) +# 4. race detector (http://blog.golang.org/race-detector) +# 5. test coverage (http://blog.golang.org/cover) set -ex # Automatic checks -test -z "$(gofmt -l -w . | tee /dev/stderr)" -test -z "$(goimports -l -w . | tee /dev/stderr)" -test -z "$(golint ./... | grep -v 'ALL_CAPS\|OP_\|NewFieldVal\|RpcCommand\|RpcRawCommand\|RpcSend\|Dns' | tee /dev/stderr)" -test -z "$(go tool vet . 2>&1 | grep -v 'Example\|newestSha' | tee /dev/stderr)" -env GORACE="halt_on_error=1" go test -v -race ./... +test -z "$(go fmt $(glide novendor) | tee /dev/stderr)" +test -z "$(for package in $(glide novendor); do golint $package; done | grep -v 'ALL_CAPS\|OP_\|NewFieldVal' | tee /dev/stderr)" +test -z "$(go vet $(glide novendor) 2>&1 | tee /dev/stderr)" +env GORACE="halt_on_error=1" go test -v -race $(glide novendor) # Run test coverage on each subdirectories and merge the coverage profile. @@ -23,7 +21,8 @@ echo "mode: count" > profile.cov # Standard go tooling behavior is to ignore dirs with leading underscores. for dir in $(find . -maxdepth 10 -not -path '.' -not -path './.git*' \ - -not -path '*/_*' -not -path './cmd*' -not -path './release*' -type d) + -not -path '*/_*' -not -path './cmd*' -not -path './release*' \ + -not -path './vendor*' -type d) do if ls $dir/*.go &> /dev/null; then go test -covermode=count -coverprofile=$dir/profile.tmp $dir From f893558d782396f10c2fe49a8bc73deff4a36d14 Mon Sep 17 00:00:00 2001 From: Nathan Bass Date: Sat, 14 May 2016 22:56:29 -0500 Subject: [PATCH 4/4] Minor correction to GenerateSharedSecret documentation. (#696) --- btcec/ciphering.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/btcec/ciphering.go b/btcec/ciphering.go index e4f3abeb..11e6fbb5 100644 --- a/btcec/ciphering.go +++ b/btcec/ciphering.go @@ -41,7 +41,7 @@ var ( ) // GenerateSharedSecret generates a shared secret based on a private key and a -// private key using Diffie-Hellman key exchange (ECDH) (RFC 4753). +// public key using Diffie-Hellman key exchange (ECDH) (RFC 4753). // RFC5903 Section 9 states we should only return x. func GenerateSharedSecret(privkey *PrivateKey, pubkey *PublicKey) []byte { x, _ := pubkey.Curve.ScalarMult(pubkey.X, pubkey.Y, privkey.D.Bytes())