From ec48786180efe0ff984983b83e439417022ff8b3 Mon Sep 17 00:00:00 2001 From: Hussam Date: Tue, 17 Dec 2024 17:15:44 -0600 Subject: [PATCH] Sanity check to prevent common values mutating --- cmd/go-quai/start.go | 12 ++++++++++-- common/big.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) 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 5efbe9e231..db6a04c9dc 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" ) @@ -80,3 +82,45 @@ 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) + big99 := big.NewInt(99) + big100 := big.NewInt(100) + big101 := big.NewInt(101) + 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 || + big99.Cmp(Big99) != 0 || + big100.Cmp(Big100) != 0 || + big101.Cmp(Big101) != 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.Fatal("A common value has mutated, exiting now") + quitCh <- struct{}{} + } + } + }(quitCh) +}