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: list snapshots query (backport #246) #252

Merged
merged 1 commit into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package baseapp

import (
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -758,6 +759,22 @@ func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) abci.Res
Value: []byte(app.version),
}

case "snapshots":
var responseValue []byte

response := app.ListSnapshots(abci.RequestListSnapshots{})

responseValue, err := json.Marshal(response)
if err != nil {
sdkerrors.QueryResult(sdkerrors.Wrap(err, fmt.Sprintf("failed to marshal list snapshots response %v", response)))
}

return abci.ResponseQuery{
Codespace: sdkerrors.RootCodespace,
Height: req.Height,
Value: responseValue,
}

default:
return sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown query: %s", path))
}
Expand Down
28 changes: 23 additions & 5 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package baseapp
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
Expand Down Expand Up @@ -1821,17 +1822,34 @@ func TestListSnapshots(t *testing.T) {
require.NoError(t, err)
defer teardown()

expected := abci.ResponseListSnapshots{Snapshots: []*abci.Snapshot{
{Height: 4, Format: 1, Chunks: 2},
{Height: 2, Format: 1, Chunks: 1},
}}

resp := app.ListSnapshots(abci.RequestListSnapshots{})
for _, s := range resp.Snapshots {
queryResponse := app.Query(abci.RequestQuery{
Path: "/app/snapshots",
})

queryListSnapshotsResp := abci.ResponseListSnapshots{}
err = json.Unmarshal(queryResponse.Value, &queryListSnapshotsResp)
require.NoError(t, err)

for i, s := range resp.Snapshots {
querySnapshot := queryListSnapshotsResp.Snapshots[i]
// we check that the query snapshot and function snapshot are equal
// Then we check that the hash and metadata are not empty. We atm
// do not have a good way to generate the expected value for these.
assert.Equal(t, *s, *querySnapshot)
assert.NotEmpty(t, s.Hash)
assert.NotEmpty(t, s.Metadata)
// Set hash and metadata to nil, so we can check the other snapshot
// fields against expected
s.Hash = nil
s.Metadata = nil
}
assert.Equal(t, abci.ResponseListSnapshots{Snapshots: []*abci.Snapshot{
{Height: 4, Format: 1, Chunks: 2},
{Height: 2, Format: 1, Chunks: 1},
}}, resp)
assert.Equal(t, expected, resp)
}

func TestSnapshotWithPruning(t *testing.T) {
Expand Down