|
| 1 | +// Package dual provides an implementaiton of a split or "dual" dht, where two parallel instances |
| 2 | +// are maintained for the global internet and the local LAN respectively. |
| 3 | +package dual |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "sync" |
| 8 | + |
| 9 | + "github.com/ipfs/go-cid" |
| 10 | + ci "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/libp2p/go-libp2p-core/protocol" |
| 14 | + "github.com/libp2p/go-libp2p-core/routing" |
| 15 | + dht "github.com/libp2p/go-libp2p-kad-dht" |
| 16 | + helper "github.com/libp2p/go-libp2p-routing-helpers" |
| 17 | + |
| 18 | + "github.com/hashicorp/go-multierror" |
| 19 | +) |
| 20 | + |
| 21 | +// DHT implements the routing interface to provide two concrete DHT implementationts for use |
| 22 | +// in IPFS that are used to support both global network users and disjoint LAN usecases. |
| 23 | +type DHT struct { |
| 24 | + WAN *dht.IpfsDHT |
| 25 | + LAN *dht.IpfsDHT |
| 26 | +} |
| 27 | + |
| 28 | +// LanExtension is used to differentiate local protocol requests from those on the WAN DHT. |
| 29 | +const LanExtension protocol.ID = "/lan" |
| 30 | + |
| 31 | +// Assert that IPFS assumptions about interfaces aren't broken. These aren't a |
| 32 | +// guarantee, but we can use them to aid refactoring. |
| 33 | +var ( |
| 34 | + _ routing.ContentRouting = (*DHT)(nil) |
| 35 | + _ routing.Routing = (*DHT)(nil) |
| 36 | + _ routing.PeerRouting = (*DHT)(nil) |
| 37 | + _ routing.PubKeyFetcher = (*DHT)(nil) |
| 38 | + _ routing.ValueStore = (*DHT)(nil) |
| 39 | +) |
| 40 | + |
| 41 | +// New creates a new DualDHT instance. Options provided are forwarded on to the two concrete |
| 42 | +// IpfsDHT internal constructions, modulo additional options used by the Dual DHT to enforce |
| 43 | +// the LAN-vs-WAN distinction. |
| 44 | +// Note: query or routing table functional options provided as arguments to this function |
| 45 | +// will be overriden by this constructor. |
| 46 | +func New(ctx context.Context, h host.Host, options ...dht.Option) (*DHT, error) { |
| 47 | + wanOpts := append(options, |
| 48 | + dht.QueryFilter(dht.PublicQueryFilter), |
| 49 | + dht.RoutingTableFilter(dht.PublicRoutingTableFilter), |
| 50 | + ) |
| 51 | + wan, err := dht.New(ctx, h, wanOpts...) |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + |
| 56 | + // Unless overridden by user supplied options, the LAN DHT should default |
| 57 | + // to 'AutoServer' mode. |
| 58 | + lanOpts := append(options, |
| 59 | + dht.ProtocolExtension(LanExtension), |
| 60 | + dht.QueryFilter(dht.PrivateQueryFilter), |
| 61 | + dht.RoutingTableFilter(dht.PrivateRoutingTableFilter), |
| 62 | + ) |
| 63 | + if wan.Mode() != dht.ModeClient { |
| 64 | + lanOpts = append(lanOpts, dht.Mode(dht.ModeServer)) |
| 65 | + } |
| 66 | + lan, err := dht.New(ctx, h, lanOpts...) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + impl := DHT{wan, lan} |
| 72 | + return &impl, nil |
| 73 | +} |
| 74 | + |
| 75 | +// Close closes the DHT context. |
| 76 | +func (dht *DHT) Close() error { |
| 77 | + return multierror.Append(dht.WAN.Close(), dht.LAN.Close()).ErrorOrNil() |
| 78 | +} |
| 79 | + |
| 80 | +func (dht *DHT) activeWAN() bool { |
| 81 | + return dht.WAN.RoutingTable().Size() > 0 |
| 82 | +} |
| 83 | + |
| 84 | +// Provide adds the given cid to the content routing system. |
| 85 | +func (dht *DHT) Provide(ctx context.Context, key cid.Cid, announce bool) error { |
| 86 | + if dht.activeWAN() { |
| 87 | + return dht.WAN.Provide(ctx, key, announce) |
| 88 | + } |
| 89 | + return dht.LAN.Provide(ctx, key, announce) |
| 90 | +} |
| 91 | + |
| 92 | +// FindProvidersAsync searches for peers who are able to provide a given key |
| 93 | +func (dht *DHT) FindProvidersAsync(ctx context.Context, key cid.Cid, count int) <-chan peer.AddrInfo { |
| 94 | + reqCtx, cancel := context.WithCancel(ctx) |
| 95 | + outCh := make(chan peer.AddrInfo) |
| 96 | + wanCh := dht.WAN.FindProvidersAsync(reqCtx, key, count) |
| 97 | + lanCh := dht.LAN.FindProvidersAsync(reqCtx, key, count) |
| 98 | + zeroCount := (count == 0) |
| 99 | + go func() { |
| 100 | + defer cancel() |
| 101 | + defer close(outCh) |
| 102 | + |
| 103 | + found := make(map[peer.ID]struct{}, count) |
| 104 | + var pi peer.AddrInfo |
| 105 | + for (zeroCount || count > 0) && (wanCh != nil || lanCh != nil) { |
| 106 | + var ok bool |
| 107 | + select { |
| 108 | + case pi, ok = <-wanCh: |
| 109 | + if !ok { |
| 110 | + wanCh = nil |
| 111 | + continue |
| 112 | + } |
| 113 | + case pi, ok = <-lanCh: |
| 114 | + if !ok { |
| 115 | + lanCh = nil |
| 116 | + continue |
| 117 | + } |
| 118 | + } |
| 119 | + // already found |
| 120 | + if _, ok = found[pi.ID]; ok { |
| 121 | + continue |
| 122 | + } |
| 123 | + |
| 124 | + select { |
| 125 | + case outCh <- pi: |
| 126 | + found[pi.ID] = struct{}{} |
| 127 | + count-- |
| 128 | + case <-ctx.Done(): |
| 129 | + return |
| 130 | + } |
| 131 | + } |
| 132 | + }() |
| 133 | + return outCh |
| 134 | +} |
| 135 | + |
| 136 | +// FindPeer searches for a peer with given ID |
| 137 | +// Note: with signed peer records, we can change this to short circuit once either DHT returns. |
| 138 | +func (dht *DHT) FindPeer(ctx context.Context, pid peer.ID) (peer.AddrInfo, error) { |
| 139 | + var wg sync.WaitGroup |
| 140 | + wg.Add(2) |
| 141 | + var wanInfo, lanInfo peer.AddrInfo |
| 142 | + var wanErr, lanErr error |
| 143 | + go func() { |
| 144 | + defer wg.Done() |
| 145 | + wanInfo, wanErr = dht.WAN.FindPeer(ctx, pid) |
| 146 | + }() |
| 147 | + go func() { |
| 148 | + defer wg.Done() |
| 149 | + lanInfo, lanErr = dht.LAN.FindPeer(ctx, pid) |
| 150 | + }() |
| 151 | + |
| 152 | + wg.Wait() |
| 153 | + |
| 154 | + return peer.AddrInfo{ |
| 155 | + ID: pid, |
| 156 | + Addrs: append(wanInfo.Addrs, lanInfo.Addrs...), |
| 157 | + }, multierror.Append(wanErr, lanErr).ErrorOrNil() |
| 158 | +} |
| 159 | + |
| 160 | +// Bootstrap allows callers to hint to the routing system to get into a |
| 161 | +// Boostrapped state and remain there. |
| 162 | +func (dht *DHT) Bootstrap(ctx context.Context) error { |
| 163 | + erra := dht.WAN.Bootstrap(ctx) |
| 164 | + errb := dht.LAN.Bootstrap(ctx) |
| 165 | + return multierror.Append(erra, errb).ErrorOrNil() |
| 166 | +} |
| 167 | + |
| 168 | +// PutValue adds value corresponding to given Key. |
| 169 | +func (dht *DHT) PutValue(ctx context.Context, key string, val []byte, opts ...routing.Option) error { |
| 170 | + if dht.activeWAN() { |
| 171 | + return dht.WAN.PutValue(ctx, key, val, opts...) |
| 172 | + } |
| 173 | + return dht.LAN.PutValue(ctx, key, val, opts...) |
| 174 | +} |
| 175 | + |
| 176 | +// GetValue searches for the value corresponding to given Key. |
| 177 | +func (d *DHT) GetValue(ctx context.Context, key string, opts ...routing.Option) ([]byte, error) { |
| 178 | + lanCtx, cancelLan := context.WithCancel(ctx) |
| 179 | + defer cancelLan() |
| 180 | + |
| 181 | + var ( |
| 182 | + lanVal []byte |
| 183 | + lanErr error |
| 184 | + lanWaiter sync.WaitGroup |
| 185 | + ) |
| 186 | + lanWaiter.Add(1) |
| 187 | + go func() { |
| 188 | + defer lanWaiter.Done() |
| 189 | + lanVal, lanErr = d.LAN.GetValue(lanCtx, key, opts...) |
| 190 | + }() |
| 191 | + |
| 192 | + wanVal, wanErr := d.WAN.GetValue(ctx, key, opts...) |
| 193 | + if wanErr == nil { |
| 194 | + cancelLan() |
| 195 | + } |
| 196 | + lanWaiter.Wait() |
| 197 | + if wanErr != nil { |
| 198 | + if lanErr != nil { |
| 199 | + return nil, multierror.Append(wanErr, lanErr).ErrorOrNil() |
| 200 | + } |
| 201 | + return lanVal, nil |
| 202 | + } |
| 203 | + return wanVal, nil |
| 204 | +} |
| 205 | + |
| 206 | +// SearchValue searches for better values from this value |
| 207 | +func (dht *DHT) SearchValue(ctx context.Context, key string, opts ...routing.Option) (<-chan []byte, error) { |
| 208 | + p := helper.Parallel{Routers: []routing.Routing{dht.WAN, dht.LAN}, Validator: dht.WAN.Validator} |
| 209 | + return p.SearchValue(ctx, key, opts...) |
| 210 | +} |
| 211 | + |
| 212 | +// GetPublicKey returns the public key for the given peer. |
| 213 | +func (dht *DHT) GetPublicKey(ctx context.Context, pid peer.ID) (ci.PubKey, error) { |
| 214 | + p := helper.Parallel{Routers: []routing.Routing{dht.WAN, dht.LAN}, Validator: dht.WAN.Validator} |
| 215 | + return p.GetPublicKey(ctx, pid) |
| 216 | +} |
0 commit comments