From 47bc8e883ce5c99da41b8c0d7b04d309c5fc8414 Mon Sep 17 00:00:00 2001 From: beer-1 <147697694+beer-1@users.noreply.github.com> Date: Tue, 23 Jan 2024 17:53:05 +0900 Subject: [PATCH] add relay message test --- x/opchild/keeper/common_test.go | 1 + x/opchild/keeper/msg_server.go | 1 + x/opchild/keeper/msg_server_test.go | 49 +++++++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/x/opchild/keeper/common_test.go b/x/opchild/keeper/common_test.go index 06ad9201..dcbc082d 100644 --- a/x/opchild/keeper/common_test.go +++ b/x/opchild/keeper/common_test.go @@ -335,6 +335,7 @@ func _createTestInput( keepers := TestKeepers{ AccountKeeper: accountKeeper, BankKeeper: bankKeeper, + OracleKeeper: oracleKeeper, OPChildKeeper: *opchildKeeper, BridgeHook: bridgeHook, EncodingConfig: encodingConfig, diff --git a/x/opchild/keeper/msg_server.go b/x/opchild/keeper/msg_server.go index ff43cce0..6c5ce60e 100644 --- a/x/opchild/keeper/msg_server.go +++ b/x/opchild/keeper/msg_server.go @@ -370,6 +370,7 @@ func (ms MsgServer) RelayOraclePrices(ctx context.Context, req *types.MsgRelayOr sdkCtx := sdk.UnwrapSDKContext(ctx) events := make([]sdk.Event, 0, len(req.Prices)) for _, price := range req.Prices { + // in slinky, it will automatically create currency pair. if err := ms.oracleKeeper.SetPriceForCurrencyPair(sdkCtx, oracletypes.NewCurrencyPair(price.Base, price.Quote), oracletypes.QuotePrice{ diff --git a/x/opchild/keeper/msg_server_test.go b/x/opchild/keeper/msg_server_test.go index d7101417..c79eeb2d 100644 --- a/x/opchild/keeper/msg_server_test.go +++ b/x/opchild/keeper/msg_server_test.go @@ -5,11 +5,14 @@ import ( "encoding/json" "errors" "testing" + "time" - "cosmossdk.io/math" "github.com/stretchr/testify/require" "golang.org/x/crypto/sha3" + "cosmossdk.io/math" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + testutilsims "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" @@ -330,5 +333,47 @@ func Test_MsgServer_Relay_OraclePrices(t *testing.T) { ctx, input := createDefaultTestInput(t) ms := keeper.NewMsgServerImpl(input.OPChildKeeper) - ms.RelayOraclePrices(ctx) + // valid relay + msg := types.NewMsgRelayOraclePrices(addrsStr[0], []types.OraclePrice{ + { + Base: "BITCOIN", + Quote: "USD", + Price: math.NewInt(1), + L1BlockHeight: 1, + L1BlockTime: time.Unix(1, 0).UTC(), + }, + { + Base: "ETHEREUM", + Quote: "USD", + Price: math.NewInt(2), + L1BlockHeight: 2, + L1BlockTime: time.Unix(2, 0).UTC(), + }, + }) + _, err := ms.RelayOraclePrices(ctx, msg) + require.NoError(t, err) + + sdkCtx := sdk.UnwrapSDKContext(ctx) + for _, cp := range input.OracleKeeper.GetAllCurrencyPairs(sdkCtx) { + price, err := input.OracleKeeper.GetPriceForCurrencyPair(sdkCtx, cp) + require.NoError(t, err) + + if cp.Base == "BITCOIN" { + require.Equal(t, cp.Quote, "USD") + require.Equal(t, math.NewInt(1), price.Price) + require.Equal(t, uint64(1), price.BlockHeight) + require.Equal(t, time.Unix(1, 0).UTC(), price.BlockTimestamp) + } else { + require.Equal(t, cp.Base, "ETHEREUM") + require.Equal(t, cp.Quote, "USD") + require.Equal(t, math.NewInt(2), price.Price) + require.Equal(t, uint64(2), price.BlockHeight) + require.Equal(t, time.Unix(2, 0).UTC(), price.BlockTimestamp) + } + } + + // unauthorized + msg.Sender = addrsStr[1] + _, err = ms.RelayOraclePrices(ctx, msg) + require.ErrorIs(t, err, sdkerrors.ErrUnauthorized) }