Skip to content

Commit

Permalink
add more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
fish-sammy committed Oct 25, 2023
1 parent 9af2430 commit a89f362
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions x/nexus/keeper/general_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (k Keeper) GenerateMessageID(ctx sdk.Context) (string, []byte, uint64) {
}

// SetNewWasmMessage sets the given general message from a wasm contract.
// Deprecated
// Deprecated: use SetNewMessage_ instead
func (k Keeper) SetNewWasmMessage(ctx sdk.Context, msg exported.GeneralMessage) error {
if msg.Asset != nil {
return fmt.Errorf("asset transfer is not supported")
Expand Down Expand Up @@ -86,7 +86,7 @@ func (k Keeper) SetNewWasmMessage(ctx sdk.Context, msg exported.GeneralMessage)
}

// SetNewMessage sets the given general message. If the messages is approved, adds the message ID to approved messages store
// Deprecated
// Deprecated: use SetNewMessage_ instead
func (k Keeper) SetNewMessage(ctx sdk.Context, m exported.GeneralMessage) error {
sourceChain, ok := k.GetChain(ctx, m.GetSourceChain())
if !ok {
Expand Down Expand Up @@ -145,7 +145,7 @@ func (k Keeper) SetNewMessage(ctx sdk.Context, m exported.GeneralMessage) error
*/

// SetMessageProcessing sets the general message as processing
// Deprecated
// Deprecated: use SetMessageProcessing_ instead
func (k Keeper) SetMessageProcessing(ctx sdk.Context, id string) error {
m, found := k.GetMessage(ctx, id)
if !found {
Expand Down Expand Up @@ -297,7 +297,7 @@ func (k Keeper) SetMessageProcessing_(ctx sdk.Context, id string) error {
return fmt.Errorf("general message %s not found", id)
}

if !msg.Is(exported.Approved) && !msg.Is(exported.Failed) {
if !(msg.Is(exported.Approved) || msg.Is(exported.Failed)) {
return fmt.Errorf("general message has to be approved or failed")
}

Expand All @@ -317,25 +317,31 @@ func (k Keeper) SetMessageProcessing_(ctx sdk.Context, id string) error {
}

func (k Keeper) validateMessage(ctx sdk.Context, msg exported.GeneralMessage) error {
// only validate sender and asset if it's not from wasm.
// the nexus module doesn't know how to validate wasm chains and addresses.
if !msg.Sender.Chain.IsFrom(wasm.ModuleName) {
if err := k.validateAddressAndAsset(ctx, msg.Sender, msg.Asset); err != nil {
return err
}
}

// only validate recipient and asset if it's not to wasm.
// the nexus module doesn't know how to validate wasm chains and addresses.
if !msg.Recipient.Chain.IsFrom(wasm.ModuleName) {
if err := k.validateAddressAndAsset(ctx, msg.Recipient, msg.Asset); err != nil {
return err
}
}

// asset is not supported for wasm messages
if (msg.Sender.Chain.IsFrom(wasm.ModuleName) || msg.Recipient.Chain.IsFrom(wasm.ModuleName)) && msg.Asset != nil {
return fmt.Errorf("asset transfer is not supported for wasm messages")
}

return nil
}

// validateAddressAndAsset validates 1) chain existence, 2) chain activation, 3) address, 4) asset
func (k Keeper) validateAddressAndAsset(ctx sdk.Context, address exported.CrossChainAddress, asset *sdk.Coin) error {
if _, ok := k.GetChain(ctx, address.Chain.Name); !ok {
return fmt.Errorf("chain %s is not registered", address.Chain.Name)
Expand Down

0 comments on commit a89f362

Please sign in to comment.