Skip to content

Commit

Permalink
fix: fix the last block time is empty issue (#437)
Browse files Browse the repository at this point in the history
Co-authored-by: zjubfd <[email protected]>
  • Loading branch information
forcodedancing and unclezoro authored Aug 24, 2023
1 parent 02e9014 commit ec39d29
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 16 deletions.
3 changes: 0 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,9 +763,6 @@ func (app *App) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.R

// EndBlocker application updates every end block
func (app *App) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
lastBlockTime := app.GetCheckState().Context().BlockHeader().Time.Unix()
ctx = ctx.WithValue(spmodule.LastBlockTimeKey, lastBlockTime)

resp := app.mm.EndBlock(ctx, req)
if app.IsIavlStore() {
bankIavl, _ := app.CommitMultiStore().GetCommitStore(sdk.NewKVStoreKey(banktypes.StoreKey)).(*iavl.Store)
Expand Down
14 changes: 4 additions & 10 deletions x/sp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import (
"github.com/bnb-chain/greenfield/x/sp/types"
)

// LastBlockTimeKey is the key to record last block's time, which will be set by app
const LastBlockTimeKey = "last_block_time"

func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
if ctx.BlockHeight()%types.MaintenanceRecordsGCFrequencyInBlocks == 0 {
k.ForceUpdateMaintenanceRecords(ctx)
Expand All @@ -28,13 +25,10 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
needUpdate = true
}
} else { // update every month
lastBlockTimeUnix := ctx.Value(LastBlockTimeKey).(int64)
if lastBlockTimeUnix != 0 {
lastBlockTime := time.Unix(lastBlockTimeUnix, 0).UTC()
currentBlockTime := ctx.BlockTime().UTC()
if lastBlockTime.Month() != currentBlockTime.Month() {
needUpdate = true
}
lastUpdateTime := time.Unix(price.UpdateTimeSec, 0).UTC()
currentBlockTime := ctx.BlockTime().UTC()
if lastUpdateTime.Month() != currentBlockTime.Month() {
needUpdate = true
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions x/sp/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (s *TestSuite) TestEndBlocker_WithUpdateInterval() {
}

func (s *TestSuite) TestEndBlocker_WithoutUpdateInterval() {
preTime := int64(1691648908)
preTime := int64(1691648908) //UTC Aug 10 2023
s.ctx = s.ctx.WithBlockTime(time.Unix(preTime, 0))
globalPrice := types.GlobalSpStorePrice{
UpdateTimeSec: 0,
Expand Down Expand Up @@ -186,7 +186,6 @@ func (s *TestSuite) TestEndBlocker_WithoutUpdateInterval() {
s.spKeeper.SetSpStoragePrice(s.ctx, spPrice)

// in the same month
s.ctx = s.ctx.WithValue(spmodule.LastBlockTimeKey, newTime+1)
s.ctx = s.ctx.WithBlockTime(time.Unix(newTime+2, 0))
spmodule.EndBlocker(s.ctx, *s.spKeeper)
// global price will not be updated
Expand All @@ -196,7 +195,6 @@ func (s *TestSuite) TestEndBlocker_WithoutUpdateInterval() {
s.Require().Equal(globalPrice.ReadPrice, globalPriceAfter.ReadPrice)

// a new month
s.ctx = s.ctx.WithValue(spmodule.LastBlockTimeKey, newTime+10)
t := time.Unix(newTime+10, 0).UTC()
year, month, _ := t.Date()
location := t.Location()
Expand Down

0 comments on commit ec39d29

Please sign in to comment.