Skip to content

Commit

Permalink
Sanity check to prevent common values mutating
Browse files Browse the repository at this point in the history
Djadih committed Dec 18, 2024

Verified

This commit was signed with the committer’s verified signature.
1 parent 9dc7043 commit f006891
Showing 2 changed files with 48 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
@@ -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()
38 changes: 38 additions & 0 deletions common/big.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit f006891

Please sign in to comment.