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

feat: expose handshake protocol version #559

Merged
merged 1 commit into from
Mar 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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