github.com/cosmos/cosmos-sdk@v0.50.10/docs/architecture/adr-068-preblock.md (about)

     1  # ADR 068: Preblock
     2  
     3  ## Changelog
     4  
     5  * Sept 13, 2023: Initial Draft
     6  
     7  ## Status
     8  
     9  DRAFT
    10  
    11  ## Abstract
    12  
    13  Introduce `PreBlock`, which runs before begin blocker other modules, and allows to modify consensus parameters, and the changes are visible to the following state machine logics.
    14  
    15  ## Context
    16  
    17  When upgrading to sdk 0.47, the storage format for consensus parameters changed, but in the migration block, `ctx.ConsensusParams()` is always `nil`, because it fails to load the old format using new code, it's supposed to be migrated by the `x/upgrade` module first, but unfortunately, the migration happens in `BeginBlocker` handler, which runs after the `ctx` is initialized.
    18  When we try to solve this, we find the `x/upgrade` module can't modify the context to make the consensus parameters visible for the other modules, the context is passed by value, and sdk team want to keep it that way, that's good for isolations between modules.
    19  
    20  ## Alternatives
    21  
    22  The first alternative solution introduced a `MigrateModuleManager`, which only includes the `x/upgrade` module right now, and baseapp will run their `BeginBlocker`s before the other modules, and reload context's consensus parameters in between.
    23  
    24  ## Decision
    25  
    26  Suggested this new lifecycle method.
    27  
    28  ### `PreBlocker`
    29  
    30  There are two semantics around the new lifecycle method:
    31  
    32  - It runs before the `BeginBlocker` of all modules
    33  - It can modify consensus parameters in storage, and signal the caller through the return value.
    34  
    35  When it returns `ConsensusParamsChanged=true`, the caller must refresh the consensus parameter in the finalize context:
    36  ```
    37  app.finalizeBlockState.ctx = app.finalizeBlockState.ctx.WithConsensusParams(app.GetConsensusParams())
    38  ```
    39  
    40  The new ctx must be passed to all the other lifecycle methods.
    41  
    42  
    43  ## Consequences
    44  
    45  ### Backwards Compatibility
    46  
    47  ### Positive
    48  
    49  ### Negative
    50  
    51  ### Neutral
    52  
    53  ## Further Discussions
    54  
    55  ## Test Cases
    56  
    57  ## References
    58  * [1] https://github.com/cosmos/cosmos-sdk/issues/16494
    59  * [2] https://github.com/cosmos/cosmos-sdk/pull/16583
    60  * [3] https://github.com/cosmos/cosmos-sdk/pull/17421
    61  * [4] https://github.com/cosmos/cosmos-sdk/pull/17713