Skip to content

Commit

Permalink
Don't allow builds with "...nomain" git tag to run on mainnet
Browse files Browse the repository at this point in the history
The check is done by Makefile. In case if the current version string
contains `nomain` substring, -X main.noMainNet=true ldflag is used.
  • Loading branch information
ivan4th committed Nov 21, 2023
1 parent c379872 commit df95f3d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ DOCKER_HUB ?= spacemeshos
DOCKER_IMAGE_REPO ?= go-spacemesh-dev
DOCKER_IMAGE_VERSION ?= $(SHA)

LDFLAGS = -ldflags "-X main.version=${VERSION} -X main.commit=${COMMIT} -X main.branch=${BRANCH}"
C_LDFLAGS = -X main.version=${VERSION} -X main.commit=${COMMIT} -X main.branch=${BRANCH}
ifneq (,$(findstring nomain,$(VERSION)))
C_LDFLAGS += -X main.noMainNet=true
endif
LDFLAGS = -ldflags "$(C_LDFLAGS)"

include Makefile-libs.Inc

UNIT_TESTS ?= $(shell go list ./... | grep -v systest/tests | grep -v cmd/node | grep -v cmd/gen-p2p-identity | grep -v cmd/trace | grep -v genvm/cmd)
Expand Down
3 changes: 3 additions & 0 deletions cmd/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ var (

// Commit is the git commit used to build the app. Designed to be overwritten by make.
Commit string

// Prohibit this build from running on the mainnet.
NoMainNet bool
)

// EnsureCLIFlags checks flag types and converts them.
Expand Down
8 changes: 5 additions & 3 deletions cmd/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ import (
)

var (
version string
commit string
branch string
version string
commit string
branch string
noMainNet string
)

func main() { // run the app
cmd.Version = version
cmd.Commit = commit
cmd.Branch = branch
cmd.NoMainNet = noMainNet == "true"
if err := node.GetCommand().Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down
9 changes: 9 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ func GetCommand() *cobra.Command {
if conf.LOGGING.Encoder == config.JSONLogEncoder {
log.JSONLog(true)
}

if cmd.NoMainNet && onMainNet(conf) {
log.With().Fatal("this is a testnet-only build not intended for mainnet")
}

app := New(
WithConfig(conf),
// NOTE(dshulyak) this needs to be max level so that child logger can can be current level or below.
Expand Down Expand Up @@ -1576,3 +1581,7 @@ func (w tortoiseWeakCoin) Set(lid types.LayerID, value bool) error {
w.tortoise.OnWeakCoin(lid, value)
return nil
}

func onMainNet(conf *config.Config) bool {
return conf.Genesis.GenesisTime == config.MainnetConfig().Genesis.GenesisTime
}

1 comment on commit df95f3d

@lrettig
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ivan4th in onMainNet() rather than checking conf.Genesis.GenesisTime, better to check conf.Genesis.GenesisID() since this also includes the extradata/golden ATX.

Please sign in to comment.