|
| 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 | +} |
0 commit comments