refactor!: reimplement PreFinalizeBlockHook as PreBlocker (#17713)
Co-authored-by: Julien Robert <julien@rbrt.fr> Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
This commit is contained in:
co-authored by
Julien Robert
Aleksandr Bezobchuk
parent
b0664917bb
commit
f99a6242a9
@@ -293,7 +293,7 @@ decision based on the vote extensions.
|
||||
|
||||
In certain contexts, it may be useful or necessary for applications to persist
|
||||
data derived from vote extensions. In order to facilitate this use case, we propose
|
||||
to allow app developers to define a pre-FinalizeBlock hook which will be called
|
||||
to allow app developers to define a pre-Blocker hook which will be called
|
||||
at the very beginning of `FinalizeBlock`, i.e. before `BeginBlock` (see below).
|
||||
|
||||
Note, we cannot allow applications to directly write to the application state
|
||||
@@ -301,7 +301,7 @@ during `ProcessProposal` because during replay, CometBFT will NOT call `ProcessP
|
||||
which would result in an incomplete state view.
|
||||
|
||||
```go
|
||||
func (a MyApp) PreFinalizeBlockHook(ctx sdk.Context, req.RequestFinalizeBlock) error {
|
||||
func (a MyApp) PreBlocker(ctx sdk.Context, req *abci.RequestFinalizeBlock) error {
|
||||
voteExts := GetVoteExtensions(ctx, req.Txs)
|
||||
|
||||
// Process and perform some compute on vote extensions, storing any resulting
|
||||
@@ -353,13 +353,17 @@ we can come up with new types and names altogether.
|
||||
func (app *BaseApp) FinalizeBlock(req abci.RequestFinalizeBlock) (*abci.ResponseFinalizeBlock, error) {
|
||||
ctx := ...
|
||||
|
||||
if app.preFinalizeBlockHook != nil {
|
||||
if err := app.preFinalizeBlockHook(ctx, req); err != nil {
|
||||
if app.preBlocker != nil {
|
||||
ctx := app.finalizeBlockState.ctx
|
||||
rsp, err := app.preBlocker(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if rsp.ConsensusParamsChanged {
|
||||
app.finalizeBlockState.ctx = ctx.WithConsensusParams(app.GetConsensusParams(ctx))
|
||||
}
|
||||
}
|
||||
|
||||
beginBlockResp := app.beginBlock(ctx, req)
|
||||
beginBlockResp, err := app.beginBlock(req)
|
||||
appendBlockEventAttr(beginBlockResp.Events, "begin_block")
|
||||
|
||||
txExecResults := make([]abci.ExecTxResult, 0, len(req.Txs))
|
||||
@@ -368,7 +372,7 @@ func (app *BaseApp) FinalizeBlock(req abci.RequestFinalizeBlock) (*abci.Response
|
||||
txExecResults = append(txExecResults, result)
|
||||
}
|
||||
|
||||
endBlockResp := app.endBlock(ctx, req)
|
||||
endBlockResp, err := app.endBlock(app.finalizeBlockState.ctx)
|
||||
appendBlockEventAttr(beginBlockResp.Events, "end_block")
|
||||
|
||||
return abci.ResponseFinalizeBlock{
|
||||
|
||||
@@ -58,3 +58,4 @@ The new ctx must be passed to all the other lifecycle methods.
|
||||
* [1] https://github.com/cosmos/cosmos-sdk/issues/16494
|
||||
* [2] https://github.com/cosmos/cosmos-sdk/pull/16583
|
||||
* [3] https://github.com/cosmos/cosmos-sdk/pull/17421
|
||||
* [4] https://github.com/cosmos/cosmos-sdk/pull/17713
|
||||
|
||||
+1
-1
@@ -60,7 +60,7 @@ In addition to basic module wiring, setup the upgrade Keeper for the app and the
|
||||
keeper's PreBlocker method:
|
||||
|
||||
```go
|
||||
func (app *myApp) PreBlocker(ctx sdk.Context, req req.RequestFinalizeBlock) (sdk.ResponsePreBlock, error) {
|
||||
func (app *myApp) PreBlocker(ctx sdk.Context, req req.RequestFinalizeBlock) (*sdk.ResponsePreBlock, error) {
|
||||
// For demonstration sake, the app PreBlocker only returns the upgrade module pre-blocker.
|
||||
// In a real app, the module manager should call all pre-blockers
|
||||
// return return app.ModuleManager.PreBlock(ctx, req)
|
||||
|
||||
+1
-1
@@ -77,7 +77,7 @@ will be available to the application during the subsequent `FinalizeBlock` call.
|
||||
An example of how a pre-FinalizeBlock hook could look like is shown below:
|
||||
|
||||
```go
|
||||
app.SetPreFinalizeBlockHook(func(ctx sdk.Context, req *abci.RequestFinalizeBlock) error {
|
||||
app.SetPreBlocker(func(ctx sdk.Context, req *abci.RequestFinalizeBlock) error {
|
||||
allVEs := []VE{} // store all parsed vote extensions here
|
||||
for _, tx := range req.Txs {
|
||||
// define a custom function that tries to parse the tx as a vote extension
|
||||
|
||||
Reference in New Issue
Block a user