cosmos-sdk/x/bank/internal/keeper/keeper_test.go

546 lines
23 KiB
Go
Raw Normal View History

2019-08-19 06:29:17 -07:00
package keeper_test
2018-04-09 15:10:58 -07:00
import (
"testing"
"time"
2018-04-09 15:10:58 -07:00
2019-01-02 13:19:48 -08:00
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
tmkv "github.com/tendermint/tendermint/libs/kv"
tmtime "github.com/tendermint/tendermint/types/time"
2019-01-02 13:19:48 -08:00
2019-08-19 06:29:17 -07:00
"github.com/cosmos/cosmos-sdk/simapp"
2018-04-09 15:10:58 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
2019-08-19 06:29:17 -07:00
keep "github.com/cosmos/cosmos-sdk/x/bank/internal/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
"github.com/cosmos/cosmos-sdk/x/supply"
2018-04-09 15:10:58 -07:00
)
2018-12-20 11:09:43 -08:00
func TestKeeper(t *testing.T) {
2019-08-19 06:29:17 -07:00
app, ctx := createTestApp(false)
2018-04-09 15:10:58 -07:00
2018-07-06 00:06:53 -07:00
addr := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
addr3 := sdk.AccAddress([]byte("addr3"))
2019-08-19 06:29:17 -07:00
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
2018-04-09 15:10:58 -07:00
// Test GetCoins/SetCoins
2019-08-19 06:29:17 -07:00
app.AccountKeeper.SetAccount(ctx, acc)
require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins()))
2018-04-09 15:10:58 -07:00
2019-08-19 06:29:17 -07:00
app.BankKeeper.SetCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10)))
require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10))))
2018-04-09 15:10:58 -07:00
// Test HasCoins
2019-08-19 06:29:17 -07:00
require.True(t, app.BankKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10))))
require.True(t, app.BankKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 5))))
require.False(t, app.BankKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 15))))
require.False(t, app.BankKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 5))))
2018-04-09 15:10:58 -07:00
// Test AddCoins
2019-08-19 06:29:17 -07:00
app.BankKeeper.AddCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 15)))
require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 25))))
2018-04-09 15:10:58 -07:00
2019-08-19 06:29:17 -07:00
app.BankKeeper.AddCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 15)))
require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 15), sdk.NewInt64Coin("foocoin", 25))))
2018-04-09 15:10:58 -07:00
// Test SubtractCoins
2019-08-19 06:29:17 -07:00
app.BankKeeper.SubtractCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10)))
app.BankKeeper.SubtractCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 5)))
require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 10), sdk.NewInt64Coin("foocoin", 15))))
2018-04-09 15:10:58 -07:00
2019-08-19 06:29:17 -07:00
app.BankKeeper.SubtractCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 11)))
require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 10), sdk.NewInt64Coin("foocoin", 15))))
2018-04-09 15:10:58 -07:00
2019-08-19 06:29:17 -07:00
app.BankKeeper.SubtractCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 10)))
require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 15))))
require.False(t, app.BankKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 1))))
2018-04-09 15:10:58 -07:00
// Test SendCoins
2019-08-19 06:29:17 -07:00
app.BankKeeper.SendCoins(ctx, addr, addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 5)))
require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10))))
require.True(t, app.BankKeeper.GetCoins(ctx, addr2).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 5))))
2018-04-09 15:10:58 -07:00
app.BankKeeper.SendCoins(ctx, addr, addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 50)))
2019-08-19 06:29:17 -07:00
require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10))))
require.True(t, app.BankKeeper.GetCoins(ctx, addr2).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 5))))
2018-04-09 15:10:58 -07:00
2019-08-19 06:29:17 -07:00
app.BankKeeper.AddCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 30)))
app.BankKeeper.SendCoins(ctx, addr, addr2, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 10), sdk.NewInt64Coin("foocoin", 5)))
require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 20), sdk.NewInt64Coin("foocoin", 5))))
require.True(t, app.BankKeeper.GetCoins(ctx, addr2).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 10), sdk.NewInt64Coin("foocoin", 10))))
2018-04-09 15:10:58 -07:00
// Test InputOutputCoins
input1 := types.NewInput(addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 2)))
output1 := types.NewOutput(addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 2)))
2019-08-19 06:29:17 -07:00
app.BankKeeper.InputOutputCoins(ctx, []types.Input{input1}, []types.Output{output1})
require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 20), sdk.NewInt64Coin("foocoin", 7))))
require.True(t, app.BankKeeper.GetCoins(ctx, addr2).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 10), sdk.NewInt64Coin("foocoin", 8))))
2018-04-09 15:10:58 -07:00
inputs := []types.Input{
types.NewInput(addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 3))),
types.NewInput(addr2, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 3), sdk.NewInt64Coin("foocoin", 2))),
2018-04-09 15:10:58 -07:00
}
outputs := []types.Output{
types.NewOutput(addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 1))),
types.NewOutput(addr3, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 2), sdk.NewInt64Coin("foocoin", 5))),
2018-04-09 15:10:58 -07:00
}
2019-08-19 06:29:17 -07:00
app.BankKeeper.InputOutputCoins(ctx, inputs, outputs)
require.True(t, app.BankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 21), sdk.NewInt64Coin("foocoin", 4))))
require.True(t, app.BankKeeper.GetCoins(ctx, addr2).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 7), sdk.NewInt64Coin("foocoin", 6))))
require.True(t, app.BankKeeper.GetCoins(ctx, addr3).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 2), sdk.NewInt64Coin("foocoin", 5))))
// Test retrieving black listed accounts
for acc := range simapp.GetMaccPerms() {
addr := supply.NewModuleAddress(acc)
require.Equal(t, app.BlacklistedAccAddrs()[addr.String()], app.BankKeeper.BlacklistedAddr(addr))
2019-08-19 06:29:17 -07:00
}
2018-04-09 15:10:58 -07:00
}
func TestSendKeeper(t *testing.T) {
2019-08-19 06:29:17 -07:00
app, ctx := createTestApp(false)
blacklistedAddrs := make(map[string]bool)
2019-08-19 06:29:17 -07:00
paramSpace := app.ParamsKeeper.Subspace("newspace")
sendKeeper := keep.NewBaseSendKeeper(app.AccountKeeper, paramSpace, blacklistedAddrs)
2019-08-19 06:29:17 -07:00
app.BankKeeper.SetSendEnabled(ctx, true)
2018-04-09 15:10:58 -07:00
2018-07-06 00:06:53 -07:00
addr := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
2019-08-19 06:29:17 -07:00
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
2018-04-09 15:10:58 -07:00
// Test GetCoins/SetCoins
2019-08-19 06:29:17 -07:00
app.AccountKeeper.SetAccount(ctx, acc)
2019-03-07 16:55:08 -08:00
require.True(t, sendKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins()))
2018-04-09 15:10:58 -07:00
2019-08-19 06:29:17 -07:00
app.BankKeeper.SetCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10)))
2019-03-07 16:55:08 -08:00
require.True(t, sendKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10))))
2018-04-09 15:10:58 -07:00
// Test HasCoins
2019-03-07 16:55:08 -08:00
require.True(t, sendKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10))))
require.True(t, sendKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 5))))
require.False(t, sendKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 15))))
require.False(t, sendKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 5))))
2018-04-09 15:10:58 -07:00
2019-08-19 06:29:17 -07:00
app.BankKeeper.SetCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 15)))
2018-04-09 15:10:58 -07:00
// Test SendCoins
2019-03-07 16:55:08 -08:00
sendKeeper.SendCoins(ctx, addr, addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 5)))
require.True(t, sendKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10))))
require.True(t, sendKeeper.GetCoins(ctx, addr2).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 5))))
2018-04-09 15:10:58 -07:00
sendKeeper.SendCoins(ctx, addr, addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 50)))
2019-03-07 16:55:08 -08:00
require.True(t, sendKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10))))
require.True(t, sendKeeper.GetCoins(ctx, addr2).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 5))))
2018-04-09 15:10:58 -07:00
2019-08-19 06:29:17 -07:00
app.BankKeeper.AddCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 30)))
2019-03-07 16:55:08 -08:00
sendKeeper.SendCoins(ctx, addr, addr2, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 10), sdk.NewInt64Coin("foocoin", 5)))
require.True(t, sendKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 20), sdk.NewInt64Coin("foocoin", 5))))
require.True(t, sendKeeper.GetCoins(ctx, addr2).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 10), sdk.NewInt64Coin("foocoin", 10))))
2018-04-09 15:10:58 -07:00
2019-01-02 13:19:48 -08:00
// validate coins with invalid denoms or negative values cannot be sent
// NOTE: We must use the Coin literal as the constructor does not allow
// negative values.
err := sendKeeper.SendCoins(ctx, addr, addr2, sdk.Coins{sdk.Coin{Denom: "FOOCOIN", Amount: sdk.NewInt(-5)}})
2019-01-02 13:19:48 -08:00
require.Error(t, err)
2018-04-09 15:10:58 -07:00
}
func TestMsgSendEvents(t *testing.T) {
app, ctx := createTestApp(false)
app.BankKeeper.SetSendEnabled(ctx, true)
addr := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
app.AccountKeeper.SetAccount(ctx, acc)
newCoins := sdk.NewCoins(sdk.NewInt64Coin("foocoin", 50))
err := app.BankKeeper.SendCoins(ctx, addr, addr2, newCoins)
require.Error(t, err)
events := ctx.EventManager().Events()
require.Equal(t, 2, len(events))
event1 := sdk.Event{
Type: types.EventTypeTransfer,
Attributes: []tmkv.Pair{},
}
event1.Attributes = append(
event1.Attributes,
tmkv.Pair{Key: []byte(types.AttributeKeyRecipient), Value: []byte(addr2.String())})
event1.Attributes = append(
event1.Attributes,
tmkv.Pair{Key: []byte(sdk.AttributeKeyAmount), Value: []byte(newCoins.String())})
event2 := sdk.Event{
Type: sdk.EventTypeMessage,
Attributes: []tmkv.Pair{},
}
event2.Attributes = append(
event2.Attributes,
tmkv.Pair{Key: []byte(types.AttributeKeySender), Value: []byte(addr.String())})
require.Equal(t, event1, events[0])
require.Equal(t, event2, events[1])
app.BankKeeper.SetCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 50)))
newCoins = sdk.NewCoins(sdk.NewInt64Coin("foocoin", 50))
err = app.BankKeeper.SendCoins(ctx, addr, addr2, newCoins)
require.NoError(t, err)
events = ctx.EventManager().Events()
require.Equal(t, 4, len(events))
require.Equal(t, event1, events[2])
require.Equal(t, event2, events[3])
}
func TestMsgMultiSendEvents(t *testing.T) {
app, ctx := createTestApp(false)
app.BankKeeper.SetSendEnabled(ctx, true)
addr := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
addr3 := sdk.AccAddress([]byte("addr3"))
addr4 := sdk.AccAddress([]byte("addr4"))
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
app.AccountKeeper.SetAccount(ctx, acc)
app.AccountKeeper.SetAccount(ctx, acc2)
newCoins := sdk.NewCoins(sdk.NewInt64Coin("foocoin", 50))
newCoins2 := sdk.NewCoins(sdk.NewInt64Coin("barcoin", 100))
inputs := []types.Input{
{Address: addr, Coins: newCoins},
{Address: addr2, Coins: newCoins2},
}
outputs := []types.Output{
{Address: addr3, Coins: newCoins},
{Address: addr4, Coins: newCoins2},
}
err := app.BankKeeper.InputOutputCoins(ctx, inputs, outputs)
require.Error(t, err)
events := ctx.EventManager().Events()
require.Equal(t, 0, len(events))
// Set addr's coins but not addr2's coins
app.BankKeeper.SetCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 50)))
err = app.BankKeeper.InputOutputCoins(ctx, inputs, outputs)
require.Error(t, err)
events = ctx.EventManager().Events()
require.Equal(t, 1, len(events))
event1 := sdk.Event{
Type: sdk.EventTypeMessage,
Attributes: []tmkv.Pair{},
}
event1.Attributes = append(
event1.Attributes,
tmkv.Pair{Key: []byte(types.AttributeKeySender), Value: []byte(addr.String())})
require.Equal(t, event1, events[0])
// Set addr's coins and addr2's coins
app.BankKeeper.SetCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 50)))
newCoins = sdk.NewCoins(sdk.NewInt64Coin("foocoin", 50))
app.BankKeeper.SetCoins(ctx, addr2, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 100)))
newCoins2 = sdk.NewCoins(sdk.NewInt64Coin("barcoin", 100))
err = app.BankKeeper.InputOutputCoins(ctx, inputs, outputs)
require.NoError(t, err)
events = ctx.EventManager().Events()
require.Equal(t, 5, len(events))
event2 := sdk.Event{
Type: sdk.EventTypeMessage,
Attributes: []tmkv.Pair{},
}
event2.Attributes = append(
event2.Attributes,
tmkv.Pair{Key: []byte(types.AttributeKeySender), Value: []byte(addr2.String())})
event3 := sdk.Event{
Type: types.EventTypeTransfer,
Attributes: []tmkv.Pair{},
}
event3.Attributes = append(
event3.Attributes,
tmkv.Pair{Key: []byte(types.AttributeKeyRecipient), Value: []byte(addr3.String())})
event3.Attributes = append(
event3.Attributes,
tmkv.Pair{Key: []byte(sdk.AttributeKeyAmount), Value: []byte(newCoins.String())})
event4 := sdk.Event{
Type: types.EventTypeTransfer,
Attributes: []tmkv.Pair{},
}
event4.Attributes = append(
event4.Attributes,
tmkv.Pair{Key: []byte(types.AttributeKeyRecipient), Value: []byte(addr4.String())})
event4.Attributes = append(
event4.Attributes,
tmkv.Pair{Key: []byte(sdk.AttributeKeyAmount), Value: []byte(newCoins2.String())})
require.Equal(t, event1, events[1])
require.Equal(t, event2, events[2])
require.Equal(t, event3, events[3])
require.Equal(t, event4, events[4])
}
2018-04-09 15:10:58 -07:00
func TestViewKeeper(t *testing.T) {
2019-08-19 06:29:17 -07:00
app, ctx := createTestApp(false)
//paramSpace := app.ParamsKeeper.Subspace(types.DefaultParamspace)
viewKeeper := keep.NewBaseViewKeeper(app.AccountKeeper)
2018-04-09 15:10:58 -07:00
2018-07-06 00:06:53 -07:00
addr := sdk.AccAddress([]byte("addr1"))
2019-08-19 06:29:17 -07:00
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
2018-04-09 15:10:58 -07:00
// Test GetCoins/SetCoins
2019-08-19 06:29:17 -07:00
app.AccountKeeper.SetAccount(ctx, acc)
2019-03-07 16:55:08 -08:00
require.True(t, viewKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins()))
2018-04-09 15:10:58 -07:00
2019-08-19 06:29:17 -07:00
app.BankKeeper.SetCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10)))
2019-03-07 16:55:08 -08:00
require.True(t, viewKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10))))
2018-04-09 15:10:58 -07:00
// Test HasCoins
2019-03-07 16:55:08 -08:00
require.True(t, viewKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 10))))
require.True(t, viewKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 5))))
require.False(t, viewKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 15))))
require.False(t, viewKeeper.HasCoins(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 5))))
2018-04-09 15:10:58 -07:00
}
func TestVestingAccountSend(t *testing.T) {
2019-08-19 06:29:17 -07:00
app, ctx := createTestApp(false)
now := tmtime.Now()
2019-08-19 06:29:17 -07:00
ctx = ctx.WithBlockHeader(abci.Header{Time: now})
endTime := now.Add(24 * time.Hour)
2019-03-14 07:32:46 -07:00
origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
sendCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
addr1 := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
bacc := auth.NewBaseAccountWithAddress(addr1)
bacc.SetCoins(origCoins)
vacc := vesting.NewContinuousVestingAccount(&bacc, ctx.BlockHeader().Time.Unix(), endTime.Unix())
2019-08-19 06:29:17 -07:00
app.AccountKeeper.SetAccount(ctx, vacc)
// require that no coins be sendable at the beginning of the vesting schedule
2019-08-19 06:29:17 -07:00
err := app.BankKeeper.SendCoins(ctx, addr1, addr2, sendCoins)
require.Error(t, err)
// receive some coins
vacc.SetCoins(origCoins.Add(sendCoins...))
2019-08-19 06:29:17 -07:00
app.AccountKeeper.SetAccount(ctx, vacc)
// require that all vested coins are spendable plus any received
ctx = ctx.WithBlockTime(now.Add(12 * time.Hour))
2019-08-19 06:29:17 -07:00
err = app.BankKeeper.SendCoins(ctx, addr1, addr2, sendCoins)
vacc = app.AccountKeeper.GetAccount(ctx, addr1).(*vesting.ContinuousVestingAccount)
require.NoError(t, err)
require.Equal(t, origCoins, vacc.GetCoins())
}
func TestPeriodicVestingAccountSend(t *testing.T) {
app, ctx := createTestApp(false)
now := tmtime.Now()
ctx = ctx.WithBlockHeader(abci.Header{Time: now})
origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
sendCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
addr1 := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
bacc := auth.NewBaseAccountWithAddress(addr1)
bacc.SetCoins(origCoins)
periods := vesting.Periods{
vesting.Period{Length: int64(12 * 60 * 60), Amount: sdk.Coins{sdk.NewInt64Coin("stake", 50)}},
vesting.Period{Length: int64(6 * 60 * 60), Amount: sdk.Coins{sdk.NewInt64Coin("stake", 25)}},
vesting.Period{Length: int64(6 * 60 * 60), Amount: sdk.Coins{sdk.NewInt64Coin("stake", 25)}},
}
vacc := vesting.NewPeriodicVestingAccount(&bacc, ctx.BlockHeader().Time.Unix(), periods)
app.AccountKeeper.SetAccount(ctx, vacc)
// require that no coins be sendable at the beginning of the vesting schedule
err := app.BankKeeper.SendCoins(ctx, addr1, addr2, sendCoins)
require.Error(t, err)
// receive some coins
vacc.SetCoins(origCoins.Add(sendCoins...))
app.AccountKeeper.SetAccount(ctx, vacc)
// require that all vested coins are spendable plus any received
ctx = ctx.WithBlockTime(now.Add(12 * time.Hour))
err = app.BankKeeper.SendCoins(ctx, addr1, addr2, sendCoins)
vacc = app.AccountKeeper.GetAccount(ctx, addr1).(*vesting.PeriodicVestingAccount)
require.NoError(t, err)
require.Equal(t, origCoins, vacc.GetCoins())
}
func TestVestingAccountReceive(t *testing.T) {
2019-08-19 06:29:17 -07:00
app, ctx := createTestApp(false)
now := tmtime.Now()
2019-08-19 06:29:17 -07:00
ctx = ctx.WithBlockHeader(abci.Header{Time: now})
endTime := now.Add(24 * time.Hour)
2019-03-14 07:32:46 -07:00
origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
sendCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
addr1 := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
bacc := auth.NewBaseAccountWithAddress(addr1)
bacc.SetCoins(origCoins)
vacc := vesting.NewContinuousVestingAccount(&bacc, ctx.BlockHeader().Time.Unix(), endTime.Unix())
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
app.AccountKeeper.SetAccount(ctx, vacc)
app.AccountKeeper.SetAccount(ctx, acc)
app.BankKeeper.SetCoins(ctx, addr2, origCoins)
// send some coins to the vesting account
app.BankKeeper.SendCoins(ctx, addr2, addr1, sendCoins)
// require the coins are spendable
vacc = app.AccountKeeper.GetAccount(ctx, addr1).(*vesting.ContinuousVestingAccount)
require.Equal(t, origCoins.Add(sendCoins...), vacc.GetCoins())
require.Equal(t, vacc.SpendableCoins(now), sendCoins)
// require coins are spendable plus any that have vested
require.Equal(t, vacc.SpendableCoins(now.Add(12*time.Hour)), origCoins)
}
func TestPeriodicVestingAccountReceive(t *testing.T) {
app, ctx := createTestApp(false)
now := tmtime.Now()
ctx = ctx.WithBlockHeader(abci.Header{Time: now})
origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
sendCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
addr1 := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
bacc := auth.NewBaseAccountWithAddress(addr1)
bacc.SetCoins(origCoins)
periods := vesting.Periods{
vesting.Period{Length: int64(12 * 60 * 60), Amount: sdk.Coins{sdk.NewInt64Coin("stake", 50)}},
vesting.Period{Length: int64(6 * 60 * 60), Amount: sdk.Coins{sdk.NewInt64Coin("stake", 25)}},
vesting.Period{Length: int64(6 * 60 * 60), Amount: sdk.Coins{sdk.NewInt64Coin("stake", 25)}},
}
vacc := vesting.NewPeriodicVestingAccount(&bacc, ctx.BlockHeader().Time.Unix(), periods)
2019-08-19 06:29:17 -07:00
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
app.AccountKeeper.SetAccount(ctx, vacc)
app.AccountKeeper.SetAccount(ctx, acc)
app.BankKeeper.SetCoins(ctx, addr2, origCoins)
// send some coins to the vesting account
2019-08-19 06:29:17 -07:00
app.BankKeeper.SendCoins(ctx, addr2, addr1, sendCoins)
// require the coins are spendable
vacc = app.AccountKeeper.GetAccount(ctx, addr1).(*vesting.PeriodicVestingAccount)
require.Equal(t, origCoins.Add(sendCoins...), vacc.GetCoins())
require.Equal(t, vacc.SpendableCoins(now), sendCoins)
// require coins are spendable plus any that have vested
require.Equal(t, vacc.SpendableCoins(now.Add(12*time.Hour)), origCoins)
}
func TestDelegateCoins(t *testing.T) {
2019-08-19 06:29:17 -07:00
app, ctx := createTestApp(false)
now := tmtime.Now()
2019-08-19 06:29:17 -07:00
ctx = ctx.WithBlockHeader(abci.Header{Time: now})
endTime := now.Add(24 * time.Hour)
2019-08-19 06:29:17 -07:00
ak := app.AccountKeeper
2019-03-14 07:32:46 -07:00
origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
delCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
addr1 := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
2019-06-28 13:11:27 -07:00
addrModule := sdk.AccAddress([]byte("moduleAcc"))
bacc := auth.NewBaseAccountWithAddress(addr1)
bacc.SetCoins(origCoins)
2019-08-19 06:29:17 -07:00
macc := ak.NewAccountWithAddress(ctx, addrModule) // we don't need to define an actual module account bc we just need the address for testing
vacc := vesting.NewContinuousVestingAccount(&bacc, ctx.BlockHeader().Time.Unix(), endTime.Unix())
2019-08-19 06:29:17 -07:00
acc := ak.NewAccountWithAddress(ctx, addr2)
ak.SetAccount(ctx, vacc)
ak.SetAccount(ctx, acc)
ak.SetAccount(ctx, macc)
app.BankKeeper.SetCoins(ctx, addr2, origCoins)
ctx = ctx.WithBlockTime(now.Add(12 * time.Hour))
// require the ability for a non-vesting account to delegate
2019-08-19 06:29:17 -07:00
err := app.BankKeeper.DelegateCoins(ctx, addr2, addrModule, delCoins)
acc = ak.GetAccount(ctx, addr2)
macc = ak.GetAccount(ctx, addrModule)
require.NoError(t, err)
2019-06-28 13:11:27 -07:00
require.Equal(t, origCoins.Sub(delCoins), acc.GetCoins())
require.Equal(t, delCoins, macc.GetCoins())
// require the ability for a vesting account to delegate
2019-08-19 06:29:17 -07:00
err = app.BankKeeper.DelegateCoins(ctx, addr1, addrModule, delCoins)
vacc = ak.GetAccount(ctx, addr1).(*vesting.ContinuousVestingAccount)
require.NoError(t, err)
require.Equal(t, delCoins, vacc.GetCoins())
}
func TestUndelegateCoins(t *testing.T) {
2019-08-19 06:29:17 -07:00
app, ctx := createTestApp(false)
now := tmtime.Now()
2019-08-19 06:29:17 -07:00
ctx = ctx.WithBlockHeader(abci.Header{Time: now})
endTime := now.Add(24 * time.Hour)
2019-08-19 06:29:17 -07:00
ak := app.AccountKeeper
2019-03-14 07:32:46 -07:00
origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
delCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
addr1 := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
2019-06-28 13:11:27 -07:00
addrModule := sdk.AccAddress([]byte("moduleAcc"))
bacc := auth.NewBaseAccountWithAddress(addr1)
bacc.SetCoins(origCoins)
2019-08-19 06:29:17 -07:00
macc := ak.NewAccountWithAddress(ctx, addrModule) // we don't need to define an actual module account bc we just need the address for testing
vacc := vesting.NewContinuousVestingAccount(&bacc, ctx.BlockHeader().Time.Unix(), endTime.Unix())
2019-08-19 06:29:17 -07:00
acc := ak.NewAccountWithAddress(ctx, addr2)
ak.SetAccount(ctx, vacc)
ak.SetAccount(ctx, acc)
ak.SetAccount(ctx, macc)
app.BankKeeper.SetCoins(ctx, addr2, origCoins)
ctx = ctx.WithBlockTime(now.Add(12 * time.Hour))
// require the ability for a non-vesting account to delegate
2019-08-19 06:29:17 -07:00
err := app.BankKeeper.DelegateCoins(ctx, addr2, addrModule, delCoins)
require.NoError(t, err)
2019-08-19 06:29:17 -07:00
acc = ak.GetAccount(ctx, addr2)
macc = ak.GetAccount(ctx, addrModule)
2019-06-28 13:11:27 -07:00
require.Equal(t, origCoins.Sub(delCoins), acc.GetCoins())
require.Equal(t, delCoins, macc.GetCoins())
// require the ability for a non-vesting account to undelegate
2019-08-19 06:29:17 -07:00
err = app.BankKeeper.UndelegateCoins(ctx, addrModule, addr2, delCoins)
require.NoError(t, err)
2019-08-19 06:29:17 -07:00
acc = ak.GetAccount(ctx, addr2)
macc = ak.GetAccount(ctx, addrModule)
require.Equal(t, origCoins, acc.GetCoins())
2019-06-28 13:11:27 -07:00
require.True(t, macc.GetCoins().Empty())
// require the ability for a vesting account to delegate
2019-08-19 06:29:17 -07:00
err = app.BankKeeper.DelegateCoins(ctx, addr1, addrModule, delCoins)
require.NoError(t, err)
vacc = ak.GetAccount(ctx, addr1).(*vesting.ContinuousVestingAccount)
2019-08-19 06:29:17 -07:00
macc = ak.GetAccount(ctx, addrModule)
2019-06-28 13:11:27 -07:00
require.Equal(t, origCoins.Sub(delCoins), vacc.GetCoins())
require.Equal(t, delCoins, macc.GetCoins())
// require the ability for a vesting account to undelegate
2019-08-19 06:29:17 -07:00
err = app.BankKeeper.UndelegateCoins(ctx, addrModule, addr1, delCoins)
require.NoError(t, err)
vacc = ak.GetAccount(ctx, addr1).(*vesting.ContinuousVestingAccount)
2019-08-19 06:29:17 -07:00
macc = ak.GetAccount(ctx, addrModule)
require.Equal(t, origCoins, vacc.GetCoins())
2019-06-28 13:11:27 -07:00
require.True(t, macc.GetCoins().Empty())
}