From a2311ee1293c7b58e2eb86ae5da3ed6ab070bd73 Mon Sep 17 00:00:00 2001 From: leonz789 Date: Sun, 2 Jun 2024 22:35:58 +0800 Subject: [PATCH] test(oracle): add test for updateparams --- x/oracle/keeper/keeper.go | 2 +- x/oracle/keeper/msg_server_update_params.go | 46 +++---- .../keeper/msg_server_update_params_test.go | 51 ++++++++ x/oracle/keeper/params_test.go | 114 ++++++++++++++++++ x/oracle/types/params.go | 13 +- 5 files changed, 201 insertions(+), 25 deletions(-) create mode 100644 x/oracle/keeper/msg_server_update_params_test.go diff --git a/x/oracle/keeper/keeper.go b/x/oracle/keeper/keeper.go index f367e14de..b0ef824e6 100644 --- a/x/oracle/keeper/keeper.go +++ b/x/oracle/keeper/keeper.go @@ -19,7 +19,7 @@ type ( storeKey storetypes.StoreKey memKey storetypes.StoreKey paramstore paramtypes.Subspace - authority string + // authority string common.KeeperStaking } ) diff --git a/x/oracle/keeper/msg_server_update_params.go b/x/oracle/keeper/msg_server_update_params.go index 1c419aa32..d68be0aac 100644 --- a/x/oracle/keeper/msg_server_update_params.go +++ b/x/oracle/keeper/msg_server_update_params.go @@ -18,42 +18,44 @@ func (ms msgServer) UpdateParams(goCtx context.Context, msg *types.MsgUpdatePara var err error height := uint64(ctx.BlockHeight()) // add sources - if len(msg.Params.Sources) > 1 { - if p, err = p.AddSources(msg.Params.Sources[1:]...); err != nil { - return nil, err - } + // if len(msg.Params.Sources) > 1 { + // if p, err = p.AddSources(msg.Params.Sources[1:]...); err != nil { + if p, err = p.AddSources(msg.Params.Sources...); err != nil { + return nil, err } + // } // add chains - if len(msg.Params.Chains) > 1 { - if p, err = p.AddChains(msg.Params.Chains[1:]...); err != nil { - return nil, err - } + // if len(msg.Params.Chains) > 1 { + if p, err = p.AddChains(msg.Params.Chains...); err != nil { + return nil, err } + // } // add tokens - if len(msg.Params.Tokens) > 1 { - if p, err = p.AddTokens(msg.Params.Tokens[1:]...); err != nil { - return nil, err - } + // if len(msg.Params.Tokens) > 1 { + if p, err = p.UpdateTokens(msg.Params.Tokens...); err != nil { + return nil, err } + // } // add rules - if len(msg.Params.Rules) > 1 { - if p, err = p.AddRules(msg.Params.Rules[1:]...); err != nil { - return nil, err - } + // if len(msg.Params.Rules) > 1 { + if p, err = p.AddRules(msg.Params.Rules...); err != nil { + return nil, err } + // } // udpate tokenFeeders - if len(msg.Params.TokenFeeders) > 1 { - for _, tokenFeeder := range msg.Params.TokenFeeders[1:] { - if p, err = p.UpdateTokenFeeder(tokenFeeder, height); err != nil { - return nil, err - } + // if len(msg.Params.TokenFeeders) > 1 { + // for _, tokenFeeder := range msg.Params.TokenFeeders[1:] { + for _, tokenFeeder := range msg.Params.TokenFeeders { + if p, err = p.UpdateTokenFeeder(tokenFeeder, height); err != nil { + return nil, err } } + // } // validate params if err = p.Validate(); err != nil { return nil, err } // set updated new params - ms.SetParams(ctx, msg.Params) + ms.SetParams(ctx, p) return &types.MsgUpdateParamsResponse{}, nil } diff --git a/x/oracle/keeper/msg_server_update_params_test.go b/x/oracle/keeper/msg_server_update_params_test.go new file mode 100644 index 000000000..797fe14f6 --- /dev/null +++ b/x/oracle/keeper/msg_server_update_params_test.go @@ -0,0 +1,51 @@ +package keeper_test + +import ( + "github.com/ExocoreNetwork/exocore/x/oracle/types" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("MsgUpdateParams", func() { + BeforeEach(func() { + ks.Reset() + Expect(ks.ms).ToNot(BeNil()) + }) + Context("", func() { + It("update StartBaseBlock for TokenFeeder", func() { + p := ks.k.GetParams(ks.ctx) + p.TokenFeeders[1].StartBaseBlock = 10 + ks.k.SetParams(ks.ctx, p) + p.TokenFeeders[1].StartBaseBlock = 5 + _, err := ks.ms.UpdateParams(ks.ctx, &types.MsgUpdateParams{ + Params: types.Params{ + TokenFeeders: []*types.TokenFeeder{ + { + TokenID: 1, + StartBaseBlock: 5, + }, + }, + }, + }) + Expect(err).Should(BeNil()) + p = ks.k.GetParams(ks.ctx) + Expect(p.TokenFeeders[1].StartBaseBlock).Should(BeEquivalentTo(5)) + }) + It("Add AssetID for Token", func() { + _, err := ks.ms.UpdateParams(ks.ctx, &types.MsgUpdateParams{ + Params: types.Params{ + Tokens: []*types.Token{ + { + Name: "ETH", + ChainID: 1, + AssetID: "0x83e6850591425e3c1e263c054f4466838b9bd9e4_0x9ce1", + }, + }, + }, + }) + Expect(err).Should(BeNil()) + p := ks.k.GetParams(ks.ctx) + Expect(p.Tokens[1].AssetID).Should(BeEquivalentTo("0x83e6850591425e3c1e263c054f4466838b9bd9e4_0x9ce1")) + }) + }) +}) diff --git a/x/oracle/keeper/params_test.go b/x/oracle/keeper/params_test.go index 3fb9269b8..baf774558 100644 --- a/x/oracle/keeper/params_test.go +++ b/x/oracle/keeper/params_test.go @@ -16,3 +16,117 @@ func TestGetParams(t *testing.T) { require.EqualValues(t, params, k.GetParams(ctx)) } + +func TestUpdateTokenFeeder(t *testing.T) { + tests := []struct { + name string + tokenFeeder types.TokenFeeder + height uint64 + err error + }{ + // fail when add/update fields, before validation + { + name: "invalid update, empty fields to update", + tokenFeeder: types.TokenFeeder{ + TokenID: 1, + }, + height: 1, + err: types.ErrInvalidParams.Wrap("invalid tokenFeeder to update, no valid field set"), + }, + { + name: "invalid udpate, for not-start feeder, set StartbaseBlock to history height", + tokenFeeder: types.TokenFeeder{ + TokenID: 1, + // set current height to 100 to test fail case + StartBaseBlock: 10, + }, + height: 100, + err: types.ErrInvalidParams.Wrap("invalid tokenFeeder to update, invalid StartBaseBlock"), + }, + { + name: "invalid update, for running feeder, set EndBlock to history height", + tokenFeeder: types.TokenFeeder{ + TokenID: 1, + // set current height to 2000000 to test fail case + EndBlock: 1500000, + }, + height: 2000000, + err: types.ErrInvalidParams.Wrap("invalid tokenFeeder to update, invalid EndBlock"), + }, + { + name: "invalid update, for stopped feeder, restart a feeder with wrong StartRoundID", + tokenFeeder: types.TokenFeeder{ + TokenID: 2, + RuleID: 1, + // set current height to 100 + StartBaseBlock: 1000, + // should be 4 + StartRoundID: 5, + Interval: 10, + }, + height: 100, + err: types.ErrInvalidParams.Wrap("invalid tokenFeeder to update"), + }, + // success adding/updating, but fail validation + { + name: "invalid update, for new feeder, EndBlock is not set properly", + tokenFeeder: types.TokenFeeder{ + TokenID: 3, + StartBaseBlock: 10, + StartRoundID: 1, + Interval: 10, + EndBlock: 51, + }, + height: 1, + err: types.ErrInvalidParams.Wrap("invalid tokenFeeder, invalid EndBlock"), + }, + { + name: "invalid update, for new feeder, tokenID not exists", + tokenFeeder: types.TokenFeeder{ + TokenID: 4, + StartBaseBlock: 10, + StartRoundID: 1, + Interval: 10, + EndBlock: 58, + }, + height: 1, + err: types.ErrInvalidParams.Wrap("invalid tokenFeeder, non-exist tokenID referred"), + }, + } + p := types.DefaultParams() + p.Tokens = append(p.Tokens, &types.Token{ + Name: "TEST", + ChainID: 1, + ContractAddress: "0x", + Decimal: 8, + Active: true, + AssetID: "", + }) + p.Tokens = append(p.Tokens, &types.Token{ + Name: "TEST_NEW", + ChainID: 1, + ContractAddress: "0x", + Decimal: 8, + Active: true, + AssetID: "", + }) + p.TokenFeeders = append(p.TokenFeeders, &types.TokenFeeder{ + TokenID: 2, + RuleID: 1, + StartRoundID: 1, + StartBaseBlock: 10, + Interval: 10, + EndBlock: 38, + }) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p, err := p.UpdateTokenFeeder(&tt.tokenFeeder, tt.height) + if err == nil { + err = p.Validate() + } + if tt.err != nil { + require.ErrorIs(t, err, tt.err) + } + }) + } +} diff --git a/x/oracle/types/params.go b/x/oracle/types/params.go index a9924646b..c08ce6a50 100644 --- a/x/oracle/types/params.go +++ b/x/oracle/types/params.go @@ -234,14 +234,23 @@ func (p Params) AddChains(chains ...*Chain) (Params, error) { return p, nil } -func (p Params) AddTokens(tokens ...*Token) (Params, error) { +func (p Params) UpdateTokens(tokens ...*Token) (Params, error) { for _, t := range tokens { + update := false for _, token := range p.Tokens { if token.ChainID == t.ChainID && token.Name == t.Name { + // AssetID is only field can be updated/modified + if len(t.AssetID) > 0 { + token.AssetID = t.AssetID + update = true + break + } return p, ErrInvalidParams.Wrap("invalid token to add, duplicated") } } - p.Tokens = append(p.Tokens, t) + if !update { + p.Tokens = append(p.Tokens, t) + } } return p, nil }