Skip to content

Commit

Permalink
fix header
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Jan 6, 2025
1 parent 32e7d0e commit 0170167
Showing 1 changed file with 11 additions and 43 deletions.
54 changes: 11 additions & 43 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (app *BaseApp) Info(_ *abci.InfoRequest) (*abci.InfoResponse, error) {
lastCommitID := app.cms.LastCommitID()
appVersion := InitialAppVersion
if lastCommitID.Version > 0 {
ctx, err := app.CreateQueryContextWithCheckHeader(lastCommitID.Version, false, false)
ctx, err := app.CreateQueryContext(lastCommitID.Version, false)
if err != nil {
return nil, fmt.Errorf("failed creating query context: %w", err)
}
Expand Down Expand Up @@ -1299,12 +1299,6 @@ func checkNegativeHeight(height int64) error {
// CreateQueryContext creates a new sdk.Context for a query, taking as args
// the block height and whether the query needs a proof or not.
func (app *BaseApp) CreateQueryContext(height int64, prove bool) (sdk.Context, error) {
return app.CreateQueryContextWithCheckHeader(height, prove, true)
}

// CreateQueryContextWithCheckHeader creates a new sdk.Context for a query, taking as args
// the block height, whether the query needs a proof or not, and whether to check the header or not.
func (app *BaseApp) CreateQueryContextWithCheckHeader(height int64, prove, checkHeader bool) (sdk.Context, error) {
if err := checkNegativeHeight(height); err != nil {
return sdk.Context{}, err
}
Expand All @@ -1328,46 +1322,19 @@ func (app *BaseApp) CreateQueryContextWithCheckHeader(height int64, prove, check
)
}

if height > 0 && height <= 1 && prove {
// when a client did not provide a query height, manually inject the latest
if height == 0 {
height = lastBlockHeight
}

if height <= 1 && prove {
return sdk.Context{},
errorsmod.Wrap(
sdkerrors.ErrInvalidRequest,
"cannot query with proof when height <= 1; please provide a valid height",
)
}

var header *cmtproto.Header
isLatest := height == 0
for _, state := range []*state{
app.checkState,
app.finalizeBlockState,
} {
if state != nil {
// branch the commit multi-store for safety
h := state.Context().BlockHeader()
if isLatest {
lastBlockHeight = qms.LatestVersion()
}
if !checkHeader || !isLatest || isLatest && h.Height == lastBlockHeight {
header = &h
break
}
}
}

if header == nil {
return sdk.Context{},
errorsmod.Wrapf(
sdkerrors.ErrInvalidHeight,
"header height in all state context is not latest height (%d)", lastBlockHeight,
)
}

// when a client did not provide a query height, manually inject the latest
if isLatest {
height = lastBlockHeight
}

cacheMS, err := qms.CacheMultiStoreWithVersion(height)
if err != nil {
return sdk.Context{},
Expand All @@ -1378,6 +1345,7 @@ func (app *BaseApp) CreateQueryContextWithCheckHeader(height int64, prove, check
}

// branch the commit multi-store for safety
header := app.checkState.Context().BlockHeader()
ctx := sdk.NewContext(cacheMS, true, app.logger).
WithMinGasPrices(app.minGasPrices).
WithGasMeter(storetypes.NewGasMeter(app.queryGasLimit)).
Expand All @@ -1386,15 +1354,15 @@ func (app *BaseApp) CreateQueryContextWithCheckHeader(height int64, prove, check
Height: height,
Time: header.Time,
}).
WithBlockHeader(*header).
WithBlockHeader(header).
WithBlockHeight(height)

if !isLatest {
if height != lastBlockHeight {
rms, ok := app.cms.(*rootmulti.Store)
if ok {
cInfo, err := rms.GetCommitInfo(height)
if cInfo != nil && err == nil {
ctx = ctx.WithHeaderInfo(coreheader.Info{Height: height, Time: cInfo.Timestamp})
ctx = ctx.WithHeaderInfo(coreheader.Info{ChainID: app.chainID, Height: height, Time: cInfo.Timestamp})
}
}
}
Expand Down

0 comments on commit 0170167

Please sign in to comment.