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

feat: upgrade terra to v0.47.x #259

Merged
merged 5 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 17 additions & 2 deletions proto/cosmos/vesting/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ service Msg {
//
// Since: cosmos-sdk 0.46
rpc CreatePeriodicVestingAccount(MsgCreatePeriodicVestingAccount) returns (MsgCreatePeriodicVestingAccountResponse);
// DonateAllVestingTokens defines a method that enables donating all vesting
// tokens to community pool
rpc DonateAllVestingTokens(MsgDonateAllVestingTokens) returns (MsgDonateAllVestingTokensResponse);
}

// MsgCreateVestingAccount defines a message that enables creating a vesting
Expand Down Expand Up @@ -76,7 +79,7 @@ message MsgCreatePermanentLockedAccount {
// Since: cosmos-sdk 0.46
message MsgCreatePermanentLockedAccountResponse {}

// MsgCreateVestingAccount defines a message that enables creating a vesting
// MsgCreatePeriodicVestingAccount defines a message that enables creating a vesting
// account.
//
// Since: cosmos-sdk 0.46
Expand All @@ -93,8 +96,20 @@ message MsgCreatePeriodicVestingAccount {
repeated Period vesting_periods = 4 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
}

// MsgCreateVestingAccountResponse defines the Msg/CreatePeriodicVestingAccount
// MsgCreatePeriodicVestingAccountResponse defines the Msg/CreatePeriodicVestingAccount
// response type.
//
// Since: cosmos-sdk 0.46
message MsgCreatePeriodicVestingAccountResponse {}

// MsgDonateAllVestingTokens defines a message that enables donating all vesting
// token to community pool.
message MsgDonateAllVestingTokens {
option (gogoproto.equal) = false;

string from_address = 1 [(gogoproto.moretags) = "yaml:\"from_address\""];
}

// MsgDonateAllVestingTokensResponse defines the Msg/MsgDonateAllVestingTokens
// response type.
message MsgDonateAllVestingTokensResponse {}
4 changes: 0 additions & 4 deletions scripts/protocgen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,3 @@ cd ..
# move proto files to the right places
cp -r github.com/cosmos/cosmos-sdk/* ./
rm -rf github.com

go mod tidy

./scripts/protocgen-pulsar.sh
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func NewSimApp(
encodingConfig.TxConfig,
),
auth.NewAppModule(appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts, app.GetSubspace(authtypes.ModuleName)),
vesting.NewAppModule(app.AccountKeeper, app.BankKeeper),
vesting.NewAppModule(app.AccountKeeper, app.BankKeeper, app.DistrKeeper, app.StakingKeeper),
bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper, app.GetSubspace(banktypes.ModuleName)),
capability.NewAppModule(appCodec, *app.CapabilityKeeper, false),
crisis.NewAppModule(app.CrisisKeeper, skipGenesisInvariants, app.GetSubspace(crisistypes.ModuleName)),
Expand Down
8 changes: 4 additions & 4 deletions x/auth/migrations/v2/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ func TestMigrateVestingAccounts(t *testing.T) {
3666666670000,
3666666670000,
0,
1601042400 + 1,
1601042400,
},
{
"periodic vesting account, all has vested",
Expand Down Expand Up @@ -430,7 +430,7 @@ func TestMigrateVestingAccounts(t *testing.T) {
3666666670000,
0,
3666666670000,
1601042400 + 31536000 + 15897600 + 15897600 + 1,
1601042400 + 31536000 + 15897600 + 15897600,
},
{
"periodic vesting account, first period has vested",
Expand Down Expand Up @@ -476,7 +476,7 @@ func TestMigrateVestingAccounts(t *testing.T) {
3666666670000,
3666666670000 - 1833333335000,
1833333335000,
1601042400 + 31536000 + 1,
1601042400 + 31536000,
},
{
"periodic vesting account, first 2 period has vested",
Expand Down Expand Up @@ -522,7 +522,7 @@ func TestMigrateVestingAccounts(t *testing.T) {
3666666670000,
3666666670000 - 1833333335000 - 916666667500,
1833333335000 + 916666667500,
1601042400 + 31536000 + 15638400 + 1,
1601042400 + 31536000 + 15638400,
},
{
"vesting account has unbonding delegations in place",
Expand Down
56 changes: 43 additions & 13 deletions x/auth/vesting/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func GetTxCmd() *cobra.Command {
NewMsgCreateVestingAccountCmd(),
NewMsgCreatePermanentLockedAccountCmd(),
NewMsgCreatePeriodicVestingAccountCmd(),
NewMsgDonateAllVestingTokensCmd(),
)

return txCmd
Expand Down Expand Up @@ -141,19 +142,20 @@ func NewMsgCreatePeriodicVestingAccountCmd() *cobra.Command {
Where periods.json contains:

An array of coin strings and unix epoch times for coins to vest
{ "start_time": 1625204910,
"periods":[
{
"coins": "10test",
"length_seconds":2592000 //30 days
},
{
"coins": "10test",
"length_seconds":2592000 //30 days
},
]
}
`,
{
"start_time": 1625204910,
"periods":[
{
"coins": "10test",
"length_seconds":2592000
},
{
"coins": "10test",
"length_seconds":2592000 //30 days
}
]
}
`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
Expand Down Expand Up @@ -207,3 +209,31 @@ func NewMsgCreatePeriodicVestingAccountCmd() *cobra.Command {

return cmd
}

// NewMsgDonateAllVestingTokensCmd returns a CLI command handler for creating a
// MsgDonateAllVestingTokens transaction.
func NewMsgDonateAllVestingTokensCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "donate-all-vesting-tokens",
Short: "Donate all vesting tokens of a vesting account to community pool.",
Long: `Donate all vesting tokens of a vesting account to community pool.
The account must not have any delegated vesting tokens to prevent complex
vesting logic changes. After donation, the account will be changed to normal
"BaseAccount".`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := types.NewMsgDonateAllVestingTokens(clientCtx.GetFromAddress())

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
39 changes: 39 additions & 0 deletions x/auth/vesting/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package vesting

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
)

// NewHandler returns a handler for x/auth message types.
func NewHandler(
ak keeper.AccountKeeper,
bk types.BankKeeper,
dk types.DistrKeeper,
sk types.StakingKeeper,
) sdk.Handler {
msgServer := NewMsgServerImpl(ak, bk, dk, sk)

return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx = ctx.WithEventManager(sdk.NewEventManager())

switch msg := msg.(type) {
case *types.MsgCreateVestingAccount:
res, err := msgServer.CreateVestingAccount(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)

case *types.MsgCreatePeriodicVestingAccount:
res, err := msgServer.CreatePeriodicVestingAccount(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)

case *types.MsgDonateAllVestingTokens:
res, err := msgServer.DonateAllVestingTokens(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)

default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s message type: %T", types.ModuleName, msg)
}
}
}
26 changes: 23 additions & 3 deletions x/auth/vesting/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"cosmossdk.io/core/appmodule"

"github.com/cosmos/cosmos-sdk/x/auth/keeper"

"github.com/cosmos/cosmos-sdk/x/auth/vesting/client/cli"
"github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
)
Expand Down Expand Up @@ -80,13 +81,22 @@ type AppModule struct {

accountKeeper keeper.AccountKeeper
bankKeeper types.BankKeeper
distrKeeper types.DistrKeeper
stakingKeeper types.StakingKeeper
}

func NewAppModule(ak keeper.AccountKeeper, bk types.BankKeeper) AppModule {
func NewAppModule(
ak keeper.AccountKeeper,
bk types.BankKeeper,
dk types.DistrKeeper,
sk types.StakingKeeper,
) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
accountKeeper: ak,
bankKeeper: bk,
distrKeeper: dk,
stakingKeeper: sk,
}
}

Expand All @@ -103,7 +113,15 @@ func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}

// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), NewMsgServerImpl(am.accountKeeper, am.bankKeeper))
types.RegisterMsgServer(
cfg.MsgServer(),
NewMsgServerImpl(
am.accountKeeper,
am.bankKeeper,
am.distrKeeper,
am.stakingKeeper,
),
)
}

// InitGenesis performs a no-op.
Expand Down Expand Up @@ -134,6 +152,8 @@ type VestingInputs struct {

AccountKeeper keeper.AccountKeeper
BankKeeper types.BankKeeper
// DistrKeeper types.DistrKeeper
StakingKeeper types.StakingKeeper
}

type VestingOutputs struct {
Expand All @@ -143,7 +163,7 @@ type VestingOutputs struct {
}

func ProvideModule(in VestingInputs) VestingOutputs {
m := NewAppModule(in.AccountKeeper, in.BankKeeper)
m := NewAppModule(in.AccountKeeper, in.BankKeeper, nil, in.StakingKeeper)

return VestingOutputs{Module: m}
}
89 changes: 87 additions & 2 deletions x/auth/vesting/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package vesting

import (
"context"
"fmt"
"math"

"github.com/armon/go-metrics"

Expand All @@ -10,18 +12,26 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/auth/vesting/exported"
"github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
)

type msgServer struct {
keeper.AccountKeeper
types.BankKeeper
types.DistrKeeper
types.StakingKeeper
}

// NewMsgServerImpl returns an implementation of the vesting MsgServer interface,
// wrapping the corresponding AccountKeeper and BankKeeper.
func NewMsgServerImpl(k keeper.AccountKeeper, bk types.BankKeeper) types.MsgServer {
return &msgServer{AccountKeeper: k, BankKeeper: bk}
func NewMsgServerImpl(
k keeper.AccountKeeper,
bk types.BankKeeper,
dk types.DistrKeeper,
sk types.StakingKeeper,
) types.MsgServer {
return &msgServer{AccountKeeper: k, BankKeeper: bk, DistrKeeper: dk, StakingKeeper: sk}
}

var _ types.MsgServer = msgServer{}
Expand Down Expand Up @@ -193,3 +203,78 @@ func (s msgServer) CreatePeriodicVestingAccount(goCtx context.Context, msg *type

return &types.MsgCreatePeriodicVestingAccountResponse{}, nil
}

func (s msgServer) DonateAllVestingTokens(goCtx context.Context, msg *types.MsgDonateAllVestingTokens) (*types.MsgDonateAllVestingTokensResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

ak := s.AccountKeeper
dk := s.DistrKeeper
sk := s.StakingKeeper

from, err := sdk.AccAddressFromBech32(msg.FromAddress)
if err != nil {
return nil, err
}

acc := ak.GetAccount(ctx, from)
if acc == nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "account %s not exists", msg.FromAddress)
}

// get all delegations of an account and undust those that have less than 1 uluna
delegations := sk.GetDelegatorDelegations(ctx, acc.GetAddress(), math.MaxUint16)
for _, delegation := range delegations {
validatorAddr, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
if err != nil {
return nil, err
}
validator, found := sk.GetValidator(ctx, validatorAddr)
if !found {
return nil, fmt.Errorf("validator not found")
}
// Try to delete the dust delegation
_, removedTokens := sk.RemoveValidatorTokensAndShares(ctx, validator, delegation.Shares)
// If the delegation is not dust, return an error and stop the donation flow
if !removedTokens.IsZero() {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "account %s has a non-zero staking entry", msg.FromAddress)
}

// Remove the dust delegation shares from the validator
err = sk.RemoveDelegation(ctx, delegation)
if err != nil {
return nil, err
}
}

// check whether an account has any other type of staking entries
if len(sk.GetUnbondingDelegations(ctx, acc.GetAddress(), 1)) != 0 ||
len(sk.GetRedelegations(ctx, acc.GetAddress(), 1)) != 0 {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "account %s has staking entry", msg.FromAddress)
}

vestingAcc, ok := acc.(exported.VestingAccount)
if !ok {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "account %s is not a vesting account", msg.FromAddress)
}

vestingCoins := vestingAcc.GetVestingCoins(ctx.BlockTime())
if vestingCoins.IsZero() {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "account %s has no vesting tokens", msg.FromAddress)
}

// Change the account as normal account
ak.SetAccount(ctx,
authtypes.NewBaseAccount(
acc.GetAddress(),
acc.GetPubKey(),
acc.GetAccountNumber(),
acc.GetSequence(),
),
)

if err := dk.FundCommunityPool(ctx, vestingCoins, from); err != nil {
return nil, err
}

return &types.MsgDonateAllVestingTokensResponse{}, nil
}
Loading