-
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
R4R: Support Vesting Accounts in add-genesis-account
#3549
Changes from 4 commits
b6caf81
bfad989
ff7af00
d46643c
aabe6b9
f7ac6b1
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"path/filepath" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
|
@@ -34,15 +35,22 @@ const ( | |
feeDenom = "feetoken" | ||
fee2Denom = "fee2token" | ||
keyBaz = "baz" | ||
keyVesting = "vesting" | ||
keyFooBarBaz = "foobarbaz" | ||
) | ||
|
||
var startCoins = sdk.Coins{ | ||
sdk.NewCoin(feeDenom, staking.TokensFromTendermintPower(1000000)), | ||
sdk.NewCoin(fee2Denom, staking.TokensFromTendermintPower(1000000)), | ||
sdk.NewCoin(fooDenom, staking.TokensFromTendermintPower(1000)), | ||
sdk.NewCoin(denom, staking.TokensFromTendermintPower(150)), | ||
} | ||
var ( | ||
startCoins = sdk.Coins{ | ||
sdk.NewCoin(feeDenom, staking.TokensFromTendermintPower(1000000)), | ||
sdk.NewCoin(fee2Denom, staking.TokensFromTendermintPower(1000000)), | ||
sdk.NewCoin(fooDenom, staking.TokensFromTendermintPower(1000)), | ||
sdk.NewCoin(denom, staking.TokensFromTendermintPower(150)), | ||
} | ||
|
||
vestingCoins = sdk.Coins{ | ||
sdk.NewCoin(feeDenom, staking.TokensFromTendermintPower(500000)), | ||
} | ||
) | ||
|
||
//___________________________________________________________________________________ | ||
// Fixtures | ||
|
@@ -108,6 +116,7 @@ func InitFixtures(t *testing.T) (f *Fixtures) { | |
f.KeysAdd(keyFoo) | ||
f.KeysAdd(keyBar) | ||
f.KeysAdd(keyBaz) | ||
f.KeysAdd(keyVesting) | ||
f.KeysAdd(keyFooBarBaz, "--multisig-threshold=2", fmt.Sprintf( | ||
"--multisig=%s,%s,%s", keyFoo, keyBar, keyBaz)) | ||
|
||
|
@@ -120,6 +129,12 @@ func InitFixtures(t *testing.T) (f *Fixtures) { | |
|
||
// Start an account with tokens | ||
f.AddGenesisAccount(f.KeyAddress(keyFoo), startCoins) | ||
f.AddGenesisAccount( | ||
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 integration test we should have had. Ty |
||
f.KeyAddress(keyVesting), startCoins, | ||
fmt.Sprintf("--vesting-amount=%s", vestingCoins), | ||
fmt.Sprintf("--vesting-start-time=%d", time.Now().UTC().UnixNano()), | ||
fmt.Sprintf("--vesting-end-time=%d", time.Now().Add(60*time.Second).UTC().UnixNano()), | ||
) | ||
f.GenTx(keyFoo) | ||
f.CollectGenTxs() | ||
return | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,11 +16,11 @@ import ( | |
"github.com/cosmos/cosmos-sdk/x/auth" | ||
) | ||
|
||
// AddGenesisAccountCmd returns add-genesis-account cobra Command | ||
// AddGenesisAccountCmd returns add-genesis-account cobra Command. | ||
func AddGenesisAccountCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "add-genesis-account [address_or_key_name] [coin][,[coin]]", | ||
Short: "Add genesis account to genesis.json", | ||
Short: "Add genesis account to a genesis file", | ||
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. I'd second this new wording if were to support an extra optional flag/argument to provide the genesis.json; needless to say that it should default to 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. A bit lost here, are you asking me to revert? 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. I think what @alessio is saying is that there is no way for this command to specify what the particular file path to genesis is, so |
||
Args: cobra.ExactArgs(2), | ||
RunE: func(_ *cobra.Command, args []string) error { | ||
config := ctx.Config | ||
|
@@ -32,22 +32,35 @@ func AddGenesisAccountCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command | |
if err != nil { | ||
return err | ||
} | ||
|
||
info, err := kb.Get(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
addr = info.GetAddress() | ||
} | ||
|
||
coins, err := sdk.ParseCoins(args[1]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
vestingStart := viper.GetInt64(flagVestingStart) | ||
vestingEnd := viper.GetInt64(flagVestingEnd) | ||
vestingAmt, err := sdk.ParseCoins(viper.GetString(flagVestingAmt)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
coins.Sort() | ||
vestingAmt.Sort() | ||
alexanderbez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
genFile := config.GenesisFile() | ||
if !common.FileExists(genFile) { | ||
return fmt.Errorf("%s does not exist, run `gaiad init` first", genFile) | ||
} | ||
|
||
genDoc, err := LoadGenesisDoc(cdc, genFile) | ||
if err != nil { | ||
return err | ||
|
@@ -58,7 +71,7 @@ func AddGenesisAccountCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command | |
return err | ||
} | ||
|
||
appState, err = addGenesisAccount(cdc, appState, addr, coins) | ||
appState, err = addGenesisAccount(cdc, appState, addr, coins, vestingAmt, vestingStart, vestingEnd) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -74,10 +87,18 @@ func AddGenesisAccountCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command | |
|
||
cmd.Flags().String(cli.HomeFlag, app.DefaultNodeHome, "node's home directory") | ||
cmd.Flags().String(flagClientHome, app.DefaultCLIHome, "client's home directory") | ||
cmd.Flags().String(flagVestingAmt, "", "amount of coins for vesting accounts") | ||
cmd.Flags().Uint64(flagVestingStart, 0, "schedule start time (unix epoch) for vesting accounts") | ||
cmd.Flags().Uint64(flagVestingEnd, 0, "schedule end time (unix epoch) for vesting accounts") | ||
|
||
return cmd | ||
} | ||
|
||
func addGenesisAccount(cdc *codec.Codec, appState app.GenesisState, addr sdk.AccAddress, coins sdk.Coins) (app.GenesisState, error) { | ||
func addGenesisAccount( | ||
cdc *codec.Codec, appState app.GenesisState, addr sdk.AccAddress, | ||
coins, vestingAmt sdk.Coins, vestingStart, vestingEnd int64, | ||
) (app.GenesisState, error) { | ||
|
||
for _, stateAcc := range appState.Accounts { | ||
if stateAcc.Address.Equals(addr) { | ||
return appState, fmt.Errorf("the application state already contains account %v", addr) | ||
|
@@ -86,6 +107,38 @@ func addGenesisAccount(cdc *codec.Codec, appState app.GenesisState, addr sdk.Acc | |
|
||
acc := auth.NewBaseAccountWithAddress(addr) | ||
acc.Coins = coins | ||
appState.Accounts = append(appState.Accounts, app.NewGenesisAccount(&acc)) | ||
|
||
if !vestingAmt.IsZero() { | ||
var vacc auth.VestingAccount | ||
|
||
bvacc := &auth.BaseVestingAccount{ | ||
BaseAccount: &acc, | ||
OriginalVesting: vestingAmt, | ||
EndTime: vestingEnd, | ||
} | ||
|
||
if bvacc.OriginalVesting.IsAllGT(acc.Coins) { | ||
alessio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return appState, fmt.Errorf("vesting amount cannot be greater than total amount") | ||
} | ||
if vestingStart >= vestingEnd { | ||
return appState, fmt.Errorf("vesting start time must before end time") | ||
} | ||
|
||
if vestingStart != 0 { | ||
vacc = &auth.ContinuousVestingAccount{ | ||
BaseVestingAccount: bvacc, | ||
StartTime: vestingStart, | ||
} | ||
} else { | ||
vacc = &auth.DelayedVestingAccount{ | ||
BaseVestingAccount: bvacc, | ||
} | ||
} | ||
|
||
appState.Accounts = append(appState.Accounts, app.NewGenesisAccountI(vacc)) | ||
} else { | ||
appState.Accounts = append(appState.Accounts, app.NewGenesisAccount(&acc)) | ||
} | ||
|
||
return appState, nil | ||
} |
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.
++