From 9748dca39bad23c378541574aa29a7a8eb5b3dcd Mon Sep 17 00:00:00 2001 From: deepan95dev Date: Tue, 27 Feb 2024 10:51:47 +0530 Subject: [PATCH] chore: consensus module included --- .github/linters/.golangci.yaml | 1 - app/app.go | 12 ++ app/migrations/migration_did_protobuf.go | 176 ------------------ app/migrations/migration_resource_protobuf.go | 80 -------- app/test_helpers.go | 157 ++++++++-------- 5 files changed, 88 insertions(+), 338 deletions(-) delete mode 100644 app/migrations/migration_did_protobuf.go delete mode 100644 app/migrations/migration_resource_protobuf.go diff --git a/.github/linters/.golangci.yaml b/.github/linters/.golangci.yaml index f44739ece..3874ac530 100644 --- a/.github/linters/.golangci.yaml +++ b/.github/linters/.golangci.yaml @@ -3,7 +3,6 @@ run: linters: - disable-all: true enable: - bodyclose - depguard diff --git a/app/app.go b/app/app.go index e633084d9..a0947cceb 100644 --- a/app/app.go +++ b/app/app.go @@ -7,6 +7,8 @@ import ( "os" "path/filepath" + "github.com/cosmos/cosmos-sdk/x/consensus" + autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1" "github.com/cheqd/cheqd-node/app/migrations" @@ -121,6 +123,7 @@ import ( porttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types" ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported" ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper" + ibctmmigrations "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint/migrations" "github.com/gorilla/mux" "github.com/rakyll/statik/fs" "github.com/spf13/cast" @@ -166,6 +169,7 @@ var ( did.AppModuleBasic{}, resource.AppModuleBasic{}, ibcfee.AppModuleBasic{}, + consensus.AppModuleBasic{}, ) // module account permissions @@ -627,6 +631,7 @@ func New( params.NewAppModule(app.ParamsKeeper), // IBC modules transfer.NewAppModule(app.TransferKeeper), + consensus.NewAppModule(appCodec, app.ConsensusParamsKeeper), ibcfee.NewAppModule(app.IBCFeeKeeper), icaModule, // cheqd modules @@ -663,6 +668,7 @@ func New( ibcfeetypes.ModuleName, didtypes.ModuleName, resourcetypes.ModuleName, + consensusparamtypes.ModuleName, ) app.ModuleManager.SetOrderEndBlockers( @@ -689,6 +695,7 @@ func New( vestingtypes.ModuleName, icatypes.ModuleName, ibcfeetypes.ModuleName, + consensusparamtypes.ModuleName, ) // NOTE: The genutils module must occur after staking so that pools are @@ -720,6 +727,7 @@ func New( vestingtypes.ModuleName, upgradetypes.ModuleName, paramstypes.ModuleName, + consensusparamtypes.ModuleName, } app.ModuleManager.SetOrderInitGenesis(genesisModuleOrder...) app.ModuleManager.SetOrderExportGenesis(genesisModuleOrder...) @@ -1186,6 +1194,10 @@ func (app *App) RegisterUpgradeHandlers() { app.UpgradeKeeper.SetUpgradeHandler( upgradeV2.UpgradeName, func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) { + _, err := ibctmmigrations.PruneExpiredConsensusStates(ctx, app.appCodec, app.IBCKeeper.ClientKeeper) + if err != nil { + return nil, err + } ctx.Logger().Info("Handler for upgrade plan: " + upgradeV2.UpgradeName) // Migrate Tendermint consensus parameters from x/params module to a // dedicated x/consensus module. diff --git a/app/migrations/migration_did_protobuf.go b/app/migrations/migration_did_protobuf.go deleted file mode 100644 index 9e6a9735e..000000000 --- a/app/migrations/migration_did_protobuf.go +++ /dev/null @@ -1,176 +0,0 @@ -package migrations - -// import ( -// "encoding/json" - -// "github.com/cheqd/cheqd-node/app/migrations/helpers" -// didtypes "github.com/cheqd/cheqd-node/x/did/types" -// didtypesv1 "github.com/cheqd/cheqd-node/x/did/types/v1" -// didutils "github.com/cheqd/cheqd-node/x/did/utils" -// "github.com/cosmos/cosmos-sdk/codec" -// codectypes "github.com/cosmos/cosmos-sdk/codec/types" -// sdk "github.com/cosmos/cosmos-sdk/types" -// ) - -// func MigrateDidProtobuf(sctx sdk.Context, mctx MigrationContext) error { -// sctx.Logger().Debug("MigrateDidProtobuf: Starting migration") - -// codec := NewLegacyProtoCodec() -// store := sctx.KVStore(mctx.didStoreKey) - -// sctx.Logger().Debug("MigrateDidProtobuf: Erasing old count key") -// // Erase old broken count key -// store.Delete([]byte(didtypesv1.DidCountKey + didtypesv1.DidCountKey)) - -// sctx.Logger().Debug("MigrateResourceProtobuf: Reading all keys") -// didKeys := helpers.ReadAllKeys(store, didutils.StrBytes(didtypesv1.DidKey)) - -// for _, didKey := range didKeys { -// sctx.Logger().Debug("MigrateDidProtobuf: Starting migration for didKey: " + string(didKey)) - -// var stateValue didtypesv1.StateValue -// sctx.Logger().Debug("MigrateDidProtobuf: Reading StateValue of DID from store") -// codec.MustUnmarshal(store.Get(didKey), &stateValue) - -// sctx.Logger().Debug("MigrateDidProtobuf: Migrating StateValue for DidDocWithMetadata") -// newDidDocWithMetadata, err := MigrateStateValue(sctx, mctx, &stateValue) -// if err != nil { -// return err -// } - -// // Remove old DID Doc -// store.Delete(didKey) - -// sctx.Logger().Debug("MigrateDidProtobuf: Setting DidDocWithMetadata to store") -// // Set new DID Doc -// err = mctx.didKeeperNew.AddNewDidDocVersion(&sctx, &newDidDocWithMetadata) -// if err != nil { -// return err -// } -// sctx.Logger().Debug("MigrateDidProtobuf: Migration finished for didKey: " + string(didKey)) -// } - -// // Migrate DID namespace (at least make sure it's not changed) -// if didtypesv1.DidNamespaceKey != didtypes.DidNamespaceKey { -// panic("DID namespace key is changed") -// } -// sctx.Logger().Debug("MigrateDidProtobuf: Migration finished") - -// return nil -// } - -// func NewLegacyProtoCodec() *codec.ProtoCodec { -// ir := codectypes.NewInterfaceRegistry() - -// ir.RegisterInterface("StateValueData", (*didtypesv1.StateValueData)(nil)) -// ir.RegisterImplementations((*didtypesv1.StateValueData)(nil), &didtypesv1.Did{}) - -// return codec.NewProtoCodec(ir) -// } - -// func MigrateStateValue(sctx sdk.Context, mctx MigrationContext, stateValue *didtypesv1.StateValue) (didtypes.DidDocWithMetadata, error) { -// oldDidDoc, err := stateValue.UnpackDataAsDid() -// if err != nil { -// return didtypes.DidDocWithMetadata{}, err -// } - -// oldMetadata := stateValue.Metadata - -// sctx.Logger().Debug("MigrateDidProtobuf: OldMetadata: " + string(mctx.codec.MustMarshalJSON(oldMetadata))) -// newDidDoc := MigrateDidDoc(oldDidDoc) -// newMetadata := MigrateMetadata(oldMetadata) -// sctx.Logger().Debug("MigrateDidProtobuf: NewMetadata: " + string(mctx.codec.MustMarshalJSON(&newMetadata))) - -// return didtypes.DidDocWithMetadata{ -// DidDoc: &newDidDoc, -// Metadata: &newMetadata, -// }, nil -// } - -// func MigrateMetadata(metadata *didtypesv1.Metadata) didtypes.Metadata { -// updated := helpers.MustParseFromStringTimeToGoTime(metadata.Updated) -// return didtypes.Metadata{ -// Created: helpers.MustParseFromStringTimeToGoTime(metadata.Created), -// Updated: &updated, -// Deactivated: metadata.Deactivated, -// VersionId: metadata.VersionId, -// NextVersionId: "", -// PreviousVersionId: "", -// } -// } - -// func MigrateDidDoc(oldDid *didtypesv1.Did) didtypes.DidDoc { -// vms := []*didtypes.VerificationMethod{} -// for _, vm := range oldDid.VerificationMethod { -// vms = append( -// vms, -// &didtypes.VerificationMethod{ -// Id: vm.Id, -// VerificationMethodType: MigrateType(vm.Type), -// Controller: vm.Controller, -// VerificationMaterial: MigrateVerificationMaterial(vm), -// }) -// } - -// srvs := []*didtypes.Service{} -// for _, srv := range oldDid.Service { -// srvs = append( -// srvs, -// &didtypes.Service{ -// Id: srv.Id, -// ServiceType: srv.Type, -// ServiceEndpoint: []string{srv.ServiceEndpoint}, -// }) -// } - -// return didtypes.DidDoc{ -// Id: oldDid.Id, -// VerificationMethod: vms, -// Service: srvs, -// Context: oldDid.Context, -// Controller: oldDid.Controller, -// Authentication: oldDid.Authentication, -// AssertionMethod: oldDid.AssertionMethod, -// CapabilityDelegation: oldDid.CapabilityDelegation, -// CapabilityInvocation: oldDid.CapabilityInvocation, -// KeyAgreement: oldDid.KeyAgreement, -// AlsoKnownAs: oldDid.AlsoKnownAs, -// } -// } - -// func MigrateType(t string) string { -// switch t { -// case didtypesv1.Ed25519VerificationKey2020: -// return didtypes.Ed25519VerificationKey2020Type -// case didtypesv1.JSONWebKey2020: -// return didtypes.JSONWebKey2020Type -// default: -// panic("Unknown type") -// } -// } - -// func MigrateVerificationMaterial(vm *didtypesv1.VerificationMethod) string { -// switch vm.Type { -// case didtypesv1.JSONWebKey2020: -// jwk := make(map[string]string) -// for _, kv := range vm.PublicKeyJwk { -// jwk[kv.Key] = kv.Value -// } -// res, err := json.Marshal(jwk) -// if err != nil { -// panic(err) -// } -// return string(res) - -// case didtypesv1.Ed25519VerificationKey2020: -// pkMulti, err := helpers.GenerateEd25519VerificationKey2020VerificationMaterial(vm.PublicKeyMultibase) -// if err != nil { -// panic(err) -// } - -// return pkMulti - -// default: -// panic("Unknown type") -// } -// } diff --git a/app/migrations/migration_resource_protobuf.go b/app/migrations/migration_resource_protobuf.go deleted file mode 100644 index 6303543bd..000000000 --- a/app/migrations/migration_resource_protobuf.go +++ /dev/null @@ -1,80 +0,0 @@ -package migrations - -// import ( -// "encoding/hex" -// "strings" - -// "github.com/cheqd/cheqd-node/app/migrations/helpers" -// didutils "github.com/cheqd/cheqd-node/x/did/utils" -// resourcetypes "github.com/cheqd/cheqd-node/x/resource/types" -// resourcetypesv1 "github.com/cheqd/cheqd-node/x/resource/types/v1" -// sdk "github.com/cosmos/cosmos-sdk/types" -// ) - -// func MigrateResourceProtobuf(sctx sdk.Context, mctx MigrationContext) error { -// sctx.Logger().Debug("MigrateResourceProtobuf: Starting migration") -// // Storage for old headers and data -// store := sctx.KVStore(mctx.resourceStoreKey) - -// sctx.Logger().Debug("MigrateResourceProtobuf: Resetting counter") -// // Reset counter -// mctx.resourceKeeperNew.SetResourceCount(&sctx, 0) - -// sctx.Logger().Debug("MigrateResourceProtobuf: Reading all keys") -// headerKeys := helpers.ReadAllKeys(store, didutils.StrBytes(resourcetypesv1.ResourceHeaderKey)) - -// for _, headerKey := range headerKeys { -// sctx.Logger().Debug("MigrateResourceProtobuf: Starting migration for resource with header key: " + string(headerKey)) -// dataKey := ResourceV1HeaderkeyToDataKey(headerKey) - -// var oldHeader resourcetypesv1.ResourceHeader -// mctx.codec.MustUnmarshal(store.Get(headerKey), &oldHeader) -// oldData := store.Get(dataKey) - -// sctx.Logger().Debug("MigrateResourceProtobuf: Collecting new resource metadata") -// newMetadata := resourcetypes.Metadata{ -// CollectionId: oldHeader.CollectionId, -// Id: oldHeader.Id, -// Name: oldHeader.Name, -// Version: "", -// ResourceType: oldHeader.ResourceType, -// AlsoKnownAs: []*resourcetypes.AlternativeUri{}, -// MediaType: oldHeader.MediaType, -// Created: helpers.MustParseFromStringTimeToGoTime(oldHeader.Created), -// Checksum: hex.EncodeToString(oldHeader.Checksum), -// PreviousVersionId: oldHeader.PreviousVersionId, -// NextVersionId: oldHeader.NextVersionId, -// } - -// resourceWithMetadata := resourcetypes.ResourceWithMetadata{ -// Metadata: &newMetadata, -// Resource: &resourcetypes.Resource{ -// Data: oldData, -// }, -// } - -// sctx.Logger().Debug("MigrateResourceProtobuf: Remove old values") -// // Remove old resource data and header -// store.Delete(headerKey) -// store.Delete(dataKey) - -// sctx.Logger().Debug("MigrateResourceProtobuf: Write new resource with metadata") -// // Write new resource -// err := mctx.resourceKeeperNew.SetResource(&sctx, &resourceWithMetadata) -// if err != nil { -// return err -// } -// sctx.Logger().Debug("MigrateResourceProtobuf: Migration finished for resource with header key: " + string(headerKey)) -// } -// sctx.Logger().Debug("MigrateResourceProtobuf: Migration finished") -// return nil -// } - -// func ResourceV1HeaderkeyToDataKey(headerKey []byte) []byte { -// return []byte( -// strings.Replace( -// string(headerKey), -// string(resourcetypesv1.ResourceHeaderKey), -// string(resourcetypesv1.ResourceDataKey), -// 1)) -// } diff --git a/app/test_helpers.go b/app/test_helpers.go index 7e436bef8..7c5f028c2 100644 --- a/app/test_helpers.go +++ b/app/test_helpers.go @@ -10,11 +10,6 @@ import ( "strconv" "time" - resourcetypes "github.com/cheqd/cheqd-node/x/resource/types" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" - cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - dbm "github.com/cometbft/cometbft-db" abci "github.com/cometbft/cometbft/abci/types" tmjson "github.com/cometbft/cometbft/libs/json" @@ -189,81 +184,81 @@ func SetupTest(isCheckTx bool) (*TestApp, error) { return app, nil } -func genesisStateWithValSet( - app *TestApp, genesisState GenesisState, - valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, - balances ...banktypes.Balance, -) (GenesisState, error) { - // set genesis accounts - authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs) - genesisState[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(authGenesis) - - validators := make([]stakingtypes.Validator, 0, len(valSet.Validators)) - delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators)) - - bondAmt := sdk.DefaultPowerReduction - - for _, val := range valSet.Validators { - pk, err := cryptocodec.FromTmPubKeyInterface(val.PubKey) - if err != nil { - return nil, err - } - pkAny, err := codectypes.NewAnyWithValue(pk) - if err != nil { - return nil, err - } - validator := stakingtypes.Validator{ - OperatorAddress: sdk.ValAddress(val.Address).String(), - ConsensusPubkey: pkAny, - Jailed: false, - Status: stakingtypes.Bonded, - Tokens: bondAmt, - DelegatorShares: sdk.OneDec(), - Description: stakingtypes.Description{}, - UnbondingHeight: int64(0), - UnbondingTime: time.Unix(0, 0).UTC(), - Commission: stakingtypes.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), - MinSelfDelegation: sdk.ZeroInt(), - } - validators = append(validators, validator) - delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec())) - - } - // set validators and delegations - stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations) - genesisState[stakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(stakingGenesis) - - totalSupply := sdk.NewCoins() - for _, b := range balances { - // add genesis acc tokens to total supply - totalSupply = totalSupply.Add(b.Coins...) - } - - for range delegations { - // add delegated tokens to total supply - totalSupply = totalSupply.Add(sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)) - } - - // add bonded amount to bonded pool module account - balances = append(balances, banktypes.Balance{ - Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(), - Coins: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)}, - }) - - // update total supply - bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{}, []banktypes.SendEnabled{}) - genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis) - - // set did module genesis state - didGenesis := didtypes.DefaultGenesis() - genesisState[didtypes.ModuleName] = app.AppCodec().MustMarshalJSON(didGenesis) - - // set resource module genesis state - resourceGenesis := resourcetypes.DefaultGenesis() - genesisState[resourcetypes.ModuleName] = app.AppCodec().MustMarshalJSON(resourceGenesis) - - return genesisState, nil -} +// func genesisStateWithValSet( +// app *TestApp, genesisState GenesisState, +// valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, +// balances ...banktypes.Balance, +// ) (GenesisState, error) { +// // set genesis accounts +// authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs) +// genesisState[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(authGenesis) + +// validators := make([]stakingtypes.Validator, 0, len(valSet.Validators)) +// delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators)) + +// bondAmt := sdk.DefaultPowerReduction + +// for _, val := range valSet.Validators { +// pk, err := cryptocodec.FromTmPubKeyInterface(val.PubKey) +// if err != nil { +// return nil, err +// } +// pkAny, err := codectypes.NewAnyWithValue(pk) +// if err != nil { +// return nil, err +// } +// validator := stakingtypes.Validator{ +// OperatorAddress: sdk.ValAddress(val.Address).String(), +// ConsensusPubkey: pkAny, +// Jailed: false, +// Status: stakingtypes.Bonded, +// Tokens: bondAmt, +// DelegatorShares: sdk.OneDec(), +// Description: stakingtypes.Description{}, +// UnbondingHeight: int64(0), +// UnbondingTime: time.Unix(0, 0).UTC(), +// Commission: stakingtypes.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), +// MinSelfDelegation: sdk.ZeroInt(), +// } +// validators = append(validators, validator) +// delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec())) + +// } +// // set validators and delegations +// stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations) +// genesisState[stakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(stakingGenesis) + +// totalSupply := sdk.NewCoins() +// for _, b := range balances { +// // add genesis acc tokens to total supply +// totalSupply = totalSupply.Add(b.Coins...) +// } + +// for range delegations { +// // add delegated tokens to total supply +// totalSupply = totalSupply.Add(sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)) +// } + +// // add bonded amount to bonded pool module account +// balances = append(balances, banktypes.Balance{ +// Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(), +// Coins: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)}, +// }) + +// // update total supply +// bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{}, []banktypes.SendEnabled{}) +// genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis) + +// // set did module genesis state +// didGenesis := didtypes.DefaultGenesis() +// genesisState[didtypes.ModuleName] = app.AppCodec().MustMarshalJSON(didGenesis) + +// // set resource module genesis state +// resourceGenesis := resourcetypes.DefaultGenesis() +// genesisState[resourcetypes.ModuleName] = app.AppCodec().MustMarshalJSON(resourceGenesis) + +// return genesisState, nil +// } // SetupWithGenesisValSet initializes a new SimApp with a validator set and genesis accounts // that also act as delegators. For simplicity, each validator is bonded with a delegation @@ -271,7 +266,7 @@ func genesisStateWithValSet( // account. A Nop logger is set in SimApp. func SetupWithGenesisValSet(valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) (*TestApp, error) { app, genesisState := setup(true, 5) - genesisState, err := genesisStateWithValSet(app, genesisState, valSet, genAccs, balances...) + genesisState, err := simtestutil.GenesisStateWithValSet(app.AppCodec(), genesisState, valSet, genAccs, balances...) if err != nil { return nil, err }