-
Notifications
You must be signed in to change notification settings - Fork 353
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
// | ||
|
@@ -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. | ||
|
@@ -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() | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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. | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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