From fddadd3bf6b812e4deebafa34bd9fd0e4a43090e Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Thu, 12 Sep 2024 05:17:49 +0700 Subject: [PATCH 01/35] move use sdk types genesis validator --- server/types/app.go | 4 ++-- simapp/export.go | 19 +--------------- types/genesis.go | 10 --------- types/staking.go | 8 +++++++ x/genutil/types/genesis.go | 45 +++++++++++++++++++++++++++++++++----- x/genutil/utils.go | 4 ++-- x/staking/genesis.go | 16 ++------------ 7 files changed, 54 insertions(+), 52 deletions(-) delete mode 100644 types/genesis.go diff --git a/server/types/app.go b/server/types/app.go index 798a4475dbbe..dbdcf1e51a38 100644 --- a/server/types/app.go +++ b/server/types/app.go @@ -5,7 +5,6 @@ import ( "io" cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1" - cmttypes "github.com/cometbft/cometbft/types" "github.com/cosmos/gogoproto/grpc" corestore "cosmossdk.io/core/store" @@ -16,6 +15,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/server/api" "github.com/cosmos/cosmos-sdk/server/config" + sdk "github.com/cosmos/cosmos-sdk/types" ) type ( @@ -73,7 +73,7 @@ type ( // AppState is the application state as JSON. AppState json.RawMessage // Validators is the exported validator set. - Validators []cmttypes.GenesisValidator + Validators []sdk.GenesisValidator // Height is the app's latest block height. Height int64 // ConsensusParams are the exported consensus params for ABCI. diff --git a/simapp/export.go b/simapp/export.go index 05346741b99e..28d15f507a9b 100644 --- a/simapp/export.go +++ b/simapp/export.go @@ -5,7 +5,6 @@ import ( "fmt" cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1" - cmttypes "github.com/cometbft/cometbft/types" "cosmossdk.io/collections" storetypes "cosmossdk.io/store/types" @@ -13,7 +12,6 @@ import ( "cosmossdk.io/x/staking" stakingtypes "cosmossdk.io/x/staking/types" - cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" servertypes "github.com/cosmos/cosmos-sdk/server/types" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -43,25 +41,10 @@ func (app *SimApp) ExportAppStateAndValidators(forZeroHeight bool, jailAllowedAd } validators, err := staking.WriteValidators(ctx, app.StakingKeeper) - cmtValidators := []cmttypes.GenesisValidator{} - for _, val := range validators { - cmtPk, err := cryptocodec.ToCmtPubKeyInterface(val.PubKey) - if err != nil { - return servertypes.ExportedApp{}, err - } - cmtVal := cmttypes.GenesisValidator{ - Address: val.Address.Bytes(), - PubKey: cmtPk, - Power: val.Power, - Name: val.Name, - } - - cmtValidators = append(cmtValidators, cmtVal) - } return servertypes.ExportedApp{ AppState: appState, - Validators: cmtValidators, + Validators: validators, Height: height, ConsensusParams: app.BaseApp.GetConsensusParams(ctx), }, err diff --git a/types/genesis.go b/types/genesis.go deleted file mode 100644 index f250ac600014..000000000000 --- a/types/genesis.go +++ /dev/null @@ -1,10 +0,0 @@ -package types - -import cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - -type GenesisValidator struct { - Address ConsAddress - PubKey cryptotypes.PubKey - Power int64 - Name string -} diff --git a/types/staking.go b/types/staking.go index f8cdb325038c..17be5d5f107c 100644 --- a/types/staking.go +++ b/types/staking.go @@ -98,3 +98,11 @@ type ValidatorI interface { SharesFromTokens(amt math.Int) (math.LegacyDec, error) // shares worth of delegator's bond SharesFromTokensTruncated(amt math.Int) (math.LegacyDec, error) // truncated shares worth of delegator's bond } + +// GenesisValidator is an initial validator. +type GenesisValidator struct { + Address ConsAddress + PubKey cryptotypes.PubKey + Power int64 + Name string +} diff --git a/x/genutil/types/genesis.go b/x/genutil/types/genesis.go index 1566c0affaac..526f67c0c9dc 100644 --- a/x/genutil/types/genesis.go +++ b/x/genutil/types/genesis.go @@ -14,6 +14,8 @@ import ( cmtjson "github.com/cometbft/cometbft/libs/json" cmttypes "github.com/cometbft/cometbft/types" + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/version" ) @@ -118,6 +120,22 @@ func AppGenesisFromReader(reader io.Reader) (*AppGenesis, error) { return nil, err } + vals := []sdk.GenesisValidator{} + for _, cmtVal := range ctmGenesis.Validators { + cmtPk, err := cryptocodec.FromCmtPubKeyInterface(cmtVal.PubKey) + if err != nil { + return nil, err + } + val := sdk.GenesisValidator{ + Address: cmtVal.Address.Bytes(), + PubKey: cmtPk, + Power: cmtVal.Power, + Name: cmtVal.Name, + } + + vals = append(vals, val) + } + ag = AppGenesis{ AppName: version.AppName, // AppVersion is not filled as we do not know it from a CometBFT genesis @@ -127,7 +145,7 @@ func AppGenesisFromReader(reader io.Reader) (*AppGenesis, error) { AppHash: ctmGenesis.AppHash, AppState: ctmGenesis.AppState, Consensus: &ConsensusGenesis{ - Validators: ctmGenesis.Validators, + Validators: vals, Params: ctmGenesis.ConsensusParams, }, } @@ -160,13 +178,28 @@ func AppGenesisFromFile(genFile string) (*AppGenesis, error) { // ToGenesisDoc converts the AppGenesis to a CometBFT GenesisDoc. func (ag *AppGenesis) ToGenesisDoc() (*cmttypes.GenesisDoc, error) { + cmtValidators := []cmttypes.GenesisValidator{} + for _, val := range ag.Consensus.Validators { + cmtPk, err := cryptocodec.ToCmtPubKeyInterface(val.PubKey) + if err != nil { + return nil, err + } + cmtVal := cmttypes.GenesisValidator{ + Address: val.Address.Bytes(), + PubKey: cmtPk, + Power: val.Power, + Name: val.Name, + } + + cmtValidators = append(cmtValidators, cmtVal) + } return &cmttypes.GenesisDoc{ GenesisTime: ag.GenesisTime, ChainID: ag.ChainID, InitialHeight: ag.InitialHeight, AppHash: ag.AppHash, AppState: ag.AppState, - Validators: ag.Consensus.Validators, + Validators: cmtValidators, ConsensusParams: ag.Consensus.Params, }, nil } @@ -174,13 +207,13 @@ func (ag *AppGenesis) ToGenesisDoc() (*cmttypes.GenesisDoc, error) { // ConsensusGenesis defines the consensus layer's genesis. // TODO(@julienrbrt) eventually abstract from CometBFT types type ConsensusGenesis struct { - Validators []cmttypes.GenesisValidator `json:"validators,omitempty"` - Params *cmttypes.ConsensusParams `json:"params,omitempty"` + Validators []sdk.GenesisValidator `json:"validators,omitempty"` + Params *cmttypes.ConsensusParams `json:"params,omitempty"` } // NewConsensusGenesis returns a ConsensusGenesis with given values. // It takes a proto consensus params so it can called from server export command. -func NewConsensusGenesis(params cmtproto.ConsensusParams, validators []cmttypes.GenesisValidator) *ConsensusGenesis { +func NewConsensusGenesis(params cmtproto.ConsensusParams, validators []sdk.GenesisValidator) *ConsensusGenesis { return &ConsensusGenesis{ Params: &cmttypes.ConsensusParams{ Block: cmttypes.BlockParams{ @@ -241,7 +274,7 @@ func (cs *ConsensusGenesis) ValidateAndComplete() error { return fmt.Errorf("incorrect address for validator %v in the genesis file, should be %v", v, v.PubKey.Address()) } if len(v.Address) == 0 { - cs.Validators[i].Address = v.PubKey.Address() + cs.Validators[i].Address = v.PubKey.Address().Bytes() } } diff --git a/x/genutil/utils.go b/x/genutil/utils.go index b20b7bfa6ec8..ee7d11e39e80 100644 --- a/x/genutil/utils.go +++ b/x/genutil/utils.go @@ -14,11 +14,11 @@ import ( tmed25519 "github.com/cometbft/cometbft/crypto/ed25519" "github.com/cometbft/cometbft/p2p" "github.com/cometbft/cometbft/privval" - cmttypes "github.com/cometbft/cometbft/types" "github.com/cosmos/go-bip39" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/genutil/types" ) @@ -35,7 +35,7 @@ func ExportGenesisFile(genesis *types.AppGenesis, genFile string) error { // ExportGenesisFileWithTime creates and writes the genesis configuration to disk. // An error is returned if building or writing the configuration to file fails. func ExportGenesisFileWithTime( - genFile, chainID string, validators []cmttypes.GenesisValidator, appState json.RawMessage, genTime time.Time, + genFile, chainID string, validators []sdk.GenesisValidator, appState json.RawMessage, genTime time.Time, ) error { appGenesis := types.NewAppGenesisWithVersion(chainID, appState) appGenesis.GenesisTime = genTime diff --git a/x/staking/genesis.go b/x/staking/genesis.go index d09ada842f44..0bf22ff33872 100644 --- a/x/staking/genesis.go +++ b/x/staking/genesis.go @@ -9,23 +9,11 @@ import ( "cosmossdk.io/x/staking/keeper" "cosmossdk.io/x/staking/types" - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" ) -// TODO: move this to sdk types and use this instead of comet types GenesisValidator -// then we can do pubkey conversion in ToGenesisDoc -// -// this is a temporary work around to avoid import comet directly in staking -type GenesisValidator struct { - Address sdk.ConsAddress - PubKey cryptotypes.PubKey - Power int64 - Name string -} - // WriteValidators returns a slice of bonded genesis validators. -func WriteValidators(ctx context.Context, keeper *keeper.Keeper) (vals []GenesisValidator, returnErr error) { +func WriteValidators(ctx context.Context, keeper *keeper.Keeper) (vals []sdk.GenesisValidator, returnErr error) { err := keeper.LastValidatorPower.Walk(ctx, nil, func(key []byte, _ gogotypes.Int64Value) (bool, error) { validator, err := keeper.GetValidator(ctx, key) if err != nil { @@ -38,7 +26,7 @@ func WriteValidators(ctx context.Context, keeper *keeper.Keeper) (vals []Genesis return true, err } - vals = append(vals, GenesisValidator{ + vals = append(vals, sdk.GenesisValidator{ Address: sdk.ConsAddress(pk.Address()), PubKey: pk, Power: validator.GetConsensusPower(keeper.PowerReduction(ctx)), From 41c5520e797c532f54ded6b4ae291bf576a71cf0 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Fri, 13 Sep 2024 09:45:21 +0700 Subject: [PATCH 02/35] debug --- types/staking.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/types/staking.go b/types/staking.go index 17be5d5f107c..406e69d8b010 100644 --- a/types/staking.go +++ b/types/staking.go @@ -101,8 +101,8 @@ type ValidatorI interface { // GenesisValidator is an initial validator. type GenesisValidator struct { - Address ConsAddress - PubKey cryptotypes.PubKey - Power int64 - Name string + Address ConsAddress `json:"address"` + PubKey cryptotypes.PubKey `json:"pub_key"` + Power int64 `json:"power"` + Name string `json:"name"` } From 219213ba880dee940a8a36ed22a81d7d41f55345 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Fri, 13 Sep 2024 12:18:27 +0700 Subject: [PATCH 03/35] debug --- go.mod | 2 +- types/staking.go | 2 +- x/staking/genesis.go | 2 +- x/staking/go.mod | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 888de0a6c75e..23df852a4a35 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -go 1.23 +go 1.23.0 module github.com/cosmos/cosmos-sdk diff --git a/types/staking.go b/types/staking.go index 406e69d8b010..9904544221aa 100644 --- a/types/staking.go +++ b/types/staking.go @@ -101,7 +101,7 @@ type ValidatorI interface { // GenesisValidator is an initial validator. type GenesisValidator struct { - Address ConsAddress `json:"address"` + Address ValAddress `json:"address"` PubKey cryptotypes.PubKey `json:"pub_key"` Power int64 `json:"power"` Name string `json:"name"` diff --git a/x/staking/genesis.go b/x/staking/genesis.go index 0bf22ff33872..49d954e64127 100644 --- a/x/staking/genesis.go +++ b/x/staking/genesis.go @@ -27,7 +27,7 @@ func WriteValidators(ctx context.Context, keeper *keeper.Keeper) (vals []sdk.Gen } vals = append(vals, sdk.GenesisValidator{ - Address: sdk.ConsAddress(pk.Address()), + Address: sdk.ValAddress(pk.Address()), PubKey: pk, Power: validator.GetConsensusPower(keeper.PowerReduction(ctx)), Name: validator.GetMoniker(), diff --git a/x/staking/go.mod b/x/staking/go.mod index ed19cb3b39c9..035b9e8a766c 100644 --- a/x/staking/go.mod +++ b/x/staking/go.mod @@ -1,6 +1,6 @@ module cosmossdk.io/x/staking -go 1.23 +go 1.23.0 require ( cosmossdk.io/api v0.7.5 From 794ecbcf582065b532f6748ec2b03df40c327784 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Fri, 13 Sep 2024 16:17:28 +0700 Subject: [PATCH 04/35] debug --- types/staking.go | 10 ++++++---- x/genutil/types/genesis.go | 8 ++++---- x/genutil/types/testdata/app_genesis.json | 2 +- x/staking/genesis.go | 2 +- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/types/staking.go b/types/staking.go index 9904544221aa..43ca137e454a 100644 --- a/types/staking.go +++ b/types/staking.go @@ -1,6 +1,8 @@ package types import ( + "github.com/cometbft/cometbft/crypto" + "cosmossdk.io/math" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" @@ -101,8 +103,8 @@ type ValidatorI interface { // GenesisValidator is an initial validator. type GenesisValidator struct { - Address ValAddress `json:"address"` - PubKey cryptotypes.PubKey `json:"pub_key"` - Power int64 `json:"power"` - Name string `json:"name"` + Address ConsAddress `json:"address"` + PubKey crypto.PubKey `json:"pub_key"` + Power int64 `json:"power"` + Name string `json:"name"` } diff --git a/x/genutil/types/genesis.go b/x/genutil/types/genesis.go index 526f67c0c9dc..490c7364cadb 100644 --- a/x/genutil/types/genesis.go +++ b/x/genutil/types/genesis.go @@ -181,12 +181,12 @@ func (ag *AppGenesis) ToGenesisDoc() (*cmttypes.GenesisDoc, error) { cmtValidators := []cmttypes.GenesisValidator{} for _, val := range ag.Consensus.Validators { cmtPk, err := cryptocodec.ToCmtPubKeyInterface(val.PubKey) - if err != nil { - return nil, err - } + // if err != nil { + // return nil, err + // } cmtVal := cmttypes.GenesisValidator{ Address: val.Address.Bytes(), - PubKey: cmtPk, + PubKey: val.PubKey, Power: val.Power, Name: val.Name, } diff --git a/x/genutil/types/testdata/app_genesis.json b/x/genutil/types/testdata/app_genesis.json index 57ca37536b6d..20bca1589f76 100644 --- a/x/genutil/types/testdata/app_genesis.json +++ b/x/genutil/types/testdata/app_genesis.json @@ -1 +1 @@ -{"app_name":"\u003cappd\u003e","app_version":"","genesis_time":"2023-02-20T11:08:30.588307671Z","chain_id":"demo","initial_height":48,"app_hash":"","app_state":{"auth":{"accounts":[{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"1","address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"8","address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"4","address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","pub_key":null,"sequence":"0"},"name":"bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"5","address":"cosmos1tygms3xhhs3yv487phx3dw4a95jn7t7lpm470r","pub_key":null,"sequence":"0"},"name":"not_bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"6","address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","pub_key":null,"sequence":"0"},"name":"gov","permissions":["burner"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"3","address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","pub_key":null,"sequence":"0"},"name":"distribution","permissions":[]},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"0","address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","pub_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"A3uyZdXedyvYx9VCL6xRjkxtcFpgxjhXFIz9b2mWz+aV"},"sequence":"4"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"7","address":"cosmos1m3h30wlvsf8llruxtpukdvsy0km2kum8g38c8q","pub_key":null,"sequence":"0"},"name":"mint","permissions":["minter"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"2","address":"cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta","pub_key":null,"sequence":"0"},"name":"fee_collector","permissions":[]}],"params":{"max_memo_characters":"256","sig_verify_cost_ed25519":"590","sig_verify_cost_secp256k1":"1000","tx_sig_limit":"7","tx_size_cost_per_byte":"10"}},"authz":{"authorization":[]},"bank":{"balances":[{"address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","coins":[{"amount":"5000000000","denom":"stake"}]},{"address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","coins":[{"amount":"1000","denom":"stake"}]},{"address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","coins":[{"amount":"1000000","denom":"stake"}]},{"address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","coins":[{"amount":"10010000","denom":"stake"}]},{"address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","coins":[{"amount":"9635","denom":"stake"}]},{"address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","coins":[{"amount":"4988989000","denom":"stake"}]}],"denom_metadata":[],"params":{"default_send_enabled":true,"send_enabled":[]},"send_enabled":[],"supply":[{"amount":"10000009635","denom":"stake"}]},"consensus":null,"crisis":{"constant_fee":{"amount":"1000","denom":"stake"}},"distribution":{"delegator_starting_infos":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","starting_info":{"height":"0","previous_period":"1","stake":"1000000.000000000000000000"},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"delegator_withdraw_infos":[],"fee_pool":{"community_pool":[{"amount":"192.700000000000000000","denom":"stake"}]},"outstanding_rewards":[{"outstanding_rewards":[{"amount":"9442.300000000000000000","denom":"stake"}],"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"params":{"base_proposer_reward":"0.000000000000000000","bonus_proposer_reward":"0.000000000000000000","community_tax":"0.020000000000000000","withdraw_addr_enabled":true},"previous_proposer":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_accumulated_commissions":[{"accumulated":{"commission":[{"amount":"944.230000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_current_rewards":[{"rewards":{"period":"2","rewards":[{"amount":"8498.070000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_historical_rewards":[{"period":"1","rewards":{"cumulative_reward_ratio":[],"reference_count":2},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_slash_events":[]},"evidence":{"evidence":[]},"feegrant":{"allowances":[]},"genutil":{"gen_txs":[]},"gov":{"deposit_params":null,"deposits":[{"amount":[{"amount":"10010000","denom":"stake"}],"depositor":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","proposal_id":"1"}],"params":{"expedited_min_deposit":[{"amount":"50000000","denom":"stake"}],"expedited_threshold":"0.667000000000000000","expedited_voting_period":"86400s","max_deposit_period":"172800s","min_deposit":[{"amount":"10000000","denom":"stake"}],"min_initial_deposit_ratio":"0.000000000000000000","proposal_cancel_dest":"","proposal_cancel_ratio":"0.500000000000000000","quorum":"0.334000000000000000","threshold":"0.500000000000000000","veto_threshold":"0.334000000000000000","voting_period":"172800s"},"proposals":[{"deposit_end_time":"2023-02-22T11:11:52.776167376Z","expedited":false,"final_tally_result":{"abstain_count":"0","no_count":"0","no_with_veto_count":"0","yes_count":"0"},"id":"1","messages":[{"@type":"/cosmos.distribution.v1beta1.MsgCommunityPoolSpend","amount":[],"authority":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","recipient":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8"}],"metadata":"ipfs://CID","proposer":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","status":"PROPOSAL_STATUS_VOTING_PERIOD","submit_time":"2023-02-20T11:11:52.776167376Z","summary":"test proposal","title":"test proposal","total_deposit":[{"amount":"10010000","denom":"stake"}],"voting_end_time":"2023-02-22T11:12:07.801161984Z","voting_start_time":"2023-02-20T11:12:07.801161984Z"}],"starting_proposal_id":"2","tally_params":null,"votes":[],"voting_params":null},"group":{"group_members":[],"group_policies":[],"group_policy_seq":"0","group_seq":"0","groups":[],"proposal_seq":"0","proposals":[],"votes":[]},"mint":{"minter":{"annual_provisions":"1300010905.175073197786747950","inflation":"0.130000967926594565"},"params":{"blocks_per_year":"6311520","goal_bonded":"0.670000000000000000","inflation_max":"0.200000000000000000","inflation_min":"0.070000000000000000","inflation_rate_change":"0.130000000000000000","mint_denom":"stake"}},"nft":{"classes":[],"entries":[]},"params":null,"slashing":{"missed_blocks":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","missed_blocks":[]}],"params":{"downtime_jail_duration":"600s","min_signed_per_window":"0.500000000000000000","signed_blocks_window":"100","slash_fraction_double_sign":"0.050000000000000000","slash_fraction_downtime":"0.010000000000000000"},"signing_infos":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_signing_info":{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","index_offset":"46","jailed_until":"1970-01-01T00:00:00Z","missed_blocks_counter":"0","start_height":"0","tombstoned":false}}]},"staking":{"delegations":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","shares":"1000000.000000000000000000","validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"exported":true,"last_total_power":"1","last_validator_powers":[{"address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","power":"1"}],"params":{"bond_denom":"stake","historical_entries":10000,"max_entries":7,"max_validators":100,"min_commission_rate":"0.000000000000000000","unbonding_time":"1814400s"},"redelegations":[],"unbonding_delegations":[],"validators":[{"commission":{"commission_rates":{"max_change_rate":"0.010000000000000000","max_rate":"0.200000000000000000","rate":"0.100000000000000000"},"update_time":"2023-02-20T11:08:30.588307671Z"},"consensus_pubkey":{"@type":"/cosmos.crypto.ed25519.PubKey","key":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="},"delegator_shares":"1000000.000000000000000000","description":{"details":"","identity":"","moniker":"test","security_contact":"","website":""},"jailed":false,"min_self_delegation":"1","operator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","status":"BOND_STATUS_BONDED","tokens":"1000000","unbonding_height":"0","unbonding_ids":[],"unbonding_on_hold_ref_count":"0","unbonding_time":"1970-01-01T00:00:00Z"}]},"upgrade":{},"vesting":{}},"consensus":{"validators":[{"address":"D336F99AA5CF77503CDD8366E68A0DFE89B4124B","pub_key":{"type":"tendermint/PubKeyEd25519","value":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="},"power":"1","name":"test"}],"params":{"block":{"max_bytes":"22020096","max_gas":"-1"},"evidence":{"max_age_num_blocks":"100000","max_age_duration":"172800000000000","max_bytes":"1048576"},"validator":{"pub_key_types":["ed25519"]},"version":{"app":"0"},"synchrony":{"precision":"0","message_delay":"0"},"feature":{"vote_extensions_enable_height":"0","pbts_enable_height":"0"}}}} \ No newline at end of file +{"app_name":"\u003cappd\u003e","app_version":"","genesis_time":"2023-02-20T11:08:30.588307671Z","chain_id":"demo","initial_height":48,"app_hash":"","app_state":{"auth":{"accounts":[{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"1","address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"8","address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"4","address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","pub_key":null,"sequence":"0"},"name":"bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"5","address":"cosmos1tygms3xhhs3yv487phx3dw4a95jn7t7lpm470r","pub_key":null,"sequence":"0"},"name":"not_bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"6","address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","pub_key":null,"sequence":"0"},"name":"gov","permissions":["burner"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"3","address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","pub_key":null,"sequence":"0"},"name":"distribution","permissions":[]},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"0","address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","pub_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"A3uyZdXedyvYx9VCL6xRjkxtcFpgxjhXFIz9b2mWz+aV"},"sequence":"4"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"7","address":"cosmos1m3h30wlvsf8llruxtpukdvsy0km2kum8g38c8q","pub_key":null,"sequence":"0"},"name":"mint","permissions":["minter"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"2","address":"cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta","pub_key":null,"sequence":"0"},"name":"fee_collector","permissions":[]}],"params":{"max_memo_characters":"256","sig_verify_cost_ed25519":"590","sig_verify_cost_secp256k1":"1000","tx_sig_limit":"7","tx_size_cost_per_byte":"10"}},"authz":{"authorization":[]},"bank":{"balances":[{"address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","coins":[{"amount":"5000000000","denom":"stake"}]},{"address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","coins":[{"amount":"1000","denom":"stake"}]},{"address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","coins":[{"amount":"1000000","denom":"stake"}]},{"address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","coins":[{"amount":"10010000","denom":"stake"}]},{"address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","coins":[{"amount":"9635","denom":"stake"}]},{"address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","coins":[{"amount":"4988989000","denom":"stake"}]}],"denom_metadata":[],"params":{"default_send_enabled":true,"send_enabled":[]},"send_enabled":[],"supply":[{"amount":"10000009635","denom":"stake"}]},"consensus":null,"crisis":{"constant_fee":{"amount":"1000","denom":"stake"}},"distribution":{"delegator_starting_infos":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","starting_info":{"height":"0","previous_period":"1","stake":"1000000.000000000000000000"},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"delegator_withdraw_infos":[],"fee_pool":{"community_pool":[{"amount":"192.700000000000000000","denom":"stake"}]},"outstanding_rewards":[{"outstanding_rewards":[{"amount":"9442.300000000000000000","denom":"stake"}],"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"params":{"base_proposer_reward":"0.000000000000000000","bonus_proposer_reward":"0.000000000000000000","community_tax":"0.020000000000000000","withdraw_addr_enabled":true},"previous_proposer":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_accumulated_commissions":[{"accumulated":{"commission":[{"amount":"944.230000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_current_rewards":[{"rewards":{"period":"2","rewards":[{"amount":"8498.070000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_historical_rewards":[{"period":"1","rewards":{"cumulative_reward_ratio":[],"reference_count":2},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_slash_events":[]},"evidence":{"evidence":[]},"feegrant":{"allowances":[]},"genutil":{"gen_txs":[]},"gov":{"deposit_params":null,"deposits":[{"amount":[{"amount":"10010000","denom":"stake"}],"depositor":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","proposal_id":"1"}],"params":{"expedited_min_deposit":[{"amount":"50000000","denom":"stake"}],"expedited_threshold":"0.667000000000000000","expedited_voting_period":"86400s","max_deposit_period":"172800s","min_deposit":[{"amount":"10000000","denom":"stake"}],"min_initial_deposit_ratio":"0.000000000000000000","proposal_cancel_dest":"","proposal_cancel_ratio":"0.500000000000000000","quorum":"0.334000000000000000","threshold":"0.500000000000000000","veto_threshold":"0.334000000000000000","voting_period":"172800s"},"proposals":[{"deposit_end_time":"2023-02-22T11:11:52.776167376Z","expedited":false,"final_tally_result":{"abstain_count":"0","no_count":"0","no_with_veto_count":"0","yes_count":"0"},"id":"1","messages":[{"@type":"/cosmos.distribution.v1beta1.MsgCommunityPoolSpend","amount":[],"authority":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","recipient":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8"}],"metadata":"ipfs://CID","proposer":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","status":"PROPOSAL_STATUS_VOTING_PERIOD","submit_time":"2023-02-20T11:11:52.776167376Z","summary":"test proposal","title":"test proposal","total_deposit":[{"amount":"10010000","denom":"stake"}],"voting_end_time":"2023-02-22T11:12:07.801161984Z","voting_start_time":"2023-02-20T11:12:07.801161984Z"}],"starting_proposal_id":"2","tally_params":null,"votes":[],"voting_params":null},"group":{"group_members":[],"group_policies":[],"group_policy_seq":"0","group_seq":"0","groups":[],"proposal_seq":"0","proposals":[],"votes":[]},"mint":{"minter":{"annual_provisions":"1300010905.175073197786747950","inflation":"0.130000967926594565"},"params":{"blocks_per_year":"6311520","goal_bonded":"0.670000000000000000","inflation_max":"0.200000000000000000","inflation_min":"0.070000000000000000","inflation_rate_change":"0.130000000000000000","mint_denom":"stake"}},"nft":{"classes":[],"entries":[]},"params":null,"slashing":{"missed_blocks":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","missed_blocks":[]}],"params":{"downtime_jail_duration":"600s","min_signed_per_window":"0.500000000000000000","signed_blocks_window":"100","slash_fraction_double_sign":"0.050000000000000000","slash_fraction_downtime":"0.010000000000000000"},"signing_infos":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_signing_info":{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","index_offset":"46","jailed_until":"1970-01-01T00:00:00Z","missed_blocks_counter":"0","start_height":"0","tombstoned":false}}]},"staking":{"delegations":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","shares":"1000000.000000000000000000","validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"exported":true,"last_total_power":"1","last_validator_powers":[{"address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","power":"1"}],"params":{"bond_denom":"stake","historical_entries":10000,"max_entries":7,"max_validators":100,"min_commission_rate":"0.000000000000000000","unbonding_time":"1814400s"},"redelegations":[],"unbonding_delegations":[],"validators":[{"commission":{"commission_rates":{"max_change_rate":"0.010000000000000000","max_rate":"0.200000000000000000","rate":"0.100000000000000000"},"update_time":"2023-02-20T11:08:30.588307671Z"},"consensus_pubkey":{"@type":"/cosmos.crypto.ed25519.PubKey","key":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="},"delegator_shares":"1000000.000000000000000000","description":{"details":"","identity":"","moniker":"test","security_contact":"","website":""},"jailed":false,"min_self_delegation":"1","operator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","status":"BOND_STATUS_BONDED","tokens":"1000000","unbonding_height":"0","unbonding_ids":[],"unbonding_on_hold_ref_count":"0","unbonding_time":"1970-01-01T00:00:00Z"}]},"upgrade":{},"vesting":{}},"consensus":{"validators":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","pub_key":{"type":"tendermint/PubKeyEd25519","value":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="},"power":"1","name":"test"}],"params":{"block":{"max_bytes":"22020096","max_gas":"-1"},"evidence":{"max_age_num_blocks":"100000","max_age_duration":"172800000000000","max_bytes":"1048576"},"validator":{"pub_key_types":["ed25519"]},"version":{"app":"0"},"synchrony":{"precision":"0","message_delay":"0"},"feature":{"vote_extensions_enable_height":"0","pbts_enable_height":"0"}}}} \ No newline at end of file diff --git a/x/staking/genesis.go b/x/staking/genesis.go index 49d954e64127..ab6f64614988 100644 --- a/x/staking/genesis.go +++ b/x/staking/genesis.go @@ -27,7 +27,7 @@ func WriteValidators(ctx context.Context, keeper *keeper.Keeper) (vals []sdk.Gen } vals = append(vals, sdk.GenesisValidator{ - Address: sdk.ValAddress(pk.Address()), + Address: sdk.ConsAddress(pk.Address()).Bytes(), PubKey: pk, Power: validator.GetConsensusPower(keeper.PowerReduction(ctx)), Name: validator.GetMoniker(), From ed34222c0fcc35a837ee652ee5e935f015a152a4 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Tue, 17 Sep 2024 13:34:01 +0700 Subject: [PATCH 05/35] debug --- crypto/codec/cmt.go | 35 +++++++++++++++++++++++++++++++++++ crypto/types/types.go | 8 ++++++++ testutil/network/util.go | 1 + types/staking.go | 10 ++++------ x/genutil/types/genesis.go | 6 +++++- 5 files changed, 53 insertions(+), 7 deletions(-) diff --git a/crypto/codec/cmt.go b/crypto/codec/cmt.go index 9617f94bf1a3..2023e2e43940 100644 --- a/crypto/codec/cmt.go +++ b/crypto/codec/cmt.go @@ -60,6 +60,31 @@ func ToCmtProtoPublicKey(pk cryptotypes.PubKey) (cmtprotocrypto.PublicKey, error } } +func ToCmtProtoPublicKeyFromJsonCompatPubkey(pk cryptotypes.JSONCompatPubKey) (cmtprotocrypto.PublicKey, error) { + switch pk := pk.(type) { + case *ed25519.PubKey: + return cmtprotocrypto.PublicKey{ + Sum: &cmtprotocrypto.PublicKey_Ed25519{ + Ed25519: pk.Key, + }, + }, nil + case *secp256k1.PubKey: + return cmtprotocrypto.PublicKey{ + Sum: &cmtprotocrypto.PublicKey_Secp256K1{ + Secp256K1: pk.Key, + }, + }, nil + case *bls12_381.PubKey: + return cmtprotocrypto.PublicKey{ + Sum: &cmtprotocrypto.PublicKey_Bls12381{ + Bls12381: pk.Key, + }, + }, nil + default: + return cmtprotocrypto.PublicKey{}, errors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v to Tendermint public key", pk) + } +} + // FromCmtPubKeyInterface converts CMT's cmtcrypto.PubKey to our own PubKey. func FromCmtPubKeyInterface(tmPk cmtcrypto.PubKey) (cryptotypes.PubKey, error) { tmProtoPk, err := encoding.PubKeyToProto(tmPk) @@ -79,3 +104,13 @@ func ToCmtPubKeyInterface(pk cryptotypes.PubKey) (cmtcrypto.PubKey, error) { return encoding.PubKeyFromProto(tmProtoPk) } + +// ToCmtPubKeyInterface converts our own PubKey to CMT's cmtcrypto.PubKey. +func ToCmtPubKeyInterfaceFromJsonCompatPubkey(pk cryptotypes.JSONCompatPubKey) (cmtcrypto.PubKey, error) { + tmProtoPk, err := ToCmtProtoPublicKeyFromJsonCompatPubkey(pk) + if err != nil { + return nil, err + } + + return encoding.PubKeyFromProto(tmProtoPk) +} diff --git a/crypto/types/types.go b/crypto/types/types.go index 7f01b6078cf3..84ec0400d783 100644 --- a/crypto/types/types.go +++ b/crypto/types/types.go @@ -5,6 +5,14 @@ import ( "github.com/cosmos/gogoproto/proto" ) +// JSONCompatPubKey defines a public key that are json compatible. +type JSONCompatPubKey interface { + Address() Address + Bytes() []byte + VerifySignature(msg []byte, sig []byte) bool + Type() string +} + // PubKey defines a public key and extends proto.Message. type PubKey interface { proto.Message diff --git a/testutil/network/util.go b/testutil/network/util.go index 08ee1b0c5d99..20aaca6d2b7a 100644 --- a/testutil/network/util.go +++ b/testutil/network/util.go @@ -59,6 +59,7 @@ func startInProcess(cfg Config, val *Validator) error { }, err } gen, err := appGenesis.ToGenesisDoc() + fmt.Println("is", gen.Validators == nil) if err != nil { return node.ChecksummedGenesisDoc{ Sha256Checksum: []byte{}, diff --git a/types/staking.go b/types/staking.go index 43ca137e454a..7e9e7ae3d757 100644 --- a/types/staking.go +++ b/types/staking.go @@ -1,8 +1,6 @@ package types import ( - "github.com/cometbft/cometbft/crypto" - "cosmossdk.io/math" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" @@ -103,8 +101,8 @@ type ValidatorI interface { // GenesisValidator is an initial validator. type GenesisValidator struct { - Address ConsAddress `json:"address"` - PubKey crypto.PubKey `json:"pub_key"` - Power int64 `json:"power"` - Name string `json:"name"` + Address ConsAddress `json:"address"` + PubKey cryptotypes.JSONCompatPubKey `json:"pub_key"` + Power int64 `json:"power"` + Name string `json:"name"` } diff --git a/x/genutil/types/genesis.go b/x/genutil/types/genesis.go index 490c7364cadb..ad6e70699ddb 100644 --- a/x/genutil/types/genesis.go +++ b/x/genutil/types/genesis.go @@ -136,6 +136,10 @@ func AppGenesisFromReader(reader io.Reader) (*AppGenesis, error) { vals = append(vals, val) } + if len(vals) == 0 { + vals = nil + } + ag = AppGenesis{ AppName: version.AppName, // AppVersion is not filled as we do not know it from a CometBFT genesis @@ -180,7 +184,7 @@ func AppGenesisFromFile(genFile string) (*AppGenesis, error) { func (ag *AppGenesis) ToGenesisDoc() (*cmttypes.GenesisDoc, error) { cmtValidators := []cmttypes.GenesisValidator{} for _, val := range ag.Consensus.Validators { - cmtPk, err := cryptocodec.ToCmtPubKeyInterface(val.PubKey) + // cmtPk, err := cryptocodec.ToCmtPubKeyInterfaceFromJsonCompatPubkey(val.PubKey) // if err != nil { // return nil, err // } From 17a42a3753f88e5385c9c6693398e84271354e0d Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Tue, 17 Sep 2024 14:18:57 +0700 Subject: [PATCH 06/35] assert nil for empty val set --- tests/systemtests/go.mod | 2 +- x/genutil/types/genesis.go | 8 ++++---- x/genutil/types/testdata/cmt_genesis.json | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/systemtests/go.mod b/tests/systemtests/go.mod index 0ab29370379e..39c99b09611f 100644 --- a/tests/systemtests/go.mod +++ b/tests/systemtests/go.mod @@ -1,6 +1,6 @@ module cosmossdk.io/tests/systemtests -go 1.23 +go 1.23.0 require ( github.com/cosmos/cosmos-proto v1.0.0-beta.5 // indirect diff --git a/x/genutil/types/genesis.go b/x/genutil/types/genesis.go index ad6e70699ddb..ef039fdfb53b 100644 --- a/x/genutil/types/genesis.go +++ b/x/genutil/types/genesis.go @@ -184,10 +184,6 @@ func AppGenesisFromFile(genFile string) (*AppGenesis, error) { func (ag *AppGenesis) ToGenesisDoc() (*cmttypes.GenesisDoc, error) { cmtValidators := []cmttypes.GenesisValidator{} for _, val := range ag.Consensus.Validators { - // cmtPk, err := cryptocodec.ToCmtPubKeyInterfaceFromJsonCompatPubkey(val.PubKey) - // if err != nil { - // return nil, err - // } cmtVal := cmttypes.GenesisValidator{ Address: val.Address.Bytes(), PubKey: val.PubKey, @@ -197,6 +193,10 @@ func (ag *AppGenesis) ToGenesisDoc() (*cmttypes.GenesisDoc, error) { cmtValidators = append(cmtValidators, cmtVal) } + // assert nil value for empty validators set + if len(cmtValidators) == 0 { + cmtValidators = nil + } return &cmttypes.GenesisDoc{ GenesisTime: ag.GenesisTime, ChainID: ag.ChainID, diff --git a/x/genutil/types/testdata/cmt_genesis.json b/x/genutil/types/testdata/cmt_genesis.json index aae3d84d0698..882ebdfd9117 100644 --- a/x/genutil/types/testdata/cmt_genesis.json +++ b/x/genutil/types/testdata/cmt_genesis.json @@ -1 +1 @@ -{"app_hash":"","app_state":{"auth":{"accounts":[{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"1","address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"8","address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"4","address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","pub_key":null,"sequence":"0"},"name":"bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"5","address":"cosmos1tygms3xhhs3yv487phx3dw4a95jn7t7lpm470r","pub_key":null,"sequence":"0"},"name":"not_bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"6","address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","pub_key":null,"sequence":"0"},"name":"gov","permissions":["burner"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"3","address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","pub_key":null,"sequence":"0"},"name":"distribution","permissions":[]},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"0","address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","pub_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"A3uyZdXedyvYx9VCL6xRjkxtcFpgxjhXFIz9b2mWz+aV"},"sequence":"4"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"7","address":"cosmos1m3h30wlvsf8llruxtpukdvsy0km2kum8g38c8q","pub_key":null,"sequence":"0"},"name":"mint","permissions":["minter"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"2","address":"cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta","pub_key":null,"sequence":"0"},"name":"fee_collector","permissions":[]}],"params":{"max_memo_characters":"256","sig_verify_cost_ed25519":"590","sig_verify_cost_secp256k1":"1000","tx_sig_limit":"7","tx_size_cost_per_byte":"10"}},"authz":{"authorization":[]},"bank":{"balances":[{"address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","coins":[{"amount":"5000000000","denom":"stake"}]},{"address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","coins":[{"amount":"1000","denom":"stake"}]},{"address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","coins":[{"amount":"1000000","denom":"stake"}]},{"address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","coins":[{"amount":"10010000","denom":"stake"}]},{"address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","coins":[{"amount":"9635","denom":"stake"}]},{"address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","coins":[{"amount":"4988989000","denom":"stake"}]}],"denom_metadata":[],"params":{"default_send_enabled":true,"send_enabled":[]},"send_enabled":[],"supply":[{"amount":"10000009635","denom":"stake"}]},"consensus":null,"crisis":{"constant_fee":{"amount":"1000","denom":"stake"}},"distribution":{"delegator_starting_infos":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","starting_info":{"height":"0","previous_period":"1","stake":"1000000.000000000000000000"},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"delegator_withdraw_infos":[],"fee_pool":{"community_pool":[{"amount":"192.700000000000000000","denom":"stake"}]},"outstanding_rewards":[{"outstanding_rewards":[{"amount":"9442.300000000000000000","denom":"stake"}],"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"params":{"base_proposer_reward":"0.000000000000000000","bonus_proposer_reward":"0.000000000000000000","community_tax":"0.020000000000000000","withdraw_addr_enabled":true},"previous_proposer":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_accumulated_commissions":[{"accumulated":{"commission":[{"amount":"944.230000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_current_rewards":[{"rewards":{"period":"2","rewards":[{"amount":"8498.070000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_historical_rewards":[{"period":"1","rewards":{"cumulative_reward_ratio":[],"reference_count":2},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_slash_events":[]},"evidence":{"evidence":[]},"feegrant":{"allowances":[]},"genutil":{"gen_txs":[]},"gov":{"deposit_params":null,"deposits":[{"amount":[{"amount":"10010000","denom":"stake"}],"depositor":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","proposal_id":"1"}],"params":{"expedited_min_deposit":[{"amount":"50000000","denom":"stake"}],"expedited_threshold":"0.667000000000000000","expedited_voting_period":"86400s","max_deposit_period":"172800s","min_deposit":[{"amount":"10000000","denom":"stake"}],"min_initial_deposit_ratio":"0.000000000000000000","proposal_cancel_dest":"","proposal_cancel_ratio":"0.500000000000000000","quorum":"0.334000000000000000","threshold":"0.500000000000000000","veto_threshold":"0.334000000000000000","voting_period":"172800s"},"proposals":[{"deposit_end_time":"2023-02-22T11:11:52.776167376Z","expedited":false,"final_tally_result":{"abstain_count":"0","no_count":"0","no_with_veto_count":"0","yes_count":"0"},"id":"1","messages":[{"@type":"/cosmos.distribution.v1beta1.MsgCommunityPoolSpend","amount":[],"authority":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","recipient":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8"}],"metadata":"ipfs://CID","proposer":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","status":"PROPOSAL_STATUS_VOTING_PERIOD","submit_time":"2023-02-20T11:11:52.776167376Z","summary":"test proposal","title":"test proposal","total_deposit":[{"amount":"10010000","denom":"stake"}],"voting_end_time":"2023-02-22T11:12:07.801161984Z","voting_start_time":"2023-02-20T11:12:07.801161984Z"}],"starting_proposal_id":"2","tally_params":null,"votes":[],"voting_params":null},"group":{"group_members":[],"group_policies":[],"group_policy_seq":"0","group_seq":"0","groups":[],"proposal_seq":"0","proposals":[],"votes":[]},"mint":{"minter":{"annual_provisions":"1300010905.175073197786747950","inflation":"0.130000967926594565"},"params":{"blocks_per_year":"6311520","goal_bonded":"0.670000000000000000","inflation_max":"0.200000000000000000","inflation_min":"0.070000000000000000","inflation_rate_change":"0.130000000000000000","mint_denom":"stake"}},"nft":{"classes":[],"entries":[]},"params":null,"slashing":{"missed_blocks":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","missed_blocks":[]}],"params":{"downtime_jail_duration":"600s","min_signed_per_window":"0.500000000000000000","signed_blocks_window":"100","slash_fraction_double_sign":"0.050000000000000000","slash_fraction_downtime":"0.010000000000000000"},"signing_infos":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_signing_info":{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","index_offset":"46","jailed_until":"1970-01-01T00:00:00Z","missed_blocks_counter":"0","start_height":"0","tombstoned":false}}]},"staking":{"delegations":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","shares":"1000000.000000000000000000","validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"exported":true,"last_total_power":"1","last_validator_powers":[{"address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","power":"1"}],"params":{"bond_denom":"stake","historical_entries":10000,"max_entries":7,"max_validators":100,"min_commission_rate":"0.000000000000000000","unbonding_time":"1814400s"},"redelegations":[],"unbonding_delegations":[],"validators":[{"commission":{"commission_rates":{"max_change_rate":"0.010000000000000000","max_rate":"0.200000000000000000","rate":"0.100000000000000000"},"update_time":"2023-02-20T11:08:30.588307671Z"},"consensus_pubkey":{"@type":"/cosmos.crypto.ed25519.PubKey","key":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="},"delegator_shares":"1000000.000000000000000000","description":{"details":"","identity":"","moniker":"test","security_contact":"","website":""},"jailed":false,"min_self_delegation":"1","operator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","status":"BOND_STATUS_BONDED","tokens":"1000000","unbonding_height":"0","unbonding_ids":[],"unbonding_on_hold_ref_count":"0","unbonding_time":"1970-01-01T00:00:00Z"}]},"upgrade":{},"vesting":{}},"chain_id":"demo","consensus_params":{"block":{"max_bytes":"22020096","max_gas":"-1"},"evidence":{"max_age_duration":"172800000000000","max_age_num_blocks":"100000","max_bytes":"1048576"},"validator":{"pub_key_types":["ed25519"]},"version":{"app":"0"}},"genesis_time":"2023-02-20T11:08:30.588307671Z","initial_height":"48","validators":[{"address":"D336F99AA5CF77503CDD8366E68A0DFE89B4124B","name":"test","power":"1","pub_key":{"type":"tendermint/PubKeyEd25519","value":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="}}]} \ No newline at end of file +{"app_hash":"","app_state":{"auth":{"accounts":[{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"1","address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"8","address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"4","address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","pub_key":null,"sequence":"0"},"name":"bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"5","address":"cosmos1tygms3xhhs3yv487phx3dw4a95jn7t7lpm470r","pub_key":null,"sequence":"0"},"name":"not_bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"6","address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","pub_key":null,"sequence":"0"},"name":"gov","permissions":["burner"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"3","address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","pub_key":null,"sequence":"0"},"name":"distribution","permissions":[]},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"0","address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","pub_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"A3uyZdXedyvYx9VCL6xRjkxtcFpgxjhXFIz9b2mWz+aV"},"sequence":"4"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"7","address":"cosmos1m3h30wlvsf8llruxtpukdvsy0km2kum8g38c8q","pub_key":null,"sequence":"0"},"name":"mint","permissions":["minter"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"2","address":"cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta","pub_key":null,"sequence":"0"},"name":"fee_collector","permissions":[]}],"params":{"max_memo_characters":"256","sig_verify_cost_ed25519":"590","sig_verify_cost_secp256k1":"1000","tx_sig_limit":"7","tx_size_cost_per_byte":"10"}},"authz":{"authorization":[]},"bank":{"balances":[{"address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","coins":[{"amount":"5000000000","denom":"stake"}]},{"address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","coins":[{"amount":"1000","denom":"stake"}]},{"address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","coins":[{"amount":"1000000","denom":"stake"}]},{"address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","coins":[{"amount":"10010000","denom":"stake"}]},{"address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","coins":[{"amount":"9635","denom":"stake"}]},{"address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","coins":[{"amount":"4988989000","denom":"stake"}]}],"denom_metadata":[],"params":{"default_send_enabled":true,"send_enabled":[]},"send_enabled":[],"supply":[{"amount":"10000009635","denom":"stake"}]},"consensus":null,"crisis":{"constant_fee":{"amount":"1000","denom":"stake"}},"distribution":{"delegator_starting_infos":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","starting_info":{"height":"0","previous_period":"1","stake":"1000000.000000000000000000"},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"delegator_withdraw_infos":[],"fee_pool":{"community_pool":[{"amount":"192.700000000000000000","denom":"stake"}]},"outstanding_rewards":[{"outstanding_rewards":[{"amount":"9442.300000000000000000","denom":"stake"}],"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"params":{"base_proposer_reward":"0.000000000000000000","bonus_proposer_reward":"0.000000000000000000","community_tax":"0.020000000000000000","withdraw_addr_enabled":true},"previous_proposer":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_accumulated_commissions":[{"accumulated":{"commission":[{"amount":"944.230000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_current_rewards":[{"rewards":{"period":"2","rewards":[{"amount":"8498.070000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_historical_rewards":[{"period":"1","rewards":{"cumulative_reward_ratio":[],"reference_count":2},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_slash_events":[]},"evidence":{"evidence":[]},"feegrant":{"allowances":[]},"genutil":{"gen_txs":[]},"gov":{"deposit_params":null,"deposits":[{"amount":[{"amount":"10010000","denom":"stake"}],"depositor":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","proposal_id":"1"}],"params":{"expedited_min_deposit":[{"amount":"50000000","denom":"stake"}],"expedited_threshold":"0.667000000000000000","expedited_voting_period":"86400s","max_deposit_period":"172800s","min_deposit":[{"amount":"10000000","denom":"stake"}],"min_initial_deposit_ratio":"0.000000000000000000","proposal_cancel_dest":"","proposal_cancel_ratio":"0.500000000000000000","quorum":"0.334000000000000000","threshold":"0.500000000000000000","veto_threshold":"0.334000000000000000","voting_period":"172800s"},"proposals":[{"deposit_end_time":"2023-02-22T11:11:52.776167376Z","expedited":false,"final_tally_result":{"abstain_count":"0","no_count":"0","no_with_veto_count":"0","yes_count":"0"},"id":"1","messages":[{"@type":"/cosmos.distribution.v1beta1.MsgCommunityPoolSpend","amount":[],"authority":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","recipient":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8"}],"metadata":"ipfs://CID","proposer":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","status":"PROPOSAL_STATUS_VOTING_PERIOD","submit_time":"2023-02-20T11:11:52.776167376Z","summary":"test proposal","title":"test proposal","total_deposit":[{"amount":"10010000","denom":"stake"}],"voting_end_time":"2023-02-22T11:12:07.801161984Z","voting_start_time":"2023-02-20T11:12:07.801161984Z"}],"starting_proposal_id":"2","tally_params":null,"votes":[],"voting_params":null},"group":{"group_members":[],"group_policies":[],"group_policy_seq":"0","group_seq":"0","groups":[],"proposal_seq":"0","proposals":[],"votes":[]},"mint":{"minter":{"annual_provisions":"1300010905.175073197786747950","inflation":"0.130000967926594565"},"params":{"blocks_per_year":"6311520","goal_bonded":"0.670000000000000000","inflation_max":"0.200000000000000000","inflation_min":"0.070000000000000000","inflation_rate_change":"0.130000000000000000","mint_denom":"stake"}},"nft":{"classes":[],"entries":[]},"params":null,"slashing":{"missed_blocks":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","missed_blocks":[]}],"params":{"downtime_jail_duration":"600s","min_signed_per_window":"0.500000000000000000","signed_blocks_window":"100","slash_fraction_double_sign":"0.050000000000000000","slash_fraction_downtime":"0.010000000000000000"},"signing_infos":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_signing_info":{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","index_offset":"46","jailed_until":"1970-01-01T00:00:00Z","missed_blocks_counter":"0","start_height":"0","tombstoned":false}}]},"staking":{"delegations":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","shares":"1000000.000000000000000000","validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"exported":true,"last_total_power":"1","last_validator_powers":[{"address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","power":"1"}],"params":{"bond_denom":"stake","historical_entries":10000,"max_entries":7,"max_validators":100,"min_commission_rate":"0.000000000000000000","unbonding_time":"1814400s"},"redelegations":[],"unbonding_delegations":[],"validators":[{"commission":{"commission_rates":{"max_change_rate":"0.010000000000000000","max_rate":"0.200000000000000000","rate":"0.100000000000000000"},"update_time":"2023-02-20T11:08:30.588307671Z"},"consensus_pubkey":{"@type":"/cosmos.crypto.ed25519.PubKey","key":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="},"delegator_shares":"1000000.000000000000000000","description":{"details":"","identity":"","moniker":"test","security_contact":"","website":""},"jailed":false,"min_self_delegation":"1","operator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","status":"BOND_STATUS_BONDED","tokens":"1000000","unbonding_height":"0","unbonding_ids":[],"unbonding_on_hold_ref_count":"0","unbonding_time":"1970-01-01T00:00:00Z"}]},"upgrade":{},"vesting":{}},"chain_id":"demo","consensus_params":{"block":{"max_bytes":"22020096","max_gas":"-1"},"evidence":{"max_age_duration":"172800000000000","max_age_num_blocks":"100000","max_bytes":"1048576"},"validator":{"pub_key_types":["ed25519"]},"version":{"app":"0"}},"genesis_time":"2023-02-20T11:08:30.588307671Z","initial_height":"48","validators":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","name":"test","power":"1","pub_key":{"type":"tendermint/PubKeyEd25519","value":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="}}]} \ No newline at end of file From 6bb526b1282a47161b3e645db58ca1b39b1db5e9 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Tue, 17 Sep 2024 14:20:19 +0700 Subject: [PATCH 07/35] minor --- tests/systemtests/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/systemtests/go.mod b/tests/systemtests/go.mod index 39c99b09611f..0ab29370379e 100644 --- a/tests/systemtests/go.mod +++ b/tests/systemtests/go.mod @@ -1,6 +1,6 @@ module cosmossdk.io/tests/systemtests -go 1.23.0 +go 1.23 require ( github.com/cosmos/cosmos-proto v1.0.0-beta.5 // indirect From f9aa48ba8030e3e7509aea51930f42ff559e2016 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Tue, 17 Sep 2024 15:14:55 +0700 Subject: [PATCH 08/35] fix test --- crypto/codec/cmt.go | 35 ----------------------- x/genutil/types/genesis.go | 11 +------ x/genutil/types/testdata/cmt_genesis.json | 2 +- 3 files changed, 2 insertions(+), 46 deletions(-) diff --git a/crypto/codec/cmt.go b/crypto/codec/cmt.go index 2023e2e43940..9617f94bf1a3 100644 --- a/crypto/codec/cmt.go +++ b/crypto/codec/cmt.go @@ -60,31 +60,6 @@ func ToCmtProtoPublicKey(pk cryptotypes.PubKey) (cmtprotocrypto.PublicKey, error } } -func ToCmtProtoPublicKeyFromJsonCompatPubkey(pk cryptotypes.JSONCompatPubKey) (cmtprotocrypto.PublicKey, error) { - switch pk := pk.(type) { - case *ed25519.PubKey: - return cmtprotocrypto.PublicKey{ - Sum: &cmtprotocrypto.PublicKey_Ed25519{ - Ed25519: pk.Key, - }, - }, nil - case *secp256k1.PubKey: - return cmtprotocrypto.PublicKey{ - Sum: &cmtprotocrypto.PublicKey_Secp256K1{ - Secp256K1: pk.Key, - }, - }, nil - case *bls12_381.PubKey: - return cmtprotocrypto.PublicKey{ - Sum: &cmtprotocrypto.PublicKey_Bls12381{ - Bls12381: pk.Key, - }, - }, nil - default: - return cmtprotocrypto.PublicKey{}, errors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v to Tendermint public key", pk) - } -} - // FromCmtPubKeyInterface converts CMT's cmtcrypto.PubKey to our own PubKey. func FromCmtPubKeyInterface(tmPk cmtcrypto.PubKey) (cryptotypes.PubKey, error) { tmProtoPk, err := encoding.PubKeyToProto(tmPk) @@ -104,13 +79,3 @@ func ToCmtPubKeyInterface(pk cryptotypes.PubKey) (cmtcrypto.PubKey, error) { return encoding.PubKeyFromProto(tmProtoPk) } - -// ToCmtPubKeyInterface converts our own PubKey to CMT's cmtcrypto.PubKey. -func ToCmtPubKeyInterfaceFromJsonCompatPubkey(pk cryptotypes.JSONCompatPubKey) (cmtcrypto.PubKey, error) { - tmProtoPk, err := ToCmtProtoPublicKeyFromJsonCompatPubkey(pk) - if err != nil { - return nil, err - } - - return encoding.PubKeyFromProto(tmProtoPk) -} diff --git a/x/genutil/types/genesis.go b/x/genutil/types/genesis.go index ef039fdfb53b..8e637aa69463 100644 --- a/x/genutil/types/genesis.go +++ b/x/genutil/types/genesis.go @@ -14,7 +14,6 @@ import ( cmtjson "github.com/cometbft/cometbft/libs/json" cmttypes "github.com/cometbft/cometbft/types" - cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/version" ) @@ -122,13 +121,9 @@ func AppGenesisFromReader(reader io.Reader) (*AppGenesis, error) { vals := []sdk.GenesisValidator{} for _, cmtVal := range ctmGenesis.Validators { - cmtPk, err := cryptocodec.FromCmtPubKeyInterface(cmtVal.PubKey) - if err != nil { - return nil, err - } val := sdk.GenesisValidator{ Address: cmtVal.Address.Bytes(), - PubKey: cmtPk, + PubKey: cmtVal.PubKey, Power: cmtVal.Power, Name: cmtVal.Name, } @@ -136,10 +131,6 @@ func AppGenesisFromReader(reader io.Reader) (*AppGenesis, error) { vals = append(vals, val) } - if len(vals) == 0 { - vals = nil - } - ag = AppGenesis{ AppName: version.AppName, // AppVersion is not filled as we do not know it from a CometBFT genesis diff --git a/x/genutil/types/testdata/cmt_genesis.json b/x/genutil/types/testdata/cmt_genesis.json index 882ebdfd9117..aae3d84d0698 100644 --- a/x/genutil/types/testdata/cmt_genesis.json +++ b/x/genutil/types/testdata/cmt_genesis.json @@ -1 +1 @@ -{"app_hash":"","app_state":{"auth":{"accounts":[{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"1","address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"8","address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"4","address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","pub_key":null,"sequence":"0"},"name":"bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"5","address":"cosmos1tygms3xhhs3yv487phx3dw4a95jn7t7lpm470r","pub_key":null,"sequence":"0"},"name":"not_bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"6","address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","pub_key":null,"sequence":"0"},"name":"gov","permissions":["burner"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"3","address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","pub_key":null,"sequence":"0"},"name":"distribution","permissions":[]},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"0","address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","pub_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"A3uyZdXedyvYx9VCL6xRjkxtcFpgxjhXFIz9b2mWz+aV"},"sequence":"4"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"7","address":"cosmos1m3h30wlvsf8llruxtpukdvsy0km2kum8g38c8q","pub_key":null,"sequence":"0"},"name":"mint","permissions":["minter"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"2","address":"cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta","pub_key":null,"sequence":"0"},"name":"fee_collector","permissions":[]}],"params":{"max_memo_characters":"256","sig_verify_cost_ed25519":"590","sig_verify_cost_secp256k1":"1000","tx_sig_limit":"7","tx_size_cost_per_byte":"10"}},"authz":{"authorization":[]},"bank":{"balances":[{"address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","coins":[{"amount":"5000000000","denom":"stake"}]},{"address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","coins":[{"amount":"1000","denom":"stake"}]},{"address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","coins":[{"amount":"1000000","denom":"stake"}]},{"address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","coins":[{"amount":"10010000","denom":"stake"}]},{"address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","coins":[{"amount":"9635","denom":"stake"}]},{"address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","coins":[{"amount":"4988989000","denom":"stake"}]}],"denom_metadata":[],"params":{"default_send_enabled":true,"send_enabled":[]},"send_enabled":[],"supply":[{"amount":"10000009635","denom":"stake"}]},"consensus":null,"crisis":{"constant_fee":{"amount":"1000","denom":"stake"}},"distribution":{"delegator_starting_infos":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","starting_info":{"height":"0","previous_period":"1","stake":"1000000.000000000000000000"},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"delegator_withdraw_infos":[],"fee_pool":{"community_pool":[{"amount":"192.700000000000000000","denom":"stake"}]},"outstanding_rewards":[{"outstanding_rewards":[{"amount":"9442.300000000000000000","denom":"stake"}],"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"params":{"base_proposer_reward":"0.000000000000000000","bonus_proposer_reward":"0.000000000000000000","community_tax":"0.020000000000000000","withdraw_addr_enabled":true},"previous_proposer":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_accumulated_commissions":[{"accumulated":{"commission":[{"amount":"944.230000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_current_rewards":[{"rewards":{"period":"2","rewards":[{"amount":"8498.070000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_historical_rewards":[{"period":"1","rewards":{"cumulative_reward_ratio":[],"reference_count":2},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_slash_events":[]},"evidence":{"evidence":[]},"feegrant":{"allowances":[]},"genutil":{"gen_txs":[]},"gov":{"deposit_params":null,"deposits":[{"amount":[{"amount":"10010000","denom":"stake"}],"depositor":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","proposal_id":"1"}],"params":{"expedited_min_deposit":[{"amount":"50000000","denom":"stake"}],"expedited_threshold":"0.667000000000000000","expedited_voting_period":"86400s","max_deposit_period":"172800s","min_deposit":[{"amount":"10000000","denom":"stake"}],"min_initial_deposit_ratio":"0.000000000000000000","proposal_cancel_dest":"","proposal_cancel_ratio":"0.500000000000000000","quorum":"0.334000000000000000","threshold":"0.500000000000000000","veto_threshold":"0.334000000000000000","voting_period":"172800s"},"proposals":[{"deposit_end_time":"2023-02-22T11:11:52.776167376Z","expedited":false,"final_tally_result":{"abstain_count":"0","no_count":"0","no_with_veto_count":"0","yes_count":"0"},"id":"1","messages":[{"@type":"/cosmos.distribution.v1beta1.MsgCommunityPoolSpend","amount":[],"authority":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","recipient":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8"}],"metadata":"ipfs://CID","proposer":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","status":"PROPOSAL_STATUS_VOTING_PERIOD","submit_time":"2023-02-20T11:11:52.776167376Z","summary":"test proposal","title":"test proposal","total_deposit":[{"amount":"10010000","denom":"stake"}],"voting_end_time":"2023-02-22T11:12:07.801161984Z","voting_start_time":"2023-02-20T11:12:07.801161984Z"}],"starting_proposal_id":"2","tally_params":null,"votes":[],"voting_params":null},"group":{"group_members":[],"group_policies":[],"group_policy_seq":"0","group_seq":"0","groups":[],"proposal_seq":"0","proposals":[],"votes":[]},"mint":{"minter":{"annual_provisions":"1300010905.175073197786747950","inflation":"0.130000967926594565"},"params":{"blocks_per_year":"6311520","goal_bonded":"0.670000000000000000","inflation_max":"0.200000000000000000","inflation_min":"0.070000000000000000","inflation_rate_change":"0.130000000000000000","mint_denom":"stake"}},"nft":{"classes":[],"entries":[]},"params":null,"slashing":{"missed_blocks":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","missed_blocks":[]}],"params":{"downtime_jail_duration":"600s","min_signed_per_window":"0.500000000000000000","signed_blocks_window":"100","slash_fraction_double_sign":"0.050000000000000000","slash_fraction_downtime":"0.010000000000000000"},"signing_infos":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_signing_info":{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","index_offset":"46","jailed_until":"1970-01-01T00:00:00Z","missed_blocks_counter":"0","start_height":"0","tombstoned":false}}]},"staking":{"delegations":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","shares":"1000000.000000000000000000","validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"exported":true,"last_total_power":"1","last_validator_powers":[{"address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","power":"1"}],"params":{"bond_denom":"stake","historical_entries":10000,"max_entries":7,"max_validators":100,"min_commission_rate":"0.000000000000000000","unbonding_time":"1814400s"},"redelegations":[],"unbonding_delegations":[],"validators":[{"commission":{"commission_rates":{"max_change_rate":"0.010000000000000000","max_rate":"0.200000000000000000","rate":"0.100000000000000000"},"update_time":"2023-02-20T11:08:30.588307671Z"},"consensus_pubkey":{"@type":"/cosmos.crypto.ed25519.PubKey","key":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="},"delegator_shares":"1000000.000000000000000000","description":{"details":"","identity":"","moniker":"test","security_contact":"","website":""},"jailed":false,"min_self_delegation":"1","operator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","status":"BOND_STATUS_BONDED","tokens":"1000000","unbonding_height":"0","unbonding_ids":[],"unbonding_on_hold_ref_count":"0","unbonding_time":"1970-01-01T00:00:00Z"}]},"upgrade":{},"vesting":{}},"chain_id":"demo","consensus_params":{"block":{"max_bytes":"22020096","max_gas":"-1"},"evidence":{"max_age_duration":"172800000000000","max_age_num_blocks":"100000","max_bytes":"1048576"},"validator":{"pub_key_types":["ed25519"]},"version":{"app":"0"}},"genesis_time":"2023-02-20T11:08:30.588307671Z","initial_height":"48","validators":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","name":"test","power":"1","pub_key":{"type":"tendermint/PubKeyEd25519","value":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="}}]} \ No newline at end of file +{"app_hash":"","app_state":{"auth":{"accounts":[{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"1","address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"8","address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"4","address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","pub_key":null,"sequence":"0"},"name":"bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"5","address":"cosmos1tygms3xhhs3yv487phx3dw4a95jn7t7lpm470r","pub_key":null,"sequence":"0"},"name":"not_bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"6","address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","pub_key":null,"sequence":"0"},"name":"gov","permissions":["burner"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"3","address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","pub_key":null,"sequence":"0"},"name":"distribution","permissions":[]},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"0","address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","pub_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"A3uyZdXedyvYx9VCL6xRjkxtcFpgxjhXFIz9b2mWz+aV"},"sequence":"4"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"7","address":"cosmos1m3h30wlvsf8llruxtpukdvsy0km2kum8g38c8q","pub_key":null,"sequence":"0"},"name":"mint","permissions":["minter"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"2","address":"cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta","pub_key":null,"sequence":"0"},"name":"fee_collector","permissions":[]}],"params":{"max_memo_characters":"256","sig_verify_cost_ed25519":"590","sig_verify_cost_secp256k1":"1000","tx_sig_limit":"7","tx_size_cost_per_byte":"10"}},"authz":{"authorization":[]},"bank":{"balances":[{"address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","coins":[{"amount":"5000000000","denom":"stake"}]},{"address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","coins":[{"amount":"1000","denom":"stake"}]},{"address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","coins":[{"amount":"1000000","denom":"stake"}]},{"address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","coins":[{"amount":"10010000","denom":"stake"}]},{"address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","coins":[{"amount":"9635","denom":"stake"}]},{"address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","coins":[{"amount":"4988989000","denom":"stake"}]}],"denom_metadata":[],"params":{"default_send_enabled":true,"send_enabled":[]},"send_enabled":[],"supply":[{"amount":"10000009635","denom":"stake"}]},"consensus":null,"crisis":{"constant_fee":{"amount":"1000","denom":"stake"}},"distribution":{"delegator_starting_infos":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","starting_info":{"height":"0","previous_period":"1","stake":"1000000.000000000000000000"},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"delegator_withdraw_infos":[],"fee_pool":{"community_pool":[{"amount":"192.700000000000000000","denom":"stake"}]},"outstanding_rewards":[{"outstanding_rewards":[{"amount":"9442.300000000000000000","denom":"stake"}],"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"params":{"base_proposer_reward":"0.000000000000000000","bonus_proposer_reward":"0.000000000000000000","community_tax":"0.020000000000000000","withdraw_addr_enabled":true},"previous_proposer":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_accumulated_commissions":[{"accumulated":{"commission":[{"amount":"944.230000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_current_rewards":[{"rewards":{"period":"2","rewards":[{"amount":"8498.070000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_historical_rewards":[{"period":"1","rewards":{"cumulative_reward_ratio":[],"reference_count":2},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_slash_events":[]},"evidence":{"evidence":[]},"feegrant":{"allowances":[]},"genutil":{"gen_txs":[]},"gov":{"deposit_params":null,"deposits":[{"amount":[{"amount":"10010000","denom":"stake"}],"depositor":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","proposal_id":"1"}],"params":{"expedited_min_deposit":[{"amount":"50000000","denom":"stake"}],"expedited_threshold":"0.667000000000000000","expedited_voting_period":"86400s","max_deposit_period":"172800s","min_deposit":[{"amount":"10000000","denom":"stake"}],"min_initial_deposit_ratio":"0.000000000000000000","proposal_cancel_dest":"","proposal_cancel_ratio":"0.500000000000000000","quorum":"0.334000000000000000","threshold":"0.500000000000000000","veto_threshold":"0.334000000000000000","voting_period":"172800s"},"proposals":[{"deposit_end_time":"2023-02-22T11:11:52.776167376Z","expedited":false,"final_tally_result":{"abstain_count":"0","no_count":"0","no_with_veto_count":"0","yes_count":"0"},"id":"1","messages":[{"@type":"/cosmos.distribution.v1beta1.MsgCommunityPoolSpend","amount":[],"authority":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","recipient":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8"}],"metadata":"ipfs://CID","proposer":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","status":"PROPOSAL_STATUS_VOTING_PERIOD","submit_time":"2023-02-20T11:11:52.776167376Z","summary":"test proposal","title":"test proposal","total_deposit":[{"amount":"10010000","denom":"stake"}],"voting_end_time":"2023-02-22T11:12:07.801161984Z","voting_start_time":"2023-02-20T11:12:07.801161984Z"}],"starting_proposal_id":"2","tally_params":null,"votes":[],"voting_params":null},"group":{"group_members":[],"group_policies":[],"group_policy_seq":"0","group_seq":"0","groups":[],"proposal_seq":"0","proposals":[],"votes":[]},"mint":{"minter":{"annual_provisions":"1300010905.175073197786747950","inflation":"0.130000967926594565"},"params":{"blocks_per_year":"6311520","goal_bonded":"0.670000000000000000","inflation_max":"0.200000000000000000","inflation_min":"0.070000000000000000","inflation_rate_change":"0.130000000000000000","mint_denom":"stake"}},"nft":{"classes":[],"entries":[]},"params":null,"slashing":{"missed_blocks":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","missed_blocks":[]}],"params":{"downtime_jail_duration":"600s","min_signed_per_window":"0.500000000000000000","signed_blocks_window":"100","slash_fraction_double_sign":"0.050000000000000000","slash_fraction_downtime":"0.010000000000000000"},"signing_infos":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_signing_info":{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","index_offset":"46","jailed_until":"1970-01-01T00:00:00Z","missed_blocks_counter":"0","start_height":"0","tombstoned":false}}]},"staking":{"delegations":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","shares":"1000000.000000000000000000","validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"exported":true,"last_total_power":"1","last_validator_powers":[{"address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","power":"1"}],"params":{"bond_denom":"stake","historical_entries":10000,"max_entries":7,"max_validators":100,"min_commission_rate":"0.000000000000000000","unbonding_time":"1814400s"},"redelegations":[],"unbonding_delegations":[],"validators":[{"commission":{"commission_rates":{"max_change_rate":"0.010000000000000000","max_rate":"0.200000000000000000","rate":"0.100000000000000000"},"update_time":"2023-02-20T11:08:30.588307671Z"},"consensus_pubkey":{"@type":"/cosmos.crypto.ed25519.PubKey","key":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="},"delegator_shares":"1000000.000000000000000000","description":{"details":"","identity":"","moniker":"test","security_contact":"","website":""},"jailed":false,"min_self_delegation":"1","operator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","status":"BOND_STATUS_BONDED","tokens":"1000000","unbonding_height":"0","unbonding_ids":[],"unbonding_on_hold_ref_count":"0","unbonding_time":"1970-01-01T00:00:00Z"}]},"upgrade":{},"vesting":{}},"chain_id":"demo","consensus_params":{"block":{"max_bytes":"22020096","max_gas":"-1"},"evidence":{"max_age_duration":"172800000000000","max_age_num_blocks":"100000","max_bytes":"1048576"},"validator":{"pub_key_types":["ed25519"]},"version":{"app":"0"}},"genesis_time":"2023-02-20T11:08:30.588307671Z","initial_height":"48","validators":[{"address":"D336F99AA5CF77503CDD8366E68A0DFE89B4124B","name":"test","power":"1","pub_key":{"type":"tendermint/PubKeyEd25519","value":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="}}]} \ No newline at end of file From 389b7b8f698b49110c95203500d29553424d0ba1 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Tue, 17 Sep 2024 15:33:45 +0700 Subject: [PATCH 09/35] lint --- crypto/types/types.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto/types/types.go b/crypto/types/types.go index 84ec0400d783..48cab946190d 100644 --- a/crypto/types/types.go +++ b/crypto/types/types.go @@ -9,7 +9,7 @@ import ( type JSONCompatPubKey interface { Address() Address Bytes() []byte - VerifySignature(msg []byte, sig []byte) bool + VerifySignature(msg, sig []byte) bool Type() string } From 5775c9fd6b905dc6d078ac525b3bc1c028e1bdec Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Tue, 17 Sep 2024 16:09:36 +0700 Subject: [PATCH 10/35] fix e2e test --- tests/e2e/genutil/export_test.go | 3 ++- x/genutil/client/cli/export.go | 2 ++ x/staking/genesis.go | 8 +++++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/e2e/genutil/export_test.go b/tests/e2e/genutil/export_test.go index 0a98ef6b4e85..798885c0900f 100644 --- a/tests/e2e/genutil/export_test.go +++ b/tests/e2e/genutil/export_test.go @@ -31,6 +31,7 @@ import ( "github.com/cosmos/cosmos-sdk/server/types" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" gentestutil "github.com/cosmos/cosmos-sdk/testutil/x/genutil" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/genutil" genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" @@ -184,7 +185,7 @@ func setupApp(t *testing.T, tempDir string) (*simapp.SimApp, context.Context, ge ChainID: "theChainId", AppState: stateBytes, Consensus: &genutiltypes.ConsensusGenesis{ - Validators: nil, + Validators: []sdk.GenesisValidator{}, }, } diff --git a/x/genutil/client/cli/export.go b/x/genutil/client/cli/export.go index 986b9c1cdb2d..faf1122ded29 100644 --- a/x/genutil/client/cli/export.go +++ b/x/genutil/client/cli/export.go @@ -90,6 +90,8 @@ func ExportCmd(appExporter servertypes.AppExporter) *cobra.Command { return err } + fmt.Println("vals", exported.Validators) + // set current binary version appGenesis.AppName = version.AppName appGenesis.AppVersion = version.Version diff --git a/x/staking/genesis.go b/x/staking/genesis.go index ab6f64614988..285a9c56b66c 100644 --- a/x/staking/genesis.go +++ b/x/staking/genesis.go @@ -9,6 +9,7 @@ import ( "cosmossdk.io/x/staking/keeper" "cosmossdk.io/x/staking/types" + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -26,9 +27,14 @@ func WriteValidators(ctx context.Context, keeper *keeper.Keeper) (vals []sdk.Gen return true, err } + cmtPk, err := cryptocodec.ToCmtPubKeyInterface(pk) + if err != nil { + return true, err + } + vals = append(vals, sdk.GenesisValidator{ Address: sdk.ConsAddress(pk.Address()).Bytes(), - PubKey: pk, + PubKey: cmtPk, Power: validator.GetConsensusPower(keeper.PowerReduction(ctx)), Name: validator.GetMoniker(), }) From 5fe570cf1e987992e28af80798c98ba22de78426 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Wed, 18 Sep 2024 18:00:46 +0700 Subject: [PATCH 11/35] add migration and genesis validator in genutil v2 --- crypto/types/types.go | 2 +- types/staking.go | 8 +- x/genutil/client/cli/export.go | 2 - x/genutil/migration/migration.go | 125 ++++++++++++++++++ x/genutil/migration/migration_test.go | 36 +++++ .../migration/testdata/old_app_genesis.json | 1 + x/genutil/v2/cli/export.go | 1 + x/genutil/v2/types.go | 4 + 8 files changed, 172 insertions(+), 7 deletions(-) create mode 100644 x/genutil/migration/migration.go create mode 100644 x/genutil/migration/migration_test.go create mode 100644 x/genutil/migration/testdata/old_app_genesis.json diff --git a/crypto/types/types.go b/crypto/types/types.go index 48cab946190d..0c11dc7d7d2c 100644 --- a/crypto/types/types.go +++ b/crypto/types/types.go @@ -6,7 +6,7 @@ import ( ) // JSONCompatPubKey defines a public key that are json compatible. -type JSONCompatPubKey interface { +type JSONPubKey interface { Address() Address Bytes() []byte VerifySignature(msg, sig []byte) bool diff --git a/types/staking.go b/types/staking.go index 7e9e7ae3d757..62401d5eb57c 100644 --- a/types/staking.go +++ b/types/staking.go @@ -101,8 +101,8 @@ type ValidatorI interface { // GenesisValidator is an initial validator. type GenesisValidator struct { - Address ConsAddress `json:"address"` - PubKey cryptotypes.JSONCompatPubKey `json:"pub_key"` - Power int64 `json:"power"` - Name string `json:"name"` + Address ConsAddress `json:"address"` + PubKey cryptotypes.JSONPubKey `json:"pub_key"` + Power int64 `json:"power"` + Name string `json:"name"` } diff --git a/x/genutil/client/cli/export.go b/x/genutil/client/cli/export.go index faf1122ded29..986b9c1cdb2d 100644 --- a/x/genutil/client/cli/export.go +++ b/x/genutil/client/cli/export.go @@ -90,8 +90,6 @@ func ExportCmd(appExporter servertypes.AppExporter) *cobra.Command { return err } - fmt.Println("vals", exported.Validators) - // set current binary version appGenesis.AppName = version.AppName appGenesis.AppVersion = version.Version diff --git a/x/genutil/migration/migration.go b/x/genutil/migration/migration.go new file mode 100644 index 000000000000..a87ed5acdf80 --- /dev/null +++ b/x/genutil/migration/migration.go @@ -0,0 +1,125 @@ +package migration + +import ( + "encoding/json" + "fmt" + "io" + "os" + "path/filepath" + "time" + + cmtjson "github.com/cometbft/cometbft/libs/json" + cmttypes "github.com/cometbft/cometbft/types" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/genutil/types" +) + +type Migrator struct { + // genesis file path + filePath string + reader io.Reader +} + +type legacyAppGenesis struct { + AppName string `json:"app_name"` + AppVersion string `json:"app_version"` + GenesisTime time.Time `json:"genesis_time"` + ChainID string `json:"chain_id"` + InitialHeight int64 `json:"initial_height"` + AppHash []byte `json:"app_hash"` + AppState json.RawMessage `json:"app_state,omitempty"` + Consensus *legacyConsensusGenesis `json:"consensus,omitempty"` +} + +type legacyConsensusGenesis struct { + Validators []cmttypes.GenesisValidator `json:"validators,omitempty"` + Params *cmttypes.ConsensusParams `json:"params,omitempty"` +} + +// NewMigrator takes in 2 file path one for the current genesis file +// and the other are the directory where the new genesis file will live. +// If you want to replace old genesis file the both path could be the same. +func NewMigrator(filePath, savePath string) (*Migrator, error) { + file, err := os.Open(filepath.Clean(filePath)) + if err != nil { + return nil, err + } + + return &Migrator{ + filePath: savePath, + reader: file, + }, nil +} + +// MigrateGenesisFile migrate current genesis file content to match of the +// new genesis validator type. +func (m Migrator) MigrateGenesisFile() error { + var newAg types.AppGenesis + var ag legacyAppGenesis + var err error + + if rs, ok := m.reader.(io.ReadSeeker); ok { + err = json.NewDecoder(rs).Decode(&ag) + if err != nil { + return fmt.Errorf("error unmarshalling legacy AppGenesis: %w", err) + } + + vals := []sdk.GenesisValidator{} + for _, cmtVal := range ag.Consensus.Validators { + val := sdk.GenesisValidator{ + Address: sdk.ConsAddress(cmtVal.Address).Bytes(), + PubKey: cmtVal.PubKey, + Power: cmtVal.Power, + Name: cmtVal.Name, + } + + vals = append(vals, val) + } + + newAg = types.AppGenesis{ + AppName: ag.AppName, + AppVersion: ag.AppVersion, + GenesisTime: ag.GenesisTime, + ChainID: ag.ChainID, + InitialHeight: ag.InitialHeight, + AppHash: ag.AppHash, + AppState: ag.AppState, + Consensus: &types.ConsensusGenesis{ + Validators: vals, + Params: ag.Consensus.Params, + }, + } + } + + err = newAg.ValidateAndComplete() + if err != nil { + return err + } + + return newAg.SaveAs(m.filePath) +} + +// CometBFT Genesis Handling for JSON, +// this is necessary for json unmarshaling of legacyConsensusGenesis +func (cs *legacyConsensusGenesis) MarshalJSON() ([]byte, error) { + type Alias legacyConsensusGenesis + return cmtjson.Marshal(&Alias{ + Validators: cs.Validators, + Params: cs.Params, + }) +} + +func (cs *legacyConsensusGenesis) UnmarshalJSON(b []byte) error { + type Alias legacyConsensusGenesis + + result := Alias{} + if err := cmtjson.Unmarshal(b, &result); err != nil { + return err + } + + cs.Params = result.Params + cs.Validators = result.Validators + + return nil +} diff --git a/x/genutil/migration/migration_test.go b/x/genutil/migration/migration_test.go new file mode 100644 index 000000000000..85f3864ba51c --- /dev/null +++ b/x/genutil/migration/migration_test.go @@ -0,0 +1,36 @@ +package migration + +import ( + "os" + "testing" + + "github.com/cosmos/cosmos-sdk/x/genutil/types" + "github.com/stretchr/testify/require" +) + +var ( + oldGenFilePath = "./testdata/old_app_genesis.json" +) + +func TestMigration(t *testing.T) { + tempDir := t.TempDir() + + // clean all content on this directory + err := os.RemoveAll(tempDir) + require.NoError(t, err) + + migrator, err := NewMigrator(oldGenFilePath, tempDir) + require.NoError(t, err) + + // should not be able to get app genesis from new genesis file + // since validators address are still in hex string and not cons address + _, err = types.AppGenesisFromFile(oldGenFilePath) + require.ErrorContains(t, err, "error unmarshalling AppGenesis: decoding bech32 failed") + + err = migrator.MigrateGenesisFile() + require.NoError(t, err) + + // should be able to get app genesis from new genesis file + _, err = types.AppGenesisFromFile(tempDir) + require.NoError(t, err) +} diff --git a/x/genutil/migration/testdata/old_app_genesis.json b/x/genutil/migration/testdata/old_app_genesis.json new file mode 100644 index 000000000000..57ca37536b6d --- /dev/null +++ b/x/genutil/migration/testdata/old_app_genesis.json @@ -0,0 +1 @@ +{"app_name":"\u003cappd\u003e","app_version":"","genesis_time":"2023-02-20T11:08:30.588307671Z","chain_id":"demo","initial_height":48,"app_hash":"","app_state":{"auth":{"accounts":[{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"1","address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"8","address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"4","address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","pub_key":null,"sequence":"0"},"name":"bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"5","address":"cosmos1tygms3xhhs3yv487phx3dw4a95jn7t7lpm470r","pub_key":null,"sequence":"0"},"name":"not_bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"6","address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","pub_key":null,"sequence":"0"},"name":"gov","permissions":["burner"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"3","address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","pub_key":null,"sequence":"0"},"name":"distribution","permissions":[]},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"0","address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","pub_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"A3uyZdXedyvYx9VCL6xRjkxtcFpgxjhXFIz9b2mWz+aV"},"sequence":"4"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"7","address":"cosmos1m3h30wlvsf8llruxtpukdvsy0km2kum8g38c8q","pub_key":null,"sequence":"0"},"name":"mint","permissions":["minter"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"2","address":"cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta","pub_key":null,"sequence":"0"},"name":"fee_collector","permissions":[]}],"params":{"max_memo_characters":"256","sig_verify_cost_ed25519":"590","sig_verify_cost_secp256k1":"1000","tx_sig_limit":"7","tx_size_cost_per_byte":"10"}},"authz":{"authorization":[]},"bank":{"balances":[{"address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","coins":[{"amount":"5000000000","denom":"stake"}]},{"address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","coins":[{"amount":"1000","denom":"stake"}]},{"address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","coins":[{"amount":"1000000","denom":"stake"}]},{"address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","coins":[{"amount":"10010000","denom":"stake"}]},{"address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","coins":[{"amount":"9635","denom":"stake"}]},{"address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","coins":[{"amount":"4988989000","denom":"stake"}]}],"denom_metadata":[],"params":{"default_send_enabled":true,"send_enabled":[]},"send_enabled":[],"supply":[{"amount":"10000009635","denom":"stake"}]},"consensus":null,"crisis":{"constant_fee":{"amount":"1000","denom":"stake"}},"distribution":{"delegator_starting_infos":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","starting_info":{"height":"0","previous_period":"1","stake":"1000000.000000000000000000"},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"delegator_withdraw_infos":[],"fee_pool":{"community_pool":[{"amount":"192.700000000000000000","denom":"stake"}]},"outstanding_rewards":[{"outstanding_rewards":[{"amount":"9442.300000000000000000","denom":"stake"}],"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"params":{"base_proposer_reward":"0.000000000000000000","bonus_proposer_reward":"0.000000000000000000","community_tax":"0.020000000000000000","withdraw_addr_enabled":true},"previous_proposer":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_accumulated_commissions":[{"accumulated":{"commission":[{"amount":"944.230000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_current_rewards":[{"rewards":{"period":"2","rewards":[{"amount":"8498.070000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_historical_rewards":[{"period":"1","rewards":{"cumulative_reward_ratio":[],"reference_count":2},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_slash_events":[]},"evidence":{"evidence":[]},"feegrant":{"allowances":[]},"genutil":{"gen_txs":[]},"gov":{"deposit_params":null,"deposits":[{"amount":[{"amount":"10010000","denom":"stake"}],"depositor":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","proposal_id":"1"}],"params":{"expedited_min_deposit":[{"amount":"50000000","denom":"stake"}],"expedited_threshold":"0.667000000000000000","expedited_voting_period":"86400s","max_deposit_period":"172800s","min_deposit":[{"amount":"10000000","denom":"stake"}],"min_initial_deposit_ratio":"0.000000000000000000","proposal_cancel_dest":"","proposal_cancel_ratio":"0.500000000000000000","quorum":"0.334000000000000000","threshold":"0.500000000000000000","veto_threshold":"0.334000000000000000","voting_period":"172800s"},"proposals":[{"deposit_end_time":"2023-02-22T11:11:52.776167376Z","expedited":false,"final_tally_result":{"abstain_count":"0","no_count":"0","no_with_veto_count":"0","yes_count":"0"},"id":"1","messages":[{"@type":"/cosmos.distribution.v1beta1.MsgCommunityPoolSpend","amount":[],"authority":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","recipient":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8"}],"metadata":"ipfs://CID","proposer":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","status":"PROPOSAL_STATUS_VOTING_PERIOD","submit_time":"2023-02-20T11:11:52.776167376Z","summary":"test proposal","title":"test proposal","total_deposit":[{"amount":"10010000","denom":"stake"}],"voting_end_time":"2023-02-22T11:12:07.801161984Z","voting_start_time":"2023-02-20T11:12:07.801161984Z"}],"starting_proposal_id":"2","tally_params":null,"votes":[],"voting_params":null},"group":{"group_members":[],"group_policies":[],"group_policy_seq":"0","group_seq":"0","groups":[],"proposal_seq":"0","proposals":[],"votes":[]},"mint":{"minter":{"annual_provisions":"1300010905.175073197786747950","inflation":"0.130000967926594565"},"params":{"blocks_per_year":"6311520","goal_bonded":"0.670000000000000000","inflation_max":"0.200000000000000000","inflation_min":"0.070000000000000000","inflation_rate_change":"0.130000000000000000","mint_denom":"stake"}},"nft":{"classes":[],"entries":[]},"params":null,"slashing":{"missed_blocks":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","missed_blocks":[]}],"params":{"downtime_jail_duration":"600s","min_signed_per_window":"0.500000000000000000","signed_blocks_window":"100","slash_fraction_double_sign":"0.050000000000000000","slash_fraction_downtime":"0.010000000000000000"},"signing_infos":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_signing_info":{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","index_offset":"46","jailed_until":"1970-01-01T00:00:00Z","missed_blocks_counter":"0","start_height":"0","tombstoned":false}}]},"staking":{"delegations":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","shares":"1000000.000000000000000000","validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"exported":true,"last_total_power":"1","last_validator_powers":[{"address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","power":"1"}],"params":{"bond_denom":"stake","historical_entries":10000,"max_entries":7,"max_validators":100,"min_commission_rate":"0.000000000000000000","unbonding_time":"1814400s"},"redelegations":[],"unbonding_delegations":[],"validators":[{"commission":{"commission_rates":{"max_change_rate":"0.010000000000000000","max_rate":"0.200000000000000000","rate":"0.100000000000000000"},"update_time":"2023-02-20T11:08:30.588307671Z"},"consensus_pubkey":{"@type":"/cosmos.crypto.ed25519.PubKey","key":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="},"delegator_shares":"1000000.000000000000000000","description":{"details":"","identity":"","moniker":"test","security_contact":"","website":""},"jailed":false,"min_self_delegation":"1","operator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","status":"BOND_STATUS_BONDED","tokens":"1000000","unbonding_height":"0","unbonding_ids":[],"unbonding_on_hold_ref_count":"0","unbonding_time":"1970-01-01T00:00:00Z"}]},"upgrade":{},"vesting":{}},"consensus":{"validators":[{"address":"D336F99AA5CF77503CDD8366E68A0DFE89B4124B","pub_key":{"type":"tendermint/PubKeyEd25519","value":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="},"power":"1","name":"test"}],"params":{"block":{"max_bytes":"22020096","max_gas":"-1"},"evidence":{"max_age_num_blocks":"100000","max_age_duration":"172800000000000","max_bytes":"1048576"},"validator":{"pub_key_types":["ed25519"]},"version":{"app":"0"},"synchrony":{"precision":"0","message_delay":"0"},"feature":{"vote_extensions_enable_height":"0","pbts_enable_height":"0"}}}} \ No newline at end of file diff --git a/x/genutil/v2/cli/export.go b/x/genutil/v2/cli/export.go index 318ad58d8a47..9b0d992bad0a 100644 --- a/x/genutil/v2/cli/export.go +++ b/x/genutil/v2/cli/export.go @@ -76,6 +76,7 @@ func ExportCmd(appExporter v2.AppExporter) *cobra.Command { appGenesis.AppState = exported.AppState appGenesis.InitialHeight = exported.Height + appGenesis.Consensus.Validators = exported.Validators out, err := json.Marshal(appGenesis) if err != nil { diff --git a/x/genutil/v2/types.go b/x/genutil/v2/types.go index 3199a5a5afb4..fbf288365a57 100644 --- a/x/genutil/v2/types.go +++ b/x/genutil/v2/types.go @@ -3,6 +3,8 @@ package v2 import ( "context" "encoding/json" + + sdk "github.com/cosmos/cosmos-sdk/types" ) // AppExporter is a function that dumps all app state to @@ -20,4 +22,6 @@ type ExportedApp struct { AppState json.RawMessage // Height is the app's latest block height. Height int64 + // Validators is the exported validator set. + Validators []sdk.GenesisValidator } From dd2a634feee91f5ba3ac9c74b0d8e4b114212bcd Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Wed, 18 Sep 2024 18:02:44 +0700 Subject: [PATCH 12/35] cleanup --- testutil/network/util.go | 1 - 1 file changed, 1 deletion(-) diff --git a/testutil/network/util.go b/testutil/network/util.go index 20aaca6d2b7a..08ee1b0c5d99 100644 --- a/testutil/network/util.go +++ b/testutil/network/util.go @@ -59,7 +59,6 @@ func startInProcess(cfg Config, val *Validator) error { }, err } gen, err := appGenesis.ToGenesisDoc() - fmt.Println("is", gen.Validators == nil) if err != nil { return node.ChecksummedGenesisDoc{ Sha256Checksum: []byte{}, From fba3f541edabb2987122eaefd3055bc1f7d4a9df Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Wed, 18 Sep 2024 18:15:09 +0700 Subject: [PATCH 13/35] lint --- x/genutil/migration/migration_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/x/genutil/migration/migration_test.go b/x/genutil/migration/migration_test.go index 85f3864ba51c..b79d76dc70f1 100644 --- a/x/genutil/migration/migration_test.go +++ b/x/genutil/migration/migration_test.go @@ -8,9 +8,7 @@ import ( "github.com/stretchr/testify/require" ) -var ( - oldGenFilePath = "./testdata/old_app_genesis.json" -) +var oldGenFilePath = "./testdata/old_app_genesis.json" func TestMigration(t *testing.T) { tempDir := t.TempDir() From 05fe6870369973fa11b6c99c590acd426336e1c6 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Wed, 18 Sep 2024 18:22:40 +0700 Subject: [PATCH 14/35] more lint --- x/genutil/migration/migration_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x/genutil/migration/migration_test.go b/x/genutil/migration/migration_test.go index b79d76dc70f1..3e17f29a4fae 100644 --- a/x/genutil/migration/migration_test.go +++ b/x/genutil/migration/migration_test.go @@ -4,8 +4,9 @@ import ( "os" "testing" - "github.com/cosmos/cosmos-sdk/x/genutil/types" "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/x/genutil/types" ) var oldGenFilePath = "./testdata/old_app_genesis.json" From 80a92ae0e2838ace10762c6b72e9d54b3257b706 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Thu, 19 Sep 2024 15:34:52 +0700 Subject: [PATCH 15/35] minor --- crypto/types/types.go | 2 +- x/genutil/migration/migration_test.go | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/crypto/types/types.go b/crypto/types/types.go index 0c11dc7d7d2c..e50e552dd80a 100644 --- a/crypto/types/types.go +++ b/crypto/types/types.go @@ -5,7 +5,7 @@ import ( "github.com/cosmos/gogoproto/proto" ) -// JSONCompatPubKey defines a public key that are json compatible. +// JSONPubKey defines a public key that are parse from JSON file. type JSONPubKey interface { Address() Address Bytes() []byte diff --git a/x/genutil/migration/migration_test.go b/x/genutil/migration/migration_test.go index 3e17f29a4fae..57ec44f0786d 100644 --- a/x/genutil/migration/migration_test.go +++ b/x/genutil/migration/migration_test.go @@ -1,6 +1,8 @@ package migration import ( + "bytes" + "encoding/json" "os" "testing" @@ -29,7 +31,18 @@ func TestMigration(t *testing.T) { err = migrator.MigrateGenesisFile() require.NoError(t, err) + var oldAppGenesis legacyAppGenesis + r, err := os.Open(oldGenFilePath) + require.NoError(t, err) + err = json.NewDecoder(r).Decode(&oldAppGenesis) + require.NoError(t, err) + // should be able to get app genesis from new genesis file - _, err = types.AppGenesisFromFile(tempDir) + newAppGenesis, err := types.AppGenesisFromFile(tempDir) + require.NotNil(t, newAppGenesis) + require.NotNil(t, newAppGenesis.Consensus) + require.True(t, bytes.Equal(oldAppGenesis.AppHash, newAppGenesis.AppHash)) + require.True(t, bytes.Equal(oldAppGenesis.Consensus.Validators[0].Address.Bytes(), newAppGenesis.Consensus.Validators[0].Address.Bytes())) + require.NoError(t, err) } From d0f9f1cd38c7ef2fd88ee4f8f2e365142d7e919b Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 23 Sep 2024 12:54:37 +0700 Subject: [PATCH 16/35] make work with migrate cmd --- x/genutil/client/cli/migrate.go | 30 +++- x/genutil/migration/migration.go | 125 ------------- x/genutil/migration/v052/migration.go | 170 ++++++++++++++++++ .../migration/{ => v052}/migration_test.go | 10 +- .../{ => v052}/testdata/old_app_genesis.json | 0 5 files changed, 202 insertions(+), 133 deletions(-) delete mode 100644 x/genutil/migration/migration.go create mode 100644 x/genutil/migration/v052/migration.go rename x/genutil/migration/{ => v052}/migration_test.go (79%) rename x/genutil/migration/{ => v052}/testdata/old_app_genesis.json (100%) diff --git a/x/genutil/client/cli/migrate.go b/x/genutil/client/cli/migrate.go index 2763ba06cfb4..feb4a75e8d37 100644 --- a/x/genutil/client/cli/migrate.go +++ b/x/genutil/client/cli/migrate.go @@ -13,13 +13,16 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/version" + v052 "github.com/cosmos/cosmos-sdk/x/genutil/migration/v052" "github.com/cosmos/cosmos-sdk/x/genutil/types" ) const flagGenesisTime = "genesis-time" // MigrationMap is a map of SDK versions to their respective genesis migration functions. -var MigrationMap = types.MigrationMap{} +var MigrationMap = types.MigrationMap{ + "v0.52": v052.Migrate, +} // MigrateGenesisCmd returns a command to execute genesis state migration. // Applications should pass their own migration map to this function. @@ -56,9 +59,27 @@ func MigrateHandler(cmd *cobra.Command, args []string, migrations types.Migratio } importGenesis := args[1] - appGenesis, err := types.AppGenesisFromFile(importGenesis) - if err != nil { - return err + outputDocument, _ := cmd.Flags().GetString(flags.FlagOutputDocument) + + // for v52 we need to migrate the consensus validator address from hex bytes to + // sdk consensus address. + var appGenesis *types.AppGenesis + var err error + if target == "v0.52" { + v52Migrator, err := v052.NewMigrator(importGenesis) + if err != nil { + return err + } + + appGenesis, err = v52Migrator.MigrateGenesisFile() + if err != nil { + return err + } + } else { + appGenesis, err = types.AppGenesisFromFile(importGenesis) + if err != nil { + return err + } } if err := appGenesis.ValidateAndComplete(); err != nil { @@ -110,7 +131,6 @@ func MigrateHandler(cmd *cobra.Command, args []string, migrations types.Migratio return fmt.Errorf("failed to marshal app genesis: %w", err) } - outputDocument, _ := cmd.Flags().GetString(flags.FlagOutputDocument) if outputDocument == "" { cmd.Println(string(bz)) return nil diff --git a/x/genutil/migration/migration.go b/x/genutil/migration/migration.go deleted file mode 100644 index a87ed5acdf80..000000000000 --- a/x/genutil/migration/migration.go +++ /dev/null @@ -1,125 +0,0 @@ -package migration - -import ( - "encoding/json" - "fmt" - "io" - "os" - "path/filepath" - "time" - - cmtjson "github.com/cometbft/cometbft/libs/json" - cmttypes "github.com/cometbft/cometbft/types" - - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/genutil/types" -) - -type Migrator struct { - // genesis file path - filePath string - reader io.Reader -} - -type legacyAppGenesis struct { - AppName string `json:"app_name"` - AppVersion string `json:"app_version"` - GenesisTime time.Time `json:"genesis_time"` - ChainID string `json:"chain_id"` - InitialHeight int64 `json:"initial_height"` - AppHash []byte `json:"app_hash"` - AppState json.RawMessage `json:"app_state,omitempty"` - Consensus *legacyConsensusGenesis `json:"consensus,omitempty"` -} - -type legacyConsensusGenesis struct { - Validators []cmttypes.GenesisValidator `json:"validators,omitempty"` - Params *cmttypes.ConsensusParams `json:"params,omitempty"` -} - -// NewMigrator takes in 2 file path one for the current genesis file -// and the other are the directory where the new genesis file will live. -// If you want to replace old genesis file the both path could be the same. -func NewMigrator(filePath, savePath string) (*Migrator, error) { - file, err := os.Open(filepath.Clean(filePath)) - if err != nil { - return nil, err - } - - return &Migrator{ - filePath: savePath, - reader: file, - }, nil -} - -// MigrateGenesisFile migrate current genesis file content to match of the -// new genesis validator type. -func (m Migrator) MigrateGenesisFile() error { - var newAg types.AppGenesis - var ag legacyAppGenesis - var err error - - if rs, ok := m.reader.(io.ReadSeeker); ok { - err = json.NewDecoder(rs).Decode(&ag) - if err != nil { - return fmt.Errorf("error unmarshalling legacy AppGenesis: %w", err) - } - - vals := []sdk.GenesisValidator{} - for _, cmtVal := range ag.Consensus.Validators { - val := sdk.GenesisValidator{ - Address: sdk.ConsAddress(cmtVal.Address).Bytes(), - PubKey: cmtVal.PubKey, - Power: cmtVal.Power, - Name: cmtVal.Name, - } - - vals = append(vals, val) - } - - newAg = types.AppGenesis{ - AppName: ag.AppName, - AppVersion: ag.AppVersion, - GenesisTime: ag.GenesisTime, - ChainID: ag.ChainID, - InitialHeight: ag.InitialHeight, - AppHash: ag.AppHash, - AppState: ag.AppState, - Consensus: &types.ConsensusGenesis{ - Validators: vals, - Params: ag.Consensus.Params, - }, - } - } - - err = newAg.ValidateAndComplete() - if err != nil { - return err - } - - return newAg.SaveAs(m.filePath) -} - -// CometBFT Genesis Handling for JSON, -// this is necessary for json unmarshaling of legacyConsensusGenesis -func (cs *legacyConsensusGenesis) MarshalJSON() ([]byte, error) { - type Alias legacyConsensusGenesis - return cmtjson.Marshal(&Alias{ - Validators: cs.Validators, - Params: cs.Params, - }) -} - -func (cs *legacyConsensusGenesis) UnmarshalJSON(b []byte) error { - type Alias legacyConsensusGenesis - - result := Alias{} - if err := cmtjson.Unmarshal(b, &result); err != nil { - return err - } - - cs.Params = result.Params - cs.Validators = result.Validators - - return nil -} diff --git a/x/genutil/migration/v052/migration.go b/x/genutil/migration/v052/migration.go new file mode 100644 index 000000000000..5aff80974eab --- /dev/null +++ b/x/genutil/migration/v052/migration.go @@ -0,0 +1,170 @@ +package migration + +import ( + "encoding/json" + "errors" + "fmt" + "io" + "os" + "path/filepath" + "time" + + cmtjson "github.com/cometbft/cometbft/libs/json" + cmttypes "github.com/cometbft/cometbft/types" + + "github.com/cosmos/cosmos-sdk/client" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/version" + "github.com/cosmos/cosmos-sdk/x/genutil/types" +) + +type Migrator struct { + // genesis file path + filePath string + reader io.Reader +} + +type legacyAppGenesis struct { + AppName string `json:"app_name"` + AppVersion string `json:"app_version"` + GenesisTime time.Time `json:"genesis_time"` + ChainID string `json:"chain_id"` + InitialHeight int64 `json:"initial_height"` + AppHash []byte `json:"app_hash"` + AppState json.RawMessage `json:"app_state,omitempty"` + Consensus *legacyConsensusGenesis `json:"consensus,omitempty"` +} + +type legacyConsensusGenesis struct { + Validators []cmttypes.GenesisValidator `json:"validators,omitempty"` + Params *cmttypes.ConsensusParams `json:"params,omitempty"` +} + +func NewMigrator(filePath string) (*Migrator, error) { + file, err := os.Open(filepath.Clean(filePath)) + if err != nil { + return nil, err + } + + return &Migrator{ + reader: file, + }, nil +} + +// MigrateGenesisFile migrate current genesis file content to match of the +// new genesis validator type. +func (m Migrator) MigrateGenesisFile() (*types.AppGenesis, error) { + var newAg types.AppGenesis + var ag legacyAppGenesis + var err error + + if rs, ok := m.reader.(io.ReadSeeker); ok { + err = json.NewDecoder(rs).Decode(&ag) + if err == nil { + vals := []sdk.GenesisValidator{} + for _, cmtVal := range ag.Consensus.Validators { + val := sdk.GenesisValidator{ + Address: sdk.ConsAddress(cmtVal.Address).Bytes(), + PubKey: cmtVal.PubKey, + Power: cmtVal.Power, + Name: cmtVal.Name, + } + + vals = append(vals, val) + } + + newAg = types.AppGenesis{ + AppName: ag.AppName, + AppVersion: ag.AppVersion, + GenesisTime: ag.GenesisTime, + ChainID: ag.ChainID, + InitialHeight: ag.InitialHeight, + AppHash: ag.AppHash, + AppState: ag.AppState, + Consensus: &types.ConsensusGenesis{ + Validators: vals, + Params: ag.Consensus.Params, + }, + } + + return &newAg, nil + } + + err = fmt.Errorf("error unmarshalling legacy AppGenesis: %w", err) + if _, serr := rs.Seek(0, io.SeekStart); serr != nil { + err = errors.Join(err, fmt.Errorf("error seeking back to the front: %w", serr)) + return nil, err + } + } + + jsonBlob, ioerr := io.ReadAll(m.reader) + if ioerr != nil { + err = errors.Join(err, fmt.Errorf("failed to read file completely: %w", ioerr)) + return nil, err + } + + // fallback to comet genesis parsing + var ctmGenesis cmttypes.GenesisDoc + if uerr := cmtjson.Unmarshal(jsonBlob, &ctmGenesis); uerr != nil { + err = errors.Join(err, fmt.Errorf("failed fallback to CometBFT GenDoc: %w", uerr)) + return nil, err + } + + vals := []sdk.GenesisValidator{} + for _, cmtVal := range ctmGenesis.Validators { + val := sdk.GenesisValidator{ + Address: cmtVal.Address.Bytes(), + PubKey: cmtVal.PubKey, + Power: cmtVal.Power, + Name: cmtVal.Name, + } + + vals = append(vals, val) + } + + newAg = types.AppGenesis{ + AppName: version.AppName, + // AppVersion is not filled as we do not know it from a CometBFT genesis + GenesisTime: ctmGenesis.GenesisTime, + ChainID: ctmGenesis.ChainID, + InitialHeight: ctmGenesis.InitialHeight, + AppHash: ctmGenesis.AppHash, + AppState: ctmGenesis.AppState, + Consensus: &types.ConsensusGenesis{ + Validators: vals, + Params: ctmGenesis.ConsensusParams, + }, + } + + return &newAg, nil +} + +// CometBFT Genesis Handling for JSON, +// this is necessary for json unmarshaling of legacyConsensusGenesis +func (cs *legacyConsensusGenesis) MarshalJSON() ([]byte, error) { + type Alias legacyConsensusGenesis + return cmtjson.Marshal(&Alias{ + Validators: cs.Validators, + Params: cs.Params, + }) +} + +func (cs *legacyConsensusGenesis) UnmarshalJSON(b []byte) error { + type Alias legacyConsensusGenesis + + result := Alias{} + if err := cmtjson.Unmarshal(b, &result); err != nil { + return err + } + + cs.Params = result.Params + cs.Validators = result.Validators + + return nil +} + +// since we only need migrate the consensus validators content so there is no +// exported state migration. +func Migrate(appState types.AppMap, _ client.Context) (types.AppMap, error) { + return appState, nil +} diff --git a/x/genutil/migration/migration_test.go b/x/genutil/migration/v052/migration_test.go similarity index 79% rename from x/genutil/migration/migration_test.go rename to x/genutil/migration/v052/migration_test.go index 57ec44f0786d..78c91caa6e26 100644 --- a/x/genutil/migration/migration_test.go +++ b/x/genutil/migration/v052/migration_test.go @@ -20,7 +20,7 @@ func TestMigration(t *testing.T) { err := os.RemoveAll(tempDir) require.NoError(t, err) - migrator, err := NewMigrator(oldGenFilePath, tempDir) + migrator, err := NewMigrator(oldGenFilePath) require.NoError(t, err) // should not be able to get app genesis from new genesis file @@ -28,9 +28,13 @@ func TestMigration(t *testing.T) { _, err = types.AppGenesisFromFile(oldGenFilePath) require.ErrorContains(t, err, "error unmarshalling AppGenesis: decoding bech32 failed") - err = migrator.MigrateGenesisFile() + newAppGenesis, err := migrator.MigrateGenesisFile() + require.NoError(t, err) + // save the new app genesis to new temp dir + err = newAppGenesis.SaveAs(tempDir) require.NoError(t, err) + // read the old app genesis to compare with the new app genesis var oldAppGenesis legacyAppGenesis r, err := os.Open(oldGenFilePath) require.NoError(t, err) @@ -38,7 +42,7 @@ func TestMigration(t *testing.T) { require.NoError(t, err) // should be able to get app genesis from new genesis file - newAppGenesis, err := types.AppGenesisFromFile(tempDir) + newAppGenesis, err = types.AppGenesisFromFile(tempDir) require.NotNil(t, newAppGenesis) require.NotNil(t, newAppGenesis.Consensus) require.True(t, bytes.Equal(oldAppGenesis.AppHash, newAppGenesis.AppHash)) diff --git a/x/genutil/migration/testdata/old_app_genesis.json b/x/genutil/migration/v052/testdata/old_app_genesis.json similarity index 100% rename from x/genutil/migration/testdata/old_app_genesis.json rename to x/genutil/migration/v052/testdata/old_app_genesis.json From ddf31491c82ef6aae19ec0c6e6cdff3b4242f2d3 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 23 Sep 2024 13:15:11 +0700 Subject: [PATCH 17/35] remove migrator --- x/genutil/client/cli/migrate.go | 7 +--- x/genutil/migration/v052/migration.go | 37 +++++++++++----------- x/genutil/migration/v052/migration_test.go | 5 +-- 3 files changed, 21 insertions(+), 28 deletions(-) diff --git a/x/genutil/client/cli/migrate.go b/x/genutil/client/cli/migrate.go index feb4a75e8d37..a421b53f2615 100644 --- a/x/genutil/client/cli/migrate.go +++ b/x/genutil/client/cli/migrate.go @@ -66,12 +66,7 @@ func MigrateHandler(cmd *cobra.Command, args []string, migrations types.Migratio var appGenesis *types.AppGenesis var err error if target == "v0.52" { - v52Migrator, err := v052.NewMigrator(importGenesis) - if err != nil { - return err - } - - appGenesis, err = v52Migrator.MigrateGenesisFile() + appGenesis, err = v052.MigrateGenesisFile(importGenesis) if err != nil { return err } diff --git a/x/genutil/migration/v052/migration.go b/x/genutil/migration/v052/migration.go index 5aff80974eab..dbad7cec0bec 100644 --- a/x/genutil/migration/v052/migration.go +++ b/x/genutil/migration/v052/migration.go @@ -18,12 +18,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/genutil/types" ) -type Migrator struct { - // genesis file path - filePath string - reader io.Reader -} - type legacyAppGenesis struct { AppName string `json:"app_name"` AppVersion string `json:"app_version"` @@ -40,31 +34,39 @@ type legacyConsensusGenesis struct { Params *cmttypes.ConsensusParams `json:"params,omitempty"` } -func NewMigrator(filePath string) (*Migrator, error) { - file, err := os.Open(filepath.Clean(filePath)) +func MigrateGenesisFile(oldGenFile string) (*types.AppGenesis, error) { + file, err := os.Open(filepath.Clean(oldGenFile)) if err != nil { return nil, err } - return &Migrator{ - reader: file, - }, nil + appGenesis, err := migrateGenesisValidator(file) + ferr := file.Close() + if err != nil { + return nil, fmt.Errorf("failed to read genesis from file %s: %w", oldGenFile, err) + } + + if ferr != nil { + return nil, ferr + } + + return appGenesis, nil } -// MigrateGenesisFile migrate current genesis file content to match of the +// migrateGenesisValidator migrate current genesis file genesis validator to match of the // new genesis validator type. -func (m Migrator) MigrateGenesisFile() (*types.AppGenesis, error) { +func migrateGenesisValidator(r io.Reader) (*types.AppGenesis, error) { var newAg types.AppGenesis var ag legacyAppGenesis var err error - if rs, ok := m.reader.(io.ReadSeeker); ok { + if rs, ok := r.(io.ReadSeeker); ok { err = json.NewDecoder(rs).Decode(&ag) if err == nil { vals := []sdk.GenesisValidator{} for _, cmtVal := range ag.Consensus.Validators { val := sdk.GenesisValidator{ - Address: sdk.ConsAddress(cmtVal.Address).Bytes(), + Address: cmtVal.Address.Bytes(), PubKey: cmtVal.PubKey, Power: cmtVal.Power, Name: cmtVal.Name, @@ -97,7 +99,7 @@ func (m Migrator) MigrateGenesisFile() (*types.AppGenesis, error) { } } - jsonBlob, ioerr := io.ReadAll(m.reader) + jsonBlob, ioerr := io.ReadAll(r) if ioerr != nil { err = errors.Join(err, fmt.Errorf("failed to read file completely: %w", ioerr)) return nil, err @@ -123,8 +125,7 @@ func (m Migrator) MigrateGenesisFile() (*types.AppGenesis, error) { } newAg = types.AppGenesis{ - AppName: version.AppName, - // AppVersion is not filled as we do not know it from a CometBFT genesis + AppName: version.AppName, GenesisTime: ctmGenesis.GenesisTime, ChainID: ctmGenesis.ChainID, InitialHeight: ctmGenesis.InitialHeight, diff --git a/x/genutil/migration/v052/migration_test.go b/x/genutil/migration/v052/migration_test.go index 78c91caa6e26..cf992b3f0542 100644 --- a/x/genutil/migration/v052/migration_test.go +++ b/x/genutil/migration/v052/migration_test.go @@ -20,15 +20,12 @@ func TestMigration(t *testing.T) { err := os.RemoveAll(tempDir) require.NoError(t, err) - migrator, err := NewMigrator(oldGenFilePath) - require.NoError(t, err) - // should not be able to get app genesis from new genesis file // since validators address are still in hex string and not cons address _, err = types.AppGenesisFromFile(oldGenFilePath) require.ErrorContains(t, err, "error unmarshalling AppGenesis: decoding bech32 failed") - newAppGenesis, err := migrator.MigrateGenesisFile() + newAppGenesis, err := MigrateGenesisFile(oldGenFilePath) require.NoError(t, err) // save the new app genesis to new temp dir err = newAppGenesis.SaveAs(tempDir) From 7c0ce57e2eb61292307a9c01d6fe440413f480ec Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 23 Sep 2024 13:17:41 +0700 Subject: [PATCH 18/35] rename --- x/genutil/migration/v052/{migration.go => migrate.go} | 2 +- x/genutil/migration/v052/{migration_test.go => migrate_test.go} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename x/genutil/migration/v052/{migration.go => migrate.go} (99%) rename x/genutil/migration/v052/{migration_test.go => migrate_test.go} (98%) diff --git a/x/genutil/migration/v052/migration.go b/x/genutil/migration/v052/migrate.go similarity index 99% rename from x/genutil/migration/v052/migration.go rename to x/genutil/migration/v052/migrate.go index dbad7cec0bec..2dc97f988d36 100644 --- a/x/genutil/migration/v052/migration.go +++ b/x/genutil/migration/v052/migrate.go @@ -1,4 +1,4 @@ -package migration +package migrate import ( "encoding/json" diff --git a/x/genutil/migration/v052/migration_test.go b/x/genutil/migration/v052/migrate_test.go similarity index 98% rename from x/genutil/migration/v052/migration_test.go rename to x/genutil/migration/v052/migrate_test.go index cf992b3f0542..41f942f546c2 100644 --- a/x/genutil/migration/v052/migration_test.go +++ b/x/genutil/migration/v052/migrate_test.go @@ -1,4 +1,4 @@ -package migration +package migrate import ( "bytes" From 641164f70bda4465951ee70241e27cd5a0face53 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 23 Sep 2024 13:24:23 +0700 Subject: [PATCH 19/35] remove duplicated code --- x/genutil/client/cli/migrate.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/x/genutil/client/cli/migrate.go b/x/genutil/client/cli/migrate.go index a421b53f2615..9f4b6c8f5204 100644 --- a/x/genutil/client/cli/migrate.go +++ b/x/genutil/client/cli/migrate.go @@ -67,14 +67,11 @@ func MigrateHandler(cmd *cobra.Command, args []string, migrations types.Migratio var err error if target == "v0.52" { appGenesis, err = v052.MigrateGenesisFile(importGenesis) - if err != nil { - return err - } } else { appGenesis, err = types.AppGenesisFromFile(importGenesis) - if err != nil { - return err - } + } + if err != nil { + return err } if err := appGenesis.ValidateAndComplete(); err != nil { From bbb3b01776284140fc274329e9a8b50cffa745f6 Mon Sep 17 00:00:00 2001 From: son trinh Date: Mon, 23 Sep 2024 13:26:24 +0700 Subject: [PATCH 20/35] Update x/genutil/migration/v052/migrate_test.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- x/genutil/migration/v052/migrate_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/x/genutil/migration/v052/migrate_test.go b/x/genutil/migration/v052/migrate_test.go index 41f942f546c2..af28d8eb30af 100644 --- a/x/genutil/migration/v052/migrate_test.go +++ b/x/genutil/migration/v052/migrate_test.go @@ -44,6 +44,7 @@ func TestMigration(t *testing.T) { require.NotNil(t, newAppGenesis.Consensus) require.True(t, bytes.Equal(oldAppGenesis.AppHash, newAppGenesis.AppHash)) require.True(t, bytes.Equal(oldAppGenesis.Consensus.Validators[0].Address.Bytes(), newAppGenesis.Consensus.Validators[0].Address.Bytes())) + require.Equal(t, len(oldAppGenesis.Consensus.Validators), len(newAppGenesis.Consensus.Validators), "Number of validators should remain the same after migration") require.NoError(t, err) } From 5d02a84a409681d624ebc733aa0eba9693c86f6a Mon Sep 17 00:00:00 2001 From: son trinh Date: Mon, 23 Sep 2024 13:27:10 +0700 Subject: [PATCH 21/35] Update x/genutil/migration/v052/migrate.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- x/genutil/migration/v052/migrate.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/x/genutil/migration/v052/migrate.go b/x/genutil/migration/v052/migrate.go index 2dc97f988d36..211fec661147 100644 --- a/x/genutil/migration/v052/migrate.go +++ b/x/genutil/migration/v052/migrate.go @@ -39,17 +39,13 @@ func MigrateGenesisFile(oldGenFile string) (*types.AppGenesis, error) { if err != nil { return nil, err } + defer file.Close() appGenesis, err := migrateGenesisValidator(file) - ferr := file.Close() if err != nil { return nil, fmt.Errorf("failed to read genesis from file %s: %w", oldGenFile, err) } - if ferr != nil { - return nil, ferr - } - return appGenesis, nil } From e9e27f6b4fb8900a9dfb3ff49af800ef6e91a36f Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 23 Sep 2024 13:30:46 +0700 Subject: [PATCH 22/35] address comment --- x/genutil/migration/v052/migrate.go | 41 +++++++++++------------------ 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/x/genutil/migration/v052/migrate.go b/x/genutil/migration/v052/migrate.go index 2dc97f988d36..617b337a8b90 100644 --- a/x/genutil/migration/v052/migrate.go +++ b/x/genutil/migration/v052/migrate.go @@ -63,18 +63,6 @@ func migrateGenesisValidator(r io.Reader) (*types.AppGenesis, error) { if rs, ok := r.(io.ReadSeeker); ok { err = json.NewDecoder(rs).Decode(&ag) if err == nil { - vals := []sdk.GenesisValidator{} - for _, cmtVal := range ag.Consensus.Validators { - val := sdk.GenesisValidator{ - Address: cmtVal.Address.Bytes(), - PubKey: cmtVal.PubKey, - Power: cmtVal.Power, - Name: cmtVal.Name, - } - - vals = append(vals, val) - } - newAg = types.AppGenesis{ AppName: ag.AppName, AppVersion: ag.AppVersion, @@ -84,7 +72,7 @@ func migrateGenesisValidator(r io.Reader) (*types.AppGenesis, error) { AppHash: ag.AppHash, AppState: ag.AppState, Consensus: &types.ConsensusGenesis{ - Validators: vals, + Validators: convertValidators(ag.Consensus.Validators), Params: ag.Consensus.Params, }, } @@ -112,18 +100,6 @@ func migrateGenesisValidator(r io.Reader) (*types.AppGenesis, error) { return nil, err } - vals := []sdk.GenesisValidator{} - for _, cmtVal := range ctmGenesis.Validators { - val := sdk.GenesisValidator{ - Address: cmtVal.Address.Bytes(), - PubKey: cmtVal.PubKey, - Power: cmtVal.Power, - Name: cmtVal.Name, - } - - vals = append(vals, val) - } - newAg = types.AppGenesis{ AppName: version.AppName, GenesisTime: ctmGenesis.GenesisTime, @@ -132,7 +108,7 @@ func migrateGenesisValidator(r io.Reader) (*types.AppGenesis, error) { AppHash: ctmGenesis.AppHash, AppState: ctmGenesis.AppState, Consensus: &types.ConsensusGenesis{ - Validators: vals, + Validators: convertValidators(ctmGenesis.Validators), Params: ctmGenesis.ConsensusParams, }, } @@ -140,6 +116,19 @@ func migrateGenesisValidator(r io.Reader) (*types.AppGenesis, error) { return &newAg, nil } +func convertValidators(cmtVals []cmttypes.GenesisValidator) []sdk.GenesisValidator { + vals := make([]sdk.GenesisValidator, len(cmtVals)) + for i, cmtVal := range cmtVals { + vals[i] = sdk.GenesisValidator{ + Address: cmtVal.Address.Bytes(), + PubKey: cmtVal.PubKey, + Power: cmtVal.Power, + Name: cmtVal.Name, + } + } + return vals +} + // CometBFT Genesis Handling for JSON, // this is necessary for json unmarshaling of legacyConsensusGenesis func (cs *legacyConsensusGenesis) MarshalJSON() ([]byte, error) { From a0d41c94ef32061df64fe9671b93eda1d4ca4677 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 23 Sep 2024 14:03:55 +0700 Subject: [PATCH 23/35] populate simapp v2 export --- simapp/v2/app_di.go | 3 +++ simapp/v2/export.go | 11 +++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/simapp/v2/app_di.go b/simapp/v2/app_di.go index 25f62b8f3ca5..757366e9f2d2 100644 --- a/simapp/v2/app_di.go +++ b/simapp/v2/app_di.go @@ -13,6 +13,7 @@ import ( "cosmossdk.io/log" "cosmossdk.io/runtime/v2" "cosmossdk.io/store/v2/root" + stakingkeeper "cosmossdk.io/x/staking/keeper" upgradekeeper "cosmossdk.io/x/upgrade/keeper" "github.com/cosmos/cosmos-sdk/client" @@ -38,6 +39,7 @@ type SimApp[T transaction.Tx] struct { // required keepers during wiring // others keepers are all in the app UpgradeKeeper *upgradekeeper.Keeper + StakingKeeper *stakingkeeper.Keeper } func init() { @@ -134,6 +136,7 @@ func NewSimApp[T transaction.Tx]( &app.txConfig, &app.interfaceRegistry, &app.UpgradeKeeper, + &app.StakingKeeper, ); err != nil { panic(err) } diff --git a/simapp/v2/export.go b/simapp/v2/export.go index 5a1757b16535..dfae34339b6f 100644 --- a/simapp/v2/export.go +++ b/simapp/v2/export.go @@ -3,6 +3,7 @@ package simapp import ( "context" + "cosmossdk.io/x/staking" v2 "github.com/cosmos/cosmos-sdk/x/genutil/v2" ) @@ -22,8 +23,14 @@ func (app *SimApp[T]) ExportAppStateAndValidators(jailAllowedAddrs []string) (v2 return v2.ExportedApp{}, err } + validators, err := staking.WriteValidators(ctx, app.StakingKeeper) + if err != nil { + return v2.ExportedApp{}, err + } + return v2.ExportedApp{ - AppState: genesis, - Height: int64(latestHeight), + AppState: genesis, + Height: int64(latestHeight), + Validators: validators, }, nil } From 7075a3d68f6bdf651ad298970cf9936c30e20c74 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 23 Sep 2024 14:11:12 +0700 Subject: [PATCH 24/35] lint --- simapp/v2/export.go | 1 + 1 file changed, 1 insertion(+) diff --git a/simapp/v2/export.go b/simapp/v2/export.go index dfae34339b6f..5ee26f1feef1 100644 --- a/simapp/v2/export.go +++ b/simapp/v2/export.go @@ -4,6 +4,7 @@ import ( "context" "cosmossdk.io/x/staking" + v2 "github.com/cosmos/cosmos-sdk/x/genutil/v2" ) From d1f59d2265fe32b5c834ddf775fca0f18521292c Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Tue, 24 Sep 2024 09:59:01 +0700 Subject: [PATCH 25/35] minor --- simapp/v2/export.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/simapp/v2/export.go b/simapp/v2/export.go index 5ee26f1feef1..1bb61e874c13 100644 --- a/simapp/v2/export.go +++ b/simapp/v2/export.go @@ -25,13 +25,10 @@ func (app *SimApp[T]) ExportAppStateAndValidators(jailAllowedAddrs []string) (v2 } validators, err := staking.WriteValidators(ctx, app.StakingKeeper) - if err != nil { - return v2.ExportedApp{}, err - } return v2.ExportedApp{ AppState: genesis, Height: int64(latestHeight), Validators: validators, - }, nil + }, err } From b5abb0907135b3a926565b32acabd7706e912de4 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Wed, 25 Sep 2024 05:31:29 +0700 Subject: [PATCH 26/35] address comment --- crypto/codec/pubkey.go | 49 ++++++++++++++++++++++++++++++++++++++++++ crypto/types/types.go | 1 + x/staking/genesis.go | 8 +------ 3 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 crypto/codec/pubkey.go diff --git a/crypto/codec/pubkey.go b/crypto/codec/pubkey.go new file mode 100644 index 000000000000..5b9462645271 --- /dev/null +++ b/crypto/codec/pubkey.go @@ -0,0 +1,49 @@ +package codec + +import ( + "cosmossdk.io/errors" + + bls12_381 "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" +) + +func PubKeyToProto(pk cryptotypes.JSONPubKey) (cryptotypes.PubKey, error) { + switch pk := pk.(type) { + case *ed25519.PubKey: + return &ed25519.PubKey{ + Key: pk.Bytes(), + }, nil + case *secp256k1.PubKey: + return &secp256k1.PubKey{ + Key: pk.Bytes(), + }, nil + case *bls12_381.PubKey: + return &bls12_381.PubKey{ + Key: pk.Bytes(), + }, nil + default: + return nil, errors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v to proto public key", pk) + } +} + +func PubKeyFromProto(pk cryptotypes.PubKey) (cryptotypes.JSONPubKey, error) { + switch pk := pk.(type) { + case *ed25519.PubKey: + return &ed25519.PubKey{ + Key: pk.Bytes(), + }, nil + case *secp256k1.PubKey: + return &secp256k1.PubKey{ + Key: pk.Bytes(), + }, nil + case *bls12_381.PubKey: + return &bls12_381.PubKey{ + Key: pk.Bytes(), + }, nil + default: + return nil, errors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v from proto public key", pk) + } +} diff --git a/crypto/types/types.go b/crypto/types/types.go index e50e552dd80a..cac1d2c95b92 100644 --- a/crypto/types/types.go +++ b/crypto/types/types.go @@ -6,6 +6,7 @@ import ( ) // JSONPubKey defines a public key that are parse from JSON file. +// convert PubKey to JSONPubKey needs a in between step type JSONPubKey interface { Address() Address Bytes() []byte diff --git a/x/staking/genesis.go b/x/staking/genesis.go index 285a9c56b66c..ab6f64614988 100644 --- a/x/staking/genesis.go +++ b/x/staking/genesis.go @@ -9,7 +9,6 @@ import ( "cosmossdk.io/x/staking/keeper" "cosmossdk.io/x/staking/types" - cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -27,14 +26,9 @@ func WriteValidators(ctx context.Context, keeper *keeper.Keeper) (vals []sdk.Gen return true, err } - cmtPk, err := cryptocodec.ToCmtPubKeyInterface(pk) - if err != nil { - return true, err - } - vals = append(vals, sdk.GenesisValidator{ Address: sdk.ConsAddress(pk.Address()).Bytes(), - PubKey: cmtPk, + PubKey: pk, Power: validator.GetConsensusPower(keeper.PowerReduction(ctx)), Name: validator.GetMoniker(), }) From 29156ed157793c5741e445db392cb1cd947fc2da Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Thu, 26 Sep 2024 14:30:44 +0700 Subject: [PATCH 27/35] remove encoding --- crypto/codec/pubkey.go | 49 ------------------------------------------ 1 file changed, 49 deletions(-) delete mode 100644 crypto/codec/pubkey.go diff --git a/crypto/codec/pubkey.go b/crypto/codec/pubkey.go deleted file mode 100644 index 5b9462645271..000000000000 --- a/crypto/codec/pubkey.go +++ /dev/null @@ -1,49 +0,0 @@ -package codec - -import ( - "cosmossdk.io/errors" - - bls12_381 "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381" - "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" - "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" -) - -func PubKeyToProto(pk cryptotypes.JSONPubKey) (cryptotypes.PubKey, error) { - switch pk := pk.(type) { - case *ed25519.PubKey: - return &ed25519.PubKey{ - Key: pk.Bytes(), - }, nil - case *secp256k1.PubKey: - return &secp256k1.PubKey{ - Key: pk.Bytes(), - }, nil - case *bls12_381.PubKey: - return &bls12_381.PubKey{ - Key: pk.Bytes(), - }, nil - default: - return nil, errors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v to proto public key", pk) - } -} - -func PubKeyFromProto(pk cryptotypes.PubKey) (cryptotypes.JSONPubKey, error) { - switch pk := pk.(type) { - case *ed25519.PubKey: - return &ed25519.PubKey{ - Key: pk.Bytes(), - }, nil - case *secp256k1.PubKey: - return &secp256k1.PubKey{ - Key: pk.Bytes(), - }, nil - case *bls12_381.PubKey: - return &bls12_381.PubKey{ - Key: pk.Bytes(), - }, nil - default: - return nil, errors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v from proto public key", pk) - } -} From bb2d7f1da20e9d13402078c085df964cdb91422c Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 30 Sep 2024 17:18:30 +0700 Subject: [PATCH 28/35] fix test --- crypto/codec/pubkey.go | 53 ++++++++++++++++++++++++ crypto/keys/jsonkey.go | 41 ++++++++++++++++++ crypto/types/types.go | 9 ---- runtime/v2/app.go | 9 ++++ simapp/v2/export.go | 37 +++++++++++++++-- types/staking.go | 9 ++-- x/genutil/migration/v052/migrate.go | 27 +++++++++--- x/genutil/migration/v052/migrate_test.go | 1 + x/genutil/types/genesis.go | 23 ++++++++-- x/staking/genesis.go | 7 +++- 10 files changed, 191 insertions(+), 25 deletions(-) create mode 100644 crypto/codec/pubkey.go create mode 100644 crypto/keys/jsonkey.go diff --git a/crypto/codec/pubkey.go b/crypto/codec/pubkey.go new file mode 100644 index 000000000000..98d09f9e76c1 --- /dev/null +++ b/crypto/codec/pubkey.go @@ -0,0 +1,53 @@ +package codec + +import ( + "cosmossdk.io/errors" + + cryptokeys "github.com/cosmos/cosmos-sdk/crypto/keys" + bls12_381 "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" +) + +func PubKeyToProto(pk cryptokeys.JSONPubkey) (cryptotypes.PubKey, error) { + switch pk.KeyType { + case ed25519.PubKeyName: + return &ed25519.PubKey{ + Key: pk.Value, + }, nil + case secp256k1.PubKeyName: + return &secp256k1.PubKey{ + Key: pk.Value, + }, nil + case bls12_381.PubKeyName: + return &bls12_381.PubKey{ + Key: pk.Value, + }, nil + default: + return nil, errors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v to proto public key", pk) + } +} + +func PubKeyFromProto(pk cryptotypes.PubKey) (cryptokeys.JSONPubkey, error) { + switch pk := pk.(type) { + case *ed25519.PubKey: + return cryptokeys.JSONPubkey{ + KeyType: ed25519.PubKeyName, + Value: pk.Bytes(), + }, nil + case *secp256k1.PubKey: + return cryptokeys.JSONPubkey{ + KeyType: ed25519.PubKeyName, + Value: pk.Bytes(), + }, nil + case *bls12_381.PubKey: + return cryptokeys.JSONPubkey{ + KeyType: ed25519.PubKeyName, + Value: pk.Bytes(), + }, nil + default: + return cryptokeys.JSONPubkey{}, errors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v from proto public key", pk) + } +} diff --git a/crypto/keys/jsonkey.go b/crypto/keys/jsonkey.go new file mode 100644 index 000000000000..8edbd91e7246 --- /dev/null +++ b/crypto/keys/jsonkey.go @@ -0,0 +1,41 @@ +package keys + +import ( + "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" + "github.com/cosmos/cosmos-sdk/crypto/types" +) + +// JSONPubKey defines a public key that are parse from JSON file. +// convert PubKey to JSONPubKey needs a in between step +type JSONPubkey struct { + KeyType string `json:"type"` + Value []byte `json:"value"` +} + +func (pk JSONPubkey) Address() types.Address { + switch pk.KeyType { + case ed25519.PubKeyName: + ed25519 := ed25519.PubKey{ + Key: pk.Value, + } + return ed25519.Address() + case secp256k1.PubKeyName: + secp256k1 := secp256k1.PubKey{ + Key: pk.Value, + } + return secp256k1.Address() + case bls12_381.PubKeyName: + bls12_381 := bls12_381.PubKey{ + Key: pk.Value, + } + return bls12_381.Address() + default: + return nil + } +} + +func (pk JSONPubkey) Bytes() []byte { + return pk.Value +} diff --git a/crypto/types/types.go b/crypto/types/types.go index cac1d2c95b92..7f01b6078cf3 100644 --- a/crypto/types/types.go +++ b/crypto/types/types.go @@ -5,15 +5,6 @@ import ( "github.com/cosmos/gogoproto/proto" ) -// JSONPubKey defines a public key that are parse from JSON file. -// convert PubKey to JSONPubKey needs a in between step -type JSONPubKey interface { - Address() Address - Bytes() []byte - VerifySignature(msg, sig []byte) bool - Type() string -} - // PubKey defines a public key and extends proto.Message. type PubKey interface { proto.Message diff --git a/runtime/v2/app.go b/runtime/v2/app.go index b7104f9e4774..88c4928218e8 100644 --- a/runtime/v2/app.go +++ b/runtime/v2/app.go @@ -1,6 +1,7 @@ package runtime import ( + "context" "encoding/json" "errors" "slices" @@ -123,3 +124,11 @@ func (a *App[T]) GetAppManager() *appmanager.AppManager[T] { func (a *App[T]) GetGPRCMethodsToMessageMap() map[string]func() gogoproto.Message { return a.GRPCMethodsToMessageMap } + +func (a *App[T]) Query(ctx context.Context, gasLimit, version uint64, req transaction.Msg) (transaction.Msg, error) { + state, err := a.db.StateAt(version) + if err != nil { + return nil, err + } + return a.stf.Query(ctx, state, gasLimit, req) +} diff --git a/simapp/v2/export.go b/simapp/v2/export.go index 1bb61e874c13..e626fde9da20 100644 --- a/simapp/v2/export.go +++ b/simapp/v2/export.go @@ -2,9 +2,12 @@ package simapp import ( "context" + "fmt" - "cosmossdk.io/x/staking" + stakingtypes "cosmossdk.io/x/staking/types" + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + sdk "github.com/cosmos/cosmos-sdk/types" v2 "github.com/cosmos/cosmos-sdk/x/genutil/v2" ) @@ -24,11 +27,39 @@ func (app *SimApp[T]) ExportAppStateAndValidators(jailAllowedAddrs []string) (v2 return v2.ExportedApp{}, err } - validators, err := staking.WriteValidators(ctx, app.StakingKeeper) + // get the current bonded validators + resp, err := app.Query(ctx, 0, latestHeight, &stakingtypes.QueryValidatorsRequest{ + Status: stakingtypes.BondStatusBonded, + }) + + vals, ok := resp.(*stakingtypes.QueryValidatorsResponse) + if !ok { + return v2.ExportedApp{}, fmt.Errorf("invalid response, expected QueryValidatorsResponse") + } + + // convert to genesis validator + var genesisVals []sdk.GenesisValidator + for _, val := range vals.Validators { + pk, err := val.ConsPubKey() + if err != nil { + return v2.ExportedApp{}, err + } + jsonPk, err := cryptocodec.PubKeyFromProto(pk) + if err != nil { + return v2.ExportedApp{}, err + } + + genesisVals = append(genesisVals, sdk.GenesisValidator{ + Address: sdk.ConsAddress(pk.Address()).Bytes(), + PubKey: jsonPk, + Power: val.GetConsensusPower(app.StakingKeeper.PowerReduction(ctx)), + Name: val.Description.Moniker, + }) + } return v2.ExportedApp{ AppState: genesis, Height: int64(latestHeight), - Validators: validators, + Validators: genesisVals, }, err } diff --git a/types/staking.go b/types/staking.go index 62401d5eb57c..b33e32ae9d04 100644 --- a/types/staking.go +++ b/types/staking.go @@ -3,6 +3,7 @@ package types import ( "cosmossdk.io/math" + cryptokeys "github.com/cosmos/cosmos-sdk/crypto/keys" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" ) @@ -101,8 +102,8 @@ type ValidatorI interface { // GenesisValidator is an initial validator. type GenesisValidator struct { - Address ConsAddress `json:"address"` - PubKey cryptotypes.JSONPubKey `json:"pub_key"` - Power int64 `json:"power"` - Name string `json:"name"` + Address ConsAddress `json:"address"` + PubKey cryptokeys.JSONPubkey `json:"pub_key"` + Power int64 `json:"power"` + Name string `json:"name"` } diff --git a/x/genutil/migration/v052/migrate.go b/x/genutil/migration/v052/migrate.go index a3cb8e80ac31..350240850cff 100644 --- a/x/genutil/migration/v052/migrate.go +++ b/x/genutil/migration/v052/migrate.go @@ -13,6 +13,7 @@ import ( cmttypes "github.com/cometbft/cometbft/types" "github.com/cosmos/cosmos-sdk/client" + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/version" "github.com/cosmos/cosmos-sdk/x/genutil/types" @@ -59,6 +60,10 @@ func migrateGenesisValidator(r io.Reader) (*types.AppGenesis, error) { if rs, ok := r.(io.ReadSeeker); ok { err = json.NewDecoder(rs).Decode(&ag) if err == nil { + vals, err := convertValidators(ag.Consensus.Validators) + if err != nil { + return nil, err + } newAg = types.AppGenesis{ AppName: ag.AppName, AppVersion: ag.AppVersion, @@ -68,7 +73,7 @@ func migrateGenesisValidator(r io.Reader) (*types.AppGenesis, error) { AppHash: ag.AppHash, AppState: ag.AppState, Consensus: &types.ConsensusGenesis{ - Validators: convertValidators(ag.Consensus.Validators), + Validators: vals, Params: ag.Consensus.Params, }, } @@ -96,6 +101,10 @@ func migrateGenesisValidator(r io.Reader) (*types.AppGenesis, error) { return nil, err } + vals, err := convertValidators(ctmGenesis.Validators) + if err != nil { + return nil, err + } newAg = types.AppGenesis{ AppName: version.AppName, GenesisTime: ctmGenesis.GenesisTime, @@ -104,7 +113,7 @@ func migrateGenesisValidator(r io.Reader) (*types.AppGenesis, error) { AppHash: ctmGenesis.AppHash, AppState: ctmGenesis.AppState, Consensus: &types.ConsensusGenesis{ - Validators: convertValidators(ctmGenesis.Validators), + Validators: vals, Params: ctmGenesis.ConsensusParams, }, } @@ -112,17 +121,25 @@ func migrateGenesisValidator(r io.Reader) (*types.AppGenesis, error) { return &newAg, nil } -func convertValidators(cmtVals []cmttypes.GenesisValidator) []sdk.GenesisValidator { +func convertValidators(cmtVals []cmttypes.GenesisValidator) ([]sdk.GenesisValidator, error) { vals := make([]sdk.GenesisValidator, len(cmtVals)) for i, cmtVal := range cmtVals { + pk, err := cryptocodec.FromCmtPubKeyInterface(cmtVal.PubKey) + if err != nil { + return nil, err + } + jsonPk, err := cryptocodec.PubKeyFromProto(pk) + if err != nil { + return nil, err + } vals[i] = sdk.GenesisValidator{ Address: cmtVal.Address.Bytes(), - PubKey: cmtVal.PubKey, + PubKey: jsonPk, Power: cmtVal.Power, Name: cmtVal.Name, } } - return vals + return vals, nil } // CometBFT Genesis Handling for JSON, diff --git a/x/genutil/migration/v052/migrate_test.go b/x/genutil/migration/v052/migrate_test.go index af28d8eb30af..5c38bb3401fa 100644 --- a/x/genutil/migration/v052/migrate_test.go +++ b/x/genutil/migration/v052/migrate_test.go @@ -44,6 +44,7 @@ func TestMigration(t *testing.T) { require.NotNil(t, newAppGenesis.Consensus) require.True(t, bytes.Equal(oldAppGenesis.AppHash, newAppGenesis.AppHash)) require.True(t, bytes.Equal(oldAppGenesis.Consensus.Validators[0].Address.Bytes(), newAppGenesis.Consensus.Validators[0].Address.Bytes())) + require.True(t, bytes.Equal(oldAppGenesis.Consensus.Validators[0].PubKey.Bytes(), newAppGenesis.Consensus.Validators[0].PubKey.Bytes())) require.Equal(t, len(oldAppGenesis.Consensus.Validators), len(newAppGenesis.Consensus.Validators), "Number of validators should remain the same after migration") require.NoError(t, err) diff --git a/x/genutil/types/genesis.go b/x/genutil/types/genesis.go index 8e637aa69463..b8ef6b31e256 100644 --- a/x/genutil/types/genesis.go +++ b/x/genutil/types/genesis.go @@ -14,6 +14,7 @@ import ( cmtjson "github.com/cometbft/cometbft/libs/json" cmttypes "github.com/cometbft/cometbft/types" + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/version" ) @@ -121,9 +122,17 @@ func AppGenesisFromReader(reader io.Reader) (*AppGenesis, error) { vals := []sdk.GenesisValidator{} for _, cmtVal := range ctmGenesis.Validators { + pk, err := cryptocodec.FromCmtPubKeyInterface(cmtVal.PubKey) + if err != nil { + return nil, err + } + jsonPk, err := cryptocodec.PubKeyFromProto(pk) + if err != nil { + return nil, err + } val := sdk.GenesisValidator{ Address: cmtVal.Address.Bytes(), - PubKey: cmtVal.PubKey, + PubKey: jsonPk, Power: cmtVal.Power, Name: cmtVal.Name, } @@ -175,9 +184,17 @@ func AppGenesisFromFile(genFile string) (*AppGenesis, error) { func (ag *AppGenesis) ToGenesisDoc() (*cmttypes.GenesisDoc, error) { cmtValidators := []cmttypes.GenesisValidator{} for _, val := range ag.Consensus.Validators { + pk, err := cryptocodec.PubKeyToProto(val.PubKey) + if err != nil { + return nil, err + } + cmtPk, err := cryptocodec.FromCmtPubKeyInterface(pk) + if err != nil { + return nil, err + } cmtVal := cmttypes.GenesisValidator{ Address: val.Address.Bytes(), - PubKey: val.PubKey, + PubKey: cmtPk, Power: val.Power, Name: val.Name, } @@ -239,7 +256,7 @@ func (cs *ConsensusGenesis) MarshalJSON() ([]byte, error) { func (cs *ConsensusGenesis) UnmarshalJSON(b []byte) error { type Alias ConsensusGenesis - result := Alias{} + var result Alias if err := cmtjson.Unmarshal(b, &result); err != nil { return err } diff --git a/x/staking/genesis.go b/x/staking/genesis.go index ab6f64614988..533560009a17 100644 --- a/x/staking/genesis.go +++ b/x/staking/genesis.go @@ -9,6 +9,7 @@ import ( "cosmossdk.io/x/staking/keeper" "cosmossdk.io/x/staking/types" + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -25,10 +26,14 @@ func WriteValidators(ctx context.Context, keeper *keeper.Keeper) (vals []sdk.Gen returnErr = err return true, err } + jsonPk, err := cryptocodec.PubKeyFromProto(pk) + if err != nil { + return true, err + } vals = append(vals, sdk.GenesisValidator{ Address: sdk.ConsAddress(pk.Address()).Bytes(), - PubKey: pk, + PubKey: jsonPk, Power: validator.GetConsensusPower(keeper.PowerReduction(ctx)), Name: validator.GetMoniker(), }) From b4014f33118f7b7cf44eeafe4f03f97728659518 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 30 Sep 2024 17:39:19 +0700 Subject: [PATCH 29/35] fix test --- x/genutil/types/genesis.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/genutil/types/genesis.go b/x/genutil/types/genesis.go index b8ef6b31e256..023abfba082c 100644 --- a/x/genutil/types/genesis.go +++ b/x/genutil/types/genesis.go @@ -188,7 +188,7 @@ func (ag *AppGenesis) ToGenesisDoc() (*cmttypes.GenesisDoc, error) { if err != nil { return nil, err } - cmtPk, err := cryptocodec.FromCmtPubKeyInterface(pk) + cmtPk, err := cryptocodec.ToCmtPubKeyInterface(pk) if err != nil { return nil, err } From bae84ba35cb988eea22a2d77784d3256ab907ea0 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 30 Sep 2024 17:48:09 +0700 Subject: [PATCH 30/35] minor --- crypto/codec/pubkey.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crypto/codec/pubkey.go b/crypto/codec/pubkey.go index 98d09f9e76c1..c45f258517b0 100644 --- a/crypto/codec/pubkey.go +++ b/crypto/codec/pubkey.go @@ -39,12 +39,12 @@ func PubKeyFromProto(pk cryptotypes.PubKey) (cryptokeys.JSONPubkey, error) { }, nil case *secp256k1.PubKey: return cryptokeys.JSONPubkey{ - KeyType: ed25519.PubKeyName, + KeyType: secp256k1.PubKeyName, Value: pk.Bytes(), }, nil case *bls12_381.PubKey: return cryptokeys.JSONPubkey{ - KeyType: ed25519.PubKeyName, + KeyType: bls12_381.PubKeyName, Value: pk.Bytes(), }, nil default: From 76ab968822dd8df7907d71815f6e3ce2b6d96ca2 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Tue, 1 Oct 2024 01:03:52 +0700 Subject: [PATCH 31/35] address comments --- x/genutil/client/cli/migrate.go | 5 +++-- x/staking/genesis.go | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/x/genutil/client/cli/migrate.go b/x/genutil/client/cli/migrate.go index 9f4b6c8f5204..f868c39632dd 100644 --- a/x/genutil/client/cli/migrate.go +++ b/x/genutil/client/cli/migrate.go @@ -18,10 +18,11 @@ import ( ) const flagGenesisTime = "genesis-time" +const v52 = "v0.52" // MigrationMap is a map of SDK versions to their respective genesis migration functions. var MigrationMap = types.MigrationMap{ - "v0.52": v052.Migrate, + v52: v052.Migrate, } // MigrateGenesisCmd returns a command to execute genesis state migration. @@ -65,7 +66,7 @@ func MigrateHandler(cmd *cobra.Command, args []string, migrations types.Migratio // sdk consensus address. var appGenesis *types.AppGenesis var err error - if target == "v0.52" { + if target == v52 { appGenesis, err = v052.MigrateGenesisFile(importGenesis) } else { appGenesis, err = types.AppGenesisFromFile(importGenesis) diff --git a/x/staking/genesis.go b/x/staking/genesis.go index 533560009a17..938dc4666569 100644 --- a/x/staking/genesis.go +++ b/x/staking/genesis.go @@ -32,7 +32,7 @@ func WriteValidators(ctx context.Context, keeper *keeper.Keeper) (vals []sdk.Gen } vals = append(vals, sdk.GenesisValidator{ - Address: sdk.ConsAddress(pk.Address()).Bytes(), + Address: pk.Address().Bytes(), PubKey: jsonPk, Power: validator.GetConsensusPower(keeper.PowerReduction(ctx)), Name: validator.GetMoniker(), From f0c55099eb5b326119f5d8af742a7bc0b9b37a75 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Wed, 2 Oct 2024 11:35:32 +0700 Subject: [PATCH 32/35] place holder --- simapp/v2/app_di.go | 10 ++- simapp/v2/export.go | 211 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 219 insertions(+), 2 deletions(-) diff --git a/simapp/v2/app_di.go b/simapp/v2/app_di.go index 02d89891686a..02d06622c329 100644 --- a/simapp/v2/app_di.go +++ b/simapp/v2/app_di.go @@ -16,6 +16,8 @@ import ( basedepinject "cosmossdk.io/x/accounts/defaults/base/depinject" lockupdepinject "cosmossdk.io/x/accounts/defaults/lockup/depinject" multisigdepinject "cosmossdk.io/x/accounts/defaults/multisig/depinject" + distrkeeper "cosmossdk.io/x/distribution/keeper" + slashingkeeper "cosmossdk.io/x/slashing/keeper" stakingkeeper "cosmossdk.io/x/staking/keeper" upgradekeeper "cosmossdk.io/x/upgrade/keeper" @@ -41,8 +43,10 @@ type SimApp[T transaction.Tx] struct { // required keepers during wiring // others keepers are all in the app - UpgradeKeeper *upgradekeeper.Keeper - StakingKeeper *stakingkeeper.Keeper + UpgradeKeeper *upgradekeeper.Keeper + StakingKeeper *stakingkeeper.Keeper + DistrKeeper *distrkeeper.Keeper + SlashingKeeper *slashingkeeper.Keeper } func init() { @@ -159,6 +163,8 @@ func NewSimApp[T transaction.Tx]( &app.interfaceRegistry, &app.UpgradeKeeper, &app.StakingKeeper, + &app.DistrKeeper, + &app.SlashingKeeper, ); err != nil { panic(err) } diff --git a/simapp/v2/export.go b/simapp/v2/export.go index e626fde9da20..f082ab4bd7d1 100644 --- a/simapp/v2/export.go +++ b/simapp/v2/export.go @@ -4,6 +4,8 @@ import ( "context" "fmt" + "cosmossdk.io/collections" + slashingtypes "cosmossdk.io/x/slashing/types" stakingtypes "cosmossdk.io/x/staking/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" @@ -63,3 +65,212 @@ func (app *SimApp[T]) ExportAppStateAndValidators(jailAllowedAddrs []string) (v2 Validators: genesisVals, }, err } + +// prepForZeroHeightGenesis prepares for fresh start at zero height +func (app *SimApp[T]) prepForZeroHeightGenesis(ctx context.Context, jailAllowedAddrs []string) { + applyAllowedAddrs := false + + // check if there is a allowed address list + if len(jailAllowedAddrs) > 0 { + applyAllowedAddrs = true + } + + allowedAddrsMap := make(map[string]bool) + + for _, addr := range jailAllowedAddrs { + _, err := app.TxConfig().SigningContext().ValidatorAddressCodec().StringToBytes(addr) + if err != nil { + panic(err) + } + allowedAddrsMap[addr] = true + } + + /* Handle fee distribution state. */ + + // withdraw all validator commission + err := app.StakingKeeper.IterateValidators(ctx, func(_ int64, val sdk.ValidatorI) (stop bool) { + valBz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator()) + if err != nil { + panic(err) + } + _, _ = app.DistrKeeper.WithdrawValidatorCommission(ctx, valBz) + return false + }) + if err != nil { + panic(err) + } + + // withdraw all delegator rewards + dels, err := app.StakingKeeper.GetAllDelegations(ctx) + if err != nil { + panic(err) + } + + for _, delegation := range dels { + valAddr, err := app.TxConfig().SigningContext().ValidatorAddressCodec().StringToBytes(delegation.ValidatorAddress) + if err != nil { + panic(err) + } + + delAddr, err := app.TxConfig().SigningContext().AddressCodec().StringToBytes(delegation.DelegatorAddress) + if err != nil { + panic(err) + } + + _, _ = app.DistrKeeper.WithdrawDelegationRewards(ctx, delAddr, valAddr) + } + + // clear validator slash events + err = app.DistrKeeper.ValidatorSlashEvents.Clear(ctx, nil) + if err != nil { + panic(err) + } + + // clear validator historical rewards + err = app.DistrKeeper.ValidatorHistoricalRewards.Clear(ctx, nil) + if err != nil { + panic(err) + } + + // reinitialize all validators + err = app.StakingKeeper.IterateValidators(ctx, func(_ int64, val sdk.ValidatorI) (stop bool) { + valBz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator()) + if err != nil { + panic(err) + } + // donate any unwithdrawn outstanding reward tokens to the community pool + rewards, err := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, valBz) + if err != nil { + panic(err) + } + feePool, err := app.DistrKeeper.FeePool.Get(ctx) + if err != nil { + panic(err) + } + feePool.DecimalPool = feePool.DecimalPool.Add(rewards...) // distribution will allocate this to the protocolpool eventually + if err := app.DistrKeeper.FeePool.Set(ctx, feePool); err != nil { + panic(err) + } + + if err := app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, valBz); err != nil { + panic(err) + } + return false + }) + if err != nil { + panic(err) + } + + // reinitialize all delegations + for _, del := range dels { + valAddr, err := app.TxConfig().SigningContext().ValidatorAddressCodec().StringToBytes(del.ValidatorAddress) + if err != nil { + panic(err) + } + delAddr, err := app.TxConfig().SigningContext().AddressCodec().StringToBytes(del.DelegatorAddress) + if err != nil { + panic(err) + } + + if err := app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, delAddr, valAddr); err != nil { + // never called as BeforeDelegationCreated always returns nil + panic(fmt.Errorf("error while incrementing period: %w", err)) + } + + if err := app.DistrKeeper.Hooks().AfterDelegationModified(ctx, delAddr, valAddr); err != nil { + // never called as AfterDelegationModified always returns nil + panic(fmt.Errorf("error while creating a new delegation period record: %w", err)) + } + } + + /* Handle staking state. */ + + // iterate through redelegations, reset creation height + err = app.StakingKeeper.IterateRedelegations(ctx, func(_ int64, red stakingtypes.Redelegation) (stop bool) { + for i := range red.Entries { + red.Entries[i].CreationHeight = 0 + } + err = app.StakingKeeper.SetRedelegation(ctx, red) + if err != nil { + panic(err) + } + return false + }) + if err != nil { + panic(err) + } + + // iterate through unbonding delegations, reset creation height + err = app.StakingKeeper.UnbondingDelegations.Walk( + ctx, + nil, + func(_ collections.Pair[[]byte, []byte], ubd stakingtypes.UnbondingDelegation) (stop bool, err error) { + for i := range ubd.Entries { + ubd.Entries[i].CreationHeight = 0 + } + err = app.StakingKeeper.SetUnbondingDelegation(ctx, ubd) + if err != nil { + return true, err + } + return false, err + }, + ) + if err != nil { + panic(err) + } + // Iterate through validators by power descending, reset bond heights, and + // update bond intra-tx counters. + store := ctx.KVStore(app.UnsafeFindStoreKey(stakingtypes.StoreKey)) + iter := storetypes.KVStoreReversePrefixIterator(store, stakingtypes.ValidatorsKey) + counter := int16(0) + + app.StakingKeeper.Validators.Walk(ctx) + + for ; iter.Valid(); iter.Next() { + addr := sdk.ValAddress(stakingtypes.AddressFromValidatorsKey(iter.Key())) + validator, err := app.StakingKeeper.GetValidator(ctx, addr) + if err != nil { + panic("expected validator, not found") + } + + valAddr, err := app.StakingKeeper.ValidatorAddressCodec().BytesToString(addr) + if err != nil { + panic(err) + } + + validator.UnbondingHeight = 0 + if applyAllowedAddrs && !allowedAddrsMap[valAddr] { + validator.Jailed = true + } + + if err = app.StakingKeeper.SetValidator(ctx, validator); err != nil { + panic(err) + } + counter++ + } + + if err := iter.Close(); err != nil { + app.Logger().Error("error while closing the key-value store reverse prefix iterator: ", err) + return + } + + _, err = app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx) + if err != nil { + panic(err) + } + + /* Handle slashing state. */ + + // reset start height on signing infos + err = app.SlashingKeeper.ValidatorSigningInfo.Walk(ctx, nil, func(addr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool, err error) { + info.StartHeight = 0 + err = app.SlashingKeeper.ValidatorSigningInfo.Set(ctx, addr, info) + if err != nil { + return true, err + } + return false, nil + }) + if err != nil { + panic(err) + } +} From 78793615b066df97be0947a9da100ea4a497599c Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Tue, 8 Oct 2024 16:32:30 -0500 Subject: [PATCH 33/35] refactor(genutil): export genesis with val set implementation (#22178) --- runtime/v2/app.go | 14 -------- runtime/v2/builder.go | 2 +- runtime/v2/manager.go | 10 ++++-- runtime/v2/services/genesis.go | 51 +++++++++++++++++++++++----- simapp/v2/app_di.go | 10 +++--- simapp/v2/app_test.go | 5 ++- simapp/v2/export.go | 61 ++++++++++++---------------------- 7 files changed, 80 insertions(+), 73 deletions(-) diff --git a/runtime/v2/app.go b/runtime/v2/app.go index e43fac0a3af6..d5999f3bd99f 100644 --- a/runtime/v2/app.go +++ b/runtime/v2/app.go @@ -1,7 +1,6 @@ package runtime import ( - "context" "encoding/json" runtimev2 "cosmossdk.io/api/cosmos/app/runtime/v2" @@ -90,11 +89,6 @@ func (a *App[T]) Close() error { return nil } -// GetStore returns the app store. -func (a *App[T]) GetStore() Store { - return a.db -} - func (a *App[T]) GetAppManager() *appmanager.AppManager[T] { return a.AppManager } @@ -102,11 +96,3 @@ func (a *App[T]) GetAppManager() *appmanager.AppManager[T] { func (a *App[T]) GetQueryHandlers() map[string]appmodulev2.Handler { return a.QueryHandlers } - -func (a *App[T]) Query(ctx context.Context, gasLimit, version uint64, req transaction.Msg) (transaction.Msg, error) { - state, err := a.db.StateAt(version) - if err != nil { - return nil, err - } - return a.stf.Query(ctx, state, gasLimit, req) -} diff --git a/runtime/v2/builder.go b/runtime/v2/builder.go index e74d6ff34f22..adb177d41eb0 100644 --- a/runtime/v2/builder.go +++ b/runtime/v2/builder.go @@ -144,7 +144,7 @@ func (a *AppBuilder[T]) Build(opts ...AppBuilderOption[T]) (*App[T], error) { return nil, errors.New("cannot init genesis on non-zero state") } genesisCtx := services.NewGenesisContext(a.branch(zeroState)) - genesisState, err := genesisCtx.Run(ctx, func(ctx context.Context) error { + genesisState, err := genesisCtx.Mutate(ctx, func(ctx context.Context) error { err = a.app.moduleManager.InitGenesisJSON(ctx, genesisJSON, txHandler) if err != nil { return fmt.Errorf("failed to init genesis: %w", err) diff --git a/runtime/v2/manager.go b/runtime/v2/manager.go index 01b0c93971ab..2d6515b81225 100644 --- a/runtime/v2/manager.go +++ b/runtime/v2/manager.go @@ -230,15 +230,17 @@ func (m *MM[T]) ExportGenesisForModules( channels[moduleName] = make(chan genesisResult) go func(moduleI ModuleI, ch chan genesisResult) { genesisCtx := services.NewGenesisContext(stateFactory()) - _, _ = genesisCtx.Run(ctx, func(ctx context.Context) error { + err := genesisCtx.Read(ctx, func(ctx context.Context) error { jm, err := moduleI.ExportGenesis(ctx) if err != nil { - ch <- genesisResult{nil, err} return err } ch <- genesisResult{jm, nil} return nil }) + if err != nil { + ch <- genesisResult{nil, err} + } }(moduleI, channels[moduleName]) } @@ -783,7 +785,9 @@ func messagePassingInterceptor(msg transaction.Msg) grpc.UnaryServerInterceptor } // requestFullNameFromMethodDesc returns the fully-qualified name of the request message and response of the provided service's method. -func requestFullNameFromMethodDesc(sd *grpc.ServiceDesc, method grpc.MethodDesc) (protoreflect.FullName, protoreflect.FullName, error) { +func requestFullNameFromMethodDesc(sd *grpc.ServiceDesc, method grpc.MethodDesc) ( + protoreflect.FullName, protoreflect.FullName, error, +) { methodFullName := protoreflect.FullName(fmt.Sprintf("%s.%s", sd.ServiceName, method.MethodName)) desc, err := gogoproto.HybridResolver.FindDescriptorByName(methodFullName) if err != nil { diff --git a/runtime/v2/services/genesis.go b/runtime/v2/services/genesis.go index 79ebd92852f8..5d09e5c78f74 100644 --- a/runtime/v2/services/genesis.go +++ b/runtime/v2/services/genesis.go @@ -11,6 +11,7 @@ import ( var ( _ store.KVStoreService = (*GenesisKVStoreService)(nil) _ header.Service = (*GenesisHeaderService)(nil) + _ store.KVStore = (*readonlyKVStore)(nil) ) type genesisContextKeyType struct{} @@ -21,28 +22,41 @@ var genesisContextKey = genesisContextKeyType{} // it backs the store.KVStoreService and header.Service interface implementations // defined in this file. type genesisContext struct { - state store.WriterMap + state store.ReaderMap } // NewGenesisContext creates a new genesis context. -func NewGenesisContext(state store.WriterMap) genesisContext { +func NewGenesisContext(state store.ReaderMap) genesisContext { return genesisContext{ state: state, } } -// Run runs the provided function within the genesis context and returns an +// Mutate runs the provided function within the genesis context and returns an // updated store.WriterMap containing the state modifications made during InitGenesis. -func (g *genesisContext) Run( +func (g genesisContext) Mutate( ctx context.Context, fn func(ctx context.Context) error, ) (store.WriterMap, error) { + writerMap, ok := g.state.(store.WriterMap) + if !ok { + return nil, fmt.Errorf("mutate requires a store.WriterMap, got a %T", g.state) + } ctx = context.WithValue(ctx, genesisContextKey, g) err := fn(ctx) if err != nil { return nil, err } - return g.state, nil + return writerMap, nil +} + +// Read runs the provided function within the genesis context. +func (g genesisContext) Read( + ctx context.Context, + fn func(ctx context.Context) error, +) error { + ctx = context.WithValue(ctx, genesisContextKey, g) + return fn(ctx) } // GenesisKVStoreService is a store.KVStoreService implementation that is used during @@ -71,15 +85,24 @@ func (g *GenesisKVStoreService) OpenKVStore(ctx context.Context) store.KVStore { if v == nil { return g.executionService.OpenKVStore(ctx) } - genCtx, ok := v.(*genesisContext) + genCtx, ok := v.(genesisContext) if !ok { panic(fmt.Errorf("unexpected genesis context type: %T", v)) } - state, err := genCtx.state.GetWriter(g.actor) + writerMap, ok := genCtx.state.(store.WriterMap) + if ok { + state, err := writerMap.GetWriter(g.actor) + if err != nil { + panic(err) + } + return state + + } + state, err := genCtx.state.GetReader(g.actor) if err != nil { panic(err) } - return state + return readonlyKVStore{state} } // GenesisHeaderService is a header.Service implementation that is used during @@ -105,3 +128,15 @@ func NewGenesisHeaderService(executionService header.Service) *GenesisHeaderServ executionService: executionService, } } + +type readonlyKVStore struct { + store.Reader +} + +func (r readonlyKVStore) Set(key, value []byte) error { + panic("tried to call Set on a readonly store") +} + +func (r readonlyKVStore) Delete(key []byte) error { + panic("tried to call Delete on a readonly store") +} diff --git a/simapp/v2/app_di.go b/simapp/v2/app_di.go index 33ba98229ee8..29f62defcde6 100644 --- a/simapp/v2/app_di.go +++ b/simapp/v2/app_di.go @@ -13,6 +13,7 @@ import ( "cosmossdk.io/log" "cosmossdk.io/runtime/v2" serverstore "cosmossdk.io/server/v2/store" + "cosmossdk.io/store/v2" "cosmossdk.io/store/v2/root" basedepinject "cosmossdk.io/x/accounts/defaults/base/depinject" lockupdepinject "cosmossdk.io/x/accounts/defaults/lockup/depinject" @@ -39,6 +40,7 @@ type SimApp[T transaction.Tx] struct { appCodec codec.Codec txConfig client.TxConfig interfaceRegistry codectypes.InterfaceRegistry + store store.RootStore // required keepers during wiring // others keepers are all in the app @@ -173,7 +175,8 @@ func NewSimApp[T transaction.Tx]( if err != nil { panic(err) } - _, err = storeBuilder.Build(logger, storeConfig) + + app.store, err = storeBuilder.Build(logger, storeConfig) if err != nil { panic(err) } @@ -216,7 +219,6 @@ func (app *SimApp[T]) TxConfig() client.TxConfig { return app.txConfig } -// GetStore gets the app store. -func (app *SimApp[T]) GetStore() any { - return app.App.GetStore() +func (app *SimApp[T]) GetStore() store.RootStore { + return app.store } diff --git a/simapp/v2/app_test.go b/simapp/v2/app_test.go index 6ee3709ee552..8c705565956c 100644 --- a/simapp/v2/app_test.go +++ b/simapp/v2/app_test.go @@ -20,7 +20,6 @@ import ( sdkmath "cosmossdk.io/math" "cosmossdk.io/runtime/v2" serverv2 "cosmossdk.io/server/v2" - comettypes "cosmossdk.io/server/v2/cometbft/types" serverv2store "cosmossdk.io/server/v2/store" "cosmossdk.io/store/v2/db" banktypes "cosmossdk.io/x/bank/types" @@ -75,7 +74,7 @@ func NewTestApp(t *testing.T) (*SimApp[transaction.Tx], context.Context) { genesisBytes, err := json.Marshal(genesis) require.NoError(t, err) - st := app.GetStore().(comettypes.Store) + st := app.GetStore() ci, err := st.LastCommitID() require.NoError(t, err) @@ -111,7 +110,7 @@ func MoveNextBlock(t *testing.T, app *SimApp[transaction.Tx], ctx context.Contex bz := sha256.Sum256([]byte{}) - st := app.GetStore().(comettypes.Store) + st := app.GetStore() ci, err := st.LastCommitID() require.NoError(t, err) diff --git a/simapp/v2/export.go b/simapp/v2/export.go index e626fde9da20..8cd82867f479 100644 --- a/simapp/v2/export.go +++ b/simapp/v2/export.go @@ -2,64 +2,45 @@ package simapp import ( "context" - "fmt" - stakingtypes "cosmossdk.io/x/staking/types" + "cosmossdk.io/runtime/v2/services" + "cosmossdk.io/x/staking" - cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" - sdk "github.com/cosmos/cosmos-sdk/types" v2 "github.com/cosmos/cosmos-sdk/x/genutil/v2" ) // ExportAppStateAndValidators exports the state of the application for a genesis // file. -func (app *SimApp[T]) ExportAppStateAndValidators(jailAllowedAddrs []string) (v2.ExportedApp, error) { - // as if they could withdraw from the start of the next block +func (app *SimApp[T]) ExportAppStateAndValidators( + jailAllowedAddrs []string, +) (v2.ExportedApp, error) { ctx := context.Background() + var exportedApp v2.ExportedApp latestHeight, err := app.LoadLatestHeight() if err != nil { - return v2.ExportedApp{}, err + return exportedApp, err } genesis, err := app.ExportGenesis(ctx, latestHeight) if err != nil { - return v2.ExportedApp{}, err + return exportedApp, err } - // get the current bonded validators - resp, err := app.Query(ctx, 0, latestHeight, &stakingtypes.QueryValidatorsRequest{ - Status: stakingtypes.BondStatusBonded, - }) - - vals, ok := resp.(*stakingtypes.QueryValidatorsResponse) - if !ok { - return v2.ExportedApp{}, fmt.Errorf("invalid response, expected QueryValidatorsResponse") + readerMap, err := app.GetStore().StateAt(latestHeight) + if err != nil { + return exportedApp, err } - - // convert to genesis validator - var genesisVals []sdk.GenesisValidator - for _, val := range vals.Validators { - pk, err := val.ConsPubKey() - if err != nil { - return v2.ExportedApp{}, err - } - jsonPk, err := cryptocodec.PubKeyFromProto(pk) - if err != nil { - return v2.ExportedApp{}, err - } - - genesisVals = append(genesisVals, sdk.GenesisValidator{ - Address: sdk.ConsAddress(pk.Address()).Bytes(), - PubKey: jsonPk, - Power: val.GetConsensusPower(app.StakingKeeper.PowerReduction(ctx)), - Name: val.Description.Moniker, - }) + genesisCtx := services.NewGenesisContext(readerMap) + err = genesisCtx.Read(ctx, func(ctx context.Context) error { + exportedApp.Validators, err = staking.WriteValidators(ctx, app.StakingKeeper) + return err + }) + if err != nil { + return exportedApp, err } - return v2.ExportedApp{ - AppState: genesis, - Height: int64(latestHeight), - Validators: genesisVals, - }, err + exportedApp.AppState = genesis + exportedApp.Height = int64(latestHeight) + return exportedApp, nil } From 687866d7ee004928561674b1264f2a8eccb517d6 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Wed, 9 Oct 2024 04:49:32 +0700 Subject: [PATCH 34/35] lint --- x/genutil/client/cli/migrate.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/x/genutil/client/cli/migrate.go b/x/genutil/client/cli/migrate.go index 60518c5f73ea..912aae4600f8 100644 --- a/x/genutil/client/cli/migrate.go +++ b/x/genutil/client/cli/migrate.go @@ -17,8 +17,10 @@ import ( "github.com/cosmos/cosmos-sdk/x/genutil/types" ) -const flagGenesisTime = "genesis-time" -const v52 = "v0.52" +const ( + flagGenesisTime = "genesis-time" + v52 = "v0.52" +) // MigrationMap is a map of SDK versions to their respective genesis migration functions. var MigrationMap = types.MigrationMap{ From 9b397f03efcf1591343d61f88587ef3a33a85d3a Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Wed, 9 Oct 2024 05:02:56 +0700 Subject: [PATCH 35/35] minor --- simapp/v2/app_di.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/simapp/v2/app_di.go b/simapp/v2/app_di.go index ded776825926..29f62defcde6 100644 --- a/simapp/v2/app_di.go +++ b/simapp/v2/app_di.go @@ -18,8 +18,6 @@ import ( basedepinject "cosmossdk.io/x/accounts/defaults/base/depinject" lockupdepinject "cosmossdk.io/x/accounts/defaults/lockup/depinject" multisigdepinject "cosmossdk.io/x/accounts/defaults/multisig/depinject" - distrkeeper "cosmossdk.io/x/distribution/keeper" - slashingkeeper "cosmossdk.io/x/slashing/keeper" stakingkeeper "cosmossdk.io/x/staking/keeper" upgradekeeper "cosmossdk.io/x/upgrade/keeper" @@ -46,10 +44,8 @@ type SimApp[T transaction.Tx] struct { // required keepers during wiring // others keepers are all in the app - UpgradeKeeper *upgradekeeper.Keeper - StakingKeeper *stakingkeeper.Keeper - DistrKeeper *distrkeeper.Keeper - SlashingKeeper *slashingkeeper.Keeper + UpgradeKeeper *upgradekeeper.Keeper + StakingKeeper *stakingkeeper.Keeper } func init() { @@ -170,8 +166,6 @@ func NewSimApp[T transaction.Tx]( &app.interfaceRegistry, &app.UpgradeKeeper, &app.StakingKeeper, - &app.DistrKeeper, - &app.SlashingKeeper, ); err != nil { panic(err) }