Skip to content

🔃 Update vesting module to cosmos-sdk v0.50.6 #634

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

Merged
merged 18 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ doc: doc-proto doc-command doc-predicate ## Generate all the documentation
doc-proto: proto-gen ## Generate the documentation from the Protobuf files
@echo "${COLOR_CYAN} 📝 Generating doc from Protobuf files${COLOR_RESET}"
@$(DOCKER_PROTO_RUN) sh ./scripts/protocgen-doc.sh
@for MODULE in $(shell find proto -name '*.proto' -print0 | xargs -0 -n1 dirname | sort | uniq | xargs dirname) ; do \
@for MODULE in $(shell find proto -name '*.proto' -maxdepth 3 -print0 | xargs -0 -n1 dirname | sort | uniq | xargs dirname) ; do \
echo "${COLOR_CYAN} 📖 Generate documentation for $${MODULE} module${COLOR_RESET}" ; \
DEFAULT_DATASOURCE="./docs/proto/templates/default.yaml" ; \
MODULE_DATASOURCE="merge:./$${MODULE}/docs.yaml|$${DEFAULT_DATASOURCE}" ; \
Expand Down
105 changes: 105 additions & 0 deletions cmd/axoned/cmd/genaccount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package cmd

import (
"bufio"
"fmt"

"github.com/spf13/cobra"

address "cosmossdk.io/core/address"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"

vestingtypes "github.com/axone-protocol/axoned/v8/x/vesting/types"
)

const (
flagVestingStart = "vesting-start-time"
flagVestingCliff = "vesting-cliff-time"
flagVestingEnd = "vesting-end-time"
flagVestingAmt = "vesting-amount"
flagAppendMode = "append"
flagModuleName = "module-name"
)

// AddGenesisAccountCmd returns 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.
//
//nolint:funlen,nestif
func AddGenesisAccountCmd(defaultNodeHome string, addressCodec address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "add-genesis-account [address_or_key_name] [coin][,[coin]]",
Short: "Add a genesis account to genesis.json",
Long: `Add a genesis account to genesis.json. The provided account must specify
the account address or key name and a list of initial coins. If a key name is given,
the address will be looked up in the local Keybase. The list of initial tokens must
contain valid denominations. Accounts may optionally be supplied with vesting parameters.
`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
serverCtx := server.GetServerContextFromCmd(cmd)
config := serverCtx.Config

config.SetRoot(clientCtx.HomeDir)

var kr keyring.Keyring
addr, err := addressCodec.StringToBytes(args[0])
if err != nil {
inBuf := bufio.NewReader(cmd.InOrStdin())
keyringBackend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)

if keyringBackend != "" && clientCtx.Keyring == nil {
var err error
kr, err = keyring.New(sdk.KeyringServiceName(), keyringBackend, clientCtx.HomeDir, inBuf, clientCtx.Codec)
if err != nil {
return err
}
} else {
kr = clientCtx.Keyring
}

k, err := kr.Key(args[0])
if err != nil {
return fmt.Errorf("failed to get address from Keyring: %w", err)
}

addr, err = k.GetAddress()
if err != nil {
return err
}
}

appendflag, _ := cmd.Flags().GetBool(flagAppendMode)
vestingStart, _ := cmd.Flags().GetInt64(flagVestingStart)
vestingCliff, _ := cmd.Flags().GetInt64(flagVestingCliff)
vestingEnd, _ := cmd.Flags().GetInt64(flagVestingEnd)
vestingAmtStr, _ := cmd.Flags().GetString(flagVestingAmt)
moduleNameStr, _ := cmd.Flags().GetString(flagModuleName)

return vestingtypes.AddGenesisAccount(clientCtx.Codec,
addr,
appendflag,
config.GenesisFile(),
args[1],
vestingAmtStr, vestingStart, vestingCliff, vestingEnd,
moduleNameStr)
},
}

cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)")
cmd.Flags().String(flagVestingAmt, "", "amount of coins for vesting accounts")
cmd.Flags().Int64(flagVestingStart, 0, "schedule start time (unix epoch) for vesting accounts")
cmd.Flags().Int64(flagVestingCliff, 0, "schedule cliff time (unix epoch) for vesting accounts")
cmd.Flags().Int64(flagVestingEnd, 0, "schedule end time (unix epoch) for vesting accounts")
cmd.Flags().Bool(flagAppendMode, false, "append the coins to an account already in the genesis.json file")
cmd.Flags().String(flagModuleName, "", "module account name")
flags.AddQueryFlagsToCmd(cmd)

return cmd
}
116 changes: 116 additions & 0 deletions cmd/axoned/cmd/genaccount_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package cmd_test

import (
"context"
"fmt"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/require"

"cosmossdk.io/log"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
genutiltest "github.com/cosmos/cosmos-sdk/x/genutil/client/testutil"
"github.com/cosmos/cosmos-sdk/x/staking"
)

var testMbm = module.NewBasicManager(
staking.AppModuleBasic{},
genutil.AppModuleBasic{},
)

func TestAddGenesisAccountCmd(t *testing.T) {
_, _, addr1 := testdata.KeyTestPubAddr()
tests := []struct {
name string
addr string
denom string
withKeyring bool
expectErr bool
}{
{
name: "invalid address",
addr: "",
denom: "1000atom",
withKeyring: false,
expectErr: true,
},
{
name: "valid address",
addr: addr1.String(),
denom: "1000atom",
withKeyring: false,
expectErr: false,
},
{
name: "multiple denoms",
addr: addr1.String(),
denom: "1000atom, 2000stake",
withKeyring: false,
expectErr: false,
},
{
name: "with keyring",
addr: "ser",
denom: "1000atom",
withKeyring: true,
expectErr: false,
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
home := t.TempDir()
logger := log.NewNopLogger()
cfg, err := genutiltest.CreateDefaultCometConfig(home)
require.NoError(t, err)

appCodec := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{}).Codec
err = genutiltest.ExecInitCmd(testMbm, home, appCodec)
require.NoError(t, err)

serverCtx := server.NewContext(viper.New(), cfg, logger)
clientCtx := client.Context{}.WithCodec(appCodec).WithHomeDir(home)

if tc.withKeyring {
path := hd.CreateHDPath(118, 0, 0).String()
kr, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, home, nil, appCodec)
require.NoError(t, err)
_, _, err = kr.NewMnemonic(tc.addr, keyring.English, path, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
require.NoError(t, err)
clientCtx = clientCtx.WithKeyring(kr)
}

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx)

cmd := genutilcli.AddGenesisAccountCmd(home, addresscodec.NewBech32Codec("cosmos"))
cmd.SetArgs([]string{
tc.addr,
tc.denom,
fmt.Sprintf("--%s=home", flags.FlagHome),
})

if tc.expectErr {
require.Error(t, cmd.ExecuteContext(ctx))
} else {
require.NoError(t, cmd.ExecuteContext(ctx))
}
})
}
}
9 changes: 9 additions & 0 deletions cmd/axoned/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,18 @@ func initRootCmd(
func genesisCommand(txConfig client.TxConfig, basicManager module.BasicManager, cmds ...*cobra.Command) *cobra.Command {
cmd := genutilcli.Commands(txConfig, basicManager, app.DefaultNodeHome)

// Remove default `add-genesis-account` command and add our custom (integrating the cliff),
for _, command := range cmd.Commands() {
if command.Name() == "add-genesis-account" {
cmd.RemoveCommand(command)
}
}
cmd.AddCommand(AddGenesisAccountCmd(app.DefaultNodeHome, txConfig.SigningContext().AddressCodec()))

for _, subCmd := range cmds {
cmd.AddCommand(subCmd)
}

return cmd
}

Expand Down
1 change: 1 addition & 0 deletions docs/command/axoned_genesis_add-genesis-account.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ axoned genesis add-genesis-account [address_or_key_name] [coin][,[coin]] [flags]
--node string <host>:<port> to CometBFT RPC interface for this chain (default "tcp://localhost:26657")
-o, --output string Output format (text|json) (default "text")
--vesting-amount string amount of coins for vesting accounts
--vesting-cliff-time int schedule cliff time (unix epoch) for vesting accounts
--vesting-end-time int schedule end time (unix epoch) for vesting accounts
--vesting-start-time int schedule start time (unix epoch) for vesting accounts
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Create a new vesting account funded with an allocation of tokens with cliff.

### Synopsis

Create a new vesting account funded with an allocation of tokens. The
Create a new vesting account funded with an allocation of tokens with cliff. The
tokens allowed will be start vested but the token will be released only after the cliff time.
All vesting accounts created will have their start time
set by the committed block's time. The end_time and cliff_time must be provided as a UNIX epoch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A sequence of coins and period length in seconds. Periods are sequential, in tha

An array of coin strings and unix epoch times for coins to vest
\{ "start_time": 1625204910,
"period":[
"periods":[
\{
"coins": "10test",
"length_seconds":2592000 //30 days
Expand Down
8 changes: 4 additions & 4 deletions docs/proto/vesting.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ continuously vests by unlocking coins after a cliff period linearly with respect
| ----- | ---- | ----- | ----------- |
| `base_vesting_account` | [BaseVestingAccount](#vesting.v1beta1.BaseVestingAccount) | | base_vesting_account implements the VestingAccount interface. It contains all the necessary fields needed for any vesting account implementation |
| `start_time` | [int64](#int64) | | start_time defines the time at which the vesting period begins |
| `cliff_time` | [int64](#int64) | | |
| `cliff_time` | [int64](#int64) | | cliff_time defines the time at which the first portion of the vesting is unlocked |

<a name="vesting.v1beta1.ContinuousVestingAccount"></a>

Expand Down Expand Up @@ -94,7 +94,7 @@ Period defines a length of time and amount of coins that will vest.

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `length` | [int64](#int64) | | |
| `length` | [int64](#int64) | | Period duration in seconds. |
| `amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | repeated | |

<a name="vesting.v1beta1.PeriodicVestingAccount"></a>
Expand Down Expand Up @@ -150,7 +150,7 @@ account.
| `to_address` | [string](#string) | | |
| `amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | repeated | |
| `end_time` | [int64](#int64) | | |
| `cliff_time` | [int64](#int64) | | |
| `cliff_time` | [int64](#int64) | | cliff time as unix time (in seconds) is the time at which the first portion of the vesting is unlocked. |

<a name="vesting.v1beta1.MsgCreateCliffVestingAccountResponse"></a>

Expand Down Expand Up @@ -218,7 +218,7 @@ account.
| `from_address` | [string](#string) | | |
| `to_address` | [string](#string) | | |
| `amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | repeated | |
| `end_time` | [int64](#int64) | | |
| `end_time` | [int64](#int64) | | end of vesting as unix time (in seconds). |
| `delayed` | [bool](#bool) | | |

<a name="vesting.v1beta1.MsgCreateVestingAccountResponse"></a>
Expand Down
2 changes: 1 addition & 1 deletion proto/buf.gen.doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ plugins:
- name: doc
out: ../docs/proto
opt:
- ../docs/proto/templates/protodoc-markdown.tmpl,docs.md
- ../docs/proto/templates/protodoc-markdown.tmpl,docs.md:module/*
2 changes: 1 addition & 1 deletion proto/buf.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: v1
name: buf.build/axone-protocol/axoned
deps:
- buf.build/cosmos/cosmos-sdk:v0.50.1
- buf.build/cosmos/cosmos-sdk:v0.50.0
- buf.build/cosmos/cosmos-proto
- buf.build/cosmos/gogo-proto
- buf.build/googleapis/googleapis
Expand Down
12 changes: 12 additions & 0 deletions proto/vesting/module/v1/module.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax = "proto3";

package vesting.module.v1;

import "cosmos/app/v1alpha1/module.proto";

// Module is the config object of the vesting module.
message Module {
option (cosmos.app.v1alpha1.module) = {
go_import: "github.com/axone-protocol/axoned/x/vesting"
};
}
8 changes: 6 additions & 2 deletions proto/vesting/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ message MsgCreateVestingAccount {
repeated cosmos.base.v1beta1.Coin amount = 3 [
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true,
(amino.encoding) = "legacy_coins",
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];

// end of vesting as unix time (in seconds).
int64 end_time = 4;
bool delayed = 6;
bool delayed = 5;
}

// MsgCreateVestingAccountResponse defines the Msg/CreateVestingAccount response type.
Expand All @@ -70,6 +72,7 @@ message MsgCreatePermanentLockedAccount {
repeated cosmos.base.v1beta1.Coin amount = 3 [
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true,
(amino.encoding) = "legacy_coins",
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
}
Expand All @@ -85,7 +88,7 @@ message MsgCreatePermanentLockedAccountResponse {}
// Since: cosmos-sdk 0.46
message MsgCreatePeriodicVestingAccount {
option (cosmos.msg.v1.signer) = "from_address";
option (amino.name) = "cosmos-sdk/MsgCreatePeriodicVestingAccount";
option (amino.name) = "cosmos-sdk/MsgCreatePeriodVestAccount";

option (gogoproto.equal) = false;

Expand Down Expand Up @@ -122,6 +125,7 @@ message MsgCreateCliffVestingAccount {
];

int64 end_time = 4;
// cliff time as unix time (in seconds) is the time at which the first portion of the vesting is unlocked.
int64 cliff_time = 5;
}

Expand Down
Loading
Loading