@@ -2,19 +2,19 @@ package ouroboros
22
33import (
44 "github.com/cloudstruct/go-ouroboros-network/muxer"
5+ "github.com/cloudstruct/go-ouroboros-network/protocol"
56 "github.com/cloudstruct/go-ouroboros-network/protocol/blockfetch"
67 "github.com/cloudstruct/go-ouroboros-network/protocol/chainsync"
78 "github.com/cloudstruct/go-ouroboros-network/protocol/handshake"
89 "github.com/cloudstruct/go-ouroboros-network/protocol/keepalive"
910 "github.com/cloudstruct/go-ouroboros-network/protocol/localtxsubmission"
10- "io"
1111 "net"
1212)
1313
1414type Ouroboros struct {
15- conn io. ReadWriteCloser
15+ conn net. Conn
1616 networkMagic uint32
17- waitForHandshake bool
17+ server bool
1818 useNodeToNodeProto bool
1919 handshakeComplete bool
2020 muxer * muxer.Muxer
@@ -33,12 +33,10 @@ type Ouroboros struct {
3333}
3434
3535type OuroborosOptions struct {
36- Conn io.ReadWriteCloser
37- NetworkMagic uint32
38- ErrorChan chan error
39- // Whether to wait for the other side to initiate the handshake. This is useful
40- // for servers
41- WaitForHandshake bool
36+ Conn net.Conn
37+ NetworkMagic uint32
38+ ErrorChan chan error
39+ Server bool
4240 UseNodeToNodeProtocol bool
4341 SendKeepAlives bool
4442 ChainSyncCallbackConfig * chainsync.ChainSyncCallbackConfig
@@ -51,7 +49,7 @@ func New(options *OuroborosOptions) (*Ouroboros, error) {
5149 o := & Ouroboros {
5250 conn : options .Conn ,
5351 networkMagic : options .NetworkMagic ,
54- waitForHandshake : options .WaitForHandshake ,
52+ server : options .Server ,
5553 useNodeToNodeProto : options .UseNodeToNodeProtocol ,
5654 chainSyncCallbackConfig : options .ChainSyncCallbackConfig ,
5755 blockFetchCallbackConfig : options .BlockFetchCallbackConfig ,
@@ -92,37 +90,49 @@ func (o *Ouroboros) setupConnection() error {
9290 err := <- o .muxer .ErrorChan
9391 o .ErrorChan <- err
9492 }()
95- // Perform handshake
96- o .Handshake = handshake .New (o .muxer , o .ErrorChan , o .useNodeToNodeProto )
93+ protoOptions := protocol.ProtocolOptions {
94+ Muxer : o .muxer ,
95+ ErrorChan : o .ErrorChan ,
96+ }
9797 var protoVersions []uint16
9898 if o .useNodeToNodeProto {
9999 protoVersions = GetProtocolVersionsNtN ()
100+ protoOptions .Mode = protocol .ProtocolModeNodeToNode
100101 } else {
101102 protoVersions = GetProtocolVersionsNtC ()
103+ protoOptions .Mode = protocol .ProtocolModeNodeToClient
102104 }
105+ if o .server {
106+ protoOptions .Role = protocol .ProtocolRoleServer
107+ } else {
108+ protoOptions .Role = protocol .ProtocolRoleClient
109+ }
110+ // Perform handshake
111+ o .Handshake = handshake .New (protoOptions , protoVersions )
103112 // TODO: figure out better way to signify automatic handshaking and returning the chosen version
104- if ! o .waitForHandshake {
113+ if ! o .server {
105114 err := o .Handshake .ProposeVersions (protoVersions , o .networkMagic )
106115 if err != nil {
107116 return err
108117 }
109118 }
110119 o .handshakeComplete = <- o .Handshake .Finished
111- // TODO: register additional mini-protocols
112120 if o .useNodeToNodeProto {
113121 versionNtN := GetProtocolVersionNtN (o .Handshake .Version )
114- o .ChainSync = chainsync .New (o .muxer , o .ErrorChan , o .useNodeToNodeProto , o .chainSyncCallbackConfig )
115- o .BlockFetch = blockfetch .New (o .muxer , o .ErrorChan , o .blockFetchCallbackConfig )
122+ protoOptions .Mode = protocol .ProtocolModeNodeToNode
123+ o .ChainSync = chainsync .New (protoOptions , o .chainSyncCallbackConfig )
124+ o .BlockFetch = blockfetch .New (protoOptions , o .blockFetchCallbackConfig )
116125 if versionNtN .EnableKeepAliveProtocol {
117- o .KeepAlive = keepalive .New (o . muxer , o . ErrorChan , o .keepAliveCallbackConfig )
126+ o .KeepAlive = keepalive .New (protoOptions , o .keepAliveCallbackConfig )
118127 if o .sendKeepAlives {
119128 o .KeepAlive .Start ()
120129 }
121130 }
122131 } else {
123132 //versionNtC := GetProtocolVersionNtC(o.Handshake.Version)
124- o .ChainSync = chainsync .New (o .muxer , o .ErrorChan , o .useNodeToNodeProto , o .chainSyncCallbackConfig )
125- o .LocalTxSubmission = localtxsubmission .New (o .muxer , o .ErrorChan , o .localTxSubmissionCallbackConfig )
133+ protoOptions .Mode = protocol .ProtocolModeNodeToClient
134+ o .ChainSync = chainsync .New (protoOptions , o .chainSyncCallbackConfig )
135+ o .LocalTxSubmission = localtxsubmission .New (protoOptions , o .localTxSubmissionCallbackConfig )
126136 }
127137 return nil
128138}
0 commit comments