Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): in-place testnet creator #19280

Merged
merged 16 commits into from
Feb 12, 2024
Prev Previous commit
Next Next commit
fix halt height edge case
  • Loading branch information
czarcas7ic committed Feb 9, 2024
commit 7d0e17e955014867ad6dc3450429f28c59aca44b
53 changes: 41 additions & 12 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,18 +765,53 @@
return nil, err
}

// There are times when a user stops their node between commits, resulting in a mismatch between the
// blockStore and state. For convenience, we just discard the uncommitted blockStore block and operate on
// the lastBlockHeight in state.
if blockStore.Height() != state.LastBlockHeight {
ctx.Viper.Set(KeyNewValAddr, validatorAddress)
ctx.Viper.Set(KeyUserPubKey, userPubKey)
testnetApp := testnetAppCreator(ctx.Logger, db, traceWriter, ctx.Viper)

// We need to create a temporary proxyApp to get the initial state of the application.
// Depending on how the node was stopped, the application height can differ from the blockStore height.
// This height difference changes how we go about modifying the state.
cmtApp := NewCometABCIWrapper(testnetApp)
_, context := getCtx(ctx, true)
clientCreator := proxy.NewLocalClientCreator(cmtApp)
metrics := node.DefaultMetricsProvider(config.Instrumentation)
_, _, _, _, proxyMetrics, _, _ := metrics(genDoc.ChainID) //nolint:dogsled

Check failure on line 779 in server/start.go

View workflow job for this annotation

GitHub Actions / Analyze

directive `//nolint:dogsled` is unused for linter "dogsled" (nolintlint)

Check failure on line 779 in server/start.go

View workflow job for this annotation

GitHub Actions / golangci-lint

directive `//nolint:dogsled` is unused for linter "dogsled" (nolintlint)
proxyApp := proxy.NewAppConns(clientCreator, proxyMetrics)
if err := proxyApp.Start(); err != nil {
return nil, fmt.Errorf("error starting proxy app connections: %v", err)
}
res, err := proxyApp.Query().Info(context, proxy.RequestInfo)
if err != nil {
return nil, fmt.Errorf("error calling Info: %v", err)
}
err = proxyApp.Stop()
if err != nil {
return nil, err
}
appHash := res.LastBlockAppHash
appHeight := res.LastBlockHeight

var block *cmttypes.Block
switch {
case appHeight == blockStore.Height():
// This state occurs when we stop the node with the halt height flag, and need to handle differently
state.LastBlockHeight++
block = blockStore.LoadBlock(blockStore.Height())
block.AppHash = appHash
state.AppHash = appHash
case blockStore.Height() > state.LastBlockHeight:
// This state occurs when we kill the node
err = blockStore.DeleteLatestBlock()
if err != nil {
return nil, err
}
block = blockStore.LoadBlock(blockStore.Height())
default:
// If there is any other state, we just load the block
block = blockStore.LoadBlock(blockStore.Height())
}

block := blockStore.LoadBlock(blockStore.Height())

block.ChainID = newChainID
state.ChainID = newChainID

Expand All @@ -789,7 +824,7 @@
Height: state.LastBlockHeight,
Round: 0,
BlockID: state.LastBlockID,
Timestamp: time.Now(),

Check warning

Code scanning / CodeQL

Calling the system time Warning

Calling the system time may be a possible source of non-determinism
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine, this is the only validator in the set, set setting the timestamp to local time is a non issue.

ValidatorAddress: validatorAddress,
ValidatorIndex: 0,
Signature: []byte{},
Expand Down Expand Up @@ -884,12 +919,6 @@
return nil, err
}

// testnetAppCreator makes any application side changes that must be made due to the above modifications.
// Also, it makes any optional application side changes to make running the testnet easier (voting times, fund accounts, etc).
ctx.Viper.Set(KeyNewValAddr, validatorAddress)
ctx.Viper.Set(KeyUserPubKey, userPubKey)
testnetApp := testnetAppCreator(ctx.Logger, db, traceWriter, ctx.Viper)

return testnetApp, err
}

Expand Down
Loading