Skip to content

Commit

Permalink
refactor(protocolproxy): address PR comments
Browse files Browse the repository at this point in the history
renames, reconfigured architecture, etc
  • Loading branch information
hannahhoward committed Sep 20, 2022
1 parent 75d18d7 commit 955f824
Show file tree
Hide file tree
Showing 17 changed files with 371 additions and 449 deletions.
154 changes: 154 additions & 0 deletions cmd/booster-bitswap/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package main

import (
"context"
"crypto/rand"
"errors"
"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"
)

type PeerIDAPI interface {
DealsGetBitswapPeerID(ctx context.Context) (peer.ID, error)
DealsSetBitswapPeerID(ctx context.Context, p peer.ID) error
}

func configureRepo(ctx context.Context, cfgDir string, papi PeerIDAPI, overrideExistingPeerID bool) (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)
if err != nil {
return nil, err
}

selfPid, err := peer.IDFromPrivateKey(peerkey)
if err != nil {
return nil, err
}
existingPid, err := papi.DealsGetBitswapPeerID(ctx)
if err != nil {
return nil, err
}
peerIDNotSet := existingPid == peer.ID("")
matchesPid := existingPid == selfPid
log.Infow("get/set peer id of bitswap from boost", "local", selfPid.String(), "boost", existingPid.String(), "boost not set", peerIDNotSet, "override", overrideExistingPeerID)
// error if a peer id is set that is different and we aren't overriding
if !peerIDNotSet && !matchesPid && !overrideExistingPeerID {
return nil, errors.New("bitswap peer id does not match boost node configuration. use --override-peer-id to force a change")
}
if peerIDNotSet || (!matchesPid && overrideExistingPeerID) {
err = papi.DealsSetBitswapPeerID(ctx, selfPid)
if err != nil {
return nil, err
}
}
return peerkey, nil
}

func setupHost(ctx context.Context, cfgDir string, port int, papi PeerIDAPI) (host.Host, error) {
peerKey, err := configureRepo(ctx, cfgDir, papi, 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) (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
}

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{
&cli.StringFlag{
Name: "api-boost",
Usage: "the endpoint for the boost API",
Required: true,
},
},
Action: func(cctx *cli.Context) error {

ctx := lcli.ReqContext(cctx)

// Connect to the Boost API
boostAPIInfo := cctx.String("api-boost")
bapi, bcloser, err := getBoostAPI(ctx, boostAPIInfo)
if err != nil {
return fmt.Errorf("getting boost API: %w", err)
}
defer bcloser()
repoDir := cctx.String(FlagRepo.Name)

_, err = configureRepo(ctx, repoDir, bapi, true)
return err
},
}
9 changes: 9 additions & 0 deletions cmd/booster-bitswap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import (

var log = logging.Logger("booster")

var FlagRepo = &cli.StringFlag{
Name: "repo",
Usage: "repo directory for Booster bitswap",
Value: "~/.booster-bitswap",
EnvVars: []string{"BOOST_BITSWAP_REPO"},
}

func main() {
app := &cli.App{
Name: "booster-bitswap",
Expand All @@ -19,8 +26,10 @@ func main() {
Version: build.UserVersion(),
Flags: []cli.Flag{
cliutil.FlagVeryVerbose,
FlagRepo,
},
Commands: []*cli.Command{
initCmd,
runCmd,
},
}
Expand Down
33 changes: 7 additions & 26 deletions cmd/booster-bitswap/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"path"
"strings"

"github.com/filecoin-project/boost/api"
Expand Down Expand Up @@ -48,11 +46,6 @@ var runCmd = &cli.Command{
Usage: "the endpoint for the tracing exporter",
Value: "http://tempo:14268/api/traces",
},
&cli.BoolFlag{
Name: "override-peer-id",
Usage: "if present, forces a change to boost's peer id for bitswap",
Value: false,
},
},
Action: func(cctx *cli.Context) error {
if cctx.Bool("pprof") {
Expand Down Expand Up @@ -90,20 +83,23 @@ var runCmd = &cli.Command{
remoteStore := remoteblockstore.NewRemoteBlockstore(bapi)
// Create the server API
port := cctx.Int("port")
server := NewBitswapServer(port, remoteStore, bapi)
repoDir := cctx.String(FlagRepo.Name)
host, err := setupHost(ctx, repoDir, port, bapi)

// Start the server
server := NewBitswapServer(remoteStore, host)

addrs, err := bapi.NetAddrsListen(ctx)
if err != nil {
return fmt.Errorf("getting boost API addrs: %w", err)
}

overrideExistingPeerID := cctx.Bool("override-peer-id")
// Start the server
log.Infof("Starting booster-bitswap node on port %d", port)
err = server.Start(ctx, dataDirPath(cctx), addrs, overrideExistingPeerID)
err = server.Start(ctx, addrs)
if err != nil {
return err
}

// Monitor for shutdown.
<-ctx.Done()

Expand Down Expand Up @@ -138,18 +134,3 @@ func getBoostAPI(ctx context.Context, ai string) (api.Boost, jsonrpc.ClientClose

return api, closer, nil
}

func dataDirPath(ctx *cli.Context) string {
dataDir := ctx.String("data-dir")

if dataDir == "" {
homeDir, err := os.UserHomeDir()
if err != nil {
homeDir = "./"
}

dataDir = path.Join(homeDir, "/.booster-bitswap")
}

return dataDir
}
Loading

0 comments on commit 955f824

Please sign in to comment.