Skip to content

Commit

Permalink
change to store historical info for all non-empty blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Nov 22, 2023
1 parent b0b1ebe commit 875d540
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 15 deletions.
66 changes: 66 additions & 0 deletions x/opchild/keeper/abci_listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package keeper

import (
"context"
"sync"

abci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/cosmos-sdk/baseapp"
store "github.com/cosmos/cosmos-sdk/store/types"
)

var _ baseapp.StreamingService = &ABCIListener{}

// ABCIListener is the abci listener to check whether current block is empty or not.
type ABCIListener struct {
emptyBlock bool
*Keeper
}

func newABCIListener(k *Keeper) ABCIListener {
return ABCIListener{emptyBlock: true, Keeper: k}
}

// ListenDeliverTx updates the steaming service with the latest DeliverTx messages
func (listener *ABCIListener) ListenDeliverTx(ctx context.Context, _ abci.RequestDeliverTx, _ abci.ResponseDeliverTx) error {
listener.emptyBlock = false

return nil
}

// Stream is the streaming service loop, awaits kv pairs and writes them to some destination stream or file
func (listener *ABCIListener) Stream(wg *sync.WaitGroup) error { return nil }

// Listeners returns the streaming service's listeners for the BaseApp to register
func (listener *ABCIListener) Listeners() map[store.StoreKey][]store.WriteListener { return nil }

// ListenBeginBlock updates the streaming service with the latest BeginBlock messages
func (listener *ABCIListener) ListenBeginBlock(ctx context.Context, req abci.RequestBeginBlock, res abci.ResponseBeginBlock) error {
listener.emptyBlock = true

return nil
}

// ListenEndBlock updates the steaming service with the latest EndBlock messages
func (listener *ABCIListener) ListenEndBlock(ctx context.Context, req abci.RequestEndBlock, res abci.ResponseEndBlock) error {

// if a block is empty, then remove historical info.
sdkCtx := sdk.UnwrapSDKContext(ctx)
if listener.emptyBlock && sdkCtx.BlockHeight() != 1 {
listener.DeleteHistoricalInfo(sdkCtx, sdkCtx.BlockHeight())
}

return nil
}

// ListenCommit updates the steaming service with the latest Commit event
func (listener *ABCIListener) ListenCommit(ctx context.Context, res abci.ResponseCommit) error {
return nil
}

// Closer is the interface that wraps the basic Close method.
func (listener *ABCIListener) Close() error {
return nil
}
31 changes: 22 additions & 9 deletions x/opchild/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ type Keeper struct {
bridgeHook types.BridgeHook

// Msg server router
router *baseapp.MsgServiceRouter
router *baseapp.MsgServiceRouter
abciListener *ABCIListener

// Legacy Proposal router
legacyRouter govv1beta1.Router
Expand All @@ -44,15 +45,22 @@ func NewKeeper(
panic("authority is not a valid acc address")
}

return Keeper{
cdc: cdc,
storeKey: key,
authKeeper: ak,
bankKeeper: bk,
bridgeHook: bh,
router: router,
authority: authority,
var abciListener *ABCIListener
k := Keeper{
cdc: cdc,
storeKey: key,
authKeeper: ak,
bankKeeper: bk,
bridgeHook: bh,
router: router,
authority: authority,
abciListener: abciListener,
}

_abciListener := newABCIListener(&k)
*abciListener = _abciListener

return k
}

// GetAuthority returns the x/move module's authority.
Expand All @@ -65,6 +73,11 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}

// GetABCIListener return ABCIListener pointer
func (k Keeper) GetABCIListener() *ABCIListener {
return k.abciListener
}

// Router returns the gov keeper's router
func (keeper Keeper) Router() *baseapp.MsgServiceRouter {
return keeper.router
Expand Down
6 changes: 0 additions & 6 deletions x/opchild/keeper/val_state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ func (k Keeper) BlockValidatorUpdates(ctx sdk.Context) []abci.ValidatorUpdate {
panic(err)
}

// if there is no validator updates,
// delete tracking info to prevent empty block creation.
if len(updates) == 0 {
k.DeleteHistoricalInfo(ctx, ctx.BlockHeight())
}

return updates
}

Expand Down

0 comments on commit 875d540

Please sign in to comment.