2021-03-16 19:02:11 +00:00
package v0_14
2021-01-18 19:12:37 +00:00
import (
2021-02-22 21:08:23 +00:00
"fmt"
2021-01-18 19:12:37 +00:00
"io/ioutil"
"os"
"path/filepath"
"testing"
2021-02-19 21:05:42 +00:00
"github.com/cosmos/cosmos-sdk/codec"
2021-01-18 19:12:37 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
2021-01-21 17:37:35 +00:00
"github.com/cosmos/cosmos-sdk/x/auth"
2021-02-22 21:08:23 +00:00
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
2021-02-23 01:28:07 +00:00
"github.com/cosmos/cosmos-sdk/x/genutil"
2021-02-22 21:08:23 +00:00
"github.com/cosmos/cosmos-sdk/x/supply"
supplyexported "github.com/cosmos/cosmos-sdk/x/supply/exported"
2021-01-18 19:12:37 +00:00
2021-02-23 01:28:07 +00:00
tmtypes "github.com/tendermint/tendermint/types"
2021-01-18 19:12:37 +00:00
"github.com/kava-labs/kava/app"
2021-02-22 21:47:28 +00:00
"github.com/kava-labs/kava/x/bep3"
2021-01-18 19:12:37 +00:00
v0_11cdp "github.com/kava-labs/kava/x/cdp/legacy/v0_11"
2021-03-16 19:02:11 +00:00
v0_14committee "github.com/kava-labs/kava/x/committee"
2021-02-19 21:05:42 +00:00
v0_11committee "github.com/kava-labs/kava/x/committee/legacy/v0_11"
2021-03-16 19:02:11 +00:00
v0_14hard "github.com/kava-labs/kava/x/hard"
2021-02-22 21:08:23 +00:00
v0_11hard "github.com/kava-labs/kava/x/hard/legacy/v0_11"
2021-03-16 19:02:11 +00:00
v0_14incentive "github.com/kava-labs/kava/x/incentive"
2021-02-22 21:08:23 +00:00
v0_11incentive "github.com/kava-labs/kava/x/incentive/legacy/v0_11"
2021-02-22 21:53:00 +00:00
v0_11pricefeed "github.com/kava-labs/kava/x/pricefeed"
2021-02-22 21:15:10 +00:00
validatorvesting "github.com/kava-labs/kava/x/validator-vesting"
2021-01-18 19:12:37 +00:00
"github.com/stretchr/testify/require"
)
func TestMain ( m * testing . M ) {
config := sdk . GetConfig ( )
app . SetBech32AddressPrefixes ( config )
app . SetBip44CoinType ( config )
os . Exit ( m . Run ( ) )
}
2021-02-19 21:05:42 +00:00
func TestCDP ( t * testing . T ) {
2021-01-18 19:12:37 +00:00
bz , err := ioutil . ReadFile ( filepath . Join ( "testdata" , "kava-4-cdp-state-block-500000.json" ) )
require . NoError ( t , err )
var oldGenState v0_11cdp . GenesisState
cdc := app . MakeCodec ( )
require . NotPanics ( t , func ( ) {
cdc . MustUnmarshalJSON ( bz , & oldGenState )
} )
2021-02-19 21:05:42 +00:00
newGenState := CDP ( oldGenState )
2021-01-18 19:12:37 +00:00
err = newGenState . Validate ( )
require . NoError ( t , err )
require . Equal ( t , len ( newGenState . Params . CollateralParams ) , len ( newGenState . PreviousAccumulationTimes ) )
cdp1 := newGenState . CDPs [ 0 ]
require . Equal ( t , sdk . OneDec ( ) , cdp1 . InterestFactor )
}
2021-01-21 17:37:35 +00:00
2021-02-19 21:05:42 +00:00
func TestAuth ( t * testing . T ) {
2021-02-22 21:15:10 +00:00
validatorVestingChangeAddress , err := sdk . AccAddressFromBech32 ( "kava1a3qmze57knfj29a5knqs5ptewh76v4fg23xsvn" )
if err != nil {
panic ( err )
}
validatorVestingUpdatedValAddress , err := sdk . ConsAddressFromBech32 ( "kavavalcons1ucxhn6zh7y2zun49m36psjffrhmux7ukqxdcte" )
if err != nil {
panic ( err )
}
2021-01-21 17:37:35 +00:00
bz , err := ioutil . ReadFile ( filepath . Join ( "testdata" , "kava-4-auth-state-block-500000.json" ) )
require . NoError ( t , err )
var oldGenState auth . GenesisState
cdc := app . MakeCodec ( )
require . NotPanics ( t , func ( ) {
cdc . MustUnmarshalJSON ( bz , & oldGenState )
} )
2021-02-22 21:08:23 +00:00
harvestCoins := getModuleAccount ( oldGenState . Accounts , "harvest" ) . GetCoins ( )
2021-02-19 21:05:42 +00:00
newGenState := Auth ( oldGenState )
2021-02-22 21:15:10 +00:00
for _ , acc := range newGenState . Accounts {
if acc . GetAddress ( ) . Equals ( validatorVestingChangeAddress ) {
vacc := acc . ( * validatorvesting . ValidatorVestingAccount )
require . Equal ( t , int64 ( 0 ) , vacc . CurrentPeriodProgress . MissedBlocks )
require . Equal ( t , validatorVestingUpdatedValAddress , vacc . ValidatorAddress )
}
}
2021-02-22 21:08:23 +00:00
2021-01-21 17:37:35 +00:00
err = auth . ValidateGenesis ( newGenState )
require . NoError ( t , err )
2021-02-22 21:08:23 +00:00
require . Equal ( t , len ( oldGenState . Accounts ) , len ( newGenState . Accounts ) + 3 )
require . Nil ( t , getModuleAccount ( newGenState . Accounts , "harvest" ) )
require . Equal ( t , getModuleAccount ( newGenState . Accounts , "hard" ) . GetCoins ( ) , harvestCoins )
}
func getModuleAccount ( accounts authexported . GenesisAccounts , name string ) supplyexported . ModuleAccountI {
modAcc , ok := getAccount ( accounts , supply . NewModuleAddress ( name ) ) . ( supplyexported . ModuleAccountI )
if ! ok {
return nil
}
return modAcc
}
func getAccount ( accounts authexported . GenesisAccounts , address sdk . AccAddress ) authexported . GenesisAccount {
for _ , acc := range accounts {
if acc . GetAddress ( ) . Equals ( address ) {
return acc
}
}
return nil
}
func TestIncentive ( t * testing . T ) {
bz , err := ioutil . ReadFile ( filepath . Join ( "testdata" , "kava-4-incentive-state.json" ) )
require . NoError ( t , err )
var oldIncentiveGenState v0_11incentive . GenesisState
cdc := app . MakeCodec ( )
require . NotPanics ( t , func ( ) {
cdc . MustUnmarshalJSON ( bz , & oldIncentiveGenState )
} )
bz , err = ioutil . ReadFile ( filepath . Join ( "testdata" , "kava-4-harvest-state.json" ) )
require . NoError ( t , err )
var oldHarvestGenState v0_11hard . GenesisState
require . NotPanics ( t , func ( ) {
cdc . MustUnmarshalJSON ( bz , & oldHarvestGenState )
} )
2021-03-16 19:02:11 +00:00
newGenState := v0_14incentive . GenesisState { }
2021-02-22 21:08:23 +00:00
require . NotPanics ( t , func ( ) {
newGenState = Incentive ( oldHarvestGenState , oldIncentiveGenState )
} )
err = newGenState . Validate ( )
require . NoError ( t , err )
fmt . Printf ( "Number of incentive claims in kava-4: %d\nNumber of incentive Claims in kava-5: %d\n" ,
len ( oldIncentiveGenState . Claims ) , len ( newGenState . USDXMintingClaims ) ,
)
fmt . Printf ( "Number of harvest claims in kava-4: %d\nNumber of hard claims in kava-5: %d\n" , len ( oldHarvestGenState . Claims ) , len ( newGenState . HardLiquidityProviderClaims ) )
}
2021-01-21 17:37:35 +00:00
2021-02-22 21:08:23 +00:00
func TestHard ( t * testing . T ) {
cdc := app . MakeCodec ( )
bz , err := ioutil . ReadFile ( filepath . Join ( "testdata" , "kava-4-harvest-state.json" ) )
require . NoError ( t , err )
var oldHarvestGenState v0_11hard . GenesisState
require . NotPanics ( t , func ( ) {
cdc . MustUnmarshalJSON ( bz , & oldHarvestGenState )
} )
2021-03-16 19:02:11 +00:00
newGenState := v0_14hard . GenesisState { }
2021-02-22 21:08:23 +00:00
require . NotPanics ( t , func ( ) {
newGenState = Hard ( oldHarvestGenState )
} )
err = newGenState . Validate ( )
require . NoError ( t , err )
2021-01-21 17:37:35 +00:00
}
2021-02-19 21:05:42 +00:00
func TestCommittee ( t * testing . T ) {
bz , err := ioutil . ReadFile ( filepath . Join ( "testdata" , "kava-4-committee-state.json" ) )
require . NoError ( t , err )
var oldGenState v0_11committee . GenesisState
cdc := codec . New ( )
sdk . RegisterCodec ( cdc )
v0_11committee . RegisterCodec ( cdc )
require . NotPanics ( t , func ( ) {
cdc . MustUnmarshalJSON ( bz , & oldGenState )
} )
newGenState := Committee ( oldGenState )
err = newGenState . Validate ( )
require . NoError ( t , err )
require . Equal ( t , len ( oldGenState . Committees ) , len ( newGenState . Committees ) )
for i := 0 ; i < len ( oldGenState . Committees ) ; i ++ {
require . Equal ( t , len ( oldGenState . Committees [ i ] . Permissions ) , len ( newGenState . Committees [ i ] . Permissions ) )
}
oldSPCP := oldGenState . Committees [ 0 ] . Permissions [ 0 ] . ( v0_11committee . SubParamChangePermission )
2021-03-16 19:02:11 +00:00
newSPCP := newGenState . Committees [ 0 ] . Permissions [ 0 ] . ( v0_14committee . SubParamChangePermission )
require . Equal ( t , len ( oldSPCP . AllowedParams ) - 14 , len ( newSPCP . AllowedParams ) ) // accounts for removed/redundant keys
2021-02-19 21:05:42 +00:00
require . Equal ( t , len ( oldSPCP . AllowedAssetParams ) , len ( newSPCP . AllowedAssetParams ) )
2021-03-16 19:02:11 +00:00
require . Equal ( t , len ( oldSPCP . AllowedCollateralParams ) + 3 , len ( newSPCP . AllowedCollateralParams ) ) // accounts for new cdp collateral types
2021-02-19 21:05:42 +00:00
require . Equal ( t , len ( oldSPCP . AllowedMarkets ) , len ( newSPCP . AllowedMarkets ) )
}
2021-02-22 21:47:28 +00:00
2021-02-22 21:53:00 +00:00
func TestPricefeed ( t * testing . T ) {
cdc := app . MakeCodec ( )
bz , err := ioutil . ReadFile ( filepath . Join ( "testdata" , "kava-4-pricefeed-state.json" ) )
require . NoError ( t , err )
var oldPricefeedGenState v0_11pricefeed . GenesisState
require . NotPanics ( t , func ( ) {
cdc . MustUnmarshalJSON ( bz , & oldPricefeedGenState )
} )
newGenState := Pricefeed ( oldPricefeedGenState )
err = newGenState . Validate ( )
require . NoError ( t , err )
require . Equal ( t , len ( oldPricefeedGenState . Params . Markets ) + 1 , len ( newGenState . Params . Markets ) )
}
2021-02-22 21:47:28 +00:00
func TestBep3 ( t * testing . T ) {
bz , err := ioutil . ReadFile ( filepath . Join ( "testdata" , "kava-4-bep3-state.json" ) )
require . NoError ( t , err )
var oldGenState bep3 . GenesisState
cdc := app . MakeCodec ( )
require . NotPanics ( t , func ( ) {
cdc . MustUnmarshalJSON ( bz , & oldGenState )
} )
newGenState := Bep3 ( oldGenState )
err = newGenState . Validate ( )
require . NoError ( t , err )
var oldBNBSupply bep3 . AssetSupply
var newBNBSupply bep3 . AssetSupply
for _ , supply := range oldGenState . Supplies {
if supply . GetDenom ( ) == "bnb" {
oldBNBSupply = supply
}
}
for _ , supply := range newGenState . Supplies {
if supply . GetDenom ( ) == "bnb" {
newBNBSupply = supply
}
}
require . Equal ( t , oldBNBSupply . CurrentSupply . Sub ( sdk . NewCoin ( "bnb" , sdk . NewInt ( 1000000000000 ) ) ) , newBNBSupply . CurrentSupply )
require . Equal ( t , uint64 ( 24686 ) , newGenState . Params . AssetParams [ 0 ] . MinBlockLock )
require . Equal ( t , uint64 ( 86400 ) , newGenState . Params . AssetParams [ 0 ] . MaxBlockLock )
}
2021-02-23 01:28:07 +00:00
func TestMigrateFull ( t * testing . T ) {
oldGenDoc , err := tmtypes . GenesisDocFromFile ( filepath . Join ( "testdata" , "kava-4-export.json" ) )
require . NoError ( t , err )
// 2) migrate
newGenDoc := Migrate ( * oldGenDoc )
tApp := app . NewTestApp ( )
cdc := app . MakeCodec ( )
var newAppState genutil . AppMap
require . NoError ( t ,
cdc . UnmarshalJSON ( newGenDoc . AppState , & newAppState ) ,
)
err = app . ModuleBasics . ValidateGenesis ( newAppState )
if err != nil {
require . NoError ( t , err )
}
require . NotPanics ( t , func ( ) {
// this runs both InitGenesis for all modules (which panic on errors) and runs all invariants
tApp . InitializeFromGenesisStatesWithTime ( newGenDoc . GenesisTime , app . GenesisState ( newAppState ) )
} )
}