@@ -2,12 +2,14 @@ package ipfs
22
33import  (
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
1315var  (
@@ -17,14 +19,12 @@ var (
1719type  CachingRouter  struct  {
1820	apiRouter  * APIRouter 
1921	routing.IpfsRouting 
20- 	RecordValidator  record.Validator 
2122}
2223
2324func  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 {
4343func  (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
5057func  (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
6282func  (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}
0 commit comments