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

proto: migrate x/distr to use hybrid codec #5610

Merged
merged 54 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
dec703b
Migrate staking types to proto
alexanderbez Feb 1, 2020
31311cb
Use int32 for validator status
alexanderbez Feb 3, 2020
0d2938b
Update staking types
alexanderbez Feb 3, 2020
60dc367
Add benchmarks for bech32 pubkey funcs
alexanderbez Feb 3, 2020
f0f6f50
Update BondStatus type alias to int32
alexanderbez Feb 3, 2020
c268a9e
Remove PubKey proto type
alexanderbez Feb 3, 2020
95edc79
Fix unit test
alexanderbez Feb 3, 2020
46be4b4
Update staking keeper
alexanderbez Feb 3, 2020
7503396
Fix staking keeper tests
alexanderbez Feb 3, 2020
97ac0b5
Update query client
alexanderbez Feb 3, 2020
8e84801
Update staking genesis and app_test
alexanderbez Feb 3, 2020
c35939a
Update staking handler
alexanderbez Feb 4, 2020
febaae8
Use Int64Value instead of IntProto
alexanderbez Feb 4, 2020
5b83d9b
Updates tests and APIs
alexanderbez Feb 4, 2020
0a904f1
Fix iteration
alexanderbez Feb 4, 2020
1ee4527
proto: move distr over to proto
tac0turtle Feb 4, 2020
7fb0d34
proto: some more types to proto
tac0turtle Feb 4, 2020
6dc3dd9
fix protolinting
tac0turtle Feb 4, 2020
2450409
Linting
alexanderbez Feb 4, 2020
1ce8248
minor reading, will wait on staking pr to be merged
tac0turtle Feb 4, 2020
97148e2
Fix linting
alexanderbez Feb 4, 2020
da9d51f
Merge branch 'master' into bez/5444-staking-proto-enc
alexanderbez Feb 4, 2020
11a0d8b
Add changelog entry
alexanderbez Feb 4, 2020
db45cfc
Update changelog entry
alexanderbez Feb 4, 2020
c5a7baa
Merge branch 'master' into bez/5444-staking-proto-enc
alexanderbez Feb 4, 2020
6140d37
Merge branch 'bez/5444-staking-proto-enc' into proto/distr
tac0turtle Feb 5, 2020
d0b2786
referencing types
tac0turtle Feb 5, 2020
c68058d
change some types to proto
tac0turtle Feb 5, 2020
37bc9c4
make it build, kinda
tac0turtle Feb 5, 2020
8c2431a
its alive
tac0turtle Feb 6, 2020
bdf9455
fix tests
tac0turtle Feb 6, 2020
ccc799c
move slashevents to proto
tac0turtle Feb 6, 2020
59d8427
bump buf to 0.7
tac0turtle Feb 6, 2020
728ac20
remove here
tac0turtle Feb 6, 2020
677a8f1
Merge branch 'master' into proto/distr
tac0turtle Feb 7, 2020
77b6f1f
add applevelcodec
tac0turtle Feb 7, 2020
41426fa
add make cmd for tendermint proto files update
tac0turtle Feb 7, 2020
789a03d
fix spacing
tac0turtle Feb 7, 2020
a2346d3
add changelog
tac0turtle Feb 7, 2020
3019263
Merge branch 'master' into proto/distr
tac0turtle Feb 7, 2020
fcbe7c3
Fix x/crisis linting
alexanderbez Feb 7, 2020
28591b3
Verify and cleanup proto types
alexanderbez Feb 7, 2020
230829b
Update CHANGELOG.md
alexanderbez Feb 10, 2020
7f2140d
Update x/distribution/keeper/delegation.go
alexanderbez Feb 10, 2020
cad3caf
Update x/distribution/keeper/invariants.go
alexanderbez Feb 10, 2020
7a5ebff
Merge branch 'master' into proto/distr
alexanderbez Feb 10, 2020
ebe0140
Review suggestions/linting
alexanderbez Feb 10, 2020
69f965a
Review suggestions/linting
alexanderbez Feb 10, 2020
d680eac
change json tag of height
tac0turtle Feb 11, 2020
1988305
revet query.go changes
tac0turtle Feb 11, 2020
45159b8
Merge branch 'master' into proto/distr
tac0turtle Feb 11, 2020
0d71b3b
add sed command to third_party tendermint cmds
tac0turtle Feb 11, 2020
6cd4103
Update changelog entry
alexanderbez Feb 11, 2020
970562d
Merge branch 'master' into proto/distr
alexanderbez Feb 11, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update staking types
  • Loading branch information
alexanderbez committed Feb 3, 2020
commit 0d2938ba1712ea05cba4135c824abf94575a36ef
1 change: 1 addition & 0 deletions x/staking/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ var (
ErrNeitherShareMsgsGiven = sdkerrors.Register(ModuleName, 43, "neither shares amount nor shares percent provided")
ErrInvalidHistoricalInfo = sdkerrors.Register(ModuleName, 44, "invalid historical info")
ErrNoHistoricalInfo = sdkerrors.Register(ModuleName, 45, "no historical info found")
ErrEmptyValidatorPubKey = sdkerrors.Register(ModuleName, 46, "empty validator public key")
)
47 changes: 7 additions & 40 deletions x/staking/types/historical_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,19 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// HistoricalInfo contains the historical information that gets stored at each height
type HistoricalInfo struct {
Header abci.Header `json:"header" yaml:"header"`
ValSet Validators `json:"valset" yaml:"valset"`
}

// NewHistoricalInfo will create a historical information struct from header and valset
// it will first sort valset before inclusion into historical info
func NewHistoricalInfo(header abci.Header, valSet Validators) HistoricalInfo {
sort.Sort(valSet)
return HistoricalInfo{
Header: header,
ValSet: valSet,
}
}

// ToProto converts a HistoricalInfo into a HistoricalInfoProto type.
func (hi HistoricalInfo) ToProto() HistoricalInfoProto {
valsetProto := make([]ValidatorProto, len(hi.ValSet))
for i, val := range hi.ValSet {
valsetProto[i] = val.ToProto()
}

return HistoricalInfoProto{
Header: hi.Header,
Valset: valsetProto,
Valset: valSet,
}
}

// ToHistoricalInfo converts a HistoricalInfoProto to a HistoricalInfo type.
func (hip HistoricalInfoProto) ToHistoricalInfo() HistoricalInfo {
valset := make(Validators, len(hip.Valset))
for i, valProto := range hip.Valset {
valset[i] = valProto.ToValidator()
}

return NewHistoricalInfo(hip.Header, valset)
}

// MustMarshalHistoricalInfo wll marshal historical info and panic on error
func MustMarshalHistoricalInfo(cdc codec.Marshaler, hi HistoricalInfo) []byte {
hiProto := hi.ToProto()
return cdc.MustMarshalBinaryLengthPrefixed(&hiProto)
return cdc.MustMarshalBinaryLengthPrefixed(&hi)
}

// MustUnmarshalHistoricalInfo wll unmarshal historical info and panic on error
Expand All @@ -65,21 +35,18 @@ func MustUnmarshalHistoricalInfo(cdc codec.Marshaler, value []byte) HistoricalIn

// UnmarshalHistoricalInfo will unmarshal historical info and return any error
func UnmarshalHistoricalInfo(cdc codec.Marshaler, value []byte) (hi HistoricalInfo, err error) {
hip := HistoricalInfoProto{}
if err := cdc.UnmarshalBinaryLengthPrefixed(value, &hip); err != nil {
return HistoricalInfo{}, err
}

return hip.ToHistoricalInfo(), nil
err = cdc.UnmarshalBinaryLengthPrefixed(value, &hi)
return hi, err
}

// ValidateBasic will ensure HistoricalInfo is not nil and sorted
func ValidateBasic(hi HistoricalInfo) error {
if len(hi.ValSet) == 0 {
if len(hi.Valset) == 0 {
return sdkerrors.Wrap(ErrInvalidHistoricalInfo, "validator set is empty")
}
if !sort.IsSorted(Validators(hi.ValSet)) {
if !sort.IsSorted(Validators(hi.Valset)) {
return sdkerrors.Wrap(ErrInvalidHistoricalInfo, "validator set is not sorted by address")
}

return nil
}
6 changes: 3 additions & 3 deletions x/staking/types/historical_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (

func TestHistoricalInfo(t *testing.T) {
hi := NewHistoricalInfo(header, validators)
require.True(t, sort.IsSorted(Validators(hi.ValSet)), "Validators are not sorted")
require.True(t, sort.IsSorted(Validators(hi.Valset)), "Validators are not sorted")

var value []byte
require.NotPanics(t, func() {
Expand All @@ -35,7 +35,7 @@ func TestHistoricalInfo(t *testing.T) {
recv, err := UnmarshalHistoricalInfo(ModuleCdc, value)
require.Nil(t, err, "Unmarshalling HistoricalInfo failed")
require.Equal(t, hi, recv, "Unmarshalled HistoricalInfo is different from original")
require.True(t, sort.IsSorted(Validators(hi.ValSet)), "Validators are not sorted")
require.True(t, sort.IsSorted(Validators(hi.Valset)), "Validators are not sorted")
}

func TestValidateBasic(t *testing.T) {
Expand All @@ -56,7 +56,7 @@ func TestValidateBasic(t *testing.T) {

hi = HistoricalInfo{
Header: header,
ValSet: validators,
Valset: validators,
}
err = ValidateBasic(hi)
require.Error(t, err, "ValidateBasic passed on unsorted ValSet")
Expand Down
138 changes: 9 additions & 129 deletions x/staking/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ package types

import (
"bytes"
"encoding/json"

"github.com/tendermint/tendermint/crypto"
yaml "gopkg.in/yaml.v2"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// ensure Msg interface compliance at compile time
var (
_ sdk.Msg = &MsgCreateValidator{}
_ sdk.Msg = &MsgEditValidator{}
Expand All @@ -20,41 +17,23 @@ var (
_ sdk.Msg = &MsgBeginRedelegate{}
)

//______________________________________________________________________

// MsgCreateValidator - struct for bonding transactions
type MsgCreateValidator struct {
Description Description `json:"description" yaml:"description"`
Commission CommissionRates `json:"commission" yaml:"commission"`
MinSelfDelegation sdk.Int `json:"min_self_delegation" yaml:"min_self_delegation"`
DelegatorAddress sdk.AccAddress `json:"delegator_address" yaml:"delegator_address"`
ValidatorAddress sdk.ValAddress `json:"validator_address" yaml:"validator_address"`
PubKey crypto.PubKey `json:"pubkey" yaml:"pubkey"`
Value sdk.Coin `json:"value" yaml:"value"`
}

type msgCreateValidatorJSON struct {
Description Description `json:"description" yaml:"description"`
Commission CommissionRates `json:"commission" yaml:"commission"`
MinSelfDelegation sdk.Int `json:"min_self_delegation" yaml:"min_self_delegation"`
DelegatorAddress sdk.AccAddress `json:"delegator_address" yaml:"delegator_address"`
ValidatorAddress sdk.ValAddress `json:"validator_address" yaml:"validator_address"`
PubKey string `json:"pubkey" yaml:"pubkey"`
Value sdk.Coin `json:"value" yaml:"value"`
}

// NewMsgCreateValidator creates a new MsgCreateValidator instance.
// Delegator address and validator address are the same.
func NewMsgCreateValidator(
valAddr sdk.ValAddress, pubKey crypto.PubKey, selfDelegation sdk.Coin,
description Description, commission CommissionRates, minSelfDelegation sdk.Int,
) MsgCreateValidator {

var pkStr string
if pubKey != nil {
pkStr = sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, pubKey)
}

return MsgCreateValidator{
Description: description,
DelegatorAddress: sdk.AccAddress(valAddr),
ValidatorAddress: valAddr,
PubKey: pubKey,
Pubkey: pkStr,
Value: selfDelegation,
Commission: commission,
MinSelfDelegation: minSelfDelegation,
Expand All @@ -81,70 +60,6 @@ func (msg MsgCreateValidator) GetSigners() []sdk.AccAddress {
return addrs
}

// MarshalJSON implements the json.Marshaler interface to provide custom JSON
// serialization of the MsgCreateValidator type.
func (msg MsgCreateValidator) MarshalJSON() ([]byte, error) {
return json.Marshal(msgCreateValidatorJSON{
Description: msg.Description,
Commission: msg.Commission,
DelegatorAddress: msg.DelegatorAddress,
ValidatorAddress: msg.ValidatorAddress,
PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, msg.PubKey),
Value: msg.Value,
MinSelfDelegation: msg.MinSelfDelegation,
})
}

// UnmarshalJSON implements the json.Unmarshaler interface to provide custom
// JSON deserialization of the MsgCreateValidator type.
func (msg *MsgCreateValidator) UnmarshalJSON(bz []byte) error {
var msgCreateValJSON msgCreateValidatorJSON
if err := json.Unmarshal(bz, &msgCreateValJSON); err != nil {
return err
}

msg.Description = msgCreateValJSON.Description
msg.Commission = msgCreateValJSON.Commission
msg.DelegatorAddress = msgCreateValJSON.DelegatorAddress
msg.ValidatorAddress = msgCreateValJSON.ValidatorAddress
var err error
msg.PubKey, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, msgCreateValJSON.PubKey)
if err != nil {
return err
}
msg.Value = msgCreateValJSON.Value
msg.MinSelfDelegation = msgCreateValJSON.MinSelfDelegation

return nil
}

// MarshalYAML implements a custom marshal yaml function due to consensus pubkey.
func (msg MsgCreateValidator) MarshalYAML() (interface{}, error) {
bs, err := yaml.Marshal(struct {
Description Description
Commission CommissionRates
MinSelfDelegation sdk.Int
DelegatorAddress sdk.AccAddress
ValidatorAddress sdk.ValAddress
PubKey string
Value sdk.Coin
}{
Description: msg.Description,
Commission: msg.Commission,
MinSelfDelegation: msg.MinSelfDelegation,
DelegatorAddress: msg.DelegatorAddress,
ValidatorAddress: msg.ValidatorAddress,
PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, msg.PubKey),
Value: msg.Value,
})

if err != nil {
return nil, err
}

return string(bs), nil
}

// GetSignBytes returns the message bytes to sign over.
func (msg MsgCreateValidator) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
Expand All @@ -163,6 +78,9 @@ func (msg MsgCreateValidator) ValidateBasic() error {
if !sdk.AccAddress(msg.ValidatorAddress).Equals(msg.DelegatorAddress) {
return ErrBadValidatorAddr
}
if msg.Pubkey == "" {
return ErrEmptyValidatorPubKey
}
if !msg.Value.Amount.IsPositive() {
return ErrBadDelegationAmount
}
Expand All @@ -185,20 +103,6 @@ func (msg MsgCreateValidator) ValidateBasic() error {
return nil
}

// MsgEditValidator - struct for editing a validator
type MsgEditValidator struct {
Description Description `json:"description" yaml:"description"`
ValidatorAddress sdk.ValAddress `json:"address" yaml:"address"`

// We pass a reference to the new commission rate and min self delegation as it's not mandatory to
// update. If not updated, the deserialized rate will be zero with no way to
// distinguish if an update was intended.
//
// REF: #2373
CommissionRate *sdk.Dec `json:"commission_rate" yaml:"commission_rate"`
MinSelfDelegation *sdk.Int `json:"min_self_delegation" yaml:"min_self_delegation"`
}

// NewMsgEditValidator creates a new MsgEditValidator instance
func NewMsgEditValidator(valAddr sdk.ValAddress, description Description, newRate *sdk.Dec, newMinSelfDelegation *sdk.Int) MsgEditValidator {
return MsgEditValidator{
Expand Down Expand Up @@ -246,13 +150,6 @@ func (msg MsgEditValidator) ValidateBasic() error {
return nil
}

// MsgDelegate - struct for bonding transactions
type MsgDelegate struct {
DelegatorAddress sdk.AccAddress `json:"delegator_address" yaml:"delegator_address"`
ValidatorAddress sdk.ValAddress `json:"validator_address" yaml:"validator_address"`
Amount sdk.Coin `json:"amount" yaml:"amount"`
}

// NewMsgDelegate creates a new MsgDelegate instance.
func NewMsgDelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount sdk.Coin) MsgDelegate {
return MsgDelegate{
Expand Down Expand Up @@ -293,16 +190,6 @@ func (msg MsgDelegate) ValidateBasic() error {
return nil
}

//______________________________________________________________________

// MsgBeginRedelegate defines the attributes of a bonding transaction.
type MsgBeginRedelegate struct {
DelegatorAddress sdk.AccAddress `json:"delegator_address" yaml:"delegator_address"`
ValidatorSrcAddress sdk.ValAddress `json:"validator_src_address" yaml:"validator_src_address"`
ValidatorDstAddress sdk.ValAddress `json:"validator_dst_address" yaml:"validator_dst_address"`
Amount sdk.Coin `json:"amount" yaml:"amount"`
}

// NewMsgBeginRedelegate creates a new MsgBeginRedelegate instance.
func NewMsgBeginRedelegate(
delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress, amount sdk.Coin,
Expand Down Expand Up @@ -349,13 +236,6 @@ func (msg MsgBeginRedelegate) ValidateBasic() error {
return nil
}

// MsgUndelegate - struct for unbonding transactions
type MsgUndelegate struct {
DelegatorAddress sdk.AccAddress `json:"delegator_address" yaml:"delegator_address"`
ValidatorAddress sdk.ValAddress `json:"validator_address" yaml:"validator_address"`
Amount sdk.Coin `json:"amount" yaml:"amount"`
}

// NewMsgUndelegate creates a new MsgUndelegate instance.
func NewMsgUndelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount sdk.Coin) MsgUndelegate {
return MsgUndelegate{
Expand Down
47 changes: 0 additions & 47 deletions x/staking/types/msg_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package types

import (
"fmt"
"testing"

yaml "gopkg.in/yaml.v2"

"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto"

Expand Down Expand Up @@ -157,47 +154,3 @@ func TestMsgUndelegate(t *testing.T) {
}
}
}

//test to validate if NewMsgCreateValidator implements yaml marshaller
func TestMsgMarshalYAML(t *testing.T) {
commission1 := NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
tc := struct {
name, moniker, identity, website, securityContact, details string
CommissionRates CommissionRates
minSelfDelegation sdk.Int
validatorAddr sdk.ValAddress
pubkey crypto.PubKey
bond sdk.Coin
expectPass bool
}{"basic good", "a", "b", "c", "d", "e", commission1, sdk.OneInt(), valAddr1, pk1, coinPos, true}

description := NewDescription(tc.moniker, tc.identity, tc.website, tc.securityContact, tc.details)
msg := NewMsgCreateValidator(tc.validatorAddr, tc.pubkey, tc.bond, description, tc.CommissionRates, tc.minSelfDelegation)
bs, err := yaml.Marshal(msg)
require.NoError(t, err)
bechifiedPub, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, msg.PubKey)
require.NoError(t, err)

want := fmt.Sprintf(`|
description:
moniker: a
identity: b
website: c
security_contact: d
details: e
commission:
rate: "0.000000000000000000"
max_rate: "0.000000000000000000"
max_change_rate: "0.000000000000000000"
minselfdelegation: "1"
delegatoraddress: %s
validatoraddress: %s
pubkey: %s
value:
denom: stake
amount: "1000"
`, msg.DelegatorAddress, msg.ValidatorAddress, bechifiedPub)

require.Equal(t, want, string(bs))

}
Loading