Skip to content

Commit

Permalink
server,cometbft: ensure app height < chain height
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow committed Oct 14, 2024
1 parent e8510e5 commit 9ec4f17
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
27 changes: 17 additions & 10 deletions cmd/kwild/server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,24 @@ var (
// initStores prepares the datastores with an atomic DB transaction. These
// actions are performed outside of ABCI's DB sessions. The stores should not
// keep the db after initialization. Their functions accept a DB connection.
func initStores(d *coreDependencies, db *pg.DB) error {
func initStores(d *coreDependencies, db *pg.DB) (int64, error) {
initTx, err := db.BeginTx(d.ctx)
if err != nil {
return fmt.Errorf("could not start app initialization DB transaction: %w", err)
return 0, fmt.Errorf("could not start app initialization DB transaction: %w", err)
}
defer initTx.Rollback(d.ctx)

// chain meta data for abci and txApp
initChainMetadata(d, initTx)
height := initChainMetadata(d, initTx)

// account store
initAccountRepository(d, initTx)

if err = initTx.Commit(d.ctx); err != nil {
return fmt.Errorf("failed to commit the app initialization DB transaction: %w", err)
return 0, fmt.Errorf("failed to commit the app initialization DB transaction: %w", err)
}

return nil
return height, nil
}

func buildServer(d *coreDependencies, closers *closeFuncs) *Server {
Expand All @@ -100,7 +100,8 @@ func buildServer(d *coreDependencies, closers *closeFuncs) *Server {
// main postgres db
db := buildDB(d, closers)

if err := initStores(d, db); err != nil {
appHeight, err := initStores(d, db)
if err != nil {
failBuild(err, "initStores failed")
}

Expand Down Expand Up @@ -130,7 +131,7 @@ func buildServer(d *coreDependencies, closers *closeFuncs) *Server {
// NOTE: buildCometNode immediately starts talking to the abciApp and
// replaying blocks (and using atomic db tx commits), i.e. calling
// FinalizeBlock+Commit. This is not just a constructor, sadly.
cometBftNode := buildCometNode(d, closers, abciApp)
cometBftNode := buildCometNode(d, closers, abciApp, appHeight)

prometheus.MustRegister(
collectors.NewBuildInfoCollector(),
Expand Down Expand Up @@ -710,11 +711,16 @@ func buildEngine(d *coreDependencies, db *pg.DB) *execution.GlobalContext {
return eng
}

func initChainMetadata(d *coreDependencies, tx sql.Tx) {
func initChainMetadata(d *coreDependencies, tx sql.Tx) int64 {
err := meta.InitializeMetaStore(d.ctx, tx)
if err != nil {
failBuild(err, "failed to initialize chain metadata store")
}
height, _, err := meta.GetChainState(d.ctx, tx)
if err != nil {
failBuild(err, "failed to initialize chain metadata store")
}
return height
}

func initAccountRepository(d *coreDependencies, tx sql.Tx) {
Expand Down Expand Up @@ -1015,7 +1021,8 @@ func buildCometBftClient(cometBftNode *cometbft.CometBftNode) *cmtlocal.Local {
return cmtlocal.New(cometBftNode.Node)
}

func buildCometNode(d *coreDependencies, closer *closeFuncs, abciApp abciTypes.Application) *cometbft.CometBftNode {
func buildCometNode(d *coreDependencies, closer *closeFuncs, abciApp abciTypes.Application,
appHeight int64) *cometbft.CometBftNode {
// for now, I'm just using a KV store for my atomic commit. This probably is not ideal; a file may be better
// I'm simply using this because we know it fsyncs the data to disk
db, err := badger.NewBadgerDB(d.ctx, kwildcfg.SigningDir(d.cfg.RootDir), &badger.Options{
Expand Down Expand Up @@ -1058,7 +1065,7 @@ func buildCometNode(d *coreDependencies, closer *closeFuncs, abciApp abciTypes.A

nodeLogger := increaseLogLevel("cometbft", &d.log, d.cfg.Logging.ConsensusLevel)
node, err := cometbft.NewCometBftNode(d.ctx, abciApp, nodeCfg, genDoc, d.privKey,
readWriter, nodeLogger)
readWriter, nodeLogger, appHeight)
if err != nil {
failBuild(err, "failed to build comet node")
}
Expand Down
19 changes: 17 additions & 2 deletions internal/abci/cometbft/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/kwilteam/kwil-db/core/log"
"github.com/kwilteam/kwil-db/internal/abci/cometbft/privval"

cometDB "github.com/cometbft/cometbft-db"
abciTypes "github.com/cometbft/cometbft/abci/types"
cometConfig "github.com/cometbft/cometbft/config"
cometEd25519 "github.com/cometbft/cometbft/crypto/ed25519"
Expand All @@ -18,6 +19,7 @@ import (
"github.com/cometbft/cometbft/p2p"
"github.com/cometbft/cometbft/proxy"
cometLocal "github.com/cometbft/cometbft/rpc/client/local"
"github.com/cometbft/cometbft/store"
"github.com/cometbft/cometbft/types"

"go.uber.org/zap"
Expand Down Expand Up @@ -171,7 +173,7 @@ WARNING: These files are overwritten on kwild startup.`
// NewCometBftNode creates a new CometBFT node.
func NewCometBftNode(ctx context.Context, app abciTypes.Application, conf *cometConfig.Config,
genDoc *types.GenesisDoc, privateKey cometEd25519.PrivKey, atomicStore privval.AtomicReadWriter,
logger *log.Logger) (*CometBftNode, error) {
logger *log.Logger, appHeight int64) (*CometBftNode, error) {
if err := writeCometBFTConfigs(conf, genDoc); err != nil {
return nil, fmt.Errorf("failed to write the effective cometbft config files: %w", err)
}
Expand All @@ -188,6 +190,19 @@ func NewCometBftNode(ctx context.Context, app abciTypes.Application, conf *comet
return nil, fmt.Errorf("failed to create private validator: %v", err)
}

dbProvider := func(ctx *cometConfig.DBContext) (cometDB.DB, error) {
dbType := cometDB.BackendType(ctx.Config.DBBackend)
db, err := cometDB.NewDB(ctx.ID, dbType, ctx.Config.DBDir())
if err != nil {
return nil, err
}
bss := store.LoadBlockStoreState(db)
if appHeight > bss.Height {
return nil, errors.New("application (postgresql) height is past blockchain database -- drop postgresql to proceed")
}
return db, nil
}

node, err := cometNodes.NewNodeWithContext(
ctx,
conf,
Expand All @@ -197,7 +212,7 @@ func NewCometBftNode(ctx context.Context, app abciTypes.Application, conf *comet
},
proxy.NewConnSyncLocalClientCreator(app), // "connection-synchronized" local client
genesisDocProvider(genDoc),
cometConfig.DefaultDBProvider,
dbProvider,
cometNodes.DefaultMetricsProvider(conf.Instrumentation),
NewLogWrapper(logger),
)
Expand Down

0 comments on commit 9ec4f17

Please sign in to comment.