Skip to content

Commit

Permalink
fix(baseapp): reenable sending signals at halt-height
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Aug 2, 2024
1 parent 3730027 commit 6fa3bd3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG-Agoric.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Bug Fixes

* (baseapp) [#338](https://github.com/agoric-labs/cosmos-sdk/pull/338) Make sure we don't execute blocks beyond the halt height. Restored from [#305](https://github.com/agoric-labs/cosmos-sdk/pull/305) but compatible with older `SIGINT`, `SIGTERM` logic
* (baseapp) [#413](https://github.com/agoric-labs/cosmos-sdk/pull/413) Ignore events from simulated transactions

## `v0.46.16-alpha.agoric.2.1` - 2024-03-08
Expand All @@ -85,7 +86,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

## `v0.46.16-alpha.agoric.1` - 2024-02-05

* Agoric/agoric-sdk#8224 Merge [cosmos/cosmos-sdk v0.46.16](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.16)
* Agoric/agoric-sdk#8224 Merge [cosmos/cosmos-sdk v0.46.16](https://github.com/osmos/cosmos-sdk/releases/tag/v0.46.16)

### Bug Fixes

Expand All @@ -99,7 +100,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Bug Fixes

* (baseapp) [#337](https://github.com/agoric-labs/cosmos-sdk/pull/337) revert #305 which causes test failures in agoric-sdk
* (baseapp) [#337](https://github.com/agoric-labs/cosmos-sdk/pull/337) revert [#305](https://github.com/agoric-labs/cosmos-sdk/pull/305) which causes test failures in agoric-sdk

## `v0.45.16-alpha.agoric.1` - 2023-09-22

Expand Down
44 changes: 35 additions & 9 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"sort"
"strings"
"syscall"
"time"

"github.com/gogo/protobuf/proto"
Expand Down Expand Up @@ -312,21 +314,45 @@ func (app *BaseApp) deliverTxWithoutEventHistory(req abci.RequestDeliverTx) (res
}
}

// checkHalt checkes if height or time exceeds halt-height or halt-time respectively.
func (app *BaseApp) checkHalt(height int64, time time.Time) {
// checkHalt forces a state machine halt and attempts to kill the current
// process if block height or timestamp exceeds halt-height or halt-time
// respectively.
func (app *BaseApp) checkHalt(blockHeight int64, blockTime time.Time) {
var halt bool
switch {
case app.haltHeight > 0 && uint64(height) > app.haltHeight:
if app.haltHeight > 0 && uint64(blockHeight) > app.haltHeight {
// height to halt has passed
halt = true

case app.haltTime > 0 && time.Unix() > int64(app.haltTime):
} else if app.haltTime > 0 && blockTime.Unix() > int64(app.haltTime) {
// time to halt has passed
halt = true
}

if halt {
app.logger.Info("halt per configuration", "height", app.haltHeight, "time", app.haltTime)
panic(errors.New("halt application"))
if !halt {
return
}

app.logger.Info(
"halt per configuration",
"haltHeight", app.haltHeight,
"haltTime", app.haltTime,
"blockHeight", blockHeight,
"blockTime", blockTime,
)

// [AGORIC] Make a best-effort attempt to kill our process.
p, err := os.FindProcess(os.Getpid())
if err == nil {
// attempt cascading signals in case SIGINT fails (os dependent)
_ = p.Signal(syscall.SIGINT)
_ = p.Signal(syscall.SIGTERM)
// Errors in these signal calls are not meaningful to us. We tried our
// best, but we don't care (and can't tell) if or how the signal handler
// responds.
}

// Prevent the state machine from advancing to the next block, no matter how
// the signals were handled.
panic(errors.New("halt application"))
}

// Commit implements the ABCI interface. It will commit all state that exists in
Expand Down
15 changes: 15 additions & 0 deletions baseapp/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package baseapp
import (
"encoding/json"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"testing"
"time"

Expand Down Expand Up @@ -247,17 +250,29 @@ func TestABCI_HaltChain(t *testing.T) {
{"halt-time", 0, 10, 1, 11, true},
}

sigs := make(chan os.Signal, 5)
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.expHalt {
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
}

defer func() {
rec := recover()
signal.Stop(sigs)
var err error
if rec != nil {
err = rec.(error)
}
if !tc.expHalt {
require.NoError(t, err)
} else {
// ensure that we received the correct signals
require.Equal(t, syscall.SIGINT, <-sigs)
require.Equal(t, syscall.SIGTERM, <-sigs)
require.Equal(t, len(sigs), 0)

// Check our error message.
require.Error(t, err)
require.True(t, strings.HasPrefix(err.Error(), "halt application"))
}
Expand Down

0 comments on commit 6fa3bd3

Please sign in to comment.