Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Danwt/research 441 sketch genesis simulation tx #1549

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions x/rollapp/genesisbridge/genesis_bridge_data.pb.go

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

66 changes: 0 additions & 66 deletions x/rollapp/genesisbridge/genesis_transfer.go

This file was deleted.

136 changes: 25 additions & 111 deletions x/rollapp/genesisbridge/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,27 @@ package genesisbridge

import (
"encoding/json"
"errors"
"fmt"
"slices"

errorsmod "cosmossdk.io/errors"
"github.com/cometbft/cometbft/libs/log"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types"
"github.com/cosmos/ibc-go/v7/modules/core/exported"

"github.com/dymensionxyz/gerr-cosmos/gerrc"
"github.com/dymensionxyz/dymension/v3/x/rollapp/types"
"github.com/dymensionxyz/sdk-utils/utils/uevent"
"github.com/dymensionxyz/sdk-utils/utils/uibc"
)

"github.com/dymensionxyz/dymension/v3/x/rollapp/types"
const (
// HubRecipient is the address of `x/rollapp` module's account on the rollapp chain.
HubRecipient = "dym1mk7pw34ypusacm29m92zshgxee3yreums8avur"
)

// IBCModule GenesisBridge is responsible for handling the genesis bridge protocol.
// (ADR: https://www.notion.so/dymension/ADR-x-Genesis-Bridge-109a4a51f86a80ba8b50db454bee04a7?pvs=4)
//
// It validated the genesis info registered on the hub, is the same as the rollapp's genesis info.
// It validated the genesis info registered on the rollapp, is the same as the rollapp's genesis info.
// It registers the denom metadata for the native denom.
// It handles the genesis transfer.
//
Expand Down Expand Up @@ -69,9 +67,9 @@ func (w IBCModule) logger(
// OnRecvPacket will handle the genesis bridge packet in case needed.
// no-op for non-rollapp chains and rollapps with transfers enabled.
//
// The genesis bridge packet is a special packet that is sent from the rollapp to the hub on channel creation.
// The hub will receive this packet and:
// - validated the genesis info registered on the hub, is the same as the rollapp's genesis info.
// The genesis bridge packet is a special packet that is sent from the rollapp to the rollapp on channel creation.
// The rollapp will receive this packet and:
// - validated the genesis info registered on the rollapp, is the same as the rollapp's genesis info.
// - registers the denom metadata for the native denom.
// - handles the genesis transfer.
// On success, it will mark the IBC channel for this rollapp as enabled. This marks the end of the genesis phase.
Expand Down Expand Up @@ -107,42 +105,38 @@ func (w IBCModule) OnRecvPacket(
return uevent.NewErrorAcknowledgement(ctx, errorsmod.Wrap(err, "unmarshal genesis bridge data"))
}

// stateless validation of the genesis bridge data
if err := genesisBridgeData.ValidateBasic(); err != nil {
l.Error("Validate basic genesis bridge data.", "err", err)
return uevent.NewErrorAcknowledgement(ctx, errorsmod.Wrap(err, "validate basic genesis bridge data"))
v := validator{
rollapp: genesisBridgeData,
hub: ra.GenesisInfo,
channelID: ra.ChannelId,
rollappID: ra.RollappId,
}

// validate genesis info against the expected data set on the rollapp
err = w.ValidateGenesisBridge(ra, genesisBridgeData.GenesisInfo)
actionItems, err := v.validateAndGetActionItems()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We call this in two places

  1. In a query that dymint will call
  2. here

if err != nil {
l.Error("Validate genesis info.", "err", err)
return uevent.NewErrorAcknowledgement(ctx, errorsmod.Wrap(err, "validate genesis info"))
return uevent.NewErrorAcknowledgement(ctx, errorsmod.Wrap(err, "validate and get actionable data"))
}
Comment on lines -117 to 118
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does the bulk of validation


// register the denom metadata. the supplied denom is validated on validateBasic
raBaseDenom, err := w.registerDenomMetadata(ctx, ra.RollappId, ra.ChannelId, genesisBridgeData.NativeDenom)
if err != nil {
l.Error("Register denom metadata.", "err", err)
return uevent.NewErrorAcknowledgement(ctx, errorsmod.Wrap(err, "transfer genesis: register denom metadata"))
w.transferKeeper.SetDenomTrace(ctx, actionItems.trace)
if err := w.denomKeeper.CreateDenomMetadata(ctx, actionItems.bankMeta); err != nil {
return uevent.NewErrorAcknowledgement(ctx, errorsmod.Wrap(err, "create denom metadata"))
}

// validate and handle the genesis transfer
err = w.handleGenesisTransfer(ctx, *ra, packet, genesisBridgeData.GenesisTransfer)
if err != nil {
l.Error("Handle genesis transfer.", "err", err)
return uevent.NewErrorAcknowledgement(ctx, errorsmod.Wrap(err, "handle genesis transfer"))
for _, data := range actionItems.fungiDatas {
if err := w.transferKeeper.OnRecvPacket(ctx, packet, data); err != nil {
return uevent.NewErrorAcknowledgement(ctx, errorsmod.Wrap(err, "handle genesis transfer"))
}
}

err = w.EnableTransfers(ctx, ra, raBaseDenom)
err = w.EnableTransfers(ctx, ra, actionItems.bankMeta.Base)
if err != nil {
l.Error("Enable transfers.", "err", err)
return uevent.NewErrorAcknowledgement(ctx, errorsmod.Wrap(err, "transfer genesis: enable transfers"))
}

ctx.EventManager().EmitEvent(sdk.NewEvent(types.EventTypeTransfersEnabled,
sdk.NewAttribute(types.AttributeKeyRollappId, ra.RollappId),
sdk.NewAttribute(types.AttributeRollappIBCdenom, raBaseDenom),
sdk.NewAttribute(types.AttributeRollappIBCdenom, actionItems.bankMeta.Base),
))

// return success ack
Expand All @@ -151,86 +145,6 @@ func (w IBCModule) OnRecvPacket(
return successAck
}

func (w IBCModule) ValidateGenesisBridge(ra *types.Rollapp, data GenesisBridgeInfo) error {
raInfo := ra.GenesisInfo

if data.GenesisChecksum != raInfo.GenesisChecksum {
return fmt.Errorf("genesis checksum mismatch: expected: %v, got: %v", raInfo.GenesisChecksum, data.GenesisChecksum)
}

if data.Bech32Prefix != raInfo.Bech32Prefix {
return fmt.Errorf("bech32 prefix mismatch: expected: %v, got: %v", raInfo.Bech32Prefix, data.Bech32Prefix)
}

if data.NativeDenom != raInfo.NativeDenom {
return fmt.Errorf("native denom mismatch: expected: %v, got: %v", raInfo.NativeDenom, data.NativeDenom)
}

if !data.InitialSupply.Equal(raInfo.InitialSupply) {
return fmt.Errorf("initial supply mismatch: expected: %v, got: %v", raInfo.InitialSupply, data.InitialSupply)
}

err := compareGenesisAccounts(raInfo.GenesisAccounts, data.GenesisAccounts)
if err != nil {
return errorsmod.Wrap(err, "genesis accounts mismatch")
}

return nil
}

func compareGenesisAccounts(raCommitted *types.GenesisAccounts, gbData []types.GenesisAccount) error {
if raCommitted == nil {
if len(gbData) == 0 {
return nil
}
return fmt.Errorf("genesis accounts length mismatch: expected 0, got %d", len(gbData))
}

if len(raCommitted.Accounts) != len(gbData) {
return fmt.Errorf("genesis accounts length mismatch: expected %d, got %d", len(raCommitted.Accounts), len(gbData))
}

for _, acc := range raCommitted.Accounts {
found := slices.ContainsFunc(gbData, func(dataAcc types.GenesisAccount) bool {
return dataAcc.Address == acc.Address && dataAcc.Amount.Equal(acc.Amount)
})

if !found {
return fmt.Errorf("genesis account mismatch: account %s with amount %v not found in data", acc.Address, acc.Amount)
}
}

return nil
}

func (w IBCModule) registerDenomMetadata(ctx sdk.Context, rollappID, channelID string, m banktypes.Metadata) (string, error) {
// Set the trace for the ibc denom
trace := uibc.GetForeignDenomTrace(channelID, m.Base)
w.transferKeeper.SetDenomTrace(ctx, trace)

// Change the base to the ibc denom, and add an alias to the original
m.Base = trace.IBCDenom()
m.Description = fmt.Sprintf("auto-generated ibc denom for rollapp: base: %s: rollapp: %s", m.GetBase(), rollappID)
for i, u := range m.DenomUnits {
if u.Exponent == 0 {
m.DenomUnits[i].Aliases = append(m.DenomUnits[i].Aliases, u.Denom)
m.DenomUnits[i].Denom = m.GetBase()
}
}

if err := m.Validate(); err != nil {
return "", errorsmod.Wrap(errors.Join(gerrc.ErrInvalidArgument, err), "metadata validate")
}

// We go by the denom keeper instead of calling bank directly, so denom creation hooks are called
err := w.denomKeeper.CreateDenomMetadata(ctx, m)
if err != nil {
return "", errorsmod.Wrap(err, "create denom metadata")
}

return m.Base, nil
}

// EnableTransfers marks the end of the genesis bridge phase.
// It sets the transfers enabled flag on the rollapp.
// It also calls the after transfers enabled hook.
Expand Down
Loading
Loading