Skip to content

Commit

Permalink
feat: expose handshake protocol version
Browse files Browse the repository at this point in the history
Fixes #558
  • Loading branch information
agaffney committed Mar 22, 2024
1 parent 0745027 commit c29a8a7
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type Connection struct {
errorChan chan error
protoErrorChan chan error
handshakeFinishedChan chan interface{}
handshakeVersion uint16
handshakeVersionData protocol.VersionData
doneChan chan interface{}
waitGroup sync.WaitGroup
onceClose sync.Once
Expand Down Expand Up @@ -205,6 +207,11 @@ func (c *Connection) TxSubmission() *txsubmission.TxSubmission {
return c.txSubmission
}

// ProtocolVersion returns the negotiated protocol version and the version data from the remote peer
func (c *Connection) ProtocolVersion() (uint16, protocol.VersionData) {
return c.handshakeVersion, c.handshakeVersionData
}

// shutdown performs cleanup operations when the connection is shutdown, either due to explicit Close() or an error
func (c *Connection) shutdown() {
// Gracefully stop the muxer
Expand Down Expand Up @@ -285,12 +292,12 @@ func (c *Connection) setupConnection() error {
protocol.QueryModeDisabled,
)
// Perform handshake
var handshakeVersion uint16
var handshakeFullDuplex bool
handshakeConfig := handshake.NewConfig(
handshake.WithProtocolVersionMap(protoVersions),
handshake.WithFinishedFunc(func(version uint16, versionData protocol.VersionData) error {
handshakeVersion = version
c.handshakeVersion = version
c.handshakeVersionData = versionData
if c.useNodeToNodeProto {
if versionData.DiffusionMode() == protocol.DiffusionModeInitiatorAndResponder {
handshakeFullDuplex = true
Expand Down Expand Up @@ -319,7 +326,7 @@ func (c *Connection) setupConnection() error {
// This is purposely empty, but we need this case to break out when this channel is closed
}
// Provide the negotiated protocol version to the various mini-protocols
protoOptions.Version = handshakeVersion
protoOptions.Version = c.handshakeVersion
// Start Goroutine to pass along errors from the mini-protocols
c.waitGroup.Add(1)
go func() {
Expand All @@ -340,7 +347,7 @@ func (c *Connection) setupConnection() error {
}()
// Configure the relevant mini-protocols
if c.useNodeToNodeProto {
versionNtN := protocol.GetProtocolVersion(handshakeVersion)
versionNtN := protocol.GetProtocolVersion(c.handshakeVersion)
protoOptions.Mode = protocol.ProtocolModeNodeToNode
c.chainSync = chainsync.New(protoOptions, c.chainSyncConfig)
c.blockFetch = blockfetch.New(protoOptions, c.blockFetchConfig)
Expand Down Expand Up @@ -377,7 +384,7 @@ func (c *Connection) setupConnection() error {
}
}
} else {
versionNtC := protocol.GetProtocolVersion(handshakeVersion)
versionNtC := protocol.GetProtocolVersion(c.handshakeVersion)
protoOptions.Mode = protocol.ProtocolModeNodeToClient
c.chainSync = chainsync.New(protoOptions, c.chainSyncConfig)
c.localTxSubmission = localtxsubmission.New(protoOptions, c.localTxSubmissionConfig)
Expand Down

0 comments on commit c29a8a7

Please sign in to comment.