mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 10:05:18 +00:00
Add kava modules to TestAppImportExport (#472)
* add kava modules to TestAppImportExport
This commit is contained in:
parent
ae4aee46ff
commit
c175e9e856
@ -8,9 +8,9 @@ const (
|
||||
|
||||
// Default simulation operation weights for messages and gov proposals
|
||||
const (
|
||||
DefaultWeightMsgPlaceBid int = 100
|
||||
DefaultWeightMsgCreateAtomicSwap int = 100
|
||||
DefaultWeightMsgUpdatePrices int = 100
|
||||
DefaultWeightMsgPlaceBid int = 75
|
||||
DefaultWeightMsgCreateAtomicSwap int = 50
|
||||
DefaultWeightMsgUpdatePrices int = 50
|
||||
DefaultWeightMsgCdp int = 100
|
||||
DefaultWeightMsgClaimReward int = 100
|
||||
DefaultWeightMsgClaimReward int = 50
|
||||
)
|
||||
|
@ -6,11 +6,6 @@ import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
"github.com/tendermint/tendermint/libs/log"
|
||||
dbm "github.com/tendermint/tm-db"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||
"github.com/cosmos/cosmos-sdk/simapp"
|
||||
"github.com/cosmos/cosmos-sdk/simapp/helpers"
|
||||
@ -25,6 +20,17 @@ import (
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing"
|
||||
"github.com/cosmos/cosmos-sdk/x/staking"
|
||||
"github.com/cosmos/cosmos-sdk/x/supply"
|
||||
"github.com/kava-labs/kava/x/auction"
|
||||
"github.com/kava-labs/kava/x/bep3"
|
||||
"github.com/kava-labs/kava/x/cdp"
|
||||
"github.com/kava-labs/kava/x/incentive"
|
||||
"github.com/kava-labs/kava/x/kavadist"
|
||||
"github.com/kava-labs/kava/x/pricefeed"
|
||||
validatorvesting "github.com/kava-labs/kava/x/validator-vesting"
|
||||
"github.com/stretchr/testify/require"
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
"github.com/tendermint/tendermint/libs/log"
|
||||
dbm "github.com/tendermint/tm-db"
|
||||
)
|
||||
|
||||
type StoreKeysPrefixes struct {
|
||||
@ -162,6 +168,13 @@ func TestAppImportExport(t *testing.T) {
|
||||
{app.keys[supply.StoreKey], newApp.keys[supply.StoreKey], [][]byte{}},
|
||||
{app.keys[params.StoreKey], newApp.keys[params.StoreKey], [][]byte{}},
|
||||
{app.keys[gov.StoreKey], newApp.keys[gov.StoreKey], [][]byte{}},
|
||||
{app.keys[auction.StoreKey], newApp.keys[auction.StoreKey], [][]byte{}},
|
||||
{app.keys[bep3.StoreKey], newApp.keys[bep3.StoreKey], [][]byte{}},
|
||||
{app.keys[cdp.StoreKey], newApp.keys[cdp.StoreKey], [][]byte{}},
|
||||
{app.keys[incentive.StoreKey], newApp.keys[incentive.StoreKey], [][]byte{}},
|
||||
{app.keys[kavadist.StoreKey], newApp.keys[kavadist.StoreKey], [][]byte{}},
|
||||
{app.keys[pricefeed.StoreKey], newApp.keys[pricefeed.StoreKey], [][]byte{}},
|
||||
{app.keys[validatorvesting.StoreKey], newApp.keys[validatorvesting.StoreKey], [][]byte{}},
|
||||
}
|
||||
|
||||
for _, skp := range storeKeysPrefixes {
|
||||
@ -170,8 +183,9 @@ func TestAppImportExport(t *testing.T) {
|
||||
|
||||
failedKVAs, failedKVBs := sdk.DiffKVStores(storeA, storeB, skp.Prefixes)
|
||||
require.Equal(t, len(failedKVAs), len(failedKVBs), "unequal sets of key-values to compare")
|
||||
|
||||
fmt.Printf("compared %d key/value pairs between %s and %s\n", len(failedKVAs), skp.A, skp.B)
|
||||
if len(failedKVAs) != 0 {
|
||||
fmt.Printf("found %d non-equal key/value pairs between %s and %s\n", len(failedKVAs), skp.A, skp.B)
|
||||
}
|
||||
require.Equal(t, len(failedKVAs), 0, simapp.GetSimulationLog(skp.A.Name(), app.SimulationManager().StoreDecoders, app.Codec(), failedKVAs, failedKVBs))
|
||||
}
|
||||
}
|
||||
@ -284,4 +298,4 @@ func TestAppStateDeterminism(t *testing.T) {
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -142,8 +142,6 @@ type (
|
||||
AugmentedCDPs = types.AugmentedCDPs
|
||||
Deposit = types.Deposit
|
||||
Deposits = types.Deposits
|
||||
SupplyKeeper = types.SupplyKeeper
|
||||
PricefeedKeeper = types.PricefeedKeeper
|
||||
GenesisState = types.GenesisState
|
||||
MsgCreateCDP = types.MsgCreateCDP
|
||||
MsgDeposit = types.MsgDeposit
|
||||
|
@ -4,10 +4,11 @@ import (
|
||||
"fmt"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/kava-labs/kava/x/cdp/types"
|
||||
)
|
||||
|
||||
// InitGenesis sets initial genesis state for cdp module
|
||||
func InitGenesis(ctx sdk.Context, k Keeper, pk PricefeedKeeper, sk SupplyKeeper, gs GenesisState) {
|
||||
func InitGenesis(ctx sdk.Context, k Keeper, pk types.PricefeedKeeper, sk types.SupplyKeeper, gs GenesisState) {
|
||||
|
||||
if err := gs.Validate(); err != nil {
|
||||
panic(fmt.Sprintf("failed to validate %s genesis state: %s", ModuleName, err))
|
||||
@ -61,12 +62,13 @@ func InitGenesis(ctx sdk.Context, k Keeper, pk PricefeedKeeper, sk SupplyKeeper,
|
||||
k.IndexCdpByOwner(ctx, cdp)
|
||||
ratio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.Principal.Add(cdp.AccumulatedFees))
|
||||
k.IndexCdpByCollateralRatio(ctx, cdp.Collateral.Denom, cdp.ID, ratio)
|
||||
k.IncrementTotalPrincipal(ctx, cdp.Collateral.Denom, cdp.Principal)
|
||||
k.IncrementTotalPrincipal(ctx, cdp.Collateral.Denom, cdp.Principal.Add(cdp.AccumulatedFees))
|
||||
}
|
||||
|
||||
k.SetNextCdpID(ctx, gs.StartingCdpID)
|
||||
k.SetDebtDenom(ctx, gs.DebtDenom)
|
||||
k.SetGovDenom(ctx, gs.GovDenom)
|
||||
k.SetPreviousSavingsDistribution(ctx, gs.PreviousDistributionTime)
|
||||
|
||||
for _, d := range gs.Deposits {
|
||||
k.SetDeposit(ctx, d)
|
||||
|
@ -1,7 +1,6 @@
|
||||
package keeper
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/store/prefix"
|
||||
@ -151,8 +150,7 @@ func (k Keeper) GetCdpID(ctx sdk.Context, owner sdk.AccAddress, denom string) (u
|
||||
func (k Keeper) GetCdpIdsByOwner(ctx sdk.Context, owner sdk.AccAddress) ([]uint64, bool) {
|
||||
store := prefix.NewStore(ctx.KVStore(k.key), types.CdpIDKeyPrefix)
|
||||
bz := store.Get(owner)
|
||||
// TODO figure out why this is necessary
|
||||
if bz == nil || bytes.Equal(bz, []byte{0}) {
|
||||
if bz == nil {
|
||||
return []uint64{}, false
|
||||
}
|
||||
var cdpIDs []uint64
|
||||
@ -272,13 +270,8 @@ func (k Keeper) IndexCdpByOwner(ctx sdk.Context, cdp types.CDP) {
|
||||
store.Set(cdp.Owner, idBytes)
|
||||
return
|
||||
}
|
||||
for _, id := range cdpIDs {
|
||||
if id == cdp.ID {
|
||||
return
|
||||
}
|
||||
cdpIDs = append(cdpIDs, cdp.ID)
|
||||
store.Set(cdp.Owner, k.cdc.MustMarshalBinaryLengthPrefixed(cdpIDs))
|
||||
}
|
||||
cdpIDs = append(cdpIDs, cdp.ID)
|
||||
store.Set(cdp.Owner, k.cdc.MustMarshalBinaryLengthPrefixed(cdpIDs))
|
||||
}
|
||||
|
||||
// RemoveCdpOwnerIndex deletes the cdp id from the store's index of cdps by owner
|
||||
@ -296,9 +289,9 @@ func (k Keeper) RemoveCdpOwnerIndex(ctx sdk.Context, cdp types.CDP) {
|
||||
}
|
||||
if len(updatedCdpIds) == 0 {
|
||||
store.Delete(cdp.Owner)
|
||||
return
|
||||
}
|
||||
store.Set(cdp.Owner, k.cdc.MustMarshalBinaryLengthPrefixed(updatedCdpIds))
|
||||
|
||||
}
|
||||
|
||||
// IndexCdpByCollateralRatio sets the cdp id in the store, indexed by the collateral type and collateral to debt ratio
|
||||
|
@ -66,7 +66,7 @@ func (suite *SavingsTestSuite) TestGetSetPreviousDistributionTime() {
|
||||
now := tmtime.Now()
|
||||
|
||||
_, f := suite.keeper.GetPreviousSavingsDistribution(suite.ctx)
|
||||
suite.False(f)
|
||||
suite.True(f)
|
||||
|
||||
suite.NotPanics(func() { suite.keeper.SetPreviousSavingsDistribution(suite.ctx, now) })
|
||||
|
||||
|
@ -11,13 +11,13 @@ import (
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/module"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||
sim "github.com/cosmos/cosmos-sdk/x/simulation"
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
|
||||
"github.com/kava-labs/kava/x/cdp/client/cli"
|
||||
"github.com/kava-labs/kava/x/cdp/client/rest"
|
||||
"github.com/kava-labs/kava/x/cdp/simulation"
|
||||
"github.com/kava-labs/kava/x/cdp/types"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -76,13 +76,13 @@ type AppModule struct {
|
||||
AppModuleBasic
|
||||
|
||||
keeper Keeper
|
||||
accountKeeper auth.AccountKeeper
|
||||
pricefeedKeeper PricefeedKeeper
|
||||
supplyKeeper SupplyKeeper
|
||||
accountKeeper types.AccountKeeper
|
||||
pricefeedKeeper types.PricefeedKeeper
|
||||
supplyKeeper types.SupplyKeeper
|
||||
}
|
||||
|
||||
// NewAppModule creates a new AppModule object
|
||||
func NewAppModule(keeper Keeper, accountKeeper auth.AccountKeeper, pricefeedKeeper PricefeedKeeper, supplyKeeper SupplyKeeper) AppModule {
|
||||
func NewAppModule(keeper Keeper, accountKeeper types.AccountKeeper, pricefeedKeeper types.PricefeedKeeper, supplyKeeper types.SupplyKeeper) AppModule {
|
||||
return AppModule{
|
||||
AppModuleBasic: AppModuleBasic{},
|
||||
keeper: keeper,
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
"github.com/cosmos/cosmos-sdk/simapp/helpers"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
|
||||
"github.com/cosmos/cosmos-sdk/x/simulation"
|
||||
|
||||
@ -23,7 +22,7 @@ const (
|
||||
|
||||
// WeightedOperations returns all the operations from the module with their respective weights
|
||||
func WeightedOperations(
|
||||
appParams simulation.AppParams, cdc *codec.Codec, ak auth.AccountKeeper,
|
||||
appParams simulation.AppParams, cdc *codec.Codec, ak types.AccountKeeper,
|
||||
k keeper.Keeper, pfk types.PricefeedKeeper,
|
||||
) simulation.WeightedOperations {
|
||||
var weightMsgCdp int
|
||||
@ -43,7 +42,7 @@ func WeightedOperations(
|
||||
}
|
||||
|
||||
// SimulateMsgCdp generates a MsgCreateCdp or MsgDepositCdp with random values.
|
||||
func SimulateMsgCdp(ak auth.AccountKeeper, k keeper.Keeper, pfk types.PricefeedKeeper) simulation.Operation {
|
||||
func SimulateMsgCdp(ak types.AccountKeeper, k keeper.Keeper, pfk types.PricefeedKeeper) simulation.Operation {
|
||||
return func(
|
||||
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, chainID string,
|
||||
) (simulation.OperationMsg, []simulation.FutureOperation, error) {
|
||||
@ -125,12 +124,12 @@ func SimulateMsgCdp(ak auth.AccountKeeper, k keeper.Keeper, pfk types.PricefeedK
|
||||
simAccount.PrivKey,
|
||||
)
|
||||
|
||||
_, result, err := app.Deliver(tx)
|
||||
_, _, err := app.Deliver(tx)
|
||||
if err != nil {
|
||||
return simulation.NoOpMsg(types.ModuleName), nil, err
|
||||
}
|
||||
|
||||
return simulation.NewOperationMsg(msg, true, result.Log), nil, nil
|
||||
return simulation.NewOperationMsg(msg, true, ""), nil, nil
|
||||
}
|
||||
|
||||
// a cdp already exists, deposit to it, draw debt from it, or repay debt to it
|
||||
@ -149,12 +148,12 @@ func SimulateMsgCdp(ak auth.AccountKeeper, k keeper.Keeper, pfk types.PricefeedK
|
||||
simAccount.PrivKey,
|
||||
)
|
||||
|
||||
_, result, err := app.Deliver(tx)
|
||||
_, _, err := app.Deliver(tx)
|
||||
if err != nil {
|
||||
return simulation.NoOpMsg(types.ModuleName), nil, err
|
||||
}
|
||||
|
||||
return simulation.NewOperationMsg(msg, true, result.Log), nil, nil
|
||||
return simulation.NewOperationMsg(msg, true, ""), nil, nil
|
||||
}
|
||||
|
||||
// deposit 25% of the time
|
||||
@ -172,12 +171,12 @@ func SimulateMsgCdp(ak auth.AccountKeeper, k keeper.Keeper, pfk types.PricefeedK
|
||||
simAccount.PrivKey,
|
||||
)
|
||||
|
||||
_, result, err := app.Deliver(tx)
|
||||
_, _, err := app.Deliver(tx)
|
||||
if err != nil {
|
||||
return simulation.NoOpMsg(types.ModuleName), nil, err
|
||||
}
|
||||
|
||||
return simulation.NewOperationMsg(msg, true, result.Log), nil, nil
|
||||
return simulation.NewOperationMsg(msg, true, ""), nil, nil
|
||||
}
|
||||
|
||||
// draw debt 25% of the time
|
||||
@ -215,12 +214,13 @@ func SimulateMsgCdp(ak auth.AccountKeeper, k keeper.Keeper, pfk types.PricefeedK
|
||||
simAccount.PrivKey,
|
||||
)
|
||||
|
||||
_, result, err := app.Deliver(tx)
|
||||
_, _, err := app.Deliver(tx)
|
||||
|
||||
if err != nil {
|
||||
return simulation.NoOpMsg(types.ModuleName), nil, err
|
||||
}
|
||||
|
||||
return simulation.NewOperationMsg(msg, true, result.Log), nil, nil
|
||||
return simulation.NewOperationMsg(msg, true, ""), nil, nil
|
||||
}
|
||||
|
||||
// repay debt 25% of the time
|
||||
@ -248,12 +248,12 @@ func SimulateMsgCdp(ak auth.AccountKeeper, k keeper.Keeper, pfk types.PricefeedK
|
||||
simAccount.PrivKey,
|
||||
)
|
||||
|
||||
_, result, err := app.Deliver(tx)
|
||||
_, _, err := app.Deliver(tx)
|
||||
if err != nil {
|
||||
return simulation.NoOpMsg(types.ModuleName), nil, err
|
||||
}
|
||||
|
||||
return simulation.NewOperationMsg(msg, true, result.Log), nil, nil
|
||||
return simulation.NewOperationMsg(msg, true, ""), nil, nil
|
||||
}
|
||||
|
||||
return simulation.NoOpMsg(types.ModuleName), nil, nil
|
||||
|
@ -9,7 +9,7 @@ import (
|
||||
pftypes "github.com/kava-labs/kava/x/pricefeed/types"
|
||||
)
|
||||
|
||||
// SupplyKeeper defines the expected supply keeper for module accounts
|
||||
// SupplyKeeper defines the expected supply keeper for module accounts (noalias)
|
||||
type SupplyKeeper interface {
|
||||
GetModuleAddress(name string) sdk.AccAddress
|
||||
GetModuleAccount(ctx sdk.Context, name string) supplyexported.ModuleAccountI
|
||||
@ -25,7 +25,7 @@ type SupplyKeeper interface {
|
||||
GetSupply(ctx sdk.Context) (supply supplyexported.SupplyI)
|
||||
}
|
||||
|
||||
// PricefeedKeeper defines the expected interface for the pricefeed
|
||||
// PricefeedKeeper defines the expected interface for the pricefeed (noalias)
|
||||
type PricefeedKeeper interface {
|
||||
GetCurrentPrice(sdk.Context, string) (pftypes.CurrentPrice, error)
|
||||
GetParams(sdk.Context) pftypes.Params
|
||||
@ -45,4 +45,5 @@ type AuctionKeeper interface {
|
||||
// AccountKeeper expected interface for the account keeper (noalias)
|
||||
type AccountKeeper interface {
|
||||
IterateAccounts(ctx sdk.Context, cb func(account authexported.Account) (stop bool))
|
||||
GetAccount(ctx sdk.Context, addr sdk.AccAddress) authexported.Account
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/exported"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/kava-labs/kava/x/validator-vesting/types"
|
||||
"github.com/tendermint/tendermint/libs/kv"
|
||||
)
|
||||
@ -15,9 +15,9 @@ import (
|
||||
func DecodeStore(cdc *codec.Codec, kvA, kvB kv.Pair) string {
|
||||
switch {
|
||||
case bytes.Equal(kvA.Key[:1], types.ValidatorVestingAccountPrefix):
|
||||
var accA, accB exported.Account
|
||||
cdc.MustUnmarshalBinaryBare(kvA.Value, &accA)
|
||||
cdc.MustUnmarshalBinaryBare(kvB.Value, &accB)
|
||||
var accA, accB sdk.AccAddress
|
||||
accA = sdk.AccAddress(kvA.Key[1:])
|
||||
accB = sdk.AccAddress(kvB.Key[1:])
|
||||
return fmt.Sprintf("%v\n%v", accA, accB)
|
||||
case bytes.Equal(kvA.Key, types.BlocktimeKey):
|
||||
var btA, btB time.Time
|
||||
|
@ -5,14 +5,18 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tendermint/tendermint/libs/kv"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||
|
||||
"github.com/kava-labs/kava/x/validator-vesting/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tendermint/tendermint/crypto/ed25519"
|
||||
"github.com/tendermint/tendermint/libs/kv"
|
||||
)
|
||||
|
||||
var (
|
||||
pk1 = ed25519.GenPrivKey().PubKey()
|
||||
addr1 = sdk.AccAddress(pk1.Address())
|
||||
)
|
||||
|
||||
func makeTestCodec() (cdc *codec.Codec) {
|
||||
@ -28,11 +32,10 @@ func makeTestCodec() (cdc *codec.Codec) {
|
||||
func TestDecodeDistributionStore(t *testing.T) {
|
||||
cdc := makeTestCodec()
|
||||
|
||||
acc := types.ValidatorVestingAccount{SigningThreshold: 1}
|
||||
now := time.Now().UTC()
|
||||
|
||||
kvPairs := kv.Pairs{
|
||||
kv.Pair{Key: types.ValidatorVestingAccountPrefix, Value: cdc.MustMarshalBinaryBare(acc)},
|
||||
kv.Pair{Key: append(types.ValidatorVestingAccountPrefix, addr1.Bytes()...), Value: []byte{0}},
|
||||
kv.Pair{Key: types.BlocktimeKey, Value: cdc.MustMarshalBinaryLengthPrefixed(now)},
|
||||
kv.Pair{Key: []byte{0x99}, Value: []byte{0x99}},
|
||||
}
|
||||
@ -41,7 +44,7 @@ func TestDecodeDistributionStore(t *testing.T) {
|
||||
name string
|
||||
expectedLog string
|
||||
}{
|
||||
{"ValidatorVestingAccount", fmt.Sprintf("%v\n%v", acc, acc)},
|
||||
{"ValidatorVestingAccount", fmt.Sprintf("%v\n%v", addr1, addr1)},
|
||||
{"BlockTime", fmt.Sprintf("%s\n%s", now, now)},
|
||||
{"other", ""},
|
||||
}
|
||||
|
@ -73,6 +73,8 @@ func RandomizedGenState(simState *module.SimulationState) {
|
||||
}
|
||||
newAuthGenesis := authtypes.NewGenesisState(authGenState.Params, newGenesisAccs)
|
||||
simState.GenState[authtypes.ModuleName] = simState.Cdc.MustMarshalJSON(newAuthGenesis)
|
||||
vestGenState := types.DefaultGenesisState()
|
||||
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(vestGenState)
|
||||
}
|
||||
|
||||
func getRandomValidatorConsAddr(simState *module.SimulationState, rint int) sdk.ConsAddress {
|
||||
|
Loading…
Reference in New Issue
Block a user