Skip to content

Commit

Permalink
Merge pull request #111 from agoric-labs/mfig-prevent-future-queries
Browse files Browse the repository at this point in the history
fix(baseapp): prevent queries for future (or in-progress) blocks
  • Loading branch information
michaelfig authored Sep 8, 2021
2 parents 7073baa + c960e7e commit 36d3828
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
25 changes: 22 additions & 3 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,18 @@ func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
}()

// when a client did not provide a query height, manually inject the latest
lastHeight := app.LastBlockHeight()
if req.Height == 0 {
req.Height = app.LastBlockHeight()
req.Height = lastHeight
}
if req.Height > lastHeight {
return sdkerrors.QueryResult(
sdkerrors.Wrapf(
sdkerrors.ErrInvalidHeight,
"given height %d is greater than latest height %d",
req.Height, lastHeight,
),
)
}

// handle gRPC routes first rather than calling splitPath because '/' characters
Expand All @@ -428,7 +438,7 @@ func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) {

path := splitPath(req.Path)
if len(path) == 0 {
sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "no query path provided"))
return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "no query path provided"))
}

switch path[0] {
Expand Down Expand Up @@ -628,8 +638,17 @@ func (app *BaseApp) createQueryContext(height int64, prove bool) (sdk.Context, e
}

// when a client did not provide a query height, manually inject the latest
lastHeight := app.LastBlockHeight()
if height == 0 {
height = app.LastBlockHeight()
height = lastHeight
}
if height > lastHeight {
return sdk.Context{}, sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest,
"cannot query with height %d; last height is %d",
height,
lastHeight,
)
}

if height <= 1 && prove {
Expand Down
20 changes: 20 additions & 0 deletions baseapp/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,23 @@ func TestBaseAppCreateQueryContextRejectsNegativeHeights(t *testing.T) {
})
}
}

func TestBaseAppCreateQueryContextRejectsFutureHeights(t *testing.T) {
t.Parallel()

logger := defaultLogger()
db := dbm.NewMemDB()
name := t.Name()
app := NewBaseApp(name, logger, db, nil)

proves := []bool{
false, true,
}
for _, prove := range proves {
t.Run(fmt.Sprintf("prove=%t", prove), func(t *testing.T) {
sctx, err := app.createQueryContext(30, true)
require.Error(t, err)
require.Equal(t, sctx, sdk.Context{})
})
}
}

0 comments on commit 36d3828

Please sign in to comment.