-
Notifications
You must be signed in to change notification settings - Fork 141
/
params.go
98 lines (77 loc) · 2.24 KB
/
params.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
package types
import (
"encoding/hex"
fmt "fmt"
txformat "github.com/babylonchain/babylon/btctxformatter"
)
const (
DefaultBtcConfirmationDepth uint64 = 10
DefaultCheckpointFinalizationTimeout uint64 = 100
DefaultCheckpointTag = "01020304"
)
// NewParams creates a new Params instance
func NewParams(btcConfirmationDepth uint64, checkpointFinalizationTimeout uint64, checkpointTag string) Params {
return Params{
BtcConfirmationDepth: btcConfirmationDepth,
CheckpointFinalizationTimeout: checkpointFinalizationTimeout,
CheckpointTag: checkpointTag,
}
}
// DefaultParams returns a default set of parameters
func DefaultParams() Params {
return NewParams(
DefaultBtcConfirmationDepth,
DefaultCheckpointFinalizationTimeout,
DefaultCheckpointTag,
)
}
// Validate validates the set of params
func (p Params) Validate() error {
if err := validateBtcConfirmationDepth(p.BtcConfirmationDepth); err != nil {
return err
}
if err := validateCheckpointFinalizationTimeout(p.CheckpointFinalizationTimeout); err != nil {
return err
}
if err := validateCheckpointTag(p.CheckpointTag); err != nil {
return err
}
if p.BtcConfirmationDepth >= p.CheckpointFinalizationTimeout {
return fmt.Errorf("BtcConfirmationDepth should be smaller than CheckpointFinalizationTimeout")
}
return nil
}
func validateBtcConfirmationDepth(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v <= 0 {
return fmt.Errorf("BtcConfirmationDepth must be positive: %d", v)
}
return nil
}
func validateCheckpointFinalizationTimeout(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v <= 0 {
return fmt.Errorf("CheckpointFinalizationTimeout must be positive: %d", v)
}
return nil
}
func validateCheckpointTag(i interface{}) error {
t, ok := i.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
decoded, err := hex.DecodeString(t)
if err != nil {
return fmt.Errorf("checkpoint tag should be in valid hex format")
}
if len(decoded) != txformat.TagLength {
return fmt.Errorf("checkpoint tag should have exactly %d bytes", txformat.TagLength)
}
return nil
}