From d79382db93c22d6ca300a08f7705ed5eb891b7af Mon Sep 17 00:00:00 2001 From: Sammy Liu Date: Wed, 1 Nov 2023 13:20:40 -0400 Subject: [PATCH] refactor(nexus): general message deprecated methods removal --- x/axelarnet/types/expected_keepers.go | 2 - x/axelarnet/types/mock/expected_keepers.go | 100 ------- x/evm/abci.go | 8 +- x/evm/abci_test.go | 4 +- x/evm/types/expected_keepers.go | 2 +- x/evm/types/mock/expected_keepers.go | 42 +-- x/nexus/keeper/general_message.go | 131 --------- x/nexus/keeper/general_message_test.go | 304 ++++----------------- x/nexus/keeper/genesis.go | 2 +- x/nexus/keeper/genesis_test.go | 2 +- x/nexus/keeper/grpc_query_test.go | 2 +- 11 files changed, 81 insertions(+), 518 deletions(-) diff --git a/x/axelarnet/types/expected_keepers.go b/x/axelarnet/types/expected_keepers.go index 3c4e6730f..3d256d93e 100644 --- a/x/axelarnet/types/expected_keepers.go +++ b/x/axelarnet/types/expected_keepers.go @@ -56,9 +56,7 @@ type Nexus interface { IsChainActivated(ctx sdk.Context, chain nexus.Chain) bool RateLimitTransfer(ctx sdk.Context, chain nexus.ChainName, asset sdk.Coin, direction nexus.TransferDirection) error GetMessage(ctx sdk.Context, id string) (m nexus.GeneralMessage, found bool) - SetNewMessage(ctx sdk.Context, m nexus.GeneralMessage) error SetNewMessage_(ctx sdk.Context, m nexus.GeneralMessage) error - SetMessageProcessing(ctx sdk.Context, id string) error SetMessageProcessing_(ctx sdk.Context, id string) error SetMessageExecuted(ctx sdk.Context, id string) error SetMessageFailed(ctx sdk.Context, id string) error diff --git a/x/axelarnet/types/mock/expected_keepers.go b/x/axelarnet/types/mock/expected_keepers.go index 3b1fb1999..344078edd 100644 --- a/x/axelarnet/types/mock/expected_keepers.go +++ b/x/axelarnet/types/mock/expected_keepers.go @@ -620,15 +620,9 @@ var _ axelarnettypes.Nexus = &NexusMock{} // SetMessageFailedFunc: func(ctx cosmossdktypes.Context, id string) error { // panic("mock out the SetMessageFailed method") // }, -// SetMessageProcessingFunc: func(ctx cosmossdktypes.Context, id string) error { -// panic("mock out the SetMessageProcessing method") -// }, // SetMessageProcessing_Func: func(ctx cosmossdktypes.Context, id string) error { // panic("mock out the SetMessageProcessing_ method") // }, -// SetNewMessageFunc: func(ctx cosmossdktypes.Context, m github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage) error { -// panic("mock out the SetNewMessage method") -// }, // SetNewMessage_Func: func(ctx cosmossdktypes.Context, m github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage) error { // panic("mock out the SetNewMessage_ method") // }, @@ -702,15 +696,9 @@ type NexusMock struct { // SetMessageFailedFunc mocks the SetMessageFailed method. SetMessageFailedFunc func(ctx cosmossdktypes.Context, id string) error - // SetMessageProcessingFunc mocks the SetMessageProcessing method. - SetMessageProcessingFunc func(ctx cosmossdktypes.Context, id string) error - // SetMessageProcessing_Func mocks the SetMessageProcessing_ method. SetMessageProcessing_Func func(ctx cosmossdktypes.Context, id string) error - // SetNewMessageFunc mocks the SetNewMessage method. - SetNewMessageFunc func(ctx cosmossdktypes.Context, m github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage) error - // SetNewMessage_Func mocks the SetNewMessage_ method. SetNewMessage_Func func(ctx cosmossdktypes.Context, m github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage) error @@ -875,13 +863,6 @@ type NexusMock struct { // ID is the id argument value. ID string } - // 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 - } // SetMessageProcessing_ holds details about calls to the SetMessageProcessing_ method. SetMessageProcessing_ []struct { // Ctx is the ctx argument value. @@ -889,13 +870,6 @@ type NexusMock struct { // 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 - // M is the m argument value. - M github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage - } // SetNewMessage_ holds details about calls to the SetNewMessage_ method. SetNewMessage_ []struct { // Ctx is the ctx argument value. @@ -937,9 +911,7 @@ type NexusMock struct { lockSetChain sync.RWMutex lockSetMessageExecuted sync.RWMutex lockSetMessageFailed sync.RWMutex - lockSetMessageProcessing sync.RWMutex lockSetMessageProcessing_ sync.RWMutex - lockSetNewMessage sync.RWMutex lockSetNewMessage_ sync.RWMutex lockSubTransferFee sync.RWMutex lockValidateAddress sync.RWMutex @@ -1669,42 +1641,6 @@ func (mock *NexusMock) SetMessageFailedCalls() []struct { return calls } -// SetMessageProcessing calls SetMessageProcessingFunc. -func (mock *NexusMock) SetMessageProcessing(ctx cosmossdktypes.Context, id string) error { - if mock.SetMessageProcessingFunc == nil { - panic("NexusMock.SetMessageProcessingFunc: 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.SetMessageProcessingFunc(ctx, id) -} - -// SetMessageProcessingCalls gets all the calls that were made to SetMessageProcessing. -// Check the length with: -// -// len(mockedNexus.SetMessageProcessingCalls()) -func (mock *NexusMock) SetMessageProcessingCalls() []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 -} - // SetMessageProcessing_ calls SetMessageProcessing_Func. func (mock *NexusMock) SetMessageProcessing_(ctx cosmossdktypes.Context, id string) error { if mock.SetMessageProcessing_Func == nil { @@ -1741,42 +1677,6 @@ func (mock *NexusMock) SetMessageProcessing_Calls() []struct { return calls } -// SetNewMessage calls SetNewMessageFunc. -func (mock *NexusMock) SetNewMessage(ctx cosmossdktypes.Context, m github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage) error { - if mock.SetNewMessageFunc == nil { - panic("NexusMock.SetNewMessageFunc: method is nil but Nexus.SetNewMessage was just called") - } - callInfo := struct { - Ctx cosmossdktypes.Context - M github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage - }{ - Ctx: ctx, - M: m, - } - mock.lockSetNewMessage.Lock() - mock.calls.SetNewMessage = append(mock.calls.SetNewMessage, callInfo) - mock.lockSetNewMessage.Unlock() - return mock.SetNewMessageFunc(ctx, m) -} - -// SetNewMessageCalls gets all the calls that were made to SetNewMessage. -// Check the length with: -// -// len(mockedNexus.SetNewMessageCalls()) -func (mock *NexusMock) SetNewMessageCalls() []struct { - Ctx cosmossdktypes.Context - M github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage -} { - var calls []struct { - Ctx cosmossdktypes.Context - M github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage - } - mock.lockSetNewMessage.RLock() - calls = mock.calls.SetNewMessage - mock.lockSetNewMessage.RUnlock() - return calls -} - // SetNewMessage_ calls SetNewMessage_Func. func (mock *NexusMock) SetNewMessage_(ctx cosmossdktypes.Context, m github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage) error { if mock.SetNewMessage_Func == nil { diff --git a/x/evm/abci.go b/x/evm/abci.go index 6552333d4..c0bab258d 100644 --- a/x/evm/abci.go +++ b/x/evm/abci.go @@ -214,12 +214,11 @@ func setMessageToNexus(ctx sdk.Context, n types.Nexus, event types.Event, asset Address: e.ContractCall.ContractAddress, } - message = nexus.NewGeneralMessage( + message = nexus.NewGeneralMessage_( string(event.GetID()), sender, recipient, e.ContractCall.PayloadHash.Bytes(), - nexus.Approved, event.TxID.Bytes(), event.Index, nil, @@ -240,12 +239,11 @@ func setMessageToNexus(ctx sdk.Context, n types.Nexus, event types.Event, asset Address: e.ContractCallWithToken.ContractAddress, } - message = nexus.NewGeneralMessage( + message = nexus.NewGeneralMessage_( string(event.GetID()), sender, recipient, e.ContractCallWithToken.PayloadHash.Bytes(), - nexus.Approved, event.TxID.Bytes(), event.Index, asset, @@ -258,7 +256,7 @@ func setMessageToNexus(ctx sdk.Context, n types.Nexus, event types.Event, asset return fmt.Errorf("%s is not a supported recipient", axelarnet.Axelarnet.Name) } - return n.SetNewMessage(ctx, message) + return n.SetNewMessage_(ctx, message) } func handleConfirmDeposit(ctx sdk.Context, event types.Event, bk types.BaseKeeper, n types.Nexus) error { diff --git a/x/evm/abci_test.go b/x/evm/abci_test.go index 7891318a9..8f4b8a31e 100644 --- a/x/evm/abci_test.go +++ b/x/evm/abci_test.go @@ -484,7 +484,7 @@ func TestHandleContractCall(t *testing.T) { setGeneralMessageSucceed := func(isSuccessful bool) func() { return func() { - n.SetNewMessageFunc = func(sdk.Context, nexus.GeneralMessage) error { + n.SetNewMessage_Func = func(sdk.Context, nexus.GeneralMessage) error { if !isSuccessful { return fmt.Errorf("set general message error") } @@ -553,7 +553,7 @@ func TestHandleContractCall(t *testing.T) { Then("should succeed", func(t *testing.T) { err := handleContractCall(ctx, event, bk, n, multisigKeeper) assert.NoError(t, err) - assert.Len(t, n.SetNewMessageCalls(), 1) + assert.Len(t, n.SetNewMessage_Calls(), 1) }). Run(t) } diff --git a/x/evm/types/expected_keepers.go b/x/evm/types/expected_keepers.go index c089fdd14..d5b42caab 100644 --- a/x/evm/types/expected_keepers.go +++ b/x/evm/types/expected_keepers.go @@ -114,7 +114,7 @@ type Nexus interface { GetChainMaintainerState(ctx sdk.Context, chain nexus.Chain, address sdk.ValAddress) (nexus.MaintainerState, bool) SetChainMaintainerState(ctx sdk.Context, maintainerState nexus.MaintainerState) error RateLimitTransfer(ctx sdk.Context, chain nexus.ChainName, asset sdk.Coin, direction nexus.TransferDirection) error - SetNewMessage(ctx sdk.Context, m nexus.GeneralMessage) error + SetNewMessage_(ctx sdk.Context, m nexus.GeneralMessage) error GetProcessingMessages(ctx sdk.Context, chain nexus.ChainName, limit int64) []nexus.GeneralMessage SetMessageFailed(ctx sdk.Context, id string) error SetMessageExecuted(ctx sdk.Context, id string) error diff --git a/x/evm/types/mock/expected_keepers.go b/x/evm/types/mock/expected_keepers.go index 03c7ace4f..68b5c94ce 100644 --- a/x/evm/types/mock/expected_keepers.go +++ b/x/evm/types/mock/expected_keepers.go @@ -167,8 +167,8 @@ var _ types.Nexus = &NexusMock{} // SetMessageFailedFunc: func(ctx github_com_cosmos_cosmos_sdk_types.Context, id string) error { // panic("mock out the SetMessageFailed method") // }, -// SetNewMessageFunc: func(ctx github_com_cosmos_cosmos_sdk_types.Context, m github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage) error { -// panic("mock out the SetNewMessage method") +// SetNewMessage_Func: func(ctx github_com_cosmos_cosmos_sdk_types.Context, m github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage) error { +// panic("mock out the SetNewMessage_ method") // }, // } // @@ -243,8 +243,8 @@ type NexusMock struct { // SetMessageFailedFunc mocks the SetMessageFailed method. SetMessageFailedFunc func(ctx github_com_cosmos_cosmos_sdk_types.Context, id string) error - // SetNewMessageFunc mocks the SetNewMessage method. - SetNewMessageFunc func(ctx github_com_cosmos_cosmos_sdk_types.Context, m github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage) error + // SetNewMessage_Func mocks the SetNewMessage_ method. + SetNewMessage_Func func(ctx github_com_cosmos_cosmos_sdk_types.Context, m github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage) error // calls tracks calls to the methods. calls struct { @@ -432,8 +432,8 @@ type NexusMock struct { // ID is the id argument value. ID string } - // SetNewMessage holds details about calls to the SetNewMessage method. - SetNewMessage []struct { + // SetNewMessage_ holds details about calls to the SetNewMessage_ method. + SetNewMessage_ []struct { // Ctx is the ctx argument value. Ctx github_com_cosmos_cosmos_sdk_types.Context // M is the m argument value. @@ -462,7 +462,7 @@ type NexusMock struct { lockSetChainMaintainerState sync.RWMutex lockSetMessageExecuted sync.RWMutex lockSetMessageFailed sync.RWMutex - lockSetNewMessage sync.RWMutex + lockSetNewMessage_ sync.RWMutex } // AddTransferFee calls AddTransferFeeFunc. @@ -1317,10 +1317,10 @@ func (mock *NexusMock) SetMessageFailedCalls() []struct { return calls } -// SetNewMessage calls SetNewMessageFunc. -func (mock *NexusMock) SetNewMessage(ctx github_com_cosmos_cosmos_sdk_types.Context, m github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage) error { - if mock.SetNewMessageFunc == nil { - panic("NexusMock.SetNewMessageFunc: method is nil but Nexus.SetNewMessage was just called") +// SetNewMessage_ calls SetNewMessage_Func. +func (mock *NexusMock) SetNewMessage_(ctx github_com_cosmos_cosmos_sdk_types.Context, m 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 github_com_cosmos_cosmos_sdk_types.Context @@ -1329,17 +1329,17 @@ func (mock *NexusMock) SetNewMessage(ctx github_com_cosmos_cosmos_sdk_types.Cont Ctx: ctx, M: m, } - mock.lockSetNewMessage.Lock() - mock.calls.SetNewMessage = append(mock.calls.SetNewMessage, callInfo) - mock.lockSetNewMessage.Unlock() - return mock.SetNewMessageFunc(ctx, m) + mock.lockSetNewMessage_.Lock() + mock.calls.SetNewMessage_ = append(mock.calls.SetNewMessage_, callInfo) + mock.lockSetNewMessage_.Unlock() + return mock.SetNewMessage_Func(ctx, m) } -// SetNewMessageCalls gets all the calls that were made to SetNewMessage. +// SetNewMessage_Calls gets all the calls that were made to SetNewMessage_. // Check the length with: // -// len(mockedNexus.SetNewMessageCalls()) -func (mock *NexusMock) SetNewMessageCalls() []struct { +// len(mockedNexus.SetNewMessage_Calls()) +func (mock *NexusMock) SetNewMessage_Calls() []struct { Ctx github_com_cosmos_cosmos_sdk_types.Context M github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage } { @@ -1347,9 +1347,9 @@ func (mock *NexusMock) SetNewMessageCalls() []struct { Ctx github_com_cosmos_cosmos_sdk_types.Context M github_com_axelarnetwork_axelar_core_x_nexus_exported.GeneralMessage } - mock.lockSetNewMessage.RLock() - calls = mock.calls.SetNewMessage - mock.lockSetNewMessage.RUnlock() + mock.lockSetNewMessage_.RLock() + calls = mock.calls.SetNewMessage_ + mock.lockSetNewMessage_.RUnlock() return calls } diff --git a/x/nexus/keeper/general_message.go b/x/nexus/keeper/general_message.go index 3a7c58e6d..ee3854dd5 100644 --- a/x/nexus/keeper/general_message.go +++ b/x/nexus/keeper/general_message.go @@ -8,7 +8,6 @@ import ( "github.com/CosmWasm/wasmd/x/wasm" "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/types/query" "github.com/axelarnetwork/axelar-core/utils" @@ -37,136 +36,6 @@ func (k Keeper) GenerateMessageID(ctx sdk.Context) (string, []byte, uint64) { return fmt.Sprintf("0x%s-%d", hex.EncodeToString(hash[:]), nonce), hash[:], nonce } -// SetNewWasmMessage sets the given general message from a wasm contract. -// 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") - } - - if _, ok := k.GetChain(ctx, msg.GetDestinationChain()); !ok { - return fmt.Errorf("destination chain %s is not a registered chain", msg.GetDestinationChain()) - } - - if !k.IsChainActivated(ctx, msg.Recipient.Chain) { - return fmt.Errorf("destination chain %s is not activated", msg.GetDestinationChain()) - } - - if err := k.ValidateAddress(ctx, msg.Recipient); err != nil { - return sdkerrors.Wrap(err, "invalid recipient address") - } - - if _, ok := k.GetMessage(ctx, msg.ID); ok { - return fmt.Errorf("general message %s already exists", msg.ID) - } - - if err := k.setMessage(ctx, msg); err != nil { - return err - } - - switch msg.Status { - case exported.Approved: - funcs.MustNoErr(ctx.EventManager().EmitTypedEvent(&types.MessageReceived{ - ID: msg.ID, - PayloadHash: msg.PayloadHash, - Sender: msg.Sender, - Recipient: msg.Recipient, - })) - case exported.Processing: - if err := k.setProcessingMessageID(ctx, msg); err != nil { - return err - } - - funcs.MustNoErr(ctx.EventManager().EmitTypedEvent(&types.MessageProcessing{ID: msg.ID})) - default: - return fmt.Errorf("invalid message status %s", msg.Status) - } - - return nil -} - -// SetNewMessage sets the given general message. If the messages is approved, adds the message ID to approved messages store -// Deprecated: use SetNewMessage_ instead -func (k Keeper) SetNewMessage(ctx sdk.Context, m exported.GeneralMessage) error { - sourceChain, ok := k.GetChain(ctx, m.GetSourceChain()) - if !ok { - return fmt.Errorf("source chain %s is not a registered chain", m.GetSourceChain()) - } - - if err := k.ValidateAddress(ctx, m.Sender); err != nil { - return err - } - - destChain, ok := k.GetChain(ctx, m.GetDestinationChain()) - if !ok { - return fmt.Errorf("destination chain %s is not a registered chain", m.GetDestinationChain()) - } - - if err := k.ValidateAddress(ctx, m.Recipient); err != nil { - return err - } - - if m.Asset != nil { - if err := k.validateAsset(ctx, sourceChain, m.Asset.Denom); err != nil { - return err - } - - if err := k.validateAsset(ctx, destChain, m.Asset.Denom); err != nil { - return err - } - } - - if _, found := k.GetMessage(ctx, m.ID); found { - return fmt.Errorf("general message %s already exists", m.ID) - } - - if m.Is(exported.Processing) { - if err := k.setProcessingMessageID(ctx, m); err != nil { - return err - } - } - - funcs.MustNoErr(ctx.EventManager().EmitTypedEvent(&types.MessageReceived{ - ID: m.ID, - PayloadHash: m.PayloadHash, - Sender: m.Sender, - Recipient: m.Recipient, - })) - - return k.setMessage(ctx, m) -} - -/* - * Below are the valid message status transitions: - * Approved -> Processing - * Processing -> Executed - * Processing -> Failed - * Failed -> Processing - */ - -// SetMessageProcessing sets the general message as processing -// Deprecated: use SetMessageProcessing_ instead -func (k Keeper) SetMessageProcessing(ctx sdk.Context, id string) error { - m, found := k.GetMessage(ctx, id) - if !found { - return fmt.Errorf("general message %s not found", id) - } - - if !(m.Is(exported.Approved) || m.Is(exported.Failed)) { - return fmt.Errorf("general message is not approved or failed") - } - - m.Status = exported.Processing - - if err := k.setMessage(ctx, m); err != nil { - return err - } - - funcs.MustNoErr(ctx.EventManager().EmitTypedEvent(&types.MessageProcessing{ID: m.ID})) - - return k.setProcessingMessageID(ctx, m) -} - // SetMessageExecuted sets the general message as executed func (k Keeper) SetMessageExecuted(ctx sdk.Context, id string) error { m, found := k.GetMessage(ctx, id) diff --git a/x/nexus/keeper/general_message_test.go b/x/nexus/keeper/general_message_test.go index ec7ceab79..3df283f29 100644 --- a/x/nexus/keeper/general_message_test.go +++ b/x/nexus/keeper/general_message_test.go @@ -5,7 +5,6 @@ import ( "encoding/hex" "fmt" "testing" - "time" "github.com/CosmWasm/wasmd/x/wasm" sdk "github.com/cosmos/cosmos-sdk/types" @@ -14,7 +13,6 @@ import ( "github.com/axelarnetwork/axelar-core/app" "github.com/axelarnetwork/axelar-core/testutils/rand" - "github.com/axelarnetwork/axelar-core/utils" axelarnet "github.com/axelarnetwork/axelar-core/x/axelarnet/exported" evm "github.com/axelarnetwork/axelar-core/x/evm/exported" evmtypes "github.com/axelarnetwork/axelar-core/x/evm/types" @@ -22,7 +20,6 @@ import ( "github.com/axelarnetwork/axelar-core/x/nexus/exported" nexustestutils "github.com/axelarnetwork/axelar-core/x/nexus/exported/testutils" nexus "github.com/axelarnetwork/axelar-core/x/nexus/keeper" - "github.com/axelarnetwork/utils/funcs" . "github.com/axelarnetwork/utils/test" ) @@ -302,223 +299,6 @@ func TestSetMessageProcessing_(t *testing.T) { Run(t) } -func randWasmMsg(status exported.GeneralMessage_Status) exported.GeneralMessage { - return exported.GeneralMessage{ - ID: rand.NormalizedStr(10), - Sender: exported.CrossChainAddress{ - Chain: nexustestutils.RandomChain(), - Address: rand.NormalizedStr(42), - }, - Recipient: exported.CrossChainAddress{ - Chain: evm.Ethereum, - Address: evmtestutils.RandomAddress().Hex(), - }, - PayloadHash: evmtestutils.RandomHash().Bytes(), - Status: status, - Asset: nil, - SourceTxID: evmtestutils.RandomHash().Bytes(), - SourceTxIndex: uint64(rand.I64Between(0, 100)), - } -} - -func TestSetNewWasmMessage(t *testing.T) { - var ( - msg exported.GeneralMessage - ctx sdk.Context - keeper nexus.Keeper - ) - - cfg := app.MakeEncodingConfig() - givenKeeper := Given("the keeper", func() { - keeper, ctx = setup(cfg) - }) - - givenKeeper. - When("the message is valid", func() { - msg = randWasmMsg(exported.Approved) - }). - Branch( - When("the message contains token transfer", func() { - coin := rand.Coin() - msg.Asset = &coin - }). - Then("should return error", func(t *testing.T) { - assert.ErrorContains(t, keeper.SetNewWasmMessage(ctx, msg), "asset transfer is not supported") - }), - - When("the destination chain is not registered", func() { - msg.Recipient.Chain = nexustestutils.RandomChain() - }). - Then("should return error", func(t *testing.T) { - assert.ErrorContains(t, keeper.SetNewWasmMessage(ctx, msg), "is not a registered chain") - }), - - When("the destination chain is not activated", func() { - keeper.DeactivateChain(ctx, msg.Recipient.Chain) - }). - Then("should return error", func(t *testing.T) { - assert.ErrorContains(t, keeper.SetNewWasmMessage(ctx, msg), "is not activated") - }), - - When("the recipient address is invalid", func() { - msg.Recipient.Address = rand.Str(20) - }). - Then("should return error", func(t *testing.T) { - assert.ErrorContains(t, keeper.SetNewWasmMessage(ctx, msg), "invalid recipient address") - }), - - When("the message already exists", func() { - keeper.SetNewWasmMessage(ctx, msg) - }). - Then("should return error", func(t *testing.T) { - assert.ErrorContains(t, keeper.SetNewWasmMessage(ctx, msg), "already exists") - }), - - When("the message is invalid", func() { - msg.Sender.Address = "" - }). - Then("should return error", func(t *testing.T) { - assert.ErrorContains(t, keeper.SetNewWasmMessage(ctx, msg), "invalid source chain: invalid address: string is empty") - }), - - When("the message status is invalid", func() { - msg.Status = exported.Failed - }). - Then("should return error", func(t *testing.T) { - assert.ErrorContains(t, keeper.SetNewWasmMessage(ctx, msg), "invalid message status") - }), - ). - Run(t) - - givenKeeper. - Branch( - When("the message status is approved", func() { - msg = randWasmMsg(exported.Approved) - }). - Then("should be stored as approved and emit MessageReceived event", func(t *testing.T) { - assert.NoError(t, keeper.SetNewWasmMessage(ctx, msg)) - - actual, ok := keeper.GetMessage(ctx, msg.ID) - assert.True(t, ok) - assert.Equal(t, msg, actual) - assert.Equal(t, "axelar.nexus.v1beta1.MessageReceived", ctx.EventManager().Events()[len(ctx.EventManager().Events())-1].Type) - }), - - When("the message status is processing", func() { - msg = randWasmMsg(exported.Processing) - }). - Then("should be stored as processing and emit MessageProcessing event", func(t *testing.T) { - assert.NoError(t, keeper.SetNewWasmMessage(ctx, msg)) - - actual, ok := keeper.GetMessage(ctx, msg.ID) - assert.True(t, ok) - assert.Equal(t, msg, actual) - assert.Equal(t, "axelar.nexus.v1beta1.MessageProcessing", ctx.EventManager().Events()[len(ctx.EventManager().Events())-1].Type) - assert.Equal(t, msg, keeper.GetProcessingMessages(ctx, msg.GetDestinationChain(), 1)[0]) - }), - ). - Run(t) -} - -func TestSetNewGeneralMessage(t *testing.T) { - var ( - generalMessage exported.GeneralMessage - ctx sdk.Context - k nexus.Keeper - ) - cfg := app.MakeEncodingConfig() - sourceChain := nexustestutils.RandomChain() - sourceChain.Module = evmtypes.ModuleName - destinationChain := nexustestutils.RandomChain() - asset := rand.Coin() - - givenContractCallEvent := Given("a general message with token", func() { - k, ctx = setup(cfg) - generalMessage = exported.GeneralMessage{ - ID: fmt.Sprintf("%s-%d", evmtestutils.RandomHash().Hex(), rand.PosI64()), - - Sender: exported.CrossChainAddress{ - Chain: sourceChain, - Address: evmtestutils.RandomAddress().Hex(), - }, - Recipient: exported.CrossChainAddress{ - Chain: destinationChain, - Address: genCosmosAddr(destinationChain.Name.String()), - }, - Status: exported.Approved, - PayloadHash: crypto.Keccak256Hash(rand.Bytes(int(rand.I64Between(1, 100)))).Bytes(), - Asset: &asset, - } - }) - - whenChainsAreRegistered := givenContractCallEvent. - When("the source and destination chains are registered", func() { - k.SetChain(ctx, sourceChain) - k.SetChain(ctx, destinationChain) - }) - - errorWith := func(msg string) func(t *testing.T) { - return func(t *testing.T) { - assert.ErrorContains(t, k.SetNewMessage(ctx, generalMessage), msg) - } - } - - isCosmosChain := func(isCosmosChain bool) func() { - return func() { - if isCosmosChain { - destChain := funcs.MustOk(k.GetChain(ctx, destinationChain.Name)) - destChain.Module = axelarnet.ModuleName - k.SetChain(ctx, destChain) - - generalMessage.Recipient.Chain.Module = axelarnet.ModuleName - } - } - } - - isAssetRegistered := func(isRegistered bool) func() { - return func() { - if isRegistered { - funcs.MustNoErr(k.RegisterAsset(ctx, sourceChain, exported.Asset{Denom: asset.Denom, IsNativeAsset: false}, utils.MaxUint, time.Hour)) - funcs.MustNoErr(k.RegisterAsset(ctx, destinationChain, exported.Asset{Denom: asset.Denom, IsNativeAsset: false}, utils.MaxUint, time.Hour)) - } - } - } - - givenContractCallEvent. - When("the source chain is not registered", func() {}). - Then("should return error", errorWith(fmt.Sprintf("source chain %s is not a registered chain", sourceChain.Name))). - Run(t) - - givenContractCallEvent. - When("the destination chain is not registered", func() { - k.SetChain(ctx, sourceChain) - }). - Then("should return error", errorWith(fmt.Sprintf("destination chain %s is not a registered chain", destinationChain.Name))). - Run(t) - - whenChainsAreRegistered. - When("address validator for destination chain is set", isCosmosChain(true)). - When("destination address is invalid", func() { - generalMessage.Recipient.Address = rand.Str(20) - }). - Then("should return error", errorWith("decoding bech32 failed")). - Run(t) - - whenChainsAreRegistered. - When("address validator for destination chain is set", isCosmosChain(true)). - When("asset is not registered", isAssetRegistered(false)). - Then("should return error", errorWith("does not support foreign asset")). - Run(t) - - whenChainsAreRegistered. - When("address validator for destination chain is set", isCosmosChain(true)). - When("asset is registered", isAssetRegistered(true)). - Then("should succeed", func(t *testing.T) { - assert.NoError(t, k.SetNewMessage(ctx, generalMessage)) - }). - Run(t) -} - func TestGenerateMessageID(t *testing.T) { var ( ctx sdk.Context @@ -547,7 +327,6 @@ func TestGenerateMessageID(t *testing.T) { } func TestStatusTransitions(t *testing.T) { - cfg := app.MakeEncodingConfig() k, ctx := setup(cfg) sourceChain := nexustestutils.RandomChain() @@ -567,19 +346,21 @@ func TestStatusTransitions(t *testing.T) { } k.SetChain(ctx, sourceChain) k.SetChain(ctx, destinationChain) + k.ActivateChain(ctx, sourceChain) + k.ActivateChain(ctx, destinationChain) // Message doesn't exist, can't set any status err := k.SetMessageFailed(ctx, msg.ID) assert.Error(t, err, fmt.Sprintf("general message %s not found", msg.ID)) - err = k.SetMessageProcessing(ctx, msg.ID) + err = k.SetMessageProcessing_(ctx, msg.ID) assert.Error(t, err, fmt.Sprintf("general message %s not found", msg.ID)) err = k.SetMessageExecuted(ctx, msg.ID) assert.Error(t, err, fmt.Sprintf("general message %s not found", msg.ID)) // Now store the message with approved status - err = k.SetNewMessage(ctx, msg) + err = k.SetNewMessage_(ctx, msg) assert.NoError(t, err) err = k.SetMessageFailed(ctx, msg.ID) @@ -588,10 +369,10 @@ func TestStatusTransitions(t *testing.T) { err = k.SetMessageExecuted(ctx, msg.ID) assert.Error(t, err, "general message is not processed") - err = k.SetMessageProcessing(ctx, msg.ID) + err = k.SetMessageProcessing_(ctx, msg.ID) assert.NoError(t, err) - err = k.SetMessageProcessing(ctx, msg.ID) + err = k.SetMessageProcessing_(ctx, msg.ID) assert.Error(t, err, "general message is not approved or failed") err = k.SetMessageFailed(ctx, msg.ID) @@ -600,7 +381,7 @@ func TestStatusTransitions(t *testing.T) { err = k.SetMessageExecuted(ctx, msg.ID) assert.Error(t, err, "general message is not processed") - err = k.SetMessageProcessing(ctx, msg.ID) + err = k.SetMessageProcessing_(ctx, msg.ID) assert.NoError(t, err) err = k.SetMessageExecuted(ctx, msg.ID) @@ -609,7 +390,7 @@ func TestStatusTransitions(t *testing.T) { err = k.SetMessageFailed(ctx, msg.ID) assert.Error(t, err, "general message is not processed") - err = k.SetMessageProcessing(ctx, msg.ID) + err = k.SetMessageProcessing_(ctx, msg.ID) assert.Error(t, err, "general message is not approved or failed") } @@ -632,10 +413,8 @@ func TestGetMessage(t *testing.T) { SourceTxID: txID, SourceTxIndex: nonce, } - k.SetChain(ctx, sourceChain) - k.SetChain(ctx, destinationChain) - err := k.SetNewMessage(ctx, msg) + err := k.SetNewMessage_(ctx, msg) assert.NoError(t, err) exp, found := k.GetMessage(ctx, msg.ID) @@ -644,7 +423,6 @@ func TestGetMessage(t *testing.T) { } func TestGetSentMessages(t *testing.T) { - cfg := app.MakeEncodingConfig() k, ctx := setup(cfg) sourceChain := nexustestutils.RandomChain() @@ -653,9 +431,10 @@ func TestGetSentMessages(t *testing.T) { destinationChain.Module = evmtypes.ModuleName k.SetChain(ctx, sourceChain) k.SetChain(ctx, destinationChain) + k.ActivateChain(ctx, sourceChain) + k.ActivateChain(ctx, destinationChain) makeSentMessages := func(numMsgs int, destChainName exported.ChainName) map[string]exported.GeneralMessage { - msgs := make(map[string]exported.GeneralMessage) for i := 0; i < numMsgs; i++ { @@ -679,8 +458,22 @@ func TestGetSentMessages(t *testing.T) { } enqueueMsgs := func(msgs map[string]exported.GeneralMessage) { for _, msg := range msgs { - err := k.SetNewMessage(ctx, msg) - assert.NoError(t, err) + status := msg.Status + + msg.Status = exported.Approved + assert.NoError(t, k.SetNewMessage_(ctx, msg)) + + switch status { + case exported.Processing: + assert.NoError(t, k.SetMessageProcessing_(ctx, msg.ID)) + case exported.Executed: + assert.NoError(t, k.SetMessageProcessing_(ctx, msg.ID)) + assert.NoError(t, k.SetMessageExecuted(ctx, msg.ID)) + case exported.Failed: + assert.NoError(t, k.SetMessageProcessing_(ctx, msg.ID)) + assert.NoError(t, k.SetMessageFailed(ctx, msg.ID)) + default: + } } } @@ -753,7 +546,7 @@ func TestGetSentMessages(t *testing.T) { checkForExistence(msgs) //resend the failed message - err = k.SetMessageProcessing(ctx, msg.ID) + err = k.SetMessageProcessing_(ctx, msg.ID) assert.NoError(t, err) sent = consumeSent(destinationChainName, 1) assert.Equal(t, len(sent), 1) @@ -763,31 +556,36 @@ func TestGetSentMessages(t *testing.T) { assert.Equal(t, msg, ret) // add multiple destinations, make sure routing works - dest2 := exported.ChainName(rand.Str(5)) - k.SetChain(ctx, exported.Chain{ - Name: dest2, + chain2 := exported.Chain{ + Name: exported.ChainName(rand.Str(5)), SupportsForeignAssets: true, KeyType: 0, Module: "evm", - }) - dest3 := exported.ChainName(rand.Str(5)) - k.SetChain(ctx, exported.Chain{ - Name: dest3, + } + k.SetChain(ctx, chain2) + k.ActivateChain(ctx, chain2) + + chain3 := exported.Chain{ + Name: exported.ChainName(rand.Str(5)), SupportsForeignAssets: true, KeyType: 0, Module: "evm", - }) - dest4 := exported.ChainName(rand.Str(5)) - k.SetChain(ctx, exported.Chain{ - Name: dest4, + } + k.SetChain(ctx, chain3) + k.ActivateChain(ctx, chain3) + + chain4 := exported.Chain{ + Name: exported.ChainName(rand.Str(5)), SupportsForeignAssets: true, KeyType: 0, Module: "evm", - }) + } + k.SetChain(ctx, chain4) + k.ActivateChain(ctx, chain4) - dest2Msgs := makeSentMessages(10, dest2) - dest3Msgs := makeSentMessages(10, dest3) - dest4Msgs := makeSentMessages(10, dest4) + dest2Msgs := makeSentMessages(10, chain2.Name) + dest3Msgs := makeSentMessages(10, chain3.Name) + dest4Msgs := makeSentMessages(10, chain4.Name) enqueueMsgs(dest2Msgs) enqueueMsgs(dest3Msgs) @@ -795,7 +593,7 @@ func TestGetSentMessages(t *testing.T) { checkForExistence(dest2Msgs) checkForExistence(dest3Msgs) checkForExistence(dest4Msgs) - assert.Equal(t, dest2Msgs, toMap(consumeSent(dest2, 100))) - assert.Equal(t, dest3Msgs, toMap(consumeSent(dest3, 100))) - assert.Equal(t, dest4Msgs, toMap(consumeSent(dest4, 100))) + assert.Equal(t, dest2Msgs, toMap(consumeSent(chain2.Name, 100))) + assert.Equal(t, dest3Msgs, toMap(consumeSent(chain3.Name, 100))) + assert.Equal(t, dest4Msgs, toMap(consumeSent(chain4.Name, 100))) } diff --git a/x/nexus/keeper/genesis.go b/x/nexus/keeper/genesis.go index b2e1215d4..2465539c0 100644 --- a/x/nexus/keeper/genesis.go +++ b/x/nexus/keeper/genesis.go @@ -93,7 +93,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) { } for _, msg := range genState.Messages { - funcs.MustNoErr(k.SetNewMessage(ctx, msg)) + funcs.MustNoErr(k.setMessage(ctx, msg)) } utils.NewCounter[uint64](messageNonceKey, k.getStore(ctx)).Set(ctx, genState.MessageNonce) diff --git a/x/nexus/keeper/genesis_test.go b/x/nexus/keeper/genesis_test.go index d53da62d1..37d986ef0 100644 --- a/x/nexus/keeper/genesis_test.go +++ b/x/nexus/keeper/genesis_test.go @@ -205,7 +205,7 @@ func TestExportGenesisInitGenesis(t *testing.T) { id, _, _ := keeper.GenerateMessageID(ctx) msg := getRandomMessage(id) expected.Messages = append(expected.Messages, msg) - funcs.MustNoErr(keeper.SetNewMessage(ctx, msg)) + funcs.MustNoErr(keeper.setMessage(ctx, msg)) } expected.MessageNonce = uint64(messageCount) diff --git a/x/nexus/keeper/grpc_query_test.go b/x/nexus/keeper/grpc_query_test.go index 01dd80bfb..16a1bf275 100644 --- a/x/nexus/keeper/grpc_query_test.go +++ b/x/nexus/keeper/grpc_query_test.go @@ -211,7 +211,7 @@ func TestKeeper_Message(t *testing.T) { SourceTxID: txID, SourceTxIndex: nonce, } - err := k.SetNewMessage(ctx, msg) + err := k.SetNewMessage_(ctx, msg) assert.NoError(t, err) }).Then("should succeed", func(t *testing.T) { response, err := q.Message(sdk.WrapSDKContext(ctx), &types.MessageRequest{ID: id})