Skip to content

Commit

Permalink
fix: skip cache commit when refill cache on node restart (#52)
Browse files Browse the repository at this point in the history
* fix(oracle):skip cache commit when refill cache on node restart

* text: add comments

* fix(oracle):set recentParams when commit cached params

* typo(oracle):lint

* fix:CVE-2024-3817, update getter to 1.7.4

---------

Co-authored-by: cloud8little <[email protected]>
  • Loading branch information
leonz789 and cloud8little authored May 11, 2024
1 parent a0de4a8 commit 7e9683d
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 8 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ replace (
github.com/evmos/evmos/v14 => github.com/ExocoreNetwork/evmos/v14 v14.1.1-0.20240408040728-a6f685cfebb9
// Security Advisory https://github.com/advisories/GHSA-h395-qcrw-5vmq
github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.9.1

github.com/hashicorp/go-getter v1.7.1 => github.com/hashicorp/go-getter v1.7.4 // indirect
// replace broken goleveldb
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
//fix cosmos-sdk error
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1090,8 +1090,6 @@ github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtng
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-getter v1.7.1 h1:SWiSWN/42qdpR0MdhaOc/bLR48PLuP1ZQtYLRlM69uY=
github.com/hashicorp/go-getter v1.7.1/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744=
github.com/hashicorp/go-getter v1.7.4 h1:3yQjWuxICvSpYwqSayAdKRFcvBl1y/vogCxczWSmix0=
github.com/hashicorp/go-getter v1.7.4/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744=
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
Expand Down
19 changes: 16 additions & 3 deletions x/oracle/keeper/cache/caches.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,28 @@ func (c *cacheParams) add(p *common.Params) {
func (c *cacheParams) commit(ctx sdk.Context, k common.KeeperOracle) {
block := uint64(ctx.BlockHeight())
index, _ := k.GetIndexRecentParams(ctx)
for i, b := range index.Index {
i := 0
for ; i < len(index.Index); i++ {
b := index.Index[i]
if b >= block-common.MaxNonce {
index.Index = index.Index[i:]
break
}
k.RemoveRecentParams(ctx, b)
}
index.Index = index.Index[i:]
// remove and append for KVStore
k.SetIndexRecentParams(ctx, index)
index.Index = append(index.Index, block)
k.SetIndexRecentParams(ctx, index)

p := types.Params(*c.params)
k.SetRecentParams(ctx, types.RecentParams{
Block: block,
Params: &p,
})
}

// memory cache
// func (c *Cache) AddCache(i any, k common.KeeperOracle) {
func (c *Cache) AddCache(i any) {
switch item := i.(type) {
case *ItemM:
Expand Down Expand Up @@ -190,6 +197,12 @@ func (c *Cache) GetCache(i any) bool {
return true
}

// SkipCommit skip real commit by setting the updage flag to false
func (c *Cache) SkipCommit() {
c.validators.update = false
c.params.update = false
}

func (c *Cache) CommitCache(ctx sdk.Context, reset bool, k common.KeeperOracle) {
if len(c.msg) > 0 {
c.msg.commit(ctx, k)
Expand Down
1 change: 1 addition & 0 deletions x/oracle/keeper/recent_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (k Keeper) GetAllRecentMsgAsMap(ctx sdk.Context) (result map[int64][]*types
iterator := sdk.KVStorePrefixIterator(store, []byte{})

defer iterator.Close()
result = make(map[int64][]*types.MsgItem)

for ; iterator.Valid(); iterator.Next() {
var val types.RecentMsg
Expand Down
2 changes: 2 additions & 0 deletions x/oracle/keeper/recent_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func (k Keeper) GetAllRecentParamsAsMap(ctx sdk.Context) (result map[int64]*type

defer iterator.Close()

result = make(map[int64]*types.Params)

for ; iterator.Valid(); iterator.Next() {
var val types.RecentParams
k.cdc.MustUnmarshal(iterator.Value(), &val)
Expand Down
7 changes: 5 additions & 2 deletions x/oracle/keeper/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func GetAggregatorContext(ctx sdk.Context, k Keeper) *aggregator.AggregatorConte
if ok := recacheAggregatorContext(ctx, agc, k, c); !ok {
// this is the very first time oracle has been started, fill relalted info as initialization
initAggregatorContext(ctx, agc, k, c)
} else {
// this is when a node restart and use the persistent state to refill cache, we don't need to commit these data again
c.SkipCommit()
}
return agc
}
Expand All @@ -45,7 +48,7 @@ func recacheAggregatorContext(ctx sdk.Context, agc *aggregator.AggregatorContext

h, ok := k.GetValidatorUpdateBlock(ctx)
recentParamsMap := k.GetAllRecentParamsAsMap(ctx)
if !ok || recentParamsMap == nil {
if !ok || len(recentParamsMap) == 0 {
// no cache, this is the very first running, so go to initial process instead
return false
}
Expand All @@ -66,7 +69,7 @@ func recacheAggregatorContext(ctx sdk.Context, agc *aggregator.AggregatorContext
agc.SetValidatorPowers(validatorPowers)
// TODO: test only
if k.GetLastTotalPower(ctx).BigInt().Cmp(totalPower) != 0 {
panic("something wrong when get validatorsPower from staking module")
ctx.Logger().Error("something wrong when get validatorsPower from staking module")
}

// reset validators
Expand Down
4 changes: 3 additions & 1 deletion x/oracle/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.Val
validator, _ := am.keeper.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(pubKey))
validatorList[validator.OperatorAddress] = big.NewInt(vu.Power)
}
// update validator set information in cache
cs.AddCache(cache.ItemV(validatorList))
validatorPowers := make(map[string]*big.Int)
cs.GetCache(cache.ItemV(validatorPowers))
// update validatorPowerList in aggregatorContext
Expand Down Expand Up @@ -214,6 +216,6 @@ func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.Val
logger.Info("prepare for next oracle round of each tokenFeeder")
agc.PrepareRound(ctx, 0)

cs.CommitCache(ctx, true, am.keeper)
cs.CommitCache(ctx, false, am.keeper)
return []abci.ValidatorUpdate{}
}

0 comments on commit 7e9683d

Please sign in to comment.