Skip to content

Commit

Permalink
abci app fatalerror type
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow committed Aug 15, 2023
1 parent e96fdc0 commit 61ddf18
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
12 changes: 11 additions & 1 deletion internal/app/kwild/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/kwilteam/kwil-db/internal/app/kwild/config"
"github.com/kwilteam/kwil-db/pkg/abci"
"github.com/kwilteam/kwil-db/pkg/grpc/gateway"
grpc "github.com/kwilteam/kwil-db/pkg/grpc/server"
"github.com/kwilteam/kwil-db/pkg/log"
Expand Down Expand Up @@ -35,7 +36,14 @@ type Server struct {
func (s *Server) Start(ctx context.Context) error {
defer func() {
if err := recover(); err != nil {
s.log.Error("kwild server panic", zap.Any("error", err))
switch et := err.(type) {
case abci.FatalError:
s.log.Error("Blockchain application hit an unrecoverable error:\n\n%v",
zap.Stringer("error", et))
// cometbft *may* already recover panics from the application. Investigate.
default:
s.log.Error("kwild server panic", zap.Any("error", err))
}
}
}()

Expand Down Expand Up @@ -103,6 +111,8 @@ func (s *Server) Start(ctx context.Context) error {
return nil
}

// Stop begins shutting down the Server. However, the caller of Start will
// normally cancel the provided context and wait for it to return.
func (s *Server) Stop() {
s.log.Warn("stop kwild services")
s.cancelCtxFunc()
Expand Down
26 changes: 24 additions & 2 deletions pkg/abci/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@ import (
"go.uber.org/zap"
)

// FatalError is a type that can be used in an explicit panic so that the nature
// of the failure may bubble up through the cometbft Node to the top level
// kwil server type.
type FatalError struct {
AppMethod string
Request fmt.Stringer // entire request for debugging
Message string
}

func (fe FatalError) String() string {
return fmt.Sprintf("Application Method: %s\nError: %s\nRequest (%T): %v",
fe.AppMethod, fe.Message, fe.Request, fe.Request)
}

func newFatalError(method string, request fmt.Stringer, message string) FatalError {
return FatalError{
AppMethod: method,
Request: request,
Message: message,
}
}

type appState struct { // TODO
prevBlockHeight int64
prevAppHash []byte
Expand Down Expand Up @@ -90,7 +112,7 @@ func (a *AbciApp) Info(p0 abciTypes.RequestInfo) abciTypes.ResponseInfo {
// Load the current validator set from our store.
vals, err := a.validators.CurrentSet(context.Background())
if err != nil { // TODO error return
panic(fmt.Sprintf("failed to load current validators: %v", err))
panic(newFatalError("Info", &p0, fmt.Sprintf("failed to load current validators: %v", err)))
}
// NOTE: We can check against cometbft/rpc/core.Validators(), but that only
// works with an *in-process* node and after the node is started.
Expand All @@ -100,7 +122,7 @@ func (a *AbciApp) Info(p0 abciTypes.RequestInfo) abciTypes.ResponseInfo {
for _, vi := range vals {
addr, err := pubkeyToAddr(vi.PubKey)
if err != nil {
panic(fmt.Sprintf("invalid validator pubkey: %v", err))
panic(newFatalError("Info", &p0, fmt.Sprintf("invalid validator pubkey: %v", err)))
}
a.valAddrToKey[addr] = vi.PubKey
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/abci/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ type ValidatorModule interface {
CurrentSet(ctx context.Context) ([]*validators.Validator, error)

// Punish may be used at the start of block processing when byzantine
// validators are listed by the consensus client.
Punish(ctx context.Context, validator []byte, dPower int64) error
// validators are listed by the consensus client (no transaction).
Punish(ctx context.Context, validator []byte, power int64) error

// Join creates a join request for a prospective validator.
Join(ctx context.Context, joiner []byte, power int64, tx *transactions.Transaction) (*transactions.TransactionStatus, error)
Expand Down

0 comments on commit 61ddf18

Please sign in to comment.