@@ -40,11 +40,6 @@ import (
40
40
"gopkg.in/urfave/cli.v1"
41
41
)
42
42
43
- var (
44
- // secureKeyPrefix is the database key prefix used to store trie node preimages.
45
- secureKeyPrefix = []byte ("secure-key-" )
46
- )
47
-
48
43
var (
49
44
initCommand = cli.Command {
50
45
Action : utils .MigrateFlags (initGenesis ),
@@ -100,6 +95,34 @@ Requires a first argument of the file to write to.
100
95
Optional second and third arguments control the first and
101
96
last block to write. In this mode, the file will be appended
102
97
if already existing.` ,
98
+ }
99
+ importPreimagesCommand = cli.Command {
100
+ Action : utils .MigrateFlags (importPreimages ),
101
+ Name : "import-preimages" ,
102
+ Usage : "Import the preimage database from an RLP stream" ,
103
+ ArgsUsage : "<datafile>" ,
104
+ Flags : []cli.Flag {
105
+ utils .DataDirFlag ,
106
+ utils .CacheFlag ,
107
+ utils .LightModeFlag ,
108
+ },
109
+ Category : "BLOCKCHAIN COMMANDS" ,
110
+ Description : `
111
+ The import-preimages command imports hash preimages from an RLP encoded stream.` ,
112
+ }
113
+ exportPreimagesCommand = cli.Command {
114
+ Action : utils .MigrateFlags (exportPreimages ),
115
+ Name : "export-preimages" ,
116
+ Usage : "Export the preimage database into an RLP stream" ,
117
+ ArgsUsage : "<dumpfile>" ,
118
+ Flags : []cli.Flag {
119
+ utils .DataDirFlag ,
120
+ utils .CacheFlag ,
121
+ utils .LightModeFlag ,
122
+ },
123
+ Category : "BLOCKCHAIN COMMANDS" ,
124
+ Description : `
125
+ The export-preimages command export hash preimages to an RLP encoded stream` ,
103
126
}
104
127
copydbCommand = cli.Command {
105
128
Action : utils .MigrateFlags (copyDb ),
@@ -146,34 +169,6 @@ Remove blockchain and state databases`,
146
169
The arguments are interpreted as block numbers or hashes.
147
170
Use "ethereum dump 0" to dump the genesis block.` ,
148
171
}
149
- preimageDumpCommand = cli.Command {
150
- Action : utils .MigrateFlags (dumpPreimage ),
151
- Name : "preimagedump" ,
152
- Usage : "Dump the preimage database in json format" ,
153
- ArgsUsage : "<dumpfile>" ,
154
- Flags : []cli.Flag {
155
- utils .DataDirFlag ,
156
- utils .CacheFlag ,
157
- utils .LightModeFlag ,
158
- },
159
- Category : "BLOCKCHAIN COMMANDS" ,
160
- Description : `
161
- Dump the preimage database in json format` ,
162
- }
163
- preimageImportCommand = cli.Command {
164
- Action : utils .MigrateFlags (importPreimage ),
165
- Name : "preimageimport" ,
166
- Usage : "Import the preimage data from the specified file" ,
167
- ArgsUsage : "<datafile>" ,
168
- Flags : []cli.Flag {
169
- utils .DataDirFlag ,
170
- utils .CacheFlag ,
171
- utils .LightModeFlag ,
172
- },
173
- Category : "BLOCKCHAIN COMMANDS" ,
174
- Description : `
175
- Import the preimage data from the specified file` ,
176
- }
177
172
)
178
173
179
174
// initGenesis will initialise the given JSON format genesis file and writes it as
@@ -332,7 +327,39 @@ func exportChain(ctx *cli.Context) error {
332
327
if err != nil {
333
328
utils .Fatalf ("Export error: %v\n " , err )
334
329
}
335
- fmt .Printf ("Export done in %v" , time .Since (start ))
330
+ fmt .Printf ("Export done in %v\n " , time .Since (start ))
331
+ return nil
332
+ }
333
+
334
+ // importPreimages imports preimage data from the specified file.
335
+ func importPreimages (ctx * cli.Context ) error {
336
+ if len (ctx .Args ()) < 1 {
337
+ utils .Fatalf ("This command requires an argument." )
338
+ }
339
+ stack := makeFullNode (ctx )
340
+ diskdb := utils .MakeChainDatabase (ctx , stack ).(* ethdb.LDBDatabase )
341
+
342
+ start := time .Now ()
343
+ if err := utils .ImportPreimages (diskdb , ctx .Args ().First ()); err != nil {
344
+ utils .Fatalf ("Export error: %v\n " , err )
345
+ }
346
+ fmt .Printf ("Export done in %v\n " , time .Since (start ))
347
+ return nil
348
+ }
349
+
350
+ // exportPreimages dumps the preimage data to specified json file in streaming way.
351
+ func exportPreimages (ctx * cli.Context ) error {
352
+ if len (ctx .Args ()) < 1 {
353
+ utils .Fatalf ("This command requires an argument." )
354
+ }
355
+ stack := makeFullNode (ctx )
356
+ diskdb := utils .MakeChainDatabase (ctx , stack ).(* ethdb.LDBDatabase )
357
+
358
+ start := time .Now ()
359
+ if err := utils .ExportPreimages (diskdb , ctx .Args ().First ()); err != nil {
360
+ utils .Fatalf ("Export error: %v\n " , err )
361
+ }
362
+ fmt .Printf ("Export done in %v\n " , time .Since (start ))
336
363
return nil
337
364
}
338
365
@@ -439,86 +466,6 @@ func dump(ctx *cli.Context) error {
439
466
return nil
440
467
}
441
468
442
- // PreimageEntry represents a map between preimage and hash.
443
- type PreimageEntry struct {
444
- Hash string `json:"hash"`
445
- Preimage string `json:"preimage"`
446
- }
447
-
448
- // dumpPreimage dumps the preimage data to specified json file in streaming way.
449
- func dumpPreimage (ctx * cli.Context ) error {
450
- // Make sure the export json file has been specified.
451
- if len (ctx .Args ()) < 1 {
452
- utils .Fatalf ("This command requires an argument." )
453
- }
454
-
455
- // Encode preimage data to json file in streaming way.
456
- file , err := os .Create (ctx .Args ().First ())
457
- if err != nil {
458
- return err
459
- }
460
- encoder := json .NewEncoder (file )
461
-
462
- stack := makeFullNode (ctx )
463
- db := utils .MakeChainDatabase (ctx , stack )
464
-
465
- // Dump all preimage entries.
466
- it := db .(* ethdb.LDBDatabase ).NewIteratorByPrefix (secureKeyPrefix )
467
- for it .Next () {
468
- hash := it .Key ()[len (secureKeyPrefix ):]
469
- if err := encoder .Encode (PreimageEntry {common .Bytes2Hex (hash ), common .Bytes2Hex (it .Value ())}); err != nil {
470
- return err
471
- }
472
- }
473
- return nil
474
- }
475
-
476
- // importPreimages imports preimage data from the specified file.
477
- func importPreimage (ctx * cli.Context ) error {
478
- // Make sure the export json file has been specified.
479
- if len (ctx .Args ()) < 1 {
480
- utils .Fatalf ("This command requires an argument." )
481
- }
482
-
483
- // Decode the preimage data in streaming way.
484
- file , err := os .Open (ctx .Args ().First ())
485
- if err != nil {
486
- return err
487
- }
488
- decoder := json .NewDecoder (file )
489
-
490
- stack := makeFullNode (ctx )
491
- db := utils .MakeChainDatabase (ctx , stack )
492
-
493
- var (
494
- entry PreimageEntry
495
- preimages = make (map [common.Hash ][]byte )
496
- )
497
-
498
- for decoder .More () {
499
- if err := decoder .Decode (& entry ); err != nil {
500
- return err
501
- }
502
- preimages [common .HexToHash (entry .Hash )] = common .Hex2Bytes (entry .Preimage )
503
- // Flush to database in batch
504
- if len (preimages ) > 1024 {
505
- err := core .WritePreimages (db , 0 , preimages )
506
- if err != nil {
507
- return err
508
- }
509
- preimages = make (map [common.Hash ][]byte )
510
- }
511
- }
512
- // Flush the last batch preimage data
513
- if len (preimages ) > 0 {
514
- err := core .WritePreimages (db , 0 , preimages )
515
- if err != nil {
516
- return err
517
- }
518
- }
519
- return nil
520
- }
521
-
522
469
// hashish returns true for strings that look like hashes.
523
470
func hashish (x string ) bool {
524
471
_ , err := strconv .Atoi (x )
0 commit comments