|
| 1 | +package coreapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" |
| 9 | + caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options" |
| 10 | + |
| 11 | + dag "gx/ipfs/QmNr4E8z9bGTztvHJktp7uQaMdx9p3r9Asrq6eYk7iCh4a/go-merkledag" |
| 12 | + offline "gx/ipfs/QmPuLWvxK1vg6ckKUpT53Dow9VLCcQGdL5Trwxa8PTLp7r/go-ipfs-exchange-offline" |
| 13 | + cidutil "gx/ipfs/QmPyxJ2QS7L5FhGkNYkNcXHGjDhvGHueJ4auqAstFHYxy5/go-cidutil" |
| 14 | + blockservice "gx/ipfs/QmQLG22wSEStiociTSKQpZAuuaaWoF1B3iKyjPFvWiTQ77/go-blockservice" |
| 15 | + peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer" |
| 16 | + routing "gx/ipfs/QmY9JUvS8kbgao3XbPh6WAV3ChE2nxGKhcGTHiwMC4gmcU/go-libp2p-routing" |
| 17 | + cid "gx/ipfs/QmZFbDTY9jfSBms2MchvYM9oYRbAF19K7Pby47yDBfpPrb/go-cid" |
| 18 | + pstore "gx/ipfs/Qmda4cPRvSRyox3SqgJN6DfSZGU5TtHufPTp9uXjFj71X6/go-libp2p-peerstore" |
| 19 | + blockstore "gx/ipfs/Qmeg56ecxRnVv7VWViMrDeEMoBHaNFMs4vQnyQrJ79Zz7i/go-ipfs-blockstore" |
| 20 | +) |
| 21 | + |
| 22 | +type DhtAPI CoreAPI |
| 23 | + |
| 24 | +func (api *DhtAPI) FindPeer(ctx context.Context, p peer.ID) (pstore.PeerInfo, error) { |
| 25 | + pi, err := api.node.Routing.FindPeer(ctx, peer.ID(p)) |
| 26 | + if err != nil { |
| 27 | + return pstore.PeerInfo{}, err |
| 28 | + } |
| 29 | + |
| 30 | + return pi, nil |
| 31 | +} |
| 32 | + |
| 33 | +func (api *DhtAPI) FindProviders(ctx context.Context, p coreiface.Path, opts ...caopts.DhtFindProvidersOption) (<-chan pstore.PeerInfo, error) { |
| 34 | + settings, err := caopts.DhtFindProvidersOptions(opts...) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + |
| 39 | + rp, err := api.core().ResolvePath(ctx, p) |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + |
| 44 | + numProviders := settings.NumProviders |
| 45 | + if numProviders < 1 { |
| 46 | + return nil, fmt.Errorf("number of providers must be greater than 0") |
| 47 | + } |
| 48 | + |
| 49 | + pchan := api.node.Routing.FindProvidersAsync(ctx, rp.Cid(), numProviders) |
| 50 | + return pchan, nil |
| 51 | +} |
| 52 | + |
| 53 | +func (api *DhtAPI) Provide(ctx context.Context, path coreiface.Path, opts ...caopts.DhtProvideOption) error { |
| 54 | + settings, err := caopts.DhtProvideOptions(opts...) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + if api.node.Routing == nil { |
| 60 | + return errors.New("cannot provide in offline mode") |
| 61 | + } |
| 62 | + |
| 63 | + rp, err := api.core().ResolvePath(ctx, path) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + c := rp.Cid() |
| 69 | + |
| 70 | + has, err := api.node.Blockstore.Has(c) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + if !has { |
| 76 | + return fmt.Errorf("block %s not found locally, cannot provide", c) |
| 77 | + } |
| 78 | + |
| 79 | + if settings.Recursive { |
| 80 | + err = provideKeysRec(ctx, api.node.Routing, api.node.Blockstore, []*cid.Cid{c}) |
| 81 | + } else { |
| 82 | + err = provideKeys(ctx, api.node.Routing, []*cid.Cid{c}) |
| 83 | + } |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +func provideKeys(ctx context.Context, r routing.IpfsRouting, cids []*cid.Cid) error { |
| 92 | + for _, c := range cids { |
| 93 | + err := r.Provide(ctx, c, true) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + } |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +func provideKeysRec(ctx context.Context, r routing.IpfsRouting, bs blockstore.Blockstore, cids []*cid.Cid) error { |
| 102 | + provided := cidutil.NewStreamingSet() |
| 103 | + |
| 104 | + errCh := make(chan error) |
| 105 | + go func() { |
| 106 | + dserv := dag.NewDAGService(blockservice.New(bs, offline.Exchange(bs))) |
| 107 | + for _, c := range cids { |
| 108 | + err := dag.EnumerateChildrenAsync(ctx, dag.GetLinksDirect(dserv), c, provided.Visitor(ctx)) |
| 109 | + if err != nil { |
| 110 | + errCh <- err |
| 111 | + } |
| 112 | + } |
| 113 | + }() |
| 114 | + |
| 115 | + for { |
| 116 | + select { |
| 117 | + case k := <-provided.New: |
| 118 | + err := r.Provide(ctx, k, true) |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + case err := <-errCh: |
| 123 | + return err |
| 124 | + case <-ctx.Done(): |
| 125 | + return ctx.Err() |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func (api *DhtAPI) core() coreiface.CoreAPI { |
| 131 | + return (*CoreAPI)(api) |
| 132 | +} |
0 commit comments