Skip to content

Commit

Permalink
feat(lightclient): export hard fork keys to genesis (#1470)
Browse files Browse the repository at this point in the history
  • Loading branch information
zale144 authored Nov 14, 2024
1 parent 7bad2d0 commit 6b75606
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 41 deletions.
1 change: 1 addition & 0 deletions proto/dymensionxyz/dymension/lightclient/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ message HeaderSignerEntry {
message GenesisState {
repeated CanonicalClient canonical_clients = 1 [ (gogoproto.nullable) = false ];
repeated HeaderSignerEntry header_signers = 3 [ (gogoproto.nullable) = false ];
repeated string hard_fork_keys = 4;
}

message CanonicalClient {
Expand Down
5 changes: 3 additions & 2 deletions x/lightclient/keeper/canonical_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
ibctm "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"

"github.com/dymensionxyz/dymension/v3/x/lightclient/types"
)

Expand Down Expand Up @@ -62,7 +63,7 @@ func (k Keeper) GetAllCanonicalClients(ctx sdk.Context) (clients []types.Canonic
return
}

func (k Keeper) expectedClient(ctx sdk.Context) ibctm.ClientState {
func (k Keeper) expectedClient() ibctm.ClientState {
return types.DefaultExpectedCanonicalClientParams()
}

Expand All @@ -77,7 +78,7 @@ func (k Keeper) validClient(ctx sdk.Context, clientID string, cs exported.Client
return errChainIDMismatch
}

expClient := k.expectedClient(ctx)
expClient := k.expectedClient()

if err := types.IsCanonicalClientParamsValid(tmClientState, &expClient); err != nil {
return errorsmod.Wrap(err, "params")
Expand Down
6 changes: 6 additions & 0 deletions x/lightclient/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"cosmossdk.io/collections"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/dymensionxyz/dymension/v3/x/lightclient/types"
)

Expand All @@ -18,13 +19,18 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genesisState types.GenesisState) {
panic(err)
}
}
for _, rollappID := range genesisState.HardForkKeys {
k.SetHardForkInProgress(ctx, rollappID)
}
}

func (k Keeper) ExportGenesis(ctx sdk.Context) types.GenesisState {
clients := k.GetAllCanonicalClients(ctx)
hardForkKeys := k.ListHardForkKeys(ctx)

ret := types.GenesisState{
CanonicalClients: clients,
HardForkKeys: hardForkKeys,
}

if err := k.headerSigners.Walk(ctx, nil,
Expand Down
14 changes: 13 additions & 1 deletion x/lightclient/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"reflect"
"testing"

"github.com/stretchr/testify/require"

keepertest "github.com/dymensionxyz/dymension/v3/testutil/keeper"
"github.com/dymensionxyz/dymension/v3/x/lightclient/types"
"github.com/stretchr/testify/require"
)

func TestInitGenesis(t *testing.T) {
Expand All @@ -18,6 +19,7 @@ func TestInitGenesis(t *testing.T) {

keeper.InitGenesis(ctx, types.GenesisState{
CanonicalClients: clients,
HardForkKeys: []string{"rollapp-1", "rollapp-2"},
})

ibc, found := keeper.GetCanonicalClient(ctx, "rollapp-1")
Expand All @@ -26,13 +28,19 @@ func TestInitGenesis(t *testing.T) {
ibc, found = keeper.GetCanonicalClient(ctx, "rollapp-2")
require.True(t, found)
require.Equal(t, "client-2", ibc)
hfks := keeper.ListHardForkKeys(ctx)
require.Len(t, hfks, 2)
require.Equal(t, "rollapp-1", hfks[0])
require.Equal(t, "rollapp-2", hfks[1])
}

func TestExportGenesis(t *testing.T) {
keeper, ctx := keepertest.LightClientKeeper(t)

keeper.SetCanonicalClient(ctx, "rollapp-1", "client-1")
keeper.SetCanonicalClient(ctx, "rollapp-2", "client-2")
keeper.SetHardForkInProgress(ctx, "rollapp-1")
keeper.SetHardForkInProgress(ctx, "rollapp-2")

genesis := keeper.ExportGenesis(ctx)

Expand All @@ -41,6 +49,9 @@ func TestExportGenesis(t *testing.T) {
require.Equal(t, "client-2", genesis.CanonicalClients[1].IbcClientId)
require.Equal(t, "rollapp-1", genesis.CanonicalClients[0].RollappId)
require.Equal(t, "rollapp-2", genesis.CanonicalClients[1].RollappId)
require.Len(t, genesis.HardForkKeys, 2)
require.Equal(t, "rollapp-1", genesis.HardForkKeys[0])
require.Equal(t, "rollapp-2", genesis.HardForkKeys[1])
}

func TestImportExportGenesis(t *testing.T) {
Expand Down Expand Up @@ -69,6 +80,7 @@ func TestImportExportGenesis(t *testing.T) {
Height: 43,
},
},
HardForkKeys: []string{"rollapp-1", "rollapp-2"},
}

k.InitGenesis(ctx, g)
Expand Down
22 changes: 17 additions & 5 deletions x/lightclient/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
errorsmod "cosmossdk.io/errors"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
"github.com/dymensionxyz/gerr-cosmos/gerrc"

"github.com/dymensionxyz/dymension/v3/internal/collcompat"
"github.com/dymensionxyz/dymension/v3/x/lightclient/types"
sequencertypes "github.com/dymensionxyz/dymension/v3/x/sequencer/types"
"github.com/dymensionxyz/gerr-cosmos/gerrc"
)

func (k Keeper) Logger(ctx sdk.Context) log.Logger {
Expand Down Expand Up @@ -142,17 +144,16 @@ func (k Keeper) LightClient(goCtx context.Context, req *types.QueryGetLightClien
return &types.QueryGetLightClientResponse{ClientId: id}, nil
}

func (k Keeper) ExpectedClientState(goCtx context.Context, req *types.QueryExpectedClientStateRequest) (*types.QueryExpectedClientStateResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
c := k.expectedClient(ctx)
func (k Keeper) ExpectedClientState(context.Context, *types.QueryExpectedClientStateRequest) (*types.QueryExpectedClientStateResponse, error) {
c := k.expectedClient()
anyClient, err := ibcclienttypes.PackClientState(&c)
if err != nil {
return nil, errorsmod.Wrap(errors.Join(gerrc.ErrInternal, err), "pack client state")
}
return &types.QueryExpectedClientStateResponse{ClientState: anyClient}, nil
}

func (k Keeper) setHardForkInProgress(ctx sdk.Context, rollappID string) {
func (k Keeper) SetHardForkInProgress(ctx sdk.Context, rollappID string) {
ctx.KVStore(k.storeKey).Set(types.HardForkKey(rollappID), []byte{0x01})
}

Expand All @@ -166,6 +167,17 @@ func (k Keeper) IsHardForkingInProgress(ctx sdk.Context, rollappID string) bool
return ctx.KVStore(k.storeKey).Has(types.HardForkKey(rollappID))
}

func (k Keeper) ListHardForkKeys(ctx sdk.Context) []string {
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.HardForkPrefix)
iter := prefixStore.Iterator(nil, nil)
defer iter.Close() // nolint: errcheck
var ret []string
for ; iter.Valid(); iter.Next() {
ret = append(ret, string(iter.Key()))
}
return ret
}

func (k Keeper) pruneSigners(ctx sdk.Context, client string, h uint64, isAbove bool) error {
var rng *collections.PairRange[string, uint64]
if isAbove {
Expand Down
2 changes: 1 addition & 1 deletion x/lightclient/keeper/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (k Keeper) RollbackCanonicalClient(ctx sdk.Context, rollappId string, newRe
}

// marks that hard fork is in progress
k.setHardForkInProgress(ctx, rollappId)
k.SetHardForkInProgress(ctx, rollappId)

// freeze the client
// it will be released after the hardfork is resolved (on the next state update)
Expand Down
2 changes: 1 addition & 1 deletion x/lightclient/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package types

import fmt "fmt"
import "fmt"

func DefaultGenesisState() GenesisState {
return GenesisState{
Expand Down
105 changes: 81 additions & 24 deletions x/lightclient/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions x/lightclient/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const (
var (
RollappClientKey = []byte{0x01}
canonicalClientKey = []byte{0x04}
hardForkKey = []byte{0x05}
HardForkPrefix = []byte{0x05}
HeaderSignersPrefixKey = collections.NewPrefix("headerSigners/")
ClientHeightToSigner = collections.NewPrefix("clientHeightToSigner/")
)
Expand All @@ -31,7 +31,7 @@ func CanonicalClientKey(clientID string) []byte {
}

func HardForkKey(rollappID string) []byte {
key := hardForkKey
key := HardForkPrefix
key = append(key, []byte(rollappID)...)
return key
}
4 changes: 1 addition & 3 deletions x/rollapp/client/cli/query_latest_height.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cli

import (
"context"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -34,7 +32,7 @@ func CmdShowLatestHeight() *cobra.Command {
Finalized: argFinalized,
}

res, err := queryClient.LatestHeight(context.Background(), req)
res, err := queryClient.LatestHeight(cmd.Context(), req)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion x/rollapp/types/fraud_proposal.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion x/rollapp/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6b75606

Please sign in to comment.