github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/cmd/blsync/main.go (about) 1 // Copyright 2022 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "context" 21 "fmt" 22 "io" 23 "os" 24 25 "github.com/ethereum/go-ethereum/beacon/blsync" 26 "github.com/ethereum/go-ethereum/cmd/utils" 27 "github.com/ethereum/go-ethereum/internal/flags" 28 "github.com/ethereum/go-ethereum/log" 29 "github.com/ethereum/go-ethereum/node" 30 "github.com/ethereum/go-ethereum/rpc" 31 "github.com/mattn/go-colorable" 32 "github.com/mattn/go-isatty" 33 "github.com/urfave/cli/v2" 34 ) 35 36 var ( 37 verbosityFlag = &cli.IntFlag{ 38 Name: "verbosity", 39 Usage: "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail", 40 Value: 3, 41 Category: flags.LoggingCategory, 42 } 43 vmoduleFlag = &cli.StringFlag{ 44 Name: "vmodule", 45 Usage: "Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=5,p2p=4)", 46 Value: "", 47 Hidden: true, 48 Category: flags.LoggingCategory, 49 } 50 ) 51 52 func main() { 53 app := flags.NewApp("beacon light syncer tool") 54 app.Flags = []cli.Flag{ 55 utils.BeaconApiFlag, 56 utils.BeaconApiHeaderFlag, 57 utils.BeaconThresholdFlag, 58 utils.BeaconNoFilterFlag, 59 utils.BeaconConfigFlag, 60 utils.BeaconGenesisRootFlag, 61 utils.BeaconGenesisTimeFlag, 62 utils.BeaconCheckpointFlag, 63 //TODO datadir for optional permanent database 64 utils.MainnetFlag, 65 utils.SepoliaFlag, 66 utils.GoerliFlag, 67 utils.BlsyncApiFlag, 68 utils.BlsyncJWTSecretFlag, 69 verbosityFlag, 70 vmoduleFlag, 71 } 72 app.Action = sync 73 74 if err := app.Run(os.Args); err != nil { 75 fmt.Fprintln(os.Stderr, err) 76 os.Exit(1) 77 } 78 } 79 80 func sync(ctx *cli.Context) error { 81 usecolor := (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb" 82 output := io.Writer(os.Stderr) 83 if usecolor { 84 output = colorable.NewColorable(os.Stderr) 85 } 86 verbosity := log.FromLegacyLevel(ctx.Int(verbosityFlag.Name)) 87 log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(output, verbosity, usecolor))) 88 89 // set up blsync 90 client := blsync.NewClient(ctx) 91 client.SetEngineRPC(makeRPCClient(ctx)) 92 client.Start() 93 94 // run until stopped 95 <-ctx.Done() 96 client.Stop() 97 return nil 98 } 99 100 func makeRPCClient(ctx *cli.Context) *rpc.Client { 101 if !ctx.IsSet(utils.BlsyncApiFlag.Name) { 102 log.Warn("No engine API target specified, performing a dry run") 103 return nil 104 } 105 if !ctx.IsSet(utils.BlsyncJWTSecretFlag.Name) { 106 utils.Fatalf("JWT secret parameter missing") //TODO use default if datadir is specified 107 } 108 109 engineApiUrl, jwtFileName := ctx.String(utils.BlsyncApiFlag.Name), ctx.String(utils.BlsyncJWTSecretFlag.Name) 110 var jwtSecret [32]byte 111 if jwt, err := node.ObtainJWTSecret(jwtFileName); err == nil { 112 copy(jwtSecret[:], jwt) 113 } else { 114 utils.Fatalf("Error loading or generating JWT secret: %v", err) 115 } 116 auth := node.NewJWTAuth(jwtSecret) 117 cl, err := rpc.DialOptions(context.Background(), engineApiUrl, rpc.WithHTTPAuth(auth)) 118 if err != nil { 119 utils.Fatalf("Could not create RPC client: %v", err) 120 } 121 return cl 122 }