From a239c68dd4320663c5cd9d7f5c54641a001bbe98 Mon Sep 17 00:00:00 2001 From: beer-1 <147697694+beer-1@users.noreply.github.com> Date: Wed, 22 Nov 2023 17:15:34 +0900 Subject: [PATCH] change to store historical info for all non-empty blocks --- x/opchild/keeper/abci_listener.go | 66 ++++++++++++++++++++++++++++ x/opchild/keeper/keeper.go | 31 +++++++++---- x/opchild/keeper/val_state_change.go | 6 --- 3 files changed, 88 insertions(+), 15 deletions(-) create mode 100644 x/opchild/keeper/abci_listener.go diff --git a/x/opchild/keeper/abci_listener.go b/x/opchild/keeper/abci_listener.go new file mode 100644 index 00000000..35a75f21 --- /dev/null +++ b/x/opchild/keeper/abci_listener.go @@ -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 +} diff --git a/x/opchild/keeper/keeper.go b/x/opchild/keeper/keeper.go index fc600036..9a8fb01e 100644 --- a/x/opchild/keeper/keeper.go +++ b/x/opchild/keeper/keeper.go @@ -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 @@ -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, + 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. @@ -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 diff --git a/x/opchild/keeper/val_state_change.go b/x/opchild/keeper/val_state_change.go index 62900af5..1f733150 100644 --- a/x/opchild/keeper/val_state_change.go +++ b/x/opchild/keeper/val_state_change.go @@ -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 }