2021-11-08 13:40:56 +00:00
|
|
|
package main
|
2020-11-13 16:16:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
2022-02-04 05:42:55 +00:00
|
|
|
"path/filepath"
|
2020-11-13 16:16:47 +00:00
|
|
|
"time"
|
|
|
|
|
2021-12-14 10:13:17 +00:00
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2021-12-24 08:03:51 +00:00
|
|
|
"github.com/c2h5oh/datasize"
|
2020-11-13 16:16:47 +00:00
|
|
|
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
|
|
|
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
|
2022-01-19 03:49:07 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/common"
|
2022-11-20 03:41:30 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/common/datadir"
|
|
|
|
"github.com/ledgerwatch/erigon-lib/downloader"
|
|
|
|
downloadercfg2 "github.com/ledgerwatch/erigon-lib/downloader/downloadercfg"
|
2021-12-14 10:13:17 +00:00
|
|
|
proto_downloader "github.com/ledgerwatch/erigon-lib/gointerfaces/downloader"
|
2022-11-20 03:41:30 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/downloader/downloadernat"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/utils"
|
2021-11-08 13:40:56 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common/paths"
|
2022-04-06 08:25:32 +00:00
|
|
|
"github.com/ledgerwatch/erigon/p2p/nat"
|
2022-11-16 12:48:23 +00:00
|
|
|
"github.com/ledgerwatch/erigon/params"
|
2022-10-25 02:58:25 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/debug"
|
|
|
|
logging2 "github.com/ledgerwatch/erigon/turbo/logging"
|
2021-07-29 10:23:23 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2021-12-21 14:12:32 +00:00
|
|
|
"github.com/pelletier/go-toml/v2"
|
2020-11-13 16:16:47 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"google.golang.org/grpc"
|
2021-11-08 13:40:56 +00:00
|
|
|
"google.golang.org/grpc/credentials"
|
2021-12-06 12:03:46 +00:00
|
|
|
"google.golang.org/grpc/health"
|
|
|
|
"google.golang.org/grpc/health/grpc_health_v1"
|
2020-11-13 16:16:47 +00:00
|
|
|
"google.golang.org/grpc/keepalive"
|
2021-11-08 13:40:56 +00:00
|
|
|
"google.golang.org/grpc/reflection"
|
2020-11-13 16:16:47 +00:00
|
|
|
)
|
|
|
|
|
2021-12-06 03:06:37 +00:00
|
|
|
var (
|
2022-06-07 03:24:50 +00:00
|
|
|
datadirCli string
|
2022-02-09 06:22:43 +00:00
|
|
|
forceRebuild bool
|
|
|
|
forceVerify bool
|
|
|
|
downloaderApiAddr string
|
2022-04-06 08:25:32 +00:00
|
|
|
natSetting string
|
2022-06-29 02:29:59 +00:00
|
|
|
torrentVerbosity int
|
2022-02-09 06:22:43 +00:00
|
|
|
downloadRateStr, uploadRateStr string
|
2022-04-28 02:10:00 +00:00
|
|
|
torrentDownloadSlots int
|
2022-02-09 06:22:43 +00:00
|
|
|
torrentPort int
|
2022-04-06 14:06:58 +00:00
|
|
|
torrentMaxPeers int
|
|
|
|
torrentConnsPerFile int
|
2022-03-28 07:44:11 +00:00
|
|
|
targetFile string
|
2022-12-15 09:40:59 +00:00
|
|
|
disableIPV6 bool
|
|
|
|
disableIPV4 bool
|
2021-12-06 03:06:37 +00:00
|
|
|
)
|
|
|
|
|
2020-11-13 16:16:47 +00:00
|
|
|
func init() {
|
2022-10-25 02:58:25 +00:00
|
|
|
utils.CobraFlags(rootCmd, debug.Flags, utils.MetricFlags, logging2.Flags)
|
2020-11-13 16:16:47 +00:00
|
|
|
|
2022-02-22 17:39:48 +00:00
|
|
|
withDataDir(rootCmd)
|
2020-11-13 16:16:47 +00:00
|
|
|
|
2022-04-22 10:30:48 +00:00
|
|
|
rootCmd.Flags().StringVar(&natSetting, "nat", utils.NATFlag.Value, utils.NATFlag.Usage)
|
2021-12-06 03:06:37 +00:00
|
|
|
rootCmd.Flags().StringVar(&downloaderApiAddr, "downloader.api.addr", "127.0.0.1:9093", "external downloader api network address, for example: 127.0.0.1:9093 serves remote downloader interface")
|
2022-04-22 10:30:48 +00:00
|
|
|
rootCmd.Flags().StringVar(&downloadRateStr, "torrent.download.rate", utils.TorrentDownloadRateFlag.Value, utils.TorrentDownloadRateFlag.Usage)
|
|
|
|
rootCmd.Flags().StringVar(&uploadRateStr, "torrent.upload.rate", utils.TorrentUploadRateFlag.Value, utils.TorrentUploadRateFlag.Usage)
|
2022-06-29 02:29:59 +00:00
|
|
|
rootCmd.Flags().IntVar(&torrentVerbosity, "torrent.verbosity", utils.TorrentVerbosityFlag.Value, utils.TorrentVerbosityFlag.Usage)
|
2022-04-22 10:30:48 +00:00
|
|
|
rootCmd.Flags().IntVar(&torrentPort, "torrent.port", utils.TorrentPortFlag.Value, utils.TorrentPortFlag.Usage)
|
|
|
|
rootCmd.Flags().IntVar(&torrentMaxPeers, "torrent.maxpeers", utils.TorrentMaxPeersFlag.Value, utils.TorrentMaxPeersFlag.Usage)
|
|
|
|
rootCmd.Flags().IntVar(&torrentConnsPerFile, "torrent.conns.perfile", utils.TorrentConnsPerFileFlag.Value, utils.TorrentConnsPerFileFlag.Usage)
|
2022-04-28 02:10:00 +00:00
|
|
|
rootCmd.Flags().IntVar(&torrentDownloadSlots, "torrent.download.slots", utils.TorrentDownloadSlotsFlag.Value, utils.TorrentDownloadSlotsFlag.Usage)
|
2022-12-15 09:40:59 +00:00
|
|
|
rootCmd.Flags().BoolVar(&disableIPV6, "downloader.disable.ipv6", utils.DisableIPV6.Value, utils.DisableIPV6.Usage)
|
|
|
|
rootCmd.Flags().BoolVar(&disableIPV4, "downloader.disable.ipv4", utils.DisableIPV4.Value, utils.DisableIPV6.Usage)
|
2021-12-14 10:13:17 +00:00
|
|
|
|
2022-02-22 17:39:48 +00:00
|
|
|
withDataDir(printTorrentHashes)
|
2022-02-07 05:07:46 +00:00
|
|
|
printTorrentHashes.PersistentFlags().BoolVar(&forceRebuild, "rebuild", false, "Force re-create .torrent files")
|
|
|
|
printTorrentHashes.PersistentFlags().BoolVar(&forceVerify, "verify", false, "Force verify data files if have .torrent files")
|
2022-03-28 07:44:11 +00:00
|
|
|
printTorrentHashes.Flags().StringVar(&targetFile, "targetfile", "", "write output to file")
|
|
|
|
if err := printTorrentHashes.MarkFlagFilename("targetfile"); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-01-05 10:14:37 +00:00
|
|
|
|
2022-02-07 05:07:46 +00:00
|
|
|
rootCmd.AddCommand(printTorrentHashes)
|
2021-12-14 10:13:17 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 17:39:48 +00:00
|
|
|
func withDataDir(cmd *cobra.Command) {
|
2022-06-07 03:24:50 +00:00
|
|
|
cmd.Flags().StringVar(&datadirCli, utils.DataDirFlag.Name, paths.DefaultDataDir(), utils.DataDirFlag.Usage)
|
2021-12-14 10:13:17 +00:00
|
|
|
if err := cmd.MarkFlagDirname(utils.DataDirFlag.Name); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-11-13 16:16:47 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 13:40:56 +00:00
|
|
|
func main() {
|
2022-01-19 03:49:07 +00:00
|
|
|
ctx, cancel := common.RootContext()
|
2021-11-08 13:40:56 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := rootCmd.ExecuteContext(ctx); err != nil {
|
2020-11-13 16:16:47 +00:00
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "",
|
2021-11-08 13:40:56 +00:00
|
|
|
Short: "snapshot downloader",
|
|
|
|
Example: "go run ./cmd/snapshots --datadir <your_datadir> --downloader.api.addr 127.0.0.1:9093",
|
2020-11-13 16:16:47 +00:00
|
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
if err := debug.SetupCobra(cmd); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
debug.Exit()
|
|
|
|
},
|
2021-12-06 03:06:37 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-10-25 02:58:25 +00:00
|
|
|
_ = logging2.GetLoggerCmd("downloader", cmd)
|
2022-02-11 07:59:51 +00:00
|
|
|
if err := Downloader(cmd.Context()); err != nil {
|
2021-12-14 10:13:17 +00:00
|
|
|
log.Error("Downloader", "err", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2020-11-13 16:16:47 +00:00
|
|
|
|
2022-02-11 07:59:51 +00:00
|
|
|
func Downloader(ctx context.Context) error {
|
2022-06-07 03:24:50 +00:00
|
|
|
dirs := datadir.New(datadirCli)
|
2022-11-20 03:41:30 +00:00
|
|
|
torrentLogLevel, _, err := downloadercfg2.Int2LogLevel(torrentVerbosity)
|
2022-05-25 08:24:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-12-14 10:13:17 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 10:14:37 +00:00
|
|
|
var downloadRate, uploadRate datasize.ByteSize
|
|
|
|
if err := downloadRate.UnmarshalText([]byte(downloadRateStr)); err != nil {
|
2021-12-24 08:03:51 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-02-09 06:22:43 +00:00
|
|
|
if err := uploadRate.UnmarshalText([]byte(uploadRateStr)); err != nil {
|
2021-12-24 08:03:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-15 09:40:59 +00:00
|
|
|
log.Info("Run snapshot downloader", "addr", downloaderApiAddr, "datadir", dirs.DataDir, "ipv6-enabled", !disableIPV6, "ipv4-enabled", !disableIPV4, "download.rate", downloadRate.String(), "upload.rate", uploadRate.String())
|
2022-04-06 08:25:32 +00:00
|
|
|
natif, err := nat.Parse(natSetting)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid nat option %s: %w", natSetting, err)
|
|
|
|
}
|
2021-12-14 10:13:17 +00:00
|
|
|
|
2022-11-16 12:48:23 +00:00
|
|
|
version := "erigon: " + params.VersionWithCommit(params.GitCommit, "")
|
2022-11-20 03:41:30 +00:00
|
|
|
cfg, err := downloadercfg2.New(dirs.Snap, version, torrentLogLevel, downloadRate, uploadRate, torrentPort, torrentConnsPerFile, torrentDownloadSlots)
|
2022-02-07 08:23:07 +00:00
|
|
|
if err != nil {
|
2022-02-18 02:24:17 +00:00
|
|
|
return err
|
2022-02-07 08:23:07 +00:00
|
|
|
}
|
2022-11-20 03:41:30 +00:00
|
|
|
downloadernat.DoNat(natif, cfg)
|
2022-04-21 03:34:31 +00:00
|
|
|
|
2022-12-15 09:40:59 +00:00
|
|
|
cfg.ClientConfig.DisableIPv6 = disableIPV6
|
|
|
|
cfg.ClientConfig.DisableIPv4 = disableIPV4
|
|
|
|
|
2022-05-10 02:29:44 +00:00
|
|
|
d, err := downloader.New(cfg)
|
2022-02-07 08:23:07 +00:00
|
|
|
if err != nil {
|
2021-12-14 10:13:17 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-05-06 02:59:23 +00:00
|
|
|
defer d.Close()
|
|
|
|
log.Info("[torrent] Start", "my peerID", fmt.Sprintf("%x", d.Torrent().PeerID()))
|
2022-05-10 02:29:44 +00:00
|
|
|
go downloader.MainLoop(ctx, d, false)
|
2021-12-14 10:13:17 +00:00
|
|
|
|
2022-05-10 02:29:44 +00:00
|
|
|
bittorrentServer, err := downloader.NewGrpcServer(d)
|
2021-12-14 10:13:17 +00:00
|
|
|
if err != nil {
|
2022-02-07 09:54:20 +00:00
|
|
|
return fmt.Errorf("new server: %w", err)
|
2021-12-31 13:42:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 10:13:17 +00:00
|
|
|
grpcServer, err := StartGrpc(bittorrentServer, downloaderApiAddr, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-11 07:57:59 +00:00
|
|
|
defer grpcServer.GracefulStop()
|
|
|
|
|
|
|
|
<-ctx.Done()
|
2021-12-14 10:13:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:07:46 +00:00
|
|
|
var printTorrentHashes = &cobra.Command{
|
|
|
|
Use: "torrent_hashes",
|
|
|
|
Example: "go run ./cmd/downloader torrent_hashes --datadir <your_datadir>",
|
2021-12-14 10:13:17 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-06-07 03:24:50 +00:00
|
|
|
dirs := datadir.New(datadirCli)
|
2022-01-05 10:14:37 +00:00
|
|
|
ctx := cmd.Context()
|
|
|
|
|
2022-02-07 05:07:46 +00:00
|
|
|
if forceVerify { // remove and create .torrent files (will re-read all snapshots)
|
2022-06-07 03:24:50 +00:00
|
|
|
return downloader.VerifyDtaFiles(ctx, dirs.Snap)
|
2022-02-07 05:07:46 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 10:14:37 +00:00
|
|
|
if forceRebuild { // remove and create .torrent files (will re-read all snapshots)
|
2022-05-17 02:40:45 +00:00
|
|
|
//removePieceCompletionStorage(snapDir)
|
2022-06-07 03:24:50 +00:00
|
|
|
files, err := downloader.AllTorrentPaths(dirs.Snap)
|
2022-01-27 05:56:08 +00:00
|
|
|
if err != nil {
|
2022-01-05 10:14:37 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-01-27 05:56:08 +00:00
|
|
|
for _, filePath := range files {
|
|
|
|
if err := os.Remove(filePath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-09-18 10:41:01 +00:00
|
|
|
if _, err := downloader.BuildTorrentFilesIfNeed(ctx, dirs.Snap); err != nil {
|
2022-04-21 03:34:10 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-01-05 10:14:37 +00:00
|
|
|
}
|
2021-12-14 10:13:17 +00:00
|
|
|
|
|
|
|
res := map[string]string{}
|
2022-06-07 03:24:50 +00:00
|
|
|
files, err := downloader.AllTorrentPaths(dirs.Snap)
|
2022-01-27 05:56:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, torrentFilePath := range files {
|
2021-12-14 10:13:17 +00:00
|
|
|
mi, err := metainfo.LoadFromFile(torrentFilePath)
|
2021-05-08 08:45:40 +00:00
|
|
|
if err != nil {
|
2021-12-14 10:13:17 +00:00
|
|
|
return err
|
2021-05-08 08:45:40 +00:00
|
|
|
}
|
2021-12-14 10:13:17 +00:00
|
|
|
info, err := mi.UnmarshalInfo()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-05-08 08:45:40 +00:00
|
|
|
}
|
2021-12-14 10:13:17 +00:00
|
|
|
res[info.Name] = mi.HashInfoBytes().String()
|
2021-05-08 08:45:40 +00:00
|
|
|
}
|
2022-03-28 07:44:11 +00:00
|
|
|
serialized, err := toml.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if targetFile == "" {
|
|
|
|
fmt.Printf("%s\n", serialized)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-23 14:43:00 +00:00
|
|
|
oldContent, err := os.ReadFile(targetFile)
|
2022-03-28 07:44:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
oldLines := map[string]string{}
|
|
|
|
if err := toml.Unmarshal(oldContent, &oldLines); err != nil {
|
|
|
|
return fmt.Errorf("unmarshal: %w", err)
|
|
|
|
}
|
|
|
|
if len(oldLines) >= len(res) {
|
|
|
|
log.Info("amount of lines in target file is equal or greater than amount of lines in snapshot dir", "old", len(oldLines), "new", len(res))
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-04 13:59:40 +00:00
|
|
|
if err := os.WriteFile(targetFile, serialized, 0644); err != nil { // nolint
|
2022-03-28 07:44:11 +00:00
|
|
|
return err
|
2021-12-21 14:12:32 +00:00
|
|
|
}
|
2021-12-06 03:06:37 +00:00
|
|
|
return nil
|
|
|
|
},
|
2020-11-13 16:16:47 +00:00
|
|
|
}
|
2021-11-08 13:40:56 +00:00
|
|
|
|
2022-08-10 12:04:13 +00:00
|
|
|
// nolint
|
2022-05-17 02:40:45 +00:00
|
|
|
func removePieceCompletionStorage(snapDir string) {
|
|
|
|
_ = os.RemoveAll(filepath.Join(snapDir, "db"))
|
|
|
|
_ = os.RemoveAll(filepath.Join(snapDir, ".torrent.db"))
|
|
|
|
_ = os.RemoveAll(filepath.Join(snapDir, ".torrent.bolt.db"))
|
|
|
|
_ = os.RemoveAll(filepath.Join(snapDir, ".torrent.db-shm"))
|
|
|
|
_ = os.RemoveAll(filepath.Join(snapDir, ".torrent.db-wal"))
|
2022-02-04 05:42:55 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 09:54:20 +00:00
|
|
|
func StartGrpc(snServer *downloader.GrpcServer, addr string, creds *credentials.TransportCredentials) (*grpc.Server, error) {
|
2021-11-08 13:40:56 +00:00
|
|
|
lis, err := net.Listen("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not create listener: %w, addr=%s", err, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
streamInterceptors []grpc.StreamServerInterceptor
|
|
|
|
unaryInterceptors []grpc.UnaryServerInterceptor
|
|
|
|
)
|
|
|
|
streamInterceptors = append(streamInterceptors, grpc_recovery.StreamServerInterceptor())
|
|
|
|
unaryInterceptors = append(unaryInterceptors, grpc_recovery.UnaryServerInterceptor())
|
|
|
|
|
|
|
|
//if metrics.Enabled {
|
|
|
|
// streamInterceptors = append(streamInterceptors, grpc_prometheus.StreamServerInterceptor)
|
|
|
|
// unaryInterceptors = append(unaryInterceptors, grpc_prometheus.UnaryServerInterceptor)
|
|
|
|
//}
|
|
|
|
|
|
|
|
opts := []grpc.ServerOption{
|
|
|
|
// https://github.com/grpc/grpc-go/issues/3171#issuecomment-552796779
|
|
|
|
grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
|
|
|
|
MinTime: 10 * time.Second,
|
|
|
|
PermitWithoutStream: true,
|
|
|
|
}),
|
|
|
|
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(streamInterceptors...)),
|
|
|
|
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(unaryInterceptors...)),
|
|
|
|
}
|
|
|
|
if creds == nil {
|
|
|
|
// no specific opts
|
|
|
|
} else {
|
|
|
|
opts = append(opts, grpc.Creds(*creds))
|
|
|
|
}
|
|
|
|
grpcServer := grpc.NewServer(opts...)
|
|
|
|
reflection.Register(grpcServer) // Register reflection service on gRPC server.
|
|
|
|
if snServer != nil {
|
2021-12-14 10:13:17 +00:00
|
|
|
proto_downloader.RegisterDownloaderServer(grpcServer, snServer)
|
2021-12-06 12:03:46 +00:00
|
|
|
}
|
2021-11-08 13:40:56 +00:00
|
|
|
|
|
|
|
//if metrics.Enabled {
|
|
|
|
// grpc_prometheus.Register(grpcServer)
|
|
|
|
//}
|
|
|
|
|
2021-12-14 10:13:17 +00:00
|
|
|
healthServer := health.NewServer()
|
|
|
|
grpc_health_v1.RegisterHealthServer(grpcServer, healthServer)
|
|
|
|
|
2021-11-08 13:40:56 +00:00
|
|
|
go func() {
|
2021-12-14 10:13:17 +00:00
|
|
|
defer healthServer.Shutdown()
|
2021-11-08 13:40:56 +00:00
|
|
|
if err := grpcServer.Serve(lis); err != nil {
|
|
|
|
log.Error("gRPC server stop", "err", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
log.Info("Started gRPC server", "on", addr)
|
|
|
|
return grpcServer, nil
|
|
|
|
}
|