-
Notifications
You must be signed in to change notification settings - Fork 1
/
proposal.go
91 lines (74 loc) · 3.74 KB
/
proposal.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
89
90
91
package keeper
import (
sdk "github.com/FiboChain/fbc/libs/cosmos-sdk/types"
sdkerrors "github.com/FiboChain/fbc/libs/cosmos-sdk/types/errors"
"github.com/FiboChain/fbc/libs/cosmos-sdk/x/upgrade"
"github.com/FiboChain/fbc/libs/ibc-go/modules/core/02-client/types"
"github.com/FiboChain/fbc/libs/ibc-go/modules/core/exported"
)
// ClientUpdateProposal will try to update the client with the new header if and only if
// the proposal passes. The localhost client is not allowed to be modified with a proposal.
func (k Keeper) ClientUpdateProposal(ctx sdk.Context, p *types.ClientUpdateProposal) error {
if p.SubjectClientId == exported.Localhost || p.SubstituteClientId == exported.Localhost {
return sdkerrors.Wrap(types.ErrInvalidUpdateClientProposal, "cannot update localhost client with proposal")
}
subjectClientState, found := k.GetClientState(ctx, p.SubjectClientId)
if !found {
return sdkerrors.Wrapf(types.ErrClientNotFound, "subject client with ID %s", p.SubjectClientId)
}
subjectClientStore := k.ClientStore(ctx, p.SubjectClientId)
if status := subjectClientState.Status(ctx, subjectClientStore, k.cdc); status == exported.Active {
return sdkerrors.Wrap(types.ErrInvalidUpdateClientProposal, "cannot update Active subject client")
}
substituteClientState, found := k.GetClientState(ctx, p.SubstituteClientId)
if !found {
return sdkerrors.Wrapf(types.ErrClientNotFound, "substitute client with ID %s", p.SubstituteClientId)
}
if subjectClientState.GetLatestHeight().GTE(substituteClientState.GetLatestHeight()) {
return sdkerrors.Wrapf(types.ErrInvalidHeight, "subject client state latest height is greater or equal to substitute client state latest height (%s >= %s)", subjectClientState.GetLatestHeight(), substituteClientState.GetLatestHeight())
}
substituteClientStore := k.ClientStore(ctx, p.SubstituteClientId)
if status := substituteClientState.Status(ctx, substituteClientStore, k.cdc); status != exported.Active {
return sdkerrors.Wrapf(types.ErrClientNotActive, "substitute client is not Active, status is %s", status)
}
clientState, err := subjectClientState.CheckSubstituteAndUpdateState(ctx, k.cdc, subjectClientStore, substituteClientStore, substituteClientState)
if err != nil {
return err
}
k.SetClientState(ctx, p.SubjectClientId, clientState)
k.Logger(ctx).Info("client updated after governance proposal passed", "client-id", p.SubjectClientId, "height", clientState.GetLatestHeight().String())
// emitting events in the keeper for proposal updates to clients
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeUpdateClientProposal,
sdk.NewAttribute(types.AttributeKeySubjectClientID, p.SubjectClientId),
//Ywmet todo add
sdk.NewAttribute(types.AttributeKeyClientType, clientState.ClientType()),
sdk.NewAttribute(types.AttributeKeyConsensusHeight, clientState.GetLatestHeight().String()),
),
)
return nil
}
func (k Keeper) HandleUpgradeProposal(ctx sdk.Context, p *types.UpgradeProposal) error {
clientState, err := types.UnpackClientState(p.UpgradedClientState)
if err != nil {
return sdkerrors.Wrap(err, "could not unpack UpgradedClientState")
}
// zero out any custom fields before setting
cs := clientState.ZeroCustomFields()
bz, err := types.MarshalClientState(k.cdc, cs)
if err != nil {
return sdkerrors.Wrap(err, "could not marshal UpgradedClientState")
}
if err := k.upgradeKeeper.ScheduleUpgrade(ctx, upgrade.Plan{
Name: p.Plan.Name,
Time: p.Plan.Time,
Height: p.Plan.Height,
Info: p.Plan.Info,
}); err != nil {
return err
}
// sets the new upgraded client in last height committed on this chain is at plan.Height,
// since the chain will panic at plan.Height and new chain will resume at plan.Height
return k.upgradeKeeper.SetUpgradedClient(ctx, p.Plan.Height, bz)
}