cosmos-sdk/x/upgrade/types/plan.go

89 lines
2.5 KiB
Go
Raw Normal View History

2019-11-08 06:40:56 -08:00
package types
import (
"fmt"
"strings"
"time"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
2019-11-08 06:40:56 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
clienttypes "github.com/cosmos/cosmos-sdk/x/ibc/core/02-client/types"
ibcexported "github.com/cosmos/cosmos-sdk/x/ibc/core/exported"
2019-11-08 06:40:56 -08:00
)
var _ codectypes.UnpackInterfacesMessage = Plan{}
2019-11-08 06:40:56 -08:00
func (p Plan) String() string {
due := p.DueAt()
dueUp := strings.ToUpper(due[0:1]) + due[1:]
var upgradedClientStr string
upgradedClient, err := clienttypes.UnpackClientState(p.UpgradedClientState)
if err != nil {
upgradedClientStr = "no upgraded client provided"
} else {
upgradedClientStr = upgradedClient.String()
}
2019-11-08 06:40:56 -08:00
return fmt.Sprintf(`Upgrade Plan
Name: %s
%s
Use any as validator pubkey (#7597) * protobuf pubkey type update * wip2 * wip3 * solving types.NewValidator issues * remove bech32 from validator type assignment * update Validator interface * Changelog update * wip4 * update genutil * fix simapp & x/ibc/testing tests * update staking * changelog update * fix import cycle in tests * fix amino panic on TestValidatorMarshalUnmarshalJSON * fix TestValidatorMarshalUnmarshalJSON consensus_pubkey check * Add UnpackInterfaces to HistoricalInfo * fix TestHistoricalInfo * update todos * fix: Expecting ed25519.PubKey to implement proto.Message * fix linter issues * Fix migrate test * Update CHANGELOG.md Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * review comments * cosmetic changes * add UnpackInterfaces got GenesisRandomized test * Validator.Equal reuses Validator.MinEqual * fix test * use Validator.Equal in tests * Fix staking simulation TestRandomizedGenState * Remove TODO * use HistoricalInfo.Equal * use proto.Equal * rename Validator.GetConsPubKey to TmConsPubKey * prefer require.Equal over reflect.DeepEqual * SetHistoricalInfo using a pointer * Fix TestQueryDelegation test * Fix TestQueryValidators test * Fix TestSimulateMsgUnjail test * experiement with LegacyAmino instances * Register codecs in all simapp tests * Fix cli_test compilation problems * fix typo sdk -> std * fix typo * fix TestPlanStringer * Rename to MakeEncodingConfig * Remove RegisterCodecsTests * Use gRPC in GetCmdQueryValidators * Empty status * fix info log check * linter fixes * rename simapparams to simappparams * Update simapp/test_helpers.go Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * comments updates * use valAddr1 instead of sdk.ValAddress(pk1.Address().Bytes()) Co-authored-by: Cory Levinson <cjlevinson@gmail.com> Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com>
2020-10-23 05:07:52 -07:00
Info: %s.
Upgraded IBC Client: %s`, p.Name, dueUp, p.Info, upgradedClientStr)
2019-11-08 06:40:56 -08:00
}
// ValidateBasic does basic validation of a Plan
func (p Plan) ValidateBasic() error {
2019-11-08 06:40:56 -08:00
if len(p.Name) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "name cannot be empty")
2019-11-08 06:40:56 -08:00
}
if p.Height < 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "height cannot be negative")
2019-11-08 06:40:56 -08:00
}
if p.Time.IsZero() && p.Height == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "must set either time or height")
2019-11-08 06:40:56 -08:00
}
if !p.Time.IsZero() && p.Height != 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "cannot set both time and height")
2019-11-08 06:40:56 -08:00
}
if !p.Time.IsZero() && p.UpgradedClientState != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "IBC chain upgrades must only set height")
}
2019-11-08 06:40:56 -08:00
return nil
}
// ShouldExecute returns true if the Plan is ready to execute given the current context
func (p Plan) ShouldExecute(ctx sdk.Context) bool {
if !p.Time.IsZero() {
return !ctx.BlockTime().Before(p.Time)
}
if p.Height > 0 {
return p.Height <= ctx.BlockHeight()
}
return false
}
// DueAt is a string representation of when this plan is due to be executed
func (p Plan) DueAt() string {
if !p.Time.IsZero() {
return fmt.Sprintf("time: %s", p.Time.UTC().Format(time.RFC3339))
}
return fmt.Sprintf("height: %d", p.Height)
}
// IsIBCPlan will return true if plan includes IBC client information
func (p Plan) IsIBCPlan() bool {
return p.UpgradedClientState != nil
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (p Plan) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
// UpgradedClientState may be nil
if p.UpgradedClientState == nil {
return nil
}
var clientState ibcexported.ClientState
return unpacker.UnpackAny(p.UpgradedClientState, &clientState)
}