-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseMultiStore.go
288 lines (242 loc) · 7.26 KB
/
baseMultiStore.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package store
import (
"fmt"
sdk "github.com/ci123chain/ci123chain/pkg/abci/types"
sdkerrors "github.com/ci123chain/ci123chain/pkg/abci/types/errors"
abci "github.com/tendermint/tendermint/abci/types"
dbm "github.com/tendermint/tm-db"
"io"
)
type baseMultiStore struct {
db dbm.DB
lastCommitID CommitID
pruning sdk.PruningStrategy
storesParams map[StoreKey]storeParams
stores map[StoreKey]CommitStore
keysByName map[string]StoreKey
}
func NewBaseMultiStore(db dbm.DB) *baseMultiStore {
return &baseMultiStore{
db: db,
storesParams: make(map[StoreKey]storeParams),
stores: make(map[StoreKey]CommitStore),
keysByName: make(map[string]StoreKey),
}
}
func(bs *baseMultiStore) SetPruning(pruning sdk.PruningStrategy) {
bs.pruning = pruning
for _, substore := range bs.stores {
substore.SetPruning(pruning)
}
}
func (bs *baseMultiStore) MountStoreWithDB(key StoreKey, typ StoreType, db dbm.DB) {
if key == nil {
panic("MountIAVLStore() types cannot be nil")
}
if _, ok := bs.storesParams[key]; ok {
panic(fmt.Sprintf("rootMultiStore duplicate store types %v", key))
}
if _, ok := bs.keysByName[key.Name()]; ok {
panic(fmt.Sprintf("rootMultiStore duplicate store types name %v", key))
}
bs.storesParams[key] = storeParams{
key: key,
typ: typ,
db: db,
}
bs.keysByName[key.Name()] = key
}
func (bs *baseMultiStore) GetStoreType() StoreType {
return sdk.StoreTypeMulti
}
func (bs *baseMultiStore) GetCommitStore(key StoreKey) CommitStore {
return nil
}
func (bs *baseMultiStore) GetCommitKVStore(key StoreKey) CommitKVStore {
return nil
}
func (bs *baseMultiStore) GetStore(key StoreKey) Store {
return nil
}
func (bs *baseMultiStore) GetKVStore(key StoreKey) KVStore {
store := bs.stores[key].(*baseKVStore)
return store
}
func (bs *baseMultiStore) LoadLatestVersion() error {
ver := getLatestVersion(dbStoreAdapter{bs.db})
err := bs.LoadVersion(ver)
return err
}
func (bs *baseMultiStore) SetInitialVersion(version int64) error {
return nil
}
func (bs *baseMultiStore) LoadVersion(ver int64) error {
if ver == 0 {
for key, storeParams := range bs.storesParams {
id := CommitID{}
err := bs.loadCommitStoreFromParams(key, id, storeParams)
if err != nil {
return fmt.Errorf("failed to load rootMultiStore: %v", err)
}
}
bs.lastCommitID = CommitID{}
return nil
}
for key, storeParams := range bs.storesParams {
id := CommitID{}
err := bs.loadCommitStoreFromParams(key, id, storeParams)
if err != nil {
return fmt.Errorf("failed to load rootMultiStore: %v", err)
}
}
cInfo, err := getCommitInfo(dbStoreAdapter{bs.db}, ver)
if err != nil {
return err
}
bs.lastCommitID = cInfo.CommitID()
return err
}
func (bs *baseMultiStore) LastCommitID() CommitID {
return bs.lastCommitID
}
func (bs *baseMultiStore) Commit() CommitID {
var CommitInfo commitInfo
version := bs.lastCommitID.Version + 1
cInfoKey := fmt.Sprintf(sdk.CommitInfoKeyFmt, version)
cInfoBytes, _ := bs.db.Get([]byte(cInfoKey))
if cInfoBytes == nil {
// Commit stores.
CommitInfo = commitBaseStores(version, bs.stores)
// Need to update atomically.
//batch := bs.db.NewBatch()
//setCommitInfo(batch, version, CommitInfo)
//setLatestVersion(batch, version)
//batch.Write()
SetCommitInfo(bs.db, version, CommitInfo)
SetLatestVersion(bs.db, version)
}else{
cdc.MustUnmarshalBinaryLengthPrefixed(cInfoBytes, &CommitInfo)
}
// Prepare for next version.
commitID := CommitID{
Version: version,
Hash: CommitInfo.Hash(),
}
bs.lastCommitID = commitID
return commitID
}
func (bs *baseMultiStore) Write() {
return
}
func commitBaseStores(version int64, storeMap map[StoreKey]CommitStore) commitInfo {
storeInfos := make([]storeInfo, 0, len(storeMap))
for key, store := range storeMap {
// Commit
commitID := store.Commit()
if store.GetStoreType() == sdk.StoreTypeTransient {
continue
}
// Record CommitID
si := storeInfo{}
si.Name = key.Name()
si.Core.CommitID = commitID
// si.Core.StoreType = store.GetStoreType()
storeInfos = append(storeInfos, si)
}
ci := commitInfo{
Version: version,
StoreInfos: storeInfos,
}
return ci
}
func (bs *baseMultiStore) WithTracer(w io.Writer) MultiStore {
return nil
}
// WithTracingContext updates the tracing context for the MultiStore by merging
// the given context with the existing context by types. Any existing keys will
// be overwritten. It is implied that the caller should update the context when
// necessary between tracing operations. It returns a modified MultiStore.
func (bs *baseMultiStore) WithTracingContext(tc TraceContext) MultiStore {
return nil
}
func (bs *baseMultiStore) TracingEnabled() bool {
return false
}
// ResetTraceContext resets the current tracing context.
func (bs *baseMultiStore) ResetTraceContext() MultiStore {
return nil
}
// Implements CacheWrapper/Store/CommitStore.
func (bs *baseMultiStore) CacheWrap() CacheWrap {
return bs.CacheMultiStore().(CacheWrap)
}
// CacheWrapWithTrace implements the CacheWrapper interface.
func (bs *baseMultiStore) CacheWrapWithTrace(_ io.Writer, _ TraceContext) CacheWrap {
return bs.CacheWrap()
}
//----------------------------------------
// +MultiStore
// Implements MultiStore.
func (bs *baseMultiStore) CacheMultiStore() CacheMultiStore {
nbs := cacheMultiStore{
//db: NewCacheKVStore(dbStoreAdapter{bs.db})
stores: make(map[StoreKey]CacheWrap, len(bs.stores)),
keysByName: bs.keysByName,
}
for key, store := range bs.stores {
nbs.stores[key] = store.CacheWrap()
}
return nbs
}
func (bs *baseMultiStore) CacheMultiStoreWithVersion(version int64) (CacheMultiStore,error) {
panic("not implement")
}
func (bs *baseMultiStore) loadCommitStoreFromParams(key sdk.StoreKey, id CommitID, params storeParams) error {
_, ok := bs.stores[key]
if !ok {
store := NewBaseKVStore(dbStoreAdapter{bs.db}, int64(0), int64(0), key)
store.SetPruning(bs.pruning)
bs.stores[key] = store
}
return nil
}
//----------------------------------------
//query
func (bs *baseMultiStore) Query(req abci.RequestQuery) abci.ResponseQuery {
// Query just routes this to a substore.
path := req.Path
storeName, subpath, err := parsePath(path)
if err != nil {
return sdkerrors.QueryResult(err)
}
store := bs.getStoreByName(storeName).Prefix([]byte(storeName + "//"))
if store == nil {
msg := fmt.Sprintf("no such store: %s", storeName)
return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrInternal, msg))
}
queryable, ok := store.Parent().(Queryable)
if !ok {
msg := fmt.Sprintf("store %s doesn't support queries", storeName)
return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrInternal, msg))
}
// trim the path and make the query
req.Path = subpath
req.Data = append([]byte(storeName + "//"),req.Data...)
res := queryable.Query(req)
return res
}
func (bs *baseMultiStore) getStoreByName(name string) KVStore {
key := bs.keysByName[name]
if key == nil {
return nil
}
return bs.stores[key].(*baseKVStore)
}
func SetCommitInfo(db dbm.DB, version int64, info commitInfo) {
infoByte, _ := cdc.MarshalBinaryLengthPrefixed(info)
db.Set([]byte(fmt.Sprintf(sdk.CommitInfoKeyFmt, version)),infoByte)
}
func SetLatestVersion(db dbm.DB, version int64) {
versionByte, _ := cdc.MarshalBinaryLengthPrefixed(version)
db.Set([]byte(sdk.LatestVersionKey),versionByte)
}