Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(oralce):separate prepareRound from endBlock into beginBlock #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions x/oracle/keeper/aggregator/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ func (agc *AggregatorContext) SealRound(ctx sdk.Context, force bool) (success []
return success, failed
}

func (agc *AggregatorContext) PrepareRound(ctx sdk.Context, block uint64) {
// TODO: test to remove PrepareRound into BeginBlock
func (agc *AggregatorContext) PrepareRoundBeginBlock(ctx sdk.Context, block uint64) {
// block>0 means recache initialization, all roundInfo is empty
if block == 0 {
block = uint64(ctx.BlockHeight())
Expand All @@ -229,15 +230,16 @@ func (agc *AggregatorContext) PrepareRound(ctx sdk.Context, block uint64) {
if feederID == 0 {
continue
}
if (feeder.EndBlock > 0 && feeder.EndBlock <= block) || feeder.StartBaseBlock > block {
if (feeder.EndBlock > 0 && feeder.EndBlock < block) || feeder.StartBaseBlock >= block {
// this feeder is inactive
continue
}

delta := block - feeder.StartBaseBlock
baseBlock := block - 1
delta := baseBlock - feeder.StartBaseBlock
left := delta % feeder.Interval
count := delta / feeder.Interval
latestBasedblock := block - left
latestBasedblock := baseBlock - left
latestNextRoundID := feeder.StartRoundID + count

feederIDUint64 := uint64(feederID)
Expand Down
14 changes: 7 additions & 7 deletions x/oracle/keeper/aggregator/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestAggregatorContext(t *testing.T) {
Convey("prepare round to gengerate round info of feeders for next block", func() {
Convey("pepare within the window", func() {
p := patchBlockHeight(12)
agc.PrepareRound(ctx, 0)
agc.PrepareRoundBeginBlock(ctx, 0)

Convey("for empty round list", func() {
So(*agc.rounds[1], ShouldResemble, roundInfo{10, 2, 1})
Expand All @@ -27,28 +27,28 @@ func TestAggregatorContext(t *testing.T) {
Convey("update already exist round info", func() {
p.Reset()
time.Sleep(1 * time.Second)
patchBlockHeight(10 + int64(common.MaxNonce))
patchBlockHeight(10 + int64(common.MaxNonce) + 1)

agc.PrepareRound(ctx, 0)
agc.PrepareRoundBeginBlock(ctx, 0)
So(agc.rounds[1].status, ShouldEqual, 2)
})
p.Reset()
time.Sleep(1 * time.Second)
})
Convey("pepare outside the window", func() {
Convey("for empty round list", func() {
p := patchBlockHeight(10 + int64(common.MaxNonce))
agc.PrepareRound(ctx, 0)
p := patchBlockHeight(10 + int64(common.MaxNonce) + 1)
agc.PrepareRoundBeginBlock(ctx, 0)
So(agc.rounds[1].status, ShouldEqual, 2)
p.Reset()
time.Sleep(1 * time.Second)
})
})
})

Convey("seal existed round without any msg recieved", func() {
Convey("seal existing round without any msg recieved", func() {
p := patchBlockHeight(11)
agc.PrepareRound(ctx, 0)
agc.PrepareRoundBeginBlock(ctx, 0)
Convey("seal when exceed the window", func() {
So(agc.rounds[1].status, ShouldEqual, 1)
p.Reset()
Expand Down
10 changes: 4 additions & 6 deletions x/oracle/keeper/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func GetAggregatorContext(ctx sdk.Context, k Keeper) *aggregator.AggregatorConte
}

func recacheAggregatorContext(ctx sdk.Context, agc *aggregator.AggregatorContext, k Keeper, c *cache.Cache) bool {
from := ctx.BlockHeight() - int64(common.MaxNonce)
from := ctx.BlockHeight() - int64(common.MaxNonce) + 1
to := ctx.BlockHeight() - 1

h, ok := k.GetValidatorUpdateBlock(ctx)
Expand All @@ -71,7 +71,7 @@ func recacheAggregatorContext(ctx sdk.Context, agc *aggregator.AggregatorContext
}

if int64(h.Block) > from {
from = int64(h.Block)
from = int64(h.Block) + 1
}

totalPower := big.NewInt(0)
Expand Down Expand Up @@ -104,7 +104,7 @@ func recacheAggregatorContext(ctx sdk.Context, agc *aggregator.AggregatorContext
}
}

agc.PrepareRound(ctx, uint64(from))
agc.PrepareRoundBeginBlock(ctx, uint64(from))

if msgs := recentMsgs[from+1]; msgs != nil {
for _, msg := range msgs {
Expand Down Expand Up @@ -138,8 +138,6 @@ func recacheAggregatorContext(ctx sdk.Context, agc *aggregator.AggregatorContext
if updated := c.GetCache(cache.ItemP(&pRet)); !updated {
c.AddCache(cache.ItemP(&pTmp))
}
// fill params cache
agc.PrepareRound(ctx, uint64(to))

return true
}
Expand Down Expand Up @@ -168,7 +166,7 @@ func initAggregatorContext(ctx sdk.Context, agc *aggregator.AggregatorContext, k
// set validatorPower cache
c.AddCache(cache.ItemV(validatorPowers))

agc.PrepareRound(ctx, uint64(ctx.BlockHeight()-1))
agc.PrepareRoundBeginBlock(ctx, uint64(ctx.BlockHeight()))
}

func ResetAggregatorContext() {
Expand Down
12 changes: 9 additions & 3 deletions x/oracle/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,15 @@
func (AppModule) ConsensusVersion() uint64 { return 1 }

// BeginBlock contains the logic that is automatically triggered at the beginning of each block
func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
_ = keeper.GetCaches()
agc := keeper.GetAggregatorContext(ctx, am.keeper)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

path flow from Begin/EndBlock to a panic call

logger := am.keeper.Logger(ctx)

logger.Info("prepare for next oracle round of each tokenFeeder")
agc.PrepareRoundBeginBlock(ctx, 0)
}

// EndBlock contains the logic that is automatically triggered at the end of each block
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
Expand Down Expand Up @@ -213,8 +221,6 @@
}
// TODO: emit events for success sealed rounds(could ignore for v1)

logger.Info("prepare for next oracle round of each tokenFeeder")
agc.PrepareRound(ctx, 0)
keeper.ResetAggregatorContextCheckTx()

// TODO: update params happened during this block for cache, for the case: agc is recached from history and cache'params is set with the latest params by recache, but parmas changed during this block as well. or force agc to be GET first before cache be GET(later approch is better)
Expand Down
Loading