Skip to content

Commit 4e30068

Browse files
committed
refactor(protocolproxy): address PR comments
renames, reconfigured architecture, etc
1 parent 75d18d7 commit 4e30068

File tree

17 files changed

+373
-449
lines changed

17 files changed

+373
-449
lines changed

cmd/booster-bitswap/init.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"crypto/rand"
6+
"errors"
7+
"fmt"
8+
"os"
9+
"path/filepath"
10+
11+
lcli "github.com/filecoin-project/lotus/cli"
12+
"github.com/libp2p/go-libp2p"
13+
crypto "github.com/libp2p/go-libp2p-core/crypto"
14+
"github.com/libp2p/go-libp2p-core/host"
15+
"github.com/libp2p/go-libp2p-core/network"
16+
peer "github.com/libp2p/go-libp2p-core/peer"
17+
"github.com/libp2p/go-libp2p/p2p/muxer/mplex"
18+
"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
19+
quic "github.com/libp2p/go-libp2p/p2p/transport/quic"
20+
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
21+
"github.com/urfave/cli/v2"
22+
)
23+
24+
type PeerIDAPI interface {
25+
DealsGetBitswapPeerID(ctx context.Context) (peer.ID, error)
26+
DealsSetBitswapPeerID(ctx context.Context, p peer.ID) error
27+
}
28+
29+
func configureRepo(ctx context.Context, cfgDir string, papi PeerIDAPI, overrideExistingPeerID bool) (crypto.PrivKey, error) {
30+
if cfgDir == "" {
31+
return nil, fmt.Errorf("dataDir must be set")
32+
}
33+
34+
if err := os.MkdirAll(cfgDir, 0744); err != nil {
35+
return nil, err
36+
}
37+
38+
peerkey, err := loadPeerKey(cfgDir)
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
selfPid, err := peer.IDFromPrivateKey(peerkey)
44+
if err != nil {
45+
return nil, err
46+
}
47+
existingPid, err := papi.DealsGetBitswapPeerID(ctx)
48+
if err != nil {
49+
return nil, err
50+
}
51+
peerIDNotSet := existingPid == peer.ID("")
52+
matchesPid := existingPid == selfPid
53+
log.Infow("get/set peer id of bitswap from boost", "local", selfPid.String(), "boost", existingPid.String(), "boost not set", peerIDNotSet, "override", overrideExistingPeerID)
54+
// error if a peer id is set that is different and we aren't overriding
55+
if !peerIDNotSet && !matchesPid && !overrideExistingPeerID {
56+
return nil, errors.New("bitswap peer id does not match boost node configuration. use --override-peer-id to force a change")
57+
}
58+
if peerIDNotSet || (!matchesPid && overrideExistingPeerID) {
59+
err = papi.DealsSetBitswapPeerID(ctx, selfPid)
60+
if err != nil {
61+
return nil, err
62+
}
63+
}
64+
return peerkey, nil
65+
}
66+
67+
func setupHost(ctx context.Context, cfgDir string, port int, papi PeerIDAPI) (host.Host, error) {
68+
peerKey, err := configureRepo(ctx, cfgDir, papi, false)
69+
if err != nil {
70+
return nil, err
71+
}
72+
return libp2p.New(
73+
libp2p.ListenAddrStrings(
74+
fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", port),
75+
fmt.Sprintf("/ip4/0.0.0.0/udp/%d/quic", port),
76+
),
77+
libp2p.Transport(tcp.NewTCPTransport),
78+
libp2p.Transport(quic.NewTransport),
79+
libp2p.Muxer("/mplex/6.7.0", mplex.DefaultTransport),
80+
libp2p.Muxer("/yamux/1.0.0", yamux.DefaultTransport),
81+
libp2p.Identity(peerKey),
82+
libp2p.ResourceManager(network.NullResourceManager),
83+
)
84+
}
85+
86+
func loadPeerKey(cfgDir string) (crypto.PrivKey, error) {
87+
var peerkey crypto.PrivKey
88+
keyPath := filepath.Join(cfgDir, "libp2p.key")
89+
keyFile, err := os.ReadFile(keyPath)
90+
if err != nil {
91+
if !os.IsNotExist(err) {
92+
return nil, err
93+
}
94+
95+
log.Infof("Generating new peer key...")
96+
97+
key, _, err := crypto.GenerateEd25519Key(rand.Reader)
98+
if err != nil {
99+
return nil, err
100+
}
101+
peerkey = key
102+
103+
data, err := crypto.MarshalPrivateKey(key)
104+
if err != nil {
105+
return nil, err
106+
}
107+
108+
if err := os.WriteFile(keyPath, data, 0600); err != nil {
109+
return nil, err
110+
}
111+
} else {
112+
key, err := crypto.UnmarshalPrivateKey(keyFile)
113+
if err != nil {
114+
return nil, err
115+
}
116+
117+
peerkey = key
118+
}
119+
120+
if peerkey == nil {
121+
panic("sanity check: peer key is uninitialized")
122+
}
123+
124+
return peerkey, nil
125+
}
126+
127+
var initCmd = &cli.Command{
128+
Name: "init",
129+
Usage: "Init booster-bitswap config",
130+
Before: before,
131+
Flags: []cli.Flag{
132+
&cli.StringFlag{
133+
Name: "api-boost",
134+
Usage: "the endpoint for the boost API",
135+
Required: true,
136+
},
137+
},
138+
Action: func(cctx *cli.Context) error {
139+
140+
ctx := lcli.ReqContext(cctx)
141+
142+
// Connect to the Boost API
143+
boostAPIInfo := cctx.String("api-boost")
144+
bapi, bcloser, err := getBoostAPI(ctx, boostAPIInfo)
145+
if err != nil {
146+
return fmt.Errorf("getting boost API: %w", err)
147+
}
148+
defer bcloser()
149+
repoDir := cctx.String(FlagRepo.Name)
150+
151+
_, err = configureRepo(ctx, repoDir, bapi, true)
152+
return err
153+
},
154+
}

cmd/booster-bitswap/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ import (
1111

1212
var log = logging.Logger("booster")
1313

14+
var FlagRepo = &cli.StringFlag{
15+
Name: "repo",
16+
Usage: "repo directory for Booster bitswap",
17+
Value: "~/.booster-bitswap",
18+
EnvVars: []string{"BOOST_BITSWAP_REPO"},
19+
}
20+
1421
func main() {
1522
app := &cli.App{
1623
Name: "booster-bitswap",
@@ -19,8 +26,10 @@ func main() {
1926
Version: build.UserVersion(),
2027
Flags: []cli.Flag{
2128
cliutil.FlagVeryVerbose,
29+
FlagRepo,
2230
},
2331
Commands: []*cli.Command{
32+
initCmd,
2433
runCmd,
2534
},
2635
}

cmd/booster-bitswap/run.go

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"fmt"
66
"net/http"
77
_ "net/http/pprof"
8-
"os"
9-
"path"
108
"strings"
119

1210
"github.com/filecoin-project/boost/api"
@@ -48,11 +46,6 @@ var runCmd = &cli.Command{
4846
Usage: "the endpoint for the tracing exporter",
4947
Value: "http://tempo:14268/api/traces",
5048
},
51-
&cli.BoolFlag{
52-
Name: "override-peer-id",
53-
Usage: "if present, forces a change to boost's peer id for bitswap",
54-
Value: false,
55-
},
5649
},
5750
Action: func(cctx *cli.Context) error {
5851
if cctx.Bool("pprof") {
@@ -90,20 +83,25 @@ var runCmd = &cli.Command{
9083
remoteStore := remoteblockstore.NewRemoteBlockstore(bapi)
9184
// Create the server API
9285
port := cctx.Int("port")
93-
server := NewBitswapServer(port, remoteStore, bapi)
86+
repoDir := cctx.String(FlagRepo.Name)
87+
host, err := setupHost(ctx, repoDir, port, bapi)
88+
if err != nil {
89+
return fmt.Errorf("setting up libp2p host: %w", err)
90+
}
91+
// Start the server
92+
server := NewBitswapServer(remoteStore, host)
9493

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

100-
overrideExistingPeerID := cctx.Bool("override-peer-id")
101-
// Start the server
10299
log.Infof("Starting booster-bitswap node on port %d", port)
103-
err = server.Start(ctx, dataDirPath(cctx), addrs, overrideExistingPeerID)
100+
err = server.Start(ctx, addrs)
104101
if err != nil {
105102
return err
106103
}
104+
107105
// Monitor for shutdown.
108106
<-ctx.Done()
109107

@@ -138,18 +136,3 @@ func getBoostAPI(ctx context.Context, ai string) (api.Boost, jsonrpc.ClientClose
138136

139137
return api, closer, nil
140138
}
141-
142-
func dataDirPath(ctx *cli.Context) string {
143-
dataDir := ctx.String("data-dir")
144-
145-
if dataDir == "" {
146-
homeDir, err := os.UserHomeDir()
147-
if err != nil {
148-
homeDir = "./"
149-
}
150-
151-
dataDir = path.Join(homeDir, "/.booster-bitswap")
152-
}
153-
154-
return dataDir
155-
}

0 commit comments

Comments
 (0)