Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(types)!: remove IntProto and DecProto #16918

Merged
merged 10 commits into from
Jul 12, 2023
Prev Previous commit
Next Next commit
remove use of IntProto
  • Loading branch information
robert-zaremba committed Jul 11, 2023
commit df43ff82fa65b5ed611ef83b992c87ed3cf01f1e
12 changes: 5 additions & 7 deletions x/staking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"context"
"fmt"

abci "github.com/cometbft/cometbft/abci/types"

storetypes "cosmossdk.io/core/store"
"cosmossdk.io/log"
"cosmossdk.io/math"
abci "github.com/cometbft/cometbft/abci/types"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -101,19 +100,18 @@ func (k Keeper) GetLastTotalPower(ctx context.Context) (math.Int, error) {
return math.ZeroInt(), nil
}

ip := sdk.IntProto{}
err = k.cdc.Unmarshal(bz, &ip)
if err != nil {
var power math.Int
if err = power.Unmarshal(bz); err != nil {
return math.ZeroInt(), err
}

return ip.Int, nil
return power, nil
}

// SetLastTotalPower sets the last total validator power.
func (k Keeper) SetLastTotalPower(ctx context.Context, power math.Int) error {
store := k.storeService.OpenKVStore(ctx)
bz, err := k.cdc.Marshal(&sdk.IntProto{Int: power})
bz, err := power.Marshal()
if err != nil {
return err
}
Expand Down
15 changes: 10 additions & 5 deletions x/staking/simulation/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"fmt"

"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/kv"
Expand All @@ -16,12 +18,15 @@ func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string {
return func(kvA, kvB kv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], types.LastTotalPowerKey):
var powerA, powerB sdk.IntProto

cdc.MustUnmarshal(kvA.Value, &powerA)
cdc.MustUnmarshal(kvB.Value, &powerB)

var powerA, powerB math.Int
if err := powerA.Unmarshal(kvA.Value); err != nil {
panic(err)
}
if err := powerB.Unmarshal(kvB.Value); err != nil {
panic(err)
}
return fmt.Sprintf("%v\n%v", powerA, powerB)

case bytes.Equal(kvA.Key[:1], types.ValidatorsKey):
var validatorA, validatorB types.Validator

Expand Down
7 changes: 4 additions & 3 deletions x/staking/simulation/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

"cosmossdk.io/math"
"github.com/stretchr/testify/require"
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -33,10 +32,12 @@ func TestDecodeStore(t *testing.T) {
del := types.NewDelegation(delAddr1, valAddr1, math.LegacyOneDec())
ubd := types.NewUnbondingDelegation(delAddr1, valAddr1, 15, bondTime, math.OneInt(), 1)
red := types.NewRedelegation(delAddr1, valAddr1, valAddr1, 12, bondTime, math.OneInt(), math.LegacyOneDec(), 0)
oneIntBz, err := math.OneInt().Marshal()
require.NoError(t, err)

kvPairs := kv.Pairs{
Pairs: []kv.Pair{
{Key: types.LastTotalPowerKey, Value: cdc.MustMarshal(&sdk.IntProto{Int: math.OneInt()})},
{Key: types.LastTotalPowerKey, Value: oneIntBz},
{Key: types.GetValidatorKey(valAddr1), Value: cdc.MustMarshal(&val)},
{Key: types.LastValidatorPowerKey, Value: valAddr1.Bytes()},
{Key: types.GetDelegationKey(delAddr1, valAddr1), Value: cdc.MustMarshal(&del)},
Expand Down
Loading