/
snapshot.go
172 lines (146 loc) · 4.67 KB
/
snapshot.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
package multi
import (
"bytes"
"fmt"
"io"
"sort"
protoio "github.com/gogo/protobuf/io"
"github.com/cosmos/cosmos-sdk/snapshots"
snapshottypes "github.com/cosmos/cosmos-sdk/snapshots/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
types "github.com/cosmos/cosmos-sdk/store/v2alpha1"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// Snapshot implements snapshottypes.Snapshotter.
func (rs *Store) Snapshot(height uint64, protoWriter protoio.Writer) error {
if height == 0 {
return snapshottypes.ErrInvalidSnapshotVersion
}
if height > uint64(rs.LastCommitID().Version) {
return sdkerrors.Wrapf(sdkerrors.ErrLogic, "cannot snapshot future height %v", height)
}
// get the saved snapshot at height
vs, err := rs.getView(int64(height))
if err != nil {
return sdkerrors.Wrap(err, fmt.Sprintf("error while get the version at height %d", height))
}
// sending the snapshot store schema
var storeByteKeys [][]byte
for sKey := range vs.schema {
if vs.schema[sKey] == storetypes.StoreTypePersistent {
storeByteKeys = append(storeByteKeys, []byte(sKey))
}
}
sort.Slice(storeByteKeys, func(i, j int) bool {
return bytes.Compare(storeByteKeys[i], storeByteKeys[j]) == -1
})
err = protoWriter.WriteMsg(&snapshottypes.SnapshotItem{
Item: &snapshottypes.SnapshotItem_Schema{
Schema: &snapshottypes.SnapshotSchema{
Keys: storeByteKeys,
},
},
})
if err != nil {
return err
}
for _, sKey := range storeByteKeys {
subStore, err := vs.getSubstore(string(sKey))
if err != nil {
return err
}
err = protoWriter.WriteMsg(&snapshottypes.SnapshotItem{
Item: &snapshottypes.SnapshotItem_Store{
Store: &snapshottypes.SnapshotStoreItem{
Name: string(sKey),
},
},
})
if err != nil {
return err
}
iter := subStore.Iterator(nil, nil)
for ; iter.Valid(); iter.Next() {
err = protoWriter.WriteMsg(&snapshottypes.SnapshotItem{
Item: &snapshottypes.SnapshotItem_KV{
KV: &snapshottypes.SnapshotKVItem{
Key: iter.Key(),
Value: iter.Value(),
},
},
})
if err != nil {
return err
}
}
err = iter.Close()
if err != nil {
return err
}
}
return nil
}
// Restore implements snapshottypes.Snapshotter.
func (rs *Store) Restore(
height uint64, format uint32, protoReader protoio.Reader,
) (snapshottypes.SnapshotItem, error) {
if err := snapshots.ValidRestoreHeight(format, height); err != nil {
return snapshottypes.SnapshotItem{}, err
}
if rs.LastCommitID().Version != 0 {
return snapshottypes.SnapshotItem{}, sdkerrors.Wrapf(sdkerrors.ErrLogic, "cannot restore snapshot for non empty store at height %v", height)
}
var subStore *substore
storeSchemaReceived := false
var snapshotItem snapshottypes.SnapshotItem
loop:
for {
snapshotItem = snapshottypes.SnapshotItem{}
err := protoReader.ReadMsg(&snapshotItem)
if err == io.EOF {
break
} else if err != nil {
return snapshottypes.SnapshotItem{}, sdkerrors.Wrap(err, "invalid protobuf message")
}
switch item := snapshotItem.Item.(type) {
case *snapshottypes.SnapshotItem_Schema:
receivedStoreSchema := make(StoreSchema, len(item.Schema.GetKeys()))
storeSchemaReceived = true
for _, sKey := range item.Schema.GetKeys() {
receivedStoreSchema[string(sKey)] = types.StoreTypePersistent
}
if !rs.schema.equal(receivedStoreSchema) {
return snapshottypes.SnapshotItem{}, sdkerrors.Wrap(sdkerrors.ErrLogic, "received schema does not match app schema")
}
case *snapshottypes.SnapshotItem_Store:
storeName := item.Store.GetName()
// checking the store schema is received or not
if !storeSchemaReceived {
return snapshottypes.SnapshotItem{}, sdkerrors.Wrapf(sdkerrors.ErrLogic, "received store name before store schema %s", storeName)
}
// checking the store schema exists or not
if _, has := rs.schema[storeName]; !has {
return snapshottypes.SnapshotItem{}, sdkerrors.Wrapf(sdkerrors.ErrLogic, "store is missing from schema %s", storeName)
}
// get the substore
subStore, err = rs.getSubstore(storeName)
if err != nil {
return snapshottypes.SnapshotItem{}, sdkerrors.Wrap(err, fmt.Sprintf("error while getting the substore for key %s", storeName))
}
case *snapshottypes.SnapshotItem_KV:
if subStore == nil {
return snapshottypes.SnapshotItem{}, sdkerrors.Wrap(sdkerrors.ErrLogic, "received KV Item before store item")
}
// update the key/value SMT.Store
subStore.Set(item.KV.Key, item.KV.Value)
default:
break loop
}
}
// commit the all key/values to store
_, err := rs.commit(height)
if err != nil {
return snapshotItem, sdkerrors.Wrap(err, fmt.Sprintf("error during commit the store at height %d", height))
}
return snapshotItem, nil
}