Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: command to convert peers.txt into addrbook.json #2623

Merged
merged 21 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions cmd/celestia-appd/cmd/addrbook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cmd

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/p2p/pex"
)

const (
// routabilityStrict is a hard-coded config value for the address book.
// See https://github.com/celestiaorg/celestia-core/blob/793ece9bbd732aec3e09018e37dc31f4bfe122d9/config/config.go#L540-L542
routabilityStrict = true
rootulp marked this conversation as resolved.
Show resolved Hide resolved
)

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 be the output filename. The address book is saved to the output file in JSON format.\n",
rootulp marked this conversation as resolved.
Show resolved Hide resolved
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")

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
}
1 change: 1 addition & 0 deletions cmd/celestia-appd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading