Skip to content

Commit

Permalink
feat: mainnet config and genesis files (#951)
Browse files Browse the repository at this point in the history
Co-authored-by: b00f <mostafa.sedaghat@gmail.com>
  • Loading branch information
themantre and b00f authored Jan 24, 2024
1 parent 80e1935 commit 5309800
Show file tree
Hide file tree
Showing 25 changed files with 280 additions and 111 deletions.
11 changes: 9 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,14 @@ func CreateNode(numValidators int, chain genesis.ChainType, workingDir string,

switch chain {
case genesis.Mainnet:
panic("not yet!")
genDoc := genesis.MainnetGenesis()
if err := genDoc.SaveToFile(genPath); err != nil {
return nil, nil, err
}
conf := config.DefaultConfigMainnet()
if err := conf.Save(confPath); err != nil {
return nil, nil, err
}
case genesis.Testnet:
genDoc := genesis.TestnetGenesis()
if err := genDoc.SaveToFile(genPath); err != nil {
Expand Down Expand Up @@ -492,7 +499,7 @@ func MakeConfig(genDoc *genesis.Genesis, confPath, walletsDir string) (*config.C
var defConf *config.Config
switch genDoc.ChainType() {
case genesis.Mainnet:
panic("not yet implemented!")
defConf = config.DefaultConfigMainnet()
case genesis.Testnet:
defConf = config.DefaultConfigTestnet()
case genesis.Localnet:
Expand Down
22 changes: 13 additions & 9 deletions cmd/daemon/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,26 @@ func buildInitCmd(parentCmd *cobra.Command) {
Short: "initialize the Pactus Blockchain node",
}
parentCmd.AddCommand(initCmd)
workingDirOpt := initCmd.Flags().StringP("working-dir", "w",
cmd.PactusDefaultHomeDir(), "the path to the working directory to save the wallet and node files")
workingDirOpt := initCmd.Flags().StringP("working-dir", "w", cmd.PactusDefaultHomeDir(),
"a path to the working directory to save the wallet and node files")

testnetOpt := initCmd.Flags().Bool("testnet", true,
"initialize working directory for joining the testnet") // TODO: make it false after mainnet launch
testnetOpt := initCmd.Flags().Bool("testnet", false,
"initialize working directory for joining the testnet")

localnetOpt := initCmd.Flags().Bool("localnet", false,
"initialize working directory for localnet for developers")
"initialize working directory for localnet (for development)")

restoreOpt := initCmd.Flags().String("restore", "", "restore the 'default_wallet' using a mnemonic or seed phrase")
restoreOpt := initCmd.Flags().String("restore", "",
"restore the 'default_wallet' using a mnemonic or seed phrase")

passwordOpt := initCmd.Flags().StringP("password", "p", "", "the wallet password")
passwordOpt := initCmd.Flags().StringP("password", "p", "",
"the wallet password")

entropyOpt := initCmd.Flags().IntP("entropy", "e", 128, "entropy bits for seed generation. range: 128 to 256")
entropyOpt := initCmd.Flags().IntP("entropy", "e", 128,
"entropy bits for seed generation. range: 128 to 256")

valNumOpt := initCmd.Flags().IntP("val-num", "", 0, "number of validators to be created. range: 1 to 32")
valNumOpt := initCmd.Flags().IntP("val-num", "", 0,
"number of validators to be created. range: 1 to 32")

initCmd.Run = func(_ *cobra.Command, _ []string) {
workingDir, _ := filepath.Abs(*workingDirOpt)
Expand Down
10 changes: 6 additions & 4 deletions cmd/daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ func buildStartCmd(parentCmd *cobra.Command) {

parentCmd.AddCommand(startCmd)

workingDirOpt := startCmd.Flags().StringP("working-dir", "w",
cmd.PactusDefaultHomeDir(), "A path to the working directory to read the wallet and node files")
workingDirOpt := startCmd.Flags().StringP("working-dir", "w", cmd.PactusDefaultHomeDir(),
"the path to the working directory to load the wallet and node files")

passwordOpt := startCmd.Flags().StringP("password", "p", "", "The wallet password")
passwordOpt := startCmd.Flags().StringP("password", "p", "",
"the wallet password")

pprofOpt := startCmd.Flags().String("pprof", "", "debug pprof server address(not recommended to expose to internet)")
pprofOpt := startCmd.Flags().String("pprof", "",
"pprof server address (for debugging)")

startCmd.Run = func(_ *cobra.Command, _ []string) {
workingDir, _ := filepath.Abs(*workingDirOpt)
Expand Down
2 changes: 1 addition & 1 deletion cmd/gtk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var (
func init() {
workingDirOpt = flag.String("working-dir", cmd.PactusDefaultHomeDir(), "working directory path")
passwordOpt = flag.String("password", "", "wallet password")
testnetOpt = flag.Bool("testnet", true, "initializing for the testnet") // TODO: make it false after mainnet launch
testnetOpt = flag.Bool("testnet", false, "initializing for the testnet")

gtk.Init(nil)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/wallet/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func buildRecoverCmd(parentCmd *cobra.Command) {
parentCmd.AddCommand(recoverCmd)

passOpt := addPasswordOption(recoverCmd)
testnetOpt := recoverCmd.Flags().Bool("testnet", true,
testnetOpt := recoverCmd.Flags().Bool("testnet", false,
"recover the wallet for the testnet environment")
seedOpt := recoverCmd.Flags().StringP("seed", "s", "", "mnemonic or seed phrase used for wallet recovery")

Expand Down
28 changes: 19 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ type NodeConfig struct {
}

func DefaultNodeConfig() *NodeConfig {
// TODO: We should have default config per network: Testnet, Mainnet.
return &NodeConfig{}
}

Expand Down Expand Up @@ -80,6 +79,21 @@ func defaultConfig() *Config {

func DefaultConfigMainnet() *Config {
conf := defaultConfig()
conf.Network.DefaultBootstrapAddrStrings = []string{}
conf.Network.MaxConns = 64
conf.Network.EnableNATService = false
conf.Network.EnableUPnP = false
conf.Network.EnableRelay = true
conf.Network.NetworkName = "pactus"
conf.Network.DefaultPort = 21888
conf.GRPC.Enable = false
conf.GRPC.Listen = "127.0.0.1:50051"
conf.GRPC.Gateway.Enable = false
conf.GRPC.Gateway.Listen = "127.0.0.1:8080"
conf.HTTP.Enable = false
conf.HTTP.Listen = "127.0.0.1:80"
conf.Nanomsg.Enable = false
conf.Nanomsg.Listen = "tcp://127.0.0.1:40899"

return conf
}
Expand All @@ -99,15 +113,11 @@ func DefaultConfigTestnet() *Config {
"/ip4/135.181.42.222/tcp/21777/p2p/12D3KooWC4fe6mQnA9RkSoDdQnNPoSiMUWoqgBsmG8DpkP7yPEMK", // NodeSync.Top (lthuan2011@gmail.com)
"/ip4/95.217.89.202/tcp/21777/p2p/12D3KooWMsi5oYkbbpyyXctmPXzF8UZu2pCvKPRZGyvymhN9BzTD", // CodeBlockLabs (emailbuatcariduit@gmail.com)
}
conf.Network.DefaultRelayAddrStrings = []string{
"/ip4/139.162.153.10/tcp/4002/p2p/12D3KooWNR79jqHVVNhNVrqnDbxbJJze4VjbEsBjZhz6mkvinHAN",
"/ip4/188.121.102.178/tcp/4002/p2p/12D3KooWCRHn8vjrKNBEQcut8uVCYX5q77RKidPaE6iMK31qEVHb",
}
conf.Network.MaxConns = 64
conf.Network.EnableNATService = false
conf.Network.EnableUPnP = false
conf.Network.EnableRelay = true
conf.Network.NetworkName = "pactus-testnet-v2"
conf.Network.NetworkName = "pactus-testnet"
conf.Network.DefaultPort = 21777
conf.GRPC.Enable = true
conf.GRPC.Listen = "[::]:50052"
Expand All @@ -116,7 +126,7 @@ func DefaultConfigTestnet() *Config {
conf.HTTP.Enable = false
conf.HTTP.Listen = "[::]:80"
conf.Nanomsg.Enable = false
conf.Nanomsg.Listen = "tcp://127.0.0.1:40799"
conf.Nanomsg.Listen = "tcp://[::]:40799"

return conf
}
Expand All @@ -134,10 +144,10 @@ func DefaultConfigLocalnet() *Config {
conf.GRPC.Listen = "[::]:0"
conf.GRPC.Gateway.Enable = true
conf.GRPC.Gateway.Listen = "[::]:0"
conf.HTTP.Enable = false
conf.HTTP.Enable = true
conf.HTTP.Listen = "[::]:0"
conf.Nanomsg.Enable = true
conf.Nanomsg.Listen = "tcp://127.0.0.1:0"
conf.Nanomsg.Listen = "tcp://[::]:0"

return conf
}
Expand Down
84 changes: 72 additions & 12 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,99 @@ func TestSaveMainnetConfig(t *testing.T) {
assert.NoError(t, err)

assert.NoError(t, conf.BasicCheck())
assert.Equal(t, defConf, conf)

confData, _ := util.ReadFile(path)
exampleData, _ := util.ReadFile("example_config.toml")
assert.Equal(t, confData, exampleData)
}

func TestSaveConfig(t *testing.T) {
func TestSaveTestnetConfig(t *testing.T) {
path := util.TempFilePath()
conf := defaultConfig()
assert.NoError(t, conf.Save(path))

defConf := DefaultConfigTestnet()
assert.NoError(t, defConf.Save(path))

conf, err := LoadFromFile(path, true, defConf)
assert.NoError(t, err)
assert.Equal(t, defConf, conf)

assert.NoError(t, conf.BasicCheck())
assert.Equal(t, conf.Network.NetworkName, "pactus-testnet-v2")
assert.Equal(t, conf.Network.DefaultPort, 21777)
}

func TestLocalnetConfig(t *testing.T) {
conf := DefaultConfigLocalnet()
func TestDefaultConfig(t *testing.T) {
conf := defaultConfig()

assert.NoError(t, conf.BasicCheck())
assert.Empty(t, conf.Network.ListenAddrStrings)
assert.Equal(t, conf.Network.NetworkName, "pactus-localnet")
assert.Equal(t, conf.Network.DefaultPort, 21666)
assert.Equal(t, conf.Network.NetworkName, "")
assert.Equal(t, conf.Network.DefaultPort, 0)

assert.False(t, conf.GRPC.Enable)
assert.False(t, conf.GRPC.Gateway.Enable)
assert.False(t, conf.HTTP.Enable)
assert.False(t, conf.Nanomsg.Enable)

assert.Equal(t, conf.GRPC.Listen, "")
assert.Equal(t, conf.GRPC.Gateway.Listen, "")
assert.Equal(t, conf.HTTP.Listen, "")
assert.Equal(t, conf.Nanomsg.Listen, "")
}

func TestMainnetConfig(t *testing.T) {
conf := DefaultConfigMainnet()

assert.NoError(t, conf.BasicCheck())
assert.Empty(t, conf.Network.ListenAddrStrings)
assert.Equal(t, conf.Network.NetworkName, "pactus")
assert.Equal(t, conf.Network.DefaultPort, 21888)

assert.False(t, conf.GRPC.Enable)
assert.False(t, conf.GRPC.Gateway.Enable)
assert.False(t, conf.HTTP.Enable)
assert.False(t, conf.Nanomsg.Enable)

assert.Equal(t, conf.GRPC.Listen, "127.0.0.1:50051")
assert.Equal(t, conf.GRPC.Gateway.Listen, "127.0.0.1:8080")
assert.Equal(t, conf.HTTP.Listen, "127.0.0.1:80")
assert.Equal(t, conf.Nanomsg.Listen, "tcp://127.0.0.1:40899")
}

func TestTestnetConfig(t *testing.T) {
conf := DefaultConfigTestnet()

assert.NoError(t, conf.BasicCheck())
assert.NotEmpty(t, conf.Network.DefaultRelayAddrStrings)
assert.Empty(t, conf.Network.ListenAddrStrings)
assert.Equal(t, conf.Network.NetworkName, "pactus-testnet-v2")
assert.Equal(t, conf.Network.NetworkName, "pactus-testnet")
assert.Equal(t, conf.Network.DefaultPort, 21777)

assert.True(t, conf.GRPC.Enable)
assert.True(t, conf.GRPC.Gateway.Enable)
assert.False(t, conf.HTTP.Enable)
assert.False(t, conf.Nanomsg.Enable)

assert.Equal(t, conf.GRPC.Listen, "[::]:50052")
assert.Equal(t, conf.GRPC.Gateway.Listen, "[::]:8080")
assert.Equal(t, conf.HTTP.Listen, "[::]:80")
assert.Equal(t, conf.Nanomsg.Listen, "tcp://[::]:40799")
}

func TestLocalnetConfig(t *testing.T) {
conf := DefaultConfigLocalnet()

assert.NoError(t, conf.BasicCheck())
assert.Empty(t, conf.Network.ListenAddrStrings)
assert.Equal(t, conf.Network.NetworkName, "pactus-localnet")
assert.Equal(t, conf.Network.DefaultPort, 21666)

assert.True(t, conf.GRPC.Enable)
assert.True(t, conf.GRPC.Gateway.Enable)
assert.True(t, conf.HTTP.Enable)
assert.True(t, conf.Nanomsg.Enable)

assert.Equal(t, conf.GRPC.Listen, "[::]:0")
assert.Equal(t, conf.GRPC.Gateway.Listen, "[::]:0")
assert.Equal(t, conf.HTTP.Listen, "[::]:0")
assert.Equal(t, conf.Nanomsg.Listen, "tcp://[::]:0")
}

func TestLoadFromFile(t *testing.T) {
Expand Down
18 changes: 9 additions & 9 deletions config/example_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@

# `enable_relay` indicates whether relay service should be enabled or not.
# Relay service is a transport protocol that routes traffic between two peers over a third-party “relay” peer.
# Default is `false`.
## enable_relay = false
# Default is `true`.
## enable_relay = true

# `enable_relay_service` indicates whether relay service should be enabled or not.
# Relay service is a transport protocol that enables peers to discover each other on the peer-to-peer network.
Expand Down Expand Up @@ -124,10 +124,10 @@
# "trace", "debug", "info", "warn", and "error".
[logger.levels]
## _consensus = "warn"
## _grpc = "error"
## _http = "error"
## _grpc = "info"
## _http = "info"
## _network = "error"
## _nonomsg = "error"
## _nonomsg = "info"
## _pool = "error"
## _state = "info"
## _sync = "error"
Expand All @@ -145,7 +145,7 @@
## enable_wallet = false

# `listen` is the address to listen for incoming connections for gRPC server.
## listen = "50051"
## listen = "127.0.0.1:50051"

# `grpc.gateway` contains configuration for the gRPC Gateway server
# which translates a RESTful HTTP API into gRPC.
Expand All @@ -156,7 +156,7 @@
## enable = false

# `listen` is the address to listen for incoming connections for gRPCGateway server.
## listen = "8080"
## listen = "127.0.0.1:8080"

# `enable_cors` indicates whether Cross-Origin Request Sharing (CORS)
# protection should be enabled or not.
Expand All @@ -171,7 +171,7 @@
## enable = false

# `listen` is the address address to listen for incoming connections for HTTP server.
## listen = "80"
## listen = "127.0.0.1:80"

# Nanomsg configuration.
[nanomsg]
Expand All @@ -181,4 +181,4 @@
## enable = false

# `listen` is the address to listen for incoming connections for nanomsg server.
## listen = "40899"
## listen = "tcp://127.0.0.1:40899"
17 changes: 11 additions & 6 deletions genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,12 @@ func (gen *Genesis) Params() *param.Params {
}

func (gen *Genesis) Accounts() map[crypto.Address]*account.Account {
accs := make(map[crypto.Address]*account.Account, 0)
accs := make(map[crypto.Address]*account.Account)
for i, genAcc := range gen.data.Accounts {
addr, _ := crypto.AddressFromString(genAcc.Address)
addr, err := crypto.AddressFromString(genAcc.Address)
if err != nil {
panic(err)
}
acc := account.NewAccount(int32(i))
acc.AddToBalance(genAcc.Balance)
accs[addr] = acc
Expand Down Expand Up @@ -183,10 +186,12 @@ func (gen *Genesis) TotalSupply() int64 {
}

func (gen *Genesis) ChainType() ChainType {
if gen.Hash() == TestnetGenesis().Hash() {
switch gen.Hash() {
case MainnetGenesis().Hash():
return Mainnet
case TestnetGenesis().Hash():
return Testnet
default:
return Localnet
}
// TODO: add mainnet here

return Localnet
}
Loading

0 comments on commit 5309800

Please sign in to comment.