From 36c2bd5f3ddbda2b6c51ab2a9d1ad22fd8ec7b99 Mon Sep 17 00:00:00 2001 From: fx0x55 <80245546+fx0x55@users.noreply.github.com> Date: Thu, 29 Feb 2024 09:44:01 +0800 Subject: [PATCH] test: fix crosschain msg test (#164) Co-authored-by: nulnut <151493716+nulnut@users.noreply.github.com> Co-authored-by: zakir <80246097+zakir-code@users.noreply.github.com> --- x/crosschain/keeper/abci_test.go | 8 ++---- x/crosschain/keeper/msg_server_test.go | 38 ++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/x/crosschain/keeper/abci_test.go b/x/crosschain/keeper/abci_test.go index 9c1026f5b..d2ab77fc7 100644 --- a/x/crosschain/keeper/abci_test.go +++ b/x/crosschain/keeper/abci_test.go @@ -216,13 +216,11 @@ func (suite *KeeperTestSuite) TestAttestationAfterOracleUpdate() { require.True(suite.T(), expectPower.Equal(power)) } - bridgeToken := helpers.GenerateAddressByModule(suite.chainName) - { firstBridgeTokenClaim := &types.MsgBridgeTokenClaim{ EventNonce: 1, BlockHeight: 1000, - TokenContract: bridgeToken, + TokenContract: helpers.GenerateAddressByModule(suite.chainName), Name: "Test Token", Symbol: "TEST", Decimals: 18, @@ -241,8 +239,6 @@ func (suite *KeeperTestSuite) TestAttestationAfterOracleUpdate() { require.NotNil(suite.T(), endBlockBeforeAttestation.Votes) require.EqualValues(suite.T(), i+1, len(endBlockBeforeAttestation.Votes)) - suite.app.EndBlock(abci.RequestEndBlock{Height: suite.ctx.BlockHeight()}) - suite.ctx = suite.ctx.WithBlockHeight(suite.ctx.BlockHeight() + 1) endBlockAfterAttestation := suite.Keeper().GetAttestation(suite.ctx, firstBridgeTokenClaim.EventNonce, firstBridgeTokenClaim.ClaimHash()) require.NotNil(suite.T(), endBlockAfterAttestation) require.False(suite.T(), endBlockAfterAttestation.Observed) @@ -263,7 +259,7 @@ func (suite *KeeperTestSuite) TestAttestationAfterOracleUpdate() { secondBridgeTokenClaim := &types.MsgBridgeTokenClaim{ EventNonce: 2, BlockHeight: 1001, - TokenContract: bridgeToken, + TokenContract: helpers.GenerateAddressByModule(suite.chainName), Name: "Test Token2", Symbol: "TEST2", Decimals: 18, diff --git a/x/crosschain/keeper/msg_server_test.go b/x/crosschain/keeper/msg_server_test.go index 488abbdb8..779652e25 100644 --- a/x/crosschain/keeper/msg_server_test.go +++ b/x/crosschain/keeper/msg_server_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "context" "encoding/hex" "fmt" "math" @@ -670,6 +671,7 @@ func (suite *KeeperTestSuite) TestClaimMsgGasConsumed() { execute: func(claimMsg types.ExternalClaim) (minGas, maxGas, avgGas uint64) { msg, ok := claimMsg.(*types.MsgSendToFxClaim) suite.True(ok) + suite.Keeper().AddBridgeToken(suite.ctx, msg.TokenContract, fmt.Sprintf("%s%s", suite.chainName, msg.TokenContract)) for i, oracle := range suite.oracleAddrs { eventNonce := suite.Keeper().GetLastEventNonceByOracle(suite.ctx, oracle) msg.EventNonce = eventNonce + 1 @@ -703,6 +705,11 @@ func (suite *KeeperTestSuite) TestClaimMsgGasConsumed() { execute: func(claimMsg types.ExternalClaim) (minGas, maxGas, avgGas uint64) { msg, ok := claimMsg.(*types.MsgOracleSetUpdatedClaim) suite.True(ok) + suite.Keeper().StoreOracleSet(suite.ctx, &types.OracleSet{ + Nonce: msg.OracleSetNonce, + Height: msg.BlockHeight, + Members: msg.Members, + }) for i, oracle := range suite.oracleAddrs { eventNonce := suite.Keeper().GetLastEventNonceByOracle(suite.ctx, oracle) msg.EventNonce = eventNonce + 1 @@ -1115,7 +1122,8 @@ func (suite *KeeperTestSuite) TestBridgeCallClaim() { require.EqualValues(suite.T(), 0, oracleLastEventNonce) tokenContract := helpers.GenerateAddressByModule(suite.chainName) - _, err = suite.MsgServer().BridgeTokenClaim(sdk.WrapSDKContext(suite.ctx), &types.MsgBridgeTokenClaim{ + ctx := sdk.WrapSDKContext(suite.ctx.WithEventManager(sdk.NewEventManager())) + _, err = suite.MsgServer().BridgeTokenClaim(ctx, &types.MsgBridgeTokenClaim{ EventNonce: oracleLastEventNonce + 1, BlockHeight: uint64(suite.ctx.BlockHeight()), TokenContract: tokenContract, @@ -1127,6 +1135,7 @@ func (suite *KeeperTestSuite) TestBridgeCallClaim() { ChainName: suite.chainName, }) require.NoError(suite.T(), err) + suite.checkObservationState(ctx, true) oracleLastEventNonce = suite.Keeper().GetLastEventNonceByOracle(suite.ctx, suite.oracleAddrs[0]) require.EqualValues(suite.T(), 1, oracleLastEventNonce) @@ -1189,11 +1198,36 @@ func (suite *KeeperTestSuite) TestBridgeCallClaim() { for _, testData := range testMsgs { err = testData.msg.ValidateBasic() require.NoError(suite.T(), err) - _, err = suite.MsgServer().BridgeCallClaim(sdk.WrapSDKContext(suite.ctx), testData.msg) + ctx = sdk.WrapSDKContext(suite.ctx.WithEventManager(sdk.NewEventManager())) + _, err = suite.MsgServer().BridgeCallClaim(ctx, testData.msg) require.ErrorIs(suite.T(), err, testData.err, testData.name) + if testData.err == nil { + suite.checkObservationState(ctx, true) + } if err == nil { continue } + require.EqualValues(suite.T(), testData.errReason, err.Error(), testData.name) } } + +func (suite *KeeperTestSuite) checkObservationState(ctx context.Context, expect bool) { + foundObservation := false + sdkCtx := sdk.UnwrapSDKContext(ctx) + for _, event := range sdkCtx.EventManager().Events() { + if event.Type != types.EventTypeContractEvent { + continue + } + suite.Require().False(foundObservation, "found multiple observation event") + for _, attr := range event.Attributes { + if string(attr.Key) != types.AttributeKeyStateSuccess { + continue + } + suite.Require().EqualValues(fmt.Sprintf("%v", expect), string(attr.Value)) + foundObservation = true + break + } + } + suite.Require().True(foundObservation, "not found observation event") +}