-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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(x/genutil)!: bulk add genesis accounts #21372
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d7fccac
feat(genutil): `AddGenesisAccount` with cache
Reecepbcups 670282d
test: bulk accounts including append
Reecepbcups e15b4ae
remove todo
Reecepbcups aab13de
Merge branch 'main' into reece/bulk-genesis-add-accounts
Reecepbcups 217e1b6
lint
Reecepbcups bd9cfa6
`AddGenesisAccounts` comment
Reecepbcups a4a0fe5
lint: gosec
Reecepbcups 25d9719
chore: move changelog entry
Reecepbcups 3cc0ec3
fix: address codec & example field
Reecepbcups 0c4aaa6
Merge branch 'main' into reece/bulk-genesis-add-accounts
Reecepbcups fb37cad
Merge branch 'main' into reece/bulk-genesis-add-accounts
julienrbrt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,9 @@ package cli | |
|
||
import ( | ||
"bufio" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
|
@@ -71,7 +73,33 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa | |
vestingAmtStr, _ := cmd.Flags().GetString(flagVestingAmt) | ||
moduleNameStr, _ := cmd.Flags().GetString(flagModuleName) | ||
|
||
return genutil.AddGenesisAccount(clientCtx.Codec, clientCtx.AddressCodec, addr, appendflag, config.GenesisFile(), args[1], vestingAmtStr, vestingStart, vestingEnd, moduleNameStr) | ||
addrStr, err := addressCodec.BytesToString(addr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
coins, err := sdk.ParseCoinsNormalized(args[1]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
vestingAmt, err := sdk.ParseCoinsNormalized(vestingAmtStr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
accounts := []genutil.GenesisAccount{ | ||
{ | ||
Address: addrStr, | ||
Coins: coins, | ||
VestingAmt: vestingAmt, | ||
VestingStart: vestingStart, | ||
VestingEnd: vestingEnd, | ||
ModuleName: moduleNameStr, | ||
}, | ||
} | ||
|
||
return genutil.AddGenesisAccounts(clientCtx.Codec, clientCtx.AddressCodec, accounts, appendflag, config.GenesisFile()) | ||
}, | ||
} | ||
|
||
|
@@ -85,3 +113,66 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa | |
|
||
return cmd | ||
} | ||
|
||
// AddBulkGenesisAccountCmd returns bulk-add-genesis-account cobra Command. | ||
// This command is provided as a default, applications are expected to provide their own command if custom genesis accounts are needed. | ||
func AddBulkGenesisAccountCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "bulk-add-genesis-account [/file/path.json]", | ||
Short: "Bulk add genesis accounts to genesis.json", | ||
Example: `bulk-add-genesis-account accounts.json | ||
|
||
where accounts.json is: | ||
|
||
[ | ||
{ | ||
"address": "cosmos139f7kncmglres2nf3h4hc4tade85ekfr8sulz5", | ||
"coins": [ | ||
{ "denom": "umuon", "amount": "100000000" }, | ||
{ "denom": "stake", "amount": "200000000" } | ||
] | ||
}, | ||
{ | ||
"address": "cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg", | ||
"coins": [ | ||
{ "denom": "umuon", "amount": "500000000" } | ||
], | ||
"vesting_amt": [ | ||
{ "denom": "umuon", "amount": "400000000" } | ||
], | ||
"vesting_start": 1724711478, | ||
"vesting_end": 1914013878 | ||
} | ||
] | ||
`, | ||
Long: `Add genesis accounts in bulk to genesis.json. The provided account must specify | ||
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. Can we add the Example field from cobra and show how the file should look like? 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. |
||
the account address and a list of initial coins. The list of initial tokens must | ||
contain valid denominations. Accounts may optionally be supplied with vesting parameters. | ||
`, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
clientCtx := client.GetClientContextFromCmd(cmd) | ||
config := client.GetConfigFromCmd(cmd) | ||
|
||
f, err := os.Open(args[0]) | ||
if err != nil { | ||
return fmt.Errorf("failed to open file: %w", err) | ||
} | ||
defer f.Close() | ||
|
||
var accounts []genutil.GenesisAccount | ||
if err := json.NewDecoder(f).Decode(&accounts); err != nil { | ||
return fmt.Errorf("failed to decode JSON: %w", err) | ||
} | ||
|
||
appendflag, _ := cmd.Flags().GetBool(flagAppendMode) | ||
|
||
return genutil.AddGenesisAccounts(clientCtx.Codec, clientCtx.AddressCodec, accounts, appendflag, config.GenesisFile()) | ||
}, | ||
} | ||
|
||
cmd.Flags().Bool(flagAppendMode, false, "append the coins to an account already in the genesis.json file") | ||
flags.AddQueryFlagsToCmd(cmd) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Ensure proper validation for GenesisAccount fields.
The
GenesisAccount
struct is well-defined. However, consider adding validation methods to ensure that fields likeAddress
andCoins
are valid.Implement validation methods within the
GenesisAccount
struct to ensure data integrity.