Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testnet Generate command #23

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions cmd/testnet/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package testnet

import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"

"github.com/chia-network/go-chia-libs/pkg/config"
"github.com/chia-network/go-chia-libs/pkg/types"
"github.com/chia-network/go-modules/pkg/slogs"
"github.com/spf13/cast"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
)

// generateCmd represents the generate command
var generateCmd = &cobra.Command{
Use: "generate",
Short: "Generates a new testnet",
Example: "chia-tools testnet generate --network examplenet",
Run: func(cmd *cobra.Command, args []string) {
networkName := viper.GetString("tn-gen-network")
genesisHashBytes := sha256.Sum256([]byte(networkName))
genesisHash := hex.EncodeToString(genesisHashBytes[:32])

constants := &config.NetworkConstants{
AggSigMeAdditionalData: genesisHash,
DifficultyConstantFactor: types.Uint128From64(viper.GetUint64("tn-gen-diff-constant-factor")),
DifficultyStarting: viper.GetUint64("tn-gen-difficulty-starting"),
EpochBlocks: viper.GetUint32("tn-gen-epoch-blocks"),
GenesisChallenge: genesisHash,
GenesisPreFarmPoolPuzzleHash: viper.GetString("tn-gen-pre-farm-pool-puz-hash"),
GenesisPreFarmFarmerPuzzleHash: viper.GetString("tn-gen-pre-farm-farmer-puz-hash"),
MempoolBlockBuffer: cast.ToUint8(viper.Get("tn-gen-mempool-block-buffer")),
MinPlotSize: cast.ToUint8(viper.Get("tn-gen-min-plot-size")),
NetworkType: 1,
SubSlotItersStarting: viper.GetUint64("tn-gen-sub-slot-iters-starting"),
}
cfg := &config.NetworkConfig{
AddressPrefix: "txch",
DefaultFullNodePort: viper.GetUint16("tn-gen-port"),
}

netOverrides := &config.NetworkOverrides{
Constants: map[string]config.NetworkConstants{
networkName: *constants,
},
Config: map[string]config.NetworkConfig{
networkName: *cfg,
},
}

var toMarshal any
if viper.GetBool("tn-gen-with-constants") {
toMarshal = netOverrides
} else {
toMarshal = constants
}

var marshalled []byte
var err error
if viper.GetBool("tn-gen-as-json") {
marshalled, err = json.Marshal(toMarshal)
} else {
marshalled, err = yaml.Marshal(toMarshal)
}

if err != nil {
slogs.Logr.Fatal("error marshalling", "error", err)
}
fmt.Print(string(marshalled))
},
}

func init() {
generateCmd.PersistentFlags().String("network", "", "Name of the network to create")
generateCmd.PersistentFlags().Uint64("diff-constant-factor", uint64(10052721566054), "Specify the value for DIFFICULTY_CONSTANT_FACTOR (Up to uint64max)")
generateCmd.PersistentFlags().String("pre-farm-farmer-puz-hash", "08296fc227decd043aee855741444538e4cc9a31772c4d1a9e6242d1e777e42a", "Specify the value for GENESIS_PRE_FARM_FARMER_PUZZLE_HASH")
generateCmd.PersistentFlags().String("pre-farm-pool-puz-hash", "08296fc227decd043aee855741444538e4cc9a31772c4d1a9e6242d1e777e42a", "Specify the value for GENESIS_PRE_FARM_POOL_PUZZLE_HASH")
generateCmd.PersistentFlags().Uint8("min-plot-size", uint8(18), "Specify the minimum plot size MIN_PLOT_SIZE")
generateCmd.PersistentFlags().Uint8("mempool-block-buffer", uint8(10), "Specify MEMPOOL_BLOCK_BUFFER")
generateCmd.PersistentFlags().Uint32("epoch-blocks", uint32(768), "specify EPOCH_BLOCKS")
generateCmd.PersistentFlags().Uint64("difficulty-starting", uint64(30), "Specify starting difficulty")
generateCmd.PersistentFlags().Uint64("sub-slot-iters-starting", uint64(1<<26), "Specify starting sub slot iters")
generateCmd.PersistentFlags().Uint16("port", uint16(58445), "Specify the port the network full nodes should use")
generateCmd.PersistentFlags().Bool("as-json", false, "Output as JSON blob instead of yaml")
generateCmd.PersistentFlags().Bool("with-constants", false, "Include constants and default ports")

cobra.CheckErr(viper.BindPFlag("tn-gen-network", generateCmd.PersistentFlags().Lookup("network")))
cobra.CheckErr(viper.BindPFlag("tn-gen-diff-constant-factor", generateCmd.PersistentFlags().Lookup("diff-constant-factor")))
cobra.CheckErr(viper.BindPFlag("tn-gen-pre-farm-farmer-puz-hash", generateCmd.PersistentFlags().Lookup("pre-farm-farmer-puz-hash")))
cobra.CheckErr(viper.BindPFlag("tn-gen-pre-farm-pool-puz-hash", generateCmd.PersistentFlags().Lookup("pre-farm-pool-puz-hash")))
cobra.CheckErr(viper.BindPFlag("tn-gen-min-plot-size", generateCmd.PersistentFlags().Lookup("min-plot-size")))
cobra.CheckErr(viper.BindPFlag("tn-gen-mempool-block-buffer", generateCmd.PersistentFlags().Lookup("mempool-block-buffer")))
cobra.CheckErr(viper.BindPFlag("tn-gen-epoch-blocks", generateCmd.PersistentFlags().Lookup("epoch-blocks")))
cobra.CheckErr(viper.BindPFlag("tn-gen-difficulty-starting", generateCmd.PersistentFlags().Lookup("difficulty-starting")))
cobra.CheckErr(viper.BindPFlag("tn-gen-sub-slot-iters-starting", generateCmd.PersistentFlags().Lookup("sub-slot-iters-starting")))
cobra.CheckErr(viper.BindPFlag("tn-gen-port", generateCmd.PersistentFlags().Lookup("port")))
cobra.CheckErr(viper.BindPFlag("tn-gen-as-json", generateCmd.PersistentFlags().Lookup("as-json")))
cobra.CheckErr(viper.BindPFlag("tn-gen-with-constants", generateCmd.PersistentFlags().Lookup("with-constants")))

testnetCmd.AddCommand(generateCmd)
}
17 changes: 17 additions & 0 deletions cmd/testnet/testnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package testnet

import (
"github.com/spf13/cobra"

"github.com/chia-network/chia-tools/cmd"
)

// testnetCmd represents the config command
var testnetCmd = &cobra.Command{
Use: "testnet",
Short: "Utilities for working with chia testnets",
}

func init() {
cmd.RootCmd.AddCommand(testnetCmd)
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ module github.com/chia-network/chia-tools
go 1.22.4

require (
github.com/chia-network/go-chia-libs v0.14.1
github.com/chia-network/go-chia-libs v0.14.2
github.com/chia-network/go-modules v0.0.5
github.com/spf13/cast v1.7.0
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -26,7 +27,6 @@ require (
github.com/samber/mo v1.13.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/chia-network/go-chia-libs v0.14.1 h1:2czAv1cKKfLgY0BMKgoQpwoIBkIhakdfsLcHz5IDxwA=
github.com/chia-network/go-chia-libs v0.14.1/go.mod h1:npTqaFSjTdMxE7hc0LOmWJmWGqcs+IERarK5fDxXk/I=
github.com/chia-network/go-chia-libs v0.14.2 h1:CMXPaxnyxT295T4kDohUzbEltgp6TMsIhFKgZ3qDJwk=
github.com/chia-network/go-chia-libs v0.14.2/go.mod h1:npTqaFSjTdMxE7hc0LOmWJmWGqcs+IERarK5fDxXk/I=
github.com/chia-network/go-modules v0.0.5 h1:5luTVlP6RgBXodnFcWFBk2sLdJn+6vQ4wObim683C7c=
github.com/chia-network/go-modules v0.0.5/go.mod h1:5AiYBxQSvf2aFSOizTqFXXSeb9AucZWrWmRCVwUMO3A=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
_ "github.com/chia-network/chia-tools/cmd/certs"
_ "github.com/chia-network/chia-tools/cmd/config"
_ "github.com/chia-network/chia-tools/cmd/datalayer"
_ "github.com/chia-network/chia-tools/cmd/testnet"
)

func main() {
Expand Down