-
Notifications
You must be signed in to change notification settings - Fork 19
/
snapshot.go
299 lines (263 loc) · 9.02 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
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
289
290
291
292
293
294
295
296
297
298
299
// 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 governance
import (
"context"
"sort"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/logging"
"code.vegaprotocol.io/vega/libs/proto"
)
var (
activeKey = (&types.PayloadGovernanceActive{}).Key()
enactedKey = (&types.PayloadGovernanceEnacted{}).Key()
nodeValidationKey = (&types.PayloadGovernanceNode{}).Key()
hashKeys = []string{
activeKey,
enactedKey,
nodeValidationKey,
}
)
type governanceSnapshotState struct {
serialisedActive []byte
serialisedEnacted []byte
serialisedNodeValidation []byte
}
func (e *Engine) OnStateLoaded(ctx context.Context) error {
// previously new market proposals that passed but where not enacted existed in both
// the active and enacted slices, but now this has changed and it is only ever in one
// or the other.
// so for upgrade purposes any active proposals in the enacted slice needs to be removed
// from the enacted slice
for _, p := range e.activeProposals {
for i := range e.enactedProposals {
if p.ID == e.enactedProposals[i].ID {
e.log.Warn("removing proposal from enacted since it is also in active", logging.String("id", p.ID))
e.enactedProposals = append(e.enactedProposals[:i], e.enactedProposals[i+1:]...)
break
}
}
}
return nil
}
// serialiseActiveProposals returns the engine's active proposals as marshalled bytes.
func (e *Engine) serialiseActiveProposals() ([]byte, error) {
pending := make([]*types.ProposalData, 0, len(e.activeProposals))
for _, p := range e.activeProposals {
pp := &types.ProposalData{
Proposal: p.Proposal,
Yes: votesAsSlice(p.yes),
No: votesAsSlice(p.no),
Invalid: votesAsSlice(p.invalidVotes),
}
pending = append(pending, pp)
}
pl := types.Payload{
Data: &types.PayloadGovernanceActive{
GovernanceActive: &types.GovernanceActive{
Proposals: pending,
},
},
}
return proto.Marshal(pl.IntoProto())
}
// serialiseEnactedProposals returns the engine's enacted proposals as marshalled bytes.
func (e *Engine) serialiseEnactedProposals() ([]byte, error) {
enacted := make([]*types.ProposalData, 0, len(e.activeProposals))
for _, p := range e.enactedProposals {
pp := &types.ProposalData{
Proposal: p.Proposal,
Yes: votesAsSlice(p.yes),
No: votesAsSlice(p.no),
Invalid: votesAsSlice(p.invalidVotes),
}
enacted = append(enacted, pp)
}
pl := types.Payload{
Data: &types.PayloadGovernanceEnacted{
GovernanceEnacted: &types.GovernanceEnacted{
Proposals: enacted,
},
},
}
return proto.Marshal(pl.IntoProto())
}
// serialiseNodeProposals returns the engine's proposals waiting for node validation.
func (e *Engine) serialiseNodeProposals() ([]byte, error) {
nodeProposals := e.nodeProposalValidation.getProposals()
proposals := make([]*types.Proposal, 0, len(nodeProposals))
for _, np := range nodeProposals {
// Given a snapshot is always taken at the end of a block the value of `state` in np will
// always be pending since any that are not will have already been resolved as accepted/rejected
// and removed from the slice. The yes/no/invalid fields in `np.proposal` are also unnecessary to
// save since "voting" as is done for active proposals is not done on node-proposals, and so the
// maps will always be empty
p := np.proposal.Proposal
proposals = append(proposals, p)
}
pl := types.Payload{
Data: &types.PayloadGovernanceNode{
GovernanceNode: &types.GovernanceNode{
Proposals: proposals,
},
},
}
return proto.Marshal(pl.IntoProto())
}
func (e *Engine) serialiseK(serialFunc func() ([]byte, error), dataField *[]byte) ([]byte, error) {
data, err := serialFunc()
if err != nil {
return nil, err
}
*dataField = data
return data, nil
}
func (e *Engine) serialise(k string) ([]byte, error) {
switch k {
case activeKey:
return e.serialiseK(e.serialiseActiveProposals, &e.gss.serialisedActive)
case enactedKey:
return e.serialiseK(e.serialiseEnactedProposals, &e.gss.serialisedEnacted)
case nodeValidationKey:
return e.serialiseK(e.serialiseNodeProposals, &e.gss.serialisedNodeValidation)
default:
return nil, types.ErrSnapshotKeyDoesNotExist
}
}
func (e *Engine) Namespace() types.SnapshotNamespace {
return types.GovernanceSnapshot
}
func (e *Engine) Keys() []string {
return hashKeys
}
func (e *Engine) Stopped() bool {
return false
}
func (e *Engine) GetState(k string) ([]byte, []types.StateProvider, error) {
data, err := e.serialise(k)
return data, nil, err
}
func (e *Engine) LoadState(ctx context.Context, p *types.Payload) ([]types.StateProvider, error) {
if e.Namespace() != p.Data.Namespace() {
return nil, types.ErrInvalidSnapshotNamespace
}
switch pl := p.Data.(type) {
case *types.PayloadGovernanceActive:
return nil, e.restoreActiveProposals(ctx, pl.GovernanceActive, p)
case *types.PayloadGovernanceEnacted:
e.restoreEnactedProposals(ctx, pl.GovernanceEnacted, p)
return nil, nil
case *types.PayloadGovernanceNode:
return nil, e.restoreNodeProposals(ctx, pl.GovernanceNode, p)
default:
return nil, types.ErrUnknownSnapshotType
}
}
func (e *Engine) restoreActiveProposals(ctx context.Context, active *types.GovernanceActive, p *types.Payload) error {
e.activeProposals = make([]*proposal, 0, len(active.Proposals))
evts := []events.Event{}
vevts := []events.Event{}
e.log.Debug("restoring active proposals snapshot", logging.Int("nproposals", len(active.Proposals)))
for _, p := range active.Proposals {
pp := &proposal{
Proposal: p.Proposal,
yes: votesAsMap(p.Yes),
no: votesAsMap(p.No),
invalidVotes: votesAsMap(p.Invalid),
}
e.log.Debug("proposals",
logging.String("id", pp.ID),
logging.Int("yes", len(pp.yes)),
logging.Int("no", len(pp.no)),
logging.Int("invalid", len(pp.invalidVotes)),
)
e.activeProposals = append(e.activeProposals, pp)
evts = append(evts, events.NewProposalEvent(ctx, *pp.Proposal))
for _, v := range pp.yes {
vevts = append(vevts, events.NewVoteEvent(ctx, *v))
}
for _, v := range pp.no {
vevts = append(vevts, events.NewVoteEvent(ctx, *v))
}
for _, v := range pp.invalidVotes {
vevts = append(vevts, events.NewVoteEvent(ctx, *v))
}
}
var err error
e.gss.serialisedActive, err = proto.Marshal(p.IntoProto())
e.broker.SendBatch(evts)
e.broker.SendBatch(vevts)
return err
}
func (e *Engine) restoreEnactedProposals(ctx context.Context, enacted *types.GovernanceEnacted, p *types.Payload) {
evts := []events.Event{}
vevts := []events.Event{}
e.log.Debug("restoring enacted proposals snapshot", logging.Int("nproposals", len(enacted.Proposals)))
for _, p := range enacted.Proposals {
pp := &proposal{
Proposal: p.Proposal,
yes: votesAsMap(p.Yes),
no: votesAsMap(p.No),
invalidVotes: votesAsMap(p.Invalid),
}
e.log.Debug("proposals",
logging.String("id", pp.ID),
logging.Int("yes", len(pp.yes)),
logging.Int("no", len(pp.no)),
logging.Int("invalid", len(pp.invalidVotes)),
)
e.enactedProposals = append(e.enactedProposals, pp)
evts = append(evts, events.NewProposalEvent(ctx, *pp.Proposal))
for _, v := range pp.yes {
vevts = append(vevts, events.NewVoteEvent(ctx, *v))
}
for _, v := range pp.no {
vevts = append(vevts, events.NewVoteEvent(ctx, *v))
}
for _, v := range pp.invalidVotes {
vevts = append(vevts, events.NewVoteEvent(ctx, *v))
}
}
e.gss.serialisedEnacted, _ = proto.Marshal(p.IntoProto())
e.broker.SendBatch(evts)
e.broker.SendBatch(vevts)
}
func (e *Engine) restoreNodeProposals(ctx context.Context, node *types.GovernanceNode, p *types.Payload) error {
for _, p := range node.Proposals {
e.nodeProposalValidation.restore(ctx, p)
e.broker.Send(events.NewProposalEvent(ctx, *p))
}
var err error
e.gss.serialisedNodeValidation, err = proto.Marshal(p.IntoProto())
return err
}
// votesAsSlice returns a sorted slice of votes from a given map of votes.
func votesAsSlice(votes map[string]*types.Vote) []*types.Vote {
ret := make([]*types.Vote, 0, len(votes))
for _, v := range votes {
ret = append(ret, v)
}
sort.SliceStable(ret, func(i, j int) bool { return ret[i].PartyID < ret[j].PartyID })
return ret
}
// votesAsMap returns an partyID => Vote map from the given slice of votes.
func votesAsMap(votes []*types.Vote) map[string]*types.Vote {
r := make(map[string]*types.Vote, len(votes))
for _, v := range votes {
r[v.PartyID] = v
}
return r
}