Skip to content

Commit d5670a7

Browse files
authored
Merge pull request #3 from crescent-network/custom/backport-barberry-vesting-feegrant-bug
fix!: barberry vesting, feegrant bug (backport)
2 parents cd29fbb + d56a3cd commit d5670a7

File tree

5 files changed

+111
-1
lines changed

5 files changed

+111
-1
lines changed

x/auth/legacy/v043/store_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ func TestMigrateVestingAccounts(t *testing.T) {
332332

333333
delayedAccount := types.NewPeriodicVestingAccount(baseAccount, vestedCoins, startTime, periods)
334334

335+
ctx = ctx.WithBlockTime(time.Unix(1601042400, 0))
336+
335337
app.AccountKeeper.SetAccount(ctx, delayedAccount)
336338

337339
// delegation of the original vesting, failed because of no spendable balances

x/auth/vesting/msg_server.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ func (s msgServer) CreatePeriodicVestingAccount(goCtx context.Context, msg *type
114114
return nil, err
115115
}
116116

117+
if bk.BlockedAddr(to) {
118+
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive funds", msg.ToAddress)
119+
}
120+
117121
if acc := ak.GetAccount(ctx, to); acc != nil {
118122
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "account %s already exists", msg.ToAddress)
119123
}
@@ -124,6 +128,10 @@ func (s msgServer) CreatePeriodicVestingAccount(goCtx context.Context, msg *type
124128
totalCoins = totalCoins.Add(period.Amount...)
125129
}
126130

131+
if err := bk.IsSendEnabledCoins(ctx, totalCoins...); err != nil {
132+
return nil, err
133+
}
134+
127135
baseAccount := ak.NewAccountWithAddress(ctx, to)
128136

129137
acc := types.NewPeriodicVestingAccount(baseAccount.(*authtypes.BaseAccount), totalCoins.Sort(), msg.StartTime, msg.VestingPeriods)

x/auth/vesting/types/msgs.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ func (msg MsgCreatePeriodicVestingAccount) ValidateBasic() error {
139139
}
140140

141141
for i, period := range msg.VestingPeriods {
142+
if !period.Amount.IsValid() {
143+
return sdkerrors.ErrInvalidCoins.Wrap(period.Amount.String())
144+
}
145+
146+
if !period.Amount.IsAllPositive() {
147+
return sdkerrors.ErrInvalidCoins.Wrap(period.Amount.String())
148+
}
149+
142150
if period.Length < 1 {
143151
return fmt.Errorf("invalid period length of %d in period %d, length must be greater than 0", period.Length, i)
144152
}

x/feegrant/filtered_fee.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,17 @@ func (a *AllowedMsgAllowance) Accept(ctx sdk.Context, fee sdk.Coins, msgs []sdk.
6363
return false, err
6464
}
6565

66-
return allowance.Accept(ctx, fee, msgs)
66+
remove, err := allowance.Accept(ctx, fee, msgs)
67+
if err != nil {
68+
return false, err
69+
}
70+
71+
a.Allowance, err = types.NewAnyWithValue(allowance.(proto.Message))
72+
if err != nil {
73+
return false, err
74+
}
75+
76+
return remove, nil
6777
}
6878

6979
func (a *AllowedMsgAllowance) allowedMsgsToMap(ctx sdk.Context) map[string]bool {

x/feegrant/filtered_fee_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package feegrant_test
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
11+
12+
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
13+
"github.com/cosmos/cosmos-sdk/simapp"
14+
sdk "github.com/cosmos/cosmos-sdk/types"
15+
"github.com/cosmos/cosmos-sdk/x/feegrant"
16+
)
17+
18+
func TestFilteredFeeValidAllow(t *testing.T) {
19+
app := simapp.Setup(false)
20+
21+
smallAtom := sdk.NewCoins(sdk.NewInt64Coin("atom", 488))
22+
bigAtom := sdk.NewCoins(sdk.NewInt64Coin("atom", 1000))
23+
leftAtom := sdk.NewCoins(sdk.NewInt64Coin("atom", 512))
24+
25+
basicAllowance, _ := codectypes.NewAnyWithValue(&feegrant.BasicAllowance{
26+
SpendLimit: bigAtom,
27+
})
28+
29+
cases := map[string]struct {
30+
allowance *feegrant.AllowedMsgAllowance
31+
// all other checks are ignored if valid=false
32+
fee sdk.Coins
33+
blockTime time.Time
34+
valid bool
35+
accept bool
36+
remove bool
37+
remains sdk.Coins
38+
}{
39+
"internal fee is updated": {
40+
allowance: &feegrant.AllowedMsgAllowance{
41+
Allowance: basicAllowance,
42+
AllowedMessages: []string{"/cosmos.bank.v1beta1.MsgSend"},
43+
},
44+
fee: smallAtom,
45+
accept: true,
46+
remove: false,
47+
remains: leftAtom,
48+
},
49+
}
50+
51+
for name, stc := range cases {
52+
tc := stc // to make scopelint happy
53+
t.Run(name, func(t *testing.T) {
54+
err := tc.allowance.ValidateBasic()
55+
require.NoError(t, err)
56+
57+
ctx := app.BaseApp.NewContext(false, tmproto.Header{}).WithBlockTime(tc.blockTime)
58+
59+
// now try to deduct
60+
removed, err := tc.allowance.Accept(ctx, tc.fee, []sdk.Msg{
61+
&bank.MsgSend{
62+
FromAddress: "gm",
63+
ToAddress: "gn",
64+
Amount: tc.fee,
65+
},
66+
})
67+
if !tc.accept {
68+
require.Error(t, err)
69+
return
70+
}
71+
require.NoError(t, err)
72+
73+
require.Equal(t, tc.remove, removed)
74+
if !removed {
75+
var basicAllowanceLeft feegrant.BasicAllowance
76+
app.AppCodec().Unmarshal(tc.allowance.Allowance.Value, &basicAllowanceLeft)
77+
78+
assert.Equal(t, tc.remains, basicAllowanceLeft.SpendLimit)
79+
}
80+
})
81+
}
82+
}

0 commit comments

Comments
 (0)