Skip to content

Commit

Permalink
feat: Implement Mithril light client codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
chinhnd-smartosc committed May 7, 2024
1 parent 81ed9d3 commit 70329c8
Show file tree
Hide file tree
Showing 7 changed files with 300 additions and 0 deletions.
28 changes: 28 additions & 0 deletions cosmos/sidechain/x/clients/mithril/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package mithril

import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"

"github.com/cosmos/ibc-go/v8/modules/core/exported"
)

// RegisterInterfaces register the ibc channel submodule interfaces to protobuf
// Any.
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterImplementations(
(*exported.ClientState)(nil),
&ClientState{},
)
registry.RegisterImplementations(
(*exported.ConsensusState)(nil),
&ConsensusState{},
)
registry.RegisterImplementations(
(*exported.Height)(nil),
&Height{},
)
registry.RegisterImplementations(
(*exported.ClientMessage)(nil),
&Misbehaviour{},
)
}
19 changes: 19 additions & 0 deletions cosmos/sidechain/x/clients/mithril/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package mithril

import (
errorsmod "cosmossdk.io/errors"
)

// IBC cardano client sentinel errors
var (
ErrInvalidChainID = errorsmod.Register(ModuleName, 2, "invalid chain-id")
ErrInvalidTrustingPeriod = errorsmod.Register(ModuleName, 3, "invalid trusting period")
ErrInvalidMithrilHeaderHeight = errorsmod.Register(ModuleName, 4, "invalid mithril header height")
ErrInvalidMithrilHeader = errorsmod.Register(ModuleName, 5, "invalid mithril header")
ErrInvalidMaxClockDrift = errorsmod.Register(ModuleName, 6, "invalid max clock drift")
ErrProcessedTimeNotFound = errorsmod.Register(ModuleName, 7, "processed time not found")
ErrProcessedHeightNotFound = errorsmod.Register(ModuleName, 8, "processed height not found")
ErrDelayPeriodNotPassed = errorsmod.Register(ModuleName, 9, "packet-specified delay period has not been reached")
ErrTrustingPeriodExpired = errorsmod.Register(ModuleName, 10, "time since latest trusted state has passed the trusting period")
ErrInvalidCurrentEpoch = errorsmod.Register(ModuleName, 11, "invalid current epoch")
)
126 changes: 126 additions & 0 deletions cosmos/sidechain/x/clients/mithril/height.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package mithril

import (
"fmt"
"math/big"
"strconv"

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
ibcerrors "github.com/cosmos/ibc-go/v8/modules/core/errors"
"github.com/cosmos/ibc-go/v8/modules/core/exported"
)

var _ exported.Height = (*Height)(nil)

// ZeroHeight is a helper function which returns an uninitialized height.
func ZeroHeight() Height {
return Height{}
}

// NewHeight is a constructor for the IBC height type
func NewHeight(mithrilHeight uint64) Height {
return Height{
MithrilHeight: mithrilHeight,
}
}

// GetRevisionNumber always returns 0
func (h Height) GetRevisionNumber() uint64 {
return 0
}

// GetMithrilHeight returns the mithril-height of the height
func (h Height) GetRevisionHeight() uint64 {
return h.MithrilHeight
}

// Compare implements a method to compare two heights. When comparing two heights a, b
// we can call a.Compare(b) which will return
// -1 if a < b
// 0 if a = b
// 1 if a > b
func (h Height) Compare(other exported.Height) int64 {
var a, b big.Int
a.SetUint64(h.MithrilHeight)
b.SetUint64(other.GetRevisionHeight())
return int64(a.Cmp(&b))
}

// LT Helper comparison function returns true if h < other
func (h Height) LT(other exported.Height) bool {
return h.Compare(other) == -1
}

// LTE Helper comparison function returns true if h <= other
func (h Height) LTE(other exported.Height) bool {
cmp := h.Compare(other)
return cmp <= 0
}

// GT Helper comparison function returns true if h > other
func (h Height) GT(other exported.Height) bool {
return h.Compare(other) == 1
}

// GTE Helper comparison function returns true if h >= other
func (h Height) GTE(other exported.Height) bool {
cmp := h.Compare(other)
return cmp >= 0
}

// EQ Helper comparison function returns true if h == other
func (h Height) EQ(other exported.Height) bool {
return h.Compare(other) == 0
}

// String returns a string representation of Height
func (h Height) String() string {
return fmt.Sprintf("%d", h.MithrilHeight)
}

// Decrement will return a new height with the MithrilHeight decremented
// If the MithrilHeight is already at lowest value (1), then false success flag is returend
func (h Height) Decrement() (decremented exported.Height, success bool) {
if h.MithrilHeight == 0 {
return Height{}, false
}
return NewHeight(h.MithrilHeight - 1), true
}

// Increment will return a height with an incremented mithril height
func (h Height) Increment() exported.Height {
return NewHeight(h.MithrilHeight + 1)
}

// IsZero returns true if mithril height is 0
func (h Height) IsZero() bool {
return h.MithrilHeight == 0
}

// MustParseHeight will attempt to parse a string representation of a height and panic if
// parsing fails.
func MustParseHeight(heightStr string) Height {
height, err := ParseHeight(heightStr)
if err != nil {
panic(err)
}

return height
}

// ParseHeight is a utility function that takes a string representation of the height
// and returns a Height struct
func ParseHeight(heightStr string) (Height, error) {
mithrilHeight, err := strconv.ParseUint(heightStr, 10, 64)
if err != nil {
return Height{}, errorsmod.Wrapf(ibcerrors.ErrInvalidHeight, "invalid mithril height. parse err: %s", err)
}
return NewHeight(mithrilHeight), nil
}

// GetSelfHeight is a utility function that returns self height given context
func GetSelfHeight(ctx sdk.Context) Height {
return NewHeight(uint64(ctx.BlockHeight()))
}
5 changes: 5 additions & 0 deletions cosmos/sidechain/x/clients/mithril/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package mithril

const (
ModuleName = "2000-cardano-mithril"
)
84 changes: 84 additions & 0 deletions cosmos/sidechain/x/clients/mithril/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package mithril

import (
"encoding/json"

"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

"cosmossdk.io/core/appmodule"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/types/module"
)

var (
_ module.AppModuleBasic = (*AppModuleBasic)(nil)
_ appmodule.AppModule = (*AppModule)(nil)
)

// AppModuleBasic defines the basic application module used by the mithril light client.
// Only the RegisterInterfaces function needs to be implemented. All other function perform
// a no-op.
type AppModuleBasic struct{}

// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (AppModuleBasic) IsOnePerModuleType() {}

// IsAppModule implements the appmodule.AppModule interface.
func (AppModuleBasic) IsAppModule() {}

// Name returns the mithril module name.
func (AppModuleBasic) Name() string {
return ModuleName
}

// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (AppModule) IsOnePerModuleType() {}

// IsAppModule implements the appmodule.AppModule interface.
func (AppModule) IsAppModule() {}

// RegisterLegacyAminoCodec performs a no-op. The Mithril client does not support amino.
func (AppModuleBasic) RegisterLegacyAminoCodec(*codec.LegacyAmino) {}

// RegisterInterfaces registers module concrete types into protobuf Any. This allows core IBC
// to unmarshal mithril light client types.
func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
RegisterInterfaces(registry)
}

// DefaultGenesis performs a no-op. Genesis is not supported for the mithril light client.
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
return nil
}

// ValidateGenesis performs a no-op. Genesis is not supported for the mithril light client.
func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error {
return nil
}

// RegisterGRPCGatewayRoutes performs a no-op.
func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {}

// GetTxCmd performs a no-op. Please see the 02-client cli commands.
func (AppModuleBasic) GetTxCmd() *cobra.Command {
return nil
}

// GetQueryCmd performs a no-op. Please see the 02-client cli commands.
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
return nil
}

// AppModule is the application module for the Mithril client module
type AppModule struct {
AppModuleBasic
}

// NewAppModule creates a new Mithril client module
func NewAppModule() AppModule {
return AppModule{}
}
19 changes: 19 additions & 0 deletions cosmos/sidechain/x/clients/mithril/proposal_handle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package mithril

import (
errorsmod "cosmossdk.io/errors"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v8/modules/core/exported"
)

func (cs ClientState) CheckSubstituteAndUpdateState(
ctx sdk.Context, cdc codec.BinaryCodec, subjectClientStore,
substituteClientStore storetypes.KVStore, substituteClient exported.ClientState,
) error {
return errorsmod.Wrap(clienttypes.ErrUpdateClientFailed, "CheckSubstituteAndUpdateState: not implemented")
}
19 changes: 19 additions & 0 deletions cosmos/sidechain/x/clients/mithril/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package mithril

import (
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

errorsmod "cosmossdk.io/errors"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v8/modules/core/exported"
)

func (cs ClientState) VerifyUpgradeAndUpdateState(
ctx sdk.Context, cdc codec.BinaryCodec, clientStore storetypes.KVStore,
upgradedClient exported.ClientState, upgradedConsState exported.ConsensusState,
proofUpgradeClient, proofUpgradeConsState []byte,
) error {
return errorsmod.Wrap(clienttypes.ErrInvalidUpgradeClient, "VerifyUpgradeAndUpdateState: not implemented")
}

0 comments on commit 70329c8

Please sign in to comment.