Skip to content

Commit 4e908ae

Browse files
authored
feat: expose handshake protocol version (#559)
Fixes #558
1 parent 0745027 commit 4e908ae

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

connection.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ type Connection struct {
5959
errorChan chan error
6060
protoErrorChan chan error
6161
handshakeFinishedChan chan interface{}
62+
handshakeVersion uint16
63+
handshakeVersionData protocol.VersionData
6264
doneChan chan interface{}
6365
waitGroup sync.WaitGroup
6466
onceClose sync.Once
@@ -205,6 +207,11 @@ func (c *Connection) TxSubmission() *txsubmission.TxSubmission {
205207
return c.txSubmission
206208
}
207209

210+
// ProtocolVersion returns the negotiated protocol version and the version data from the remote peer
211+
func (c *Connection) ProtocolVersion() (uint16, protocol.VersionData) {
212+
return c.handshakeVersion, c.handshakeVersionData
213+
}
214+
208215
// shutdown performs cleanup operations when the connection is shutdown, either due to explicit Close() or an error
209216
func (c *Connection) shutdown() {
210217
// Gracefully stop the muxer
@@ -285,12 +292,12 @@ func (c *Connection) setupConnection() error {
285292
protocol.QueryModeDisabled,
286293
)
287294
// Perform handshake
288-
var handshakeVersion uint16
289295
var handshakeFullDuplex bool
290296
handshakeConfig := handshake.NewConfig(
291297
handshake.WithProtocolVersionMap(protoVersions),
292298
handshake.WithFinishedFunc(func(version uint16, versionData protocol.VersionData) error {
293-
handshakeVersion = version
299+
c.handshakeVersion = version
300+
c.handshakeVersionData = versionData
294301
if c.useNodeToNodeProto {
295302
if versionData.DiffusionMode() == protocol.DiffusionModeInitiatorAndResponder {
296303
handshakeFullDuplex = true
@@ -319,7 +326,7 @@ func (c *Connection) setupConnection() error {
319326
// This is purposely empty, but we need this case to break out when this channel is closed
320327
}
321328
// Provide the negotiated protocol version to the various mini-protocols
322-
protoOptions.Version = handshakeVersion
329+
protoOptions.Version = c.handshakeVersion
323330
// Start Goroutine to pass along errors from the mini-protocols
324331
c.waitGroup.Add(1)
325332
go func() {
@@ -340,7 +347,7 @@ func (c *Connection) setupConnection() error {
340347
}()
341348
// Configure the relevant mini-protocols
342349
if c.useNodeToNodeProto {
343-
versionNtN := protocol.GetProtocolVersion(handshakeVersion)
350+
versionNtN := protocol.GetProtocolVersion(c.handshakeVersion)
344351
protoOptions.Mode = protocol.ProtocolModeNodeToNode
345352
c.chainSync = chainsync.New(protoOptions, c.chainSyncConfig)
346353
c.blockFetch = blockfetch.New(protoOptions, c.blockFetchConfig)
@@ -377,7 +384,7 @@ func (c *Connection) setupConnection() error {
377384
}
378385
}
379386
} else {
380-
versionNtC := protocol.GetProtocolVersion(handshakeVersion)
387+
versionNtC := protocol.GetProtocolVersion(c.handshakeVersion)
381388
protoOptions.Mode = protocol.ProtocolModeNodeToClient
382389
c.chainSync = chainsync.New(protoOptions, c.chainSyncConfig)
383390
c.localTxSubmission = localtxsubmission.New(protoOptions, c.localTxSubmissionConfig)

0 commit comments

Comments
 (0)