package exported import ( "encoding/json" ics23 "github.com/confio/ics23/go" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/codec" ) // ClientState defines the required common functions for light clients. type ClientState interface { ClientType() ClientType GetLatestHeight() Height IsFrozen() bool GetFrozenHeight() Height Validate() error GetProofSpecs() []*ics23.ProofSpec // Update and Misbehaviour functions CheckHeaderAndUpdateState(sdk.Context, codec.BinaryMarshaler, sdk.KVStore, Header) (ClientState, ConsensusState, error) CheckMisbehaviourAndUpdateState(sdk.Context, codec.BinaryMarshaler, sdk.KVStore, Misbehaviour) (ClientState, error) CheckProposedHeaderAndUpdateState(sdk.Context, codec.BinaryMarshaler, sdk.KVStore, Header) (ClientState, ConsensusState, error) // State verification functions VerifyClientState( store sdk.KVStore, cdc codec.BinaryMarshaler, root Root, height Height, prefix Prefix, counterpartyClientIdentifier string, proof []byte, clientState ClientState, ) error VerifyClientConsensusState( store sdk.KVStore, cdc codec.BinaryMarshaler, root Root, height Height, counterpartyClientIdentifier string, consensusHeight Height, prefix Prefix, proof []byte, consensusState ConsensusState, ) error VerifyConnectionState( store sdk.KVStore, cdc codec.BinaryMarshaler, height Height, prefix Prefix, proof []byte, connectionID string, connectionEnd ConnectionI, ) error VerifyChannelState( store sdk.KVStore, cdc codec.BinaryMarshaler, height Height, prefix Prefix, proof []byte, portID, channelID string, channel ChannelI, ) error VerifyPacketCommitment( store sdk.KVStore, cdc codec.BinaryMarshaler, height Height, prefix Prefix, proof []byte, portID, channelID string, sequence uint64, commitmentBytes []byte, ) error VerifyPacketAcknowledgement( store sdk.KVStore, cdc codec.BinaryMarshaler, height Height, prefix Prefix, proof []byte, portID, channelID string, sequence uint64, acknowledgement []byte, ) error VerifyPacketAcknowledgementAbsence( store sdk.KVStore, cdc codec.BinaryMarshaler, height Height, prefix Prefix, proof []byte, portID, channelID string, sequence uint64, ) error VerifyNextSequenceRecv( store sdk.KVStore, cdc codec.BinaryMarshaler, height Height, prefix Prefix, proof []byte, portID, channelID string, nextSequenceRecv uint64, ) error } // ConsensusState is the state of the consensus process type ConsensusState interface { ClientType() ClientType // Consensus kind // GetRoot returns the commitment root of the consensus state, // which is used for key-value pair verification. GetRoot() Root // GetTimestamp returns the timestamp (in nanoseconds) of the consensus state GetTimestamp() uint64 ValidateBasic() error } // TypeClientMisbehaviour is the shared evidence misbehaviour type const TypeClientMisbehaviour string = "client_misbehaviour" // Misbehaviour defines counterparty misbehaviour for a specific consensus type type Misbehaviour interface { ClientType() ClientType GetClientID() string String() string ValidateBasic() error // Height at which the infraction occurred GetHeight() Height } // Header is the consensus state update information type Header interface { ClientType() ClientType GetHeight() Height ValidateBasic() error } // Height is a wrapper interface over clienttypes.Height // all clients must use the concrete implementation in types type Height interface { IsZero() bool LT(Height) bool LTE(Height) bool EQ(Height) bool GT(Height) bool GTE(Height) bool GetEpochNumber() uint64 GetEpochHeight() uint64 Decrement() (Height, bool) String() string } // ClientType defines the type of the consensus algorithm type ClientType byte // available client types const ( SoloMachine ClientType = 6 Tendermint ClientType = 7 Localhost ClientType = 9 ) // string representation of the client types const ( ClientTypeSoloMachine string = "solomachine" ClientTypeTendermint string = "tendermint" ClientTypeLocalHost string = "localhost" ) func (ct ClientType) String() string { switch ct { case SoloMachine: return ClientTypeSoloMachine case Tendermint: return ClientTypeTendermint case Localhost: return ClientTypeLocalHost default: return "" } } // MarshalJSON marshal to JSON using string. func (ct ClientType) MarshalJSON() ([]byte, error) { return json.Marshal(ct.String()) } // UnmarshalJSON decodes from JSON. func (ct *ClientType) UnmarshalJSON(data []byte) error { var s string err := json.Unmarshal(data, &s) if err != nil { return err } clientType := ClientTypeFromString(s) if clientType == 0 { return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "invalid client type '%s'", s) } *ct = clientType return nil } // ClientTypeFromString returns a byte that corresponds to the registered client // type. It returns 0 if the type is not found/registered. func ClientTypeFromString(clientType string) ClientType { switch clientType { case ClientTypeSoloMachine: return SoloMachine case ClientTypeTendermint: return Tendermint case ClientTypeLocalHost: return Localhost default: return 0 } }