2020-03-05 06:00:45 -08:00
|
|
|
package distribution_test
|
2019-05-21 03:02:10 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-04-20 08:22:12 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-05-21 03:02:10 -07:00
|
|
|
"github.com/tendermint/tendermint/crypto/ed25519"
|
2020-08-14 10:58:53 -07:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2019-05-21 03:02:10 -07:00
|
|
|
|
2020-04-20 08:22:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
2019-05-21 03:02:10 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-04-20 08:22:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/distribution"
|
2019-05-21 03:02:10 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
delPk1 = ed25519.GenPrivKey().PubKey()
|
|
|
|
delAddr1 = sdk.AccAddress(delPk1.Address())
|
2019-06-28 13:11:27 -07:00
|
|
|
|
|
|
|
amount = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1)))
|
2019-05-21 03:02:10 -07:00
|
|
|
)
|
|
|
|
|
2020-04-14 12:05:14 -07:00
|
|
|
func testProposal(recipient sdk.AccAddress, amount sdk.Coins) *types.CommunityPoolSpendProposal {
|
2020-01-30 13:31:16 -08:00
|
|
|
return types.NewCommunityPoolSpendProposal("Test", "description", recipient, amount)
|
2019-05-21 03:02:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProposalHandlerPassed(t *testing.T) {
|
2020-03-05 06:00:45 -08:00
|
|
|
app := simapp.Setup(false)
|
2020-08-14 10:58:53 -07:00
|
|
|
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
2020-03-05 06:00:45 -08:00
|
|
|
|
2019-05-21 03:02:10 -07:00
|
|
|
recipient := delAddr1
|
2019-06-28 13:11:27 -07:00
|
|
|
|
|
|
|
// add coins to the module account
|
2020-03-05 06:00:45 -08:00
|
|
|
macc := app.DistrKeeper.GetDistributionAccount(ctx)
|
|
|
|
balances := app.BankKeeper.GetAllBalances(ctx, macc.GetAddress())
|
|
|
|
err := app.BankKeeper.SetBalances(ctx, macc.GetAddress(), balances.Add(amount...))
|
2019-06-28 13:11:27 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-04-20 12:32:10 -07:00
|
|
|
app.AccountKeeper.SetModuleAccount(ctx, macc)
|
2019-05-21 03:02:10 -07:00
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
account := app.AccountKeeper.NewAccountWithAddress(ctx, recipient)
|
|
|
|
app.AccountKeeper.SetAccount(ctx, account)
|
|
|
|
require.True(t, app.BankKeeper.GetAllBalances(ctx, account.GetAddress()).IsZero())
|
2019-05-21 03:02:10 -07:00
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
feePool := app.DistrKeeper.GetFeePool(ctx)
|
2020-01-03 12:44:53 -08:00
|
|
|
feePool.CommunityPool = sdk.NewDecCoinsFromCoins(amount...)
|
2020-03-05 06:00:45 -08:00
|
|
|
app.DistrKeeper.SetFeePool(ctx, feePool)
|
2019-05-21 03:02:10 -07:00
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
tp := testProposal(recipient, amount)
|
2020-03-05 06:00:45 -08:00
|
|
|
hdlr := distribution.NewCommunityPoolSpendProposalHandler(app.DistrKeeper)
|
2019-05-21 03:02:10 -07:00
|
|
|
require.NoError(t, hdlr(ctx, tp))
|
2020-01-30 13:31:16 -08:00
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
balances = app.BankKeeper.GetAllBalances(ctx, recipient)
|
2020-01-30 13:31:16 -08:00
|
|
|
require.Equal(t, balances, amount)
|
2019-05-21 03:02:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProposalHandlerFailed(t *testing.T) {
|
2020-03-05 06:00:45 -08:00
|
|
|
app := simapp.Setup(false)
|
2020-08-14 10:58:53 -07:00
|
|
|
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
2020-03-05 06:00:45 -08:00
|
|
|
|
2019-05-21 03:02:10 -07:00
|
|
|
recipient := delAddr1
|
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
account := app.AccountKeeper.NewAccountWithAddress(ctx, recipient)
|
|
|
|
app.AccountKeeper.SetAccount(ctx, account)
|
|
|
|
require.True(t, app.BankKeeper.GetAllBalances(ctx, account.GetAddress()).IsZero())
|
2019-05-21 03:02:10 -07:00
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
tp := testProposal(recipient, amount)
|
2020-03-05 06:00:45 -08:00
|
|
|
hdlr := distribution.NewCommunityPoolSpendProposalHandler(app.DistrKeeper)
|
2019-05-21 03:02:10 -07:00
|
|
|
require.Error(t, hdlr(ctx, tp))
|
2020-01-30 13:31:16 -08:00
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
balances := app.BankKeeper.GetAllBalances(ctx, recipient)
|
2020-01-30 13:31:16 -08:00
|
|
|
require.True(t, balances.IsZero())
|
2019-05-21 03:02:10 -07:00
|
|
|
}
|