Skip to content

Commit

Permalink
test: fix crosschain msg test (#240)
Browse files Browse the repository at this point in the history
Co-authored-by: fx0x55 <[email protected]>
Co-authored-by: nulnut <[email protected]>
  • Loading branch information
3 people authored Feb 29, 2024
1 parent a840f9e commit bf2703b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
8 changes: 2 additions & 6 deletions x/crosschain/keeper/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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,
Expand Down
38 changes: 36 additions & 2 deletions x/crosschain/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"context"
"encoding/hex"
"fmt"
"math"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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")
}

0 comments on commit bf2703b

Please sign in to comment.