@@ -42,8 +42,8 @@ import (
4242 "github.com/blinklabs-io/gouroboros/protocol/txsubmission"
4343)
4444
45- // The Ouroboros type is a wrapper around a net.Conn object that handles communication using the Ouroboros network protocol over that connection
46- type Ouroboros struct {
45+ // The Connection type is a wrapper around a net.Conn object that handles communication using the Ouroboros network protocol over that connection
46+ type Connection struct {
4747 conn net.Conn
4848 networkMagic uint32
4949 server bool
@@ -76,206 +76,211 @@ type Ouroboros struct {
7676 txSubmissionConfig * txsubmission.Config
7777}
7878
79- // New returns a new Ouroboros object with the specified options. If a connection is provided, the
79+ // NewConnection returns a new Connection object with the specified options. If a connection is provided, the
8080// handshake will be started. An error will be returned if the handshake fails
81- func New (options ... OuroborosOptionFunc ) (* Ouroboros , error ) {
82- o := & Ouroboros {
81+ func NewConnection (options ... ConnectionOptionFunc ) (* Connection , error ) {
82+ c := & Connection {
8383 protoErrorChan : make (chan error , 10 ),
8484 handshakeFinishedChan : make (chan interface {}),
8585 doneChan : make (chan interface {}),
8686 }
8787 // Apply provided options functions
8888 for _ , option := range options {
89- option (o )
89+ option (c )
9090 }
91- if o .errorChan == nil {
92- o .errorChan = make (chan error , 10 )
91+ if c .errorChan == nil {
92+ c .errorChan = make (chan error , 10 )
9393 }
94- if o .conn != nil {
95- if err := o .setupConnection (); err != nil {
94+ if c .conn != nil {
95+ if err := c .setupConnection (); err != nil {
9696 return nil , err
9797 }
9898 }
99- return o , nil
99+ return c , nil
100+ }
101+
102+ // New is an alias to NewConnection for backward compatibility
103+ func New (options ... ConnectionOptionFunc ) (* Connection , error ) {
104+ return NewConnection (options ... )
100105}
101106
102107// Muxer returns the muxer object for the Ouroboros connection
103- func (o * Ouroboros ) Muxer () * muxer.Muxer {
104- return o .muxer
108+ func (c * Connection ) Muxer () * muxer.Muxer {
109+ return c .muxer
105110}
106111
107112// ErrorChan returns the channel for asynchronous errors
108- func (o * Ouroboros ) ErrorChan () chan error {
109- return o .errorChan
113+ func (c * Connection ) ErrorChan () chan error {
114+ return c .errorChan
110115}
111116
112117// Dial will establish a connection using the specified protocol and address. These parameters are
113118// passed to the [net.Dial] func. The handshake will be started when a connection is established.
114119// An error will be returned if the connection fails, a connection was already established, or the
115120// handshake fails
116- func (o * Ouroboros ) Dial (proto string , address string ) error {
117- if o .conn != nil {
121+ func (c * Connection ) Dial (proto string , address string ) error {
122+ if c .conn != nil {
118123 return fmt .Errorf ("a connection was already established" )
119124 }
120125 conn , err := net .Dial (proto , address )
121126 if err != nil {
122127 return err
123128 }
124- o .conn = conn
125- if err := o .setupConnection (); err != nil {
129+ c .conn = conn
130+ if err := c .setupConnection (); err != nil {
126131 return err
127132 }
128133 return nil
129134}
130135
131136// Close will shutdown the Ouroboros connection
132- func (o * Ouroboros ) Close () error {
137+ func (c * Connection ) Close () error {
133138 var err error
134- o .onceClose .Do (func () {
139+ c .onceClose .Do (func () {
135140 // Close doneChan to signify that we're shutting down
136- close (o .doneChan )
141+ close (c .doneChan )
137142 // Gracefully stop the muxer
138- if o .muxer != nil {
139- o .muxer .Stop ()
143+ if c .muxer != nil {
144+ c .muxer .Stop ()
140145 }
141146 // Wait for other goroutines to finish
142- o .waitGroup .Wait ()
147+ c .waitGroup .Wait ()
143148 // Close channels
144- close (o .errorChan )
145- close (o .protoErrorChan )
149+ close (c .errorChan )
150+ close (c .protoErrorChan )
146151 // We can only close a channel once, so we have to jump through a few hoops
147152 select {
148153 // The channel is either closed or has an item pending
149- case _ , ok := <- o .handshakeFinishedChan :
154+ case _ , ok := <- c .handshakeFinishedChan :
150155 // We successfully retrieved an item
151156 // This will probably never happen, but it doesn't hurt to cover this case
152157 if ok {
153- close (o .handshakeFinishedChan )
158+ close (c .handshakeFinishedChan )
154159 }
155160 // The channel is open and has no pending items
156161 default :
157- close (o .handshakeFinishedChan )
162+ close (c .handshakeFinishedChan )
158163 }
159164 })
160165 return err
161166}
162167
163168// ChainSync returns the chain-sync protocol handler
164- func (o * Ouroboros ) ChainSync () * chainsync.ChainSync {
165- return o .chainSync
169+ func (c * Connection ) ChainSync () * chainsync.ChainSync {
170+ return c .chainSync
166171}
167172
168173// BlockFetch returns the block-fetch protocol handler
169- func (o * Ouroboros ) BlockFetch () * blockfetch.BlockFetch {
170- return o .blockFetch
174+ func (c * Connection ) BlockFetch () * blockfetch.BlockFetch {
175+ return c .blockFetch
171176}
172177
173178// Handshake returns the handshake protocol handler
174- func (o * Ouroboros ) Handshake () * handshake.Handshake {
175- return o .handshake
179+ func (c * Connection ) Handshake () * handshake.Handshake {
180+ return c .handshake
176181}
177182
178183// KeepAlive returns the keep-alive protocol handler
179- func (o * Ouroboros ) KeepAlive () * keepalive.KeepAlive {
180- return o .keepAlive
184+ func (c * Connection ) KeepAlive () * keepalive.KeepAlive {
185+ return c .keepAlive
181186}
182187
183188// LocalTxMonitor returns the local-tx-monitor protocol handler
184- func (o * Ouroboros ) LocalTxMonitor () * localtxmonitor.LocalTxMonitor {
185- return o .localTxMonitor
189+ func (c * Connection ) LocalTxMonitor () * localtxmonitor.LocalTxMonitor {
190+ return c .localTxMonitor
186191}
187192
188193// LocalTxSubmission returns the local-tx-submission protocol handler
189- func (o * Ouroboros ) LocalTxSubmission () * localtxsubmission.LocalTxSubmission {
190- return o .localTxSubmission
194+ func (c * Connection ) LocalTxSubmission () * localtxsubmission.LocalTxSubmission {
195+ return c .localTxSubmission
191196}
192197
193198// LocalStateQuery returns the local-state-query protocol handler
194- func (o * Ouroboros ) LocalStateQuery () * localstatequery.LocalStateQuery {
195- return o .localStateQuery
199+ func (c * Connection ) LocalStateQuery () * localstatequery.LocalStateQuery {
200+ return c .localStateQuery
196201}
197202
198203// TxSubmission returns the tx-submission protocol handler
199- func (o * Ouroboros ) TxSubmission () * txsubmission.TxSubmission {
200- return o .txSubmission
204+ func (c * Connection ) TxSubmission () * txsubmission.TxSubmission {
205+ return c .txSubmission
201206}
202207
203208// setupConnection establishes the muxer, configures and starts the handshake process, and initializes
204209// the appropriate mini-protocols
205- func (o * Ouroboros ) setupConnection () error {
206- o .muxer = muxer .New (o .conn )
210+ func (c * Connection ) setupConnection () error {
211+ c .muxer = muxer .New (c .conn )
207212 // Start Goroutine to pass along errors from the muxer
208- o .waitGroup .Add (1 )
213+ c .waitGroup .Add (1 )
209214 go func () {
210- defer o .waitGroup .Done ()
215+ defer c .waitGroup .Done ()
211216 select {
212- case <- o .doneChan :
217+ case <- c .doneChan :
213218 return
214- case err , ok := <- o .muxer .ErrorChan ():
219+ case err , ok := <- c .muxer .ErrorChan ():
215220 // Break out of goroutine if muxer's error channel is closed
216221 if ! ok {
217222 return
218223 }
219224 if errors .Is (err , io .EOF ) || errors .Is (err , io .ErrUnexpectedEOF ) {
220225 // Return a bare io.EOF error if error is EOF/ErrUnexpectedEOF
221- o .errorChan <- io .EOF
226+ c .errorChan <- io .EOF
222227 } else {
223228 // Wrap error message to denote it comes from the muxer
224- o .errorChan <- fmt .Errorf ("muxer error: %s" , err )
229+ c .errorChan <- fmt .Errorf ("muxer error: %s" , err )
225230 }
226231 // Close connection on muxer errors
227- o .Close ()
232+ c .Close ()
228233 }
229234 }()
230235 protoOptions := protocol.ProtocolOptions {
231- Muxer : o .muxer ,
232- ErrorChan : o .protoErrorChan ,
236+ Muxer : c .muxer ,
237+ ErrorChan : c .protoErrorChan ,
233238 }
234239 var protoVersions []uint16
235- if o .useNodeToNodeProto {
240+ if c .useNodeToNodeProto {
236241 protoVersions = GetProtocolVersionsNtN ()
237242 protoOptions .Mode = protocol .ProtocolModeNodeToNode
238243 } else {
239244 protoVersions = GetProtocolVersionsNtC ()
240245 protoOptions .Mode = protocol .ProtocolModeNodeToClient
241246 }
242- if o .server {
247+ if c .server {
243248 protoOptions .Role = protocol .ProtocolRoleServer
244249 } else {
245250 protoOptions .Role = protocol .ProtocolRoleClient
246251 }
247252 // Check network magic value
248- if o .networkMagic == 0 {
249- return fmt .Errorf ("invalid network magic value provided: %d\n " , o .networkMagic )
253+ if c .networkMagic == 0 {
254+ return fmt .Errorf ("invalid network magic value provided: %d\n " , c .networkMagic )
250255 }
251256 // Perform handshake
252257 var handshakeVersion uint16
253258 var handshakeFullDuplex bool
254259 handshakeConfig := handshake .NewConfig (
255260 handshake .WithProtocolVersions (protoVersions ),
256- handshake .WithNetworkMagic (o .networkMagic ),
257- handshake .WithClientFullDuplex (o .fullDuplex ),
261+ handshake .WithNetworkMagic (c .networkMagic ),
262+ handshake .WithClientFullDuplex (c .fullDuplex ),
258263 handshake .WithFinishedFunc (func (version uint16 , fullDuplex bool ) error {
259264 handshakeVersion = version
260265 handshakeFullDuplex = fullDuplex
261- close (o .handshakeFinishedChan )
266+ close (c .handshakeFinishedChan )
262267 return nil
263268 }),
264269 )
265- o .handshake = handshake .New (protoOptions , & handshakeConfig )
266- if o .server {
267- o .handshake .Server .Start ()
270+ c .handshake = handshake .New (protoOptions , & handshakeConfig )
271+ if c .server {
272+ c .handshake .Server .Start ()
268273 } else {
269- o .handshake .Client .Start ()
274+ c .handshake .Client .Start ()
270275 }
271276 // Wait for handshake completion or error
272277 select {
273- case <- o .doneChan :
278+ case <- c .doneChan :
274279 // Return an error if we're shutting down
275280 return io .EOF
276- case err := <- o .protoErrorChan :
281+ case err := <- c .protoErrorChan :
277282 return err
278- case <- o .handshakeFinishedChan :
283+ case <- c .handshakeFinishedChan :
279284 // This is purposely empty, but we need this case to break out when this channel is closed
280285 }
281286 // Provide the negotiated protocol version to the various mini-protocols
@@ -285,58 +290,58 @@ func (o *Ouroboros) setupConnection() error {
285290 protoOptions .Version = protoOptions .Version - protocolVersionNtCFlag
286291 }
287292 // Start Goroutine to pass along errors from the mini-protocols
288- o .waitGroup .Add (1 )
293+ c .waitGroup .Add (1 )
289294 go func () {
290- defer o .waitGroup .Done ()
295+ defer c .waitGroup .Done ()
291296 select {
292- case <- o .doneChan :
297+ case <- c .doneChan :
293298 // Return if we're shutting down
294299 return
295- case err , ok := <- o .protoErrorChan :
300+ case err , ok := <- c .protoErrorChan :
296301 // The channel is closed, which means we're already shutting down
297302 if ! ok {
298303 return
299304 }
300- o .errorChan <- fmt .Errorf ("protocol error: %s" , err )
305+ c .errorChan <- fmt .Errorf ("protocol error: %s" , err )
301306 // Close connection on mini-protocol errors
302- o .Close ()
307+ c .Close ()
303308 }
304309 }()
305310 // Configure the relevant mini-protocols
306- if o .useNodeToNodeProto {
311+ if c .useNodeToNodeProto {
307312 versionNtN := GetProtocolVersionNtN (handshakeVersion )
308313 protoOptions .Mode = protocol .ProtocolModeNodeToNode
309- o .chainSync = chainsync .New (protoOptions , o .chainSyncConfig )
310- o .blockFetch = blockfetch .New (protoOptions , o .blockFetchConfig )
311- o .txSubmission = txsubmission .New (protoOptions , o .txSubmissionConfig )
314+ c .chainSync = chainsync .New (protoOptions , c .chainSyncConfig )
315+ c .blockFetch = blockfetch .New (protoOptions , c .blockFetchConfig )
316+ c .txSubmission = txsubmission .New (protoOptions , c .txSubmissionConfig )
312317 if versionNtN .EnableKeepAliveProtocol {
313- o .keepAlive = keepalive .New (protoOptions , o .keepAliveConfig )
314- if ! o .server && o .sendKeepAlives {
315- o .keepAlive .Client .Start ()
318+ c .keepAlive = keepalive .New (protoOptions , c .keepAliveConfig )
319+ if ! c .server && c .sendKeepAlives {
320+ c .keepAlive .Client .Start ()
316321 }
317322 }
318323 } else {
319324 versionNtC := GetProtocolVersionNtC (handshakeVersion )
320325 protoOptions .Mode = protocol .ProtocolModeNodeToClient
321- o .chainSync = chainsync .New (protoOptions , o .chainSyncConfig )
322- o .localTxSubmission = localtxsubmission .New (protoOptions , o .localTxSubmissionConfig )
326+ c .chainSync = chainsync .New (protoOptions , c .chainSyncConfig )
327+ c .localTxSubmission = localtxsubmission .New (protoOptions , c .localTxSubmissionConfig )
323328 if versionNtC .EnableLocalQueryProtocol {
324- o .localStateQuery = localstatequery .New (protoOptions , o .localStateQueryConfig )
329+ c .localStateQuery = localstatequery .New (protoOptions , c .localStateQueryConfig )
325330 }
326331 if versionNtC .EnableLocalTxMonitorProtocol {
327- o .localTxMonitor = localtxmonitor .New (protoOptions , o .localTxMonitorConfig )
332+ c .localTxMonitor = localtxmonitor .New (protoOptions , c .localTxMonitorConfig )
328333 }
329334 }
330335 // Start muxer
331336 diffusionMode := muxer .DiffusionModeInitiator
332337 if handshakeFullDuplex {
333338 diffusionMode = muxer .DiffusionModeInitiatorAndResponder
334- } else if o .server {
339+ } else if c .server {
335340 diffusionMode = muxer .DiffusionModeResponder
336341 }
337- o .muxer .SetDiffusionMode (diffusionMode )
338- if ! o .delayMuxerStart {
339- o .muxer .Start ()
342+ c .muxer .SetDiffusionMode (diffusionMode )
343+ if ! c .delayMuxerStart {
344+ c .muxer .Start ()
340345 }
341346 return nil
342347}
0 commit comments