|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/rand" |
| 6 | + "fmt" |
| 7 | + "github.com/filecoin-project/boost/cmd/booster-bitswap/bitswap" |
| 8 | + "github.com/filecoin-project/boost/itests/shared" |
| 9 | + carv2 "github.com/ipld/go-car/v2" |
| 10 | + "github.com/libp2p/go-libp2p/core/crypto" |
| 11 | + "github.com/libp2p/go-libp2p/core/host" |
| 12 | + "github.com/libp2p/go-libp2p/core/peer" |
| 13 | + "github.com/multiformats/go-multiaddr" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + "path" |
| 16 | + "sort" |
| 17 | + "testing" |
| 18 | + "time" |
| 19 | +) |
| 20 | + |
| 21 | +func TestMultiMinerBitswapRetrieval(t *testing.T) { |
| 22 | + shared.RunMultiminerRetrievalTest(t, func(ctx context.Context, t *testing.T, rt *shared.RetrievalTest) { |
| 23 | + miner1ApiInfo, err := rt.BoostAndMiner1.LotusMinerApiInfo() |
| 24 | + require.NoError(t, err) |
| 25 | + |
| 26 | + miner2ApiInfo, err := rt.BoostAndMiner2.LotusMinerApiInfo() |
| 27 | + require.NoError(t, err) |
| 28 | + |
| 29 | + fullNode2ApiInfo, err := rt.BoostAndMiner2.LotusFullNodeApiInfo() |
| 30 | + require.NoError(t, err) |
| 31 | + |
| 32 | + repoDir := t.TempDir() |
| 33 | + peerID, _, err := configureRepo(repoDir, true) |
| 34 | + require.NoError(t, err) |
| 35 | + |
| 36 | + runCtx, cancelRun := context.WithCancel(ctx) |
| 37 | + defer cancelRun() |
| 38 | + |
| 39 | + go func() { |
| 40 | + // Configure booster-bitswap to |
| 41 | + // - Get piece location information from the shared LID instance |
| 42 | + // - Get the piece data from either miner1 or miner2 (depending on the location info) |
| 43 | + apiInfo := []string{miner1ApiInfo, miner2ApiInfo} |
| 44 | + _ = runBoosterBitswap(runCtx, repoDir, apiInfo, fullNode2ApiInfo, "ws://localhost:8042") |
| 45 | + }() |
| 46 | + |
| 47 | + maddr, err := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/8888") |
| 48 | + require.NoError(t, err) |
| 49 | + |
| 50 | + privKey, _, err := crypto.GenerateECDSAKeyPair(rand.Reader) |
| 51 | + require.NoError(t, err) |
| 52 | + |
| 53 | + clientHost, err := createClientHost(privKey) |
| 54 | + require.NoError(t, err) |
| 55 | + |
| 56 | + t.Logf("waiting for server to come up") |
| 57 | + start := time.Now() |
| 58 | + require.Eventually(t, func() bool { |
| 59 | + err := connectToHost(ctx, clientHost, maddr, peerID) |
| 60 | + if err != nil { |
| 61 | + t.Logf("connecting to host: %s", err) |
| 62 | + return false |
| 63 | + } |
| 64 | + return true |
| 65 | + }, 30*time.Second, time.Second) |
| 66 | + t.Logf("booster-bitswap is up after %s", time.Since(start)) |
| 67 | + |
| 68 | + outPath := path.Join(t.TempDir(), "out.dat") |
| 69 | + fetchAddr := maddr.String() + "/p2p/" + peerID.String() |
| 70 | + err = runBoosterBitswapFetch(ctx, fetchAddr, rt.RootCid.String(), outPath) |
| 71 | + require.NoError(t, err) |
| 72 | + |
| 73 | + t.Logf("retrieval is done, compare root cid %s to downloaded CAR root cid", rt.RootCid) |
| 74 | + r, err := carv2.OpenReader(outPath) |
| 75 | + require.NoError(t, err) |
| 76 | + |
| 77 | + roots, err := r.Roots() |
| 78 | + require.NoError(t, err) |
| 79 | + require.Len(t, roots, 1) |
| 80 | + require.Equal(t, rt.RootCid, roots[0]) |
| 81 | + |
| 82 | + t.Logf("file retrieved successfully") |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +func connectToHost(ctx context.Context, clientHost host.Host, maddr multiaddr.Multiaddr, pid peer.ID) error { |
| 87 | + // Connect to host |
| 88 | + err := clientHost.Connect(ctx, peer.AddrInfo{ |
| 89 | + ID: pid, |
| 90 | + Addrs: []multiaddr.Multiaddr{maddr}, |
| 91 | + }) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + // Check host's libp2p protocols |
| 97 | + protos, err := clientHost.Peerstore().GetProtocols(pid) |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("getting protocols from peer store for %s: %w", pid, err) |
| 100 | + } |
| 101 | + sort.Slice(protos, func(i, j int) bool { |
| 102 | + return protos[i] < protos[j] |
| 103 | + }) |
| 104 | + fmt.Println("host libp2p protocols", "protocols", protos) |
| 105 | + p, err := clientHost.Peerstore().FirstSupportedProtocol(pid, bitswap.Protocols...) |
| 106 | + if err != nil { |
| 107 | + return fmt.Errorf("getting first supported protocol from peer store for %s: %w", pid, err) |
| 108 | + } |
| 109 | + if p == "" { |
| 110 | + return fmt.Errorf("host %s does not support any know bitswap protocols: %s", pid, bitswap.ProtocolStrings) |
| 111 | + } |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +func runBoosterBitswap(ctx context.Context, repo string, minerApiInfo []string, fullNodeApiInfo string, lidApiInfo string) error { |
| 116 | + app.Setup() |
| 117 | + |
| 118 | + args := []string{"booster-bitswap", |
| 119 | + "--repo=" + repo, |
| 120 | + "run", |
| 121 | + "--api-fullnode=" + fullNodeApiInfo, |
| 122 | + "--api-lid=" + lidApiInfo, |
| 123 | + "--api-version-check=false", |
| 124 | + } |
| 125 | + for _, apiInfo := range minerApiInfo { |
| 126 | + args = append(args, "--api-storage="+apiInfo) |
| 127 | + } |
| 128 | + return app.RunContext(ctx, args) |
| 129 | +} |
| 130 | + |
| 131 | +func runBoosterBitswapFetch(ctx context.Context, multiaddr string, rootCid string, outputPath string) error { |
| 132 | + args := []string{"booster-bitswap", "fetch", multiaddr, rootCid, outputPath} |
| 133 | + return app.RunContext(ctx, args) |
| 134 | +} |
0 commit comments