2021-11-08 13:40:56 +00:00
|
|
|
package main
|
2020-11-13 16:16:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-02-13 05:17:01 +00:00
|
|
|
"errors"
|
2020-11-13 16:16:47 +00:00
|
|
|
"fmt"
|
2023-09-28 04:38:29 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-10-04 04:01:02 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2023-09-28 04:38:29 +00:00
|
|
|
"time"
|
|
|
|
|
2021-12-24 08:03:51 +00:00
|
|
|
"github.com/c2h5oh/datasize"
|
2023-10-05 07:25:00 +00:00
|
|
|
mdbx2 "github.com/erigontech/mdbx-go/mdbx"
|
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"
|
2023-10-05 07:25:00 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/common/dir"
|
2022-11-20 03:41:30 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/downloader"
|
|
|
|
downloadercfg2 "github.com/ledgerwatch/erigon-lib/downloader/downloadercfg"
|
2023-10-05 07:25:00 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/downloader/snaptype"
|
2021-12-14 10:13:17 +00:00
|
|
|
proto_downloader "github.com/ledgerwatch/erigon-lib/gointerfaces/downloader"
|
2023-10-05 07:25:00 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
|
|
|
"github.com/ledgerwatch/erigon-lib/kv/mdbx"
|
2023-09-13 04:49:45 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2023-10-05 07:25:00 +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"
|
2023-09-13 08:21:43 +00:00
|
|
|
|
|
|
|
"github.com/ledgerwatch/erigon/cmd/downloader/downloadernat"
|
2023-10-05 07:25:00 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/hack/tool"
|
2023-09-13 08:21:43 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/utils"
|
|
|
|
"github.com/ledgerwatch/erigon/common/paths"
|
|
|
|
"github.com/ledgerwatch/erigon/p2p/nat"
|
|
|
|
"github.com/ledgerwatch/erigon/params"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/debug"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/logging"
|
2023-10-05 07:25:00 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/snapshotsync/snapcfg"
|
2020-11-13 16:16:47 +00:00
|
|
|
)
|
|
|
|
|
2023-09-15 06:46:59 +00:00
|
|
|
func main() {
|
|
|
|
ctx, cancel := common.RootContext()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := rootCmd.ExecuteContext(ctx); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 03:06:37 +00:00
|
|
|
var (
|
2023-09-12 05:18:47 +00:00
|
|
|
webseeds string
|
2023-09-13 08:21:43 +00:00
|
|
|
datadirCli, chain string
|
2023-09-12 05:18:47 +00:00
|
|
|
filePath 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
|
2023-03-08 08:30:00 +00:00
|
|
|
staticPeersStr string
|
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() {
|
2023-04-07 21:08:44 +00:00
|
|
|
utils.CobraFlags(rootCmd, debug.Flags, utils.MetricFlags, logging.Flags)
|
2020-11-13 16:16:47 +00:00
|
|
|
|
2022-02-22 17:39:48 +00:00
|
|
|
withDataDir(rootCmd)
|
2023-09-13 08:21:43 +00:00
|
|
|
rootCmd.Flags().StringVar(&chain, utils.ChainFlag.Name, utils.ChainFlag.Value, utils.ChainFlag.Usage)
|
2023-09-12 05:18:47 +00:00
|
|
|
rootCmd.Flags().StringVar(&webseeds, utils.WebSeedsFlag.Name, utils.WebSeedsFlag.Value, utils.WebSeedsFlag.Usage)
|
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)
|
2023-03-08 08:30:00 +00:00
|
|
|
rootCmd.Flags().StringVar(&staticPeersStr, utils.TorrentStaticPeersFlag.Name, utils.TorrentStaticPeersFlag.Value, utils.TorrentStaticPeersFlag.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)
|
2023-05-17 05:33:59 +00:00
|
|
|
rootCmd.PersistentFlags().BoolVar(&forceVerify, "verify", false, "Force verify data files if have .torrent files")
|
2021-12-14 10:13:17 +00:00
|
|
|
|
2023-09-12 05:18:47 +00:00
|
|
|
withDataDir(createTorrent)
|
|
|
|
withFile(createTorrent)
|
|
|
|
|
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")
|
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
|
|
|
|
2023-09-12 05:18:47 +00:00
|
|
|
rootCmd.AddCommand(createTorrent)
|
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
|
|
|
}
|
2023-09-12 05:18:47 +00:00
|
|
|
func withFile(cmd *cobra.Command) {
|
|
|
|
cmd.Flags().StringVar(&filePath, "file", "", "")
|
|
|
|
if err := cmd.MarkFlagFilename(utils.DataDirFlag.Name); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2020-11-13 16:16:47 +00:00
|
|
|
|
2023-10-04 04:01:02 +00:00
|
|
|
var logger log.Logger
|
2020-11-13 16:16:47 +00:00
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "",
|
2021-11-08 13:40:56 +00:00
|
|
|
Short: "snapshot downloader",
|
2023-05-17 05:33:59 +00:00
|
|
|
Example: "go run ./cmd/downloader --datadir <your_datadir> --downloader.api.addr 127.0.0.1:9093",
|
2020-11-13 16:16:47 +00:00
|
|
|
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
debug.Exit()
|
|
|
|
},
|
2023-10-04 04:01:02 +00:00
|
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
logger = debug.SetupCobra(cmd, "downloader")
|
|
|
|
logger.Info("Build info", "git_branch", params.GitBranch, "git_tag", params.GitTag, "git_commit", params.GitCommit)
|
|
|
|
},
|
2023-02-13 05:17:01 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2023-05-07 06:28:15 +00:00
|
|
|
if err := Downloader(cmd.Context(), logger); err != nil {
|
2023-02-13 05:17:01 +00:00
|
|
|
if !errors.Is(err, context.Canceled) {
|
2023-05-07 06:28:15 +00:00
|
|
|
logger.Error(err.Error())
|
2023-02-13 05:17:01 +00:00
|
|
|
}
|
|
|
|
return
|
2021-12-14 10:13:17 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2020-11-13 16:16:47 +00:00
|
|
|
|
2023-05-07 06:28:15 +00:00
|
|
|
func Downloader(ctx context.Context, logger log.Logger) error {
|
2022-06-07 03:24:50 +00:00
|
|
|
dirs := datadir.New(datadirCli)
|
2023-10-05 07:25:00 +00:00
|
|
|
if err := datadir.ApplyMigrations(dirs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-13 08:21:43 +00:00
|
|
|
if err := checkChainName(dirs, chain); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-04 04:01:02 +00:00
|
|
|
logger.Info("[snapshots] cli flags", "chain", chain, "addr", downloaderApiAddr, "datadir", dirs.DataDir, "ipv6-enabled", !disableIPV6, "ipv4-enabled", !disableIPV4, "download.rate", downloadRate.String(), "upload.rate", uploadRate.String(), "webseed", webseeds)
|
2023-09-12 05:18:47 +00:00
|
|
|
staticPeers := common.CliString2Array(staticPeersStr)
|
2021-12-14 10:13:17 +00:00
|
|
|
|
2023-02-13 05:17:01 +00:00
|
|
|
version := "erigon: " + params.VersionWithCommit(params.GitCommit)
|
2023-09-12 05:18:47 +00:00
|
|
|
cfg, err := downloadercfg2.New(dirs, version, torrentLogLevel, downloadRate, uploadRate, torrentPort, torrentConnsPerFile, torrentDownloadSlots, staticPeers, webseeds)
|
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-04-21 03:34:31 +00:00
|
|
|
|
2023-10-04 04:01:02 +00:00
|
|
|
cfg.ClientConfig.PieceHashersPerTorrent = runtime.NumCPU() * 4
|
2022-12-15 09:40:59 +00:00
|
|
|
cfg.ClientConfig.DisableIPv6 = disableIPV6
|
|
|
|
cfg.ClientConfig.DisableIPv4 = disableIPV4
|
|
|
|
|
2023-09-15 06:46:59 +00:00
|
|
|
natif, err := nat.Parse(natSetting)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid nat option %s: %w", natSetting, err)
|
|
|
|
}
|
2023-09-12 05:18:47 +00:00
|
|
|
downloadernat.DoNat(natif, cfg.ClientConfig, logger)
|
2023-05-10 03:25:53 +00:00
|
|
|
|
2023-10-04 02:57:37 +00:00
|
|
|
d, err := downloader.New(ctx, cfg, logger, log.LvlInfo)
|
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()
|
2023-10-04 04:01:02 +00:00
|
|
|
logger.Info("[snapshots] Start bittorrent server", "my_peer_id", fmt.Sprintf("%x", d.TorrentClient().PeerID()))
|
2023-05-17 05:33:59 +00:00
|
|
|
|
2023-10-04 05:34:45 +00:00
|
|
|
if forceVerify { // remove and create .torrent files (will re-read all snapshots)
|
|
|
|
if err = d.VerifyData(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 06:46:59 +00:00
|
|
|
d.MainLoopInBackground(false)
|
2021-12-14 10:13:17 +00:00
|
|
|
|
2023-09-13 08:21:43 +00:00
|
|
|
if err := addPreConfiguredHashes(ctx, d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-05-07 06:28:15 +00:00
|
|
|
grpcServer, err := StartGrpc(bittorrentServer, downloaderApiAddr, nil /* transportCredentials */, logger)
|
2021-12-14 10:13:17 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-09-12 05:18:47 +00:00
|
|
|
var createTorrent = &cobra.Command{
|
|
|
|
Use: "torrent_create",
|
2023-09-15 06:46:59 +00:00
|
|
|
Example: "go run ./cmd/downloader torrent_create --datadir=<your_datadir> --file=<relative_file_path>",
|
2023-09-12 05:18:47 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
//logger := debug.SetupCobra(cmd, "integration")
|
2023-09-15 06:46:59 +00:00
|
|
|
dirs := datadir.New(datadirCli)
|
2023-10-04 04:01:02 +00:00
|
|
|
err := downloader.BuildTorrentFilesIfNeed(cmd.Context(), dirs)
|
2023-09-12 05:18:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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 {
|
2023-10-04 04:01:02 +00:00
|
|
|
logger := debug.SetupCobra(cmd, "downloader")
|
|
|
|
if err := doPrintTorrentHashes(cmd.Context(), logger); err != nil {
|
|
|
|
log.Error(err.Error())
|
2022-01-05 10:14:37 +00:00
|
|
|
}
|
2023-10-04 04:01:02 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2021-12-14 10:13:17 +00:00
|
|
|
|
2023-10-04 04:01:02 +00:00
|
|
|
func doPrintTorrentHashes(ctx context.Context, logger log.Logger) error {
|
|
|
|
dirs := datadir.New(datadirCli)
|
2023-10-05 07:25:00 +00:00
|
|
|
if err := datadir.ApplyMigrations(dirs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-04 04:01:02 +00:00
|
|
|
if forceRebuild { // remove and create .torrent files (will re-read all snapshots)
|
|
|
|
//removePieceCompletionStorage(snapDir)
|
|
|
|
files, err := downloader.AllTorrentPaths(dirs)
|
2022-01-27 05:56:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-04 04:01:02 +00:00
|
|
|
for _, filePath := range files {
|
|
|
|
if err := os.Remove(filePath); err != nil {
|
2021-12-14 10:13:17 +00:00
|
|
|
return err
|
2021-05-08 08:45:40 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-04 04:01:02 +00:00
|
|
|
if err := downloader.BuildTorrentFilesIfNeed(ctx, dirs); err != nil {
|
|
|
|
return fmt.Errorf("BuildTorrentFilesIfNeed: %w", err)
|
2022-03-28 07:44:11 +00:00
|
|
|
}
|
2023-10-04 04:01:02 +00:00
|
|
|
}
|
2022-03-28 07:44:11 +00:00
|
|
|
|
2023-10-04 04:01:02 +00:00
|
|
|
res := map[string]string{}
|
|
|
|
torrents, err := downloader.AllTorrentSpecs(dirs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, t := range torrents {
|
|
|
|
// we don't release commitment history in this time. let's skip it here.
|
|
|
|
if strings.HasPrefix(t.DisplayName, "history/commitment") {
|
|
|
|
continue
|
2022-03-28 07:44:11 +00:00
|
|
|
}
|
2023-10-04 04:01:02 +00:00
|
|
|
if strings.HasPrefix(t.DisplayName, "idx/commitment") {
|
|
|
|
continue
|
2021-12-21 14:12:32 +00:00
|
|
|
}
|
2023-10-04 04:01:02 +00:00
|
|
|
res[t.DisplayName] = t.InfoHash.String()
|
|
|
|
}
|
|
|
|
serialized, err := toml.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if targetFile == "" {
|
|
|
|
fmt.Printf("%s\n", serialized)
|
2021-12-06 03:06:37 +00:00
|
|
|
return nil
|
2023-10-04 04:01:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
oldContent, err := os.ReadFile(targetFile)
|
|
|
|
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) {
|
|
|
|
logger.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
|
|
|
|
}
|
|
|
|
if err := os.WriteFile(targetFile, serialized, 0644); err != nil { // nolint
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-11-13 16:16:47 +00:00
|
|
|
}
|
2021-11-08 13:40:56 +00:00
|
|
|
|
2023-05-07 06:28:15 +00:00
|
|
|
func StartGrpc(snServer *downloader.GrpcServer, addr string, creds *credentials.TransportCredentials, logger log.Logger) (*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 {
|
2023-05-07 06:28:15 +00:00
|
|
|
logger.Error("gRPC server stop", "err", err)
|
2021-11-08 13:40:56 +00:00
|
|
|
}
|
|
|
|
}()
|
2023-05-07 06:28:15 +00:00
|
|
|
logger.Info("Started gRPC server", "on", addr)
|
2021-11-08 13:40:56 +00:00
|
|
|
return grpcServer, nil
|
|
|
|
}
|
2023-09-13 08:21:43 +00:00
|
|
|
|
|
|
|
// Add pre-configured
|
|
|
|
func addPreConfiguredHashes(ctx context.Context, d *downloader.Downloader) error {
|
|
|
|
for _, it := range snapcfg.KnownCfg(chain, nil, nil).Preverified {
|
|
|
|
if err := d.AddInfoHashAsMagnetLink(ctx, snaptype.Hex2InfoHash(it.Hash), it.Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkChainName(dirs datadir.Dirs, chainName string) error {
|
|
|
|
if !dir.FileExist(filepath.Join(dirs.Chaindata, "mdbx.dat")) {
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-05 07:25:00 +00:00
|
|
|
db := mdbx.NewMDBX(log.New()).
|
|
|
|
Path(dirs.Chaindata).Label(kv.ChainDB).
|
|
|
|
Flags(func(flags uint) uint { return flags | mdbx2.Accede }).
|
|
|
|
MustOpen()
|
2023-09-13 08:21:43 +00:00
|
|
|
defer db.Close()
|
|
|
|
if err := db.View(context.Background(), func(tx kv.Tx) error {
|
|
|
|
cc := tool.ChainConfig(tx)
|
|
|
|
if cc != nil && cc.ChainName != chainName {
|
|
|
|
return fmt.Errorf("datadir already was configured with --chain=%s. can't change to '%s'", cc.ChainName, chainName)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|