/
snapshot.go
216 lines (188 loc) · 5.28 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
// 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 netparams
import (
"context"
"fmt"
"sort"
"code.vegaprotocol.io/vega/core/types"
vgcontext "code.vegaprotocol.io/vega/libs/context"
"code.vegaprotocol.io/vega/libs/proto"
)
type snapState struct {
data []byte
pl *types.NetParams
index map[string]int
t *types.PayloadNetParams
}
func newSnapState(store map[string]value) *snapState {
state := &snapState{
pl: &types.NetParams{},
index: make(map[string]int, len(store)),
t: &types.PayloadNetParams{},
}
// set pointer
state.t.NetParams = state.pl
// set the initial state
state.build(store)
return state
}
func (s *snapState) build(store map[string]value) {
params := make([]*types.NetworkParameter, 0, len(store))
for k, v := range store {
params = append(params, &types.NetworkParameter{
Key: k,
Value: v.String(),
})
}
// sort by key
sort.SliceStable(params, func(i, j int) bool {
return params[i].Key < params[j].Key
})
// build the index
for i, p := range params {
s.index[p.Key] = i
}
s.pl.Params = params
}
func (s *snapState) Keys() []string {
return []string{
s.t.Key(),
}
}
func (s *snapState) Namespace() types.SnapshotNamespace {
return s.t.Namespace()
}
func (s *snapState) hashState() error {
// apparently the payload types can't me marshalled by themselves
pl := types.Payload{
Data: s.t,
}
data, err := proto.Marshal(pl.IntoProto())
if err != nil {
return err
}
s.data = data
return nil
}
func (s *snapState) GetState(_ string) ([]byte, error) {
if err := s.hashState(); err != nil {
return nil, err
}
return s.data, nil
}
func (s *snapState) update(k, v string) {
i, ok := s.index[k]
if !ok {
i = len(s.pl.Params)
s.pl.Params = append(s.pl.Params, &types.NetworkParameter{
Key: k,
})
}
s.pl.Params[i].Value = v
}
// make Store implement/forward the dataprovider interface
func (s *Store) Namespace() types.SnapshotNamespace {
return s.state.Namespace()
}
func (s *Store) Keys() []string {
return s.state.Keys()
}
func (s *Store) Stopped() bool {
return false
}
func (s *Store) GetState(k string) ([]byte, []types.StateProvider, error) {
state, err := s.state.GetState(k)
return state, nil, err
}
func (s *Store) LoadState(ctx context.Context, pl *types.Payload) ([]types.StateProvider, error) {
overrideSet := map[string]struct{}{}
if vgcontext.InProgressUpgradeFrom(ctx, "v0.73.1") {
// this is our list of network parameters to override
overrides := []types.NetworkParameter{
{
Key: "governance.proposal.transfer.minProposerBalance",
Value: "20000000000000000000000",
},
{
Key: "governance.proposal.transfer.minVoterBalance",
Value: "1000000000000000000",
},
{
Key: "governance.proposal.transfer.minVoterBalance",
Value: "1000000000000000000",
},
{
Key: "governance.proposal.transfer.requiredParticipation",
Value: "0.01",
},
{
Key: "governance.proposal.transfer.minClose",
Value: "168h",
},
{
Key: "governance.proposal.transfer.minEnact",
Value: "168h",
},
}
// let's apply them first
for _, v := range overrides {
// add to the set
overrideSet[v.Key] = struct{}{}
// always in the override
s.protocolUpgradeNewParameters = append(
s.protocolUpgradeNewParameters, v.Key,
)
// then apply the update
if err := s.UpdateOptionalValidation(ctx, v.Key, v.Value, false, false); err != nil {
return nil, err
}
}
}
if pl.Namespace() != s.state.Namespace() {
return nil, types.ErrInvalidSnapshotNamespace
}
np, ok := pl.Data.(*types.PayloadNetParams)
if !ok {
return nil, types.ErrInconsistentNamespaceKeys // it's the only possible key/namespace combo here
}
fromSnapshot := map[string]struct{}{}
for _, kv := range np.NetParams.Params {
// execute this if not in the override list
if _, ok := overrideSet[kv.Key]; !ok {
if err := s.UpdateOptionalValidation(ctx, kv.Key, kv.Value, false, false); err != nil {
return nil, err
}
}
fromSnapshot[kv.Key] = struct{}{}
}
// Now they have been loaded, dispatch the changes so that the other engines pick them up
for k := range s.store {
// is this a new parameter? if yes, we are likely doing a protocol
// upgrade, and should be sending that to the datanode please.
if _, ok := fromSnapshot[k]; !ok {
s.protocolUpgradeNewParameters = append(
s.protocolUpgradeNewParameters, k,
)
}
if err := s.dispatchUpdate(ctx, k); err != nil {
return nil, fmt.Errorf("could not propagate netparams update to listener, %v: %v", k, err)
}
}
var err error
s.state.data, err = proto.Marshal(pl.IntoProto())
s.paramUpdates = map[string]struct{}{}
return nil, err
}