Skip to content

Commit

Permalink
feat(fork): restricts to post genesis transfer heights only (#1600)
Browse files Browse the repository at this point in the history
  • Loading branch information
danwt authored Dec 3, 2024
1 parent 29e5c75 commit 3e0c553
Show file tree
Hide file tree
Showing 35 changed files with 269 additions and 303 deletions.
6 changes: 3 additions & 3 deletions app/ante/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/ante"
ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
ibcante "github.com/cosmos/ibc-go/v7/modules/core/ante"
"github.com/dymensionxyz/dymension/v3/x/common/types"
ethante "github.com/evmos/ethermint/app/ante"
txfeesante "github.com/osmosis-labs/osmosis/v15/x/txfees/ante"

vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"

"github.com/dymensionxyz/dymension/v3/x/delayedack"
lightclientante "github.com/dymensionxyz/dymension/v3/x/lightclient/ante"
)

Expand Down Expand Up @@ -79,7 +79,7 @@ func newLegacyCosmosAnteHandlerEip712(options HandlerOptions) sdk.AnteHandler {
NewLegacyEip712SigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
ante.NewIncrementSequenceDecorator(options.AccountKeeper),
lightclientante.NewIBCMessagesDecorator(*options.LightClientKeeper, options.IBCKeeper.ClientKeeper, options.IBCKeeper.ChannelKeeper, options.RollappKeeper),
delayedack.NewIBCProofHeightDecorator(),
types.NewIBCProofHeightDecorator(),
ibcante.NewRedundantRelayDecorator(options.IBCKeeper),
ethante.NewGasWantedDecorator(options.EvmKeeper, options.FeeMarketKeeper),
)
Expand Down Expand Up @@ -119,7 +119,7 @@ func newCosmosAnteHandler(options HandlerOptions) sdk.AnteHandler {
ante.NewSigGasConsumeDecorator(options.AccountKeeper, ethante.DefaultSigVerificationGasConsumer),
ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
ante.NewIncrementSequenceDecorator(options.AccountKeeper),
delayedack.NewIBCProofHeightDecorator(),
types.NewIBCProofHeightDecorator(),
lightclientante.NewIBCMessagesDecorator(*options.LightClientKeeper, options.IBCKeeper.ClientKeeper, options.IBCKeeper.ChannelKeeper, options.RollappKeeper),
ibcante.NewRedundantRelayDecorator(options.IBCKeeper),
ethante.NewGasWantedDecorator(options.EvmKeeper, options.FeeMarketKeeper),
Expand Down
6 changes: 5 additions & 1 deletion app/upgrades/v4/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,14 @@ func ConvertOldRollappToNew(oldRollapp rollapptypes.Rollapp) rollapptypes.Rollap
genesisInfo = crynuxGenesisInfo
}

genesisState := oldRollapp.GenesisState
// min(nim=813701, mande=1332619) on Dec 2nd : a sufficient and safe number
genesisState.TransferProofHeight = 813701

return rollapptypes.Rollapp{
RollappId: oldRollapp.RollappId,
Owner: oldRollapp.Owner,
GenesisState: oldRollapp.GenesisState,
GenesisState: genesisState,
ChannelId: oldRollapp.ChannelId,
Metadata: &rollapptypes.RollappMetadata{ // Can be updated in runtime
Website: "",
Expand Down
6 changes: 3 additions & 3 deletions ibctesting/genesis_bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (s *transferGenesisSuite) TestHappyPath_NoGenesisAccounts() {

rollapp = s.hubApp().RollappKeeper.MustGetRollapp(s.hubCtx(), rollappChainID())
// assert the transfers are enabled
s.Require().True(rollapp.GenesisState.TransfersEnabled)
s.Require().True(rollapp.GenesisState.IsTransferEnabled())

// assert denom registered
expectedIBCdenom := types.ParseDenomTrace(types.GetPrefixedDenom(s.path.EndpointB.ChannelConfig.PortID, s.path.EndpointB.ChannelID, rollapp.GenesisInfo.NativeDenom.Base)).IBCDenom()
Expand Down Expand Up @@ -160,7 +160,7 @@ func (s *transferGenesisSuite) TestIRO() {

// assert the transfers are enabled
rollapp = s.hubApp().RollappKeeper.MustGetRollapp(s.hubCtx(), rollappChainID())
s.Require().True(rollapp.GenesisState.TransfersEnabled)
s.Require().True(rollapp.GenesisState.IsTransferEnabled())

// the iro plan should be settled
plan, found := s.hubApp().IROKeeper.GetPlanByRollapp(s.hubCtx(), rollappChainID())
Expand Down Expand Up @@ -346,7 +346,7 @@ func (s *transferGenesisSuite) TestBridgeDisabledEnabled() {

// assert the transfers are enabled
rollapp = s.hubApp().RollappKeeper.MustGetRollapp(s.hubCtx(), rollappChainID())
s.Require().True(rollapp.GenesisState.TransfersEnabled)
s.Require().True(rollapp.GenesisState.IsTransferEnabled())

// assert the transfer now goes through
res, err = s.rollappChain().SendMsgs(msg)
Expand Down
4 changes: 2 additions & 2 deletions ibctesting/transfers_enabled_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (s *transfersEnabledSuite) SetupTest() {
// manually set the rollapp to have transfers disabled by default
// (rollapp is setup correctly, meaning transfer channel is canonical)
ra := s.hubApp().RollappKeeper.MustGetRollapp(s.hubCtx(), rollappChainID())
ra.GenesisState.TransfersEnabled = false
ra.GenesisState.TransferProofHeight = 0
ra.ChannelId = s.path.EndpointA.ChannelID
s.hubApp().RollappKeeper.SetRollapp(s.hubCtx(), ra)
s.hubApp().LightClientKeeper.SetCanonicalClient(s.hubCtx(), rollappChainID(), s.path.EndpointA.ClientID)
Expand Down Expand Up @@ -88,7 +88,7 @@ func (s *transfersEnabledSuite) TestHubToRollappDisabled() {
s.Require().True(errorsmod.IsOf(err, gerrc.ErrFailedPrecondition))
ra := s.hubApp().RollappKeeper.MustGetRollapp(s.hubCtx(), rollappChainID())
ra.ChannelId = s.path.EndpointA.ChannelID
ra.GenesisState.TransfersEnabled = true
ra.GenesisState.TransferProofHeight = 1 // enable
s.hubApp().RollappKeeper.SetRollapp(s.hubCtx(), ra)
} else {
s.Require().NoError(err)
Expand Down
5 changes: 4 additions & 1 deletion ibctesting/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ func (s *utilSuite) createRollapp(transfersEnabled bool, channelID *string) {
a := s.hubApp()
ra := a.RollappKeeper.MustGetRollapp(s.hubCtx(), rollappChainID())
ra.ChannelId = *channelID
ra.GenesisState.TransfersEnabled = transfersEnabled
ra.GenesisState.TransferProofHeight = 0
if transfersEnabled {
ra.GenesisState.TransferProofHeight = 1
}
a.RollappKeeper.SetRollapp(s.hubCtx(), ra)
}
}
Expand Down
17 changes: 8 additions & 9 deletions proto/dymensionxyz/dymension/rollapp/rollapp.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ import "dymensionxyz/dymension/rollapp/genesis_info.proto";
// RollappGenesisState is a partial repr of the state the hub can expect the
// rollapp to be in upon genesis
message RollappGenesisState {
reserved 1;
// If true, then full usage of the canonical ibc transfer channel is enabled.
// Note: in v3.1.0 and prior this field marked the completion of the 'genesis
// event' Keeping and renaming the field enables a seamless upgrade
// https://www.notion.so/dymension/ADR-x-Genesis-Bridge-Phase-2-89769aa551b5440b9ed403a101775ce1?pvs=4#89698384d815435b87393dbe45bc5a74
// to the new genesis transfer protocol
// Note: if this field is false, ibc transfers may still be allowed in one or
// either direction.
bool transfers_enabled = 2;
reserved 1,2;


// 0 means unpopulated
// If populated, it's the proof height that the hub received the genesis transfer
// packet from the rollapp. If populated, the bridge is considered open. It's not allowed
// to fork to a height prior to this height.
uint64 transfer_proof_height = 3;
}

// Rollapp defines a rollapp object. First, the RollApp is created and then
Expand Down
5 changes: 0 additions & 5 deletions x/bridgingfee/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/osmosis-labs/osmosis/v15/osmoutils"
txfeeskeeper "github.com/osmosis-labs/osmosis/v15/x/txfees/keeper"

commontypes "github.com/dymensionxyz/dymension/v3/x/common/types"
delayedackkeeper "github.com/dymensionxyz/dymension/v3/x/delayedack/keeper"
rollappkeeper "github.com/dymensionxyz/dymension/v3/x/rollapp/keeper"
)
Expand Down Expand Up @@ -73,10 +72,6 @@ func (w IBCModule) logger(
func (w IBCModule) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress) exported.Acknowledgement {
l := w.logger(ctx, packet, "OnRecvPacket")

if commontypes.SkipRollappMiddleware(ctx) {
return w.IBCModule.OnRecvPacket(ctx, packet, relayer)
}

transfer, err := w.rollappKeeper.GetValidTransfer(ctx, packet.GetData(), packet.GetDestPort(), packet.GetDestChannel())
if err != nil {
l.Error("Get valid transfer.", "err", err)
Expand Down
95 changes: 95 additions & 0 deletions x/common/types/rollapp_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"fmt"
"strconv"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
ibctypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
"github.com/dymensionxyz/gerr-cosmos/gerrc"
)

func (r RollappPacket) LogString() string {
Expand Down Expand Up @@ -120,3 +123,95 @@ func (r RollappPacket) RestoreOriginalTransferTarget() RollappPacket {
}
return r
}

const (
// proofHeightCtxKey is a context key to pass the proof height from the msg to the IBC middleware
proofHeightCtxKey = "ibc_proof_height"
)

func CtxWithPacketProofHeight(ctx sdk.Context, packetId PacketUID, height ibctypes.Height) sdk.Context {
key := fmt.Sprintf("%s_%s", proofHeightCtxKey, packetId.String())
return ctx.WithValue(key, height)
}

func PacketProofHeightFromCtx(ctx sdk.Context, packetId PacketUID) (ibctypes.Height, bool) {
key := fmt.Sprintf("%s_%s", proofHeightCtxKey, packetId.String())
u, ok := ctx.Value(key).(ibctypes.Height)
return u, ok
}

type IBCProofHeightDecorator struct{}

func NewIBCProofHeightDecorator() IBCProofHeightDecorator {
return IBCProofHeightDecorator{}
}

func (rrd IBCProofHeightDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
for _, m := range tx.GetMsgs() {
var (
height ibctypes.Height
packetId PacketUID
)
switch msg := m.(type) {
case *channeltypes.MsgRecvPacket:
height = msg.ProofHeight
packetId = NewPacketUID(
RollappPacket_ON_RECV,
msg.Packet.DestinationPort,
msg.Packet.DestinationChannel,
msg.Packet.Sequence,
)

case *channeltypes.MsgAcknowledgement:
height = msg.ProofHeight
packetId = NewPacketUID(
RollappPacket_ON_ACK,
msg.Packet.SourcePort,
msg.Packet.SourceChannel,
msg.Packet.Sequence,
)

case *channeltypes.MsgTimeout:
height = msg.ProofHeight
packetId = NewPacketUID(
RollappPacket_ON_TIMEOUT,
msg.Packet.SourcePort,
msg.Packet.SourceChannel,
msg.Packet.Sequence,
)
default:
continue
}

ctx = CtxWithPacketProofHeight(ctx, packetId, height)
}
return next(ctx, tx, simulate)
}

func PacketHubPortChan(packetType RollappPacket_Type, packet channeltypes.Packet) (string, string) {
var port string
var channel string

switch packetType {
case RollappPacket_ON_RECV:
port, channel = packet.GetDestPort(), packet.GetDestChannel()
case RollappPacket_ON_TIMEOUT, RollappPacket_ON_ACK:
port, channel = packet.GetSourcePort(), packet.GetSourceChannel()
}
return port, channel
}

func UnpackPacketProofHeight(
ctx sdk.Context,
packet channeltypes.Packet,
packetType RollappPacket_Type,
) (uint64, error) {
port, channel := PacketHubPortChan(packetType, packet)

packetID := NewPacketUID(packetType, port, channel, packet.Sequence)
height, ok := PacketProofHeightFromCtx(ctx, packetID)
if !ok {
return 0, errorsmod.Wrapf(gerrc.ErrInternal, "get proof height from context: packetID: %s", packetID)
}
return height.RevisionHeight, nil
}
17 changes: 0 additions & 17 deletions x/common/types/skip_middleware.go

This file was deleted.

5 changes: 0 additions & 5 deletions x/delayedack/ibc_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ func (w IBCMiddleware) OnRecvPacket(
) exported.Acknowledgement {
l := w.logger(ctx, packet, "OnRecvPacket")

if commontypes.SkipRollappMiddleware(ctx) {
l.Debug("Skipping because of skip delay ctx.")
return w.IBCModule.OnRecvPacket(ctx, packet, relayer)
}

transfer, err := w.GetValidTransferWithFinalizationInfo(ctx, packet, commontypes.RollappPacket_ON_RECV)
if err != nil {
l.Error("Get valid rollapp and transfer.", "err", err)
Expand Down
22 changes: 6 additions & 16 deletions x/delayedack/keeper/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,21 @@ func (k Keeper) GetValidTransferWithFinalizationInfo(
packet channeltypes.Packet,
packetType commontypes.RollappPacket_Type,
) (data types.TransferDataWithFinalization, err error) {
var port string
var channel string
port, channel := commontypes.PacketHubPortChan(packetType, packet)

switch packetType {
case commontypes.RollappPacket_ON_RECV:
port, channel = packet.GetDestPort(), packet.GetDestChannel()
case commontypes.RollappPacket_ON_TIMEOUT, commontypes.RollappPacket_ON_ACK:
port, channel = packet.GetSourcePort(), packet.GetSourceChannel()
height, err := commontypes.UnpackPacketProofHeight(ctx, packet, packetType)
if err != nil {
err = errorsmod.Wrap(err, "unpack packet proof height")
return
}
data.ProofHeight = height

data.TransferData, err = k.rollappKeeper.GetValidTransfer(ctx, packet.GetData(), port, channel)
if err != nil {
err = errorsmod.Wrap(err, "get valid transfer data")
return
}

packetID := commontypes.NewPacketUID(packetType, port, channel, packet.Sequence)
height, ok := types.PacketProofHeightFromCtx(ctx, packetID)
if !ok {
// TODO: should probably be a panic
err = errorsmod.Wrapf(gerrc.ErrNotFound, "get proof height from context: packetID: %s", packetID)
return
}
data.ProofHeight = height.RevisionHeight

if !data.IsRollapp() {
return
}
Expand Down
59 changes: 0 additions & 59 deletions x/delayedack/proof_height_ante.go

This file was deleted.

Loading

0 comments on commit 3e0c553

Please sign in to comment.