Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 97d21ff

Browse files
authored
Merge pull request #1560 from OpenBazaar/proper-network-lookup-fix
Proper network lookup fix
2 parents 35844a3 + b478fd7 commit 97d21ff

File tree

4 files changed

+61
-30
lines changed

4 files changed

+61
-30
lines changed

ipfs/api_router.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
routing "gx/ipfs/QmYxUdYY9S6yg5tSPVin5GFTvtfsLauVcr7reHDD3dM8xf/go-libp2p-routing"
1616
ropts "gx/ipfs/QmYxUdYY9S6yg5tSPVin5GFTvtfsLauVcr7reHDD3dM8xf/go-libp2p-routing/options"
1717
pstore "gx/ipfs/QmaCTz9RkrU13bm9kMB54f7atgqM4qkjDZpRwRoJiWXEqs/go-libp2p-peerstore"
18+
record "gx/ipfs/QmbeHtaBy9nZsW4cHRcvgVY4CnDhXudE2Dr6qDxS7yg9rX/go-libp2p-record"
1819
)
1920

2021
var apiRouterHTTPClient = &http.Client{
@@ -32,13 +33,14 @@ var ErrNotStarted = errors.New("API router not started")
3233
// provides the features offerened by routing.ValueStore and marks the others as
3334
// unsupported.
3435
type APIRouter struct {
35-
uri string
36-
started chan (struct{})
36+
uri string
37+
started chan (struct{})
38+
validator record.Validator
3739
}
3840

3941
// NewAPIRouter creates a new APIRouter backed by the given URI.
40-
func NewAPIRouter(uri string) APIRouter {
41-
return APIRouter{uri: uri, started: make(chan (struct{}))}
42+
func NewAPIRouter(uri string, validator record.Validator) APIRouter {
43+
return APIRouter{uri: uri, started: make(chan (struct{})), validator: validator}
4244
}
4345

4446
func (r *APIRouter) Start(proxyDialer proxy.Dialer) {
@@ -79,7 +81,11 @@ func (r APIRouter) GetValue(ctx context.Context, key string, opts ...ropts.Optio
7981
defer resp.Body.Close()
8082

8183
log.Debugf("read value from %s", path)
82-
return ioutil.ReadAll(resp.Body)
84+
value, err := ioutil.ReadAll(resp.Body)
85+
if err != nil {
86+
return nil, err
87+
}
88+
return value, r.validator.Validate(key, value)
8389
}
8490

8591
// GetValues reads the value for the given key. The API does not return multiple

ipfs/caching_router.go

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package ipfs
22

33
import (
44
"context"
5+
"encoding/hex"
56
"errors"
6-
routinghelpers "gx/ipfs/QmRCrPXk2oUwpK1Cj2FXrUotRpddUxz56setkny2gz13Cx/go-libp2p-routing-helpers"
7-
dht "gx/ipfs/QmSY3nkMNLzh9GdbFKK5tT7YMfLpf52iUZ8ZRkr29MJaa5/go-libp2p-kad-dht"
8-
routing "gx/ipfs/QmYxUdYY9S6yg5tSPVin5GFTvtfsLauVcr7reHDD3dM8xf/go-libp2p-routing"
9-
ropts "gx/ipfs/QmYxUdYY9S6yg5tSPVin5GFTvtfsLauVcr7reHDD3dM8xf/go-libp2p-routing/options"
10-
record "gx/ipfs/QmbeHtaBy9nZsW4cHRcvgVY4CnDhXudE2Dr6qDxS7yg9rX/go-libp2p-record"
7+
8+
"gx/ipfs/QmSY3nkMNLzh9GdbFKK5tT7YMfLpf52iUZ8ZRkr29MJaa5/go-libp2p-kad-dht"
9+
ci "gx/ipfs/QmTW4SdgBWq9GjsBsHeUx8WuGxzhgzAf88UMH2w62PC8yK/go-libp2p-crypto"
10+
"gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer"
11+
"gx/ipfs/QmYxUdYY9S6yg5tSPVin5GFTvtfsLauVcr7reHDD3dM8xf/go-libp2p-routing"
12+
"gx/ipfs/QmYxUdYY9S6yg5tSPVin5GFTvtfsLauVcr7reHDD3dM8xf/go-libp2p-routing/options"
1113
)
1214

1315
var (
@@ -17,14 +19,12 @@ var (
1719
type CachingRouter struct {
1820
apiRouter *APIRouter
1921
routing.IpfsRouting
20-
RecordValidator record.Validator
2122
}
2223

2324
func NewCachingRouter(dht *dht.IpfsDHT, apiRouter *APIRouter) *CachingRouter {
2425
return &CachingRouter{
25-
apiRouter: apiRouter,
26-
IpfsRouting: dht,
27-
RecordValidator: dht.Validator,
26+
apiRouter: apiRouter,
27+
IpfsRouting: dht,
2828
}
2929
}
3030

@@ -43,28 +43,53 @@ func (r *CachingRouter) APIRouter() *APIRouter {
4343
func (r *CachingRouter) PutValue(ctx context.Context, key string, value []byte, opts ...ropts.Option) error {
4444
// Write to the tiered router in the background then write to the caching
4545
// router and return
46-
go r.IpfsRouting.PutValue(ctx, key, value, opts...)
47-
return r.apiRouter.PutValue(ctx, key, value, opts...)
46+
var err error
47+
if err = r.IpfsRouting.PutValue(ctx, key, value, opts...); err != nil {
48+
log.Errorf("ipfs dht put (%s): %s", hex.EncodeToString([]byte(key)), err)
49+
return err
50+
}
51+
if err = r.apiRouter.PutValue(ctx, key, value, opts...); err != nil {
52+
log.Errorf("api cache put (%s): %s", hex.EncodeToString([]byte(key)), err)
53+
}
54+
return err
4855
}
4956

5057
func (r *CachingRouter) GetValue(ctx context.Context, key string, opts ...ropts.Option) ([]byte, error) {
5158
// First check the DHT router. If it's successful return the value otherwise
5259
// continue on to check the other routers.
5360
val, err := r.IpfsRouting.GetValue(ctx, key, opts...)
54-
if err == nil {
55-
return val, r.apiRouter.PutValue(ctx, key, val, opts...)
61+
if err != nil && len(val) == 0 {
62+
// No values from the DHT, check the API cache
63+
log.Warningf("ipfs dht lookup was empty: %s", err.Error())
64+
if val, err = r.apiRouter.GetValue(ctx, key, opts...); err != nil && len(val) == 0 {
65+
// No values still, report NotFound
66+
return nil, routing.ErrNotFound
67+
}
5668
}
69+
if err := r.apiRouter.PutValue(ctx, key, val, opts...); err != nil {
70+
log.Errorf("api cache put found dht value (%s): %s", hex.EncodeToString([]byte(key)), err.Error())
71+
}
72+
return val, nil
73+
}
5774

58-
// Value miss; Check API router
59-
return r.apiRouter.GetValue(ctx, key, opts...)
75+
func (r *CachingRouter) GetPublicKey(ctx context.Context, p peer.ID) (ci.PubKey, error) {
76+
if dht, ok := r.IpfsRouting.(routing.PubKeyFetcher); ok {
77+
return dht.GetPublicKey(ctx, p)
78+
}
79+
return nil, routing.ErrNotSupported
6080
}
6181

6282
func (r *CachingRouter) SearchValue(ctx context.Context, key string, opts ...ropts.Option) (<-chan []byte, error) {
63-
return routinghelpers.Parallel{
64-
Routers: []routing.IpfsRouting{
65-
r.IpfsRouting,
66-
r.apiRouter,
67-
},
68-
Validator: r.RecordValidator,
69-
}.SearchValue(ctx, key, opts...)
83+
// TODO: Restore parallel lookup once validation is properly applied to
84+
// the apiRouter results ensuring it doesn't return invalid records before the
85+
// IpfsRouting object can. For some reason the validation is not being considered
86+
// on returned results.
87+
return r.IpfsRouting.SearchValue(ctx, key, opts...)
88+
//return routinghelpers.Parallel{
89+
//Routers: []routing.IpfsRouting{
90+
//r.IpfsRouting,
91+
//r.apiRouter,
92+
//},
93+
//Validator: r.RecordValidator,
94+
//}.SearchValue(ctx, key, opts...)
7095
}

ipfs/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func constructRouting(ctx context.Context, host p2phost.Host, dstore ds.Batching
6767
if err != nil {
6868
return nil, err
6969
}
70-
apiRouter := NewAPIRouter(routerCacheURI)
70+
apiRouter := NewAPIRouter(routerCacheURI, dhtRouting.Validator)
7171
cachingRouter := NewCachingRouter(dhtRouting, &apiRouter)
7272
return cachingRouter, nil
7373
}
@@ -99,7 +99,7 @@ func constructTestnetRouting(ctx context.Context, host p2phost.Host, dstore ds.B
9999
if err != nil {
100100
return nil, err
101101
}
102-
apiRouter := NewAPIRouter(routerCacheURI)
102+
apiRouter := NewAPIRouter(routerCacheURI, dhtRouting.Validator)
103103
cachingRouter := NewCachingRouter(dhtRouting, &apiRouter)
104104
return cachingRouter, nil
105105
}

mobile/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func constructMobileRouting(ctx context.Context, host p2phost.Host, dstore ds.Ba
269269
if err != nil {
270270
return nil, err
271271
}
272-
apiRouter := ipfs.NewAPIRouter(schema.IPFSCachingRouterDefaultURI)
272+
apiRouter := ipfs.NewAPIRouter(schema.IPFSCachingRouterDefaultURI, dhtRouting.Validator)
273273
apiRouter.Start(nil)
274274
cachingRouter := ipfs.NewCachingRouter(dhtRouting, &apiRouter)
275275
return cachingRouter, nil

0 commit comments

Comments
 (0)