Skip to content

Commit

Permalink
Sanity check to prevent common values mutating
Browse files Browse the repository at this point in the history
  • Loading branch information
Djadih committed Dec 17, 2024
1 parent 2e4a598 commit ec48786
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
12 changes: 10 additions & 2 deletions cmd/go-quai/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
44 changes: 44 additions & 0 deletions common/big.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package common

import (
"math/big"
"time"

"github.com/dominant-strategies/go-quai/log"
"modernc.org/mathutil"
)

Expand Down Expand Up @@ -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)
}

0 comments on commit ec48786

Please sign in to comment.