2019-08-19 06:29:17 -07:00
package keeper_test
2018-04-09 15:10:58 -07:00
import (
"testing"
2019-01-14 08:11:24 -08:00
"time"
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
"github.com/stretchr/testify/suite"
2019-01-02 13:19:48 -08:00
abci "github.com/tendermint/tendermint/abci/types"
2020-08-14 10:58:53 -07:00
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
2019-01-14 08:11:24 -08:00
tmtime "github.com/tendermint/tendermint/types/time"
2019-01-02 13:19:48 -08:00
2020-07-28 05:53:35 -07:00
"github.com/cosmos/cosmos-sdk/baseapp"
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"
2021-06-29 03:23:13 -07:00
"github.com/cosmos/cosmos-sdk/types/query"
2020-06-17 11:42:27 -07:00
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
2021-06-29 03:23:13 -07:00
"github.com/cosmos/cosmos-sdk/x/auth/vesting/exported"
2020-07-02 06:08:44 -07:00
vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
2020-06-14 16:06:16 -07:00
"github.com/cosmos/cosmos-sdk/x/bank/keeper"
2021-06-29 03:23:13 -07:00
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
2020-02-20 06:24:26 -08:00
"github.com/cosmos/cosmos-sdk/x/bank/types"
2021-06-29 03:23:13 -07:00
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
2018-04-09 15:10:58 -07:00
)
2020-01-30 13:31:16 -08:00
const (
2020-04-20 08:22:12 -07:00
fooDenom = "foo"
barDenom = "bar"
initialPower = int64 ( 100 )
holder = "holder"
multiPerm = "multiple permissions account"
randomPerm = "random permission"
)
var (
2020-06-17 11:42:27 -07:00
holderAcc = authtypes . NewEmptyModuleAccount ( holder )
burnerAcc = authtypes . NewEmptyModuleAccount ( authtypes . Burner , authtypes . Burner )
minterAcc = authtypes . NewEmptyModuleAccount ( authtypes . Minter , authtypes . Minter )
multiPermAcc = authtypes . NewEmptyModuleAccount ( multiPerm , authtypes . Burner , authtypes . Minter , authtypes . Staking )
randomPermAcc = authtypes . NewEmptyModuleAccount ( randomPerm , "random" )
2020-04-20 08:22:12 -07:00
2021-02-01 06:00:45 -08:00
// The default power validators are initialized to have within tests
2021-01-27 02:24:00 -08:00
initTokens = sdk . TokensFromConsensusPower ( initialPower , sdk . DefaultPowerReduction )
2020-04-20 08:22:12 -07:00
initCoins = sdk . NewCoins ( sdk . NewCoin ( sdk . DefaultBondDenom , initTokens ) )
2020-01-30 13:31:16 -08:00
)
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
func newFooCoin ( amt int64 ) sdk . Coin {
return sdk . NewInt64Coin ( fooDenom , amt )
}
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
func newBarCoin ( amt int64 ) sdk . Coin {
return sdk . NewInt64Coin ( barDenom , amt )
}
2018-04-09 15:10:58 -07:00
2020-04-20 08:22:12 -07:00
// nolint: interfacer
2020-06-14 16:06:16 -07:00
func getCoinsByName ( ctx sdk . Context , bk keeper . Keeper , ak types . AccountKeeper , moduleName string ) sdk . Coins {
2020-04-20 12:32:10 -07:00
moduleAddress := ak . GetModuleAddress ( moduleName )
2020-04-20 08:22:12 -07:00
macc := ak . GetAccount ( ctx , moduleAddress )
if macc == nil {
return sdk . Coins ( nil )
}
return bk . GetAllBalances ( ctx , macc . GetAddress ( ) )
}
2020-01-30 13:31:16 -08:00
type IntegrationTestSuite struct {
suite . Suite
2018-04-09 15:10:58 -07:00
2020-07-28 05:53:35 -07:00
app * simapp . SimApp
ctx sdk . Context
queryClient types . QueryClient
2020-01-30 13:31:16 -08:00
}
2018-04-09 15:10:58 -07:00
2021-04-30 09:54:08 -07:00
func ( suite * IntegrationTestSuite ) initKeepersWithmAccPerms ( blockedAddrs map [ string ] bool ) ( authkeeper . AccountKeeper , keeper . BaseKeeper ) {
app := suite . app
maccPerms := simapp . GetMaccPerms ( )
2021-07-01 01:52:38 -07:00
appCodec := simapp . MakeTestEncodingConfig ( ) . Codec
2021-04-30 09:54:08 -07:00
maccPerms [ holder ] = nil
maccPerms [ authtypes . Burner ] = [ ] string { authtypes . Burner }
maccPerms [ authtypes . Minter ] = [ ] string { authtypes . Minter }
maccPerms [ multiPerm ] = [ ] string { authtypes . Burner , authtypes . Minter , authtypes . Staking }
maccPerms [ randomPerm ] = [ ] string { "random" }
authKeeper := authkeeper . NewAccountKeeper (
appCodec , app . GetKey ( types . StoreKey ) , app . GetSubspace ( types . ModuleName ) ,
authtypes . ProtoBaseAccount , maccPerms ,
)
keeper := keeper . NewBaseKeeper (
appCodec , app . GetKey ( types . StoreKey ) , authKeeper ,
app . GetSubspace ( types . ModuleName ) , blockedAddrs ,
)
return authKeeper , keeper
}
2020-01-30 13:31:16 -08:00
func ( suite * IntegrationTestSuite ) SetupTest ( ) {
app := simapp . Setup ( false )
2020-08-14 10:58:53 -07:00
ctx := app . BaseApp . NewContext ( false , tmproto . Header { } )
2018-04-09 15:10:58 -07:00
2020-06-17 11:42:27 -07:00
app . AccountKeeper . SetParams ( ctx , authtypes . DefaultParams ( ) )
2020-07-08 10:43:08 -07:00
app . BankKeeper . SetParams ( ctx , types . DefaultParams ( ) )
2018-04-09 15:10:58 -07:00
2020-07-28 05:53:35 -07:00
queryHelper := baseapp . NewQueryServerTestHelper ( ctx , app . InterfaceRegistry ( ) )
types . RegisterQueryServer ( queryHelper , app . BankKeeper )
queryClient := types . NewQueryClient ( queryHelper )
2020-01-30 13:31:16 -08:00
suite . app = app
suite . ctx = ctx
2020-07-28 05:53:35 -07:00
suite . queryClient = queryClient
2020-01-30 13:31:16 -08:00
}
2018-04-09 15:10:58 -07:00
2020-04-20 08:22:12 -07:00
func ( suite * IntegrationTestSuite ) TestSupply ( ) {
2021-05-10 09:17:55 -07:00
ctx := suite . ctx
2021-04-30 09:54:08 -07:00
require := suite . Require ( )
// add module accounts to supply keeper
authKeeper , keeper := suite . initKeepersWithmAccPerms ( make ( map [ string ] bool ) )
2020-04-20 08:22:12 -07:00
initialPower := int64 ( 100 )
2021-05-10 09:17:55 -07:00
initTokens := suite . app . StakingKeeper . TokensFromConsensusPower ( ctx , initialPower )
2020-04-20 08:22:12 -07:00
totalSupply := sdk . NewCoins ( sdk . NewCoin ( sdk . DefaultBondDenom , initTokens ) )
2021-04-30 09:54:08 -07:00
// set burnerAcc balance
authKeeper . SetModuleAccount ( ctx , burnerAcc )
require . NoError ( keeper . MintCoins ( ctx , authtypes . Minter , totalSupply ) )
require . NoError ( keeper . SendCoinsFromModuleToAccount ( ctx , authtypes . Minter , burnerAcc . GetAddress ( ) , totalSupply ) )
total , _ , err := keeper . GetPaginatedTotalSupply ( ctx , & query . PageRequest { } )
require . NoError ( err )
require . Equal ( totalSupply , total )
// burning all supplied tokens
err = keeper . BurnCoins ( ctx , authtypes . Burner , totalSupply )
require . NoError ( err )
total , _ , err = keeper . GetPaginatedTotalSupply ( ctx , & query . PageRequest { } )
require . NoError ( err )
require . Equal ( total . String ( ) , "" )
2020-04-20 08:22:12 -07:00
}
2021-05-10 09:17:55 -07:00
func ( suite * IntegrationTestSuite ) TestSendCoinsFromModuleToAccount_Blocklist ( ) {
2021-04-30 09:54:08 -07:00
ctx := suite . ctx
2021-03-24 09:05:33 -07:00
// add module accounts to supply keeper
addr1 := sdk . AccAddress ( [ ] byte ( "addr1_______________" ) )
2021-04-30 09:54:08 -07:00
_ , keeper := suite . initKeepersWithmAccPerms ( map [ string ] bool { addr1 . String ( ) : true } )
2021-03-24 09:05:33 -07:00
suite . Require ( ) . NoError ( keeper . MintCoins ( ctx , minttypes . ModuleName , initCoins ) )
suite . Require ( ) . Error ( keeper . SendCoinsFromModuleToAccount (
ctx , minttypes . ModuleName , addr1 , initCoins ,
) )
}
2020-04-20 08:22:12 -07:00
func ( suite * IntegrationTestSuite ) TestSupply_SendCoins ( ) {
2021-04-30 09:54:08 -07:00
ctx := suite . ctx
2020-04-20 08:22:12 -07:00
// add module accounts to supply keeper
2021-04-30 09:54:08 -07:00
authKeeper , keeper := suite . initKeepersWithmAccPerms ( make ( map [ string ] bool ) )
2020-04-20 08:22:12 -07:00
2020-06-17 11:42:27 -07:00
baseAcc := authKeeper . NewAccountWithAddress ( ctx , authtypes . NewModuleAddress ( "baseAcc" ) )
2020-04-20 08:22:12 -07:00
2021-02-17 10:20:33 -08:00
// set initial balances
suite .
Require ( ) .
NoError ( keeper . MintCoins ( ctx , minttypes . ModuleName , initCoins ) )
suite .
Require ( ) .
NoError ( keeper . SendCoinsFromModuleToAccount ( ctx , minttypes . ModuleName , holderAcc . GetAddress ( ) , initCoins ) )
2020-04-20 12:32:10 -07:00
authKeeper . SetModuleAccount ( ctx , holderAcc )
authKeeper . SetModuleAccount ( ctx , burnerAcc )
authKeeper . SetAccount ( ctx , baseAcc )
2020-04-20 08:22:12 -07:00
suite . Require ( ) . Panics ( func ( ) {
2021-02-17 10:20:33 -08:00
_ = keeper . SendCoinsFromModuleToModule ( ctx , "" , holderAcc . GetName ( ) , initCoins ) // nolint:errcheck
2020-04-20 08:22:12 -07:00
} )
suite . Require ( ) . Panics ( func ( ) {
2021-02-17 10:20:33 -08:00
_ = keeper . SendCoinsFromModuleToModule ( ctx , authtypes . Burner , "" , initCoins ) // nolint:errcheck
2020-04-20 08:22:12 -07:00
} )
suite . Require ( ) . Panics ( func ( ) {
2021-02-17 10:20:33 -08:00
_ = keeper . SendCoinsFromModuleToAccount ( ctx , "" , baseAcc . GetAddress ( ) , initCoins ) // nolint:errcheck
2020-04-20 08:22:12 -07:00
} )
suite . Require ( ) . Error (
keeper . SendCoinsFromModuleToAccount ( ctx , holderAcc . GetName ( ) , baseAcc . GetAddress ( ) , initCoins . Add ( initCoins ... ) ) ,
)
suite . Require ( ) . NoError (
2020-06-17 11:42:27 -07:00
keeper . SendCoinsFromModuleToModule ( ctx , holderAcc . GetName ( ) , authtypes . Burner , initCoins ) ,
2020-04-20 08:22:12 -07:00
)
2021-02-25 10:15:02 -08:00
suite . Require ( ) . Equal ( sdk . NewCoins ( ) . String ( ) , getCoinsByName ( ctx , keeper , authKeeper , holderAcc . GetName ( ) ) . String ( ) )
2020-06-17 11:42:27 -07:00
suite . Require ( ) . Equal ( initCoins , getCoinsByName ( ctx , keeper , authKeeper , authtypes . Burner ) )
2020-04-20 08:22:12 -07:00
suite . Require ( ) . NoError (
2020-06-17 11:42:27 -07:00
keeper . SendCoinsFromModuleToAccount ( ctx , authtypes . Burner , baseAcc . GetAddress ( ) , initCoins ) ,
2020-04-20 08:22:12 -07:00
)
2021-02-25 10:15:02 -08:00
suite . Require ( ) . Equal ( sdk . NewCoins ( ) . String ( ) , getCoinsByName ( ctx , keeper , authKeeper , authtypes . Burner ) . String ( ) )
2020-04-20 08:22:12 -07:00
suite . Require ( ) . Equal ( initCoins , keeper . GetAllBalances ( ctx , baseAcc . GetAddress ( ) ) )
2020-06-17 11:42:27 -07:00
suite . Require ( ) . NoError ( keeper . SendCoinsFromAccountToModule ( ctx , baseAcc . GetAddress ( ) , authtypes . Burner , initCoins ) )
2021-02-25 10:15:02 -08:00
suite . Require ( ) . Equal ( sdk . NewCoins ( ) . String ( ) , keeper . GetAllBalances ( ctx , baseAcc . GetAddress ( ) ) . String ( ) )
2020-06-17 11:42:27 -07:00
suite . Require ( ) . Equal ( initCoins , getCoinsByName ( ctx , keeper , authKeeper , authtypes . Burner ) )
2020-04-20 08:22:12 -07:00
}
func ( suite * IntegrationTestSuite ) TestSupply_MintCoins ( ) {
2021-04-30 09:54:08 -07:00
ctx := suite . ctx
2020-04-20 08:22:12 -07:00
// add module accounts to supply keeper
2021-04-30 09:54:08 -07:00
authKeeper , keeper := suite . initKeepersWithmAccPerms ( make ( map [ string ] bool ) )
2020-04-20 08:22:12 -07:00
2020-04-20 12:32:10 -07:00
authKeeper . SetModuleAccount ( ctx , burnerAcc )
authKeeper . SetModuleAccount ( ctx , minterAcc )
authKeeper . SetModuleAccount ( ctx , multiPermAcc )
authKeeper . SetModuleAccount ( ctx , randomPermAcc )
2020-04-20 08:22:12 -07:00
2021-04-06 07:43:08 -07:00
initialSupply , _ , err := keeper . GetPaginatedTotalSupply ( ctx , & query . PageRequest { } )
suite . Require ( ) . NoError ( err )
2020-04-20 08:22:12 -07:00
2020-06-17 11:42:27 -07:00
suite . Require ( ) . Panics ( func ( ) { keeper . MintCoins ( ctx , "" , initCoins ) } , "no module account" ) // nolint:errcheck
suite . Require ( ) . Panics ( func ( ) { keeper . MintCoins ( ctx , authtypes . Burner , initCoins ) } , "invalid permission" ) // nolint:errcheck
2020-04-20 08:22:12 -07:00
2021-04-06 07:43:08 -07:00
err = keeper . MintCoins ( ctx , authtypes . Minter , sdk . Coins { sdk . Coin { Denom : "denom" , Amount : sdk . NewInt ( - 10 ) } } )
2020-04-20 08:22:12 -07:00
suite . Require ( ) . Error ( err , "insufficient coins" )
suite . Require ( ) . Panics ( func ( ) { keeper . MintCoins ( ctx , randomPerm , initCoins ) } ) // nolint:errcheck
2020-06-17 11:42:27 -07:00
err = keeper . MintCoins ( ctx , authtypes . Minter , initCoins )
2020-04-20 08:22:12 -07:00
suite . Require ( ) . NoError ( err )
2020-06-17 11:42:27 -07:00
suite . Require ( ) . Equal ( initCoins , getCoinsByName ( ctx , keeper , authKeeper , authtypes . Minter ) )
2021-04-06 07:43:08 -07:00
totalSupply , _ , err := keeper . GetPaginatedTotalSupply ( ctx , & query . PageRequest { } )
suite . Require ( ) . NoError ( err )
suite . Require ( ) . Equal ( initialSupply . Add ( initCoins ... ) , totalSupply )
2020-04-20 08:22:12 -07:00
// test same functionality on module account with multiple permissions
2021-04-06 07:43:08 -07:00
initialSupply , _ , err = keeper . GetPaginatedTotalSupply ( ctx , & query . PageRequest { } )
suite . Require ( ) . NoError ( err )
2020-04-20 08:22:12 -07:00
err = keeper . MintCoins ( ctx , multiPermAcc . GetName ( ) , initCoins )
suite . Require ( ) . NoError ( err )
2021-04-06 07:43:08 -07:00
totalSupply , _ , err = keeper . GetPaginatedTotalSupply ( ctx , & query . PageRequest { } )
suite . Require ( ) . NoError ( err )
2020-04-20 12:32:10 -07:00
suite . Require ( ) . Equal ( initCoins , getCoinsByName ( ctx , keeper , authKeeper , multiPermAcc . GetName ( ) ) )
2021-04-06 07:43:08 -07:00
suite . Require ( ) . Equal ( initialSupply . Add ( initCoins ... ) , totalSupply )
2020-06-17 11:42:27 -07:00
suite . Require ( ) . Panics ( func ( ) { keeper . MintCoins ( ctx , authtypes . Burner , initCoins ) } ) // nolint:errcheck
2020-04-20 08:22:12 -07:00
}
func ( suite * IntegrationTestSuite ) TestSupply_BurnCoins ( ) {
2021-04-30 09:54:08 -07:00
ctx := suite . ctx
2020-04-20 08:22:12 -07:00
// add module accounts to supply keeper
2021-04-30 09:54:08 -07:00
authKeeper , keeper := suite . initKeepersWithmAccPerms ( make ( map [ string ] bool ) )
2020-04-20 08:22:12 -07:00
2021-02-17 10:20:33 -08:00
// set burnerAcc balance
2020-04-20 12:32:10 -07:00
authKeeper . SetModuleAccount ( ctx , burnerAcc )
2021-02-17 10:20:33 -08:00
suite .
Require ( ) .
NoError ( keeper . MintCoins ( ctx , authtypes . Minter , initCoins ) )
suite .
Require ( ) .
NoError ( keeper . SendCoinsFromModuleToAccount ( ctx , authtypes . Minter , burnerAcc . GetAddress ( ) , initCoins ) )
// inflate supply
suite .
Require ( ) .
NoError ( keeper . MintCoins ( ctx , authtypes . Minter , initCoins ) )
2021-04-06 07:43:08 -07:00
supplyAfterInflation , _ , err := keeper . GetPaginatedTotalSupply ( ctx , & query . PageRequest { } )
2021-02-17 10:20:33 -08:00
2021-03-03 01:58:16 -08:00
suite . Require ( ) . Panics ( func ( ) { keeper . BurnCoins ( ctx , "" , initCoins ) } , "no module account" ) // nolint:errcheck
suite . Require ( ) . Panics ( func ( ) { keeper . BurnCoins ( ctx , authtypes . Minter , initCoins ) } , "invalid permission" ) // nolint:errcheck
suite . Require ( ) . Panics ( func ( ) { keeper . BurnCoins ( ctx , randomPerm , supplyAfterInflation ) } , "random permission" ) // nolint:errcheck
2021-04-06 07:43:08 -07:00
err = keeper . BurnCoins ( ctx , authtypes . Burner , supplyAfterInflation )
2020-04-20 08:22:12 -07:00
suite . Require ( ) . Error ( err , "insufficient coins" )
2020-06-17 11:42:27 -07:00
err = keeper . BurnCoins ( ctx , authtypes . Burner , initCoins )
2020-04-20 08:22:12 -07:00
suite . Require ( ) . NoError ( err )
2021-04-06 07:43:08 -07:00
supplyAfterBurn , _ , err := keeper . GetPaginatedTotalSupply ( ctx , & query . PageRequest { } )
suite . Require ( ) . NoError ( err )
2021-02-25 10:15:02 -08:00
suite . Require ( ) . Equal ( sdk . NewCoins ( ) . String ( ) , getCoinsByName ( ctx , keeper , authKeeper , authtypes . Burner ) . String ( ) )
2021-04-06 07:43:08 -07:00
suite . Require ( ) . Equal ( supplyAfterInflation . Sub ( initCoins ) , supplyAfterBurn )
2020-04-20 08:22:12 -07:00
// test same functionality on module account with multiple permissions
2021-02-17 10:20:33 -08:00
suite .
Require ( ) .
NoError ( keeper . MintCoins ( ctx , authtypes . Minter , initCoins ) )
2020-04-20 08:22:12 -07:00
2021-04-06 07:43:08 -07:00
supplyAfterInflation , _ , err = keeper . GetPaginatedTotalSupply ( ctx , & query . PageRequest { } )
suite . Require ( ) . NoError ( err )
2021-02-17 10:20:33 -08:00
suite . Require ( ) . NoError ( keeper . SendCoins ( ctx , authtypes . NewModuleAddress ( authtypes . Minter ) , multiPermAcc . GetAddress ( ) , initCoins ) )
2020-04-20 12:32:10 -07:00
authKeeper . SetModuleAccount ( ctx , multiPermAcc )
2020-04-20 08:22:12 -07:00
err = keeper . BurnCoins ( ctx , multiPermAcc . GetName ( ) , initCoins )
2021-04-06 07:43:08 -07:00
supplyAfterBurn , _ , err = keeper . GetPaginatedTotalSupply ( ctx , & query . PageRequest { } )
suite . Require ( ) . NoError ( err )
2020-04-20 08:22:12 -07:00
suite . Require ( ) . NoError ( err )
2021-02-25 10:15:02 -08:00
suite . Require ( ) . Equal ( sdk . NewCoins ( ) . String ( ) , getCoinsByName ( ctx , keeper , authKeeper , multiPermAcc . GetName ( ) ) . String ( ) )
2021-04-06 07:43:08 -07:00
suite . Require ( ) . Equal ( supplyAfterInflation . Sub ( initCoins ) , supplyAfterBurn )
2020-04-20 08:22:12 -07:00
}
2020-02-04 18:33:02 -08:00
func ( suite * IntegrationTestSuite ) TestSendCoinsNewAccount ( ) {
app , ctx := suite . app , suite . ctx
balances := sdk . NewCoins ( newFooCoin ( 100 ) , newBarCoin ( 50 ) )
2020-09-25 03:25:37 -07:00
addr1 := sdk . AccAddress ( [ ] byte ( "addr1_______________" ) )
2020-02-04 18:33:02 -08:00
acc1 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr1 )
app . AccountKeeper . SetAccount ( ctx , acc1 )
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr1 , balances ) )
2020-02-04 18:33:02 -08:00
acc1Balances := app . BankKeeper . GetAllBalances ( ctx , addr1 )
suite . Require ( ) . Equal ( balances , acc1Balances )
2020-09-25 03:25:37 -07:00
addr2 := sdk . AccAddress ( [ ] byte ( "addr2_______________" ) )
2020-02-04 18:33:02 -08:00
suite . Require ( ) . Nil ( app . AccountKeeper . GetAccount ( ctx , addr2 ) )
2020-06-20 16:44:04 -07:00
app . BankKeeper . GetAllBalances ( ctx , addr2 )
2020-02-04 18:33:02 -08:00
suite . Require ( ) . Empty ( app . BankKeeper . GetAllBalances ( ctx , addr2 ) )
2021-04-30 09:54:08 -07:00
sendAmt := sdk . NewCoins ( newFooCoin ( 50 ) , newBarCoin ( 50 ) )
2020-02-04 18:33:02 -08:00
suite . Require ( ) . NoError ( app . BankKeeper . SendCoins ( ctx , addr1 , addr2 , sendAmt ) )
acc2Balances := app . BankKeeper . GetAllBalances ( ctx , addr2 )
2021-04-30 09:54:08 -07:00
acc1Balances = app . BankKeeper . GetAllBalances ( ctx , addr1 )
2020-02-04 18:33:02 -08:00
suite . Require ( ) . Equal ( sendAmt , acc2Balances )
2021-04-30 09:54:08 -07:00
updatedAcc1Bal := balances . Sub ( sendAmt )
suite . Require ( ) . Len ( acc1Balances , len ( updatedAcc1Bal ) )
suite . Require ( ) . Equal ( acc1Balances , updatedAcc1Bal )
2020-02-04 18:33:02 -08:00
suite . Require ( ) . NotNil ( app . AccountKeeper . GetAccount ( ctx , addr2 ) )
}
2020-05-27 20:00:01 -07:00
func ( suite * IntegrationTestSuite ) TestInputOutputNewAccount ( ) {
app , ctx := suite . app , suite . ctx
balances := sdk . NewCoins ( newFooCoin ( 100 ) , newBarCoin ( 50 ) )
2020-08-17 07:07:27 -07:00
addr1 := sdk . AccAddress ( [ ] byte ( "addr1_______________" ) )
2020-05-27 20:00:01 -07:00
acc1 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr1 )
app . AccountKeeper . SetAccount ( ctx , acc1 )
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr1 , balances ) )
2020-05-27 20:00:01 -07:00
acc1Balances := app . BankKeeper . GetAllBalances ( ctx , addr1 )
suite . Require ( ) . Equal ( balances , acc1Balances )
2020-08-17 07:07:27 -07:00
addr2 := sdk . AccAddress ( [ ] byte ( "addr2_______________" ) )
2020-05-27 20:00:01 -07:00
suite . Require ( ) . Nil ( app . AccountKeeper . GetAccount ( ctx , addr2 ) )
suite . Require ( ) . Empty ( app . BankKeeper . GetAllBalances ( ctx , addr2 ) )
inputs := [ ] types . Input {
2020-09-25 03:25:37 -07:00
{ Address : addr1 . String ( ) , Coins : sdk . NewCoins ( newFooCoin ( 30 ) , newBarCoin ( 10 ) ) } ,
2020-05-27 20:00:01 -07:00
}
outputs := [ ] types . Output {
2020-09-25 03:25:37 -07:00
{ Address : addr2 . String ( ) , Coins : sdk . NewCoins ( newFooCoin ( 30 ) , newBarCoin ( 10 ) ) } ,
2020-05-27 20:00:01 -07:00
}
suite . Require ( ) . NoError ( app . BankKeeper . InputOutputCoins ( ctx , inputs , outputs ) )
expected := sdk . NewCoins ( newFooCoin ( 30 ) , newBarCoin ( 10 ) )
acc2Balances := app . BankKeeper . GetAllBalances ( ctx , addr2 )
suite . Require ( ) . Equal ( expected , acc2Balances )
suite . Require ( ) . NotNil ( app . AccountKeeper . GetAccount ( ctx , addr2 ) )
}
2020-01-30 13:31:16 -08:00
func ( suite * IntegrationTestSuite ) TestInputOutputCoins ( ) {
app , ctx := suite . app , suite . ctx
balances := sdk . NewCoins ( newFooCoin ( 90 ) , newBarCoin ( 30 ) )
2018-04-09 15:10:58 -07:00
2020-08-17 07:07:27 -07:00
addr1 := sdk . AccAddress ( [ ] byte ( "addr1_______________" ) )
2020-01-30 13:31:16 -08:00
acc1 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr1 )
app . AccountKeeper . SetAccount ( ctx , acc1 )
2018-04-09 15:10:58 -07:00
2020-08-17 07:07:27 -07:00
addr2 := sdk . AccAddress ( [ ] byte ( "addr2_______________" ) )
2020-01-30 13:31:16 -08:00
acc2 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr2 )
app . AccountKeeper . SetAccount ( ctx , acc2 )
2018-04-09 15:10:58 -07:00
2020-08-17 07:07:27 -07:00
addr3 := sdk . AccAddress ( [ ] byte ( "addr3_______________" ) )
2020-01-30 13:31:16 -08:00
acc3 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr3 )
app . AccountKeeper . SetAccount ( ctx , acc3 )
2018-04-09 15:10:58 -07:00
2019-06-05 16:26:17 -07:00
inputs := [ ] types . Input {
2020-09-25 03:25:37 -07:00
{ Address : addr1 . String ( ) , Coins : sdk . NewCoins ( newFooCoin ( 30 ) , newBarCoin ( 10 ) ) } ,
{ Address : addr1 . String ( ) , Coins : sdk . NewCoins ( newFooCoin ( 30 ) , newBarCoin ( 10 ) ) } ,
2018-04-09 15:10:58 -07:00
}
2019-06-05 16:26:17 -07:00
outputs := [ ] types . Output {
2020-09-25 03:25:37 -07:00
{ Address : addr2 . String ( ) , Coins : sdk . NewCoins ( newFooCoin ( 30 ) , newBarCoin ( 10 ) ) } ,
{ Address : addr3 . String ( ) , Coins : sdk . NewCoins ( newFooCoin ( 30 ) , newBarCoin ( 10 ) ) } ,
2018-04-09 15:10:58 -07:00
}
2020-01-30 13:31:16 -08:00
suite . Require ( ) . Error ( app . BankKeeper . InputOutputCoins ( ctx , inputs , [ ] types . Output { } ) )
suite . Require ( ) . Error ( app . BankKeeper . InputOutputCoins ( ctx , inputs , outputs ) )
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr1 , balances ) )
2020-01-30 13:31:16 -08:00
insufficientInputs := [ ] types . Input {
2020-09-25 03:25:37 -07:00
{ Address : addr1 . String ( ) , Coins : sdk . NewCoins ( newFooCoin ( 300 ) , newBarCoin ( 100 ) ) } ,
{ Address : addr1 . String ( ) , Coins : sdk . NewCoins ( newFooCoin ( 300 ) , newBarCoin ( 100 ) ) } ,
2019-08-19 06:29:17 -07:00
}
2020-01-30 13:31:16 -08:00
insufficientOutputs := [ ] types . Output {
2020-09-25 03:25:37 -07:00
{ Address : addr2 . String ( ) , Coins : sdk . NewCoins ( newFooCoin ( 300 ) , newBarCoin ( 100 ) ) } ,
{ Address : addr3 . String ( ) , Coins : sdk . NewCoins ( newFooCoin ( 300 ) , newBarCoin ( 100 ) ) } ,
2020-01-30 13:31:16 -08:00
}
suite . Require ( ) . Error ( app . BankKeeper . InputOutputCoins ( ctx , insufficientInputs , insufficientOutputs ) )
suite . Require ( ) . NoError ( app . BankKeeper . InputOutputCoins ( ctx , inputs , outputs ) )
acc1Balances := app . BankKeeper . GetAllBalances ( ctx , addr1 )
expected := sdk . NewCoins ( newFooCoin ( 30 ) , newBarCoin ( 10 ) )
suite . Require ( ) . Equal ( expected , acc1Balances )
acc2Balances := app . BankKeeper . GetAllBalances ( ctx , addr2 )
suite . Require ( ) . Equal ( expected , acc2Balances )
acc3Balances := app . BankKeeper . GetAllBalances ( ctx , addr3 )
suite . Require ( ) . Equal ( expected , acc3Balances )
2018-04-09 15:10:58 -07:00
}
2020-01-30 13:31:16 -08:00
func ( suite * IntegrationTestSuite ) TestSendCoins ( ) {
app , ctx := suite . app , suite . ctx
balances := sdk . NewCoins ( newFooCoin ( 100 ) , newBarCoin ( 50 ) )
2019-07-31 08:39:02 -07:00
2021-02-17 10:20:33 -08:00
addr1 := sdk . AccAddress ( "addr1_______________" )
2020-01-30 13:31:16 -08:00
acc1 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr1 )
app . AccountKeeper . SetAccount ( ctx , acc1 )
2019-07-31 08:39:02 -07:00
2021-02-17 10:20:33 -08:00
addr2 := sdk . AccAddress ( "addr2_______________" )
2020-01-30 13:31:16 -08:00
acc2 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr2 )
app . AccountKeeper . SetAccount ( ctx , acc2 )
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr2 , balances ) )
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
sendAmt := sdk . NewCoins ( newFooCoin ( 50 ) , newBarCoin ( 25 ) )
suite . Require ( ) . Error ( app . BankKeeper . SendCoins ( ctx , addr1 , addr2 , sendAmt ) )
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr1 , balances ) )
2020-01-30 13:31:16 -08:00
suite . Require ( ) . NoError ( app . BankKeeper . SendCoins ( ctx , addr1 , addr2 , sendAmt ) )
acc1Balances := app . BankKeeper . GetAllBalances ( ctx , addr1 )
expected := sdk . NewCoins ( newFooCoin ( 50 ) , newBarCoin ( 25 ) )
suite . Require ( ) . Equal ( expected , acc1Balances )
acc2Balances := app . BankKeeper . GetAllBalances ( ctx , addr2 )
expected = sdk . NewCoins ( newFooCoin ( 150 ) , newBarCoin ( 75 ) )
suite . Require ( ) . Equal ( expected , acc2Balances )
2021-06-22 05:41:56 -07:00
// we sent all foo coins to acc2, so foo balance should be deleted for acc1 and bar should be still there
var coins [ ] sdk . Coin
app . BankKeeper . IterateAccountBalances ( ctx , addr1 , func ( c sdk . Coin ) ( stop bool ) {
coins = append ( coins , c )
return true
} )
suite . Require ( ) . Len ( coins , 1 )
suite . Require ( ) . Equal ( newBarCoin ( 25 ) , coins [ 0 ] , "expected only bar coins in the account balance, got: %v" , coins )
2020-01-30 13:31:16 -08:00
}
func ( suite * IntegrationTestSuite ) TestValidateBalance ( ) {
app , ctx := suite . app , suite . ctx
now := tmtime . Now ( )
2020-08-14 10:58:53 -07:00
ctx = ctx . WithBlockHeader ( tmproto . Header { Time : now } )
2020-01-30 13:31:16 -08:00
endTime := now . Add ( 24 * time . Hour )
2020-09-25 03:25:37 -07:00
addr1 := sdk . AccAddress ( [ ] byte ( "addr1_______________" ) )
addr2 := sdk . AccAddress ( [ ] byte ( "addr2_______________" ) )
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
suite . Require ( ) . Error ( app . BankKeeper . ValidateBalance ( ctx , addr1 ) )
acc := app . AccountKeeper . NewAccountWithAddress ( ctx , addr1 )
2019-08-19 06:29:17 -07:00
app . AccountKeeper . SetAccount ( ctx , acc )
2020-01-30 13:31:16 -08:00
balances := sdk . NewCoins ( newFooCoin ( 100 ) )
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr1 , balances ) )
2020-01-30 13:31:16 -08:00
suite . Require ( ) . NoError ( app . BankKeeper . ValidateBalance ( ctx , addr1 ) )
2020-06-17 11:42:27 -07:00
bacc := authtypes . NewBaseAccountWithAddress ( addr2 )
2020-02-18 04:50:13 -08:00
vacc := vesting . NewContinuousVestingAccount ( bacc , balances . Add ( balances ... ) , now . Unix ( ) , endTime . Unix ( ) )
2020-01-30 13:31:16 -08:00
app . AccountKeeper . SetAccount ( ctx , vacc )
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr2 , balances ) )
2020-01-30 13:31:16 -08:00
suite . Require ( ) . Error ( app . BankKeeper . ValidateBalance ( ctx , addr2 ) )
2018-04-09 15:10:58 -07:00
}
2020-01-30 13:31:16 -08:00
func ( suite * IntegrationTestSuite ) TestSendEnabled ( ) {
app , ctx := suite . app , suite . ctx
2020-07-08 10:43:08 -07:00
enabled := true
params := types . DefaultParams ( )
suite . Require ( ) . Equal ( enabled , params . DefaultSendEnabled )
app . BankKeeper . SetParams ( ctx , params )
bondCoin := sdk . NewCoin ( sdk . DefaultBondDenom , sdk . OneInt ( ) )
fooCoin := sdk . NewCoin ( "foocoin" , sdk . OneInt ( ) )
barCoin := sdk . NewCoin ( "barcoin" , sdk . OneInt ( ) )
// assert with default (all denom) send enabled both Bar and Bond Denom are enabled
2021-05-10 09:17:55 -07:00
suite . Require ( ) . Equal ( enabled , app . BankKeeper . IsSendEnabledCoin ( ctx , barCoin ) )
suite . Require ( ) . Equal ( enabled , app . BankKeeper . IsSendEnabledCoin ( ctx , bondCoin ) )
2020-07-08 10:43:08 -07:00
// Both coins should be send enabled.
2021-05-10 09:17:55 -07:00
err := app . BankKeeper . IsSendEnabledCoins ( ctx , fooCoin , bondCoin )
2020-07-08 10:43:08 -07:00
suite . Require ( ) . NoError ( err )
// Set default send_enabled to !enabled, add a foodenom that overrides default as enabled
params . DefaultSendEnabled = ! enabled
params = params . SetSendEnabledParam ( fooCoin . Denom , enabled )
app . BankKeeper . SetParams ( ctx , params )
// Expect our specific override to be enabled, others to be !enabled.
2021-05-10 09:17:55 -07:00
suite . Require ( ) . Equal ( enabled , app . BankKeeper . IsSendEnabledCoin ( ctx , fooCoin ) )
suite . Require ( ) . Equal ( ! enabled , app . BankKeeper . IsSendEnabledCoin ( ctx , barCoin ) )
suite . Require ( ) . Equal ( ! enabled , app . BankKeeper . IsSendEnabledCoin ( ctx , bondCoin ) )
2020-07-08 10:43:08 -07:00
// Foo coin should be send enabled.
2021-05-10 09:17:55 -07:00
err = app . BankKeeper . IsSendEnabledCoins ( ctx , fooCoin )
2020-07-08 10:43:08 -07:00
suite . Require ( ) . NoError ( err )
// Expect an error when one coin is not send enabled.
2021-05-10 09:17:55 -07:00
err = app . BankKeeper . IsSendEnabledCoins ( ctx , fooCoin , bondCoin )
2020-07-08 10:43:08 -07:00
suite . Require ( ) . Error ( err )
// Expect an error when all coins are not send enabled.
2021-05-10 09:17:55 -07:00
err = app . BankKeeper . IsSendEnabledCoins ( ctx , bondCoin , barCoin )
2020-07-08 10:43:08 -07:00
suite . Require ( ) . Error ( err )
2020-01-30 13:31:16 -08:00
}
func ( suite * IntegrationTestSuite ) TestHasBalance ( ) {
app , ctx := suite . app , suite . ctx
2020-09-25 03:25:37 -07:00
addr := sdk . AccAddress ( [ ] byte ( "addr1_______________" ) )
2020-01-30 13:31:16 -08:00
acc := app . AccountKeeper . NewAccountWithAddress ( ctx , addr )
app . AccountKeeper . SetAccount ( ctx , acc )
balances := sdk . NewCoins ( newFooCoin ( 100 ) )
suite . Require ( ) . False ( app . BankKeeper . HasBalance ( ctx , addr , newFooCoin ( 99 ) ) )
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr , balances ) )
2020-01-30 13:31:16 -08:00
suite . Require ( ) . False ( app . BankKeeper . HasBalance ( ctx , addr , newFooCoin ( 101 ) ) )
suite . Require ( ) . True ( app . BankKeeper . HasBalance ( ctx , addr , newFooCoin ( 100 ) ) )
suite . Require ( ) . True ( app . BankKeeper . HasBalance ( ctx , addr , newFooCoin ( 1 ) ) )
}
func ( suite * IntegrationTestSuite ) TestMsgSendEvents ( ) {
app , ctx := suite . app , suite . ctx
2020-09-25 03:25:37 -07:00
addr := sdk . AccAddress ( [ ] byte ( "addr1_______________" ) )
addr2 := sdk . AccAddress ( [ ] byte ( "addr2_______________" ) )
2019-08-27 06:17:31 -07:00
acc := app . AccountKeeper . NewAccountWithAddress ( ctx , addr )
app . AccountKeeper . SetAccount ( ctx , acc )
2020-01-30 13:31:16 -08:00
newCoins := sdk . NewCoins ( sdk . NewInt64Coin ( fooDenom , 50 ) )
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr , newCoins ) )
2020-01-30 13:31:16 -08:00
2021-05-10 09:17:55 -07:00
suite . Require ( ) . NoError ( app . BankKeeper . SendCoins ( ctx , addr , addr2 , newCoins ) )
2019-08-27 06:17:31 -07:00
event1 := sdk . Event {
Type : types . EventTypeTransfer ,
2020-08-14 10:58:53 -07:00
Attributes : [ ] abci . EventAttribute { } ,
2019-08-27 06:17:31 -07:00
}
event1 . Attributes = append (
event1 . Attributes ,
2020-08-14 10:58:53 -07:00
abci . EventAttribute { Key : [ ] byte ( types . AttributeKeyRecipient ) , Value : [ ] byte ( addr2 . String ( ) ) } ,
2020-01-30 13:31:16 -08:00
)
2020-07-01 00:25:35 -07:00
event1 . Attributes = append (
event1 . Attributes ,
2020-08-14 10:58:53 -07:00
abci . EventAttribute { Key : [ ] byte ( types . AttributeKeySender ) , Value : [ ] byte ( addr . String ( ) ) } ,
2020-07-01 00:25:35 -07:00
)
2019-08-27 06:17:31 -07:00
event1 . Attributes = append (
event1 . Attributes ,
2020-08-14 10:58:53 -07:00
abci . EventAttribute { Key : [ ] byte ( sdk . AttributeKeyAmount ) , Value : [ ] byte ( newCoins . String ( ) ) } ,
2020-01-30 13:31:16 -08:00
)
2021-02-25 10:15:02 -08:00
2019-08-27 06:17:31 -07:00
event2 := sdk . Event {
Type : sdk . EventTypeMessage ,
2020-08-14 10:58:53 -07:00
Attributes : [ ] abci . EventAttribute { } ,
2019-08-27 06:17:31 -07:00
}
event2 . Attributes = append (
event2 . Attributes ,
2020-08-14 10:58:53 -07:00
abci . EventAttribute { Key : [ ] byte ( types . AttributeKeySender ) , Value : [ ] byte ( addr . String ( ) ) } ,
2020-01-30 13:31:16 -08:00
)
2021-02-17 10:20:33 -08:00
// events are shifted due to the funding account events
2021-05-10 09:17:55 -07:00
events := ctx . EventManager ( ) . ABCIEvents ( )
suite . Require ( ) . Equal ( 10 , len ( events ) )
2021-02-25 10:15:02 -08:00
suite . Require ( ) . Equal ( abci . Event ( event1 ) , events [ 8 ] )
suite . Require ( ) . Equal ( abci . Event ( event2 ) , events [ 9 ] )
2019-08-27 06:17:31 -07:00
}
2020-01-30 13:31:16 -08:00
func ( suite * IntegrationTestSuite ) TestMsgMultiSendEvents ( ) {
app , ctx := suite . app , suite . ctx
2020-01-27 06:46:55 -08:00
2020-07-08 10:43:08 -07:00
app . BankKeeper . SetParams ( ctx , types . DefaultParams ( ) )
2020-01-27 06:46:55 -08:00
2020-08-17 07:07:27 -07:00
addr := sdk . AccAddress ( [ ] byte ( "addr1_______________" ) )
addr2 := sdk . AccAddress ( [ ] byte ( "addr2_______________" ) )
addr3 := sdk . AccAddress ( [ ] byte ( "addr3_______________" ) )
addr4 := sdk . AccAddress ( [ ] byte ( "addr4_______________" ) )
2020-01-27 06:46:55 -08:00
acc := app . AccountKeeper . NewAccountWithAddress ( ctx , addr )
acc2 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr2 )
app . AccountKeeper . SetAccount ( ctx , acc )
app . AccountKeeper . SetAccount ( ctx , acc2 )
2020-01-30 13:31:16 -08:00
newCoins := sdk . NewCoins ( sdk . NewInt64Coin ( fooDenom , 50 ) )
newCoins2 := sdk . NewCoins ( sdk . NewInt64Coin ( barDenom , 100 ) )
2020-01-27 06:46:55 -08:00
inputs := [ ] types . Input {
2020-09-25 03:25:37 -07:00
{ Address : addr . String ( ) , Coins : newCoins } ,
{ Address : addr2 . String ( ) , Coins : newCoins2 } ,
2020-01-27 06:46:55 -08:00
}
outputs := [ ] types . Output {
2020-09-25 03:25:37 -07:00
{ Address : addr3 . String ( ) , Coins : newCoins } ,
{ Address : addr4 . String ( ) , Coins : newCoins2 } ,
2020-01-27 06:46:55 -08:00
}
2020-01-30 13:31:16 -08:00
suite . Require ( ) . Error ( app . BankKeeper . InputOutputCoins ( ctx , inputs , outputs ) )
2020-03-26 09:46:10 -07:00
events := ctx . EventManager ( ) . ABCIEvents ( )
2020-01-30 13:31:16 -08:00
suite . Require ( ) . Equal ( 0 , len ( events ) )
2020-01-27 06:46:55 -08:00
// Set addr's coins but not addr2's coins
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr , sdk . NewCoins ( sdk . NewInt64Coin ( fooDenom , 50 ) ) ) )
2020-01-30 13:31:16 -08:00
suite . Require ( ) . Error ( app . BankKeeper . InputOutputCoins ( ctx , inputs , outputs ) )
2020-01-27 06:46:55 -08:00
2020-03-26 09:46:10 -07:00
events = ctx . EventManager ( ) . ABCIEvents ( )
2021-02-25 10:15:02 -08:00
suite . Require ( ) . Equal ( 8 , len ( events ) ) // 7 events because account funding causes extra minting + coin_spent + coin_recv events
2020-01-30 13:31:16 -08:00
2020-01-27 06:46:55 -08:00
event1 := sdk . Event {
Type : sdk . EventTypeMessage ,
2020-08-14 10:58:53 -07:00
Attributes : [ ] abci . EventAttribute { } ,
2020-01-27 06:46:55 -08:00
}
event1 . Attributes = append (
event1 . Attributes ,
2020-08-14 10:58:53 -07:00
abci . EventAttribute { Key : [ ] byte ( types . AttributeKeySender ) , Value : [ ] byte ( addr . String ( ) ) } ,
2020-01-30 13:31:16 -08:00
)
2021-02-25 10:15:02 -08:00
suite . Require ( ) . Equal ( abci . Event ( event1 ) , events [ 7 ] )
2020-01-27 06:46:55 -08:00
// Set addr's coins and addr2's coins
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr , sdk . NewCoins ( sdk . NewInt64Coin ( fooDenom , 50 ) ) ) )
2020-01-30 13:31:16 -08:00
newCoins = sdk . NewCoins ( sdk . NewInt64Coin ( fooDenom , 50 ) )
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr2 , sdk . NewCoins ( sdk . NewInt64Coin ( barDenom , 100 ) ) ) )
2020-01-30 13:31:16 -08:00
newCoins2 = sdk . NewCoins ( sdk . NewInt64Coin ( barDenom , 100 ) )
suite . Require ( ) . NoError ( app . BankKeeper . InputOutputCoins ( ctx , inputs , outputs ) )
2020-01-27 06:46:55 -08:00
2020-03-26 09:46:10 -07:00
events = ctx . EventManager ( ) . ABCIEvents ( )
2021-02-25 10:15:02 -08:00
suite . Require ( ) . Equal ( 28 , len ( events ) ) // 25 due to account funding + coin_spent + coin_recv events
2020-01-30 13:31:16 -08:00
2020-01-27 06:46:55 -08:00
event2 := sdk . Event {
Type : sdk . EventTypeMessage ,
2020-08-14 10:58:53 -07:00
Attributes : [ ] abci . EventAttribute { } ,
2020-01-27 06:46:55 -08:00
}
event2 . Attributes = append (
event2 . Attributes ,
2020-08-14 10:58:53 -07:00
abci . EventAttribute { Key : [ ] byte ( types . AttributeKeySender ) , Value : [ ] byte ( addr2 . String ( ) ) } ,
2020-01-30 13:31:16 -08:00
)
2020-01-27 06:46:55 -08:00
event3 := sdk . Event {
Type : types . EventTypeTransfer ,
2020-08-14 10:58:53 -07:00
Attributes : [ ] abci . EventAttribute { } ,
2020-01-27 06:46:55 -08:00
}
event3 . Attributes = append (
event3 . Attributes ,
2020-08-14 10:58:53 -07:00
abci . EventAttribute { Key : [ ] byte ( types . AttributeKeyRecipient ) , Value : [ ] byte ( addr3 . String ( ) ) } ,
2020-01-30 13:31:16 -08:00
)
2020-01-27 06:46:55 -08:00
event3 . Attributes = append (
event3 . Attributes ,
2020-08-14 10:58:53 -07:00
abci . EventAttribute { Key : [ ] byte ( sdk . AttributeKeyAmount ) , Value : [ ] byte ( newCoins . String ( ) ) } )
2020-01-27 06:46:55 -08:00
event4 := sdk . Event {
Type : types . EventTypeTransfer ,
2020-08-14 10:58:53 -07:00
Attributes : [ ] abci . EventAttribute { } ,
2020-01-27 06:46:55 -08:00
}
event4 . Attributes = append (
event4 . Attributes ,
2020-08-14 10:58:53 -07:00
abci . EventAttribute { Key : [ ] byte ( types . AttributeKeyRecipient ) , Value : [ ] byte ( addr4 . String ( ) ) } ,
2020-01-30 13:31:16 -08:00
)
2020-01-27 06:46:55 -08:00
event4 . Attributes = append (
event4 . Attributes ,
2020-08-14 10:58:53 -07:00
abci . EventAttribute { Key : [ ] byte ( sdk . AttributeKeyAmount ) , Value : [ ] byte ( newCoins2 . String ( ) ) } ,
2020-01-30 13:31:16 -08:00
)
2021-02-17 10:20:33 -08:00
// events are shifted due to the funding account events
2021-02-25 10:15:02 -08:00
suite . Require ( ) . Equal ( abci . Event ( event1 ) , events [ 21 ] )
suite . Require ( ) . Equal ( abci . Event ( event2 ) , events [ 23 ] )
suite . Require ( ) . Equal ( abci . Event ( event3 ) , events [ 25 ] )
suite . Require ( ) . Equal ( abci . Event ( event4 ) , events [ 27 ] )
2020-01-27 06:46:55 -08:00
}
2020-01-30 13:31:16 -08:00
func ( suite * IntegrationTestSuite ) TestSpendableCoins ( ) {
app , ctx := suite . app , suite . ctx
now := tmtime . Now ( )
2020-08-14 10:58:53 -07:00
ctx = ctx . WithBlockHeader ( tmproto . Header { Time : now } )
2020-01-30 13:31:16 -08:00
endTime := now . Add ( 24 * time . Hour )
2019-08-19 06:29:17 -07:00
2020-01-30 13:31:16 -08:00
origCoins := sdk . NewCoins ( sdk . NewInt64Coin ( "stake" , 100 ) )
delCoins := sdk . NewCoins ( sdk . NewInt64Coin ( "stake" , 50 ) )
2018-04-09 15:10:58 -07:00
2020-09-25 03:25:37 -07:00
addr1 := sdk . AccAddress ( [ ] byte ( "addr1_______________" ) )
addr2 := sdk . AccAddress ( [ ] byte ( "addr2_______________" ) )
addrModule := sdk . AccAddress ( [ ] byte ( "moduleAcc___________" ) )
2020-01-30 13:31:16 -08:00
macc := app . AccountKeeper . NewAccountWithAddress ( ctx , addrModule )
2020-06-17 11:42:27 -07:00
bacc := authtypes . NewBaseAccountWithAddress ( addr1 )
2020-02-18 04:50:13 -08:00
vacc := vesting . NewContinuousVestingAccount ( bacc , origCoins , ctx . BlockHeader ( ) . Time . Unix ( ) , endTime . Unix ( ) )
2020-01-30 13:31:16 -08:00
acc := app . AccountKeeper . NewAccountWithAddress ( ctx , addr2 )
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
app . AccountKeeper . SetAccount ( ctx , macc )
app . AccountKeeper . SetAccount ( ctx , vacc )
2019-08-19 06:29:17 -07:00
app . AccountKeeper . SetAccount ( ctx , acc )
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr1 , origCoins ) )
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr2 , origCoins ) )
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
suite . Require ( ) . Equal ( origCoins , app . BankKeeper . SpendableCoins ( ctx , addr2 ) )
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
ctx = ctx . WithBlockTime ( now . Add ( 12 * time . Hour ) )
suite . Require ( ) . NoError ( app . BankKeeper . DelegateCoins ( ctx , addr2 , addrModule , delCoins ) )
suite . Require ( ) . Equal ( origCoins . Sub ( delCoins ) , app . BankKeeper . SpendableCoins ( ctx , addr1 ) )
2018-04-09 15:10:58 -07:00
}
2019-01-14 08:11:24 -08:00
2020-01-30 13:31:16 -08:00
func ( suite * IntegrationTestSuite ) TestVestingAccountSend ( ) {
app , ctx := suite . app , suite . ctx
2019-01-14 08:11:24 -08:00
now := tmtime . Now ( )
2020-08-14 10:58:53 -07:00
ctx = ctx . WithBlockHeader ( tmproto . Header { Time : now } )
2019-01-14 08:11:24 -08:00
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 ) )
2019-01-14 08:11:24 -08:00
2020-09-25 03:25:37 -07:00
addr1 := sdk . AccAddress ( [ ] byte ( "addr1_______________" ) )
addr2 := sdk . AccAddress ( [ ] byte ( "addr2_______________" ) )
2020-01-30 13:31:16 -08:00
2020-06-17 11:42:27 -07:00
bacc := authtypes . NewBaseAccountWithAddress ( addr1 )
2020-02-18 04:50:13 -08:00
vacc := vesting . NewContinuousVestingAccount ( bacc , origCoins , now . Unix ( ) , endTime . Unix ( ) )
2020-01-30 13:31:16 -08:00
2019-08-19 06:29:17 -07:00
app . AccountKeeper . SetAccount ( ctx , vacc )
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr1 , origCoins ) )
2019-01-14 08:11:24 -08:00
// require that no coins be sendable at the beginning of the vesting schedule
2020-01-30 13:31:16 -08:00
suite . Require ( ) . Error ( app . BankKeeper . SendCoins ( ctx , addr1 , addr2 , sendCoins ) )
2019-01-14 08:11:24 -08:00
// receive some coins
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr1 , sendCoins ) )
2019-01-14 08:11:24 -08:00
// require that all vested coins are spendable plus any received
ctx = ctx . WithBlockTime ( now . Add ( 12 * time . Hour ) )
2020-01-30 13:31:16 -08:00
suite . Require ( ) . NoError ( app . BankKeeper . SendCoins ( ctx , addr1 , addr2 , sendCoins ) )
suite . Require ( ) . Equal ( origCoins , app . BankKeeper . GetAllBalances ( ctx , addr1 ) )
2019-10-10 08:53:30 -07:00
}
2020-01-30 13:31:16 -08:00
func ( suite * IntegrationTestSuite ) TestPeriodicVestingAccountSend ( ) {
app , ctx := suite . app , suite . ctx
2019-10-10 08:53:30 -07:00
now := tmtime . Now ( )
2020-08-14 10:58:53 -07:00
ctx = ctx . WithBlockHeader ( tmproto . Header { Time : now } )
2019-10-10 08:53:30 -07:00
origCoins := sdk . NewCoins ( sdk . NewInt64Coin ( "stake" , 100 ) )
sendCoins := sdk . NewCoins ( sdk . NewInt64Coin ( "stake" , 50 ) )
2020-09-25 03:25:37 -07:00
addr1 := sdk . AccAddress ( [ ] byte ( "addr1_______________" ) )
addr2 := sdk . AccAddress ( [ ] byte ( "addr2_______________" ) )
2019-10-10 08:53:30 -07:00
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 ) } } ,
}
2020-01-30 13:31:16 -08:00
2020-06-17 11:42:27 -07:00
bacc := authtypes . NewBaseAccountWithAddress ( addr1 )
2020-02-18 04:50:13 -08:00
vacc := vesting . NewPeriodicVestingAccount ( bacc , origCoins , ctx . BlockHeader ( ) . Time . Unix ( ) , periods )
2020-01-30 13:31:16 -08:00
2019-10-10 08:53:30 -07:00
app . AccountKeeper . SetAccount ( ctx , vacc )
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr1 , origCoins ) )
2019-10-10 08:53:30 -07:00
// require that no coins be sendable at the beginning of the vesting schedule
2020-01-30 13:31:16 -08:00
suite . Require ( ) . Error ( app . BankKeeper . SendCoins ( ctx , addr1 , addr2 , sendCoins ) )
2019-10-10 08:53:30 -07:00
// receive some coins
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr1 , sendCoins ) )
2019-10-10 08:53:30 -07:00
// require that all vested coins are spendable plus any received
ctx = ctx . WithBlockTime ( now . Add ( 12 * time . Hour ) )
2020-01-30 13:31:16 -08:00
suite . Require ( ) . NoError ( app . BankKeeper . SendCoins ( ctx , addr1 , addr2 , sendCoins ) )
suite . Require ( ) . Equal ( origCoins , app . BankKeeper . GetAllBalances ( ctx , addr1 ) )
2019-01-14 08:11:24 -08:00
}
2020-01-30 13:31:16 -08:00
func ( suite * IntegrationTestSuite ) TestVestingAccountReceive ( ) {
app , ctx := suite . app , suite . ctx
2019-01-14 08:11:24 -08:00
now := tmtime . Now ( )
2020-08-14 10:58:53 -07:00
ctx = ctx . WithBlockHeader ( tmproto . Header { Time : now } )
2019-01-14 08:11:24 -08:00
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 ) )
2019-01-14 08:11:24 -08:00
2020-09-25 03:25:37 -07:00
addr1 := sdk . AccAddress ( [ ] byte ( "addr1_______________" ) )
addr2 := sdk . AccAddress ( [ ] byte ( "addr2_______________" ) )
2019-01-14 08:11:24 -08:00
2020-06-17 11:42:27 -07:00
bacc := authtypes . NewBaseAccountWithAddress ( addr1 )
2020-02-18 04:50:13 -08:00
vacc := vesting . NewContinuousVestingAccount ( bacc , origCoins , ctx . BlockHeader ( ) . Time . Unix ( ) , endTime . Unix ( ) )
2019-10-10 08:53:30 -07:00
acc := app . AccountKeeper . NewAccountWithAddress ( ctx , addr2 )
2020-01-30 13:31:16 -08:00
2019-10-10 08:53:30 -07:00
app . AccountKeeper . SetAccount ( ctx , vacc )
app . AccountKeeper . SetAccount ( ctx , acc )
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr1 , origCoins ) )
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr2 , origCoins ) )
2019-10-10 08:53:30 -07:00
// send some coins to the vesting account
2020-01-30 13:31:16 -08:00
suite . Require ( ) . NoError ( app . BankKeeper . SendCoins ( ctx , addr2 , addr1 , sendCoins ) )
2019-10-10 08:53:30 -07:00
// require the coins are spendable
vacc = app . AccountKeeper . GetAccount ( ctx , addr1 ) . ( * vesting . ContinuousVestingAccount )
2020-01-30 13:31:16 -08:00
balances := app . BankKeeper . GetAllBalances ( ctx , addr1 )
suite . Require ( ) . Equal ( origCoins . Add ( sendCoins ... ) , balances )
suite . Require ( ) . Equal ( balances . Sub ( vacc . LockedCoins ( now ) ) , sendCoins )
2019-10-10 08:53:30 -07:00
// require coins are spendable plus any that have vested
2020-01-30 13:31:16 -08:00
suite . Require ( ) . Equal ( balances . Sub ( vacc . LockedCoins ( now . Add ( 12 * time . Hour ) ) ) , origCoins )
2019-10-10 08:53:30 -07:00
}
2020-01-30 13:31:16 -08:00
func ( suite * IntegrationTestSuite ) TestPeriodicVestingAccountReceive ( ) {
app , ctx := suite . app , suite . ctx
2019-10-10 08:53:30 -07:00
now := tmtime . Now ( )
2020-08-14 10:58:53 -07:00
ctx = ctx . WithBlockHeader ( tmproto . Header { Time : now } )
2019-10-10 08:53:30 -07:00
origCoins := sdk . NewCoins ( sdk . NewInt64Coin ( "stake" , 100 ) )
sendCoins := sdk . NewCoins ( sdk . NewInt64Coin ( "stake" , 50 ) )
2020-09-25 03:25:37 -07:00
addr1 := sdk . AccAddress ( [ ] byte ( "addr1_______________" ) )
addr2 := sdk . AccAddress ( [ ] byte ( "addr2_______________" ) )
2019-10-10 08:53:30 -07:00
2020-06-17 11:42:27 -07:00
bacc := authtypes . NewBaseAccountWithAddress ( addr1 )
2019-10-10 08:53:30 -07:00
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 ) } } ,
}
2020-01-30 13:31:16 -08:00
2020-02-18 04:50:13 -08:00
vacc := vesting . NewPeriodicVestingAccount ( bacc , origCoins , ctx . BlockHeader ( ) . Time . Unix ( ) , periods )
2019-08-19 06:29:17 -07:00
acc := app . AccountKeeper . NewAccountWithAddress ( ctx , addr2 )
2020-01-30 13:31:16 -08:00
2019-08-19 06:29:17 -07:00
app . AccountKeeper . SetAccount ( ctx , vacc )
app . AccountKeeper . SetAccount ( ctx , acc )
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr1 , origCoins ) )
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr2 , origCoins ) )
2019-01-14 08:11:24 -08:00
// send some coins to the vesting account
2020-01-30 13:31:16 -08:00
suite . Require ( ) . NoError ( app . BankKeeper . SendCoins ( ctx , addr2 , addr1 , sendCoins ) )
2019-01-14 08:11:24 -08:00
// require the coins are spendable
2019-10-10 08:53:30 -07:00
vacc = app . AccountKeeper . GetAccount ( ctx , addr1 ) . ( * vesting . PeriodicVestingAccount )
2020-01-30 13:31:16 -08:00
balances := app . BankKeeper . GetAllBalances ( ctx , addr1 )
suite . Require ( ) . Equal ( origCoins . Add ( sendCoins ... ) , balances )
suite . Require ( ) . Equal ( balances . Sub ( vacc . LockedCoins ( now ) ) , sendCoins )
2019-01-14 08:11:24 -08:00
// require coins are spendable plus any that have vested
2020-01-30 13:31:16 -08:00
suite . Require ( ) . Equal ( balances . Sub ( vacc . LockedCoins ( now . Add ( 12 * time . Hour ) ) ) , origCoins )
2019-01-14 08:11:24 -08:00
}
2020-01-30 13:31:16 -08:00
func ( suite * IntegrationTestSuite ) TestDelegateCoins ( ) {
app , ctx := suite . app , suite . ctx
2019-01-14 08:11:24 -08:00
now := tmtime . Now ( )
2020-08-14 10:58:53 -07:00
ctx = ctx . WithBlockHeader ( tmproto . Header { Time : now } )
2019-01-14 08:11:24 -08:00
endTime := now . Add ( 24 * time . Hour )
2019-03-14 07:32:46 -07:00
origCoins := sdk . NewCoins ( sdk . NewInt64Coin ( "stake" , 100 ) )
delCoins := sdk . NewCoins ( sdk . NewInt64Coin ( "stake" , 50 ) )
2019-01-14 08:11:24 -08:00
2020-09-25 03:25:37 -07:00
addr1 := sdk . AccAddress ( [ ] byte ( "addr1_______________" ) )
addr2 := sdk . AccAddress ( [ ] byte ( "addr2_______________" ) )
addrModule := sdk . AccAddress ( [ ] byte ( "moduleAcc___________" ) )
2019-01-14 08:11:24 -08:00
2020-01-30 13:31:16 -08:00
macc := app . AccountKeeper . NewAccountWithAddress ( ctx , addrModule ) // we don't need to define an actual module account bc we just need the address for testing
acc := app . AccountKeeper . NewAccountWithAddress ( ctx , addr2 )
2020-06-17 11:42:27 -07:00
bacc := authtypes . NewBaseAccountWithAddress ( addr1 )
2020-02-18 04:50:13 -08:00
vacc := vesting . NewContinuousVestingAccount ( bacc , origCoins , ctx . BlockHeader ( ) . Time . Unix ( ) , endTime . Unix ( ) )
2020-01-30 13:31:16 -08:00
app . AccountKeeper . SetAccount ( ctx , vacc )
app . AccountKeeper . SetAccount ( ctx , acc )
app . AccountKeeper . SetAccount ( ctx , macc )
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr1 , origCoins ) )
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr2 , origCoins ) )
2019-01-14 08:11:24 -08:00
ctx = ctx . WithBlockTime ( now . Add ( 12 * time . Hour ) )
// require the ability for a non-vesting account to delegate
2020-01-30 13:31:16 -08:00
suite . Require ( ) . NoError ( app . BankKeeper . DelegateCoins ( ctx , addr2 , addrModule , delCoins ) )
suite . Require ( ) . Equal ( origCoins . Sub ( delCoins ) , app . BankKeeper . GetAllBalances ( ctx , addr2 ) )
suite . Require ( ) . Equal ( delCoins , app . BankKeeper . GetAllBalances ( ctx , addrModule ) )
2019-01-14 08:11:24 -08:00
// require the ability for a vesting account to delegate
2020-01-30 13:31:16 -08:00
suite . Require ( ) . NoError ( app . BankKeeper . DelegateCoins ( ctx , addr1 , addrModule , delCoins ) )
suite . Require ( ) . Equal ( delCoins , app . BankKeeper . GetAllBalances ( ctx , addr1 ) )
2021-04-08 08:20:29 -07:00
// require that delegated vesting amount is equal to what was delegated with DelegateCoins
acc = app . AccountKeeper . GetAccount ( ctx , addr1 )
vestingAcc , ok := acc . ( exported . VestingAccount )
suite . Require ( ) . True ( ok )
suite . Require ( ) . Equal ( delCoins , vestingAcc . GetDelegatedVesting ( ) )
2020-01-30 13:31:16 -08:00
}
func ( suite * IntegrationTestSuite ) TestDelegateCoins_Invalid ( ) {
app , ctx := suite . app , suite . ctx
origCoins := sdk . NewCoins ( newFooCoin ( 100 ) )
delCoins := sdk . NewCoins ( newFooCoin ( 50 ) )
2020-09-25 03:25:37 -07:00
addr1 := sdk . AccAddress ( [ ] byte ( "addr1_______________" ) )
addrModule := sdk . AccAddress ( [ ] byte ( "moduleAcc___________" ) )
2020-01-30 13:31:16 -08:00
macc := app . AccountKeeper . NewAccountWithAddress ( ctx , addrModule ) // we don't need to define an actual module account bc we just need the address for testing
acc := app . AccountKeeper . NewAccountWithAddress ( ctx , addr1 )
suite . Require ( ) . Error ( app . BankKeeper . DelegateCoins ( ctx , addr1 , addrModule , delCoins ) )
invalidCoins := sdk . Coins { sdk . Coin { Denom : "fooDenom" , Amount : sdk . NewInt ( - 50 ) } }
suite . Require ( ) . Error ( app . BankKeeper . DelegateCoins ( ctx , addr1 , addrModule , invalidCoins ) )
app . AccountKeeper . SetAccount ( ctx , macc )
suite . Require ( ) . Error ( app . BankKeeper . DelegateCoins ( ctx , addr1 , addrModule , delCoins ) )
app . AccountKeeper . SetAccount ( ctx , acc )
suite . Require ( ) . Error ( app . BankKeeper . DelegateCoins ( ctx , addr1 , addrModule , origCoins . Add ( origCoins ... ) ) )
2019-01-14 08:11:24 -08:00
}
2020-01-30 13:31:16 -08:00
func ( suite * IntegrationTestSuite ) TestUndelegateCoins ( ) {
app , ctx := suite . app , suite . ctx
2019-01-14 08:11:24 -08:00
now := tmtime . Now ( )
2020-08-14 10:58:53 -07:00
ctx = ctx . WithBlockHeader ( tmproto . Header { Time : now } )
2019-01-14 08:11:24 -08:00
endTime := now . Add ( 24 * time . Hour )
2019-03-14 07:32:46 -07:00
origCoins := sdk . NewCoins ( sdk . NewInt64Coin ( "stake" , 100 ) )
delCoins := sdk . NewCoins ( sdk . NewInt64Coin ( "stake" , 50 ) )
2019-01-14 08:11:24 -08:00
2020-09-25 03:25:37 -07:00
addr1 := sdk . AccAddress ( [ ] byte ( "addr1_______________" ) )
addr2 := sdk . AccAddress ( [ ] byte ( "addr2_______________" ) )
addrModule := sdk . AccAddress ( [ ] byte ( "moduleAcc___________" ) )
2019-01-14 08:11:24 -08:00
2020-06-17 11:42:27 -07:00
bacc := authtypes . NewBaseAccountWithAddress ( addr1 )
2020-01-30 13:31:16 -08:00
macc := app . AccountKeeper . NewAccountWithAddress ( ctx , addrModule ) // we don't need to define an actual module account bc we just need the address for testing
2020-02-18 04:50:13 -08:00
vacc := vesting . NewContinuousVestingAccount ( bacc , origCoins , ctx . BlockHeader ( ) . Time . Unix ( ) , endTime . Unix ( ) )
2020-01-30 13:31:16 -08:00
acc := app . AccountKeeper . NewAccountWithAddress ( ctx , addr2 )
app . AccountKeeper . SetAccount ( ctx , vacc )
app . AccountKeeper . SetAccount ( ctx , acc )
app . AccountKeeper . SetAccount ( ctx , macc )
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr1 , origCoins ) )
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr2 , origCoins ) )
2019-01-14 08:11:24 -08:00
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 )
2020-01-30 13:31:16 -08:00
suite . Require ( ) . NoError ( err )
2019-01-14 08:11:24 -08:00
2020-01-30 13:31:16 -08:00
suite . Require ( ) . Equal ( origCoins . Sub ( delCoins ) , app . BankKeeper . GetAllBalances ( ctx , addr2 ) )
suite . Require ( ) . Equal ( delCoins , app . BankKeeper . GetAllBalances ( ctx , addrModule ) )
2019-06-28 13:11:27 -07:00
2019-01-14 08:11:24 -08:00
// require the ability for a non-vesting account to undelegate
2020-01-30 13:31:16 -08:00
suite . Require ( ) . NoError ( app . BankKeeper . UndelegateCoins ( ctx , addrModule , addr2 , delCoins ) )
2019-01-14 08:11:24 -08:00
2020-01-30 13:31:16 -08:00
suite . Require ( ) . Equal ( origCoins , app . BankKeeper . GetAllBalances ( ctx , addr2 ) )
suite . Require ( ) . True ( app . BankKeeper . GetAllBalances ( ctx , addrModule ) . Empty ( ) )
2019-01-14 08:11:24 -08:00
// require the ability for a vesting account to delegate
2020-01-30 13:31:16 -08:00
suite . Require ( ) . NoError ( app . BankKeeper . DelegateCoins ( ctx , addr1 , addrModule , delCoins ) )
2019-01-14 08:11:24 -08:00
2020-01-30 13:31:16 -08:00
suite . Require ( ) . Equal ( origCoins . Sub ( delCoins ) , app . BankKeeper . GetAllBalances ( ctx , addr1 ) )
suite . Require ( ) . Equal ( delCoins , app . BankKeeper . GetAllBalances ( ctx , addrModule ) )
2019-06-28 13:11:27 -07:00
2019-01-14 08:11:24 -08:00
// require the ability for a vesting account to undelegate
2020-01-30 13:31:16 -08:00
suite . Require ( ) . NoError ( app . BankKeeper . UndelegateCoins ( ctx , addrModule , addr1 , delCoins ) )
suite . Require ( ) . Equal ( origCoins , app . BankKeeper . GetAllBalances ( ctx , addr1 ) )
suite . Require ( ) . True ( app . BankKeeper . GetAllBalances ( ctx , addrModule ) . Empty ( ) )
2021-04-08 08:20:29 -07:00
// require that delegated vesting amount is completely empty, since they were completely undelegated
acc = app . AccountKeeper . GetAccount ( ctx , addr1 )
vestingAcc , ok := acc . ( exported . VestingAccount )
suite . Require ( ) . True ( ok )
suite . Require ( ) . Empty ( vestingAcc . GetDelegatedVesting ( ) )
2020-01-30 13:31:16 -08:00
}
func ( suite * IntegrationTestSuite ) TestUndelegateCoins_Invalid ( ) {
app , ctx := suite . app , suite . ctx
origCoins := sdk . NewCoins ( newFooCoin ( 100 ) )
delCoins := sdk . NewCoins ( newFooCoin ( 50 ) )
2020-09-25 03:25:37 -07:00
addr1 := sdk . AccAddress ( [ ] byte ( "addr1_______________" ) )
addrModule := sdk . AccAddress ( [ ] byte ( "moduleAcc___________" ) )
2020-01-30 13:31:16 -08:00
macc := app . AccountKeeper . NewAccountWithAddress ( ctx , addrModule ) // we don't need to define an actual module account bc we just need the address for testing
acc := app . AccountKeeper . NewAccountWithAddress ( ctx , addr1 )
suite . Require ( ) . Error ( app . BankKeeper . UndelegateCoins ( ctx , addrModule , addr1 , delCoins ) )
app . AccountKeeper . SetAccount ( ctx , macc )
2021-06-29 03:23:13 -07:00
suite . Require ( ) . NoError ( testutil . FundAccount ( app . BankKeeper , ctx , addr1 , origCoins ) )
2020-01-30 13:31:16 -08:00
suite . Require ( ) . Error ( app . BankKeeper . UndelegateCoins ( ctx , addrModule , addr1 , delCoins ) )
app . AccountKeeper . SetAccount ( ctx , acc )
suite . Require ( ) . Error ( app . BankKeeper . UndelegateCoins ( ctx , addrModule , addr1 , delCoins ) )
}
2019-01-14 08:11:24 -08:00
2020-07-28 00:49:56 -07:00
func ( suite * IntegrationTestSuite ) TestSetDenomMetaData ( ) {
app , ctx := suite . app , suite . ctx
metadata := suite . getTestMetadata ( )
for i := range [ ] int { 1 , 2 } {
app . BankKeeper . SetDenomMetaData ( ctx , metadata [ i ] )
}
2021-01-18 10:15:16 -08:00
actualMetadata , found := app . BankKeeper . GetDenomMetaData ( ctx , metadata [ 1 ] . Base )
suite . Require ( ) . True ( found )
2020-07-28 00:49:56 -07:00
suite . Require ( ) . Equal ( metadata [ 1 ] . GetBase ( ) , actualMetadata . GetBase ( ) )
suite . Require ( ) . Equal ( metadata [ 1 ] . GetDisplay ( ) , actualMetadata . GetDisplay ( ) )
suite . Require ( ) . Equal ( metadata [ 1 ] . GetDescription ( ) , actualMetadata . GetDescription ( ) )
suite . Require ( ) . Equal ( metadata [ 1 ] . GetDenomUnits ( ) [ 1 ] . GetDenom ( ) , actualMetadata . GetDenomUnits ( ) [ 1 ] . GetDenom ( ) )
suite . Require ( ) . Equal ( metadata [ 1 ] . GetDenomUnits ( ) [ 1 ] . GetExponent ( ) , actualMetadata . GetDenomUnits ( ) [ 1 ] . GetExponent ( ) )
suite . Require ( ) . Equal ( metadata [ 1 ] . GetDenomUnits ( ) [ 1 ] . GetAliases ( ) , actualMetadata . GetDenomUnits ( ) [ 1 ] . GetAliases ( ) )
}
func ( suite * IntegrationTestSuite ) TestIterateAllDenomMetaData ( ) {
app , ctx := suite . app , suite . ctx
expectedMetadata := suite . getTestMetadata ( )
// set metadata
for i := range [ ] int { 1 , 2 } {
app . BankKeeper . SetDenomMetaData ( ctx , expectedMetadata [ i ] )
}
// retrieve metadata
actualMetadata := make ( [ ] types . Metadata , 0 )
app . BankKeeper . IterateAllDenomMetaData ( ctx , func ( metadata types . Metadata ) bool {
actualMetadata = append ( actualMetadata , metadata )
return false
} )
// execute checks
for i := range [ ] int { 1 , 2 } {
suite . Require ( ) . Equal ( expectedMetadata [ i ] . GetBase ( ) , actualMetadata [ i ] . GetBase ( ) )
suite . Require ( ) . Equal ( expectedMetadata [ i ] . GetDisplay ( ) , actualMetadata [ i ] . GetDisplay ( ) )
suite . Require ( ) . Equal ( expectedMetadata [ i ] . GetDescription ( ) , actualMetadata [ i ] . GetDescription ( ) )
suite . Require ( ) . Equal ( expectedMetadata [ i ] . GetDenomUnits ( ) [ 1 ] . GetDenom ( ) , actualMetadata [ i ] . GetDenomUnits ( ) [ 1 ] . GetDenom ( ) )
suite . Require ( ) . Equal ( expectedMetadata [ i ] . GetDenomUnits ( ) [ 1 ] . GetExponent ( ) , actualMetadata [ i ] . GetDenomUnits ( ) [ 1 ] . GetExponent ( ) )
suite . Require ( ) . Equal ( expectedMetadata [ i ] . GetDenomUnits ( ) [ 1 ] . GetAliases ( ) , actualMetadata [ i ] . GetDenomUnits ( ) [ 1 ] . GetAliases ( ) )
}
}
2021-02-25 10:15:02 -08:00
func ( suite * IntegrationTestSuite ) TestBalanceTrackingEvents ( ) {
// replace account keeper and bank keeper otherwise the account keeper won't be aware of the
// existence of the new module account because GetModuleAccount checks for the existence via
// permissions map and not via state... weird
maccPerms := simapp . GetMaccPerms ( )
maccPerms [ multiPerm ] = [ ] string { authtypes . Burner , authtypes . Minter , authtypes . Staking }
suite . app . AccountKeeper = authkeeper . NewAccountKeeper (
suite . app . AppCodec ( ) , suite . app . GetKey ( authtypes . StoreKey ) , suite . app . GetSubspace ( authtypes . ModuleName ) ,
authtypes . ProtoBaseAccount , maccPerms ,
)
suite . app . BankKeeper = keeper . NewBaseKeeper ( suite . app . AppCodec ( ) , suite . app . GetKey ( types . StoreKey ) ,
suite . app . AccountKeeper , suite . app . GetSubspace ( types . ModuleName ) , nil )
// set account with multiple permissions
suite . app . AccountKeeper . SetModuleAccount ( suite . ctx , multiPermAcc )
// mint coins
suite . Require ( ) . NoError (
suite . app . BankKeeper . MintCoins (
suite . ctx ,
multiPermAcc . Name ,
sdk . NewCoins ( sdk . NewCoin ( "utxo" , sdk . NewInt ( 100000 ) ) ) ) ,
)
// send coins to address
addr1 := sdk . AccAddress ( "addr1_______________" )
suite . Require ( ) . NoError (
suite . app . BankKeeper . SendCoinsFromModuleToAccount (
suite . ctx ,
multiPermAcc . Name ,
addr1 ,
sdk . NewCoins ( sdk . NewCoin ( "utxo" , sdk . NewInt ( 50000 ) ) ) ,
) ,
)
// burn coins from module account
suite . Require ( ) . NoError (
suite . app . BankKeeper . BurnCoins (
suite . ctx ,
multiPermAcc . Name ,
sdk . NewCoins ( sdk . NewInt64Coin ( "utxo" , 1000 ) ) ,
) ,
)
// process balances and supply from events
supply := sdk . NewCoins ( )
balances := make ( map [ string ] sdk . Coins )
for _ , e := range suite . ctx . EventManager ( ) . ABCIEvents ( ) {
switch e . Type {
case types . EventTypeCoinBurn :
burnedCoins , err := sdk . ParseCoinsNormalized ( ( string ) ( e . Attributes [ 1 ] . Value ) )
suite . Require ( ) . NoError ( err )
supply = supply . Sub ( burnedCoins )
case types . EventTypeCoinMint :
mintedCoins , err := sdk . ParseCoinsNormalized ( ( string ) ( e . Attributes [ 1 ] . Value ) )
suite . Require ( ) . NoError ( err )
supply = supply . Add ( mintedCoins ... )
case types . EventTypeCoinSpent :
coinsSpent , err := sdk . ParseCoinsNormalized ( ( string ) ( e . Attributes [ 1 ] . Value ) )
suite . Require ( ) . NoError ( err )
spender , err := sdk . AccAddressFromBech32 ( ( string ) ( e . Attributes [ 0 ] . Value ) )
suite . Require ( ) . NoError ( err )
balances [ spender . String ( ) ] = balances [ spender . String ( ) ] . Sub ( coinsSpent )
case types . EventTypeCoinReceived :
coinsRecv , err := sdk . ParseCoinsNormalized ( ( string ) ( e . Attributes [ 1 ] . Value ) )
suite . Require ( ) . NoError ( err )
receiver , err := sdk . AccAddressFromBech32 ( ( string ) ( e . Attributes [ 0 ] . Value ) )
suite . Require ( ) . NoError ( err )
balances [ receiver . String ( ) ] = balances [ receiver . String ( ) ] . Add ( coinsRecv ... )
}
}
// check balance and supply tracking
2021-03-03 01:58:16 -08:00
savedSupply := suite . app . BankKeeper . GetSupply ( suite . ctx , "utxo" )
utxoSupply := savedSupply
suite . Require ( ) . Equal ( utxoSupply . Amount , supply . AmountOf ( "utxo" ) )
2021-02-25 10:15:02 -08:00
// iterate accounts and check balances
suite . app . BankKeeper . IterateAllBalances ( suite . ctx , func ( address sdk . AccAddress , coin sdk . Coin ) ( stop bool ) {
// if it's not utxo coin then skip
if coin . Denom != "utxo" {
return false
}
balance , exists := balances [ address . String ( ) ]
suite . Require ( ) . True ( exists )
expectedUtxo := sdk . NewCoin ( "utxo" , balance . AmountOf ( coin . Denom ) )
suite . Require ( ) . Equal ( expectedUtxo . String ( ) , coin . String ( ) )
return false
} )
}
2020-07-28 00:49:56 -07:00
func ( suite * IntegrationTestSuite ) getTestMetadata ( ) [ ] types . Metadata {
return [ ] types . Metadata { {
2021-02-24 15:06:04 -08:00
Name : "Cosmos Hub Atom" ,
Symbol : "ATOM" ,
2020-07-28 00:49:56 -07:00
Description : "The native staking token of the Cosmos Hub." ,
2020-08-19 00:36:41 -07:00
DenomUnits : [ ] * types . DenomUnit {
2020-07-28 00:49:56 -07:00
{ "uatom" , uint32 ( 0 ) , [ ] string { "microatom" } } ,
{ "matom" , uint32 ( 3 ) , [ ] string { "milliatom" } } ,
{ "atom" , uint32 ( 6 ) , nil } ,
} ,
Base : "uatom" ,
Display : "atom" ,
} ,
{
2021-02-24 15:06:04 -08:00
Name : "Token" ,
Symbol : "TOKEN" ,
2020-07-28 00:49:56 -07:00
Description : "The native staking token of the Token Hub." ,
2020-08-19 00:36:41 -07:00
DenomUnits : [ ] * types . DenomUnit {
2020-07-28 00:49:56 -07:00
{ "1token" , uint32 ( 5 ) , [ ] string { "decitoken" } } ,
{ "2token" , uint32 ( 4 ) , [ ] string { "centitoken" } } ,
{ "3token" , uint32 ( 7 ) , [ ] string { "dekatoken" } } ,
} ,
Base : "utoken" ,
Display : "token" ,
} ,
}
}
2020-01-30 13:31:16 -08:00
func TestKeeperTestSuite ( t * testing . T ) {
suite . Run ( t , new ( IntegrationTestSuite ) )
2019-01-14 08:11:24 -08:00
}