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: backport addrbook command (backport #2623 and #2783) #2796

Merged
merged 3 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
57 changes: 57 additions & 0 deletions cmd/celestia-appd/cmd/addrbook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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 {
fmt.Printf("Error parsing %s: %s\n", line, err)
continue
}
err = book.AddAddress(address, address)
if err != nil {
fmt.Printf("Error adding %s: %s\n", address, err)
continue
}
}

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 @@ -128,6 +128,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig encoding.Config) {
debugCmd,
config.Cmd(),
commands.CompactGoLevelDBCmd,
addrbookCommand(),
downloadGenesisCommand(),
)

Expand Down
Loading