Skip to content

Commit

Permalink
test(oracle): add test for updateparams
Browse files Browse the repository at this point in the history
  • Loading branch information
leonz789 committed Jun 2, 2024
1 parent c6b54b3 commit a2311ee
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 25 deletions.
2 changes: 1 addition & 1 deletion x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type (
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
authority string
// authority string
common.KeeperStaking
}
)
Expand Down
46 changes: 24 additions & 22 deletions x/oracle/keeper/msg_server_update_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
51 changes: 51 additions & 0 deletions x/oracle/keeper/msg_server_update_params_test.go
Original file line number Diff line number Diff line change
@@ -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"))
})
})
})
114 changes: 114 additions & 0 deletions x/oracle/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
13 changes: 11 additions & 2 deletions x/oracle/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit a2311ee

Please sign in to comment.