github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/modules/core/02-client/keeper/proposal.go (about) 1 package keeper 2 3 import ( 4 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 5 sdkerrors "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors" 6 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/upgrade" 7 "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/02-client/types" 8 "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/exported" 9 ) 10 11 // ClientUpdateProposal will try to update the client with the new header if and only if 12 // the proposal passes. The localhost client is not allowed to be modified with a proposal. 13 func (k Keeper) ClientUpdateProposal(ctx sdk.Context, p *types.ClientUpdateProposal) error { 14 if p.SubjectClientId == exported.Localhost || p.SubstituteClientId == exported.Localhost { 15 return sdkerrors.Wrap(types.ErrInvalidUpdateClientProposal, "cannot update localhost client with proposal") 16 } 17 18 subjectClientState, found := k.GetClientState(ctx, p.SubjectClientId) 19 if !found { 20 return sdkerrors.Wrapf(types.ErrClientNotFound, "subject client with ID %s", p.SubjectClientId) 21 } 22 23 subjectClientStore := k.ClientStore(ctx, p.SubjectClientId) 24 25 if status := subjectClientState.Status(ctx, subjectClientStore, k.cdc); status == exported.Active { 26 return sdkerrors.Wrap(types.ErrInvalidUpdateClientProposal, "cannot update Active subject client") 27 } 28 29 substituteClientState, found := k.GetClientState(ctx, p.SubstituteClientId) 30 if !found { 31 return sdkerrors.Wrapf(types.ErrClientNotFound, "substitute client with ID %s", p.SubstituteClientId) 32 } 33 34 if subjectClientState.GetLatestHeight().GTE(substituteClientState.GetLatestHeight()) { 35 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()) 36 } 37 38 substituteClientStore := k.ClientStore(ctx, p.SubstituteClientId) 39 40 if status := substituteClientState.Status(ctx, substituteClientStore, k.cdc); status != exported.Active { 41 return sdkerrors.Wrapf(types.ErrClientNotActive, "substitute client is not Active, status is %s", status) 42 } 43 44 clientState, err := subjectClientState.CheckSubstituteAndUpdateState(ctx, k.cdc, subjectClientStore, substituteClientStore, substituteClientState) 45 if err != nil { 46 return err 47 } 48 k.SetClientState(ctx, p.SubjectClientId, clientState) 49 50 k.Logger(ctx).Info("client updated after governance proposal passed", "client-id", p.SubjectClientId, "height", clientState.GetLatestHeight().String()) 51 52 // emitting events in the keeper for proposal updates to clients 53 ctx.EventManager().EmitEvent( 54 sdk.NewEvent( 55 types.EventTypeUpdateClientProposal, 56 sdk.NewAttribute(types.AttributeKeySubjectClientID, p.SubjectClientId), 57 //Ywmet todo add 58 sdk.NewAttribute(types.AttributeKeyClientType, clientState.ClientType()), 59 sdk.NewAttribute(types.AttributeKeyConsensusHeight, clientState.GetLatestHeight().String()), 60 ), 61 ) 62 63 return nil 64 } 65 66 func (k Keeper) HandleUpgradeProposal(ctx sdk.Context, p *types.UpgradeProposal) error { 67 clientState, err := types.UnpackClientState(p.UpgradedClientState) 68 if err != nil { 69 return sdkerrors.Wrap(err, "could not unpack UpgradedClientState") 70 } 71 72 // zero out any custom fields before setting 73 cs := clientState.ZeroCustomFields() 74 bz, err := types.MarshalClientState(k.cdc, cs) 75 if err != nil { 76 return sdkerrors.Wrap(err, "could not marshal UpgradedClientState") 77 } 78 79 if err := k.upgradeKeeper.ScheduleUpgrade(ctx, upgrade.Plan{ 80 Name: p.Plan.Name, 81 Time: p.Plan.Time, 82 Height: p.Plan.Height, 83 Info: p.Plan.Info, 84 }); err != nil { 85 return err 86 } 87 88 // sets the new upgraded client in last height committed on this chain is at plan.Height, 89 // since the chain will panic at plan.Height and new chain will resume at plan.Height 90 return k.upgradeKeeper.SetUpgradedClient(ctx, p.Plan.Height, bz) 91 }