|
1 | 1 | package dht |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | 4 | "context" |
6 | 5 | "errors" |
7 | 6 | "fmt" |
@@ -32,7 +31,6 @@ import ( |
32 | 31 | goprocessctx "github.com/jbenet/goprocess/context" |
33 | 32 | "github.com/multiformats/go-base32" |
34 | 33 | ma "github.com/multiformats/go-multiaddr" |
35 | | - "github.com/multiformats/go-multihash" |
36 | 34 | "go.opencensus.io/tag" |
37 | 35 | "go.uber.org/zap" |
38 | 36 | ) |
@@ -101,8 +99,7 @@ type IpfsDHT struct { |
101 | 99 | ctx context.Context |
102 | 100 | proc goprocess.Process |
103 | 101 |
|
104 | | - strmap map[peer.ID]*messageSender |
105 | | - smlk sync.Mutex |
| 102 | + protoMessenger *ProtocolMessenger |
106 | 103 |
|
107 | 104 | plk sync.Mutex |
108 | 105 |
|
@@ -183,6 +180,7 @@ func New(ctx context.Context, h host.Host, options ...Option) (*IpfsDHT, error) |
183 | 180 | dht.enableValues = cfg.enableValues |
184 | 181 |
|
185 | 182 | dht.Validator = cfg.validator |
| 183 | + dht.protoMessenger = NewProtocolMessenger(dht.host, dht.protocols, dht.Validator) |
186 | 184 |
|
187 | 185 | dht.auto = cfg.mode |
188 | 186 | switch cfg.mode { |
@@ -273,7 +271,6 @@ func makeDHT(ctx context.Context, h host.Host, cfg config) (*IpfsDHT, error) { |
273 | 271 | selfKey: kb.ConvertPeerID(h.ID()), |
274 | 272 | peerstore: h.Peerstore(), |
275 | 273 | host: h, |
276 | | - strmap: make(map[peer.ID]*messageSender), |
277 | 274 | birth: time.Now(), |
278 | 275 | protocols: protocols, |
279 | 276 | protocolsStrs: protocol.ConvertToStrings(protocols), |
@@ -477,67 +474,8 @@ func (dht *IpfsDHT) persistRTPeersInPeerStore() { |
477 | 474 | } |
478 | 475 | } |
479 | 476 |
|
480 | | -// putValueToPeer stores the given key/value pair at the peer 'p' |
481 | | -func (dht *IpfsDHT) putValueToPeer(ctx context.Context, p peer.ID, rec *recpb.Record) error { |
482 | | - pmes := pb.NewMessage(pb.Message_PUT_VALUE, rec.Key, 0) |
483 | | - pmes.Record = rec |
484 | | - rpmes, err := dht.sendRequest(ctx, p, pmes) |
485 | | - if err != nil { |
486 | | - logger.Debugw("failed to put value to peer", "to", p, "key", loggableKeyBytes(rec.Key), "error", err) |
487 | | - return err |
488 | | - } |
489 | | - |
490 | | - if !bytes.Equal(rpmes.GetRecord().Value, pmes.GetRecord().Value) { |
491 | | - logger.Infow("value not put correctly", "put-message", pmes, "get-message", rpmes) |
492 | | - return errors.New("value not put correctly") |
493 | | - } |
494 | | - |
495 | | - return nil |
496 | | -} |
497 | | - |
498 | 477 | var errInvalidRecord = errors.New("received invalid record") |
499 | 478 |
|
500 | | -// getValueOrPeers queries a particular peer p for the value for |
501 | | -// key. It returns either the value or a list of closer peers. |
502 | | -// NOTE: It will update the dht's peerstore with any new addresses |
503 | | -// it finds for the given peer. |
504 | | -func (dht *IpfsDHT) getValueOrPeers(ctx context.Context, p peer.ID, key string) (*recpb.Record, []*peer.AddrInfo, error) { |
505 | | - pmes, err := dht.getValueSingle(ctx, p, key) |
506 | | - if err != nil { |
507 | | - return nil, nil, err |
508 | | - } |
509 | | - |
510 | | - // Perhaps we were given closer peers |
511 | | - peers := pb.PBPeersToPeerInfos(pmes.GetCloserPeers()) |
512 | | - |
513 | | - if record := pmes.GetRecord(); record != nil { |
514 | | - // Success! We were given the value |
515 | | - logger.Debug("got value") |
516 | | - |
517 | | - // make sure record is valid. |
518 | | - err = dht.Validator.Validate(string(record.GetKey()), record.GetValue()) |
519 | | - if err != nil { |
520 | | - logger.Debug("received invalid record (discarded)") |
521 | | - // return a sentinal to signify an invalid record was received |
522 | | - err = errInvalidRecord |
523 | | - record = new(recpb.Record) |
524 | | - } |
525 | | - return record, peers, err |
526 | | - } |
527 | | - |
528 | | - if len(peers) > 0 { |
529 | | - return nil, peers, nil |
530 | | - } |
531 | | - |
532 | | - return nil, nil, routing.ErrNotFound |
533 | | -} |
534 | | - |
535 | | -// getValueSingle simply performs the get value RPC with the given parameters |
536 | | -func (dht *IpfsDHT) getValueSingle(ctx context.Context, p peer.ID, key string) (*pb.Message, error) { |
537 | | - pmes := pb.NewMessage(pb.Message_GET_VALUE, []byte(key), 0) |
538 | | - return dht.sendRequest(ctx, p, pmes) |
539 | | -} |
540 | | - |
541 | 479 | // getLocal attempts to retrieve the value from the datastore |
542 | 480 | func (dht *IpfsDHT) getLocal(key string) (*recpb.Record, error) { |
543 | 481 | logger.Debugw("finding value in datastore", "key", loggableKeyString(key)) |
@@ -627,17 +565,6 @@ func (dht *IpfsDHT) FindLocal(id peer.ID) peer.AddrInfo { |
627 | 565 | } |
628 | 566 | } |
629 | 567 |
|
630 | | -// findPeerSingle asks peer 'p' if they know where the peer with id 'id' is |
631 | | -func (dht *IpfsDHT) findPeerSingle(ctx context.Context, p peer.ID, id peer.ID) (*pb.Message, error) { |
632 | | - pmes := pb.NewMessage(pb.Message_FIND_NODE, []byte(id), 0) |
633 | | - return dht.sendRequest(ctx, p, pmes) |
634 | | -} |
635 | | - |
636 | | -func (dht *IpfsDHT) findProvidersSingle(ctx context.Context, p peer.ID, key multihash.Multihash) (*pb.Message, error) { |
637 | | - pmes := pb.NewMessage(pb.Message_GET_PROVIDERS, key, 0) |
638 | | - return dht.sendRequest(ctx, p, pmes) |
639 | | -} |
640 | | - |
641 | 568 | // nearestPeersToQuery returns the routing tables closest peers. |
642 | 569 | func (dht *IpfsDHT) nearestPeersToQuery(pmes *pb.Message, count int) []peer.ID { |
643 | 570 | closer := dht.routingTable.NearestPeers(kb.ConvertKey(string(pmes.GetKey())), count) |
@@ -778,15 +705,7 @@ func (dht *IpfsDHT) Host() host.Host { |
778 | 705 |
|
779 | 706 | // Ping sends a ping message to the passed peer and waits for a response. |
780 | 707 | func (dht *IpfsDHT) Ping(ctx context.Context, p peer.ID) error { |
781 | | - req := pb.NewMessage(pb.Message_PING, nil, 0) |
782 | | - resp, err := dht.sendRequest(ctx, p, req) |
783 | | - if err != nil { |
784 | | - return fmt.Errorf("sending request: %w", err) |
785 | | - } |
786 | | - if resp.Type != pb.Message_PING { |
787 | | - return fmt.Errorf("got unexpected response type: %v", resp.Type) |
788 | | - } |
789 | | - return nil |
| 708 | + return dht.protoMessenger.Ping(ctx, p) |
790 | 709 | } |
791 | 710 |
|
792 | 711 | // newContextWithLocalTags returns a new context.Context with the InstanceID and |
|
0 commit comments