forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrootmultistore_test.go
197 lines (161 loc) · 5.09 KB
/
rootmultistore_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package store
import (
"testing"
"github.com/stretchr/testify/assert"
abci "github.com/tendermint/abci/types"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/merkle"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func TestMultistoreCommitLoad(t *testing.T) {
db := dbm.NewMemDB()
store := newMultiStoreWithMounts(db)
err := store.LoadLatestVersion()
assert.Nil(t, err)
// new store has empty last commit
commitID := CommitID{}
checkStore(t, store, commitID, commitID)
// make sure we can get stores by name
s1 := store.getStoreByName("store1")
assert.NotNil(t, s1)
s3 := store.getStoreByName("store3")
assert.NotNil(t, s3)
s77 := store.getStoreByName("store77")
assert.Nil(t, s77)
// make a few commits and check them
nCommits := int64(3)
for i := int64(0); i < nCommits; i++ {
commitID = store.Commit()
expectedCommitID := getExpectedCommitID(store, i+1)
checkStore(t, store, expectedCommitID, commitID)
}
// Load the latest multistore again and check version
store = newMultiStoreWithMounts(db)
err = store.LoadLatestVersion()
assert.Nil(t, err)
commitID = getExpectedCommitID(store, nCommits)
checkStore(t, store, commitID, commitID)
// commit and check version
commitID = store.Commit()
expectedCommitID := getExpectedCommitID(store, nCommits+1)
checkStore(t, store, expectedCommitID, commitID)
// Load an older multistore and check version
ver := nCommits - 1
store = newMultiStoreWithMounts(db)
err = store.LoadVersion(ver)
assert.Nil(t, err)
commitID = getExpectedCommitID(store, ver)
checkStore(t, store, commitID, commitID)
// XXX: commit this older version
commitID = store.Commit()
expectedCommitID = getExpectedCommitID(store, ver+1)
checkStore(t, store, expectedCommitID, commitID)
// XXX: confirm old commit is overwritten and
// we have rolled back LatestVersion
store = newMultiStoreWithMounts(db)
err = store.LoadLatestVersion()
assert.Nil(t, err)
commitID = getExpectedCommitID(store, ver+1)
checkStore(t, store, commitID, commitID)
}
func TestParsePath(t *testing.T) {
_, _, err := parsePath("foo")
assert.Error(t, err)
store, subpath, err := parsePath("/foo")
assert.NoError(t, err)
assert.Equal(t, store, "foo")
assert.Equal(t, subpath, "")
store, subpath, err = parsePath("/fizz/bang/baz")
assert.NoError(t, err)
assert.Equal(t, store, "fizz")
assert.Equal(t, subpath, "/bang/baz")
substore, subsubpath, err := parsePath(subpath)
assert.NoError(t, err)
assert.Equal(t, substore, "bang")
assert.Equal(t, subsubpath, "/baz")
}
func TestMultiStoreQuery(t *testing.T) {
db := dbm.NewMemDB()
multi := newMultiStoreWithMounts(db)
err := multi.LoadLatestVersion()
assert.Nil(t, err)
k, v := []byte("wind"), []byte("blows")
k2, v2 := []byte("water"), []byte("flows")
// v3 := []byte("is cold")
cid := multi.Commit()
// make sure we can get by name
garbage := multi.getStoreByName("bad-name")
assert.Nil(t, garbage)
// set and commit data in one store
store1 := multi.getStoreByName("store1").(KVStore)
store1.Set(k, v)
// and another
store2 := multi.getStoreByName("store2").(KVStore)
store2.Set(k2, v2)
// commit the multistore
cid = multi.Commit()
ver := cid.Version
// bad path
query := abci.RequestQuery{Path: "/key", Data: k, Height: ver}
qres := multi.Query(query)
assert.Equal(t, uint32(sdk.CodeUnknownRequest), qres.Code)
query.Path = "h897fy32890rf63296r92"
qres = multi.Query(query)
assert.Equal(t, uint32(sdk.CodeUnknownRequest), qres.Code)
// invalid store name
query.Path = "/garbage/key"
qres = multi.Query(query)
assert.Equal(t, uint32(sdk.CodeUnknownRequest), qres.Code)
// valid query with data
query.Path = "/store1/key"
qres = multi.Query(query)
assert.Equal(t, uint32(sdk.CodeOK), qres.Code)
assert.Equal(t, v, qres.Value)
// valid but empty
query.Path = "/store2/key"
query.Prove = true
qres = multi.Query(query)
assert.Equal(t, uint32(sdk.CodeOK), qres.Code)
assert.Nil(t, qres.Value)
// store2 data
query.Data = k2
qres = multi.Query(query)
assert.Equal(t, uint32(sdk.CodeOK), qres.Code)
assert.Equal(t, v2, qres.Value)
}
//-----------------------------------------------------------------------
// utils
func newMultiStoreWithMounts(db dbm.DB) *rootMultiStore {
store := NewCommitMultiStore(db)
store.MountStoreWithDB(
sdk.NewKVStoreKey("store1"), sdk.StoreTypeIAVL, db)
store.MountStoreWithDB(
sdk.NewKVStoreKey("store2"), sdk.StoreTypeIAVL, db)
store.MountStoreWithDB(
sdk.NewKVStoreKey("store3"), sdk.StoreTypeIAVL, db)
return store
}
func checkStore(t *testing.T, store *rootMultiStore, expect, got CommitID) {
assert.Equal(t, expect, got)
assert.Equal(t, expect, store.LastCommitID())
}
func getExpectedCommitID(store *rootMultiStore, ver int64) CommitID {
return CommitID{
Version: ver,
Hash: hashStores(store.stores),
}
}
func hashStores(stores map[StoreKey]CommitStore) []byte {
m := make(map[string]merkle.Hasher, len(stores))
for key, store := range stores {
name := key.Name()
m[name] = storeInfo{
Name: name,
Core: storeCore{
CommitID: store.LastCommitID(),
// StoreType: store.GetStoreType(),
},
}
}
return merkle.SimpleHashFromMap(m)
}