Skip to content

Commit

Permalink
debug this madness
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow committed Mar 20, 2024
1 parent b4681f9 commit 20af418
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
5 changes: 0 additions & 5 deletions internal/abci/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,6 @@ func (a *AbciApp) Info(ctx context.Context, _ *abciTypes.RequestInfo) (*abciType

a.log.Info("ABCI application is ready", zap.Int64("height", height))

// CometBFT needs height 0 to signal we need ChainInit.
if height == -1 {
height = 0 // empty DB, we need genesis
}

return &abciTypes.ResponseInfo{
LastBlockHeight: height,
LastBlockAppHash: appHash,
Expand Down
3 changes: 2 additions & 1 deletion internal/abci/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package meta
import (
"context"
"fmt"
"slices"

"github.com/kwilteam/kwil-db/common/sql"
"github.com/kwilteam/kwil-db/internal/sql/versioning"
Expand Down Expand Up @@ -75,7 +76,7 @@ func GetChainState(ctx context.Context, db sql.Executor) (int64, []byte, error)
return 0, nil, fmt.Errorf("expected bytes for apphash, got %T", row[1])
}

return height, appHash, nil
return height, slices.Clone(appHash), nil
}

// SetChainState will update the current height and app hash.
Expand Down
31 changes: 26 additions & 5 deletions internal/txapp/txapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package txapp
import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -158,8 +159,7 @@ func (r *TxApp) GenesisInit(ctx context.Context, validators []*types.Validator,

// ChainInfo is called be the ABCI applications' Info method, which is called
// once on startup except when the node is at genesis, in which case GenesisInit
// is called by the application's ChainInit method. If height is -1, the
// database is empty. The ABCI application should return 0 to cometbft.
// is called by the application's ChainInit method.
func (r *TxApp) ChainInfo(ctx context.Context) (int64, []byte, error) {
tx, err := r.Database.BeginReadTx(ctx)
if err != nil {
Expand All @@ -170,7 +170,18 @@ func (r *TxApp) ChainInfo(ctx context.Context) (int64, []byte, error) {
// MAYBE: return r.height, r.appHash from the exported method and only query
// the DB from an unexported method that c'tor uses. Needs mutex. Hitting DB
// always may also be good to ensure the exported method gets committed.
return getChainState(ctx, tx)

// return getChainState(ctx, tx)
height, appHash, err := getChainState(ctx, tx)
if err != nil {
return 0, nil, err
}
r.log.Info("ChainInfo", log.Int("height", height), log.String("appHash", hex.EncodeToString(appHash)),
log.Int("height_x", r.height), log.String("appHash_x", hex.EncodeToString(r.appHash)))
if height == -1 {
height = 0 // for ChainInfo caller, non-negative is expected for genesis
}
return height, appHash, nil
}

// UpdateValidator updates a validator's power.
Expand Down Expand Up @@ -286,6 +297,12 @@ func (r *TxApp) Finalize(ctx context.Context, blockHeight int64) (appHash []byte
}
}()

r.log.Info("Finalize(start)", log.Int("height", r.height), log.String("appHash", hex.EncodeToString(r.appHash)))

if blockHeight != r.height+1 {
return nil, nil, fmt.Errorf("Finalize was expecting height %d, got %d", r.height+1, blockHeight)
}

err = r.processVotes(ctx, blockHeight)
if err != nil {
return nil, nil, err
Expand All @@ -296,12 +313,12 @@ func (r *TxApp) Finalize(ctx context.Context, blockHeight int64) (appHash []byte
return nil, nil, err
}

// Update to this next height but previous app hash. If we crash before
// Update to this next height but dummy app hash. If we crash before
// Commit can store the next app hash that we get after Precommit, the
// startup handshake's call to Info will detect the mismatch. That requires
// manual recovery, but it at least detects this recorded height rather that
// not recognizing that we have committed the data for this block at all.
err = setChainState(ctx, r.currentTx, blockHeight, r.appHash)
err = setChainState(ctx, r.currentTx, blockHeight, []byte{0x42})
if err != nil {
return nil, nil, err
}
Expand All @@ -314,6 +331,8 @@ func (r *TxApp) Finalize(ctx context.Context, blockHeight int64) (appHash []byte
r.appHash = crypto.Sha256(append(r.appHash, engineHash...))
r.height = blockHeight

r.log.Info("Finalize(end)", log.Int("height", r.height), log.String("appHash", hex.EncodeToString(r.appHash)))

// I'd really like to setChainState here with appHash, but we can't use
// currentTx for anything now except Commit.

Expand Down Expand Up @@ -513,6 +532,8 @@ func (r *TxApp) Commit(ctx context.Context) error {
}
defer r.mempool.reset()

r.log.Info("Commit", log.Int("height", r.height), log.String("appHash", hex.EncodeToString(r.appHash)))

err := r.currentTx.Commit(ctx)
if err != nil {
return err
Expand Down

0 comments on commit 20af418

Please sign in to comment.