-
Notifications
You must be signed in to change notification settings - Fork 2
/
import.go
561 lines (469 loc) · 13.9 KB
/
import.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
package cmds
import (
"bytes"
"context"
"io"
"net/url"
"os"
"path/filepath"
"github.com/ProtoconNet/mitum2/util/ps"
"github.com/ProtoconNet/mitum2/util/valuehash"
"github.com/ProtoconNet/mitum2/base"
"github.com/ProtoconNet/mitum2/isaac"
isaacblock "github.com/ProtoconNet/mitum2/isaac/block"
"github.com/ProtoconNet/mitum2/launch"
"github.com/ProtoconNet/mitum2/util"
"github.com/ProtoconNet/mitum2/util/encoder"
"github.com/ProtoconNet/mitum2/util/logging"
"github.com/pkg/errors"
"github.com/rs/zerolog"
)
var (
pNamePreImportBlocks = ps.Name("cmd-pre-import-blocks")
pNameImportBlocks = ps.Name("cmd-import-blocks")
)
type ImportCommand struct { //nolint:govet //...
// revive:disable:line-length-limit
launch.DesignFlag
Source string `arg:"" name:"source directory" help:"block data directory to import" type:"existingdir"`
HeightRange launch.RangeFlag `name:"range" help:"<from>-<to>" default:""`
launch.PrivatekeyFlags
Do bool `name:"do" help:"really do import"`
CacheDirectory string `name:"cache-directory" help:"directory for remote block item file"`
log *zerolog.Logger
launch.DevFlags `embed:"" prefix:"dev."`
fromHeight base.Height
toHeight base.Height
lastHeight base.Height
prevblockmap base.BlockMap
sourceReaders *isaac.BlockItemReaders
toReaders *isaac.BlockItemReaders
importedReaders *isaac.BlockItemReaders
// revive:enable:line-length-limit
}
func (cmd *ImportCommand) Run(pctx context.Context) error {
var log *logging.Logging
if err := util.LoadFromContextOK(pctx, launch.LoggingContextKey, &log); err != nil {
return err
}
cmd.log = log.Log()
if err := cmd.prepare(); err != nil {
return err
}
defer func() {
_ = os.RemoveAll(cmd.CacheDirectory)
}()
log.Log().Debug().
Interface("design", cmd.DesignFlag).
Interface("privatekey", cmd.PrivatekeyFlags).
Interface("dev", cmd.DevFlags).
Str("source", cmd.Source).
Interface("from_height", cmd.fromHeight).
Interface("to_height", cmd.toHeight).
Bool("do", cmd.Do).
Str("cache_directory", cmd.CacheDirectory).
Msg("flags")
nctx := util.ContextWithValues(pctx, map[util.ContextKey]interface{}{
launch.DesignFlagContextKey: cmd.DesignFlag,
launch.DevFlagsContextKey: cmd.DevFlags,
launch.PrivatekeyContextKey: string(cmd.PrivatekeyFlags.Flag.Body()),
})
pps := DefaultImportPS()
_ = pps.SetLogging(log)
_ = pps.AddOK(pNameImportBlocks, cmd.importBlocks, nil, launch.PNameStorage)
_ = pps.POK(pNameImportBlocks).
PreAddOK(pNamePreImportBlocks, cmd.preImportBlocks)
cmd.log.Debug().Interface("process", pps.Verbose()).Msg("process ready")
nctx, err := pps.Run(nctx)
defer func() {
cmd.log.Debug().Interface("process", pps.Verbose()).Msg("process will be closed")
if _, err = pps.Close(nctx); err != nil {
cmd.log.Error().Err(err).Msg("failed to close")
}
}()
return err
}
func (cmd *ImportCommand) prepare() error {
cmd.fromHeight, cmd.toHeight = base.NilHeight, base.NilHeight
if h := cmd.HeightRange.From(); h != nil {
cmd.fromHeight = base.Height(*h)
if err := cmd.fromHeight.IsValid(nil); err != nil {
return errors.WithMessagef(err, "invalid from height; from=%d", *h)
}
}
if h := cmd.HeightRange.To(); h != nil {
cmd.toHeight = base.Height(*h)
if err := cmd.toHeight.IsValid(nil); err != nil {
return errors.WithMessagef(err, "invalid to height; to=%d", *h)
}
if cmd.fromHeight > cmd.toHeight {
return errors.Errorf("from height is higher than to; from=%d to=%d", cmd.fromHeight, cmd.toHeight)
}
}
var checkCacheDirectory func() error
switch {
case len(cmd.CacheDirectory) < 1:
checkCacheDirectory = func() error {
i, err := os.MkdirTemp("", "mitum-import-")
if err != nil {
return errors.WithStack(err)
}
cmd.CacheDirectory = i
return nil
}
default:
checkCacheDirectory = func() error {
switch fi, err := os.Stat(cmd.CacheDirectory); {
case os.IsNotExist(err):
if err = os.MkdirAll(filepath.Clean(cmd.CacheDirectory), 0o700); err != nil {
return errors.WithStack(err)
}
case err != nil:
return errors.WithStack(err)
case !fi.IsDir():
return errors.Errorf("cache directory is not directory")
}
return nil
}
}
return checkCacheDirectory()
}
func (cmd *ImportCommand) preImportBlocks(pctx context.Context) (context.Context, error) {
var design launch.NodeDesign
var isaacparams *isaac.Params
var db isaac.Database
var newReaders func(context.Context, string, *isaac.BlockItemReadersArgs) (*isaac.BlockItemReaders, error)
var fromRemotes isaac.RemotesBlockItemReadFunc
if err := util.LoadFromContextOK(pctx,
launch.DesignContextKey, &design,
launch.ISAACParamsContextKey, &isaacparams,
launch.CenterDatabaseContextKey, &db,
launch.NewBlockItemReadersFuncContextKey, &newReaders,
launch.RemotesBlockItemReaderFuncContextKey, &fromRemotes,
); err != nil {
return pctx, err
}
switch i, err := newReaders(pctx, cmd.Source, nil); {
case err != nil:
return pctx, err
default:
cmd.sourceReaders = i
}
switch i, err := newReaders(pctx, launch.LocalFSDataDirectory(design.Storage.Base), nil); {
case err != nil:
return pctx, err
default:
cmd.toReaders = i
}
switch i, err := newReaders(pctx, launch.LocalFSDataDirectory(design.Storage.Base), nil); {
case err != nil:
return pctx, err
default:
cmd.importedReaders = i
}
if cmd.fromHeight > base.GenesisHeight {
switch i, found, err := db.BlockMap(cmd.fromHeight - 1); {
case err != nil:
return pctx, err
case !found:
return pctx, errors.Errorf("previous blockmap not found for from height, %d", cmd.fromHeight-1)
default:
cmd.prevblockmap = i
}
}
switch i, err := cmd.checkHeights(pctx); {
case err != nil:
return pctx, err
default:
cmd.lastHeight = i
}
if err := cmd.validateSourceBlocks(
cmd.loadItemFile(cmd.sourceReaders, fromRemotes, cmd.Do),
cmd.lastHeight,
isaacparams.NetworkID(),
); err != nil {
return pctx, err
}
return pctx, nil
}
func (cmd *ImportCommand) importBlocks(pctx context.Context) (context.Context, error) {
if !cmd.Do {
cmd.log.Debug().Msg("to import really blocks, `--do`")
return pctx, nil
}
e := util.StringError("import blocks")
var encs *encoder.Encoders
var isaacparams *isaac.Params
var db isaac.Database
var fromRemotes isaac.RemotesBlockItemReadFunc
if err := util.LoadFromContextOK(pctx,
launch.EncodersContextKey, &encs,
launch.ISAACParamsContextKey, &isaacparams,
launch.CenterDatabaseContextKey, &db,
launch.RemotesBlockItemReaderFuncContextKey, &fromRemotes,
); err != nil {
return pctx, e.Wrap(err)
}
if err := launch.ImportBlocks(
cmd.sourceReaders,
func(ctx context.Context,
uri url.URL,
compressFormat string,
callback func(_ io.Reader, compressFormat string) error,
) (known, found bool, _ error) {
switch found, err := cmd.readTempRemoteItemFile(uri, compressFormat, callback); {
case err != nil:
return false, false, err
case found:
return true, true, nil
default:
return fromRemotes(ctx, uri, compressFormat, callback)
}
},
cmd.toReaders,
cmd.fromHeight,
cmd.lastHeight,
encs,
db,
isaacparams,
); err != nil {
return pctx, e.Wrap(err)
}
if err := cmd.validateImported(cmd.importedReaders, cmd.lastHeight, isaacparams, db); err != nil {
return pctx, e.Wrap(err)
}
return pctx, nil
}
func (cmd *ImportCommand) checkHeights(pctx context.Context) (base.Height, error) {
var last base.Height
var isaacparams *isaac.Params
var db isaac.Database
if err := util.LoadFromContextOK(pctx,
launch.ISAACParamsContextKey, &isaacparams,
launch.CenterDatabaseContextKey, &db,
); err != nil {
return last, err
}
switch fromHeight, toHeight, i, err := checkLastHeight(pctx, cmd.Source, cmd.fromHeight, cmd.toHeight); {
case err != nil:
return last, err
default:
cmd.fromHeight = fromHeight
cmd.toHeight = toHeight
last = i
cmd.log.Debug().
Interface("from_height", cmd.fromHeight).
Interface("to_height", cmd.toHeight).
Interface("last", last).
Msg("heights checked")
}
switch i, found, err := db.LastBlockMap(); {
case err != nil:
return last, err
case !found:
case cmd.fromHeight < base.GenesisHeight:
cmd.fromHeight = i.Manifest().Height() + 1
case i.Manifest().Height() != cmd.fromHeight-1:
return last, errors.Errorf(
"from height should be same with last height + 1; from=%d last=%d", cmd.fromHeight, i.Manifest().Height())
}
cmd.log.Debug().
Interface("from_height", cmd.fromHeight).
Interface("to_height", cmd.toHeight).
Interface("last", last).
Msg("heights checked")
return last, nil
}
func (cmd *ImportCommand) validateSourceBlocks(
itemf isaac.BlockItemReadersItemFunc,
last base.Height,
networkID base.NetworkID,
) error {
// NOTE if cmd.Do is true, save the remote item files in temp directory.
e := util.StringError("validate source blocks")
d := last - cmd.fromHeight
if err := util.BatchWork(
context.Background(),
d.Int64()+1,
333, //nolint:gomnd //...
func(context.Context, uint64) error {
return nil
},
func(_ context.Context, i, _ uint64) error {
height := base.Height(int64(i) + cmd.fromHeight.Int64())
return isaacblock.IsValidBlockFromLocalFS(
itemf,
height,
networkID,
nil, nil, nil,
)
},
); err != nil {
return e.Wrap(err)
}
cmd.log.Debug().Msg("source blocks validated")
return nil
}
func (cmd *ImportCommand) loadItemFile( //revive:disable-line:flag-parameter
sourceReaders *isaac.BlockItemReaders,
fromRemotes isaac.RemotesBlockItemReadFunc,
saveTemp bool,
) isaac.BlockItemReadersItemFunc {
return isaac.BlockItemReadersItemFuncWithRemote(
sourceReaders,
fromRemotes,
func(itemfile base.BlockItemFile, ir isaac.BlockItemReader, f isaac.BlockItemReaderCallbackFunc) error {
if isaac.IsInLocalBlockItemFile(itemfile.URI()) {
return f(ir)
}
if !saveTemp {
return f(ir)
}
buf := bytes.NewBuffer(nil)
defer func() {
buf.Reset()
}()
if _, err := ir.Reader().Tee(buf, nil); err != nil {
return err
}
if err := f(ir); err != nil {
return err
}
return cmd.writeTempRemoteItemFile(itemfile.URI(), itemfile.CompressFormat(), buf)
},
)(context.Background())
}
func (cmd *ImportCommand) validateImported(
importedReaders *isaac.BlockItemReaders,
last base.Height,
params *isaac.Params,
db isaac.Database,
) error {
e := util.StringError("validate imported")
if err := isaacblock.IsValidBlocksFromStorage(
importedReaders.Item, cmd.fromHeight, last, params.NetworkID(), db, nil); err != nil {
return e.Wrap(err)
}
cmd.log.Debug().Msg("imported blocks validated")
return nil
}
func (cmd *ImportCommand) tempRemoteItemFileName(uri url.URL, compressFormat string) string {
h := valuehash.NewSHA256(util.ConcatBytesSlice(
[]byte((&uri).String()),
[]byte(compressFormat),
))
return filepath.Join(cmd.CacheDirectory, util.DelmSplitStrings(h.String(), "/", 32)) //nolint:gomnd //...
}
func (cmd *ImportCommand) writeTempRemoteItemFile(
uri url.URL,
compressFormat string,
r io.Reader,
) error {
p := cmd.tempRemoteItemFileName(uri, compressFormat)
switch fi, err := os.Stat(p); {
case os.IsNotExist(err):
case err != nil:
return errors.WithStack(err)
case fi.IsDir():
return errors.Errorf("directory")
default:
return nil
}
switch err := os.MkdirAll(filepath.Dir(p), 0o700); {
case os.IsExist(err):
case err != nil:
return errors.WithStack(err)
}
switch f, err := os.OpenFile(p, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600); {
case err != nil:
return errors.WithStack(err)
default:
defer func() {
_ = f.Close()
}()
if _, err := io.Copy(f, r); err != nil {
return errors.WithStack(err)
}
}
return nil
}
func (cmd *ImportCommand) readTempRemoteItemFile(
uri url.URL,
compressFormat string,
f func(io.Reader, string) error,
) (bool, error) {
switch i, err := os.Open(cmd.tempRemoteItemFileName(uri, compressFormat)); {
case os.IsNotExist(err):
return false, nil
case err != nil:
return false, errors.WithStack(err)
default:
defer func() {
_ = i.Close()
}()
return true, f(i, compressFormat)
}
}
func checkLastHeight(pctx context.Context, root string, fromHeight, toHeight base.Height) (
base.Height,
base.Height,
base.Height,
error,
) {
var last base.Height
var encs *encoder.Encoders
var isaacparams *isaac.Params
var db isaac.Database
if err := util.LoadFromContextOK(pctx,
launch.EncodersContextKey, &encs,
launch.ISAACParamsContextKey, &isaacparams,
launch.CenterDatabaseContextKey, &db,
); err != nil {
return fromHeight, toHeight, last, err
}
lastlocalheight := base.NilHeight
switch i, found, err := db.LastBlockMap(); {
case err != nil:
return fromHeight, toHeight, last, err
case !found:
default:
lastlocalheight = i.Manifest().Height()
}
if toHeight > base.NilHeight && toHeight > lastlocalheight {
return fromHeight, toHeight, last, errors.Errorf(
"to height higher than last; to=%d last=%d", toHeight, lastlocalheight)
}
nfromHeight := fromHeight
switch {
case nfromHeight < base.GenesisHeight:
nfromHeight = base.GenesisHeight
case nfromHeight > base.NilHeight:
switch _, found, err := db.BlockMap(nfromHeight - 1); {
case err != nil:
return nfromHeight, toHeight, last, err
case !found:
return nfromHeight, toHeight, last, errors.Errorf(
"previous blockmap not found for from height, %d", nfromHeight-1)
}
}
switch i, _, found, err := isaacblock.FindHighestDirectory(root); {
case err != nil:
return nfromHeight, toHeight, last, err
case !found, i < base.GenesisHeight:
return nfromHeight, toHeight, last, errors.Errorf("last height not found in source")
case i < toHeight:
return nfromHeight, toHeight, last, errors.Errorf("last is lower than to height; last=%d to=%d", i, toHeight)
case toHeight > base.NilHeight:
last = toHeight
default:
last = i
}
switch {
case nfromHeight > last:
return nfromHeight, toHeight, last, errors.Errorf(
"from height is higher than to; from=%d to=%d", nfromHeight, last)
case nfromHeight < base.GenesisHeight:
nfromHeight = base.GenesisHeight
}
return nfromHeight, toHeight, last, nil
}