Skip to content

Commit

Permalink
feat(axelarnet/nexus)!: pick up the new methods for setting general m…
Browse files Browse the repository at this point in the history
…essages in ibc message_handler and nexus msg_dispatcher
  • Loading branch information
fish-sammy committed Oct 26, 2023
1 parent b206392 commit e96a2e8
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 60 deletions.
10 changes: 4 additions & 6 deletions x/axelarnet/message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,11 @@ func handleMessage(ctx sdk.Context, n types.Nexus, b types.BankKeeper, sourceAdd

destChain := funcs.MustOk(n.GetChain(ctx, nexus.ChainName(msg.DestinationChain)))
recipient := nexus.CrossChainAddress{Chain: destChain, Address: msg.DestinationAddress}
m := nexus.NewGeneralMessage(
m := nexus.NewGeneralMessage_(
id,
sourceAddress,
recipient,
crypto.Keccak256Hash(msg.Payload).Bytes(),
nexus.Approved,
txID,
nonce,
nil,
Expand All @@ -267,7 +266,7 @@ func handleMessage(ctx sdk.Context, n types.Nexus, b types.BankKeeper, sourceAdd
Payload: msg.Payload,
})

return n.SetNewMessage(ctx, m)
return n.SetNewMessage_(ctx, m)
}

func handleMessageWithToken(ctx sdk.Context, n types.Nexus, b types.BankKeeper, sourceAddress nexus.CrossChainAddress, msg Message, token keeper.Coin) error {
Expand All @@ -284,12 +283,11 @@ func handleMessageWithToken(ctx sdk.Context, n types.Nexus, b types.BankKeeper,

destChain := funcs.MustOk(n.GetChain(ctx, nexus.ChainName(msg.DestinationChain)))
recipient := nexus.CrossChainAddress{Chain: destChain, Address: msg.DestinationAddress}
m := nexus.NewGeneralMessage(
m := nexus.NewGeneralMessage_(
id,
sourceAddress,
recipient,
crypto.Keccak256Hash(msg.Payload).Bytes(),
nexus.Approved,
txID,
nonce,
&token.Coin,
Expand All @@ -306,7 +304,7 @@ func handleMessageWithToken(ctx sdk.Context, n types.Nexus, b types.BankKeeper,
Asset: token.Coin,
})

return n.SetNewMessage(ctx, m)
return n.SetNewMessage_(ctx, m)
}

func handleTokenSent(ctx sdk.Context, n types.Nexus, b types.BankKeeper, sourceAddress nexus.CrossChainAddress, msg Message, token keeper.Coin) error {
Expand Down
6 changes: 3 additions & 3 deletions x/axelarnet/message_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestHandleMessage(t *testing.T) {
}))
channelK.SendPacketFunc = func(sdk.Context, *captypes.Capability, ibcexported.PacketI) error { return nil }
n = &mock.NexusMock{
SetNewMessageFunc: func(ctx sdk.Context, msg nexus.GeneralMessage) error {
SetNewMessage_Func: func(ctx sdk.Context, msg nexus.GeneralMessage) error {
genMsg = msg
return nil
},
Expand Down Expand Up @@ -473,7 +473,7 @@ func TestHandleMessageWithToken(t *testing.T) {

channelK.SendPacketFunc = func(sdk.Context, *captypes.Capability, ibcexported.PacketI) error { return nil }
n = &mock.NexusMock{
SetNewMessageFunc: func(ctx sdk.Context, msg nexus.GeneralMessage) error {
SetNewMessage_Func: func(ctx sdk.Context, msg nexus.GeneralMessage) error {
genMsg = msg
return nil
},
Expand Down Expand Up @@ -689,7 +689,7 @@ func TestHandleSendToken(t *testing.T) {

channelK.SendPacketFunc = func(sdk.Context, *captypes.Capability, ibcexported.PacketI) error { return nil }
n = &mock.NexusMock{
SetNewMessageFunc: func(sdk.Context, nexus.GeneralMessage) error { return nil },
SetNewMessage_Func: func(sdk.Context, nexus.GeneralMessage) error { return nil },
GetChainFunc: func(ctx sdk.Context, chain nexus.ChainName) (nexus.Chain, bool) {
switch chain {
case srcChain.Name:
Expand Down
29 changes: 10 additions & 19 deletions x/nexus/keeper/msg_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,18 @@ func (m Messenger) routeMsg(ctx sdk.Context, msg exported.WasmMessage) error {
sender := exported.CrossChainAddress{Chain: sourceChain, Address: msg.SourceAddress}
recipient := exported.CrossChainAddress{Chain: destinationChain, Address: msg.DestinationAddress}

// set status to approved if the message is sent to a cosmos chain and set
// to processing otherwise, because messages sent to cosmos chains require
// translation with the original payload.
// https://github.com/axelarnetwork/axelar-core/blob/ea48d5b974dfd94ea235311eddabe23bfa430cd9/x/axelarnet/keeper/msg_server.go#L520
status := exported.Approved
if !destinationChain.IsFrom(axelarnet.ModuleName) {
status = exported.Processing
nexusMsg := exported.NewGeneralMessage_(id, sender, recipient, msg.PayloadHash, msg.SourceTxID, msg.SourceTxIndex, nil)
if err := m.Nexus.SetNewMessage_(ctx, nexusMsg); err != nil {
return err
}

if err := m.Nexus.SetNewWasmMessage(ctx, exported.NewGeneralMessage(
id,
sender,
recipient,
msg.PayloadHash,
status,
msg.SourceTxID,
msg.SourceTxIndex,
nil,
)); err != nil {
return err
if destinationChain.IsFrom(axelarnet.ModuleName) {
return nil
}

return nil
// // set status to approved if the message is sent to a cosmos chain and set
// // to processing otherwise, because messages sent to cosmos chains require
// // translation with the original payload.
// // https://github.com/axelarnetwork/axelar-core/blob/ea48d5b974dfd94ea235311eddabe23bfa430cd9/x/axelarnet/keeper/msg_server.go#L520
return m.Nexus.SetMessageProcessing_(ctx, nexusMsg.ID)
}
26 changes: 16 additions & 10 deletions x/nexus/keeper/msg_dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestMessenger_DispatchMsg(t *testing.T) {
nexus.GenerateMessageIDFunc = func(_ sdk.Context) (string, []byte, uint64) {
return "1", []byte("1"), 1
}
nexus.SetNewWasmMessageFunc = func(_ sdk.Context, _ exported.GeneralMessage) error {
nexus.SetNewMessage_Func = func(_ sdk.Context, _ exported.GeneralMessage) error {
return fmt.Errorf("set msg error")
}
}).
Expand Down Expand Up @@ -161,9 +161,10 @@ func TestMessenger_DispatchMsg(t *testing.T) {
nexus.GenerateMessageIDFunc = func(_ sdk.Context) (string, []byte, uint64) {
return "1", []byte("1"), 1
}
nexus.SetNewWasmMessageFunc = func(_ sdk.Context, msg exported.GeneralMessage) error {
nexus.SetNewMessage_Func = func(_ sdk.Context, msg exported.GeneralMessage) error {
return nil
}
nexus.SetMessageProcessing_Func = func(ctx sdk.Context, id string) error { return nil }
}).
Branch(
When("the destination chain is a cosmos chain", func() {
Expand All @@ -175,10 +176,12 @@ func TestMessenger_DispatchMsg(t *testing.T) {
_, _, err := messenger.DispatchMsg(ctx, contractAddr, "", msg)
assert.NoError(t, err)

assert.Len(t, nexus.SetNewWasmMessageCalls(), 1)
assert.Equal(t, nexus.SetNewWasmMessageCalls()[0].Msg.Recipient.Chain, axelarnet.Axelarnet)
assert.Equal(t, nexus.SetNewWasmMessageCalls()[0].Msg.Status, exported.Approved)
assert.Nil(t, nexus.SetNewWasmMessageCalls()[0].Msg.Asset)
assert.Len(t, nexus.SetNewMessage_Calls(), 1)
assert.Equal(t, nexus.SetNewMessage_Calls()[0].Msg.Recipient.Chain, axelarnet.Axelarnet)
assert.Equal(t, nexus.SetNewMessage_Calls()[0].Msg.Status, exported.Approved)
assert.Nil(t, nexus.SetNewMessage_Calls()[0].Msg.Asset)

assert.Empty(t, nexus.SetMessageProcessing_Calls())
}),

When("the destination chain is a non-cosmos chain", func() {
Expand All @@ -190,10 +193,13 @@ func TestMessenger_DispatchMsg(t *testing.T) {
_, _, err := messenger.DispatchMsg(ctx, contractAddr, "", msg)
assert.NoError(t, err)

assert.Len(t, nexus.SetNewWasmMessageCalls(), 1)
assert.Equal(t, nexus.SetNewWasmMessageCalls()[0].Msg.Recipient.Chain, evm.Ethereum)
assert.Equal(t, nexus.SetNewWasmMessageCalls()[0].Msg.Status, exported.Processing)
assert.Nil(t, nexus.SetNewWasmMessageCalls()[0].Msg.Asset)
assert.Len(t, nexus.SetNewMessage_Calls(), 1)
assert.Equal(t, nexus.SetNewMessage_Calls()[0].Msg.Recipient.Chain, evm.Ethereum)
assert.Equal(t, nexus.SetNewMessage_Calls()[0].Msg.Status, exported.Approved)
assert.Nil(t, nexus.SetNewMessage_Calls()[0].Msg.Asset)

assert.Len(t, nexus.SetMessageProcessing_Calls(), 1)
assert.Equal(t, nexus.SetNewMessage_Calls()[0].Msg.ID, nexus.SetMessageProcessing_Calls()[0].ID)
}),
).
Run(t)
Expand Down
3 changes: 2 additions & 1 deletion x/nexus/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ type Nexus interface {
SetRateLimit(ctx sdk.Context, chainName exported.ChainName, limit sdk.Coin, window time.Duration) error
RateLimitTransfer(ctx sdk.Context, chain exported.ChainName, asset sdk.Coin, direction exported.TransferDirection) error
GenerateMessageID(ctx sdk.Context) (string, []byte, uint64)
SetNewWasmMessage(ctx sdk.Context, msg exported.GeneralMessage) error
SetNewMessage_(ctx sdk.Context, msg exported.GeneralMessage) error
SetMessageProcessing_(ctx sdk.Context, id string) error
}

// Snapshotter provides functionality to the snapshot module
Expand Down
92 changes: 71 additions & 21 deletions x/nexus/types/mock/expected_keepers.go

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

0 comments on commit e96a2e8

Please sign in to comment.