Skip to content

Commit

Permalink
feat: connection ID
Browse files Browse the repository at this point in the history
This adds a connection ID composed of the local and remote address of
the connection.

Fixes #571
  • Loading branch information
agaffney committed Apr 5, 2024
1 parent aca581f commit 72374e4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
toolchain go1.21.5

require (
github.com/blinklabs-io/ouroboros-mock v0.2.0
github.com/blinklabs-io/ouroboros-mock v0.3.0
github.com/fxamacker/cbor/v2 v2.6.0
github.com/jinzhu/copier v0.4.0
github.com/utxorpc/go-codegen v0.4.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/blinklabs-io/ouroboros-mock v0.2.0 h1:Wff7mJiFUzktQ5tuWRN9vXNk38wR0ij2Q4bYHwJXaV4=
github.com/blinklabs-io/ouroboros-mock v0.2.0/go.mod h1:t9eIDjmj339GJtfV7jandJnCqmj8WkZsFg2N1TR68io=
github.com/blinklabs-io/ouroboros-mock v0.3.0 h1:6VRWyhAv0k7nQEgzFpuqhS/n8OM+OAaLN/sCT5K2Hbc=
github.com/blinklabs-io/ouroboros-mock v0.3.0/go.mod h1:0dzTNEk/Kvqa7qYHDy7/Nn3OTt+EOosMknB37FRzI1k=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fxamacker/cbor/v2 v2.6.0 h1:sU6J2usfADwWlYDAFhZBQ6TnLFBHxgesMrQfQgk1tWA=
Expand Down

0 comments on commit 72374e4

Please sign in to comment.