From 03beaef1d9ab27030ab2011b3ed52564b40eb013 Mon Sep 17 00:00:00 2001 From: Rootul P Date: Fri, 20 Oct 2023 18:19:55 -0400 Subject: [PATCH] feat: command to convert `peers.txt` into `addrbook.json` (#2623) Closes https://github.com/celestiaorg/celestia-app/issues/2619 ## Description Adds a new command to celestia-appd called `addrbook`. ``` ./build/celestia-appd addrbook --help Convert a list of peers into an address book. The first argument (peers.txt) should contain a new line separated list of peers. The format for a peer is `id@ip:port` or `id@domain:port`. The second argument (addrbook.json) should be the output filename. The address book is saved to the output file in JSON format. Usage: celestia-appd addrbook peers.txt addrbook.json [flags] ``` ## Testing I was able to run a node on arabica-10 with an `addrbook.json` that was created from [arabica-10/peers.txt](https://github.com/celestiaorg/networks/blob/master/arabica-10/peers.txt). Note, testing failed on mocha-4 due to https://github.com/celestiaorg/networks/issues/349 --- cmd/celestia-appd/cmd/addrbook.go | 55 +++++++++++++++++++++++++++++++ cmd/celestia-appd/cmd/root.go | 1 + 2 files changed, 56 insertions(+) create mode 100644 cmd/celestia-appd/cmd/addrbook.go diff --git a/cmd/celestia-appd/cmd/addrbook.go b/cmd/celestia-appd/cmd/addrbook.go new file mode 100644 index 0000000000..f363ed3626 --- /dev/null +++ b/cmd/celestia-appd/cmd/addrbook.go @@ -0,0 +1,55 @@ +package cmd + +import ( + "fmt" + "os" + "strings" + + "github.com/celestiaorg/celestia-app/app" + "github.com/spf13/cobra" + "github.com/tendermint/tendermint/p2p" + "github.com/tendermint/tendermint/p2p/pex" +) + +func addrbookCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "addrbook peers.txt addrbook.json", + Short: "Convert a list of peers into an address book", + Long: "Convert a list of peers into an address book.\n" + + "The first argument (peers.txt) should contain a new line separated list of peers. The format for a peer is `id@ip:port` or `id@domain:port`.\n" + + "The second argument (addrbook.json) should contain the complete file path, including both the directory path and the desired output file name, in the following format: `path/to/filename`. The address book is saved to the output file in JSON format.\n", + Args: cobra.ExactArgs(2), + RunE: func(cmd *cobra.Command, args []string) error { + inputFile := args[0] + outputFile := args[1] + + data, err := os.ReadFile(inputFile) + if err != nil { + return err + } + lines := strings.Split(string(data), "\n") + + routabilityStrict := app.DefaultConsensusConfig().P2P.AddrBookStrict + book := pex.NewAddrBook(outputFile, routabilityStrict) + for _, line := range lines { + if line == "" { + continue + } + address, err := p2p.NewNetAddressString(line) + if err != nil { + return err + } + err = book.AddAddress(address, address) + if err != nil { + return err + } + } + + book.Save() + fmt.Printf("Converted %s into %s\n", inputFile, outputFile) + return nil + }, + } + + return cmd +} diff --git a/cmd/celestia-appd/cmd/root.go b/cmd/celestia-appd/cmd/root.go index 63584c279c..be95396665 100644 --- a/cmd/celestia-appd/cmd/root.go +++ b/cmd/celestia-appd/cmd/root.go @@ -135,6 +135,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig encoding.Config) { debugCmd, config.Cmd(), commands.CompactGoLevelDBCmd, + addrbookCommand(), ) server.AddCommands(rootCmd, app.DefaultNodeHome, NewAppServer, createAppAndExport, addModuleInitFlags)