-
Notifications
You must be signed in to change notification settings - Fork 72
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 init-from-state cmd #264
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -52,7 +52,7 @@ const ( | |||||||||||||||||||||||||||||||||
FlagIncreaseCoinAmount = "increase-coin-amount" | ||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
// bitsongd init-from-state v021 export-v021.json v021 --old-moniker Cosmostation --old-account-addr bitsong1wf3q0a3uzechxvf27reuqts8nqm45sn29ykncv --increase-coin-amount 10000000000000000ubtsg --o | ||||||||||||||||||||||||||||||||||
// bitsongd init-from-state v021 export-v021.json v021 --old-moniker Cosmostation --old-account-addr bitsong1wf3q0a3uzechxvf27reuqts8nqm45sn29ykncv --increase-coin-amount 10000000000000000 -o | ||||||||||||||||||||||||||||||||||
// InitFromStateCmd returns a command that initializes all files needed for Tendermint | ||||||||||||||||||||||||||||||||||
// and the respective application. | ||||||||||||||||||||||||||||||||||
func InitFromStateCmd(defaultNodeHome string) *cobra.Command { | ||||||||||||||||||||||||||||||||||
|
@@ -114,7 +114,7 @@ func InitFromStateCmd(defaultNodeHome string) *cobra.Command { | |||||||||||||||||||||||||||||||||
increaseCoinAmount, _ := cmd.Flags().GetInt64(FlagIncreaseCoinAmount) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
// attempt to lookup address from Keybase if no address was provided | ||||||||||||||||||||||||||||||||||
kb, err := keyring.New(sdk.KeyringServiceName(), "test", clientCtx.HomeDir, bufio.NewReader(cmd.InOrStdin()), clientCtx.Codec) | ||||||||||||||||||||||||||||||||||
kb, err := keyring.New(sdk.KeyringServiceName(), "os", clientCtx.HomeDir, bufio.NewReader(cmd.InOrStdin()), clientCtx.Codec) | ||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||
return fmt.Errorf("failed to open keyring: %w", err) | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
@@ -346,18 +346,27 @@ func ConvertStateExport(clientCtx client.Context, params StateExportParams) (*tm | |||||||||||||||||||||||||||||||||
// Impersonate validator | ||||||||||||||||||||||||||||||||||
var oldValidator tmtypes.GenesisValidator | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
// Update tendermint validator data | ||||||||||||||||||||||||||||||||||
// Filter the validators to keep only the one matching params.OldMoniker | ||||||||||||||||||||||||||||||||||
var updatedValidators []tmtypes.GenesisValidator | ||||||||||||||||||||||||||||||||||
for i := range genDoc.Validators { | ||||||||||||||||||||||||||||||||||
if genDoc.Validators[i].Name == params.OldMoniker { | ||||||||||||||||||||||||||||||||||
oldValidator = genDoc.Validators[i] | ||||||||||||||||||||||||||||||||||
// Update the matching validator's data | ||||||||||||||||||||||||||||||||||
validator := &genDoc.Validators[i] | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
// Replace validator data | ||||||||||||||||||||||||||||||||||
validator.PubKey = params.TmPubKey | ||||||||||||||||||||||||||||||||||
validator.Address = params.TmPubKey.Address() | ||||||||||||||||||||||||||||||||||
validator.Power = validator.Power + (params.IncreaseCoinAmount / 1000000) | ||||||||||||||||||||||||||||||||||
validator.Power = validator.Power + (params.IncreaseCoinAmount) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
// Add the updated validator to the new slice | ||||||||||||||||||||||||||||||||||
updatedValidators = append(updatedValidators, *validator) | ||||||||||||||||||||||||||||||||||
break // Exit loop after finding and updating the matching validator | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
// Replace the original validators slice with the updated slice | ||||||||||||||||||||||||||||||||||
genDoc.Validators = updatedValidators | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
if oldValidator.Name == "" { | ||||||||||||||||||||||||||||||||||
return nil, fmt.Errorf("validator to replace %s not found", params.OldMoniker) | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
@@ -389,14 +398,15 @@ func ConvertStateExport(clientCtx client.Context, params StateExportParams) (*tm | |||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
// Update total power | ||||||||||||||||||||||||||||||||||
for i := range stakingGenState.LastValidatorPowers { | ||||||||||||||||||||||||||||||||||
validatorPower := &stakingGenState.LastValidatorPowers[i] | ||||||||||||||||||||||||||||||||||
if validatorPower.Address == operatorAddr { | ||||||||||||||||||||||||||||||||||
validatorPower.Power = validatorPower.Power + (params.IncreaseCoinAmount / 1000000) | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
// Update LastValidatorPowers | ||||||||||||||||||||||||||||||||||
stakingGenState.LastValidatorPowers = []stakingtypes.LastValidatorPower{ | ||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||
Address: operatorAddr, | ||||||||||||||||||||||||||||||||||
Power: updatedValidators[0].Power, | ||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
stakingGenState.LastTotalPower = stakingGenState.LastTotalPower.Add(sdk.NewInt(params.IncreaseCoinAmount / 1000000)) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
Comment on lines
+401
to
+408
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix potential panic in LastValidatorPowers update. The code assumes Apply this safer implementation: - stakingGenState.LastValidatorPowers = []stakingtypes.LastValidatorPower{
- {
- Address: operatorAddr,
- Power: updatedValidators[0].Power,
- },
- }
+ if len(updatedValidators) == 0 {
+ return nil, fmt.Errorf("no validator found with moniker %s", params.OldMoniker)
+ }
+ stakingGenState.LastValidatorPowers = []stakingtypes.LastValidatorPower{{
+ Address: operatorAddr,
+ Power: updatedValidators[0].Power,
+ }} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||
stakingGenState.LastTotalPower = sdk.NewInt(updatedValidators[0].Power) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
// Update self delegation on operator address | ||||||||||||||||||||||||||||||||||
for i := range stakingGenState.Delegations { | ||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improve validator update logic to handle edge cases.
The current implementation has several potential issues:
updatedValidators
could be problematic if multiple validators share the same moniker.Consider this safer implementation:
📝 Committable suggestion