From 9b4707917138099f7577ae705faf4fb3ba7124a8 Mon Sep 17 00:00:00 2001 From: Alexander Bezobchuk Date: Thu, 20 Feb 2020 17:15:22 +0100 Subject: [PATCH] Merge PR #5682: Change pubkey to bytes --- x/auth/types/account.go | 67 +++++--- x/auth/types/account_test.go | 10 +- x/auth/types/types.pb.go | 95 ++++++------ x/auth/types/types.proto | 2 +- x/auth/vesting/types/vesting_account.go | 154 +++++++++---------- x/auth/vesting/types/vesting_account_test.go | 24 +-- x/slashing/types/types.pb.go | 17 +- x/slashing/types/types.proto | 24 +-- 8 files changed, 214 insertions(+), 179 deletions(-) diff --git a/x/auth/types/account.go b/x/auth/types/account.go index 1c2460166e..08b67facc3 100644 --- a/x/auth/types/account.go +++ b/x/auth/types/account.go @@ -7,6 +7,7 @@ import ( "github.com/tendermint/tendermint/crypto" yaml "gopkg.in/yaml.v2" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth/exported" ) @@ -16,17 +17,14 @@ var _ exported.GenesisAccount = (*BaseAccount)(nil) // NewBaseAccount creates a new BaseAccount object func NewBaseAccount(address sdk.AccAddress, pubKey crypto.PubKey, accountNumber, sequence uint64) *BaseAccount { - var pkStr string - if pubKey != nil { - pkStr = sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pubKey) - } - - return &BaseAccount{ + acc := &BaseAccount{ Address: address, - PubKey: pkStr, AccountNumber: accountNumber, Sequence: sequence, } + + acc.SetPubKey(pubKey) + return acc } // ProtoBaseAccount - a prototype function for BaseAccount @@ -57,22 +55,23 @@ func (acc *BaseAccount) SetAddress(addr sdk.AccAddress) error { } // GetPubKey - Implements sdk.Account. -func (acc BaseAccount) GetPubKey() crypto.PubKey { - if acc.PubKey == "" { +func (acc BaseAccount) GetPubKey() (pk crypto.PubKey) { + if len(acc.PubKey) == 0 { return nil } - return sdk.MustGetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, acc.PubKey) + codec.Cdc.MustUnmarshalBinaryBare(acc.PubKey, &pk) + return pk } // SetPubKey - Implements sdk.Account. func (acc *BaseAccount) SetPubKey(pubKey crypto.PubKey) error { - pkStr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pubKey) - if err != nil { - return err + if pubKey == nil { + acc.PubKey = nil + } else { + acc.PubKey = pubKey.Bytes() } - acc.PubKey = pkStr return nil } @@ -100,15 +99,47 @@ func (acc *BaseAccount) SetSequence(seq uint64) error { // Validate checks for errors on the account fields func (acc BaseAccount) Validate() error { - if acc.PubKey != "" && acc.Address != nil && + if len(acc.PubKey) != 0 && acc.Address != nil && !bytes.Equal(acc.GetPubKey().Address().Bytes(), acc.Address.Bytes()) { - return errors.New("pubkey and address pair is invalid") + return errors.New("account address and pubkey address do not match") } return nil } func (acc BaseAccount) String() string { - out, _ := yaml.Marshal(acc) - return string(out) + out, _ := acc.MarshalYAML() + return out.(string) +} + +type baseAccountPretty struct { + Address sdk.AccAddress `json:"address" yaml:"address"` + PubKey string `json:"public_key" yaml:"public_key"` + AccountNumber uint64 `json:"account_number" yaml:"account_number"` + Sequence uint64 `json:"sequence" yaml:"sequence"` +} + +// MarshalYAML returns the YAML representation of an account. +func (acc BaseAccount) MarshalYAML() (interface{}, error) { + alias := baseAccountPretty{ + Address: acc.Address, + AccountNumber: acc.AccountNumber, + Sequence: acc.Sequence, + } + + if acc.PubKey != nil { + pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, acc.GetPubKey()) + if err != nil { + return nil, err + } + + alias.PubKey = pks + } + + bz, err := yaml.Marshal(alias) + if err != nil { + return nil, err + } + + return string(bz), err } diff --git a/x/auth/types/account_test.go b/x/auth/types/account_test.go index e86f4b5940..ca612f1fe1 100644 --- a/x/auth/types/account_test.go +++ b/x/auth/types/account_test.go @@ -1,7 +1,6 @@ package types_test import ( - "errors" "testing" "github.com/stretchr/testify/require" @@ -86,17 +85,17 @@ func TestGenesisAccountValidate(t *testing.T) { tests := []struct { name string acc exported.GenesisAccount - expErr error + expErr bool }{ { "valid base account", baseAcc, - nil, + false, }, { "invalid base valid account", types.NewBaseAccount(addr, secp256k1.GenPrivKey().PubKey(), 0, 0), - errors.New("pubkey and address pair is invalid"), + true, }, } @@ -104,8 +103,7 @@ func TestGenesisAccountValidate(t *testing.T) { tt := tt t.Run(tt.name, func(t *testing.T) { - err := tt.acc.Validate() - require.Equal(t, tt.expErr, err) + require.Equal(t, tt.expErr, tt.acc.Validate() != nil) }) } } diff --git a/x/auth/types/types.pb.go b/x/auth/types/types.pb.go index 04705c07d3..6770d4eb2b 100644 --- a/x/auth/types/types.pb.go +++ b/x/auth/types/types.pb.go @@ -30,7 +30,7 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // type for additional functionality (e.g. vesting). type BaseAccount struct { Address github_com_cosmos_cosmos_sdk_types.AccAddress `protobuf:"bytes,1,opt,name=address,proto3,casttype=github.com/cosmos/cosmos-sdk/types.AccAddress" json:"address,omitempty"` - PubKey string `protobuf:"bytes,2,opt,name=pub_key,json=pubKey,proto3" json:"pub_key,omitempty" yaml:"public_key"` + PubKey []byte `protobuf:"bytes,2,opt,name=pub_key,json=pubKey,proto3" json:"pub_key,omitempty" yaml:"public_key"` AccountNumber uint64 `protobuf:"varint,3,opt,name=account_number,json=accountNumber,proto3" json:"account_number,omitempty" yaml:"account_number"` Sequence uint64 `protobuf:"varint,4,opt,name=sequence,proto3" json:"sequence,omitempty"` } @@ -207,46 +207,45 @@ func init() { func init() { proto.RegisterFile("x/auth/types/types.proto", fileDescriptor_2d526fa662daab74) } var fileDescriptor_2d526fa662daab74 = []byte{ - // 610 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x53, 0x3d, 0x6f, 0xd3, 0x4e, - 0x18, 0x8f, 0xff, 0xc9, 0x3f, 0x2d, 0xd7, 0x82, 0x88, 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89, 0xae, 0x7c, 0xba, 0xd0, 0x2b, - 0x67, 0x17, 0x7a, 0xe5, 0xeb, 0x85, 0x5e, 0x79, 0xf7, 0xe8, 0xaf, 0xf7, 0x67, 0xf6, 0xcd, 0xbb, - 0xf5, 0xf2, 0x75, 0x3e, 0xf9, 0x19, 0x00, 0x00, 0xff, 0xff, 0xd6, 0x52, 0x88, 0x39, 0x0a, 0x04, - 0x00, 0x00, + // 606 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x53, 0x3f, 0x6f, 0xd3, 0x40, + 0x14, 0x8f, 0x49, 0x48, 0xab, 0x6b, 0x41, 0xc4, 0xfd, 0xe7, 0x46, 0xc8, 0x17, 0x79, 0x40, 0x61, + 0xa8, 0x43, 0x8a, 0x8a, 0xd4, 0x0c, 0x88, 0x3a, 0xc0, 0x52, 0xa8, 0x2a, 0x47, 0x62, 0x40, 0x42, + 0xd6, 0xf9, 0x7c, 0x38, 0x56, 0x7a, 0x39, 0xd7, 0x77, 0xae, 0xec, 0x7e, 0x02, 0x46, 0x46, 0xc6, + 0xce, 0x7c, 0x92, 0x8e, 0x1d, 0x99, 0x5c, 0x94, 0x2e, 0xcc, 0x1e, 0x99, 0x90, 0x7d, 0x69, 0x49, + 0x4b, 0x41, 0x2c, 0xc9, 0xbd, 0xf7, 0xfb, 0x77, 0xf7, 0x7c, 0x07, 0xb4, 0xa4, 0x83, 0x62, 0x31, + 0xec, 0x88, 0x34, 0x24, 0x5c, 0xfe, 0x9a, 0x61, 0xc4, 0x04, 0x53, 0x97, 0x31, 0xe3, 0x94, 0x71, + 0x87, 0x7b, 0x23, 0x33, 0x31, 0x0b, 0x92, 0x79, 0xd4, 0x6d, 0x3e, 0x12, 0xc3, 0x20, 0xf2, 0x9c, + 0x10, 0x45, 0x22, 0xed, 0x94, 0xc4, 0x8e, 0xcf, 0x7c, 0xf6, 0x7b, 0x25, 0xd5, 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, 0x5e, 0xe6, 0xd7, 0x74, 0xb2, 0xad, 0xa6, + 0x60, 0xfd, 0xa6, 0x82, 0x13, 0x1c, 0x6e, 0x6e, 0x3d, 0x1b, 0x75, 0xb5, 0xbb, 0x65, 0xe8, 0xf3, + 0x49, 0x06, 0x57, 0xaf, 0x85, 0x0e, 0x2e, 0x19, 0x79, 0x06, 0x5b, 0xb7, 0xc7, 0x5e, 0x99, 0x18, + 0xf6, 0x2a, 0xbf, 0x55, 0xdb, 0x9b, 0x2f, 0x2e, 0xd6, 0x8f, 0x13, 0xa8, 0x58, 0xfd, 0xd3, 0x89, + 0xae, 0x9c, 0x4d, 0x74, 0xe5, 0xfb, 0x44, 0x57, 0x3e, 0x5f, 0xe8, 0x95, 0xb3, 0x0b, 0xbd, 0xf2, + 0xed, 0x42, 0xaf, 0xbc, 0x7f, 0xfc, 0xcf, 0xfb, 0x33, 0xfb, 0xe6, 0xdd, 0x7a, 0xf9, 0x3a, 0x9f, + 0xfe, 0x0a, 0x00, 0x00, 0xff, 0xff, 0x84, 0x2e, 0x85, 0xe8, 0x0a, 0x04, 0x00, 0x00, } func (this *Params) Equal(that interface{}) bool { @@ -571,7 +570,7 @@ func (m *BaseAccount) Unmarshal(dAtA []byte) error { if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field PubKey", wireType) } - var stringLen uint64 + var byteLen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowTypes @@ -581,23 +580,25 @@ func (m *BaseAccount) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - stringLen |= uint64(b&0x7F) << shift + byteLen |= int(b&0x7F) << shift if b < 0x80 { break } } - intStringLen := int(stringLen) - if intStringLen < 0 { + if byteLen < 0 { return ErrInvalidLengthTypes } - postIndex := iNdEx + intStringLen + postIndex := iNdEx + byteLen if postIndex < 0 { return ErrInvalidLengthTypes } if postIndex > l { return io.ErrUnexpectedEOF } - m.PubKey = string(dAtA[iNdEx:postIndex]) + m.PubKey = append(m.PubKey[:0], dAtA[iNdEx:postIndex]...) + if m.PubKey == nil { + m.PubKey = []byte{} + } iNdEx = postIndex case 3: if wireType != 0 { diff --git a/x/auth/types/types.proto b/x/auth/types/types.proto index c707eb9c55..8a0d6555c7 100644 --- a/x/auth/types/types.proto +++ b/x/auth/types/types.proto @@ -14,7 +14,7 @@ message BaseAccount { option (gogoproto.goproto_stringer) = false; bytes address = 1 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"]; - string pub_key = 2 [(gogoproto.moretags) = "yaml:\"public_key\""]; + bytes pub_key = 2 [(gogoproto.moretags) = "yaml:\"public_key\""]; uint64 account_number = 3 [(gogoproto.moretags) = "yaml:\"account_number\""]; uint64 sequence = 4; } diff --git a/x/auth/vesting/types/vesting_account.go b/x/auth/vesting/types/vesting_account.go index cbbeffc031..d4b6fc6345 100644 --- a/x/auth/vesting/types/vesting_account.go +++ b/x/auth/vesting/types/vesting_account.go @@ -1,10 +1,10 @@ package types import ( - "encoding/json" "errors" "time" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" @@ -164,7 +164,7 @@ func (bva BaseVestingAccount) Validate() error { return bva.BaseAccount.Validate() } -type vestingAccountPretty struct { +type vestingAccountYAML struct { Address sdk.AccAddress `json:"address" yaml:"address"` PubKey string `json:"public_key" yaml:"public_key"` AccountNumber uint64 `json:"account_number" yaml:"account_number"` @@ -179,6 +179,21 @@ type vestingAccountPretty struct { VestingPeriods Periods `json:"vesting_periods,omitempty" yaml:"vesting_periods,omitempty"` } +type vestingAccountJSON struct { + Address sdk.AccAddress `json:"address" yaml:"address"` + PubKey crypto.PubKey `json:"public_key" yaml:"public_key"` + AccountNumber uint64 `json:"account_number" yaml:"account_number"` + Sequence uint64 `json:"sequence" yaml:"sequence"` + OriginalVesting sdk.Coins `json:"original_vesting" yaml:"original_vesting"` + DelegatedFree sdk.Coins `json:"delegated_free" yaml:"delegated_free"` + DelegatedVesting sdk.Coins `json:"delegated_vesting" yaml:"delegated_vesting"` + EndTime int64 `json:"end_time" yaml:"end_time"` + + // custom fields based on concrete vesting type which can be omitted + StartTime int64 `json:"start_time,omitempty" yaml:"start_time,omitempty"` + VestingPeriods Periods `json:"vesting_periods,omitempty" yaml:"vesting_periods,omitempty"` +} + func (bva BaseVestingAccount) String() string { out, _ := bva.MarshalYAML() return out.(string) @@ -186,9 +201,8 @@ func (bva BaseVestingAccount) String() string { // MarshalYAML returns the YAML representation of a BaseVestingAccount. func (bva BaseVestingAccount) MarshalYAML() (interface{}, error) { - alias := vestingAccountPretty{ + alias := vestingAccountYAML{ Address: bva.Address, - PubKey: bva.PubKey, AccountNumber: bva.AccountNumber, Sequence: bva.Sequence, OriginalVesting: bva.OriginalVesting, @@ -197,6 +211,16 @@ func (bva BaseVestingAccount) MarshalYAML() (interface{}, error) { EndTime: bva.EndTime, } + pk := bva.GetPubKey() + if pk != nil { + pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk) + if err != nil { + return nil, err + } + + alias.PubKey = pks + } + bz, err := yaml.Marshal(alias) if err != nil { return nil, err @@ -207,9 +231,9 @@ func (bva BaseVestingAccount) MarshalYAML() (interface{}, error) { // MarshalJSON returns the JSON representation of a BaseVestingAccount. func (bva BaseVestingAccount) MarshalJSON() ([]byte, error) { - alias := vestingAccountPretty{ + alias := vestingAccountJSON{ Address: bva.Address, - PubKey: bva.PubKey, + PubKey: bva.GetPubKey(), AccountNumber: bva.AccountNumber, Sequence: bva.Sequence, OriginalVesting: bva.OriginalVesting, @@ -218,29 +242,17 @@ func (bva BaseVestingAccount) MarshalJSON() ([]byte, error) { EndTime: bva.EndTime, } - return json.Marshal(alias) + return codec.Cdc.MarshalJSON(alias) } // UnmarshalJSON unmarshals raw JSON bytes into a BaseVestingAccount. func (bva *BaseVestingAccount) UnmarshalJSON(bz []byte) error { - var alias vestingAccountPretty - if err := json.Unmarshal(bz, &alias); err != nil { + var alias vestingAccountJSON + if err := codec.Cdc.UnmarshalJSON(bz, &alias); err != nil { return err } - var ( - pk crypto.PubKey - err error - ) - - if alias.PubKey != "" { - pk, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, alias.PubKey) - if err != nil { - return err - } - } - - bva.BaseAccount = authtypes.NewBaseAccount(alias.Address, pk, alias.AccountNumber, alias.Sequence) + bva.BaseAccount = authtypes.NewBaseAccount(alias.Address, alias.PubKey, alias.AccountNumber, alias.Sequence) bva.OriginalVesting = alias.OriginalVesting bva.DelegatedFree = alias.DelegatedFree bva.DelegatedVesting = alias.DelegatedVesting @@ -344,9 +356,8 @@ func (cva ContinuousVestingAccount) String() string { // MarshalYAML returns the YAML representation of a ContinuousVestingAccount. func (cva ContinuousVestingAccount) MarshalYAML() (interface{}, error) { - alias := vestingAccountPretty{ + alias := vestingAccountYAML{ Address: cva.Address, - PubKey: cva.PubKey, AccountNumber: cva.AccountNumber, Sequence: cva.Sequence, OriginalVesting: cva.OriginalVesting, @@ -356,6 +367,16 @@ func (cva ContinuousVestingAccount) MarshalYAML() (interface{}, error) { StartTime: cva.StartTime, } + pk := cva.GetPubKey() + if pk != nil { + pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk) + if err != nil { + return nil, err + } + + alias.PubKey = pks + } + bz, err := yaml.Marshal(alias) if err != nil { return nil, err @@ -366,9 +387,9 @@ func (cva ContinuousVestingAccount) MarshalYAML() (interface{}, error) { // MarshalJSON returns the JSON representation of a ContinuousVestingAccount. func (cva ContinuousVestingAccount) MarshalJSON() ([]byte, error) { - alias := vestingAccountPretty{ + alias := vestingAccountJSON{ Address: cva.Address, - PubKey: cva.PubKey, + PubKey: cva.GetPubKey(), AccountNumber: cva.AccountNumber, Sequence: cva.Sequence, OriginalVesting: cva.OriginalVesting, @@ -378,30 +399,18 @@ func (cva ContinuousVestingAccount) MarshalJSON() ([]byte, error) { StartTime: cva.StartTime, } - return json.Marshal(alias) + return codec.Cdc.MarshalJSON(alias) } // UnmarshalJSON unmarshals raw JSON bytes into a ContinuousVestingAccount. func (cva *ContinuousVestingAccount) UnmarshalJSON(bz []byte) error { - var alias vestingAccountPretty - if err := json.Unmarshal(bz, &alias); err != nil { + var alias vestingAccountJSON + if err := codec.Cdc.UnmarshalJSON(bz, &alias); err != nil { return err } - var ( - pk crypto.PubKey - err error - ) - - if alias.PubKey != "" { - pk, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, alias.PubKey) - if err != nil { - return err - } - } - cva.BaseVestingAccount = &BaseVestingAccount{ - BaseAccount: authtypes.NewBaseAccount(alias.Address, pk, alias.AccountNumber, alias.Sequence), + BaseAccount: authtypes.NewBaseAccount(alias.Address, alias.PubKey, alias.AccountNumber, alias.Sequence), OriginalVesting: alias.OriginalVesting, DelegatedFree: alias.DelegatedFree, DelegatedVesting: alias.DelegatedVesting, @@ -536,9 +545,8 @@ func (pva PeriodicVestingAccount) String() string { // MarshalYAML returns the YAML representation of a PeriodicVestingAccount. func (pva PeriodicVestingAccount) MarshalYAML() (interface{}, error) { - alias := vestingAccountPretty{ + alias := vestingAccountYAML{ Address: pva.Address, - PubKey: pva.PubKey, AccountNumber: pva.AccountNumber, Sequence: pva.Sequence, OriginalVesting: pva.OriginalVesting, @@ -549,6 +557,16 @@ func (pva PeriodicVestingAccount) MarshalYAML() (interface{}, error) { VestingPeriods: pva.VestingPeriods, } + pk := pva.GetPubKey() + if pk != nil { + pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk) + if err != nil { + return nil, err + } + + alias.PubKey = pks + } + bz, err := yaml.Marshal(alias) if err != nil { return nil, err @@ -559,9 +577,9 @@ func (pva PeriodicVestingAccount) MarshalYAML() (interface{}, error) { // MarshalJSON returns the JSON representation of a PeriodicVestingAccount. func (pva PeriodicVestingAccount) MarshalJSON() ([]byte, error) { - alias := vestingAccountPretty{ + alias := vestingAccountJSON{ Address: pva.Address, - PubKey: pva.PubKey, + PubKey: pva.GetPubKey(), AccountNumber: pva.AccountNumber, Sequence: pva.Sequence, OriginalVesting: pva.OriginalVesting, @@ -572,30 +590,18 @@ func (pva PeriodicVestingAccount) MarshalJSON() ([]byte, error) { VestingPeriods: pva.VestingPeriods, } - return json.Marshal(alias) + return codec.Cdc.MarshalJSON(alias) } // UnmarshalJSON unmarshals raw JSON bytes into a PeriodicVestingAccount. func (pva *PeriodicVestingAccount) UnmarshalJSON(bz []byte) error { - var alias vestingAccountPretty - if err := json.Unmarshal(bz, &alias); err != nil { + var alias vestingAccountJSON + if err := codec.Cdc.UnmarshalJSON(bz, &alias); err != nil { return err } - var ( - pk crypto.PubKey - err error - ) - - if alias.PubKey != "" { - pk, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, alias.PubKey) - if err != nil { - return err - } - } - pva.BaseVestingAccount = &BaseVestingAccount{ - BaseAccount: authtypes.NewBaseAccount(alias.Address, pk, alias.AccountNumber, alias.Sequence), + BaseAccount: authtypes.NewBaseAccount(alias.Address, alias.PubKey, alias.AccountNumber, alias.Sequence), OriginalVesting: alias.OriginalVesting, DelegatedFree: alias.DelegatedFree, DelegatedVesting: alias.DelegatedVesting, @@ -676,9 +682,9 @@ func (dva DelayedVestingAccount) String() string { // MarshalJSON returns the JSON representation of a DelayedVestingAccount. func (dva DelayedVestingAccount) MarshalJSON() ([]byte, error) { - alias := vestingAccountPretty{ + alias := vestingAccountJSON{ Address: dva.Address, - PubKey: dva.PubKey, + PubKey: dva.GetPubKey(), AccountNumber: dva.AccountNumber, Sequence: dva.Sequence, OriginalVesting: dva.OriginalVesting, @@ -687,30 +693,18 @@ func (dva DelayedVestingAccount) MarshalJSON() ([]byte, error) { EndTime: dva.EndTime, } - return json.Marshal(alias) + return codec.Cdc.MarshalJSON(alias) } // UnmarshalJSON unmarshals raw JSON bytes into a DelayedVestingAccount. func (dva *DelayedVestingAccount) UnmarshalJSON(bz []byte) error { - var alias vestingAccountPretty - if err := json.Unmarshal(bz, &alias); err != nil { + var alias vestingAccountJSON + if err := codec.Cdc.UnmarshalJSON(bz, &alias); err != nil { return err } - var ( - pk crypto.PubKey - err error - ) - - if alias.PubKey != "" { - pk, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, alias.PubKey) - if err != nil { - return err - } - } - dva.BaseVestingAccount = &BaseVestingAccount{ - BaseAccount: authtypes.NewBaseAccount(alias.Address, pk, alias.AccountNumber, alias.Sequence), + BaseAccount: authtypes.NewBaseAccount(alias.Address, alias.PubKey, alias.AccountNumber, alias.Sequence), OriginalVesting: alias.OriginalVesting, DelegatedFree: alias.DelegatedFree, DelegatedVesting: alias.DelegatedVesting, diff --git a/x/auth/vesting/types/vesting_account_test.go b/x/auth/vesting/types/vesting_account_test.go index e36661111f..974a1f3149 100644 --- a/x/auth/vesting/types/vesting_account_test.go +++ b/x/auth/vesting/types/vesting_account_test.go @@ -2,7 +2,6 @@ package types_test import ( "encoding/json" - "errors" "testing" "time" @@ -589,58 +588,59 @@ func TestGenesisAccountValidate(t *testing.T) { tests := []struct { name string acc authexported.GenesisAccount - expErr error + expErr bool }{ { "valid base account", baseAcc, - nil, + false, }, { "invalid base valid account", authtypes.NewBaseAccount(addr, secp256k1.GenPrivKey().PubKey(), 0, 0), - errors.New("pubkey and address pair is invalid"), + true, }, { "valid base vesting account", baseVestingWithCoins, - nil, + false, }, { "valid continuous vesting account", types.NewContinuousVestingAccount(baseAcc, initialVesting, 100, 200), - nil, + false, }, { "invalid vesting times", types.NewContinuousVestingAccount(baseAcc, initialVesting, 1654668078, 1554668078), - errors.New("vesting start-time cannot be before end-time"), + true, }, { "valid periodic vesting account", types.NewPeriodicVestingAccount(baseAcc, initialVesting, 0, types.Periods{types.Period{Length: int64(100), Amount: sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 50)}}}), - nil, + false, }, { "invalid vesting period lengths", types.NewPeriodicVestingAccountRaw( baseVestingWithCoins, 0, types.Periods{types.Period{Length: int64(50), Amount: sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 50)}}}), - errors.New("vesting end time does not match length of all vesting periods"), + true, }, { "invalid vesting period amounts", types.NewPeriodicVestingAccountRaw( baseVestingWithCoins, 0, types.Periods{types.Period{Length: int64(100), Amount: sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 25)}}}), - errors.New("original vesting coins does not match the sum of all coins in vesting periods"), + true, }, } + for _, tt := range tests { tt := tt + t.Run(tt.name, func(t *testing.T) { - err := tt.acc.Validate() - require.Equal(t, tt.expErr, err) + require.Equal(t, tt.expErr, tt.acc.Validate() != nil) }) } } diff --git a/x/slashing/types/types.pb.go b/x/slashing/types/types.pb.go index 3537919b2c..a5444def9e 100644 --- a/x/slashing/types/types.pb.go +++ b/x/slashing/types/types.pb.go @@ -76,12 +76,17 @@ func (m *MsgUnjail) GetValidatorAddr() github_com_cosmos_cosmos_sdk_types.ValAdd // ValidatorSigningInfo defines the signing info for a validator type ValidatorSigningInfo struct { - Address github_com_cosmos_cosmos_sdk_types.ConsAddress `protobuf:"bytes,1,opt,name=address,proto3,casttype=github.com/cosmos/cosmos-sdk/types.ConsAddress" json:"address,omitempty"` - StartHeight int64 `protobuf:"varint,2,opt,name=start_height,json=startHeight,proto3" json:"start_height,omitempty" yaml:"start_height"` - IndexOffset int64 `protobuf:"varint,3,opt,name=index_offset,json=indexOffset,proto3" json:"index_offset,omitempty" yaml:"index_offset"` - JailedUntil time.Time `protobuf:"bytes,4,opt,name=jailed_until,json=jailedUntil,proto3,stdtime" json:"jailed_until" yaml:"jailed_until"` - Tombstoned bool `protobuf:"varint,5,opt,name=tombstoned,proto3" json:"tombstoned,omitempty"` - MissedBlocksCounter int64 `protobuf:"varint,6,opt,name=missed_blocks_counter,json=missedBlocksCounter,proto3" json:"missed_blocks_counter,omitempty" yaml:"missed_blocks_counter"` + Address github_com_cosmos_cosmos_sdk_types.ConsAddress `protobuf:"bytes,1,opt,name=address,proto3,casttype=github.com/cosmos/cosmos-sdk/types.ConsAddress" json:"address,omitempty"` + // height at which validator was first a candidate OR was unjailed + StartHeight int64 `protobuf:"varint,2,opt,name=start_height,json=startHeight,proto3" json:"start_height,omitempty" yaml:"start_height"` + // index offset into signed block bit array + IndexOffset int64 `protobuf:"varint,3,opt,name=index_offset,json=indexOffset,proto3" json:"index_offset,omitempty" yaml:"index_offset"` + // timestamp validator cannot be unjailed until + JailedUntil time.Time `protobuf:"bytes,4,opt,name=jailed_until,json=jailedUntil,proto3,stdtime" json:"jailed_until" yaml:"jailed_until"` + // whether or not a validator has been tombstoned (killed out of validator set) + Tombstoned bool `protobuf:"varint,5,opt,name=tombstoned,proto3" json:"tombstoned,omitempty"` + // missed blocks counter (to avoid scanning the array every time) + MissedBlocksCounter int64 `protobuf:"varint,6,opt,name=missed_blocks_counter,json=missedBlocksCounter,proto3" json:"missed_blocks_counter,omitempty" yaml:"missed_blocks_counter"` } func (m *ValidatorSigningInfo) Reset() { *m = ValidatorSigningInfo{} } diff --git a/x/slashing/types/types.proto b/x/slashing/types/types.proto index 9aafe07c3b..dfd7cf75db 100644 --- a/x/slashing/types/types.proto +++ b/x/slashing/types/types.proto @@ -11,7 +11,7 @@ message MsgUnjail { bytes validator_addr = 1 [ (gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.ValAddress", (gogoproto.moretags) = "yaml:\"address\"", - (gogoproto.jsontag) = "address" + (gogoproto.jsontag) = "address" ]; } @@ -21,15 +21,21 @@ message ValidatorSigningInfo { option (gogoproto.goproto_stringer) = false; bytes address = 1 [ - (gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.ConsAddress" // validator consensus address + (gogoproto.casttype) = + "github.com/cosmos/cosmos-sdk/types.ConsAddress" // validator consensus address ]; - int64 start_height = 2 [(gogoproto.moretags) = "yaml:\"start_height\""]; // height at which validator was first a candidate OR was unjailed - int64 index_offset = 3 [(gogoproto.moretags) = "yaml:\"index_offset\""]; // index offset into signed block bit array + // height at which validator was first a candidate OR was unjailed + int64 start_height = 2 [(gogoproto.moretags) = "yaml:\"start_height\""]; + // index offset into signed block bit array + int64 index_offset = 3 [(gogoproto.moretags) = "yaml:\"index_offset\""]; + // timestamp validator cannot be unjailed until google.protobuf.Timestamp jailed_until = 4 [ - (gogoproto.moretags) = "yaml:\"jailed_until\"", + (gogoproto.moretags) = "yaml:\"jailed_until\"", (gogoproto.stdtime) = true, (gogoproto.nullable) = false - ]; // timestamp validator cannot be unjailed until - bool tombstoned = 5; // whether or not a validator has been tombstoned (killed out of validator set) - int64 missed_blocks_counter = 6 [(gogoproto.moretags) = "yaml:\"missed_blocks_counter\""]; // missed blocks counter (to avoid scanning the array every time) -} \ No newline at end of file + ]; + // whether or not a validator has been tombstoned (killed out of validator set) + bool tombstoned = 5; + // missed blocks counter (to avoid scanning the array every time) + int64 missed_blocks_counter = 6 [(gogoproto.moretags) = "yaml:\"missed_blocks_counter\""]; +}