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

R4R: Support Vesting Accounts in add-genesis-account #3549

Merged
merged 6 commits into from
Feb 8, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ FEATURES
* [\#3429](https://github.com/cosmos/cosmos-sdk/issues/3429) Support querying
for all delegator distribution rewards.
* \#3449 Proof verification now works with absence proofs
* [\#3484](https://github.com/cosmos/cosmos-sdk/issues/3484) Add support
vesting accounts to the add-genesis-account command.

* Gaia
- [\#3397](https://github.com/cosmos/cosmos-sdk/pull/3397) Implement genesis file sanitization to avoid failures at chain init.
Expand Down
27 changes: 21 additions & 6 deletions cmd/gaia/cli_test/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"

Expand All @@ -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{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++

sdk.NewCoin(feeDenom, staking.TokensFromTendermintPower(500000)),
}
)

//___________________________________________________________________________________
// Fixtures
Expand Down Expand Up @@ -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))

Expand All @@ -120,6 +129,12 @@ func InitFixtures(t *testing.T) (f *Fixtures) {

// Start an account with tokens
f.AddGenesisAccount(f.KeyAddress(keyFoo), startCoins)
f.AddGenesisAccount(
Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Down
63 changes: 58 additions & 5 deletions cmd/gaia/init/genesis_accts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Contributor

Choose a reason for hiding this comment

The 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 gaiad default genesis file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit lost here, are you asking me to revert?

Copy link
Member

Choose a reason for hiding this comment

The 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 to a genesis file is maybe overselling.

Args: cobra.ExactArgs(2),
RunE: func(_ *cobra.Command, args []string) error {
config := ctx.Config
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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
}
18 changes: 10 additions & 8 deletions cmd/gaia/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import (
)

const (
flagOverwrite = "overwrite"
flagClientHome = "home-client"
flagOverwrite = "overwrite"
flagClientHome = "home-client"
flagVestingStart = "vesting-start-time"
flagVestingEnd = "vesting-end-time"
flagVestingAmt = "vesting-amount"
)

type printInfo struct {
Expand All @@ -31,19 +34,19 @@ type printInfo struct {
AppMessage json.RawMessage `json:"app_message"`
}

// nolint: errcheck
func displayInfo(cdc *codec.Codec, info printInfo) error {
out, err := codec.MarshalJSONIndent(cdc, info)
if err != nil {
return err
}
fmt.Fprintf(os.Stderr, "%s\n", string(out))

fmt.Fprintf(os.Stderr, "%s\n", string(out)) // nolint: errcheck
return nil
}

// get cmd to initialize all files for tendermint and application
// nolint
func InitCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command {
// InitCmd returns a command that initializes all files needed for Tendermint
// and the respective application.
func InitCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command { // nolint: golint
cmd := &cobra.Command{
Use: "init [moniker]",
Short: "Initialize private validator, p2p, genesis, and application configuration files",
Expand Down Expand Up @@ -80,7 +83,6 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command {
toPrint := newPrintInfo(config.Moniker, chainID, nodeID, "", appState)

cfg.WriteConfigFile(filepath.Join(config.RootDir, "config", "config.toml"), config)

return displayInfo(cdc, toPrint)
},
}
Expand Down