Skip to content

Commit

Permalink
add relay message test
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Jan 23, 2024
1 parent 0d13471 commit 47bc8e8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions x/opchild/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ func _createTestInput(
keepers := TestKeepers{
AccountKeeper: accountKeeper,
BankKeeper: bankKeeper,
OracleKeeper: oracleKeeper,
OPChildKeeper: *opchildKeeper,
BridgeHook: bridgeHook,
EncodingConfig: encodingConfig,
Expand Down
1 change: 1 addition & 0 deletions x/opchild/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
49 changes: 47 additions & 2 deletions x/opchild/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

0 comments on commit 47bc8e8

Please sign in to comment.