-
Notifications
You must be signed in to change notification settings - Fork 31
/
plan.go
88 lines (77 loc) · 2.53 KB
/
plan.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package types
import (
"fmt"
"strings"
"time"
codectypes "github.com/line/lbm-sdk/v2/codec/types"
sdk "github.com/line/lbm-sdk/v2/types"
sdkerrors "github.com/line/lbm-sdk/v2/types/errors"
clienttypes "github.com/line/lbm-sdk/v2/x/ibc/core/02-client/types"
ibcexported "github.com/line/lbm-sdk/v2/x/ibc/core/exported"
)
var _ codectypes.UnpackInterfacesMessage = Plan{}
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()
}
return fmt.Sprintf(`Upgrade Plan
Name: %s
%s
Info: %s.
Upgraded IBC Client: %s`, p.Name, dueUp, p.Info, upgradedClientStr)
}
// ValidateBasic does basic validation of a Plan
func (p Plan) ValidateBasic() error {
if len(p.Name) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "name cannot be empty")
}
if p.Height < 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "height cannot be negative")
}
if p.Time.Unix() <= 0 && p.Height == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "must set either time or height")
}
if p.Time.Unix() > 0 && p.Height != 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "cannot set both time and height")
}
if p.Time.Unix() > 0 && p.UpgradedClientState != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "IBC chain upgrades must only set height")
}
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.Unix() > 0 {
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.Unix() > 0 {
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)
}