-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
fix: ensure height is correct on first block #16259
Changes from 3 commits
3c0cafa
50135af
5cb7c72
637c31d
8ffc8b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,6 +74,16 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC | |
} | ||
} | ||
|
||
defer func() { | ||
// InitChain represents the state of the application BEFORE the first block, | ||
// i.e. the genesis block. This means that when processing the app's InitChain | ||
// handler, the block height is zero by default. However, after Commit is called | ||
// the height needs to reflect the true block height. | ||
initHeader.Height = req.InitialHeight | ||
app.checkState.ctx = app.checkState.ctx.WithBlockHeader(initHeader) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we set this in header info as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Header info? What do you mean by that? |
||
app.deliverState.ctx = app.deliverState.ctx.WithBlockHeader(initHeader) | ||
}() | ||
|
||
if app.initChainer == nil { | ||
return | ||
} | ||
|
@@ -119,8 +129,8 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC | |
appHash = emptyHash[:] | ||
} | ||
|
||
// NOTE: We don't commit, but BeginBlock for block `initial_height` starts from this | ||
// deliverState. | ||
// NOTE: We don't commit, but BeginBlock for block InitialHeight starts from | ||
// this deliverState. | ||
return abci.ResponseInitChain{ | ||
ConsensusParams: res.ConsensusParams, | ||
Validators: res.Validators, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,19 +7,17 @@ import ( | |
"strings" | ||
"testing" | ||
|
||
dbm "github.com/cosmos/cosmos-db" | ||
|
||
abci "github.com/cometbft/cometbft/abci/types" | ||
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" | ||
"github.com/cosmos/gogoproto/jsonpb" | ||
"github.com/stretchr/testify/require" | ||
|
||
errorsmod "cosmossdk.io/errors" | ||
"cosmossdk.io/log" | ||
pruningtypes "cosmossdk.io/store/pruning/types" | ||
"cosmossdk.io/store/snapshots" | ||
snapshottypes "cosmossdk.io/store/snapshots/types" | ||
storetypes "cosmossdk.io/store/types" | ||
abci "github.com/cometbft/cometbft/abci/types" | ||
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" | ||
dbm "github.com/cosmos/cosmos-db" | ||
"github.com/cosmos/gogoproto/jsonpb" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/cosmos-sdk/baseapp" | ||
baseapptestutil "github.com/cosmos/cosmos-sdk/baseapp/testutil" | ||
|
@@ -44,6 +42,21 @@ func TestABCI_Info(t *testing.T) { | |
require.Equal(t, suite.baseApp.AppVersion(), res.AppVersion) | ||
} | ||
|
||
func TestABCI_First_block_Height(t *testing.T) { | ||
suite := NewBaseAppSuite(t, baseapp.SetChainID("test-chain-id")) | ||
app := suite.baseApp | ||
|
||
app.InitChain(abci.RequestInitChain{ | ||
ChainId: "test-chain-id", | ||
ConsensusParams: &cmtproto.ConsensusParams{Block: &cmtproto.BlockParams{MaxGas: 5000000}}, | ||
InitialHeight: 1, | ||
}) | ||
_ = app.Commit() | ||
|
||
ctx := app.GetContextForCheckTx(nil) | ||
require.Equal(t, int64(1), ctx.BlockHeight()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. w/o this fix, this value is actually 0 which would cause |
||
} | ||
|
||
func TestABCI_InitChain(t *testing.T) { | ||
name := t.Name() | ||
db := dbm.NewMemDB() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defer
is usually for resource cleanup, I think it's clearer to simply put the code after the commit call, for example if panic happens before it, there's no point to still run this code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although the tutorial on defer states that, there's no idiomatic usage of it.
Finally is exactly what we're trying to do here. I think if we do it in
Commit
, we'll have to explicitly check if height == 0.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean just after the initChainer call, I think we don't want to run this if there's any errors or panics?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahh yes, the reason why I added in the defer is if
initChainer == nil
, you'll need to add it before the return. That's why I added in the defer instead of having it twice.