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

fix: defining ChainType in genesis to detect the type of network #483

Merged
merged 1 commit into from
May 24, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 0 additions & 9 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,6 @@ and then start the node:
./pactus-daemon start -w=<working_dir>
```

### Local net

You can create a local node with one validator to test Pactus on your machine:

```text
./pactus-daemon init -w=<working_dir> --localnet
./pactus-daemon start -w=<working_dir>
```

## What is pactus-wallet?

Pactus wallet is a native wallet in the Pactus blockchain that lets users easily manage
Expand Down
50 changes: 29 additions & 21 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/pactus-project/pactus/config"
"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/genesis"
"github.com/pactus-project/pactus/node"
"github.com/pactus-project/pactus/wallet"
)

Expand Down Expand Up @@ -253,16 +254,12 @@ func TrapSignal(cleanupFunc func()) {
}()
}

func CreateNode(numValidators int, testnet bool, workingDir string,
func CreateNode(numValidators int, chain genesis.ChainType, workingDir string,
mnemonic string, walletPassword string) (
validatorAddrs []string, rewardAddrs []string, err error) {
// To make process faster, we update the password after creating the addresses
network := wallet.NetworkMainNet
if testnet {
network = wallet.NetworkTestNet
}
walletPath := PactusDefaultWalletPath(workingDir)
wallet, err := wallet.Create(walletPath, mnemonic, "", network)
wallet, err := wallet.Create(walletPath, mnemonic, "", chain)
if err != nil {
return nil, nil, err
}
Expand All @@ -286,8 +283,9 @@ func CreateNode(numValidators int, testnet bool, workingDir string,
confPath := PactusConfigPath(workingDir)
genPath := PactusGenesisPath(workingDir)

if testnet {
err = genesis.Testnet().SaveToFile(genPath)
switch chain {
case genesis.Testnet:
err = genesis.TestnetGenesis().SaveToFile(genPath)
if err != nil {
return nil, nil, err
}
Expand All @@ -296,7 +294,7 @@ func CreateNode(numValidators int, testnet bool, workingDir string,
if err != nil {
return nil, nil, err
}
} else {
case genesis.Mainnet:
panic("not yet!")
}

Expand All @@ -313,14 +311,14 @@ func CreateNode(numValidators int, testnet bool, workingDir string,
return validatorAddrs, rewardAddrs, nil
}

func GetKeys(workingDir string, passwordFetcher func(*wallet.Wallet) (string, bool)) (
*genesis.Genesis, *config.Config, []crypto.Signer, []crypto.Address, *wallet.Wallet, error) {
func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, bool)) (
*node.Node, *wallet.Wallet, error) {
gen, err := genesis.LoadFromFile(PactusGenesisPath(workingDir))
if err != nil {
return nil, nil, nil, nil, nil, err
return nil, nil, err
}

if gen.Params().IsTestnet() {
if gen.ChainType().IsTestnet() {
crypto.AddressHRP = "tpc"
crypto.PublicKeyHRP = "tpublic"
crypto.PrivateKeyHRP = "tsecret"
Expand All @@ -330,24 +328,24 @@ func GetKeys(workingDir string, passwordFetcher func(*wallet.Wallet) (string, bo

conf, err := config.LoadFromFile(PactusConfigPath(workingDir))
if err != nil {
return nil, nil, nil, nil, nil, err
return nil, nil, err
}

err = conf.SanityCheck()
if err != nil {
return nil, nil, nil, nil, nil, err
return nil, nil, err
}

walletPath := PactusDefaultWalletPath(workingDir)
wallet, err := wallet.Open(walletPath, true)
if err != nil {
return nil, nil, nil, nil, nil, err
return nil, nil, err
}
addrLabels := wallet.AddressLabels()

// Create signers
if len(addrLabels) < conf.Node.NumValidators {
return nil, nil, nil, nil, nil, fmt.Errorf("not enough addresses in wallet")
return nil, nil, fmt.Errorf("not enough addresses in wallet")
}
validatorAddrs := make([]string, conf.Node.NumValidators)
for i := 0; i < conf.Node.NumValidators; i++ {
Expand All @@ -356,11 +354,11 @@ func GetKeys(workingDir string, passwordFetcher func(*wallet.Wallet) (string, bo
signers := make([]crypto.Signer, conf.Node.NumValidators)
password, ok := passwordFetcher(wallet)
if !ok {
return nil, nil, nil, nil, nil, fmt.Errorf("aborted")
return nil, nil, fmt.Errorf("aborted")
}
prvKeys, err := wallet.PrivateKeys(password, validatorAddrs)
if err != nil {
return nil, nil, nil, nil, nil, err
return nil, nil, err
}
for i, key := range prvKeys {
signers[i] = crypto.NewSigner(key)
Expand All @@ -374,13 +372,23 @@ func GetKeys(workingDir string, passwordFetcher func(*wallet.Wallet) (string, bo
}
} else {
if len(addrLabels) < 2*conf.Node.NumValidators {
return nil, nil, nil, nil, nil, fmt.Errorf("not enough addresses in wallet")
return nil, nil, fmt.Errorf("not enough addresses in wallet")
}
for i := 0; i < conf.Node.NumValidators; i++ {
rewardAddrs[i], _ =
crypto.AddressFromString(addrLabels[conf.Node.NumValidators+i].Address)
}
}

return gen, conf, signers, rewardAddrs, wallet, nil
node, err := node.NewNode(gen, conf, signers, rewardAddrs)
if err != nil {
return nil, nil, err
}

err = node.Start()
if err != nil {
return nil, nil, err
}

return node, wallet, nil
}
9 changes: 8 additions & 1 deletion cmd/daemon/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

cli "github.com/jawher/mow.cli"
"github.com/pactus-project/pactus/cmd"
"github.com/pactus-project/pactus/genesis"
"github.com/pactus-project/pactus/util"
"github.com/pactus-project/pactus/wallet"
)
Expand Down Expand Up @@ -54,7 +55,11 @@ func Init() func(c *cli.Cmd) {
cmd.PrintInfoMsg("You can define validators based on the amount of coins you want to stake.")
numValidators := cmd.PromptInputWithRange("Number of Validators", 7, 1, 32)

validatorAddrs, rewardAddrs, err := cmd.CreateNode(numValidators, *testnetOpt, workingDir, mnemonic, password)
network := genesis.Mainnet
if *testnetOpt {
network = genesis.Testnet
}
validatorAddrs, rewardAddrs, err := cmd.CreateNode(numValidators, network, workingDir, mnemonic, password)
cmd.FatalErrorCheck(err)

cmd.PrintLine()
Expand All @@ -69,6 +74,8 @@ func Init() func(c *cli.Cmd) {
cmd.PrintInfoMsg("%v- %s", i+1, addr)
}

cmd.PrintLine()
cmd.PrintInfoMsgBold("Network: %v", network.String())
cmd.PrintLine()
cmd.PrintSuccessMsg("A pactus node is successfully initialized at %v", workingDir)
cmd.PrintLine()
Expand Down
11 changes: 1 addition & 10 deletions cmd/daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

cli "github.com/jawher/mow.cli"
"github.com/pactus-project/pactus/cmd"
"github.com/pactus-project/pactus/node"
"github.com/pactus-project/pactus/wallet"
)

Expand Down Expand Up @@ -70,18 +69,10 @@ func Start() func(c *cli.Cmd) {
}
return password, true
}
gen, conf, signers, rewardAddrs, _, err := cmd.GetKeys(
node, _, err := cmd.StartNode(
workingDir, passwordFetcher)
cmd.FatalErrorCheck(err)

cmd.PrintLine()

node, err := node.NewNode(gen, conf, signers, rewardAddrs)
cmd.FatalErrorCheck(err)

err = node.Start()
cmd.FatalErrorCheck(err)

cmd.TrapSignal(func() {
node.Stop()
cmd.PrintInfoMsg("Exiting ...")
Expand Down
48 changes: 39 additions & 9 deletions cmd/gtk/assets/ui/widget_node.ui
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<property name="label-xalign">0.019999999552965164</property>
<property name="shadow-type">in</property>
<child>
<!-- n-columns=2 n-rows=5 -->
<!-- n-columns=2 n-rows=6 -->
<object class="GtkGrid">
<property name="width-request">400</property>
<property name="visible">True</property>
Expand Down Expand Up @@ -104,7 +104,7 @@
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">1</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
Expand All @@ -118,7 +118,7 @@
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">1</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
Expand All @@ -133,7 +133,7 @@
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">2</property>
<property name="top-attach">3</property>
</packing>
</child>
<child>
Expand All @@ -147,7 +147,7 @@
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">2</property>
<property name="top-attach">3</property>
</packing>
</child>
<child>
Expand All @@ -162,7 +162,7 @@
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">3</property>
<property name="top-attach">4</property>
</packing>
</child>
<child>
Expand All @@ -176,7 +176,7 @@
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">3</property>
<property name="top-attach">4</property>
</packing>
</child>
<child>
Expand All @@ -191,7 +191,7 @@
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">4</property>
<property name="top-attach">5</property>
</packing>
</child>
<child>
Expand All @@ -204,7 +204,37 @@
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">4</property>
<property name="top-attach">5</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">start</property>
<property name="valign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="label" translatable="yes">Netwotk:</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="id_label_network">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">start</property>
<property name="valign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="selectable">True</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">1</property>
</packing>
</child>
</object>
Expand Down