diff --git a/go.mod b/go.mod index 07e0fec6..a7f87f02 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,6 @@ require ( github.com/minio/sha256-simd v0.1.1 github.com/mr-tron/base58 v1.1.3 github.com/multiformats/go-multiaddr v0.2.0 - github.com/multiformats/go-multiaddr-net v0.1.1 github.com/multiformats/go-multihash v0.0.10 github.com/multiformats/go-varint v0.0.1 github.com/smola/gocompat v0.2.0 diff --git a/host/helpers.go b/host/helpers.go index aa7ae3a2..fd77cd1d 100644 --- a/host/helpers.go +++ b/host/helpers.go @@ -1,12 +1,7 @@ package host import ( - "errors" "github.com/libp2p/go-libp2p-core/peer" - "github.com/libp2p/go-libp2p-core/peerstore" - "github.com/libp2p/go-libp2p-core/routing" - ma "github.com/multiformats/go-multiaddr" - manet "github.com/multiformats/go-multiaddr-net" ) // InfoFromHost returns a peer.AddrInfo struct with the Host's ID and all of its Addrs. @@ -16,54 +11,3 @@ func InfoFromHost(h Host) *peer.AddrInfo { Addrs: h.Addrs(), } } - -// SignedRoutingStateFromHost returns a SignedRoutingState record containing -// the Host's listen addresses, signed with the Host's private key. -// -// By default, only publicly routable addresses will be included. -// To include loopback and LAN addresses, pass in the IncludeLocalAddrs option: -// -// state := SignedRoutingStateFromHost(h, IncludeLocalAddrs) -func SignedRoutingStateFromHost(h minimalHost, opts ...Option) (*routing.SignedRoutingState, error) { - cfg := config{} - for _, opt := range opts { - opt(&cfg) - } - - privKey := h.Peerstore().PrivKey(h.ID()) - if privKey == nil { - return nil, errors.New("unable to find host's private key in peerstore") - } - - var addrs []ma.Multiaddr - if cfg.includeLocalAddrs { - addrs = h.Addrs() - } else { - for _, a := range h.Addrs() { - if manet.IsPublicAddr(a) { - addrs = append(addrs, a) - } - } - } - - return routing.MakeSignedRoutingState(privKey, addrs) -} - -// IncludeLocalAddrs can be passed into SignedRoutingStateFromHost to -// produce a routing record with LAN and loopback addresses included. -func IncludeLocalAddrs(cfg *config) { - cfg.includeLocalAddrs = true -} - -// minimalHost is the subset of the Host interface that's required by -// SignedRoutingStateFromHost. -type minimalHost interface { - ID() peer.ID - Peerstore() peerstore.Peerstore - Addrs() []ma.Multiaddr -} - -type Option func(cfg *config) -type config struct { - includeLocalAddrs bool -} diff --git a/host/helpers_test.go b/host/helpers_test.go deleted file mode 100644 index 52cf261a..00000000 --- a/host/helpers_test.go +++ /dev/null @@ -1,131 +0,0 @@ -package host - -import ( - "context" - "github.com/libp2p/go-libp2p-core/crypto" - "github.com/libp2p/go-libp2p-core/peer" - "github.com/libp2p/go-libp2p-core/peerstore" - "github.com/libp2p/go-libp2p-core/routing" - "github.com/libp2p/go-libp2p-core/test" - ma "github.com/multiformats/go-multiaddr" - "testing" - "time" -) - -type mockHost struct { - fixedPrivKey crypto.PrivKey - addrs []ma.Multiaddr -} - -func (h *mockHost) Addrs() []ma.Multiaddr { - return h.addrs -} - -func (h *mockHost) Peerstore() peerstore.Peerstore { - return mockPeerstore{fixedPrivKey: h.fixedPrivKey} -} - -func (*mockHost) ID() peer.ID { return "" } - -type mockPeerstore struct { - fixedPrivKey crypto.PrivKey -} - -// the one method I care about... -func (m mockPeerstore) PrivKey(peer.ID) crypto.PrivKey { - return m.fixedPrivKey -} - -// so many other things in the Peerstore interface... -func (m mockPeerstore) Close() error { return nil } -func (m mockPeerstore) AddAddr(p peer.ID, addr ma.Multiaddr, ttl time.Duration) {} -func (m mockPeerstore) AddAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration) {} -func (m mockPeerstore) AddCertifiedAddrs(s *routing.SignedRoutingState, ttl time.Duration) error { - return nil -} -func (m mockPeerstore) SetAddr(p peer.ID, addr ma.Multiaddr, ttl time.Duration) {} -func (m mockPeerstore) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration) {} -func (m mockPeerstore) UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL time.Duration) {} -func (m mockPeerstore) Addrs(p peer.ID) []ma.Multiaddr { return nil } -func (m mockPeerstore) CertifiedAddrs(p peer.ID) []ma.Multiaddr { return nil } -func (m mockPeerstore) AddrStream(context.Context, peer.ID) <-chan ma.Multiaddr { return nil } -func (m mockPeerstore) ClearAddrs(p peer.ID) {} -func (m mockPeerstore) PeersWithAddrs() peer.IDSlice { return nil } -func (m mockPeerstore) SignedRoutingState(p peer.ID) *routing.SignedRoutingState { return nil } -func (m mockPeerstore) Get(p peer.ID, key string) (interface{}, error) { return nil, nil } -func (m mockPeerstore) Put(p peer.ID, key string, val interface{}) error { return nil } -func (m mockPeerstore) RecordLatency(peer.ID, time.Duration) {} -func (m mockPeerstore) LatencyEWMA(peer.ID) time.Duration { return 0 } -func (m mockPeerstore) GetProtocols(peer.ID) ([]string, error) { return nil, nil } -func (m mockPeerstore) AddProtocols(peer.ID, ...string) error { return nil } -func (m mockPeerstore) SetProtocols(peer.ID, ...string) error { return nil } -func (m mockPeerstore) RemoveProtocols(peer.ID, ...string) error { return nil } -func (m mockPeerstore) SupportsProtocols(peer.ID, ...string) ([]string, error) { return nil, nil } -func (m mockPeerstore) PeerInfo(peer.ID) peer.AddrInfo { return peer.AddrInfo{} } -func (m mockPeerstore) Peers() peer.IDSlice { return nil } -func (m mockPeerstore) PubKey(peer.ID) crypto.PubKey { return nil } -func (m mockPeerstore) AddPubKey(peer.ID, crypto.PubKey) error { return nil } -func (m mockPeerstore) AddPrivKey(peer.ID, crypto.PrivKey) error { return nil } -func (m mockPeerstore) PeersWithKeys() peer.IDSlice { return nil } - -func TestSignedRoutingStateFromHost_FailsIfPrivKeyIsNil(t *testing.T) { - _, err := SignedRoutingStateFromHost(&mockHost{}) - test.ExpectError(t, err, "expected generating signed routing state to fail when host private key is nil") -} - -func TestSignedRoutingStateFromHost_AddrFiltering(t *testing.T) { - localAddrs := parseAddrs(t, - // loopback - "/ip4/127.0.0.1/tcp/42", - "/ip6/::1/tcp/9999", - - // ip4 LAN reserved - "/ip4/10.0.0.1/tcp/1234", - "/ip4/100.64.0.123/udp/10101", - "/ip4/172.16.0.254/tcp/2345", - "/ip4/192.168.1.4/udp/1600", - - // link local - "/ip4/169.254.0.1/udp/1234", - "/ip6/fe80::c001:37ff:fe6c:0/tcp/42", - ) - - wanAddrs := parseAddrs(t, - "/ip4/1.2.3.4/tcp/42", - "/ip4/8.8.8.8/udp/1234", - "/ip6/2607:f8b0:4002:c02::8a/udp/1234", - "/ip6/2a03:2880:f111:83:face:b00c:0:25de/udp/2345/quic", - ) - - priv, _, err := test.RandTestKeyPair(crypto.Ed25519, 256) - if err != nil { - t.Fatal(err) - } - - host := &mockHost{ - fixedPrivKey: priv, - addrs: append(localAddrs, wanAddrs...), - } - - // test with local addrs - state, err := SignedRoutingStateFromHost(host, IncludeLocalAddrs) - if err != nil { - t.Fatalf("error generating routing state: %v", err) - } - test.AssertAddressesEqual(t, host.addrs, state.Addrs) - - // test filtering out local addrs - state, err = SignedRoutingStateFromHost(host) - if err != nil { - t.Fatalf("error generating routing state: %v", err) - } - test.AssertAddressesEqual(t, wanAddrs, state.Addrs) -} - -func parseAddrs(t *testing.T, addrStrings ...string) (out []ma.Multiaddr) { - t.Helper() - for _, s := range addrStrings { - out = append(out, ma.StringCast(s)) - } - return out -}