-
Notifications
You must be signed in to change notification settings - Fork 19
/
snapshot.go
191 lines (163 loc) · 4.8 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.VEGA file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package assets
import (
"context"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/proto"
)
var (
activeKey = (&types.PayloadActiveAssets{}).Key()
pendingKey = (&types.PayloadPendingAssets{}).Key()
pendingUpdatesKey = (&types.PayloadPendingAssetUpdates{}).Key()
hashKeys = []string{
activeKey,
pendingKey,
pendingUpdatesKey,
}
)
type assetsSnapshotState struct {
serialisedActive []byte
serialisedPending []byte
serialisedPendingUpdates []byte
}
func (s *Service) Namespace() types.SnapshotNamespace {
return types.AssetsSnapshot
}
func (s *Service) Keys() []string {
return hashKeys
}
func (s *Service) Stopped() bool {
return false
}
func (s *Service) serialiseActive() ([]byte, error) {
enabled := s.GetEnabledAssets()
payload := types.Payload{
Data: &types.PayloadActiveAssets{
ActiveAssets: &types.ActiveAssets{
Assets: enabled,
},
},
}
return proto.Marshal(payload.IntoProto())
}
func (s *Service) serialisePending() ([]byte, error) {
pending := s.getPendingAssets()
payload := types.Payload{
Data: &types.PayloadPendingAssets{
PendingAssets: &types.PendingAssets{
Assets: pending,
},
},
}
return proto.Marshal(payload.IntoProto())
}
func (s *Service) serialisePendingUpdates() ([]byte, error) {
pendingUpdates := s.getPendingAssetUpdates()
payload := types.Payload{
Data: &types.PayloadPendingAssetUpdates{
PendingAssetUpdates: &types.PendingAssetUpdates{
Assets: pendingUpdates,
},
},
}
return proto.Marshal(payload.IntoProto())
}
func (s *Service) serialiseK(serialFunc func() ([]byte, error), dataField *[]byte) ([]byte, error) {
data, err := serialFunc()
if err != nil {
return nil, err
}
*dataField = data
return data, nil
}
// get the serialised form and hash of the given key.
func (s *Service) serialise(k string) ([]byte, error) {
switch k {
case activeKey:
return s.serialiseK(s.serialiseActive, &s.ass.serialisedActive)
case pendingKey:
return s.serialiseK(s.serialisePending, &s.ass.serialisedPending)
case pendingUpdatesKey:
return s.serialiseK(s.serialisePendingUpdates, &s.ass.serialisedPendingUpdates)
default:
return nil, types.ErrSnapshotKeyDoesNotExist
}
}
func (s *Service) GetState(k string) ([]byte, []types.StateProvider, error) {
state, err := s.serialise(k)
return state, nil, err
}
func (s *Service) LoadState(ctx context.Context, p *types.Payload) ([]types.StateProvider, error) {
if s.Namespace() != p.Data.Namespace() {
return nil, types.ErrInvalidSnapshotNamespace
}
// see what we're reloading
switch pl := p.Data.(type) {
case *types.PayloadActiveAssets:
return nil, s.restoreActive(ctx, pl.ActiveAssets, p)
case *types.PayloadPendingAssets:
return nil, s.restorePending(ctx, pl.PendingAssets, p)
case *types.PayloadPendingAssetUpdates:
return nil, s.restorePendingUpdates(ctx, pl.PendingAssetUpdates, p)
default:
return nil, types.ErrUnknownSnapshotType
}
}
func (s *Service) restoreActive(ctx context.Context, active *types.ActiveAssets, p *types.Payload) error {
var err error
s.assets = map[string]*Asset{}
for _, p := range active.Assets {
if _, err = s.NewAsset(ctx, p.ID, p.Details); err != nil {
return err
}
pa, _ := s.Get(p.ID)
if s.isValidator {
if err = s.validateAsset(pa); err != nil {
return err
}
}
// at this point asset is always valid
pa.SetValid()
if err = s.Enable(ctx, p.ID); err != nil {
return err
}
}
s.ass.serialisedActive, err = proto.Marshal(p.IntoProto())
return err
}
func (s *Service) restorePending(ctx context.Context, pending *types.PendingAssets, p *types.Payload) error {
var err error
s.pendingAssets = map[string]*Asset{}
for _, p := range pending.Assets {
assetID, err := s.NewAsset(ctx, p.ID, p.Details)
if err != nil {
return err
}
if p.Status == types.AssetStatusPendingListing {
s.SetPendingListing(ctx, assetID)
}
}
s.ass.serialisedPending, err = proto.Marshal(p.IntoProto())
return err
}
func (s *Service) restorePendingUpdates(_ context.Context, pending *types.PendingAssetUpdates, p *types.Payload) error {
var err error
s.pendingAssetUpdates = map[string]*Asset{}
for _, p := range pending.Assets {
if err = s.StageAssetUpdate(p); err != nil {
return err
}
}
s.ass.serialisedPendingUpdates, err = proto.Marshal(p.IntoProto())
return err
}