Skip to content

Commit

Permalink
use cosmossdk.io/core/codec
Browse files Browse the repository at this point in the history
for x/staking
  • Loading branch information
hungdinh82 committed Jan 10, 2025
1 parent 517839b commit f7d797d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 23 deletions.
2 changes: 1 addition & 1 deletion x/staking/migrations/v5/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"cosmossdk.io/store/prefix"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/codec"
"cosmossdk.io/core/codec"
)

func migrateDelegationsByValidatorIndex(store storetypes.KVStore) error {
Expand Down
12 changes: 9 additions & 3 deletions x/staking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"cosmossdk.io/x/staking/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"cosmossdk.io/core/codec"
"github.com/cosmos/cosmos-sdk/types/module"
)

Expand Down Expand Up @@ -119,7 +119,11 @@ func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error {

// DefaultGenesis returns default genesis state as raw bytes for the staking module.
func (am AppModule) DefaultGenesis() json.RawMessage {
return am.cdc.MustMarshalJSON(types.DefaultGenesisState())
data, err := am.cdc.MarshalJSON(types.DefaultGenesisState())
if err != nil {
panic(err)
}
return data
}

// ValidateGenesis performs genesis state validation for the staking module.
Expand All @@ -135,7 +139,9 @@ func (am AppModule) ValidateGenesis(bz json.RawMessage) error {
// InitGenesis performs genesis initialization for the staking module.
func (am AppModule) InitGenesis(ctx context.Context, data json.RawMessage) ([]appmodule.ValidatorUpdate, error) {
var genesisState types.GenesisState
am.cdc.MustUnmarshalJSON(data, &genesisState)
if err := am.cdc.UnmarshalJSON(data, &genesisState); err != nil {
panic(err)
}
return am.keeper.InitGenesis(ctx, &genesisState)
}

Expand Down
58 changes: 43 additions & 15 deletions x/staking/simulation/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"cosmossdk.io/math"
"cosmossdk.io/x/staking/types"

"github.com/cosmos/cosmos-sdk/codec"
"cosmossdk.io/core/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/kv"
)
Expand All @@ -30,8 +30,12 @@ func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string {
case bytes.Equal(kvA.Key[:1], types.ValidatorsKey):
var validatorA, validatorB types.Validator

cdc.MustUnmarshal(kvA.Value, &validatorA)
cdc.MustUnmarshal(kvB.Value, &validatorB)
if err := cdc.Unmarshal(kvA.Value, &validatorA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &validatorB); err != nil {
panic(err)
}

return fmt.Sprintf("%v\n%v", validatorA, validatorB)
case bytes.Equal(kvA.Key[:1], types.LastValidatorPowerKey),
Expand All @@ -42,45 +46,69 @@ func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string {
case bytes.Equal(kvA.Key[:1], types.DelegationKey):
var delegationA, delegationB types.Delegation

cdc.MustUnmarshal(kvA.Value, &delegationA)
cdc.MustUnmarshal(kvB.Value, &delegationB)
if err := cdc.Unmarshal(kvA.Value, &delegationA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &delegationB); err != nil {
panic(err)
}

return fmt.Sprintf("%v\n%v", delegationA, delegationB)
case bytes.Equal(kvA.Key[:1], types.UnbondingDelegationKey),
bytes.Equal(kvA.Key[:1], types.UnbondingDelegationByValIndexKey):
var ubdA, ubdB types.UnbondingDelegation

cdc.MustUnmarshal(kvA.Value, &ubdA)
cdc.MustUnmarshal(kvB.Value, &ubdB)
if err := cdc.Unmarshal(kvA.Value, &ubdA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &ubdB); err != nil {
panic(err)
}

return fmt.Sprintf("%v\n%v", ubdA, ubdB)
case bytes.Equal(kvA.Key[:1], types.RedelegationKey),
bytes.Equal(kvA.Key[:1], types.RedelegationByValSrcIndexKey):
var redA, redB types.Redelegation

cdc.MustUnmarshal(kvA.Value, &redA)
cdc.MustUnmarshal(kvB.Value, &redB)
if err := cdc.Unmarshal(kvA.Value, &redA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &redB); err != nil {
panic(err)
}

return fmt.Sprintf("%v\n%v", redA, redB)
case bytes.Equal(kvA.Key[:1], types.ParamsKey):
var paramsA, paramsB types.Params

cdc.MustUnmarshal(kvA.Value, &paramsA)
cdc.MustUnmarshal(kvB.Value, &paramsB)
if err := cdc.Unmarshal(kvA.Value, &paramsA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &paramsB); err != nil {
panic(err)
}

return fmt.Sprintf("%v\n%v", paramsA, paramsB)
case bytes.Equal(kvA.Key[:1], types.ValidatorConsPubKeyRotationHistoryKey):
var historyA, historyB types.ConsPubKeyRotationHistory

cdc.MustUnmarshal(kvA.Value, &historyA)
cdc.MustUnmarshal(kvB.Value, &historyB)
if err := cdc.Unmarshal(kvA.Value, &historyA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &historyB); err != nil {
panic(err)
}

return fmt.Sprintf("%v\n%v", historyA, historyB)
case bytes.Equal(kvA.Key[:1], types.ValidatorConsensusKeyRotationRecordQueueKey):
var historyA, historyB types.ValAddrsOfRotatedConsKeys

cdc.MustUnmarshal(kvA.Value, &historyA)
cdc.MustUnmarshal(kvB.Value, &historyB)
if err := cdc.Unmarshal(kvA.Value, &historyA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &historyB); err != nil {
panic(err)
}

return fmt.Sprintf("%v\n%v", historyA, historyB)
default:
Expand Down
20 changes: 16 additions & 4 deletions x/staking/types/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"cosmossdk.io/core/address"
"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/codec"
"cosmossdk.io/core/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -26,7 +26,11 @@ func NewDelegation(delegatorAddr, validatorAddr string, shares math.LegacyDec) D

// MustMarshalDelegation returns the delegation bytes. Panics if fails
func MustMarshalDelegation(cdc codec.BinaryCodec, delegation Delegation) []byte {
return cdc.MustMarshal(&delegation)
data, err := cdc.Marshal(&delegation)
if err != nil {
panic(err)
}
return data
}

// MustUnmarshalDelegation return the unmarshaled delegation from bytes.
Expand Down Expand Up @@ -143,7 +147,11 @@ func (ubd *UnbondingDelegation) RemoveEntry(i int64) {

// return the unbonding delegation
func MustMarshalUBD(cdc codec.BinaryCodec, ubd UnbondingDelegation) []byte {
return cdc.MustMarshal(&ubd)
data, err := cdc.Marshal(&ubd)
if err != nil {
panic(err)
}
return data
}

// unmarshal a unbonding delegation from a store value
Expand Down Expand Up @@ -234,7 +242,11 @@ func (red *Redelegation) RemoveEntry(i int64) {

// MustMarshalRED returns the Redelegation bytes. Panics if fails.
func MustMarshalRED(cdc codec.BinaryCodec, red Redelegation) []byte {
return cdc.MustMarshal(&red)
data, err := cdc.Marshal(&red)
if err != nil {
panic(err)
}
return data
}

// MustUnmarshalRED unmarshals a redelegation from a store value. Panics if fails.
Expand Down

0 comments on commit f7d797d

Please sign in to comment.