forked from decred/politeia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pi.go
381 lines (351 loc) · 11.9 KB
/
pi.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
371
372
373
374
375
376
377
378
379
380
381
// Copyright (c) 2020-2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package pi
import (
"container/list"
"encoding/json"
"os"
"path/filepath"
"regexp"
"strconv"
"github.com/decred/politeia/politeiad/api/v1/identity"
backend "github.com/decred/politeia/politeiad/backendv2"
"github.com/decred/politeia/politeiad/backendv2/tstorebe/plugins"
"github.com/decred/politeia/politeiad/plugins/pi"
"github.com/decred/politeia/util"
"github.com/pkg/errors"
)
var (
_ plugins.PluginClient = (*piPlugin)(nil)
)
// piPlugin is the tstore backend implementation of the pi plugin. The pi
// plugin extends a record with functionality specific to the decred proposal
// system.
//
// piPlugin satisfies the plugins PluginClient interface.
type piPlugin struct {
backend backend.Backend
tstore plugins.TstoreClient
// statuses holds proposal statuses and various proposal metadata in an
// in-memory cache to improve the performance of determining the proposal
// statuses at runtime.
statuses proposalStatuses
// dataDir is the pi plugin data directory. The only data that is
// stored here is cached data that can be re-created at any time
// by walking the trillian trees.
dataDir string
// identity contains the full identity that the plugin uses to
// create receipts, i.e. signatures of user provided data that
// prove the backend received and processed a plugin command.
identity *identity.FullIdentity
// Plugin settings
textFileCountMax uint32
textFileSizeMax uint32 // In bytes
imageFileCountMax uint32
imageFileSizeMax uint32 // In bytes
titleSupportedChars string // JSON encoded []string
titleLengthMin uint32 // In characters
titleLengthMax uint32 // In characters
titleRegexp *regexp.Regexp
proposalAmountMin uint64 // In cents
proposalAmountMax uint64 // In cents
proposalStartDateMin int64 // Seconds from current time
proposalEndDateMax int64 // Seconds from current time
proposalDomainsEncoded string // JSON encoded []string
proposalDomains map[string]struct{}
billingStatusChangesMax uint32
summariesPageSize uint32
billingStatusChangesPageSize uint32
}
// Setup performs any plugin setup that is required.
//
// This function satisfies the plugins PluginClient interface.
func (p *piPlugin) Setup() error {
log.Tracef("pi Setup")
return nil
}
// Cmd executes a plugin command.
//
// This function satisfies the plugins PluginClient interface.
func (p *piPlugin) Cmd(token []byte, cmd, payload string) (string, error) {
log.Tracef("pi Cmd: %x %v %v", token, cmd, payload)
switch cmd {
case pi.CmdSetBillingStatus:
return p.cmdSetBillingStatus(token, payload)
case pi.CmdSummary:
return p.cmdSummary(token)
case pi.CmdBillingStatusChanges:
return p.cmdBillingStatusChanges(token)
}
return "", backend.ErrPluginCmdInvalid
}
// Hook executes a plugin hook.
//
// This function satisfies the plugins PluginClient interface.
func (p *piPlugin) Hook(h plugins.HookT, payload string) error {
log.Tracef("pi Hook: %v", plugins.Hooks[h])
switch h {
case plugins.HookTypeNewRecordPre:
return p.hookNewRecordPre(payload)
case plugins.HookTypeEditRecordPre:
return p.hookEditRecordPre(payload)
case plugins.HookTypePluginPre:
return p.hookPluginPre(payload)
}
return nil
}
// Fsck performs a plugin file system check. The plugin is provided with the
// tokens for all records in the backend.
//
// This function satisfies the plugins PluginClient interface.
func (p *piPlugin) Fsck(tokens [][]byte) error {
log.Tracef("pi Fsck")
return nil
}
// Settings returns the plugin's settings.
//
// This function satisfies the plugins PluginClient interface.
func (p *piPlugin) Settings() []backend.PluginSetting {
log.Tracef("pi Settings")
return []backend.PluginSetting{
{
Key: pi.SettingKeyTextFileSizeMax,
Value: strconv.FormatUint(uint64(p.textFileSizeMax), 10),
},
{
Key: pi.SettingKeyImageFileCountMax,
Value: strconv.FormatUint(uint64(p.imageFileCountMax), 10),
},
{
Key: pi.SettingKeyImageFileCountMax,
Value: strconv.FormatUint(uint64(p.imageFileCountMax), 10),
},
{
Key: pi.SettingKeyImageFileSizeMax,
Value: strconv.FormatUint(uint64(p.imageFileSizeMax), 10),
},
{
Key: pi.SettingKeyTitleLengthMin,
Value: strconv.FormatUint(uint64(p.titleLengthMin), 10),
},
{
Key: pi.SettingKeyTitleLengthMax,
Value: strconv.FormatUint(uint64(p.titleLengthMax), 10),
},
{
Key: pi.SettingKeyTitleSupportedChars,
Value: p.titleSupportedChars,
},
{
Key: pi.SettingKeyProposalAmountMin,
Value: strconv.FormatUint(p.proposalAmountMin, 10),
},
{
Key: pi.SettingKeyProposalAmountMax,
Value: strconv.FormatUint(p.proposalAmountMax, 10),
},
{
Key: pi.SettingKeyProposalStartDateMin,
Value: strconv.FormatInt(p.proposalStartDateMin, 10),
},
{
Key: pi.SettingKeyProposalEndDateMax,
Value: strconv.FormatInt(p.proposalEndDateMax, 10),
},
{
Key: pi.SettingKeyProposalDomains,
Value: p.proposalDomainsEncoded,
},
{
Key: pi.SettingKeyBillingStatusChangesMax,
Value: strconv.FormatUint(uint64(p.billingStatusChangesMax), 10),
},
{
Key: pi.SettingKeySummariesPageSize,
Value: strconv.FormatUint(uint64(p.summariesPageSize), 10),
},
{
Key: pi.SettingKeyBillingStatusChangesPageSize,
Value: strconv.FormatUint(uint64(p.billingStatusChangesPageSize), 10),
},
}
}
// New returns a new piPlugin.
func New(backend backend.Backend, tstore plugins.TstoreClient, settings []backend.PluginSetting, dataDir string, id *identity.FullIdentity) (*piPlugin, error) {
// Create plugin data directory
dataDir = filepath.Join(dataDir, pi.PluginID)
err := os.MkdirAll(dataDir, 0700)
if err != nil {
return nil, err
}
// Setup plugin setting default values
var (
textFileSizeMax = pi.SettingTextFileSizeMax
imageFileCountMax = pi.SettingImageFileCountMax
imageFileSizeMax = pi.SettingImageFileSizeMax
titleLengthMin = pi.SettingTitleLengthMin
titleLengthMax = pi.SettingTitleLengthMax
titleSupportedChars = pi.SettingTitleSupportedChars
amountMin = pi.SettingProposalAmountMin
amountMax = pi.SettingProposalAmountMax
startDateMin = pi.SettingProposalStartDateMin
endDateMax = pi.SettingProposalEndDateMax
domains = pi.SettingProposalDomains
billingStatusChangesMax = pi.SettingBillingStatusChangesMax
summariesPageSize = pi.SettingSummariesPageSize
billingStatusChangesPageSize = pi.SettingBillingStatusChangesPageSize
)
// Override defaults with any passed in settings
for _, v := range settings {
switch v.Key {
case pi.SettingKeyTextFileSizeMax:
u, err := strconv.ParseUint(v.Value, 10, 64)
if err != nil {
return nil, errors.Errorf("invalid plugin setting %v '%v': %v",
v.Key, v.Value, err)
}
textFileSizeMax = uint32(u)
case pi.SettingKeyImageFileCountMax:
u, err := strconv.ParseUint(v.Value, 10, 64)
if err != nil {
return nil, errors.Errorf("invalid plugin setting %v '%v': %v",
v.Key, v.Value, err)
}
imageFileCountMax = uint32(u)
case pi.SettingKeyImageFileSizeMax:
u, err := strconv.ParseUint(v.Value, 10, 64)
if err != nil {
return nil, errors.Errorf("invalid plugin setting %v '%v': %v",
v.Key, v.Value, err)
}
imageFileSizeMax = uint32(u)
case pi.SettingKeyTitleLengthMin:
u, err := strconv.ParseUint(v.Value, 10, 64)
if err != nil {
return nil, errors.Errorf("invalid plugin setting %v '%v': %v",
v.Key, v.Value, err)
}
titleLengthMin = uint32(u)
case pi.SettingKeyTitleLengthMax:
u, err := strconv.ParseUint(v.Value, 10, 64)
if err != nil {
return nil, errors.Errorf("invalid plugin setting %v '%v': %v",
v.Key, v.Value, err)
}
titleLengthMax = uint32(u)
case pi.SettingKeyTitleSupportedChars:
err := json.Unmarshal([]byte(v.Value), &titleSupportedChars)
if err != nil {
return nil, errors.Errorf("invalid plugin setting %v '%v': %v",
v.Key, v.Value, err)
}
case pi.SettingKeyProposalAmountMin:
u, err := strconv.ParseUint(v.Value, 10, 64)
if err != nil {
return nil, errors.Errorf("invalid plugin setting %v '%v': %v",
v.Key, v.Value, err)
}
amountMin = u
case pi.SettingKeyProposalAmountMax:
u, err := strconv.ParseUint(v.Value, 10, 64)
if err != nil {
return nil, errors.Errorf("invalid plugin setting %v '%v': %v",
v.Key, v.Value, err)
}
amountMax = u
case pi.SettingKeyProposalEndDateMax:
u, err := strconv.ParseInt(v.Value, 10, 64)
if err != nil {
return nil, errors.Errorf("invalid plugin setting %v '%v': %v",
v.Key, v.Value, err)
}
// Ensure provided max end date is not in the past
if u < 0 {
return nil, errors.Errorf("invalid plugin setting %v '%v': "+
"must be in the future", v.Key, v.Value)
}
endDateMax = u
case pi.SettingKeyProposalDomains:
err := json.Unmarshal([]byte(v.Value), &domains)
if err != nil {
return nil, errors.Errorf("invalid plugin setting %v '%v': %v",
v.Key, v.Value, err)
}
case pi.SettingKeyBillingStatusChangesMax:
u, err := strconv.ParseUint(v.Value, 10, 64)
if err != nil {
return nil, errors.Errorf("invalid plugin setting %v '%v': %v",
v.Key, v.Value, err)
}
billingStatusChangesMax = uint32(u)
case pi.SettingKeySummariesPageSize:
u, err := strconv.ParseUint(v.Value, 10, 64)
if err != nil {
return nil, errors.Errorf("invalid plugin setting %v '%v': %v",
v.Key, v.Value, err)
}
summariesPageSize = uint32(u)
case pi.SettingKeyBillingStatusChangesPageSize:
u, err := strconv.ParseUint(v.Value, 10, 64)
if err != nil {
return nil, errors.Errorf("invalid plugin setting %v '%v': %v",
v.Key, v.Value, err)
}
billingStatusChangesPageSize = uint32(u)
default:
return nil, errors.Errorf("invalid plugin setting: %v", v.Key)
}
}
// Setup title regex
rexp, err := util.Regexp(titleSupportedChars, uint64(titleLengthMin),
uint64(titleLengthMax))
if err != nil {
return nil, errors.Errorf("proposal name regexp: %v", err)
}
// Encode the title supported chars so that they
// can be returned as a plugin setting string.
b, err := json.Marshal(titleSupportedChars)
if err != nil {
return nil, err
}
titleSupportedCharsString := string(b)
// Encode the proposal domains so that they can be returned as a
// plugin setting string.
b, err = json.Marshal(domains)
if err != nil {
return nil, err
}
domainsString := string(b)
// Translate domains slice to a Map[string]string.
domainsMap := make(map[string]struct{}, len(domains))
for _, d := range domains {
domainsMap[d] = struct{}{}
}
return &piPlugin{
dataDir: dataDir,
identity: id,
backend: backend,
textFileSizeMax: textFileSizeMax,
tstore: tstore,
imageFileCountMax: imageFileCountMax,
imageFileSizeMax: imageFileSizeMax,
titleLengthMin: titleLengthMin,
titleLengthMax: titleLengthMax,
titleSupportedChars: titleSupportedCharsString,
titleRegexp: rexp,
proposalAmountMin: amountMin,
proposalAmountMax: amountMax,
proposalStartDateMin: startDateMin,
proposalEndDateMax: endDateMax,
proposalDomainsEncoded: domainsString,
proposalDomains: domainsMap,
billingStatusChangesMax: billingStatusChangesMax,
summariesPageSize: summariesPageSize,
billingStatusChangesPageSize: billingStatusChangesPageSize,
statuses: proposalStatuses{
data: make(map[string]*statusEntry, statusesCacheLimit),
entries: list.New(),
},
}, nil
}