Skip to content

Commit 2bb9f46

Browse files
mergify[bot]p0mvn
andauthored
feat: list snapshots query (#246) (#252)
(cherry picked from commit f69c198) Co-authored-by: Roman <roman@osmosis.team>
1 parent 830f277 commit 2bb9f46

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

baseapp/abci.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package baseapp
22

33
import (
44
"crypto/sha256"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"os"
@@ -758,6 +759,22 @@ func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) abci.Res
758759
Value: []byte(app.version),
759760
}
760761

762+
case "snapshots":
763+
var responseValue []byte
764+
765+
response := app.ListSnapshots(abci.RequestListSnapshots{})
766+
767+
responseValue, err := json.Marshal(response)
768+
if err != nil {
769+
sdkerrors.QueryResult(sdkerrors.Wrap(err, fmt.Sprintf("failed to marshal list snapshots response %v", response)))
770+
}
771+
772+
return abci.ResponseQuery{
773+
Codespace: sdkerrors.RootCodespace,
774+
Height: req.Height,
775+
Value: responseValue,
776+
}
777+
761778
default:
762779
return sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown query: %s", path))
763780
}

baseapp/baseapp_test.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package baseapp
33
import (
44
"bytes"
55
"encoding/binary"
6+
"encoding/json"
67
"fmt"
78
"io/ioutil"
89
"math/rand"
@@ -1821,17 +1822,34 @@ func TestListSnapshots(t *testing.T) {
18211822
require.NoError(t, err)
18221823
defer teardown()
18231824

1825+
expected := abci.ResponseListSnapshots{Snapshots: []*abci.Snapshot{
1826+
{Height: 4, Format: 1, Chunks: 2},
1827+
{Height: 2, Format: 1, Chunks: 1},
1828+
}}
1829+
18241830
resp := app.ListSnapshots(abci.RequestListSnapshots{})
1825-
for _, s := range resp.Snapshots {
1831+
queryResponse := app.Query(abci.RequestQuery{
1832+
Path: "/app/snapshots",
1833+
})
1834+
1835+
queryListSnapshotsResp := abci.ResponseListSnapshots{}
1836+
err = json.Unmarshal(queryResponse.Value, &queryListSnapshotsResp)
1837+
require.NoError(t, err)
1838+
1839+
for i, s := range resp.Snapshots {
1840+
querySnapshot := queryListSnapshotsResp.Snapshots[i]
1841+
// we check that the query snapshot and function snapshot are equal
1842+
// Then we check that the hash and metadata are not empty. We atm
1843+
// do not have a good way to generate the expected value for these.
1844+
assert.Equal(t, *s, *querySnapshot)
18261845
assert.NotEmpty(t, s.Hash)
18271846
assert.NotEmpty(t, s.Metadata)
1847+
// Set hash and metadata to nil, so we can check the other snapshot
1848+
// fields against expected
18281849
s.Hash = nil
18291850
s.Metadata = nil
18301851
}
1831-
assert.Equal(t, abci.ResponseListSnapshots{Snapshots: []*abci.Snapshot{
1832-
{Height: 4, Format: 1, Chunks: 2},
1833-
{Height: 2, Format: 1, Chunks: 1},
1834-
}}, resp)
1852+
assert.Equal(t, expected, resp)
18351853
}
18361854

18371855
func TestSnapshotWithPruning(t *testing.T) {

0 commit comments

Comments
 (0)