Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Feat/memory transport #3022

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ require (
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b
github.com/mr-tron/base58 v1.2.0
github.com/multiformats/go-base32 v0.1.0
github.com/multiformats/go-multiaddr v0.13.0
github.com/multiformats/go-multiaddr v0.14.0
github.com/multiformats/go-multiaddr-dns v0.4.0
github.com/multiformats/go-multiaddr-fmt v0.1.0
github.com/multiformats/go-multibase v0.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ github.com/multiformats/go-base32 v0.1.0/go.mod h1:Kj3tFY6zNr+ABYMqeUNeGvkIC/UYg
github.com/multiformats/go-base36 v0.2.0 h1:lFsAbNOGeKtuKozrtBsAkSVhv1p9D0/qedU9rQyccr0=
github.com/multiformats/go-base36 v0.2.0/go.mod h1:qvnKE++v+2MWCfePClUEjE78Z7P2a1UV0xHgWc0hkp4=
github.com/multiformats/go-multiaddr v0.1.1/go.mod h1:aMKBKNEYmzmDmxfX88/vz+J5IU55txyt0p4aiWVohjo=
github.com/multiformats/go-multiaddr v0.13.0 h1:BCBzs61E3AGHcYYTv8dqRH43ZfyrqM8RXVPT8t13tLQ=
github.com/multiformats/go-multiaddr v0.13.0/go.mod h1:sBXrNzucqkFJhvKOiwwLyqamGa/P5EIXNPLovyhQCII=
github.com/multiformats/go-multiaddr v0.14.0 h1:bfrHrJhrRuh/NXH5mCnemjpbGjzRw/b+tJFOD41g2tU=
github.com/multiformats/go-multiaddr v0.14.0/go.mod h1:6EkVAxtznq2yC3QT5CM1UTAwG0GTP3EWAIcjHuzQ+r4=
github.com/multiformats/go-multiaddr-dns v0.4.0 h1:P76EJ3qzBXpUXZ3twdCDx/kvagMsNo0LMFXpyms/zgU=
github.com/multiformats/go-multiaddr-dns v0.4.0/go.mod h1:7hfthtB4E4pQwirrz+J0CcDUfbWzTqEzVyYKKIKpgkc=
github.com/multiformats/go-multiaddr-fmt v0.1.0 h1:WLEFClPycPkp4fnIzoFoV9FVd49/eQsuaL3/CWe167E=
Expand Down
40 changes: 12 additions & 28 deletions p2p/test/transport/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
"github.com/libp2p/go-libp2p/p2p/security/noise"
tls "github.com/libp2p/go-libp2p/p2p/security/tls"
libp2pmemory "github.com/libp2p/go-libp2p/p2p/transport/memory"
libp2pwebrtc "github.com/libp2p/go-libp2p/p2p/transport/webrtc"
"go.uber.org/mock/gomock"

Expand Down Expand Up @@ -100,27 +101,9 @@ var transportsToTest = []TransportTestCase{
},
},
{
Name: "TCP-Shared / TLS / Yamux",
HostGenerator: func(t *testing.T, opts TransportTestCaseOpts) host.Host {
libp2pOpts := transformOpts(opts)
libp2pOpts = append(libp2pOpts, libp2p.ShareTCPListener())
libp2pOpts = append(libp2pOpts, libp2p.Security(tls.ID, tls.New))
libp2pOpts = append(libp2pOpts, libp2p.Muxer(yamux.ID, yamux.DefaultTransport))
if opts.NoListen {
libp2pOpts = append(libp2pOpts, libp2p.NoListenAddrs)
} else {
libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))
}
h, err := libp2p.New(libp2pOpts...)
require.NoError(t, err)
return h
},
},
{
Name: "WebSocket-Shared",
Name: "WebSocket",
HostGenerator: func(t *testing.T, opts TransportTestCaseOpts) host.Host {
libp2pOpts := transformOpts(opts)
libp2pOpts = append(libp2pOpts, libp2p.ShareTCPListener())
if opts.NoListen {
libp2pOpts = append(libp2pOpts, libp2p.NoListenAddrs)
} else {
Expand All @@ -132,56 +115,57 @@ var transportsToTest = []TransportTestCase{
},
},
{
Name: "WebSocket",
Name: "QUIC",
HostGenerator: func(t *testing.T, opts TransportTestCaseOpts) host.Host {
libp2pOpts := transformOpts(opts)
if opts.NoListen {
libp2pOpts = append(libp2pOpts, libp2p.NoListenAddrs)
} else {
libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0/ws"))
libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings("/ip4/127.0.0.1/udp/0/quic-v1"))
}
h, err := libp2p.New(libp2pOpts...)
require.NoError(t, err)
return h
},
},
{
Name: "QUIC",
Name: "WebTransport",
HostGenerator: func(t *testing.T, opts TransportTestCaseOpts) host.Host {
libp2pOpts := transformOpts(opts)
if opts.NoListen {
libp2pOpts = append(libp2pOpts, libp2p.NoListenAddrs)
} else {
libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings("/ip4/127.0.0.1/udp/0/quic-v1"))
libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings("/ip4/127.0.0.1/udp/0/quic-v1/webtransport"))
}
h, err := libp2p.New(libp2pOpts...)
require.NoError(t, err)
return h
},
},
{
Name: "WebTransport",
Name: "WebRTC",
HostGenerator: func(t *testing.T, opts TransportTestCaseOpts) host.Host {
libp2pOpts := transformOpts(opts)
libp2pOpts = append(libp2pOpts, libp2p.Transport(libp2pwebrtc.New))
if opts.NoListen {
libp2pOpts = append(libp2pOpts, libp2p.NoListenAddrs)
} else {
libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings("/ip4/127.0.0.1/udp/0/quic-v1/webtransport"))
libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings("/ip4/127.0.0.1/udp/0/webrtc-direct"))
}
h, err := libp2p.New(libp2pOpts...)
require.NoError(t, err)
return h
},
},
{
Name: "WebRTC",
Name: "Memory",
HostGenerator: func(t *testing.T, opts TransportTestCaseOpts) host.Host {
libp2pOpts := transformOpts(opts)
libp2pOpts = append(libp2pOpts, libp2p.Transport(libp2pwebrtc.New))
libp2pOpts = append(libp2pOpts, libp2p.Transport(libp2pmemory.NewTransport))
if opts.NoListen {
libp2pOpts = append(libp2pOpts, libp2p.NoListenAddrs)
} else {
libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings("/ip4/127.0.0.1/udp/0/webrtc-direct"))
libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings("/memory/1234"))
}
h, err := libp2p.New(libp2pOpts...)
require.NoError(t, err)
Expand Down
146 changes: 146 additions & 0 deletions p2p/transport/memory/conn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package memory

import (
"context"
"log"
"sync"
"sync/atomic"

ic "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
tpt "github.com/libp2p/go-libp2p/core/transport"
ma "github.com/multiformats/go-multiaddr"
)

type conn struct {
id int64
rconn *conn

scope network.ConnManagementScope
listener *listener
transport *transport

localPeer peer.ID
localMultiaddr ma.Multiaddr

remotePeerID peer.ID
remotePubKey ic.PubKey
remoteMultiaddr ma.Multiaddr

mu sync.Mutex

closed atomic.Bool
closeOnce sync.Once

streamC chan *stream
streams map[int64]network.MuxedStream
}

var _ tpt.CapableConn = &conn{}

func newConnection(
t *transport,
s *stream,
localPeer peer.ID,
localMultiaddr ma.Multiaddr,
remotePubKey ic.PubKey,
remotePeer peer.ID,
remoteMultiaddr ma.Multiaddr,
) *conn {
c := &conn{
id: connCounter.Add(1),
transport: t,
localPeer: localPeer,
localMultiaddr: localMultiaddr,
remotePubKey: remotePubKey,
remotePeerID: remotePeer,
remoteMultiaddr: remoteMultiaddr,
streamC: make(chan *stream, 1),
streams: make(map[int64]network.MuxedStream),
}

c.addStream(s.id, s)
return c
}

func (c *conn) Close() error {
c.closeOnce.Do(func() {
c.closed.Store(true)
go c.rconn.Close()
c.teardown()
})

return nil
}

func (c *conn) IsClosed() bool {
return c.closed.Load()
}

func (c *conn) OpenStream(ctx context.Context) (network.MuxedStream, error) {
sl, sr := newStreamPair()
sl.conn = c
c.addStream(sl.id, sl)
log.Println("opening stream", sl.id, sr.id)

c.rconn.streamC <- sr
return sl, nil
}

func (c *conn) AcceptStream() (network.MuxedStream, error) {
in := <-c.streamC
in.conn = c
c.addStream(in.id, in)
return in, nil
}

func (c *conn) LocalPeer() peer.ID { return c.localPeer }

// RemotePeer returns the peer ID of the remote peer.
func (c *conn) RemotePeer() peer.ID { return c.remotePeerID }

// RemotePublicKey returns the public pkey of the remote peer.
func (c *conn) RemotePublicKey() ic.PubKey { return c.remotePubKey }

// LocalMultiaddr returns the local Multiaddr associated
func (c *conn) LocalMultiaddr() ma.Multiaddr { return c.localMultiaddr }

// RemoteMultiaddr returns the remote Multiaddr associated
func (c *conn) RemoteMultiaddr() ma.Multiaddr { return c.remoteMultiaddr }

func (c *conn) Transport() tpt.Transport {
return c.transport
}

func (c *conn) Scope() network.ConnScope {
return c.scope
}

// ConnState is the state of security connection.
func (c *conn) ConnState() network.ConnectionState {
return network.ConnectionState{Transport: "memory"}
}

func (c *conn) addStream(id int64, stream network.MuxedStream) {
c.mu.Lock()
defer c.mu.Unlock()

c.streams[id] = stream
}

func (c *conn) removeStream(id int64) {
c.mu.Lock()
defer c.mu.Unlock()

delete(c.streams, id)
}

func (c *conn) teardown() {
for id, s := range c.streams {
log.Println("tearing down stream", id)
s.Reset()
}

// TODO: remove self from listener
}
1 change: 1 addition & 0 deletions p2p/transport/memory/conn_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package memory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write these before transport integration tests.

80 changes: 80 additions & 0 deletions p2p/transport/memory/hub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package memory

import (
ic "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
ma "github.com/multiformats/go-multiaddr"
mafmt "github.com/multiformats/go-multiaddr-fmt"
"sync"
"sync/atomic"
)

var (
connCounter atomic.Int64
streamCounter atomic.Int64
listenerCounter atomic.Int64
dialMatcher = mafmt.Base(ma.P_MEMORY)
memhub = newHub()
)

type hub struct {
mu sync.RWMutex
closeOnce sync.Once
pubKeys map[peer.ID]ic.PubKey
listeners map[string]*listener
}

func newHub() *hub {
return &hub{
pubKeys: make(map[peer.ID]ic.PubKey),
listeners: make(map[string]*listener),
}
}

func (h *hub) addListener(addr string, l *listener) {
h.mu.Lock()
defer h.mu.Unlock()

h.listeners[addr] = l
}

func (h *hub) removeListener(addr string, l *listener) {
h.mu.Lock()
defer h.mu.Unlock()

delete(h.listeners, addr)
}

func (h *hub) getListener(addr string) (*listener, bool) {
h.mu.RLock()
defer h.mu.RUnlock()

l, ok := h.listeners[addr]
return l, ok
}

func (h *hub) addPubKey(p peer.ID, pk ic.PubKey) {
h.mu.Lock()
defer h.mu.Unlock()

h.pubKeys[p] = pk
}

func (h *hub) getPubKey(p peer.ID) (ic.PubKey, bool) {
h.mu.RLock()
defer h.mu.RUnlock()

pk, ok := h.pubKeys[p]
return pk, ok
}

func (h *hub) close() {
h.closeOnce.Do(func() {
h.mu.Lock()
defer h.mu.Unlock()

for _, l := range h.listeners {
l.Close()
}
})
}
Loading