From faacc8e0faa89f5b3a7055e86924a95b6a9be944 Mon Sep 17 00:00:00 2001
From: Martin Holst Swende <martin@swende.se>
Date: Mon, 15 Mar 2021 15:25:45 +0100
Subject: [PATCH] cmd/geth, eth/downloader: remove copydb command (#22501)

* cmd/geth: remove copydb command

* eth/downloader: remove fakepeer
---
 cmd/geth/chaincmd.go       |  81 -------------------
 cmd/geth/main.go           |   1 -
 eth/downloader/fakepeer.go | 161 -------------------------------------
 3 files changed, 243 deletions(-)
 delete mode 100644 eth/downloader/fakepeer.go

diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go
index 1834dab6a..2010ae4b3 100644
--- a/cmd/geth/chaincmd.go
+++ b/cmd/geth/chaincmd.go
@@ -28,14 +28,10 @@ import (
 	"github.com/ethereum/go-ethereum/cmd/utils"
 	"github.com/ethereum/go-ethereum/common"
 	"github.com/ethereum/go-ethereum/core"
-	"github.com/ethereum/go-ethereum/core/rawdb"
 	"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/event"
 	"github.com/ethereum/go-ethereum/log"
 	"github.com/ethereum/go-ethereum/metrics"
-	"github.com/ethereum/go-ethereum/trie"
 	"gopkg.in/urfave/cli.v1"
 )
 
@@ -150,27 +146,6 @@ be gzipped.`,
 		Category: "BLOCKCHAIN COMMANDS",
 		Description: `
 The export-preimages command export hash preimages to an RLP encoded stream`,
-	}
-	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.MainnetFlag,
-			utils.RopstenFlag,
-			utils.RinkebyFlag,
-			utils.TxLookupLimitFlag,
-			utils.GoerliFlag,
-			utils.YoloV3Flag,
-		},
-		Category: "BLOCKCHAIN COMMANDS",
-		Description: `
-The first argument must be the directory containing the blockchain to download from`,
 	}
 	dumpCommand = cli.Command{
 		Action:    utils.MigrateFlags(dump),
@@ -394,62 +369,6 @@ func exportPreimages(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")
-	}
-	if len(ctx.Args()) < 2 {
-		utils.Fatalf("Source ancient chain directory path argument missing")
-	}
-	// Initialize a new chain for the running node to sync into
-	stack, _ := makeConfigNode(ctx)
-	defer stack.Close()
-
-	chain, chainDb := utils.MakeChain(ctx, stack, false)
-	syncMode := *utils.GlobalTextMarshaler(ctx, utils.SyncModeFlag.Name).(*downloader.SyncMode)
-
-	var syncBloom *trie.SyncBloom
-	if syncMode == downloader.FastSync {
-		syncBloom = trie.NewSyncBloom(uint64(ctx.GlobalInt(utils.CacheFlag.Name)/2), chainDb)
-	}
-	dl := downloader.New(0, chainDb, syncBloom, new(event.TypeMux), chain, nil, nil)
-
-	// Create a source peer to satisfy downloader requests from
-	db, err := rawdb.NewLevelDBDatabaseWithFreezer(ctx.Args().First(), ctx.GlobalInt(utils.CacheFlag.Name)/2, 256, ctx.Args().Get(1), "")
-	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 = db.Compact(nil, nil); err != nil {
-		utils.Fatalf("Compaction failed: %v", err)
-	}
-	fmt.Printf("Compaction done in %v.\n\n", time.Since(start))
-	return nil
-}
-
 func dump(ctx *cli.Context) error {
 	stack, _ := makeConfigNode(ctx)
 	defer stack.Close()
diff --git a/cmd/geth/main.go b/cmd/geth/main.go
index 207c93c0d..555983549 100644
--- a/cmd/geth/main.go
+++ b/cmd/geth/main.go
@@ -212,7 +212,6 @@ func init() {
 		exportCommand,
 		importPreimagesCommand,
 		exportPreimagesCommand,
-		copydbCommand,
 		removedbCommand,
 		dumpCommand,
 		dumpGenesisCommand,
diff --git a/eth/downloader/fakepeer.go b/eth/downloader/fakepeer.go
deleted file mode 100644
index 3ec90bc9e..000000000
--- a/eth/downloader/fakepeer.go
+++ /dev/null
@@ -1,161 +0,0 @@
-// Copyright 2017 The go-ethereum Authors
-// This file is part of the go-ethereum library.
-//
-// The go-ethereum library is free software: you can redistribute it and/or modify
-// it under the terms of the GNU Lesser General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// The go-ethereum library is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public License
-// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
-
-package downloader
-
-import (
-	"math/big"
-
-	"github.com/ethereum/go-ethereum/common"
-	"github.com/ethereum/go-ethereum/core"
-	"github.com/ethereum/go-ethereum/core/rawdb"
-	"github.com/ethereum/go-ethereum/core/types"
-	"github.com/ethereum/go-ethereum/ethdb"
-)
-
-// FakePeer is a mock downloader peer that operates on a local database instance
-// instead of being an actual live node. It's useful for testing and to implement
-// sync commands from an existing local database.
-type FakePeer struct {
-	id string
-	db ethdb.Database
-	hc *core.HeaderChain
-	dl *Downloader
-}
-
-// NewFakePeer creates a new mock downloader peer with the given data sources.
-func NewFakePeer(id string, db ethdb.Database, hc *core.HeaderChain, dl *Downloader) *FakePeer {
-	return &FakePeer{id: id, db: db, hc: hc, dl: dl}
-}
-
-// Head implements downloader.Peer, returning the current head hash and number
-// of the best known header.
-func (p *FakePeer) Head() (common.Hash, *big.Int) {
-	header := p.hc.CurrentHeader()
-	return header.Hash(), header.Number
-}
-
-// RequestHeadersByHash implements downloader.Peer, returning a batch of headers
-// defined by the origin hash and the associated query parameters.
-func (p *FakePeer) RequestHeadersByHash(hash common.Hash, amount int, skip int, reverse bool) error {
-	var (
-		headers []*types.Header
-		unknown bool
-	)
-	for !unknown && len(headers) < amount {
-		origin := p.hc.GetHeaderByHash(hash)
-		if origin == nil {
-			break
-		}
-		number := origin.Number.Uint64()
-		headers = append(headers, origin)
-		if reverse {
-			for i := 0; i <= skip; i++ {
-				if header := p.hc.GetHeader(hash, number); header != nil {
-					hash = header.ParentHash
-					number--
-				} else {
-					unknown = true
-					break
-				}
-			}
-		} else {
-			var (
-				current = origin.Number.Uint64()
-				next    = current + uint64(skip) + 1
-			)
-			if header := p.hc.GetHeaderByNumber(next); header != nil {
-				if p.hc.GetBlockHashesFromHash(header.Hash(), uint64(skip+1))[skip] == hash {
-					hash = header.Hash()
-				} else {
-					unknown = true
-				}
-			} else {
-				unknown = true
-			}
-		}
-	}
-	p.dl.DeliverHeaders(p.id, headers)
-	return nil
-}
-
-// RequestHeadersByNumber implements downloader.Peer, returning a batch of headers
-// defined by the origin number and the associated query parameters.
-func (p *FakePeer) RequestHeadersByNumber(number uint64, amount int, skip int, reverse bool) error {
-	var (
-		headers []*types.Header
-		unknown bool
-	)
-	for !unknown && len(headers) < amount {
-		origin := p.hc.GetHeaderByNumber(number)
-		if origin == nil {
-			break
-		}
-		if reverse {
-			if number >= uint64(skip+1) {
-				number -= uint64(skip + 1)
-			} else {
-				unknown = true
-			}
-		} else {
-			number += uint64(skip + 1)
-		}
-		headers = append(headers, origin)
-	}
-	p.dl.DeliverHeaders(p.id, headers)
-	return nil
-}
-
-// RequestBodies implements downloader.Peer, returning a batch of block bodies
-// corresponding to the specified block hashes.
-func (p *FakePeer) RequestBodies(hashes []common.Hash) error {
-	var (
-		txs    [][]*types.Transaction
-		uncles [][]*types.Header
-	)
-	for _, hash := range hashes {
-		block := rawdb.ReadBlock(p.db, hash, *p.hc.GetBlockNumber(hash))
-
-		txs = append(txs, block.Transactions())
-		uncles = append(uncles, block.Uncles())
-	}
-	p.dl.DeliverBodies(p.id, txs, uncles)
-	return nil
-}
-
-// RequestReceipts implements downloader.Peer, returning a batch of transaction
-// receipts corresponding to the specified block hashes.
-func (p *FakePeer) RequestReceipts(hashes []common.Hash) error {
-	var receipts [][]*types.Receipt
-	for _, hash := range hashes {
-		receipts = append(receipts, rawdb.ReadRawReceipts(p.db, hash, *p.hc.GetBlockNumber(hash)))
-	}
-	p.dl.DeliverReceipts(p.id, receipts)
-	return nil
-}
-
-// RequestNodeData implements downloader.Peer, returning a batch of state trie
-// nodes corresponding to the specified trie hashes.
-func (p *FakePeer) RequestNodeData(hashes []common.Hash) error {
-	var data [][]byte
-	for _, hash := range hashes {
-		if entry, err := p.db.Get(hash.Bytes()); err == nil {
-			data = append(data, entry)
-		}
-	}
-	p.dl.DeliverNodeData(p.id, data)
-	return nil
-}
-- 
GitLab