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

Co-authored-by: Julien Robert <julien@rbrt.fr>
This commit is contained in:
Robert Zaremba 2023-07-13 00:16:28 +02:00 committed by GitHub
parent 74c351e2fd
commit bc4ccab74b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 747 additions and 2041 deletions

View File

@ -75,6 +75,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* remove `Keeper`: `IterateValidatorHistoricalRewards`, `GetValidatorHistoricalRewards`, `SetValidatorHistoricalRewards`, `DeleteValidatorHistoricalRewards`, `DeleteValidatorHistoricalReward`, `DeleteAllValidatorHistoricalRewards`
* (x/staking) [#16795](https://github.com/cosmos/cosmos-sdk/pull/16795) `DelegationToDelegationResponse`, `DelegationsToDelegationResponses`, `RedelegationsToRedelegationResponses` are no longer exported.
* [#16441](https://github.com/cosmos/cosmos-sdk/pull/16441) Params state is migrated to collections. `GetParams` has been removed
* [#16918](https://github.com/cosmos/cosmos-sdk/pull/16918) Remove `IntProto` and `DecProto`. Instead, `math.Int` and `math.LegacyDec` should be used respectively. Both types support `Marshal` and `Unmarshal` which should be used for binary marshaling.
## CLI Breaking Changes

File diff suppressed because it is too large Load Diff

View File

@ -33,16 +33,9 @@ message DecCoin {
option (gogoproto.equal) = true;
string denom = 1;
string amount = 2
[(cosmos_proto.scalar) = "cosmos.Dec", (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec", (gogoproto.nullable) = false];
}
// IntProto defines a Protobuf wrapper around an Int object.
message IntProto {
string int = 1 [(cosmos_proto.scalar) = "cosmos.Int", (gogoproto.customtype) = "cosmossdk.io/math.Int", (gogoproto.nullable) = false];
}
// DecProto defines a Protobuf wrapper around a Dec object.
message DecProto {
string dec = 1 [(cosmos_proto.scalar) = "cosmos.Dec", (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec", (gogoproto.nullable) = false];
string amount = 2 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
}

View File

@ -122,91 +122,15 @@ func (m *DecCoin) GetDenom() string {
return ""
}
// IntProto defines a Protobuf wrapper around an Int object.
type IntProto struct {
Int cosmossdk_io_math.Int `protobuf:"bytes,1,opt,name=int,proto3,customtype=cosmossdk.io/math.Int" json:"int"`
}
func (m *IntProto) Reset() { *m = IntProto{} }
func (*IntProto) ProtoMessage() {}
func (*IntProto) Descriptor() ([]byte, []int) {
return fileDescriptor_189a96714eafc2df, []int{2}
}
func (m *IntProto) XXX_Unmarshal(b []byte) error {
return m.Unmarshal(b)
}
func (m *IntProto) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
if deterministic {
return xxx_messageInfo_IntProto.Marshal(b, m, deterministic)
} else {
b = b[:cap(b)]
n, err := m.MarshalToSizedBuffer(b)
if err != nil {
return nil, err
}
return b[:n], nil
}
}
func (m *IntProto) XXX_Merge(src proto.Message) {
xxx_messageInfo_IntProto.Merge(m, src)
}
func (m *IntProto) XXX_Size() int {
return m.Size()
}
func (m *IntProto) XXX_DiscardUnknown() {
xxx_messageInfo_IntProto.DiscardUnknown(m)
}
var xxx_messageInfo_IntProto proto.InternalMessageInfo
// DecProto defines a Protobuf wrapper around a Dec object.
type DecProto struct {
Dec cosmossdk_io_math.LegacyDec `protobuf:"bytes,1,opt,name=dec,proto3,customtype=cosmossdk.io/math.LegacyDec" json:"dec"`
}
func (m *DecProto) Reset() { *m = DecProto{} }
func (*DecProto) ProtoMessage() {}
func (*DecProto) Descriptor() ([]byte, []int) {
return fileDescriptor_189a96714eafc2df, []int{3}
}
func (m *DecProto) XXX_Unmarshal(b []byte) error {
return m.Unmarshal(b)
}
func (m *DecProto) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
if deterministic {
return xxx_messageInfo_DecProto.Marshal(b, m, deterministic)
} else {
b = b[:cap(b)]
n, err := m.MarshalToSizedBuffer(b)
if err != nil {
return nil, err
}
return b[:n], nil
}
}
func (m *DecProto) XXX_Merge(src proto.Message) {
xxx_messageInfo_DecProto.Merge(m, src)
}
func (m *DecProto) XXX_Size() int {
return m.Size()
}
func (m *DecProto) XXX_DiscardUnknown() {
xxx_messageInfo_DecProto.DiscardUnknown(m)
}
var xxx_messageInfo_DecProto proto.InternalMessageInfo
func init() {
proto.RegisterType((*Coin)(nil), "cosmos.base.v1beta1.Coin")
proto.RegisterType((*DecCoin)(nil), "cosmos.base.v1beta1.DecCoin")
proto.RegisterType((*IntProto)(nil), "cosmos.base.v1beta1.IntProto")
proto.RegisterType((*DecProto)(nil), "cosmos.base.v1beta1.DecProto")
}
func init() { proto.RegisterFile("cosmos/base/v1beta1/coin.proto", fileDescriptor_189a96714eafc2df) }
var fileDescriptor_189a96714eafc2df = []byte{
// 341 bytes of a gzipped FileDescriptorProto
// 294 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x4b, 0xce, 0x2f, 0xce,
0xcd, 0x2f, 0xd6, 0x4f, 0x4a, 0x2c, 0x4e, 0xd5, 0x2f, 0x33, 0x4c, 0x4a, 0x2d, 0x49, 0x34, 0xd4,
0x4f, 0xce, 0xcf, 0xcc, 0xd3, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x86, 0xc8, 0xeb, 0x81,
@ -220,15 +144,12 @@ var fileDescriptor_189a96714eafc2df = []byte{
0x99, 0x57, 0xb2, 0xe2, 0xf9, 0x06, 0x2d, 0xc6, 0x20, 0xa8, 0x7e, 0x2b, 0x96, 0x17, 0x0b, 0xe4,
0x19, 0x95, 0x0a, 0xb8, 0xd8, 0x5d, 0x52, 0x93, 0xf1, 0x58, 0xe8, 0x89, 0x66, 0xa1, 0x21, 0xd4,
0x42, 0x69, 0x4c, 0x0b, 0x7d, 0x52, 0xd3, 0x13, 0x93, 0x2b, 0x5d, 0x52, 0x93, 0x91, 0xac, 0x75,
0x49, 0x4d, 0x46, 0xb3, 0xd1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b, 0x9c, 0x92, 0xad, 0x5f, 0x52, 0x59,
0x90, 0x5a, 0x9c, 0xc4, 0x06, 0x0e, 0x2c, 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0x3a, 0xd8,
0xb7, 0xd2, 0xa7, 0x01, 0x00, 0x00,
}
func (this *Coin) Equal(that interface{}) bool {
@ -365,72 +286,6 @@ func (m *DecCoin) MarshalToSizedBuffer(dAtA []byte) (int, error) {
return len(dAtA) - i, nil
}
func (m *IntProto) Marshal() (dAtA []byte, err error) {
size := m.Size()
dAtA = make([]byte, size)
n, err := m.MarshalToSizedBuffer(dAtA[:size])
if err != nil {
return nil, err
}
return dAtA[:n], nil
}
func (m *IntProto) MarshalTo(dAtA []byte) (int, error) {
size := m.Size()
return m.MarshalToSizedBuffer(dAtA[:size])
}
func (m *IntProto) MarshalToSizedBuffer(dAtA []byte) (int, error) {
i := len(dAtA)
_ = i
var l int
_ = l
{
size := m.Int.Size()
i -= size
if _, err := m.Int.MarshalTo(dAtA[i:]); err != nil {
return 0, err
}
i = encodeVarintCoin(dAtA, i, uint64(size))
}
i--
dAtA[i] = 0xa
return len(dAtA) - i, nil
}
func (m *DecProto) Marshal() (dAtA []byte, err error) {
size := m.Size()
dAtA = make([]byte, size)
n, err := m.MarshalToSizedBuffer(dAtA[:size])
if err != nil {
return nil, err
}
return dAtA[:n], nil
}
func (m *DecProto) MarshalTo(dAtA []byte) (int, error) {
size := m.Size()
return m.MarshalToSizedBuffer(dAtA[:size])
}
func (m *DecProto) MarshalToSizedBuffer(dAtA []byte) (int, error) {
i := len(dAtA)
_ = i
var l int
_ = l
{
size := m.Dec.Size()
i -= size
if _, err := m.Dec.MarshalTo(dAtA[i:]); err != nil {
return 0, err
}
i = encodeVarintCoin(dAtA, i, uint64(size))
}
i--
dAtA[i] = 0xa
return len(dAtA) - i, nil
}
func encodeVarintCoin(dAtA []byte, offset int, v uint64) int {
offset -= sovCoin(v)
base := offset
@ -472,28 +327,6 @@ func (m *DecCoin) Size() (n int) {
return n
}
func (m *IntProto) Size() (n int) {
if m == nil {
return 0
}
var l int
_ = l
l = m.Int.Size()
n += 1 + l + sovCoin(uint64(l))
return n
}
func (m *DecProto) Size() (n int) {
if m == nil {
return 0
}
var l int
_ = l
l = m.Dec.Size()
n += 1 + l + sovCoin(uint64(l))
return n
}
func sovCoin(x uint64) (n int) {
return (math_bits.Len64(x|1) + 6) / 7
}
@ -732,174 +565,6 @@ func (m *DecCoin) Unmarshal(dAtA []byte) error {
}
return nil
}
func (m *IntProto) Unmarshal(dAtA []byte) error {
l := len(dAtA)
iNdEx := 0
for iNdEx < l {
preIndex := iNdEx
var wire uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowCoin
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
wire |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
fieldNum := int32(wire >> 3)
wireType := int(wire & 0x7)
if wireType == 4 {
return fmt.Errorf("proto: IntProto: wiretype end group for non-group")
}
if fieldNum <= 0 {
return fmt.Errorf("proto: IntProto: illegal tag %d (wire type %d)", fieldNum, wire)
}
switch fieldNum {
case 1:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Int", wireType)
}
var stringLen uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowCoin
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
stringLen |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
intStringLen := int(stringLen)
if intStringLen < 0 {
return ErrInvalidLengthCoin
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return ErrInvalidLengthCoin
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
if err := m.Int.Unmarshal(dAtA[iNdEx:postIndex]); err != nil {
return err
}
iNdEx = postIndex
default:
iNdEx = preIndex
skippy, err := skipCoin(dAtA[iNdEx:])
if err != nil {
return err
}
if (skippy < 0) || (iNdEx+skippy) < 0 {
return ErrInvalidLengthCoin
}
if (iNdEx + skippy) > l {
return io.ErrUnexpectedEOF
}
iNdEx += skippy
}
}
if iNdEx > l {
return io.ErrUnexpectedEOF
}
return nil
}
func (m *DecProto) Unmarshal(dAtA []byte) error {
l := len(dAtA)
iNdEx := 0
for iNdEx < l {
preIndex := iNdEx
var wire uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowCoin
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
wire |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
fieldNum := int32(wire >> 3)
wireType := int(wire & 0x7)
if wireType == 4 {
return fmt.Errorf("proto: DecProto: wiretype end group for non-group")
}
if fieldNum <= 0 {
return fmt.Errorf("proto: DecProto: illegal tag %d (wire type %d)", fieldNum, wire)
}
switch fieldNum {
case 1:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Dec", wireType)
}
var stringLen uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowCoin
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
stringLen |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
intStringLen := int(stringLen)
if intStringLen < 0 {
return ErrInvalidLengthCoin
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return ErrInvalidLengthCoin
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
if err := m.Dec.Unmarshal(dAtA[iNdEx:postIndex]); err != nil {
return err
}
iNdEx = postIndex
default:
iNdEx = preIndex
skippy, err := skipCoin(dAtA[iNdEx:])
if err != nil {
return err
}
if (skippy < 0) || (iNdEx+skippy) < 0 {
return ErrInvalidLengthCoin
}
if (iNdEx + skippy) > l {
return io.ErrUnexpectedEOF
}
iNdEx += skippy
}
}
if iNdEx > l {
return io.ErrUnexpectedEOF
}
return nil
}
func skipCoin(dAtA []byte) (n int, err error) {
l := len(dAtA)
iNdEx := 0

View File

@ -1,9 +0,0 @@
package types
func (ip IntProto) String() string {
return ip.Int.String()
}
func (dp DecProto) String() string {
return dp.Dec.String()
}

View File

@ -101,19 +101,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
}

View File

@ -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"
@ -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

View File

@ -33,10 +33,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)},

File diff suppressed because it is too large Load Diff