diff options
author | Péter Szilágyi <peterke@gmail.com> | 2017-10-11 18:10:18 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-11 18:10:18 +0800 |
commit | ad444752311b1a318a7933562749b4586d4469e9 (patch) | |
tree | ac0df3e6fc330a186d409adb723a78e16b5605ea /cmd/geth/chaincmd.go | |
parent | cefeb5859866f2fe119b7936f7c97137b7efb7b6 (diff) | |
parent | 35767dfd0c468872acf99c3fccc3a206ce709128 (diff) | |
download | dexon-ad444752311b1a318a7933562749b4586d4469e9.tar.gz dexon-ad444752311b1a318a7933562749b4586d4469e9.tar.zst dexon-ad444752311b1a318a7933562749b4586d4469e9.zip |
Merge pull request #14785 from Arachnid/downloaddb
cmd: Added support for downloading to another DB instance
Diffstat (limited to 'cmd/geth/chaincmd.go')
-rw-r--r-- | cmd/geth/chaincmd.go | 69 |
1 files changed, 68 insertions, 1 deletions
diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index 12bc1d7c6..4a9a7b11b 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -31,7 +31,9 @@ import ( "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/trie" "github.com/syndtr/goleveldb/leveldb/util" @@ -71,7 +73,7 @@ It expects the genesis file as argument.`, The import command imports blocks from an RLP-encoded form. The form can be one file with several RLP-encoded blocks, or several files can be used. -If only one file is used, import error will result in failure. If several files are used, +If only one file is used, import error will result in failure. If several files are used, processing will proceed even if an individual RLP-file import failure occurs.`, } exportCommand = cli.Command{ @@ -91,6 +93,23 @@ Optional second and third arguments control the first and last block to write. In this mode, the file will be appended if already existing.`, } + copydbCommand = cli.Command{ + Action: utils.MigrateFlags(copyDb), + Name: "copydb", + Usage: "Create a local chain from a target chaindata folder", + ArgsUsage: "<sourceChaindataDir>", + Flags: []cli.Flag{ + utils.DataDirFlag, + utils.CacheFlag, + utils.SyncModeFlag, + utils.FakePoWFlag, + utils.TestnetFlag, + utils.RinkebyFlag, + }, + Category: "BLOCKCHAIN COMMANDS", + Description: ` +The first argument must be the directory containing the blockchain to download from`, + } removedbCommand = cli.Command{ Action: utils.MigrateFlags(removeDB), Name: "removedb", @@ -268,6 +287,54 @@ func exportChain(ctx *cli.Context) error { return nil } +func copyDb(ctx *cli.Context) error { + // Ensure we have a source chain directory to copy + if len(ctx.Args()) != 1 { + utils.Fatalf("Source chaindata directory path argument missing") + } + // Initialize a new chain for the running node to sync into + stack := makeFullNode(ctx) + chain, chainDb := utils.MakeChain(ctx, stack) + + syncmode := *utils.GlobalTextMarshaler(ctx, utils.SyncModeFlag.Name).(*downloader.SyncMode) + dl := downloader.New(syncmode, chainDb, new(event.TypeMux), chain, nil, nil) + + // Create a source peer to satisfy downloader requests from + db, err := ethdb.NewLDBDatabase(ctx.Args().First(), ctx.GlobalInt(utils.CacheFlag.Name), 256) + if err != nil { + return err + } + hc, err := core.NewHeaderChain(db, chain.Config(), chain.Engine(), func() bool { return false }) + if err != nil { + return err + } + peer := downloader.NewFakePeer("local", db, hc, dl) + if err = dl.RegisterPeer("local", 63, peer); err != nil { + return err + } + // Synchronise with the simulated peer + start := time.Now() + + currentHeader := hc.CurrentHeader() + if err = dl.Synchronise("local", currentHeader.Hash(), hc.GetTd(currentHeader.Hash(), currentHeader.Number.Uint64()), syncmode); err != nil { + return err + } + for dl.Synchronising() { + time.Sleep(10 * time.Millisecond) + } + fmt.Printf("Database copy done in %v\n", time.Since(start)) + + // Compact the entire database to remove any sync overhead + start = time.Now() + fmt.Println("Compacting entire database...") + if err = chainDb.(*ethdb.LDBDatabase).LDB().CompactRange(util.Range{}); err != nil { + utils.Fatalf("Compaction failed: %v", err) + } + fmt.Printf("Compaction done in %v.\n\n", time.Since(start)) + + return nil +} + func removeDB(ctx *cli.Context) error { stack, _ := makeConfigNode(ctx) |