-
Notifications
You must be signed in to change notification settings - Fork 256
/
ParamsParser.go
370 lines (302 loc) · 10.8 KB
/
ParamsParser.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package parser
import (
"encoding/hex"
"encoding/json"
"fmt"
"math"
"os"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
)
const (
// tag length constant in bytes. We define it here as it won't change, but
// this allows us to not bring whole Babylon node as dependency.
TagLen = 4
// minimum unbonding output value to avoid the unbonding output value being
// less than Bitcoin dust
MinUnbondingOutputValue = btcutil.Amount(1000)
)
func checkPositive(value uint64) error {
if value == 0 {
return fmt.Errorf("value must be positive")
}
return nil
}
func parseTimeLockValue(timelock uint64) (uint16, error) {
if timelock > math.MaxUint16 {
return 0, fmt.Errorf("timelock value %d is too large. Max: %d", timelock, math.MaxUint16)
}
if err := checkPositive(timelock); err != nil {
return 0, fmt.Errorf("invalid timelock value: %w", err)
}
return uint16(timelock), nil
}
func parseConfirmationDepthValue(confirmationDepth uint64) (uint16, error) {
if confirmationDepth > math.MaxUint16 {
return 0, fmt.Errorf("timelock value %d is too large. Max: %d", confirmationDepth, math.MaxUint16)
}
if confirmationDepth <= 1 {
return 0, fmt.Errorf("confirmation depth value should be at least 2, got %d", confirmationDepth)
}
return uint16(confirmationDepth), nil
}
func parseBtcValue(value uint64) (btcutil.Amount, error) {
if value > math.MaxInt64 {
return 0, fmt.Errorf("value %d is too large. Max: %d", value, math.MaxInt64)
}
if err := checkPositive(value); err != nil {
return 0, fmt.Errorf("invalid btc value: %w", err)
}
// return amount in satoshis
return btcutil.Amount(value), nil
}
func parseUint32(value uint64) (uint32, error) {
if value > math.MaxUint32 {
return 0, fmt.Errorf("value %d is too large. Max: %d", value, math.MaxUint32)
}
if err := checkPositive(value); err != nil {
return 0, fmt.Errorf("invalid value: %w", err)
}
return uint32(value), nil
}
// parseCovenantPubKeyFromHex parses public key string to btc public key
// the input should be 33 bytes
func parseCovenantPubKeyFromHex(pkStr string) (*btcec.PublicKey, error) {
pkBytes, err := hex.DecodeString(pkStr)
if err != nil {
return nil, err
}
pk, err := btcec.ParsePubKey(pkBytes)
if err != nil {
return nil, err
}
return pk, nil
}
// either staking cap and cap height should be positive if cap height is positive
func parseCap(stakingCap, capHeight uint64) (btcutil.Amount, uint64, error) {
if stakingCap != 0 && capHeight != 0 {
return 0, 0, fmt.Errorf("only either of staking cap and cap height can be set")
}
if stakingCap == 0 && capHeight == 0 {
return 0, 0, fmt.Errorf("either of staking cap and cap height must be set")
}
if stakingCap != 0 {
parsedStakingCap, err := parseBtcValue(stakingCap)
return parsedStakingCap, 0, err
}
return 0, capHeight, nil
}
type GlobalParams struct {
Versions []*VersionedGlobalParams `json:"versions"`
}
type VersionedGlobalParams struct {
Version uint64 `json:"version"`
ActivationHeight uint64 `json:"activation_height"`
StakingCap uint64 `json:"staking_cap,omitempty"`
CapHeight uint64 `json:"cap_height,omitempty"`
Tag string `json:"tag"`
CovenantPks []string `json:"covenant_pks"`
CovenantQuorum uint64 `json:"covenant_quorum"`
UnbondingTime uint64 `json:"unbonding_time"`
UnbondingFee uint64 `json:"unbonding_fee"`
MaxStakingAmount uint64 `json:"max_staking_amount"`
MinStakingAmount uint64 `json:"min_staking_amount"`
MaxStakingTime uint64 `json:"max_staking_time"`
MinStakingTime uint64 `json:"min_staking_time"`
ConfirmationDepth uint64 `json:"confirmation_depth"`
}
type ParsedGlobalParams struct {
Versions []*ParsedVersionedGlobalParams
}
type ParsedVersionedGlobalParams struct {
Version uint64
ActivationHeight uint64
StakingCap btcutil.Amount
CapHeight uint64
Tag []byte
CovenantPks []*btcec.PublicKey
CovenantQuorum uint32
UnbondingTime uint16
UnbondingFee btcutil.Amount
MaxStakingAmount btcutil.Amount
MinStakingAmount btcutil.Amount
MaxStakingTime uint16
MinStakingTime uint16
ConfirmationDepth uint16
}
func ParseGlobalParams(p *GlobalParams) (*ParsedGlobalParams, error) {
if len(p.Versions) == 0 {
return nil, fmt.Errorf("global params must have at least one version")
}
var parsedVersions []*ParsedVersionedGlobalParams
for i, v := range p.Versions {
vCopy := v
cv, err := parseVersionedGlobalParams(vCopy)
if err != nil {
return nil, fmt.Errorf("invalid params with version %d: %w", vCopy.Version, err)
}
// Check latest version
if len(parsedVersions) > 0 {
pv := parsedVersions[len(parsedVersions)-1]
lastStakingCap := FindLastStakingCap(p.Versions[:i])
if cv.Version != pv.Version+1 {
return nil, fmt.Errorf("invalid params with version %d. versions should be monotonically increasing by 1", cv.Version)
}
if cv.StakingCap != 0 && cv.StakingCap < btcutil.Amount(lastStakingCap) {
return nil, fmt.Errorf("invalid params with version %d. staking cap cannot be decreased in later versions, last non-zero staking cap: %d, got: %d",
cv.Version, lastStakingCap, cv.StakingCap)
}
if cv.ActivationHeight <= pv.ActivationHeight {
return nil, fmt.Errorf("invalid params with version %d. activation height cannot be overlapping between earlier and later versions", cv.Version)
}
}
parsedVersions = append(parsedVersions, cv)
}
return &ParsedGlobalParams{
Versions: parsedVersions,
}, nil
}
func parseVersionedGlobalParams(p *VersionedGlobalParams) (*ParsedVersionedGlobalParams, error) {
tag, err := hex.DecodeString(p.Tag)
if err != nil {
return nil, fmt.Errorf("invalid tag: %w", err)
}
if len(tag) != TagLen {
return nil, fmt.Errorf("invalid tag length, expected %d, got %d", TagLen, len(tag))
}
if len(p.CovenantPks) == 0 {
return nil, fmt.Errorf("empty covenant public keys")
}
if p.CovenantQuorum > uint64(len(p.CovenantPks)) {
return nil, fmt.Errorf("covenant quorum %d cannot be more than the amount of covenants %d", p.CovenantQuorum, len(p.CovenantPks))
}
quorum, err := parseUint32(p.CovenantQuorum)
if err != nil {
return nil, fmt.Errorf("invalid covenant quorum: %w", err)
}
var covenantKeys []*btcec.PublicKey
for _, covPk := range p.CovenantPks {
pk, err := parseCovenantPubKeyFromHex(covPk)
if err != nil {
return nil, fmt.Errorf("invalid covenant public key %s: %w", covPk, err)
}
covenantKeys = append(covenantKeys, pk)
}
maxStakingAmount, err := parseBtcValue(p.MaxStakingAmount)
if err != nil {
return nil, fmt.Errorf("invalid max_staking_amount: %w", err)
}
minStakingAmount, err := parseBtcValue(p.MinStakingAmount)
if err != nil {
return nil, fmt.Errorf("invalid min_staking_amount: %w", err)
}
// NOTE: Allow config when max-staking-amount is equal tomin-staking-amount, as then
// we can configure a fixed staking amount
if maxStakingAmount < minStakingAmount {
return nil, fmt.Errorf("max-staking-amount %d must be larger than or equal to min-staking-amount %d", maxStakingAmount, minStakingAmount)
}
ubTime, err := parseTimeLockValue(p.UnbondingTime)
if err != nil {
return nil, fmt.Errorf("invalid unbonding_time: %w", err)
}
ubFee, err := parseBtcValue(p.UnbondingFee)
if err != nil {
return nil, fmt.Errorf("invalid unbonding_fee: %w", err)
}
if minStakingAmount < ubFee+MinUnbondingOutputValue {
return nil, fmt.Errorf("min_staking_amount %d should not be less than unbonding fee %d plus %d",
minStakingAmount, ubFee, MinUnbondingOutputValue)
}
maxStakingTime, err := parseTimeLockValue(p.MaxStakingTime)
if err != nil {
return nil, fmt.Errorf("invalid max_staking_time: %w", err)
}
minStakingTime, err := parseTimeLockValue(p.MinStakingTime)
if err != nil {
return nil, fmt.Errorf("invalid min_staking_time: %w", err)
}
// NOTE: Allow config when max-staking-time is equal to min-staking-time, as then
// we can configure a fixed staking time.
if maxStakingTime < minStakingTime {
return nil, fmt.Errorf("max-staking-time %d must be larger than or equal to min-staking-time %d", maxStakingTime, minStakingTime)
}
confirmationDepth, err := parseConfirmationDepthValue(p.ConfirmationDepth)
if err != nil {
return nil, fmt.Errorf("invalid confirmation_depth: %w", err)
}
if err := checkPositive(p.ActivationHeight); err != nil {
return nil, fmt.Errorf("activation_height: %w", err)
}
stakingCap, capHeight, err := parseCap(p.StakingCap, p.CapHeight)
if err != nil {
return nil, fmt.Errorf("invalid cap: %w", err)
}
if stakingCap != 0 && stakingCap < maxStakingAmount {
return nil, fmt.Errorf("invalid staking_cap, should be larger than max_staking_amount: %d, got: %d",
maxStakingAmount, stakingCap)
}
return &ParsedVersionedGlobalParams{
Version: p.Version,
ActivationHeight: p.ActivationHeight,
StakingCap: stakingCap,
CapHeight: capHeight,
Tag: tag,
CovenantPks: covenantKeys,
CovenantQuorum: quorum,
UnbondingTime: ubTime,
UnbondingFee: ubFee,
MaxStakingAmount: maxStakingAmount,
MinStakingAmount: minStakingAmount,
MaxStakingTime: maxStakingTime,
MinStakingTime: minStakingTime,
ConfirmationDepth: confirmationDepth,
}, nil
}
// GetVersionedGlobalParamsByHeight return the parsed versioned global params which
// are applicable at the given BTC btcHeight. If there in no versioned global params
// applicable at the given btcHeight, it will return nil.
func (g *ParsedGlobalParams) GetVersionedGlobalParamsByHeight(btcHeight uint64) *ParsedVersionedGlobalParams {
// Iterate the list in reverse (i.e. decreasing ActivationHeight)
// and identify the first element that has an activation height below
// the specified BTC height.
for i := len(g.Versions) - 1; i >= 0; i-- {
paramsVersion := g.Versions[i]
if paramsVersion.ActivationHeight <= btcHeight {
return paramsVersion
}
}
return nil
}
// FindLastStakingCap finds the last staking cap that is not zero
// it returns zero if not non-zero value is found
func FindLastStakingCap(prevVersions []*VersionedGlobalParams) uint64 {
numPrevVersions := len(prevVersions)
if len(prevVersions) == 0 {
return 0
}
for i := numPrevVersions - 1; i >= 0; i-- {
if prevVersions[i].StakingCap > 0 {
return prevVersions[i].StakingCap
}
}
return 0
}
func NewParsedGlobalParamsFromFile(filePath string) (*ParsedGlobalParams, error) {
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
return NewParsedGlobalParamsFromBytes(data)
}
func NewParsedGlobalParamsFromBytes(data []byte) (*ParsedGlobalParams, error) {
var globalParams GlobalParams
err := json.Unmarshal(data, &globalParams)
if err != nil {
return nil, err
}
parsedGlobalParams, err := ParseGlobalParams(&globalParams)
if err != nil {
return nil, err
}
return parsedGlobalParams, nil
}