-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
426 lines (354 loc) · 11.6 KB
/
base.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package cmds
import (
"context"
"fmt"
"io"
"net/url"
"os"
"strings"
"github.com/pkg/errors"
"github.com/soonkuk/mitum-blocksign/blocksign"
"gopkg.in/yaml.v3"
"github.com/spikeekips/mitum/base"
"github.com/spikeekips/mitum/base/key"
"github.com/spikeekips/mitum/base/state"
"github.com/spikeekips/mitum/isaac"
mitumcmds "github.com/spikeekips/mitum/launch/cmds"
"github.com/spikeekips/mitum/launch/config"
"github.com/spikeekips/mitum/launch/pm"
"github.com/spikeekips/mitum/launch/process"
"github.com/spikeekips/mitum/network"
mongodbstorage "github.com/spikeekips/mitum/storage/mongodb"
"github.com/spikeekips/mitum/util"
jsonenc "github.com/spikeekips/mitum/util/encoder/json"
"github.com/spikeekips/mitum/util/hint"
"github.com/spikeekips/mitum/util/localtime"
"github.com/spikeekips/mitum/util/logging"
"github.com/soonkuk/mitum-blocksign/digest"
currencycmds "github.com/spikeekips/mitum-currency/cmds"
"github.com/spikeekips/mitum-currency/currency"
)
var BaseNodeCommandHooks = func(cmd *BaseNodeCommand) []pm.Hook {
return []pm.Hook{
pm.NewHook(pm.HookPrefixPost, process.ProcessNameEncoders,
process.HookNameAddHinters, process.HookAddHinters(Types, Hinters)).
SetOverride(true),
pm.NewHook(pm.HookPrefixPost, process.ProcessNameConfig,
"load_digest_config", cmd.hookLoadDigestConfig).
SetOverride(true).
SetDir(process.HookNameConfigGenesisOperations, pm.HookDirAfter),
pm.NewHook(pm.HookPrefixPost, process.ProcessNameConfig,
"validate_digest_config", cmd.hookValidateDigestConfig).
SetOverride(true).
SetDir(process.HookNameValidateConfig, pm.HookDirAfter),
pm.NewHook(pm.HookPrefixPost, process.ProcessNameConfig,
process.HookNameConfigVerbose, hookVerboseConfig).
SetOverride(true),
}
}
type BaseNodeCommand struct {
*logging.Logging
}
func NewBaseNodeCommand(l *logging.Logging) *BaseNodeCommand {
return &BaseNodeCommand{Logging: l}
}
func (cmd *BaseNodeCommand) BaseProcesses(ps *pm.Processes) (*pm.Processes, error) {
hooks := BaseNodeCommandHooks(cmd)
for i := range hooks {
if err := hooks[i].Add(ps); err != nil {
return nil, err
}
}
return ps, nil
}
func HookLoadCurrencies(ctx context.Context) (context.Context, error) {
var log *logging.Logging
if err := config.LoadLogContextValue(ctx, &log); err != nil {
return ctx, err
}
log.Log().Debug().Msg("loading currencies from mitum database")
var st *mongodbstorage.Database
if err := LoadDatabaseContextValue(ctx, &st); err != nil {
return ctx, err
}
cp := currency.NewCurrencyPool()
if err := digest.LoadCurrenciesFromDatabase(st, base.NilHeight, func(sta state.State) (bool, error) {
if err := cp.Set(sta); err != nil {
return false, err
}
log.Log().Debug().Interface("currency", sta).Msg("currency loaded from mitum database")
return true, nil
}); err != nil {
return ctx, err
}
return context.WithValue(ctx, ContextValueCurrencyPool, cp), nil
}
func HookInitializeProposalProcessor(ctx context.Context) (context.Context, error) {
var log *logging.Logging
if err := config.LoadLogContextValue(ctx, &log); err != nil {
return ctx, err
}
var suffrage base.Suffrage
if err := process.LoadSuffrageContextValue(ctx, &suffrage); err != nil {
return ctx, err
}
var nodepool *network.Nodepool
if err := process.LoadNodepoolContextValue(ctx, &nodepool); err != nil {
return ctx, err
}
if !suffrage.IsInside(nodepool.LocalNode().Address()) {
log.Log().Debug().Msg("none-suffrage node; proposal processor will not be used")
return ctx, nil
}
var policy *isaac.LocalPolicy
if err := process.LoadPolicyContextValue(ctx, &policy); err != nil {
return ctx, err
}
var cp *currency.CurrencyPool
if err := LoadCurrencyPoolContextValue(ctx, &cp); err != nil {
return ctx, err
}
opr, err := AttachProposalProcessor(policy, nodepool, suffrage, cp)
if err != nil {
return ctx, err
}
return InitializeProposalProcessor(ctx, opr)
}
func AttachProposalProcessor(
policy *isaac.LocalPolicy,
nodepool *network.Nodepool,
suffrage base.Suffrage,
cp *currency.CurrencyPool,
) (*blocksign.OperationProcessor, error) {
opr := blocksign.NewOperationProcessor(cp)
if _, err := opr.SetProcessor(currency.CreateAccounts{}, currency.NewCreateAccountsProcessor(cp)); err != nil {
return nil, err
} else if _, err := opr.SetProcessor(currency.KeyUpdater{}, currency.NewKeyUpdaterProcessor(cp)); err != nil {
return nil, err
} else if _, err := opr.SetProcessor(currency.Transfers{}, currency.NewTransfersProcessor(cp)); err != nil {
return nil, err
} else if _, err := opr.SetProcessor(blocksign.CreateDocuments{}, blocksign.NewCreateDocumentsProcessor(cp)); err != nil {
return nil, err
} else if _, err := opr.SetProcessor(blocksign.TransferDocuments{}, blocksign.NewTransferDocumentsProcessor(cp)); err != nil {
return nil, err
} else if _, err := opr.SetProcessor(blocksign.SignDocuments{}, blocksign.NewSignDocumentsProcessor(cp)); err != nil {
return nil, err
}
threshold, err := base.NewThreshold(uint(len(suffrage.Nodes())), policy.ThresholdRatio())
if err != nil {
return nil, err
}
suffrageNodes := suffrage.Nodes()
pubs := make([]key.Publickey, len(suffrageNodes))
for i := range suffrageNodes {
n, _, found := nodepool.Node(suffrageNodes[i])
if !found {
return nil, errors.Errorf("suffrage node, %q not found in nodepool", suffrageNodes[i])
}
pubs[i] = n.Publickey()
}
if _, err := opr.SetProcessor(currency.CurrencyRegister{},
currency.NewCurrencyRegisterProcessor(cp, pubs, threshold),
); err != nil {
return nil, err
}
if _, err := opr.SetProcessor(currency.CurrencyPolicyUpdater{},
currency.NewCurrencyPolicyUpdaterProcessor(cp, pubs, threshold),
); err != nil {
return nil, err
}
return opr, nil
}
func InitializeProposalProcessor(ctx context.Context, opr *blocksign.OperationProcessor) (context.Context, error) {
var oprs *hint.Hintmap
if err := process.LoadOperationProcessorsContextValue(ctx, &oprs); err != nil {
if !errors.Is(err, util.ContextValueNotFoundError) {
return ctx, err
}
}
if oprs == nil {
oprs = hint.NewHintmap()
ctx = context.WithValue(ctx, process.ContextValueOperationProcessors, oprs)
}
for _, hinter := range []hint.Hinter{
currency.CreateAccounts{},
currency.KeyUpdater{},
currency.Transfers{},
currency.CurrencyPolicyUpdater{},
currency.CurrencyRegister{},
blocksign.CreateDocuments{},
blocksign.TransferDocuments{},
blocksign.SignDocuments{},
} {
if err := oprs.Add(hinter, opr); err != nil {
return ctx, err
}
}
return context.WithValue(ctx, process.ContextValueOperationProcessors, oprs), nil
}
func (*BaseNodeCommand) hookLoadDigestConfig(ctx context.Context) (context.Context, error) {
var source []byte
if err := process.LoadConfigSourceContextValue(ctx, &source); err != nil {
return ctx, err
}
var sourceType string
if err := process.LoadConfigSourceTypeContextValue(ctx, &sourceType); err != nil {
return ctx, err
} else if sourceType != "yaml" {
return ctx, errors.Errorf("unknown source type, %q", sourceType)
}
var m struct {
Digest *currencycmds.DigestDesign
}
if err := yaml.Unmarshal(source, &m); err != nil {
return ctx, err
} else if m.Digest == nil {
return ctx, nil
} else if i, err := m.Digest.Set(ctx); err != nil {
return ctx, err
} else {
ctx = i
}
return context.WithValue(ctx, ContextValueDigestDesign, *m.Digest), nil
}
func (cmd *BaseNodeCommand) hookValidateDigestConfig(ctx context.Context) (context.Context, error) {
var conf config.LocalNode
if err := config.LoadConfigContextValue(ctx, &conf); err != nil {
return ctx, err
}
var design currencycmds.DigestDesign
if err := LoadDigestDesignContextValue(ctx, &design); err != nil {
if errors.Is(err, util.ContextValueNotFoundError) {
return ctx, nil
}
return ctx, err
}
if design.Network() != nil {
i, err := cmd.validateDigestConfigNetwork(ctx, conf, design)
if err != nil {
return ctx, err
}
ctx = i
}
return ctx, nil
}
func (cmd *BaseNodeCommand) validateDigestConfigNetwork(
ctx context.Context,
conf config.LocalNode,
design currencycmds.DigestDesign,
) (context.Context, error) {
if design.Network().ConnInfo() == nil {
return ctx, errors.Errorf("digest network url is missing")
}
a := design.Network().Bind()
if a == nil {
return ctx, errors.Errorf("digest network bind is missing")
} else if sameBind(a, conf.Network().Bind()) {
return ctx, errors.Errorf("digest bind same with mitum bind: %q", a.String())
}
if len(design.Network().Certs()) < 1 && design.Network().Bind().Scheme == "https" {
if h := design.Network().Bind().Hostname(); !strings.HasPrefix(h, "127.") && h != "localhost" {
return ctx, errors.Errorf("missing certificates for https")
}
if priv, err := util.GenerateED25519Privatekey(); err != nil {
return ctx, err
} else if ct, err := util.GenerateTLSCerts("localhost", priv); err != nil {
return ctx, err
} else if err := design.Network().SetCerts(ct); err != nil {
return ctx, err
}
}
if design.Network().RateLimit() != nil {
i, err := cmd.validateDigestConfigNetworkRateLimit(ctx, design)
if err != nil {
return i, err
}
ctx = i
}
return ctx, nil
}
func (*BaseNodeCommand) validateDigestConfigNetworkRateLimit(
ctx context.Context,
design currencycmds.DigestDesign,
) (context.Context, error) {
rcc := config.NewRateLimitChecker(ctx, design.Network().RateLimit(), nil)
if err := util.NewChecker("config-ratelimit-checker", []util.CheckerFunc{
rcc.Initialize,
rcc.Check,
}).Check(); err != nil {
if !errors.Is(err, util.IgnoreError) {
return ctx, err
}
}
return ctx, design.Network().SetRateLimit(rcc.Config())
}
func isLocal(u *url.URL) bool {
h := u.Hostname()
return h == "localhost" || strings.HasPrefix(h, "127.") || strings.HasPrefix(h, "0.")
}
func sameBind(a, b *url.URL) bool {
if a.Scheme != b.Scheme || a.Port() != b.Port() {
return false
}
ha := a.Hostname()
if isLocal(a) {
ha = "127.0.0.1"
}
hb := b.Hostname()
if isLocal(b) {
hb = "127.0.0.1"
}
return ha == hb
}
type BaseCommand struct {
*mitumcmds.BaseCommand
Out io.Writer `kong:"-"`
}
func NewBaseCommand(name string) *BaseCommand {
return &BaseCommand{
BaseCommand: mitumcmds.NewBaseCommand(name),
Out: os.Stdout,
}
}
func (co *BaseCommand) print(f string, a ...interface{}) {
_, _ = fmt.Fprintf(co.Out, f, a...)
_, _ = fmt.Fprintln(co.Out)
}
func hookVerboseConfig(ctx context.Context) (context.Context, error) {
var conf config.LocalNode
if err := config.LoadConfigContextValue(ctx, &conf); err != nil {
return ctx, err
}
var log *logging.Logging
if err := config.LoadLogContextValue(ctx, &log); err != nil {
return ctx, err
}
var dd currencycmds.DigestDesign
if err := LoadDigestDesignContextValue(ctx, &dd); err != nil {
if !errors.Is(err, util.ContextValueNotFoundError) {
return ctx, err
}
}
var m map[string]interface{}
if b, err := jsonenc.Marshal(conf); err != nil {
return ctx, err
} else if err := jsonenc.Unmarshal(b, &m); err != nil {
return ctx, err
}
m["digest"] = dd
log.Log().Debug().Interface("config", m).Msg("config loaded")
return ctx, nil
}
type OperationFlags struct {
Privatekey currencycmds.PrivatekeyFlag `arg:"" name:"privatekey" help:"privatekey to sign operation" required:"true"`
Token string `help:"token for operation" optional:""`
NetworkID mitumcmds.NetworkIDFlag `name:"network-id" help:"network-id" required:"true"`
Memo string `name:"memo" help:"memo"`
Pretty bool `name:"pretty" help:"pretty format"`
}
func (op *OperationFlags) IsValid([]byte) error {
if len(op.Token) < 1 {
op.Token = localtime.String(localtime.UTCNow())
}
return nil
}