-
Notifications
You must be signed in to change notification settings - Fork 19
/
checkpoint.go
139 lines (124 loc) · 3.85 KB
/
checkpoint.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
// 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 (
"bytes"
"context"
"code.vegaprotocol.io/vega/core/types"
vgcrypto "code.vegaprotocol.io/vega/libs/crypto"
"code.vegaprotocol.io/vega/libs/proto"
"code.vegaprotocol.io/vega/logging"
checkpoint "code.vegaprotocol.io/vega/protos/vega/checkpoint/v1"
)
func (*Service) Name() types.CheckpointName {
return types.AssetsCheckpoint
}
func (s *Service) Checkpoint() ([]byte, error) {
t := &checkpoint.Assets{
Assets: s.getEnabled(),
PendingListingAssets: s.getPendingListed(),
}
return proto.Marshal(t)
}
func (s *Service) Load(ctx context.Context, cp []byte) error {
data := &checkpoint.Assets{}
if err := proto.Unmarshal(cp, data); err != nil {
return err
}
s.amu.Lock()
s.pamu.Lock()
s.pendingAssets = map[string]*Asset{}
s.pamu.Unlock()
s.amu.Unlock()
for _, a := range data.Assets {
details, _ := types.AssetDetailsFromProto(a.AssetDetails)
// first check if the asset did get loaded from genesis
// if yes and the details + IDs are same, then all good
if existing, ok := s.assets[a.Id]; ok {
// we know this ID, are the details the same
// if not, then there's an error, and we should not overwrite an existing
// asset, only new ones can be added
if !bytes.Equal(vgcrypto.Hash([]byte(details.String())), vgcrypto.Hash([]byte(existing.Type().Details.String()))) {
s.log.Panic("invalid asset loaded from genesis",
logging.String("id", a.Id),
logging.String("details-genesis", existing.String()),
logging.String("details-checkpoint", details.String()))
}
continue
}
// asset didn't match anything, we need to go through the process to add it.
s.restoreAsset(ctx, a.Id, details)
if err := s.Enable(ctx, a.Id); err != nil {
return err
}
}
// now do pending assets
for _, pa := range data.PendingListingAssets {
details, _ := types.AssetDetailsFromProto(pa.AssetDetails)
// restore it as valid
s.restoreAsset(ctx, pa.Id, details)
// set as pending and generate the signatures to list on the contract
s.SetPendingListing(ctx, pa.Id)
s.EnactPendingAsset(pa.Id)
}
return nil
}
func (s *Service) restoreAsset(ctx context.Context, id string, details *types.AssetDetails) error {
id, err := s.NewAsset(ctx, id, details)
if err != nil {
return err
}
pa, _ := s.Get(id)
if s.isValidator {
if err := s.validateAsset(pa); err != nil {
return err
}
}
// always valid now
pa.SetValid()
return nil
}
func (s *Service) getEnabled() []*checkpoint.AssetEntry {
aa := s.GetEnabledAssets()
if len(aa) == 0 {
return nil
}
ret := make([]*checkpoint.AssetEntry, 0, len(aa))
for _, a := range aa {
ret = append(ret, &checkpoint.AssetEntry{
Id: a.ID,
AssetDetails: a.Details.IntoProto(),
})
}
return ret
}
func (s *Service) getPendingListed() []*checkpoint.AssetEntry {
pd := s.getPendingAssets()
if len(pd) == 0 {
return nil
}
ret := make([]*checkpoint.AssetEntry, 0, len(pd))
for _, a := range pd {
if a.Status != types.AssetStatusPendingListing {
// we only want enacted but not listed assets
continue
}
ret = append(ret, &checkpoint.AssetEntry{
Id: a.ID,
AssetDetails: a.Details.IntoProto(),
})
}
return ret
}