-
Notifications
You must be signed in to change notification settings - Fork 211
/
post_types.go
144 lines (117 loc) · 2.94 KB
/
post_types.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
package activation
import (
"encoding/hex"
"fmt"
"strconv"
"github.com/spacemeshos/post/config"
)
type PowDifficulty [32]byte
func (d PowDifficulty) String() string {
return hex.EncodeToString(d[:])
}
// Set implements pflag.Value.Set.
func (f *PowDifficulty) Set(value string) error {
return f.UnmarshalText([]byte(value))
}
// Type implements pflag.Value.Type.
func (PowDifficulty) Type() string {
return "PowDifficulty"
}
func (d *PowDifficulty) UnmarshalText(text []byte) error {
decodedLen := hex.DecodedLen(len(text))
if decodedLen != 32 {
return fmt.Errorf("expected 32 bytes, got %d", decodedLen)
}
var dst [32]byte
if _, err := hex.Decode(dst[:], text); err != nil {
return err
}
*d = PowDifficulty(dst)
return nil
}
type PostProviderID struct {
value *uint32
}
// String implements pflag.Value.String.
func (id PostProviderID) String() string {
if id.value == nil {
return ""
}
return strconv.FormatUint(uint64(*id.value), 10)
}
// Type implements pflag.Value.Type.
func (PostProviderID) Type() string {
return "PostProviderID"
}
// Set implements pflag.Value.Set.
func (id *PostProviderID) Set(value string) error {
if len(value) == 0 {
id.value = nil
return nil
}
i, err := strconv.ParseUint(value, 10, 32)
if err != nil {
return fmt.Errorf("failed to parse PoST Provider ID (\"%s\"): %w", value, err)
}
id.value = new(uint32)
*id.value = uint32(i)
return nil
}
// SetInt64 sets the value of the PostProviderID to the given int64.
func (id *PostProviderID) SetUint32(value uint32) {
id.value = &value
}
// Value returns the value of the PostProviderID as a pointer to uint32.
func (id *PostProviderID) Value() *uint32 {
return id.value
}
type PostPowFlags config.PowFlags
// String implements pflag.Value.String.
func (f PostPowFlags) String() string {
return fmt.Sprintf("%d", f)
}
// Type implements pflag.Value.Type.
func (PostPowFlags) Type() string {
return "PostPowFlags"
}
// Set implements pflag.Value.Set.
func (f *PostPowFlags) Set(value string) error {
if len(value) == 0 {
*f = 0
return nil
}
i, err := strconv.ParseUint(value, 10, 32)
if err != nil {
return fmt.Errorf("failed to parse PoST PoW flags (\"%s\"): %w", value, err)
}
*f = PostPowFlags(i)
return nil
}
func (f *PostPowFlags) Value() config.PowFlags {
return config.PowFlags(*f)
}
type PostRandomXMode string
const (
PostRandomXModeFast PostRandomXMode = "fast"
PostRandomXModeLight PostRandomXMode = "light"
)
// String implements pflag.Value.String.
func (m PostRandomXMode) String() string {
return string(m)
}
// Type implements pflag.Value.Type.
func (PostRandomXMode) Type() string {
return "PostRandomXMode"
}
// Set implements pflag.Value.Set.
func (m *PostRandomXMode) Set(value string) error {
switch value {
case string(PostRandomXModeFast):
*m = PostRandomXModeFast
case string(PostRandomXModeLight):
*m = PostRandomXModeLight
default:
return fmt.Errorf("invalid PoST RandomX mode (\"%s\")", value)
}
return nil
}