mukan-ibc/modules/light-clients/07-tendermint/header.go
Mukan Erkin Törük 6852832fe8
Some checks failed
CodeQL / Analyze (push) Waiting to run
Docker Build & Push Simapp (main) / docker-build (push) Waiting to run
golangci-lint / lint (push) Waiting to run
Tests / Code Coverage / build (amd64) (push) Waiting to run
Tests / Code Coverage / build (arm64) (push) Waiting to run
Tests / Code Coverage / unit-tests (map[additional-args:-tags="test_e2e" name:e2e path:./e2e]) (push) Waiting to run
Tests / Code Coverage / unit-tests (map[name:08-wasm path:./modules/light-clients/08-wasm]) (push) Waiting to run
Tests / Code Coverage / unit-tests (map[name:ibc-go path:.]) (push) Waiting to run
Deploy to GitHub Pages / Deploy to GitHub Pages (push) Has been cancelled
Buf-Push / push (push) Has been cancelled
initial: sovereign Mukan Network fork
2026-05-11 03:18:28 +03:00

83 lines
2.8 KiB
Go

package tendermint
import (
"bytes"
"time"
errorsmod "cosmossdk.io/errors"
cmttypes "github.com/cometbft/cometbft/types"
clienttypes "github.com/cosmos/ibc-go/v10/modules/core/02-client/types"
commitmenttypes "github.com/cosmos/ibc-go/v10/modules/core/23-commitment/types"
"github.com/cosmos/ibc-go/v10/modules/core/exported"
)
var _ exported.ClientMessage = (*Header)(nil)
// ConsensusState returns the updated consensus state associated with the header
func (h Header) ConsensusState() *ConsensusState {
return &ConsensusState{
Timestamp: h.GetTime(),
Root: commitmenttypes.NewMerkleRoot(h.Header.GetAppHash()),
NextValidatorsHash: h.Header.NextValidatorsHash,
}
}
// ClientType defines that the Header is a Tendermint consensus algorithm
func (Header) ClientType() string {
return exported.Tendermint
}
// GetHeight returns the current height. It returns 0 if the tendermint
// header is nil.
// NOTE: the header.Header is checked to be non nil in ValidateBasic.
func (h Header) GetHeight() exported.Height {
revision := clienttypes.ParseChainID(h.Header.ChainID)
return clienttypes.NewHeight(revision, uint64(h.Header.Height))
}
// GetTime returns the current block timestamp. It returns a zero time if
// the tendermint header is nil.
// NOTE: the header.Header is checked to be non nil in ValidateBasic.
func (h Header) GetTime() time.Time {
return h.Header.Time
}
// ValidateBasic calls the SignedHeader ValidateBasic function and checks
// that validatorsets are not nil.
// NOTE: TrustedHeight and TrustedValidators may be empty when creating client
// with MsgCreateClient
func (h Header) ValidateBasic() error {
if h.SignedHeader == nil {
return errorsmod.Wrap(clienttypes.ErrInvalidHeader, "tendermint signed header cannot be nil")
}
if h.Header == nil {
return errorsmod.Wrap(clienttypes.ErrInvalidHeader, "tendermint header cannot be nil")
}
tmSignedHeader, err := cmttypes.SignedHeaderFromProto(h.SignedHeader)
if err != nil {
return errorsmod.Wrap(err, "header is not a tendermint header")
}
if err := tmSignedHeader.ValidateBasic(h.Header.GetChainID()); err != nil {
return errorsmod.Wrap(err, "header failed basic validation")
}
// TrustedHeight is less than Header for updates and misbehaviour
if h.TrustedHeight.GTE(h.GetHeight()) {
return errorsmod.Wrapf(ErrInvalidHeaderHeight, "TrustedHeight %d must be less than header height %d",
h.TrustedHeight, h.GetHeight())
}
if h.ValidatorSet == nil {
return errorsmod.Wrap(clienttypes.ErrInvalidHeader, "validator set is nil")
}
tmValset, err := cmttypes.ValidatorSetFromProto(h.ValidatorSet)
if err != nil {
return errorsmod.Wrap(err, "validator set is not tendermint validator set")
}
if !bytes.Equal(h.Header.ValidatorsHash, tmValset.Hash()) {
return errorsmod.Wrap(clienttypes.ErrInvalidHeader, "validator set does not match hash")
}
return nil
}