From 9eb6ba7cf55cd252a021f6e0c484c26bf8fc1a86 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 20 Aug 2020 16:55:31 -0400 Subject: [PATCH 01/10] refactoring files --- metrics/metrics.go => monitor/health/health.go | 0 {metrics => monitor/health}/http.go | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename metrics/metrics.go => monitor/health/health.go (100%) rename {metrics => monitor/health}/http.go (100%) diff --git a/metrics/metrics.go b/monitor/health/health.go similarity index 100% rename from metrics/metrics.go rename to monitor/health/health.go diff --git a/metrics/http.go b/monitor/health/http.go similarity index 100% rename from metrics/http.go rename to monitor/health/http.go From 927bff0e2eb20bcb3e96f78c741c9ac569962615 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 20 Aug 2020 17:01:19 -0400 Subject: [PATCH 02/10] changing package names --- cmd/chainbridge/main.go | 4 ++-- monitor/health/health.go | 2 +- monitor/health/http.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/chainbridge/main.go b/cmd/chainbridge/main.go index f5cc1e190..082b215a1 100644 --- a/cmd/chainbridge/main.go +++ b/cmd/chainbridge/main.go @@ -17,7 +17,7 @@ import ( "github.com/ChainSafe/ChainBridge/config" "github.com/ChainSafe/ChainBridge/core" msg "github.com/ChainSafe/ChainBridge/message" - "github.com/ChainSafe/ChainBridge/metrics" + "github.com/ChainSafe/ChainBridge/monitor/health" log "github.com/ChainSafe/log15" "github.com/urfave/cli/v2" ) @@ -189,7 +189,7 @@ func run(ctx *cli.Context) error { c.AddChain(newChain) } - metrics.Start() + health.Start() c.Start() diff --git a/monitor/health/health.go b/monitor/health/health.go index d0fde6941..5039a2825 100644 --- a/monitor/health/health.go +++ b/monitor/health/health.go @@ -1,4 +1,4 @@ -package metrics +package health // Start spins up the metrics server func Start() { diff --git a/monitor/health/http.go b/monitor/health/http.go index 45160543a..25a37f3f7 100644 --- a/monitor/health/http.go +++ b/monitor/health/http.go @@ -1,4 +1,4 @@ -package metrics +package health import ( "fmt" From 624707a059f3ffebc08a4c3a5225abcf7454cab9 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 20 Aug 2020 17:10:44 -0400 Subject: [PATCH 03/10] adding licenses to files --- monitor/health/health.go | 7 +++++++ monitor/health/http.go | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/monitor/health/health.go b/monitor/health/health.go index 5039a2825..e6f944199 100644 --- a/monitor/health/health.go +++ b/monitor/health/health.go @@ -1,3 +1,10 @@ +// Copyright 2020 ChainSafe Systems +// SPDX-License-Identifier: LGPL-3.0-only +/* +Provides the command-line interface for the chainbridge application. + +For configuration and CLI commands see the README: https://github.com/ChainSafe/ChainBridge. +*/ package health // Start spins up the metrics server diff --git a/monitor/health/http.go b/monitor/health/http.go index 25a37f3f7..76c5f07bd 100644 --- a/monitor/health/http.go +++ b/monitor/health/http.go @@ -1,3 +1,10 @@ +// Copyright 2020 ChainSafe Systems +// SPDX-License-Identifier: LGPL-3.0-only +/* +Provides the command-line interface for the chainbridge application. + +For configuration and CLI commands see the README: https://github.com/ChainSafe/ChainBridge. +*/ package health import ( From e8c86e4077515a583c61674f8a0faee84769e79f Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 20 Aug 2020 17:11:30 -0400 Subject: [PATCH 04/10] edit comment --- monitor/health/health.go | 4 ---- monitor/health/http.go | 4 ---- 2 files changed, 8 deletions(-) diff --git a/monitor/health/health.go b/monitor/health/health.go index e6f944199..16b70e569 100644 --- a/monitor/health/health.go +++ b/monitor/health/health.go @@ -1,10 +1,6 @@ // Copyright 2020 ChainSafe Systems // SPDX-License-Identifier: LGPL-3.0-only -/* -Provides the command-line interface for the chainbridge application. -For configuration and CLI commands see the README: https://github.com/ChainSafe/ChainBridge. -*/ package health // Start spins up the metrics server diff --git a/monitor/health/http.go b/monitor/health/http.go index 76c5f07bd..c48007020 100644 --- a/monitor/health/http.go +++ b/monitor/health/http.go @@ -1,10 +1,6 @@ // Copyright 2020 ChainSafe Systems // SPDX-License-Identifier: LGPL-3.0-only -/* -Provides the command-line interface for the chainbridge application. -For configuration and CLI commands see the README: https://github.com/ChainSafe/ChainBridge. -*/ package health import ( From 89573f3865f59a191fddb951cefc1d25586ffc78 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 20 Aug 2020 17:27:02 -0400 Subject: [PATCH 05/10] adding missing method --- chains/substrate/chain.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/chains/substrate/chain.go b/chains/substrate/chain.go index beed65c3a..37841963c 100644 --- a/chains/substrate/chain.go +++ b/chains/substrate/chain.go @@ -31,6 +31,7 @@ import ( msg "github.com/ChainSafe/ChainBridge/message" "github.com/ChainSafe/ChainBridge/router" "github.com/ChainSafe/log15" + "math/big" ) var _ core.Chain = &Chain{} @@ -132,6 +133,10 @@ func (c *Chain) SetRouter(r *router.Router) { c.listener.setRouter(r) } +func (c *Chain) GetLatestBlock() (*big.Int, error) { + return c.listener.blockstore.TryLoadLatestBlock() +} + func (c *Chain) Id() msg.ChainId { return c.cfg.Id } From 251b5c727b59c790c93485b37cafa56d15157e2e Mon Sep 17 00:00:00 2001 From: luu-alex Date: Thu, 20 Aug 2020 18:01:49 -0400 Subject: [PATCH 06/10] removing unused imports --- e2e/bootstrap/bootstrap_e2e_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/e2e/bootstrap/bootstrap_e2e_test.go b/e2e/bootstrap/bootstrap_e2e_test.go index fa2b5c458..9fdd86daf 100644 --- a/e2e/bootstrap/bootstrap_e2e_test.go +++ b/e2e/bootstrap/bootstrap_e2e_test.go @@ -5,10 +5,7 @@ package e2e import ( "fmt" - "io/ioutil" "math/big" - "os" - "strings" "testing" ethChain "github.com/ChainSafe/ChainBridge/chains/ethereum" From 3e9cb600f2158fd08286947a2bf41a1fd1331e6c Mon Sep 17 00:00:00 2001 From: luu-alex Date: Fri, 21 Aug 2020 12:27:03 -0400 Subject: [PATCH 07/10] fixing linter errors for http.go --- monitor/health/http.go | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/monitor/health/http.go b/monitor/health/http.go index c48007020..488e552d1 100644 --- a/monitor/health/http.go +++ b/monitor/health/http.go @@ -31,9 +31,9 @@ type httpMetricOptions struct { core *core.Core } -type chain struct { - id int -} +//type chain struct { +// id int +//} func newHTTPMetricServer(opts httpMetricOptions) *HttpMetricServer { return &HttpMetricServer{ @@ -52,7 +52,10 @@ func (s HttpMetricServer) Start() { // Start http server // TODO Push strconv to cli parser - http.ListenAndServe(":"+strconv.Itoa(s.port), nil) + err := http.ListenAndServe(":"+strconv.Itoa(s.port), nil) + if err != nil{ + log.Error("Server failed to start", err) + } } // healthStatus is a catch-all update that grabs the latest updates on the running chains @@ -73,7 +76,10 @@ func (s HttpMetricServer) healthStatus(w http.ResponseWriter, r *http.Request) { // TODO better error messaging errorMsg := fmt.Sprintf("%s%d%s%s", "Failed to receive latest head for: ", chain.Id(), "Error:", err) w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(errorMsg)) + _, err := w.Write([]byte(errorMsg)) + if err != nil { + log.Error("Failed to write, failed to get latest height and writing error", err) + } } // Get old blockheight @@ -91,12 +97,18 @@ func (s HttpMetricServer) healthStatus(w http.ResponseWriter, r *http.Request) { // Error if we exceeded the time limit errorMsg := fmt.Sprintf("%s%s%s%s%s", "Chain height hasn't changed in: ", timeDiff, "Current height", latestHeight) w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(errorMsg)) + _, err = w.Write([]byte(errorMsg)) + if err != nil { + log.Error("Failed to write, chain height hasn't changed in: %d seconds, Current height: %d", timeDiff.Seconds(), latestHeight, err) + } } else { // Error for having a smaller blockheight than previous errorMsg := fmt.Sprintf("%s%s%s%s%s", "latestHeight is <= previousHeight", "previousHeight", prevHeight.height, "latestHeight", latestHeight) w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(errorMsg)) + if err != nil { + log.Error("Failed to write, latest height less than previous height, latest height: %d, previous height: %d", latestHeight, prevHeight.height, err) + } } } } else { @@ -109,5 +121,8 @@ func (s HttpMetricServer) healthStatus(w http.ResponseWriter, r *http.Request) { } } w.WriteHeader(http.StatusOK) - w.Write([]byte("200 - Operational")) + _, err := w.Write([]byte("200 - Operational")) + if err != nil { + log.Error("Failed to write, 200 - Operational") + } } From 267e8280d09d84a57a4b5915190d60a48cf160ed Mon Sep 17 00:00:00 2001 From: luu-alex Date: Fri, 21 Aug 2020 13:18:14 -0400 Subject: [PATCH 08/10] fixing up linter --- chains/substrate/chain.go | 3 ++- cmd/chainbridge/account.go | 6 +++--- monitor/health/http.go | 8 ++++---- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/chains/substrate/chain.go b/chains/substrate/chain.go index 37841963c..c5c5a436e 100644 --- a/chains/substrate/chain.go +++ b/chains/substrate/chain.go @@ -24,6 +24,8 @@ As the writer receives messages from the router, it constructs proposals. If a p package substrate import ( + "math/big" + "github.com/ChainSafe/ChainBridge/blockstore" "github.com/ChainSafe/ChainBridge/core" "github.com/ChainSafe/ChainBridge/crypto/sr25519" @@ -31,7 +33,6 @@ import ( msg "github.com/ChainSafe/ChainBridge/message" "github.com/ChainSafe/ChainBridge/router" "github.com/ChainSafe/log15" - "math/big" ) var _ core.Chain = &Chain{} diff --git a/cmd/chainbridge/account.go b/cmd/chainbridge/account.go index 2fbfc67c6..bd79e56af 100644 --- a/cmd/chainbridge/account.go +++ b/cmd/chainbridge/account.go @@ -183,7 +183,7 @@ func importPrivKey(ctx *cli.Context, keytype, datadir, key string, password []by return "", fmt.Errorf("invalid filepath: %s", err) } - file, err := os.OpenFile(fp, os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0600) + file, err := os.OpenFile(filepath.Clean(fp), os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0600) if err != nil { return "", fmt.Errorf("Unable to Open File: %s", err) } @@ -233,7 +233,7 @@ func importEthKey(filename, datadir string, password, newPassword []byte) (strin return "", fmt.Errorf("invalid filepath: %s", err) } - file, err := os.OpenFile(fp, os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0600) + file, err := os.OpenFile(filepath.Clean(fp), os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0600) if err != nil { return "", err } @@ -374,7 +374,7 @@ func generateKeypair(keytype, datadir string, password []byte, subNetwork string return "", fmt.Errorf("invalid filepath: %s", err) } - file, err := os.OpenFile(fp, os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0600) + file, err := os.OpenFile(filepath.Clean(fp), os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0600) if err != nil { return "", err } diff --git a/monitor/health/http.go b/monitor/health/http.go index 488e552d1..0ad2607ec 100644 --- a/monitor/health/http.go +++ b/monitor/health/http.go @@ -53,7 +53,7 @@ func (s HttpMetricServer) Start() { // Start http server // TODO Push strconv to cli parser err := http.ListenAndServe(":"+strconv.Itoa(s.port), nil) - if err != nil{ + if err != nil { log.Error("Server failed to start", err) } } @@ -76,7 +76,7 @@ func (s HttpMetricServer) healthStatus(w http.ResponseWriter, r *http.Request) { // TODO better error messaging errorMsg := fmt.Sprintf("%s%d%s%s", "Failed to receive latest head for: ", chain.Id(), "Error:", err) w.WriteHeader(http.StatusInternalServerError) - _, err := w.Write([]byte(errorMsg)) + _, err = w.Write([]byte(errorMsg)) if err != nil { log.Error("Failed to write, failed to get latest height and writing error", err) } @@ -95,7 +95,7 @@ func (s HttpMetricServer) healthStatus(w http.ResponseWriter, r *http.Request) { } else { if timeDiff.Seconds() > 120 { // Error if we exceeded the time limit - errorMsg := fmt.Sprintf("%s%s%s%s%s", "Chain height hasn't changed in: ", timeDiff, "Current height", latestHeight) + errorMsg := fmt.Sprintf("%s%s%s%s", "Chain height hasn't changed in: ", timeDiff, "Current height", latestHeight) w.WriteHeader(http.StatusInternalServerError) _, err = w.Write([]byte(errorMsg)) if err != nil { @@ -105,7 +105,7 @@ func (s HttpMetricServer) healthStatus(w http.ResponseWriter, r *http.Request) { // Error for having a smaller blockheight than previous errorMsg := fmt.Sprintf("%s%s%s%s%s", "latestHeight is <= previousHeight", "previousHeight", prevHeight.height, "latestHeight", latestHeight) w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(errorMsg)) + _, err := w.Write([]byte(errorMsg)) if err != nil { log.Error("Failed to write, latest height less than previous height, latest height: %d, previous height: %d", latestHeight, prevHeight.height, err) } From 88c046127850cacb46c6a2bd6480a5b34712cc40 Mon Sep 17 00:00:00 2001 From: luu-alex Date: Fri, 21 Aug 2020 13:50:43 -0400 Subject: [PATCH 09/10] removing extra import --- chains/substrate/chain.go | 1 - 1 file changed, 1 deletion(-) diff --git a/chains/substrate/chain.go b/chains/substrate/chain.go index 60d8425c8..c5c5a436e 100644 --- a/chains/substrate/chain.go +++ b/chains/substrate/chain.go @@ -33,7 +33,6 @@ import ( msg "github.com/ChainSafe/ChainBridge/message" "github.com/ChainSafe/ChainBridge/router" "github.com/ChainSafe/log15" - "math/big" ) var _ core.Chain = &Chain{} From 4650a3d23b27514581233f7427437bfc9f667903 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 21 Aug 2020 15:49:52 -0400 Subject: [PATCH 10/10] handling graceful shutdown --- monitor/health/http.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/monitor/health/http.go b/monitor/health/http.go index 0ad2607ec..24c81ca22 100644 --- a/monitor/health/http.go +++ b/monitor/health/http.go @@ -53,8 +53,11 @@ func (s HttpMetricServer) Start() { // Start http server // TODO Push strconv to cli parser err := http.ListenAndServe(":"+strconv.Itoa(s.port), nil) - if err != nil { - log.Error("Server failed to start", err) + + if err == http.ErrServerClosed { + log.Info("Server is shutting down", err) + } else { + log.Error("Shutting down, server error: ", err) } }