github.com/evdatsion/aphelion-dpos-bft@v0.32.1/tools/tm-signer-harness/main.go (about) 1 package main 2 3 import ( 4 "flag" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "time" 10 11 "github.com/evdatsion/aphelion-dpos-bft/crypto/ed25519" 12 "github.com/evdatsion/aphelion-dpos-bft/libs/log" 13 "github.com/evdatsion/aphelion-dpos-bft/privval" 14 "github.com/evdatsion/aphelion-dpos-bft/tools/tm-signer-harness/internal" 15 "github.com/evdatsion/aphelion-dpos-bft/version" 16 ) 17 18 const ( 19 defaultAcceptRetries = 100 20 defaultBindAddr = "tcp://127.0.0.1:0" 21 defaultTMHome = "~/.tendermint" 22 defaultAcceptDeadline = 1 23 defaultConnDeadline = 3 24 defaultExtractKeyOutput = "./signing.key" 25 ) 26 27 var logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout)) 28 29 // Command line flags 30 var ( 31 flagAcceptRetries int 32 flagBindAddr string 33 flagTMHome string 34 flagKeyOutputPath string 35 ) 36 37 // Command line commands 38 var ( 39 rootCmd *flag.FlagSet 40 runCmd *flag.FlagSet 41 extractKeyCmd *flag.FlagSet 42 versionCmd *flag.FlagSet 43 ) 44 45 func init() { 46 rootCmd = flag.NewFlagSet("root", flag.ExitOnError) 47 rootCmd.Usage = func() { 48 fmt.Println(`Remote signer test harness for Tendermint. 49 50 Usage: 51 tm-signer-harness <command> [flags] 52 53 Available Commands: 54 extract_key Extracts a signing key from a local Tendermint instance 55 help Help on the available commands 56 run Runs the test harness 57 version Display version information and exit 58 59 Use "tm-signer-harness help <command>" for more information about that command.`) 60 fmt.Println("") 61 } 62 63 runCmd = flag.NewFlagSet("run", flag.ExitOnError) 64 runCmd.IntVar(&flagAcceptRetries, "accept-retries", defaultAcceptRetries, "The number of attempts to listen for incoming connections") 65 runCmd.StringVar(&flagBindAddr, "addr", defaultBindAddr, "Bind to this address for the testing") 66 runCmd.StringVar(&flagTMHome, "tmhome", defaultTMHome, "Path to the Tendermint home directory") 67 runCmd.Usage = func() { 68 fmt.Println(`Runs the remote signer test harness for Tendermint. 69 70 Usage: 71 tm-signer-harness run [flags] 72 73 Flags:`) 74 runCmd.PrintDefaults() 75 fmt.Println("") 76 } 77 78 extractKeyCmd = flag.NewFlagSet("extract_key", flag.ExitOnError) 79 extractKeyCmd.StringVar(&flagKeyOutputPath, "output", defaultExtractKeyOutput, "Path to which signing key should be written") 80 extractKeyCmd.StringVar(&flagTMHome, "tmhome", defaultTMHome, "Path to the Tendermint home directory") 81 extractKeyCmd.Usage = func() { 82 fmt.Println(`Extracts a signing key from a local Tendermint instance for use in the remote 83 signer under test. 84 85 Usage: 86 tm-signer-harness extract_key [flags] 87 88 Flags:`) 89 extractKeyCmd.PrintDefaults() 90 fmt.Println("") 91 } 92 93 versionCmd = flag.NewFlagSet("version", flag.ExitOnError) 94 versionCmd.Usage = func() { 95 fmt.Println(` 96 Prints the Tendermint version for which this remote signer harness was built. 97 98 Usage: 99 tm-signer-harness version`) 100 fmt.Println("") 101 } 102 } 103 104 func runTestHarness(acceptRetries int, bindAddr, tmhome string) { 105 tmhome = internal.ExpandPath(tmhome) 106 cfg := internal.TestHarnessConfig{ 107 BindAddr: bindAddr, 108 KeyFile: filepath.Join(tmhome, "config", "priv_validator_key.json"), 109 StateFile: filepath.Join(tmhome, "data", "priv_validator_state.json"), 110 GenesisFile: filepath.Join(tmhome, "config", "genesis.json"), 111 AcceptDeadline: time.Duration(defaultAcceptDeadline) * time.Second, 112 AcceptRetries: acceptRetries, 113 ConnDeadline: time.Duration(defaultConnDeadline) * time.Second, 114 SecretConnKey: ed25519.GenPrivKey(), 115 ExitWhenComplete: true, 116 } 117 harness, err := internal.NewTestHarness(logger, cfg) 118 if err != nil { 119 logger.Error(err.Error()) 120 if therr, ok := err.(*internal.TestHarnessError); ok { 121 os.Exit(therr.Code) 122 } 123 os.Exit(internal.ErrOther) 124 } 125 harness.Run() 126 } 127 128 func extractKey(tmhome, outputPath string) { 129 keyFile := filepath.Join(internal.ExpandPath(tmhome), "config", "priv_validator_key.json") 130 stateFile := filepath.Join(internal.ExpandPath(tmhome), "data", "priv_validator_state.json") 131 fpv := privval.LoadFilePV(keyFile, stateFile) 132 pkb := [64]byte(fpv.Key.PrivKey.(ed25519.PrivKeyEd25519)) 133 if err := ioutil.WriteFile(internal.ExpandPath(outputPath), pkb[:32], 0644); err != nil { 134 logger.Info("Failed to write private key", "output", outputPath, "err", err) 135 os.Exit(1) 136 } 137 logger.Info("Successfully wrote private key", "output", outputPath) 138 } 139 140 func main() { 141 rootCmd.Parse(os.Args[1:]) 142 if rootCmd.NArg() == 0 || (rootCmd.NArg() == 1 && rootCmd.Arg(0) == "help") { 143 rootCmd.Usage() 144 os.Exit(0) 145 } 146 147 logger = log.NewFilter(logger, log.AllowInfo()) 148 149 switch rootCmd.Arg(0) { 150 case "help": 151 switch rootCmd.Arg(1) { 152 case "run": 153 runCmd.Usage() 154 case "extract_key": 155 extractKeyCmd.Usage() 156 case "version": 157 versionCmd.Usage() 158 default: 159 fmt.Printf("Unrecognized command: %s\n", rootCmd.Arg(1)) 160 os.Exit(1) 161 } 162 case "run": 163 runCmd.Parse(os.Args[2:]) 164 runTestHarness(flagAcceptRetries, flagBindAddr, flagTMHome) 165 case "extract_key": 166 extractKeyCmd.Parse(os.Args[2:]) 167 extractKey(flagTMHome, flagKeyOutputPath) 168 case "version": 169 fmt.Println(version.Version) 170 default: 171 fmt.Printf("Unrecognized command: %s\n", flag.Arg(0)) 172 os.Exit(1) 173 } 174 }