-
Notifications
You must be signed in to change notification settings - Fork 76
LoadBalancer for bitswap (and later, more of libp2p) #786
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
Changes from all commits
77366ad
f5860c9
f0cc216
aa8e96c
d96e0e1
fbc3d7f
f7e9f20
931bfc5
4c74733
948f28d
b8a4858
b25944b
84fc4a9
ee13a72
a60a15d
156f62e
38af2ee
08f14b2
1ae395e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/rand" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| lcli "github.com/filecoin-project/lotus/cli" | ||
| "github.com/libp2p/go-libp2p" | ||
| crypto "github.com/libp2p/go-libp2p-core/crypto" | ||
| "github.com/libp2p/go-libp2p-core/host" | ||
| "github.com/libp2p/go-libp2p-core/network" | ||
| peer "github.com/libp2p/go-libp2p-core/peer" | ||
| "github.com/libp2p/go-libp2p/p2p/muxer/mplex" | ||
| "github.com/libp2p/go-libp2p/p2p/muxer/yamux" | ||
| quic "github.com/libp2p/go-libp2p/p2p/transport/quic" | ||
| "github.com/libp2p/go-libp2p/p2p/transport/tcp" | ||
| "github.com/urfave/cli/v2" | ||
| ) | ||
|
|
||
| func configureRepo(ctx context.Context, cfgDir string, createIfNotExist bool) (peer.ID, crypto.PrivKey, error) { | ||
| if cfgDir == "" { | ||
| return "", nil, fmt.Errorf("dataDir must be set") | ||
| } | ||
|
|
||
| if err := os.MkdirAll(cfgDir, 0744); err != nil { | ||
| return "", nil, err | ||
| } | ||
|
|
||
| peerkey, err := loadPeerKey(cfgDir, createIfNotExist) | ||
| if err != nil { | ||
| return "", nil, err | ||
| } | ||
|
|
||
| selfPid, err := peer.IDFromPrivateKey(peerkey) | ||
| if err != nil { | ||
| return "", nil, err | ||
| } | ||
|
|
||
| return selfPid, peerkey, nil | ||
| } | ||
|
|
||
| func setupHost(ctx context.Context, cfgDir string, port int) (host.Host, error) { | ||
| _, peerKey, err := configureRepo(ctx, cfgDir, false) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return libp2p.New( | ||
| libp2p.ListenAddrStrings( | ||
| fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", port), | ||
| fmt.Sprintf("/ip4/0.0.0.0/udp/%d/quic", port), | ||
| ), | ||
| libp2p.Transport(tcp.NewTCPTransport), | ||
| libp2p.Transport(quic.NewTransport), | ||
| libp2p.Muxer("/mplex/6.7.0", mplex.DefaultTransport), | ||
| libp2p.Muxer("/yamux/1.0.0", yamux.DefaultTransport), | ||
| libp2p.Identity(peerKey), | ||
| libp2p.ResourceManager(network.NullResourceManager), | ||
| ) | ||
| } | ||
|
|
||
| func loadPeerKey(cfgDir string, createIfNotExists bool) (crypto.PrivKey, error) { | ||
| var peerkey crypto.PrivKey | ||
| keyPath := filepath.Join(cfgDir, "libp2p.key") | ||
| keyFile, err := os.ReadFile(keyPath) | ||
| if err != nil { | ||
| if !os.IsNotExist(err) { | ||
| return nil, err | ||
| } | ||
| if os.IsNotExist(err) && !createIfNotExists { | ||
| return nil, err | ||
| } | ||
| log.Infof("Generating new peer key...") | ||
|
|
||
| key, _, err := crypto.GenerateEd25519Key(rand.Reader) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| peerkey = key | ||
|
|
||
| data, err := crypto.MarshalPrivateKey(key) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if err := os.WriteFile(keyPath, data, 0600); err != nil { | ||
| return nil, err | ||
| } | ||
| } else { | ||
| key, err := crypto.UnmarshalPrivateKey(keyFile) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| peerkey = key | ||
| } | ||
|
|
||
| if peerkey == nil { | ||
| panic("sanity check: peer key is uninitialized") | ||
| } | ||
|
|
||
| return peerkey, nil | ||
| } | ||
|
|
||
| var initCmd = &cli.Command{ | ||
| Name: "init", | ||
| Usage: "Init booster-bitswap config", | ||
| Before: before, | ||
| Flags: []cli.Flag{}, | ||
| Action: func(cctx *cli.Context) error { | ||
|
|
||
| ctx := lcli.ReqContext(cctx) | ||
|
|
||
| repoDir := cctx.String(FlagRepo.Name) | ||
|
|
||
| peerID, _, err := configureRepo(ctx, repoDir, true) | ||
| fmt.Println(peerID) | ||
| return err | ||
| }, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,14 +83,25 @@ var runCmd = &cli.Command{ | |
| remoteStore := remoteblockstore.NewRemoteBlockstore(bapi) | ||
| // Create the server API | ||
| port := cctx.Int("port") | ||
| server := NewBitswapServer(port, remoteStore) | ||
|
|
||
| repoDir := cctx.String(FlagRepo.Name) | ||
| host, err := setupHost(ctx, repoDir, port) | ||
| if err != nil { | ||
| return fmt.Errorf("setting up libp2p host: %w", err) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the peer key is not found, I think this will output an error like "setting up libp2p host: libp2p.key not found" |
||
| } | ||
| // Start the server | ||
| server := NewBitswapServer(remoteStore, host) | ||
|
|
||
| addrs, err := bapi.NetAddrsListen(ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("getting boost API addrs: %w", err) | ||
| } | ||
|
|
||
| log.Infof("Starting booster-bitswap node on port %d", port) | ||
| err = server.Start(ctx) | ||
| err = server.Start(ctx, addrs) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Monitor for shutdown. | ||
| <-ctx.Done() | ||
|
|
||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -206,6 +206,8 @@ type DealmakingConfig struct { | |
| // The time that can elapse before a download is considered stalled (and | ||
| // another concurrent download is allowed to start). | ||
| HttpTransferStallTimeout Duration | ||
|
|
||
| BitswapPeerID string | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a detailed comment about what this config value is. The comment ends up being copied into the config file itself, so it should be easy for users to understand what the value is and know what to set it to. |
||
| } | ||
|
|
||
| type FeeConfig struct { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest changing this error message to `FlagRepo.name + " is a required flag"