Skip to content

Commit

Permalink
add test & emit event for param change
Browse files Browse the repository at this point in the history
  • Loading branch information
colmazia committed Aug 16, 2023
1 parent 8912759 commit 98268a6
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 5 deletions.
92 changes: 92 additions & 0 deletions x/oracle/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,3 +823,95 @@ func TestActivateFail(t *testing.T) {
_, err = oracle.NewHandler(k)(ctx, msg)
require.NoError(t, err)
}

func TestUpdateParamsSuccess(t *testing.T) {
_, ctx, k := testapp.CreateTestInput(true)
expectedParams := types.Params{
MaxRawRequestCount: 1,
MaxAskCount: 10,
MaxCalldataSize: 256,
MaxReportDataSize: 512,
ExpirationBlockCount: 30,
BaseOwasmGas: 50000,
PerValidatorRequestGas: 3000,
SamplingTryCount: 3,
OracleRewardPercentage: 50,
InactivePenaltyDuration: 1000,
IBCRequestEnabled: true,
}
msg := types.NewMsgUpdateParams(k.GetAuthority(), expectedParams)
res, err := oracle.NewHandler(k)(ctx, msg)
require.NoError(t, err)
require.Equal(t, expectedParams, k.GetParams(ctx))
event := abci.Event{
Type: types.EventTypeUpdateParams,
Attributes: []abci.EventAttribute{
{Key: types.AttributeKeyParams, Value: expectedParams.String()},
},
}
require.Equal(t, abci.Event(event), res.Events[0])

expectedParams = types.Params{
MaxRawRequestCount: 2,
MaxAskCount: 20,
MaxCalldataSize: 512,
MaxReportDataSize: 256,
ExpirationBlockCount: 40,
BaseOwasmGas: 0,
PerValidatorRequestGas: 0,
SamplingTryCount: 5,
OracleRewardPercentage: 0,
InactivePenaltyDuration: 0,
IBCRequestEnabled: false,
}
msg = types.NewMsgUpdateParams(k.GetAuthority(), expectedParams)
res, err = oracle.NewHandler(k)(ctx, msg)
require.NoError(t, err)
require.Equal(t, expectedParams, k.GetParams(ctx))
event = abci.Event{
Type: types.EventTypeUpdateParams,
Attributes: []abci.EventAttribute{
{Key: types.AttributeKeyParams, Value: expectedParams.String()},
},
}
require.Equal(t, abci.Event(event), res.Events[0])
}

func TestUpdateParamsFail(t *testing.T) {
_, ctx, k := testapp.CreateTestInput(true)
expectedParams := types.Params{
MaxRawRequestCount: 1,
MaxAskCount: 10,
MaxCalldataSize: 256,
MaxReportDataSize: 512,
ExpirationBlockCount: 30,
BaseOwasmGas: 50000,
PerValidatorRequestGas: 3000,
SamplingTryCount: 3,
OracleRewardPercentage: 50,
InactivePenaltyDuration: 1000,
IBCRequestEnabled: true,
}
msg := types.NewMsgUpdateParams("foo", expectedParams)
res, err := oracle.NewHandler(k)(ctx, msg)
require.ErrorContains(t, err, "invalid authority")
require.Nil(t, res)

expectedParams = types.Params{
MaxRawRequestCount: 0,
MaxAskCount: 10,
MaxCalldataSize: 256,
MaxReportDataSize: 512,
ExpirationBlockCount: 30,
BaseOwasmGas: 50000,
PerValidatorRequestGas: 3000,
SamplingTryCount: 3,
OracleRewardPercentage: 50,
InactivePenaltyDuration: 1000,
IBCRequestEnabled: true,
}
msg = types.NewMsgUpdateParams(k.GetAuthority(), expectedParams)
res, err = oracle.NewHandler(k)(ctx, msg)
require.ErrorContains(t, err, "max raw request count must be positive")
require.Nil(t, res)
}
13 changes: 9 additions & 4 deletions x/oracle/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,21 +293,26 @@ func (k msgServer) Activate(goCtx context.Context, msg *types.MsgActivate) (*typ

func (ms msgServer) UpdateParams(
goCtx context.Context,
req *types.MsgUpdateParams,
msg *types.MsgUpdateParams,
) (*types.MsgUpdateParamsResponse, error) {
if ms.authority != req.Authority {
if ms.authority != msg.Authority {
return nil, sdkerrors.Wrapf(
govtypes.ErrInvalidSigner,
"invalid authority; expected %s, got %s",
ms.authority,
req.Authority,
msg.Authority,
)
}

ctx := sdk.UnwrapSDKContext(goCtx)
if err := ms.SetParams(ctx, req.Params); err != nil {
if err := ms.SetParams(ctx, msg.Params); err != nil {
return nil, err
}

ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeUpdateParams,
sdk.NewAttribute(types.AttributeKeyParams, msg.Params.String()),
))

return &types.MsgUpdateParamsResponse{}, nil
}
34 changes: 34 additions & 0 deletions x/oracle/keeper/params_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -26,6 +27,7 @@ func TestGetSetParams(t *testing.T) {
}
k.SetParams(ctx, expectedParams)
require.Equal(t, expectedParams, k.GetParams(ctx))

expectedParams = types.Params{
MaxRawRequestCount: 2,
MaxAskCount: 20,
Expand All @@ -41,4 +43,36 @@ func TestGetSetParams(t *testing.T) {
}
k.SetParams(ctx, expectedParams)
require.Equal(t, expectedParams, k.GetParams(ctx))

expectedParams = types.Params{
MaxRawRequestCount: 2,
MaxAskCount: 20,
MaxCalldataSize: 512,
MaxReportDataSize: 256,
ExpirationBlockCount: 40,
BaseOwasmGas: 0,
PerValidatorRequestGas: 0,
SamplingTryCount: 5,
OracleRewardPercentage: 0,
InactivePenaltyDuration: 0,
IBCRequestEnabled: false,
}
k.SetParams(ctx, expectedParams)
require.Equal(t, expectedParams, k.GetParams(ctx))

expectedParams = types.Params{
MaxRawRequestCount: 0,
MaxAskCount: 20,
MaxCalldataSize: 512,
MaxReportDataSize: 256,
ExpirationBlockCount: 40,
BaseOwasmGas: 150000,
PerValidatorRequestGas: 30000,
SamplingTryCount: 5,
OracleRewardPercentage: 80,
InactivePenaltyDuration: 10000,
IBCRequestEnabled: false,
}
err := k.SetParams(ctx, expectedParams)
require.EqualError(t, fmt.Errorf("max raw request count must be positive: 0"), err.Error())
}
45 changes: 45 additions & 0 deletions x/oracle/migrations/v2/migrator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package v2_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/bandprotocol/chain/v2/x/oracle"
"github.com/bandprotocol/chain/v2/x/oracle/exported"
v2 "github.com/bandprotocol/chain/v2/x/oracle/migrations/v2"
"github.com/bandprotocol/chain/v2/x/oracle/types"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
)

type mockSubspace struct {
ps types.Params
}

func newMockSubspace(ps types.Params) mockSubspace {
return mockSubspace{ps: ps}
}

func (ms mockSubspace) GetParamSet(ctx sdk.Context, ps exported.ParamSet) {
*ps.(*types.Params) = ms.ps
}

func TestMigrate(t *testing.T) {
encCfg := moduletestutil.MakeTestEncodingConfig(oracle.AppModuleBasic{})
cdc := encCfg.Codec

storeKey := sdk.NewKVStoreKey(v2.ModuleName)
tKey := sdk.NewTransientStoreKey("transient_test")
ctx := testutil.DefaultContext(storeKey, tKey)
store := ctx.KVStore(storeKey)

legacySubspace := newMockSubspace(types.DefaultParams())
require.NoError(t, v2.Migrate(ctx, store, legacySubspace, cdc))

var res types.Params
bz := store.Get(types.ParamsKeyPrefix)
require.NoError(t, cdc.Unmarshal(bz, &res))
require.Equal(t, legacySubspace.ps, res)
}
2 changes: 2 additions & 0 deletions x/oracle/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
EventTypeRemoveReporter = "remove_reporter"
EventTypeResolve = "resolve"
EventTypeSendPacketFail = "send_packet_fail"
EventTypeUpdateParams = "update_params"

AttributeKeyID = "id"
AttributeKeyDataSourceID = "data_source_id"
Expand All @@ -33,4 +34,5 @@ const (
AttributeKeyFee = "fee"
AttributeKeyResult = "result"
AttributeKeyReason = "reason"
AttributeKeyParams = "params"
)
8 changes: 8 additions & 0 deletions x/oracle/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,14 @@ func (msg MsgActivate) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}

// NewMsgActivate creates a new MsgActivate instance
func NewMsgUpdateParams(authority string, params Params) *MsgUpdateParams {
return &MsgUpdateParams{
Authority: authority,
Params: params,
}
}

// GetSignBytes implements the LegacyMsg interface.
func (m MsgUpdateParams) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))
Expand Down
2 changes: 1 addition & 1 deletion x/oracle/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func DefaultParams() Params {

// Validate does the sanity check on the params.
func (p Params) Validate() error {
if err := validateUint64("max data source count", true)(p.MaxRawRequestCount); err != nil {
if err := validateUint64("max raw request count", true)(p.MaxRawRequestCount); err != nil {
return err
}
if err := validateUint64("max ask count", true)(p.MaxAskCount); err != nil {
Expand Down

0 comments on commit 98268a6

Please sign in to comment.