Skip to content

Commit

Permalink
Problem: halt-height is not deterministic (#998)
Browse files Browse the repository at this point in the history
* Problem: halt-height is not deterministic

Solution:
- port the fix from sdk: cosmos/cosmos-sdk#16639

* Update CHANGELOG.md

Signed-off-by: yihuang <huang@crypto.com>

---------

Signed-off-by: yihuang <huang@crypto.com>
  • Loading branch information
yihuang authored Jul 5, 2023
1 parent 681c32b commit f689474
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- [#991](https://github.com/crypto-org-chain/chain-main/pull/991) Update cometbft `v0.34.29` with several minor bug fixes and low-severity security-fixes.
- [#998](https://github.com/crypto-org-chain/chain-main/pull/998) Port halt-height fix from sdk

### Bug Fixes

Expand Down
32 changes: 28 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/server/api"
"github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
Expand Down Expand Up @@ -270,6 +271,10 @@ type ChainApp struct {

// the configurator
configurator module.Configurator

// duplicate the logic here because it's private in sdk
haltHeight uint64
haltTime uint64
}

func init() {
Expand Down Expand Up @@ -334,6 +339,8 @@ func New(
keys: keys,
tkeys: tkeys,
memKeys: memKeys,
haltHeight: cast.ToUint64(appOpts.Get(server.FlagHaltHeight)),
haltTime: cast.ToUint64(appOpts.Get(server.FlagHaltTime)),
}

app.ParamsKeeper = initParamsKeeper(appCodec, legacyAmino, keys[paramstypes.StoreKey], tkeys[paramstypes.TStoreKey])
Expand Down Expand Up @@ -729,6 +736,24 @@ func (app *ChainApp) Name() string { return app.BaseApp.Name() }

// BeginBlocker application updates every begin block
func (app *ChainApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
// backport: https://github.com/cosmos/cosmos-sdk/pull/16639
var halt bool
switch {
case app.haltHeight > 0 && uint64(req.Header.Height) > app.haltHeight:
halt = true

case app.haltTime > 0 && req.Header.Time.Unix() > int64(app.haltTime):
halt = true
}

if halt {
app.Logger().Info("halting node per configuration", "height", app.haltHeight, "time", app.haltTime)
if err := app.Close(); err != nil {
app.Logger().Info("close application failed", "error", err)
}
panic("halt application")
}

return app.mm.BeginBlock(ctx, req)
}

Expand Down Expand Up @@ -937,11 +962,10 @@ func StoreKeys() (

// Close will be called in graceful shutdown in start cmd
func (app *ChainApp) Close() error {
err := app.BaseApp.Close()

var err error
if cms, ok := app.CommitMultiStore().(io.Closer); ok {
return errors.Join(err, cms.Close())
err = cms.Close()
}

return err
return errors.Join(err, app.BaseApp.Close())
}

0 comments on commit f689474

Please sign in to comment.