Skip to content

Commit

Permalink
fix map iteration, ref
Browse files Browse the repository at this point in the history
  • Loading branch information
leonz789 committed Jan 13, 2025
1 parent 2e7bf74 commit 9f25460
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 26 deletions.
6 changes: 4 additions & 2 deletions x/oracle/keeper/feedermanagement/caches.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package feedermanagement

import (
"fmt"
"math/big"
"reflect"

Check notice

Code scanning / CodeQL

Sensitive package import Note

Certain system packages contain functions which may be a possible source of non-determinism
"slices"
Expand Down Expand Up @@ -233,7 +234,7 @@ func (cp *cacheParams) commit(ctx sdk.Context, k Submitter) {
}

// memory cache
func (c *caches) AddCache(i any) {
func (c *caches) AddCache(i any) error {
switch item := i.(type) {
case *oracletypes.MsgItem:
c.msg.add(item)
Expand All @@ -242,8 +243,9 @@ func (c *caches) AddCache(i any) {
case ItemV:
c.validators.add(item)
default:
panic("no other types are support")
return fmt.Errorf("unsuppported caceh type: %T", i)
}
return nil
}

// Read reads the cache
Expand Down
12 changes: 8 additions & 4 deletions x/oracle/keeper/feedermanagement/feedermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (f *FeederManager) EndBlock(ctx sdk.Context) {
func (f *FeederManager) EndBlockInRecovery(ctx sdk.Context, params *oracletypes.Params) {
if params != nil {
f.SetParamsUpdated()
f.cs.AddCache(params)
_ = f.cs.AddCache(params)
}
f.updateAndCommitRoundsInRecovery(ctx)
f.prepareRounds(ctx)
Expand Down Expand Up @@ -197,7 +197,7 @@ func (f *FeederManager) updateAndCommitCaches(ctx sdk.Context) (addedValidators
if paramsOld.IsSlashingResetUpdate(&params) {
f.SetResetSlasing()
}
f.cs.AddCache(&params)
_ = f.cs.AddCache(&params)
}

// update validators
Expand All @@ -216,7 +216,7 @@ func (f *FeederManager) updateAndCommitCaches(ctx sdk.Context) (addedValidators
}
}
// update validator set information in cache
f.cs.AddCache(ItemV(validatorMap))
_ = f.cs.AddCache(ItemV(validatorMap))
}

// commit caches: msgs is exists, params if updated, validatorPowers is updated
Expand Down Expand Up @@ -250,11 +250,14 @@ func (f *FeederManager) commitRounds(ctx sdk.Context) {
finalPrice, ok := r.FinalPrice()
if !ok {
logger.Info("commit round with price from previous", "feederID", r.feederID, "roundID", r.roundID, "baseBlock", r.roundBaseBlock, "heigth", height)
// #nosec G115 // tokenID is index of slice
f.k.GrowRoundID(ctx, uint64(r.tokenID))
} else {
if f.cs.IsRuleV1(r.feederID) {
priceCommit := finalPrice.ProtoPriceTimeRound(r.roundID, ctx.BlockTime().Format(oracletypes.TimeLayout))
logger.Info("commit round with aggregated price", "feederID", r.feederID, "roundID", r.roundID, "baseBlock", r.roundBaseBlock, "price", priceCommit, "heigth", height)

// #nosec G115 // tokenID is index of slice
f.k.AppendPriceTR(ctx, uint64(r.tokenID), *priceCommit)

fstr := strconv.FormatInt(feederID, 10)
Expand Down Expand Up @@ -366,6 +369,7 @@ func (f *FeederManager) handleQuotingMisBehavior(ctx sdk.Context) {
continue
}
reportedRoundsWindow := f.k.GetReportedRoundsWindow(ctx)
// #nosec G115
index := uint64(reportedInfo.IndexOffset % reportedRoundsWindow)
reportedInfo.IndexOffset++
// Update reported round bit array & counter
Expand Down Expand Up @@ -585,7 +589,7 @@ func (f *FeederManager) ProcessQuote(ctx sdk.Context, msg *oracletypes.MsgCreate
if !isCheckTx &&
validMsgItem != nil &&
(err == nil || sdkerrors.IsOf(err, oracletypes.ErrQuoteRecorded)) {
f.cs.AddCache(validMsgItem)
_ = f.cs.AddCache(validMsgItem)
}
}()

Expand Down
2 changes: 1 addition & 1 deletion x/oracle/keeper/feedermanagement/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (t *Test) NewRecordsDS(filled bool) *recordsDS {
ret.accumulatedPowers = big1
ret.records = append(ret.records, t.NewPricePower())
}
return nil
return ret
}

func (t *Test) NewRecordsDSs(filled bool) *recordsDSs {
Expand Down
9 changes: 8 additions & 1 deletion x/oracle/keeper/feedermanagement/prices.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math/big"
"reflect"

Check notice

Code scanning / CodeQL

Sensitive package import Note

Certain system packages contain functions which may be a possible source of non-determinism
"slices"
"sort"

oracletypes "github.com/ExocoreNetwork/exocore/x/oracle/types"
Expand Down Expand Up @@ -153,7 +154,13 @@ func (pv *priceValidator) GetFinalPrice() (*PriceResult, bool) {
if len(pv.priceSources) == 0 {
return nil, false
}
for _, price := range pv.priceSources {
keySlice := make([]int64, 0, len(pv.priceSources))
for sourceID := range pv.priceSources {
keySlice = append(keySlice, sourceID)
}

Check warning

Code scanning / CodeQL

Iteration over map Warning

Iteration over map may be a possible source of non-determinism
slices.Sort(keySlice)
for _, sourceID := range keySlice {
price := pv.priceSources[sourceID]
if price.finalPrice == nil {
defaultAggMedian.Reset()
return nil, false
Expand Down
16 changes: 11 additions & 5 deletions x/oracle/keeper/feedermanagement/round.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ func (r *round) CopyForCheckTx() *round {
return &ret
}

func (r *round) getMsgItemFromProto(msg *oracletypes.MsgItem) *MsgItem {
power, _ := r.cache.GetPowerForValidator(msg.Validator)
func (r *round) getMsgItemFromProto(msg *oracletypes.MsgItem) (*MsgItem, error) {
power, found := r.cache.GetPowerForValidator(msg.Validator)
if !found {
return nil, fmt.Errorf("failed to get power for validator:%s", msg.Validator)
}
priceSources := make([]*priceSource, 0, len(msg.PSources))
for _, ps := range msg.PSources {
priceSources = append(priceSources, getPriceSourceFromProto(ps, r.cache))
Expand All @@ -77,7 +80,7 @@ func (r *round) getMsgItemFromProto(msg *oracletypes.MsgItem) *MsgItem {
Validator: msg.Validator,
Power: power,
PriceSources: priceSources,
}
}, nil
}

func (r *round) ValidQuotingBaseBlock(height int64) bool {
Expand All @@ -92,7 +95,10 @@ func (r *round) Tally(protoMsg *oracletypes.MsgItem) (*PriceResult, *oracletypes
return nil, nil, fmt.Errorf("quoting window is not open, feederID:%d", r.feederID)
}

msg := r.getMsgItemFromProto(protoMsg)
msg, err := r.getMsgItemFromProto(protoMsg)
if err != nil {
return nil, nil, fmt.Errorf("failed to get msgItem from proto, error:%w", err)
}
if !r.IsQuoting() {
// record msg for 'handlQuotingMisBehavior'
err := r.a.RecordMsg(msg)
Expand All @@ -102,7 +108,7 @@ func (r *round) Tally(protoMsg *oracletypes.MsgItem) (*PriceResult, *oracletypes
return nil, nil, fmt.Errorf("failed to record quote for aggregated round, error:%w", err)
}

err := r.a.AddMsg(msg)
err = r.a.AddMsg(msg)
if err != nil {
return nil, nil, fmt.Errorf("failed to add quote for aggregation of feederID:%d, roundID:%d, error:%w", r.feederID, r.roundID, err)
}
Expand Down
12 changes: 0 additions & 12 deletions x/oracle/keeper/feedermanagement/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,6 @@ func (osi *orderedSliceInt64) sort() {
})
}

func (osi orderedSliceInt64) Equal(o orderedSliceInt64) bool {
if len(osi) != len(o) {
return false
}
for idx, v := range osi {
if v != o[idx] {
return false
}
}
return true
}

type FeederManager struct {
fCheckTx *FeederManager
k common.KeeperOracle
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 @@ -556,7 +556,7 @@ func (p Params) IsValidSource(sourceID uint64) bool {
func (p Params) GetTokenFeeder(feederID uint64) *TokenFeeder {
for k, v := range p.TokenFeeders {
// #nosec G115 // index of array is uint
if uint64(k) == feederID {
if k >= 0 && uint64(k) == feederID {
return v
}
}
Expand Down

0 comments on commit 9f25460

Please sign in to comment.