2020-08-21 22:56:20 +00:00
|
|
|
package issuance_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
sdkmath "cosmossdk.io/math"
|
2020-08-21 22:56:20 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2020-08-21 22:56:20 +00:00
|
|
|
tmtime "github.com/tendermint/tendermint/types/time"
|
|
|
|
|
|
|
|
"github.com/kava-labs/kava/app"
|
|
|
|
"github.com/kava-labs/kava/x/issuance"
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/kava-labs/kava/x/issuance/keeper"
|
|
|
|
"github.com/kava-labs/kava/x/issuance/types"
|
2020-08-21 22:56:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Test suite used for all keeper tests
|
|
|
|
type ABCITestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
keeper keeper.Keeper
|
2020-08-21 22:56:20 +00:00
|
|
|
app app.TestApp
|
|
|
|
ctx sdk.Context
|
|
|
|
addrs []sdk.AccAddress
|
|
|
|
modAccount sdk.AccAddress
|
|
|
|
blockTime time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// The default state used by each test
|
|
|
|
func (suite *ABCITestSuite) SetupTest() {
|
|
|
|
tApp := app.NewTestApp()
|
|
|
|
blockTime := tmtime.Now()
|
2022-01-08 00:39:27 +00:00
|
|
|
ctx := tApp.NewContext(true, tmproto.Header{Height: 1, Time: blockTime})
|
2020-08-21 22:56:20 +00:00
|
|
|
tApp.InitializeFromGenesisStates()
|
|
|
|
_, addrs := app.GeneratePrivKeyAddressPairs(5)
|
|
|
|
keeper := tApp.GetIssuanceKeeper()
|
|
|
|
modAccount, err := sdk.AccAddressFromBech32("kava1cj7njkw2g9fqx4e768zc75dp9sks8u9znxrf0w")
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.app = tApp
|
|
|
|
suite.ctx = ctx
|
|
|
|
suite.keeper = keeper
|
|
|
|
suite.addrs = addrs
|
|
|
|
suite.modAccount = modAccount
|
|
|
|
suite.blockTime = blockTime
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *ABCITestSuite) TestRateLimitingTimePassage() {
|
|
|
|
type args struct {
|
2022-01-08 00:39:27 +00:00
|
|
|
assets []types.Asset
|
|
|
|
supplies []types.AssetSupply
|
2020-08-21 22:56:20 +00:00
|
|
|
blockTimes []time.Duration
|
2022-01-08 00:39:27 +00:00
|
|
|
expectedSupply types.AssetSupply
|
2020-08-21 22:56:20 +00:00
|
|
|
}
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"time passage same period",
|
|
|
|
args{
|
2022-01-08 00:39:27 +00:00
|
|
|
assets: []types.Asset{
|
2023-04-05 23:21:59 +00:00
|
|
|
types.NewAsset(suite.addrs[0].String(), "usdtoken", []string{suite.addrs[1].String()}, false, true, types.NewRateLimit(true, sdkmath.NewInt(10000000000), time.Hour*24)),
|
2020-08-21 22:56:20 +00:00
|
|
|
},
|
2022-01-08 00:39:27 +00:00
|
|
|
supplies: []types.AssetSupply{
|
|
|
|
types.NewAssetSupply(sdk.NewCoin("usdtoken", sdk.ZeroInt()), time.Hour),
|
2020-08-21 22:56:20 +00:00
|
|
|
},
|
|
|
|
blockTimes: []time.Duration{time.Hour},
|
2022-01-08 00:39:27 +00:00
|
|
|
expectedSupply: types.NewAssetSupply(sdk.NewCoin("usdtoken", sdk.ZeroInt()), time.Hour*2),
|
2020-08-21 22:56:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"time passage new period",
|
|
|
|
args{
|
2022-01-08 00:39:27 +00:00
|
|
|
assets: []types.Asset{
|
2023-04-05 23:21:59 +00:00
|
|
|
types.NewAsset(suite.addrs[0].String(), "usdtoken", []string{suite.addrs[1].String()}, false, true, types.NewRateLimit(true, sdkmath.NewInt(10000000000), time.Hour*24)),
|
2020-08-21 22:56:20 +00:00
|
|
|
},
|
2022-01-08 00:39:27 +00:00
|
|
|
supplies: []types.AssetSupply{
|
|
|
|
types.NewAssetSupply(sdk.NewCoin("usdtoken", sdk.ZeroInt()), time.Hour),
|
2020-08-21 22:56:20 +00:00
|
|
|
},
|
|
|
|
blockTimes: []time.Duration{time.Hour * 12, time.Hour * 12},
|
2022-01-08 00:39:27 +00:00
|
|
|
expectedSupply: types.NewAssetSupply(sdk.NewCoin("usdtoken", sdk.ZeroInt()), time.Duration(0)),
|
2020-08-21 22:56:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(tc.name, func() {
|
|
|
|
suite.SetupTest()
|
2022-01-08 00:39:27 +00:00
|
|
|
params := types.NewParams(tc.args.assets)
|
2020-08-21 22:56:20 +00:00
|
|
|
suite.keeper.SetParams(suite.ctx, params)
|
|
|
|
for _, supply := range tc.args.supplies {
|
|
|
|
suite.keeper.SetAssetSupply(suite.ctx, supply, supply.GetDenom())
|
|
|
|
}
|
|
|
|
suite.keeper.SetPreviousBlockTime(suite.ctx, suite.blockTime)
|
|
|
|
for _, bt := range tc.args.blockTimes {
|
|
|
|
nextBlockTime := suite.ctx.BlockTime().Add(bt)
|
|
|
|
suite.ctx = suite.ctx.WithBlockTime(nextBlockTime)
|
|
|
|
suite.Require().NotPanics(func() {
|
|
|
|
issuance.BeginBlocker(suite.ctx, suite.keeper)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
actualSupply, found := suite.keeper.GetAssetSupply(suite.ctx, tc.args.expectedSupply.GetDenom())
|
|
|
|
suite.Require().True(found)
|
|
|
|
suite.Require().Equal(tc.args.expectedSupply, actualSupply)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestABCITestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(ABCITestSuite))
|
|
|
|
}
|