From e96a2e866c4534611d1076758510152cdc6b27a7 Mon Sep 17 00:00:00 2001 From: Sammy Liu Date: Thu, 26 Oct 2023 16:16:09 -0400 Subject: [PATCH] feat(axelarnet/nexus)!: pick up the new methods for setting general messages in ibc message_handler and nexus msg_dispatcher --- x/axelarnet/message_handler.go | 10 ++- x/axelarnet/message_handler_test.go | 6 +- x/nexus/keeper/msg_dispatcher.go | 29 +++----- x/nexus/keeper/msg_dispatcher_test.go | 26 +++++--- x/nexus/types/expected_keepers.go | 3 +- x/nexus/types/mock/expected_keepers.go | 92 ++++++++++++++++++++------ 6 files changed, 106 insertions(+), 60 deletions(-) diff --git a/x/axelarnet/message_handler.go b/x/axelarnet/message_handler.go index 296874f14..f917baf4b 100644 --- a/x/axelarnet/message_handler.go +++ b/x/axelarnet/message_handler.go @@ -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, @@ -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 { @@ -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, @@ -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 { diff --git a/x/axelarnet/message_handler_test.go b/x/axelarnet/message_handler_test.go index 1232fef7a..ab4dfc52c 100644 --- a/x/axelarnet/message_handler_test.go +++ b/x/axelarnet/message_handler_test.go @@ -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 }, @@ -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 }, @@ -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: diff --git a/x/nexus/keeper/msg_dispatcher.go b/x/nexus/keeper/msg_dispatcher.go index 4dd6b74df..179c5afba 100644 --- a/x/nexus/keeper/msg_dispatcher.go +++ b/x/nexus/keeper/msg_dispatcher.go @@ -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) } diff --git a/x/nexus/keeper/msg_dispatcher_test.go b/x/nexus/keeper/msg_dispatcher_test.go index a586319e2..83c9401a6 100644 --- a/x/nexus/keeper/msg_dispatcher_test.go +++ b/x/nexus/keeper/msg_dispatcher_test.go @@ -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") } }). @@ -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() { @@ -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() { @@ -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) diff --git a/x/nexus/types/expected_keepers.go b/x/nexus/types/expected_keepers.go index 30e8b10b9..f2f403bd4 100644 --- a/x/nexus/types/expected_keepers.go +++ b/x/nexus/types/expected_keepers.go @@ -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 diff --git a/x/nexus/types/mock/expected_keepers.go b/x/nexus/types/mock/expected_keepers.go index 902881a41..4694e35f7 100644 --- a/x/nexus/types/mock/expected_keepers.go +++ b/x/nexus/types/mock/expected_keepers.go @@ -82,8 +82,11 @@ var _ nexustypes.Nexus = &NexusMock{} // RemoveChainMaintainerFunc: func(ctx cosmossdktypes.Context, chain github_com_axelarnetwork_axelar_core_x_nexus_exported.Chain, validator cosmossdktypes.ValAddress) error { // panic("mock out the RemoveChainMaintainer method") // }, -// SetNewWasmMessageFunc: func(ctx cosmossdktypes.Context, msg github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage) error { -// panic("mock out the SetNewWasmMessage method") +// SetMessageProcessing_Func: func(ctx cosmossdktypes.Context, id string) error { +// panic("mock out the SetMessageProcessing_ method") +// }, +// SetNewMessage_Func: func(ctx cosmossdktypes.Context, msg github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage) error { +// panic("mock out the SetNewMessage_ method") // }, // SetParamsFunc: func(ctx cosmossdktypes.Context, p nexustypes.Params) { // panic("mock out the SetParams method") @@ -155,8 +158,11 @@ type NexusMock struct { // RemoveChainMaintainerFunc mocks the RemoveChainMaintainer method. RemoveChainMaintainerFunc func(ctx cosmossdktypes.Context, chain github_com_axelarnetwork_axelar_core_x_nexus_exported.Chain, validator cosmossdktypes.ValAddress) error - // SetNewWasmMessageFunc mocks the SetNewWasmMessage method. - SetNewWasmMessageFunc func(ctx cosmossdktypes.Context, msg github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage) error + // SetMessageProcessing_Func mocks the SetMessageProcessing_ method. + SetMessageProcessing_Func func(ctx cosmossdktypes.Context, id string) error + + // SetNewMessage_Func mocks the SetNewMessage_ method. + SetNewMessage_Func func(ctx cosmossdktypes.Context, msg github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage) error // SetParamsFunc mocks the SetParams method. SetParamsFunc func(ctx cosmossdktypes.Context, p nexustypes.Params) @@ -305,8 +311,15 @@ type NexusMock struct { // Validator is the validator argument value. Validator cosmossdktypes.ValAddress } - // SetNewWasmMessage holds details about calls to the SetNewWasmMessage method. - SetNewWasmMessage []struct { + // SetMessageProcessing_ holds details about calls to the SetMessageProcessing_ method. + SetMessageProcessing_ []struct { + // Ctx is the ctx argument value. + Ctx cosmossdktypes.Context + // ID is the id argument value. + ID string + } + // SetNewMessage_ holds details about calls to the SetNewMessage_ method. + SetNewMessage_ []struct { // Ctx is the ctx argument value. Ctx cosmossdktypes.Context // Msg is the msg argument value. @@ -350,7 +363,8 @@ type NexusMock struct { lockRateLimitTransfer sync.RWMutex lockRegisterFee sync.RWMutex lockRemoveChainMaintainer sync.RWMutex - lockSetNewWasmMessage sync.RWMutex + lockSetMessageProcessing_ sync.RWMutex + lockSetNewMessage_ sync.RWMutex lockSetParams sync.RWMutex lockSetRateLimit sync.RWMutex } @@ -1051,10 +1065,46 @@ func (mock *NexusMock) RemoveChainMaintainerCalls() []struct { return calls } -// SetNewWasmMessage calls SetNewWasmMessageFunc. -func (mock *NexusMock) SetNewWasmMessage(ctx cosmossdktypes.Context, msg github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage) error { - if mock.SetNewWasmMessageFunc == nil { - panic("NexusMock.SetNewWasmMessageFunc: method is nil but Nexus.SetNewWasmMessage was just called") +// SetMessageProcessing_ calls SetMessageProcessing_Func. +func (mock *NexusMock) SetMessageProcessing_(ctx cosmossdktypes.Context, id string) error { + if mock.SetMessageProcessing_Func == nil { + panic("NexusMock.SetMessageProcessing_Func: method is nil but Nexus.SetMessageProcessing_ was just called") + } + callInfo := struct { + Ctx cosmossdktypes.Context + ID string + }{ + Ctx: ctx, + ID: id, + } + mock.lockSetMessageProcessing_.Lock() + mock.calls.SetMessageProcessing_ = append(mock.calls.SetMessageProcessing_, callInfo) + mock.lockSetMessageProcessing_.Unlock() + return mock.SetMessageProcessing_Func(ctx, id) +} + +// SetMessageProcessing_Calls gets all the calls that were made to SetMessageProcessing_. +// Check the length with: +// +// len(mockedNexus.SetMessageProcessing_Calls()) +func (mock *NexusMock) SetMessageProcessing_Calls() []struct { + Ctx cosmossdktypes.Context + ID string +} { + var calls []struct { + Ctx cosmossdktypes.Context + ID string + } + mock.lockSetMessageProcessing_.RLock() + calls = mock.calls.SetMessageProcessing_ + mock.lockSetMessageProcessing_.RUnlock() + return calls +} + +// SetNewMessage_ calls SetNewMessage_Func. +func (mock *NexusMock) SetNewMessage_(ctx cosmossdktypes.Context, msg github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage) error { + if mock.SetNewMessage_Func == nil { + panic("NexusMock.SetNewMessage_Func: method is nil but Nexus.SetNewMessage_ was just called") } callInfo := struct { Ctx cosmossdktypes.Context @@ -1063,17 +1113,17 @@ func (mock *NexusMock) SetNewWasmMessage(ctx cosmossdktypes.Context, msg github_ Ctx: ctx, Msg: msg, } - mock.lockSetNewWasmMessage.Lock() - mock.calls.SetNewWasmMessage = append(mock.calls.SetNewWasmMessage, callInfo) - mock.lockSetNewWasmMessage.Unlock() - return mock.SetNewWasmMessageFunc(ctx, msg) + mock.lockSetNewMessage_.Lock() + mock.calls.SetNewMessage_ = append(mock.calls.SetNewMessage_, callInfo) + mock.lockSetNewMessage_.Unlock() + return mock.SetNewMessage_Func(ctx, msg) } -// SetNewWasmMessageCalls gets all the calls that were made to SetNewWasmMessage. +// SetNewMessage_Calls gets all the calls that were made to SetNewMessage_. // Check the length with: // -// len(mockedNexus.SetNewWasmMessageCalls()) -func (mock *NexusMock) SetNewWasmMessageCalls() []struct { +// len(mockedNexus.SetNewMessage_Calls()) +func (mock *NexusMock) SetNewMessage_Calls() []struct { Ctx cosmossdktypes.Context Msg github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage } { @@ -1081,9 +1131,9 @@ func (mock *NexusMock) SetNewWasmMessageCalls() []struct { Ctx cosmossdktypes.Context Msg github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage } - mock.lockSetNewWasmMessage.RLock() - calls = mock.calls.SetNewWasmMessage - mock.lockSetNewWasmMessage.RUnlock() + mock.lockSetNewMessage_.RLock() + calls = mock.calls.SetNewMessage_ + mock.lockSetNewMessage_.RUnlock() return calls }