diff --git a/Makefile b/Makefile index aa202807..acd4c7e9 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ format: golines: golines -w --ignore-generated --chain-split-dots --max-len=80 --reformat-tags . -test: +test: mod-tidy go test -v -race ./... # Build our program binaries diff --git a/connection.go b/connection.go index 5e391a3f..c6a4dbf2 100644 --- a/connection.go +++ b/connection.go @@ -51,6 +51,7 @@ const ( // The Connection type is a wrapper around a net.Conn object that handles communication using the Ouroboros network protocol over that connection type Connection struct { + id ConnectionId conn net.Conn networkMagic uint32 server bool @@ -89,6 +90,19 @@ type Connection struct { txSubmissionConfig *txsubmission.Config } +type ConnectionId struct { + LocalAddr net.Addr + RemoteAddr net.Addr +} + +func (c ConnectionId) String() string { + return fmt.Sprintf( + "%s<->%s", + c.LocalAddr.String(), + c.RemoteAddr.String(), + ) +} + // NewConnection returns a new Connection object with the specified options. If a connection is provided, the // handshake will be started. An error will be returned if the handshake fails func NewConnection(options ...ConnectionOptionFunc) (*Connection, error) { @@ -117,6 +131,11 @@ func New(options ...ConnectionOptionFunc) (*Connection, error) { return NewConnection(options...) } +// Id returns the connection ID +func (c *Connection) Id() ConnectionId { + return c.id +} + // Muxer returns the muxer object for the Ouroboros connection func (c *Connection) Muxer() *muxer.Muxer { return c.muxer @@ -239,6 +258,11 @@ func (c *Connection) setupConnection() error { <-c.doneChan c.shutdown() }() + // Populate connection ID + c.id = ConnectionId{ + LocalAddr: c.conn.LocalAddr(), + RemoteAddr: c.conn.RemoteAddr(), + } // Create muxer instance c.muxer = muxer.New(c.conn) // Start Goroutine to pass along errors from the muxer