-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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: make helper function for adding accounts to genesis state #13298
Merged
Merged
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
4f45d36
add genesis account helper
likhita-809 4660787
wip: clean up
likhita-809 fbadc23
fix something
likhita-809 a6dbe27
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 bda760e
wip: move add genesis account helper to auth module
likhita-809 049c208
wip
likhita-809 8373828
add inline doc for AddGenesisAccount func
likhita-809 268c941
address review comments
likhita-809 ae83616
nit
likhita-809 1ef0552
wip: use genesisFileUrl instead of config
likhita-809 10dad63
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 1de8641
add changelog
likhita-809 1b1cc69
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 abfc581
wip: nit
likhita-809 968ac50
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 614fb12
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 fcd4461
wip: apply suggestions
likhita-809 a7cc4a5
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 d86e4d0
address review comments
likhita-809 5c74c6e
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 8d5937a
fix lint issue
likhita-809 ca886bf
fix nit
likhita-809 7b03564
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 62d8a42
fix changelog
likhita-809 0ac6b78
Merge branch 'main' into likhita/add-genesis-account-helper
julienrbrt 98c9b27
Merge branch 'main' into likhita/add-genesis-account-helper
julienrbrt 723f7e4
Merge branch 'main' into likhita/add-genesis-account-helper
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 |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package helpers | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
authvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" | ||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
"github.com/cosmos/cosmos-sdk/x/genutil" | ||
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" | ||
) | ||
|
||
// AddGenesisAccount adds a genesis account to the genesis state. | ||
// Where `cdc` is client codec, `genesisFileUrl` is the path/url of current genesis file, | ||
// `accAddr` is the address to be added to the genesis state, `amountStr` is the list of initial coins | ||
// to be added for the account, `appendAcct` updates the account if already exists. | ||
// `vestingStart, vestingEnd and vestingAmtStr` respectively are the schedule start time, end time (unix epoch) | ||
// and coins to be appended to the account already in the genesis.json file. | ||
func AddGenesisAccount( | ||
cdc codec.Codec, | ||
accAddr sdk.AccAddress, | ||
appendAcct bool, | ||
genesisFileUrl, amountStr, vestingAmtStr string, | ||
vestingStart, vestingEnd int64, | ||
) error { | ||
coins, err := sdk.ParseCoinsNormalized(amountStr) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse coins: %w", err) | ||
} | ||
|
||
vestingAmt, err := sdk.ParseCoinsNormalized(vestingAmtStr) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse vesting amount: %w", err) | ||
} | ||
|
||
// create concrete account type based on input parameters | ||
var genAccount authtypes.GenesisAccount | ||
|
||
balances := banktypes.Balance{Address: accAddr.String(), Coins: coins.Sort()} | ||
baseAccount := authtypes.NewBaseAccount(accAddr, nil, 0, 0) | ||
|
||
if !vestingAmt.IsZero() { | ||
baseVestingAccount := authvesting.NewBaseVestingAccount(baseAccount, vestingAmt.Sort(), vestingEnd) | ||
|
||
if (balances.Coins.IsZero() && !baseVestingAccount.OriginalVesting.IsZero()) || | ||
baseVestingAccount.OriginalVesting.IsAnyGT(balances.Coins) { | ||
return errors.New("vesting amount cannot be greater than total amount") | ||
} | ||
|
||
switch { | ||
case vestingStart != 0 && vestingEnd != 0: | ||
genAccount = authvesting.NewContinuousVestingAccountRaw(baseVestingAccount, vestingStart) | ||
|
||
case vestingEnd != 0: | ||
genAccount = authvesting.NewDelayedVestingAccountRaw(baseVestingAccount) | ||
|
||
default: | ||
return errors.New("invalid vesting parameters; must supply start and end time or end time") | ||
} | ||
} else { | ||
genAccount = baseAccount | ||
} | ||
|
||
if err := genAccount.Validate(); err != nil { | ||
return fmt.Errorf("failed to validate new genesis account: %w", err) | ||
} | ||
|
||
appState, genDoc, err := genutiltypes.GenesisStateFromGenFile(genesisFileUrl) | ||
if err != nil { | ||
return fmt.Errorf("failed to unmarshal genesis state: %w", err) | ||
} | ||
|
||
authGenState := authtypes.GetGenesisStateFromAppState(cdc, appState) | ||
|
||
accs, err := authtypes.UnpackAccounts(authGenState.Accounts) | ||
if err != nil { | ||
return fmt.Errorf("failed to get accounts from any: %w", err) | ||
} | ||
|
||
bankGenState := banktypes.GetGenesisStateFromAppState(cdc, appState) | ||
if accs.Contains(accAddr) { | ||
if !appendAcct { | ||
return fmt.Errorf(" Account %s already exists\nUse `append` flag to append account at existing address", accAddr) | ||
} | ||
|
||
genesisB := banktypes.GetGenesisStateFromAppState(cdc, appState) | ||
for idx, acc := range genesisB.Balances { | ||
if acc.Address != accAddr.String() { | ||
continue | ||
} | ||
|
||
updatedCoins := acc.Coins.Add(coins...) | ||
bankGenState.Balances[idx] = banktypes.Balance{Address: accAddr.String(), Coins: updatedCoins.Sort()} | ||
break | ||
} | ||
} else { | ||
// Add the new account to the set of genesis accounts and sanitize the accounts afterwards. | ||
accs = append(accs, genAccount) | ||
accs = authtypes.SanitizeGenesisAccounts(accs) | ||
|
||
genAccs, err := authtypes.PackAccounts(accs) | ||
if err != nil { | ||
return fmt.Errorf("failed to convert accounts into any's: %w", err) | ||
} | ||
authGenState.Accounts = genAccs | ||
|
||
authGenStateBz, err := cdc.MarshalJSON(&authGenState) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal auth genesis state: %w", err) | ||
} | ||
appState[authtypes.ModuleName] = authGenStateBz | ||
|
||
bankGenState.Balances = append(bankGenState.Balances, balances) | ||
} | ||
|
||
bankGenState.Balances = banktypes.SanitizeGenesisBalances(bankGenState.Balances) | ||
|
||
bankGenState.Supply = bankGenState.Supply.Add(balances.Coins...) | ||
|
||
bankGenStateBz, err := cdc.MarshalJSON(bankGenState) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal bank genesis state: %w", err) | ||
} | ||
appState[banktypes.ModuleName] = bankGenStateBz | ||
|
||
appStateJSON, err := json.Marshal(appState) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal application genesis state: %w", err) | ||
} | ||
|
||
genDoc.AppState = appStateJSON | ||
return genutil.ExportGenesisFile(genDoc, genesisFileUrl) | ||
} |
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.
Check warning
Code scanning / gosec
Returned error is not propagated up the stack.