diff --git a/cmd/go-quai/start.go b/cmd/go-quai/start.go index 4f611cfeab..9ec235563d 100644 --- a/cmd/go-quai/start.go +++ b/cmd/go-quai/start.go @@ -15,6 +15,7 @@ import ( "github.com/spf13/viper" "github.com/dominant-strategies/go-quai/cmd/utils" + "github.com/dominant-strategies/go-quai/common" "github.com/dominant-strategies/go-quai/log" "github.com/dominant-strategies/go-quai/metrics_config" "github.com/dominant-strategies/go-quai/p2p/node" @@ -73,6 +74,7 @@ func runStart(cmd *cobra.Command, args []string) error { // create a quit channel for services to signal for a clean shutdown quitCh := make(chan struct{}) + common.SanityCheck(quitCh) // create a new p2p node node, err := node.NewNode(ctx, quitCh) if err != nil { @@ -104,8 +106,14 @@ func runStart(cmd *cobra.Command, args []string) error { // wait for a SIGINT or SIGTERM signal ch := make(chan os.Signal, 1) signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) - <-ch - log.Global.Warn("Received 'stop' signal, shutting down gracefully...") + + select { + case <-ch: + log.Global.Warn("Received 'stop' signal, shutting down gracefully...") + case <-quitCh: + log.Global.Warn("Received 'quit' signal from child, shutting down...") + } + cancel() // stop the hierarchical co-ordinator hc.Stop() diff --git a/common/big.go b/common/big.go index 3626dd51a0..571a417f9f 100644 --- a/common/big.go +++ b/common/big.go @@ -18,7 +18,9 @@ package common import ( "math/big" + "time" + "github.com/dominant-strategies/go-quai/log" "modernc.org/mathutil" ) @@ -77,3 +79,39 @@ func LogBig(diff *big.Int) *big.Int { bigBits = new(big.Int).Add(bigBits, m) return bigBits } + +// Continously verify that the common values have not been overwritten. +func SanityCheck(quitCh chan struct{}) { + big0 := big.NewInt(0) + big1 := big.NewInt(1) + big2 := big.NewInt(2) + big3 := big.NewInt(3) + big8 := big.NewInt(8) + big32 := big.NewInt(32) + big256 := big.NewInt(256) + big257 := big.NewInt(257) + big2e256 := new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0)) + big2e64 := new(big.Int).Exp(big.NewInt(2), big.NewInt(64), big.NewInt(0)) + + go func(quitCh chan struct{}) { + for { + time.Sleep(1 * time.Minute) + + // Verify that none of the values have mutated. + if big0.Cmp(Big0) != 0 || + big1.Cmp(Big1) != 0 || + big2.Cmp(Big2) != 0 || + big3.Cmp(Big3) != 0 || + big8.Cmp(Big8) != 0 || + big32.Cmp(Big32) != 0 || + big256.Cmp(Big256) != 0 || + big257.Cmp(Big257) != 0 || + big2e256.Cmp(new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0))) != 0 || + big2e64.Cmp(new(big.Int).Exp(big.NewInt(2), big.NewInt(64), big.NewInt(0))) != 0 { + // Send a message to quitCh to abort. + log.Global.Error("A common value has mutated, exiting now") + quitCh <- struct{}{} + } + } + }(quitCh) +}