diff --git a/x/oracle/keeper/aggregator/context.go b/x/oracle/keeper/aggregator/context.go index 6726634f6..73b28c9d4 100644 --- a/x/oracle/keeper/aggregator/context.go +++ b/x/oracle/keeper/aggregator/context.go @@ -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()) @@ -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) diff --git a/x/oracle/keeper/aggregator/context_test.go b/x/oracle/keeper/aggregator/context_test.go index 65f839753..b00c036a2 100644 --- a/x/oracle/keeper/aggregator/context_test.go +++ b/x/oracle/keeper/aggregator/context_test.go @@ -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}) @@ -27,9 +27,9 @@ 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() @@ -37,8 +37,8 @@ func TestAggregatorContext(t *testing.T) { }) 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) @@ -46,9 +46,9 @@ func TestAggregatorContext(t *testing.T) { }) }) - 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() diff --git a/x/oracle/keeper/single.go b/x/oracle/keeper/single.go index dccec0100..1a1f67c4e 100644 --- a/x/oracle/keeper/single.go +++ b/x/oracle/keeper/single.go @@ -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) @@ -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) @@ -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 { @@ -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 } @@ -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() { diff --git a/x/oracle/module.go b/x/oracle/module.go index b6c1b83d8..24d290263 100644 --- a/x/oracle/module.go +++ b/x/oracle/module.go @@ -150,7 +150,15 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw 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) + + 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 { @@ -213,8 +221,6 @@ func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.Val } // 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)