Skip to content

Commit

Permalink
feat: list snapshots query (#246) (#252)
Browse files Browse the repository at this point in the history
(cherry picked from commit f69c198)

Co-authored-by: Roman <[email protected]>
  • Loading branch information
mergify[bot] and p0mvn authored Jun 7, 2022
1 parent 830f277 commit 2bb9f46
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
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

0 comments on commit 2bb9f46

Please sign in to comment.