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

feat(wallet): create single ed25519 reward address for all validators #1570

Merged
merged 5 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
55 changes: 31 additions & 24 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,31 +294,29 @@ func TrapSignal(cleanupFunc func()) {

func CreateNode(numValidators int, chain genesis.ChainType, workingDir string,
mnemonic string, walletPassword string,
) ([]string, []string, error) {
) ([]string, string, error) {
// To make process faster, we update the password after creating the addresses
walletPath := PactusDefaultWalletPath(workingDir)
walletInstance, err := wallet.Create(walletPath, mnemonic, "", chain)
if err != nil {
return nil, nil, err
return nil, "", err
}

validatorAddrs := []string{}
for i := 0; i < numValidators; i++ {
addressInfo, err := walletInstance.NewValidatorAddress(fmt.Sprintf("Validator address %v", i+1))
if err != nil {
return nil, nil, err
return nil, "", err
}
validatorAddrs = append(validatorAddrs, addressInfo.Address)
}

rewardAddrs := []string{}
for i := 0; i < numValidators; i++ {
addressInfo, err := walletInstance.NewBLSAccountAddress(fmt.Sprintf("Reward address %v", i+1))
if err != nil {
return nil, nil, err
}
rewardAddrs = append(rewardAddrs, addressInfo.Address)
addressInfo, err := walletInstance.NewEd25519AccountAddress(
"Reward address", "")
if err != nil {
return nil, "", err
}
rewardAddr := addressInfo.Address

confPath := PactusConfigPath(workingDir)
genPath := PactusGenesisPath(workingDir)
Expand All @@ -327,51 +325,51 @@ func CreateNode(numValidators int, chain genesis.ChainType, workingDir string,
case genesis.Mainnet:
genDoc := genesis.MainnetGenesis()
if err := genDoc.SaveToFile(genPath); err != nil {
return nil, nil, err
return nil, "", err
}
err := config.SaveMainnetConfig(confPath)
if err != nil {
return nil, nil, err
return nil, "", err
}
case genesis.Testnet:
genDoc := genesis.TestnetGenesis()
if err := genDoc.SaveToFile(genPath); err != nil {
return nil, nil, err
return nil, "", err
}
conf := config.DefaultConfigTestnet()
if err := conf.Save(confPath); err != nil {
return nil, nil, err
return nil, "", err
}

case genesis.Localnet:
if numValidators < 4 {
return nil, nil, fmt.Errorf("LocalNeed needs at least 4 validators")
return nil, "", fmt.Errorf("LocalNeed needs at least 4 validators")
}
genDoc := makeLocalGenesis(*walletInstance)
if err := genDoc.SaveToFile(genPath); err != nil {
return nil, nil, err
return nil, "", err
}

conf := config.DefaultConfigLocalnet()
if err := conf.Save(confPath); err != nil {
return nil, nil, err
return nil, "", err
}
}

if err := walletInstance.UpdatePassword("", walletPassword); err != nil {
return nil, nil, err
return nil, "", err
}

if err := walletInstance.Save(); err != nil {
return nil, nil, err
return nil, "", err
}

return validatorAddrs, rewardAddrs, nil
return validatorAddrs, rewardAddr, nil
}

// StartNode starts the node from the given working directory.
// The passwordFetcher will be used to fetch the password for the default_wallet if it is encrypted.
// It returns an error if the genesis doc or default_wallet can't be found inside the working directory.
// It returns an error if the genesis doc or default_wallet can't be found inside the working directory.1234
akbariandev marked this conversation as resolved.
Show resolved Hide resolved
// TODO: write test for me.
func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, bool)) (
*node.Node, *wallet.Wallet, error,
Expand Down Expand Up @@ -433,7 +431,7 @@ func makeLocalGenesis(wlt wallet.Wallet) *genesis.Genesis {
genValNum := 4
vals := make([]*validator.Validator, genValNum)
for i := 0; i < genValNum; i++ {
info := wlt.AddressInfo(wlt.AddressInfos()[i].Address)
info := wlt.AddressInfo(wlt.AllValidatorAddresses()[i].Address)
pub, _ := bls.PublicKeyFromString(info.PublicKey)
vals[i] = validator.NewValidator(pub, int32(i))
}
Expand Down Expand Up @@ -586,8 +584,17 @@ func MakeRewardAddresses(walletInstance *wallet.Wallet,

addrInfo := walletInstance.AddressFromPath(accAddrPath.String())
if addrInfo == nil {
return nil, fmt.Errorf("unable to find reward address for: %s [%s]",
valAddrsInfo[i].Address, accAddrPath)
accAddrPath = addresspath.NewPath(
vault.PurposeBIP44+hdkeychain.HardenedKeyStart,
valAddrPath.CoinType(),
uint32(crypto.AddressTypeEd25519Account)+hdkeychain.HardenedKeyStart,
0+hdkeychain.HardenedKeyStart)

addrInfo = walletInstance.AddressFromPath(accAddrPath.String())
if addrInfo == nil {
return nil, fmt.Errorf("unable to find reward address for: %s [%s]",
valAddrsInfo[i].Address, accAddrPath)
}
}

addr, _ := crypto.AddressFromString(addrInfo.Address)
Expand Down
19 changes: 7 additions & 12 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func TestCreateNode(t *testing.T) {
mnemonic string
withErr bool
validatorAddrs []string
rewardAddrs []string
rewardAddrs string
}{
{
name: "Create node for Mainnet",
Expand All @@ -308,7 +308,7 @@ func TestCreateNode(t *testing.T) {
workingDir: util.TempDirPath(),
mnemonic: "legal winner thank year wave sausage worth useful legal winner thank yellow",
validatorAddrs: []string{"pc1pqpu5tkuctj6ecxjs85f9apm802hhc65amwhuyw"},
rewardAddrs: []string{"pc1zmpnme0xrgzhml77e3k70ey9hwwwsfed6l04pqc"},
rewardAddrs: "pc1rkg0nhswqj85wnz9sm0g9kfkxj68lfx9lhftl8n",
withErr: false,
},
{
Expand All @@ -318,7 +318,7 @@ func TestCreateNode(t *testing.T) {
workingDir: util.TempDirPath(),
mnemonic: "legal winner thank year wave sausage worth useful legal winner thank yellow",
validatorAddrs: []string{"tpc1p54ex6jvqkz6qyld5wgm77qm7walgy664hxz2pc"},
rewardAddrs: []string{"tpc1zlkjrgfkrh7f9enpt730tp5vgx7tgtqzplhfksa"},
rewardAddrs: "tpc1rps3xncfvepre5w754xtxxqmrmhwuackjvaft5y",
withErr: false,
},

Expand All @@ -334,13 +334,8 @@ func TestCreateNode(t *testing.T) {
"tpc1pe5px2dddn6g4zgnu3wpwgrqpdjrufvda57a4wm",
"tpc1p8yyhysp380j9q9gxa6vlhstgkd94238kunttpr",
},
rewardAddrs: []string{
"tpc1zlkjrgfkrh7f9enpt730tp5vgx7tgtqzplhfksa",
"tpc1ztzwc9x98j88wctmzm5t09z592lqw0sqc3rn6lu",
"tpc1zslef8hjkwqxdcekcqxra6djgjr5gryrj8l3fyf",
"tpc1zru3xxmgz5dqqkv0mesqq3t3luepzg3e6jeqkeu",
},
withErr: false,
rewardAddrs: "tpc1rps3xncfvepre5w754xtxxqmrmhwuackjvaft5y",
withErr: false,
},
{
name: "Localnet with one validator",
Expand All @@ -349,7 +344,7 @@ func TestCreateNode(t *testing.T) {
workingDir: util.TempDirPath(),
mnemonic: "legal winner thank year wave sausage worth useful legal winner thank yellow",
validatorAddrs: nil,
rewardAddrs: nil,
rewardAddrs: "",
withErr: true,
},
{
Expand All @@ -359,7 +354,7 @@ func TestCreateNode(t *testing.T) {
workingDir: util.TempDirPath(),
mnemonic: "",
validatorAddrs: nil,
rewardAddrs: nil,
rewardAddrs: "",
withErr: true,
},
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/daemon/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ func buildInitCmd(parentCmd *cobra.Command) {
cmd.PrintLine()

cmd.PrintInfoMsgBoldf("Reward addresses:")
akbariandev marked this conversation as resolved.
Show resolved Hide resolved
for i, addr := range rewardAddrs {
cmd.PrintInfoMsgf("%v- %s", i+1, addr)
}
cmd.PrintInfoMsgf("%s", rewardAddrs)

cmd.PrintLine()
cmd.PrintInfoMsgBoldf("Network: %v", chain.String())
Expand Down
4 changes: 1 addition & 3 deletions cmd/gtk/startup_assistant.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,7 @@ func startupAssistant(workingDir string, chainType genesis.ChainType) bool {
}

nodeInfo += "\nReward addresses:\n"
akbariandev marked this conversation as resolved.
Show resolved Hide resolved
for i, addr := range rewardAddrs {
nodeInfo += fmt.Sprintf("%v- %s\n", i+1, addr)
}
nodeInfo += fmt.Sprintf("%s", rewardAddrs)

setTextViewContent(txtNodeInfo, nodeInfo)
}
Expand Down
Loading