@@ -2,14 +2,17 @@ package handshake
22
33import (
44 "fmt"
5+
56 "github.com/cloudstruct/go-ouroboros-network/protocol"
67)
78
9+ // Client implements the Handshake client
810type Client struct {
911 * protocol.Protocol
1012 config * Config
1113}
1214
15+ // NewClient returns a new Handshake client object
1316func NewClient (protoOptions protocol.ProtocolOptions , cfg * Config ) * Client {
1417 if cfg == nil {
1518 tmpCfg := NewConfig ()
@@ -20,34 +23,35 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
2023 }
2124 // Update state map with timeout
2225 stateMap := StateMap .Copy ()
23- if entry , ok := stateMap [STATE_CONFIRM ]; ok {
26+ if entry , ok := stateMap [stateConfirm ]; ok {
2427 entry .Timeout = c .config .Timeout
25- stateMap [STATE_CONFIRM ] = entry
28+ stateMap [stateConfirm ] = entry
2629 }
2730 // Configure underlying Protocol
2831 protoConfig := protocol.ProtocolConfig {
29- Name : PROTOCOL_NAME ,
30- ProtocolId : PROTOCOL_ID ,
32+ Name : protocolName ,
33+ ProtocolId : protocolId ,
3134 Muxer : protoOptions .Muxer ,
3235 ErrorChan : protoOptions .ErrorChan ,
3336 Mode : protoOptions .Mode ,
3437 Role : protocol .ProtocolRoleClient ,
3538 MessageHandlerFunc : c .handleMessage ,
3639 MessageFromCborFunc : NewMsgFromCbor ,
3740 StateMap : stateMap ,
38- InitialState : STATE_PROPOSE ,
41+ InitialState : statePropose ,
3942 }
4043 c .Protocol = protocol .New (protoConfig )
4144 return c
4245}
4346
47+ // Start begins the handshake process
4448func (c * Client ) Start () {
4549 c .Protocol .Start ()
4650 // Send our ProposeVersions message
4751 versionMap := make (map [uint16 ]interface {})
48- diffusionMode := DIFFUSION_MODE_INITIATOR_ONLY
52+ diffusionMode := DiffusionModeInitiatorOnly
4953 if c .config .ClientFullDuplex {
50- diffusionMode = DIFFUSION_MODE_INITIATOR_AND_RESPONDER
54+ diffusionMode = DiffusionModeInitiatorAndResponder
5155 }
5256 for _ , version := range c .config .ProtocolVersions {
5357 if c .Mode () == protocol .ProtocolModeNodeToNode {
@@ -63,12 +67,12 @@ func (c *Client) Start() {
6367func (c * Client ) handleMessage (msg protocol.Message , isResponse bool ) error {
6468 var err error
6569 switch msg .Type () {
66- case MESSAGE_TYPE_ACCEPT_VERSION :
70+ case MessageTypeAcceptVersion :
6771 err = c .handleAcceptVersion (msg )
68- case MESSAGE_TYPE_REFUSE :
72+ case MessageTypeRefuse :
6973 err = c .handleRefuse (msg )
7074 default :
71- err = fmt .Errorf ("%s: received unexpected message type %d" , PROTOCOL_NAME , msg .Type ())
75+ err = fmt .Errorf ("%s: received unexpected message type %d" , protocolName , msg .Type ())
7276 }
7377 return err
7478}
@@ -82,7 +86,7 @@ func (c *Client) handleAcceptVersion(msgGeneric protocol.Message) error {
8286 if c .Mode () == protocol .ProtocolModeNodeToNode {
8387 versionData := msg .VersionData .([]interface {})
8488 //nolint:gosimple
85- if versionData [1 ].(bool ) == DIFFUSION_MODE_INITIATOR_AND_RESPONDER {
89+ if versionData [1 ].(bool ) == DiffusionModeInitiatorAndResponder {
8690 fullDuplex = true
8791 }
8892 }
@@ -93,12 +97,12 @@ func (c *Client) handleRefuse(msgGeneric protocol.Message) error {
9397 msg := msgGeneric .(* MsgRefuse )
9498 var err error
9599 switch msg .Reason [0 ].(uint64 ) {
96- case REFUSE_REASON_VERSION_MISMATCH :
97- err = fmt .Errorf ("%s: version mismatch" , PROTOCOL_NAME )
98- case REFUSE_REASON_DECODE_ERROR :
99- err = fmt .Errorf ("%s: decode error: %s" , PROTOCOL_NAME , msg .Reason [2 ].(string ))
100- case REFUSE_REASON_REFUSED :
101- err = fmt .Errorf ("%s: refused: %s" , PROTOCOL_NAME , msg .Reason [2 ].(string ))
100+ case RefuseReasonVersionMismatch :
101+ err = fmt .Errorf ("%s: version mismatch" , protocolName )
102+ case RefuseReasonDecodeError :
103+ err = fmt .Errorf ("%s: decode error: %s" , protocolName , msg .Reason [2 ].(string ))
104+ case RefuseReasonRefused :
105+ err = fmt .Errorf ("%s: refused: %s" , protocolName , msg .Reason [2 ].(string ))
102106 }
103107 return err
104108}
0 commit comments