-
Notifications
You must be signed in to change notification settings - Fork 19
/
snapshot.go
260 lines (224 loc) · 7.4 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
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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package assets
import (
"context"
"code.vegaprotocol.io/vega/core/types"
vgcontext "code.vegaprotocol.io/vega/libs/context"
"code.vegaprotocol.io/vega/libs/proto"
"code.vegaprotocol.io/vega/logging"
)
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
}
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
}
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 {
s.applyMigrations(ctx, p)
if _, err = s.NewAsset(ctx, p.ID, p.Details); err != nil {
return err
}
pa, _ := s.Get(p.ID)
// at this point asset is always valid because we've loaded from a snapshot and have validated it when it was proposed
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 {
s.applyMigrations(ctx, p)
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(ctx context.Context, pending *types.PendingAssetUpdates, p *types.Payload) error {
var err error
s.pendingAssetUpdates = map[string]*Asset{}
for _, p := range pending.Assets {
s.applyMigrations(ctx, p)
if err = s.StageAssetUpdate(p); err != nil {
return err
}
}
s.ass.serialisedPendingUpdates, err = proto.Marshal(p.IntoProto())
return err
}
func (s *Service) OnStateLoaded(ctx context.Context) error {
if !vgcontext.InProgressUpgrade(ctx) || s.isValidator {
return nil
}
// note that non-validator nodes do not know the chain-id for the bridges until the network parameters are propagated, but also *validator* nodes need to
// restore assets before the network parameters. So for the non-validator nodes only, we have to do the migration to include the chain-id here, after everything
// else is restored.
s.log.Info("migrating chain-id in existing active assets for non-validator nodes")
for k, a := range s.assets {
eth, ok := a.ERC20()
if !ok || eth.ChainID() != "" {
continue
}
s.log.Info("setting chain-id for active asset",
logging.String("asset-id", k),
logging.String("chain-id", s.primaryEthChainID),
)
eth.SetChainID(s.primaryEthChainID)
}
s.log.Info("migrating chain-id in existing pending assets for non-validator nodes")
for k, p := range s.pendingAssets {
eth, ok := p.ERC20()
if !ok || eth.ChainID() != "" {
continue
}
s.log.Info("setting chain-id for pending asset",
logging.String("asset-id", k),
logging.String("chain-id", s.primaryEthChainID),
)
eth.SetChainID(s.primaryEthChainID)
}
s.log.Info("migrating chain-id in existing update-pending assets for non-validator nodes")
for k, pu := range s.pendingAssetUpdates {
eth, ok := pu.ERC20()
if !ok || eth.ChainID() != "" {
continue
}
s.log.Info("setting chain-id for pending update asset",
logging.String("asset-id", k),
logging.String("chain-id", s.primaryEthChainID),
)
eth.SetChainID(s.primaryEthChainID)
}
return nil
}
func (s *Service) applyMigrations(ctx context.Context, p *types.Asset) {
// TODO when we know what versions we are upgrading from we can put in a upgrade from tag
if vgcontext.InProgressUpgrade(ctx) && s.isValidator {
// Prior the introduction of the second bridge, existing assets did not track
// the chain ID they originated from. So, when loaded, assets without a chain
// ID are automatically considered to originate from Ethereum Mainnet.
if erc20 := p.Details.GetERC20(); erc20 != nil && erc20.ChainID == "" {
s.log.Info("migrating chain-id in existin asset for validator nodes",
logging.String("asset-id", p.ID),
logging.String("chain-id", s.primaryEthChainID),
)
erc20.ChainID = s.primaryEthChainID
}
}
}