From 324a3fce37430879477b6021783555a11b741326 Mon Sep 17 00:00:00 2001 From: Aurora Gaffney Date: Wed, 11 Oct 2023 18:05:28 -0500 Subject: [PATCH] feat: consistent naming for protocol constants --- protocol/blockfetch/blockfetch.go | 44 ++++++++++++++-------------- protocol/blockfetch/client.go | 26 ++++++++-------- protocol/blockfetch/messages.go | 38 ++++++++++++------------ protocol/blockfetch/messages_test.go | 2 +- protocol/blockfetch/server.go | 8 ++--- protocol/keepalive/client.go | 14 ++++----- protocol/keepalive/keepalive.go | 36 +++++++++++------------ protocol/keepalive/messages.go | 20 ++++++------- protocol/keepalive/messages_test.go | 6 ++-- protocol/keepalive/server.go | 12 ++++---- 10 files changed, 103 insertions(+), 103 deletions(-) diff --git a/protocol/blockfetch/blockfetch.go b/protocol/blockfetch/blockfetch.go index 01233060..a71a7fc6 100644 --- a/protocol/blockfetch/blockfetch.go +++ b/protocol/blockfetch/blockfetch.go @@ -23,58 +23,58 @@ import ( ) const ( - PROTOCOL_NAME = "block-fetch" - PROTOCOL_ID uint16 = 3 + ProtocolName = "block-fetch" + ProtocolId uint16 = 3 ) var ( - STATE_IDLE = protocol.NewState(1, "Idle") - STATE_BUSY = protocol.NewState(2, "Busy") - STATE_STREAMING = protocol.NewState(3, "Streaming") - STATE_DONE = protocol.NewState(4, "Done") + StateIdle = protocol.NewState(1, "Idle") + StateBusy = protocol.NewState(2, "Busy") + StateStreaming = protocol.NewState(3, "Streaming") + StateDone = protocol.NewState(4, "Done") ) var StateMap = protocol.StateMap{ - STATE_IDLE: protocol.StateMapEntry{ + StateIdle: protocol.StateMapEntry{ Agency: protocol.AgencyClient, Transitions: []protocol.StateTransition{ { - MsgType: MESSAGE_TYPE_REQUEST_RANGE, - NewState: STATE_BUSY, + MsgType: MessageTypeRequestRange, + NewState: StateBusy, }, { - MsgType: MESSAGE_TYPE_CLIENT_DONE, - NewState: STATE_DONE, + MsgType: MessageTypeClientDone, + NewState: StateDone, }, }, }, - STATE_BUSY: protocol.StateMapEntry{ + StateBusy: protocol.StateMapEntry{ Agency: protocol.AgencyServer, Transitions: []protocol.StateTransition{ { - MsgType: MESSAGE_TYPE_START_BATCH, - NewState: STATE_STREAMING, + MsgType: MessageTypeStartBatch, + NewState: StateStreaming, }, { - MsgType: MESSAGE_TYPE_NO_BLOCKS, - NewState: STATE_IDLE, + MsgType: MessageTypeNoBlocks, + NewState: StateIdle, }, }, }, - STATE_STREAMING: protocol.StateMapEntry{ + StateStreaming: protocol.StateMapEntry{ Agency: protocol.AgencyServer, Transitions: []protocol.StateTransition{ { - MsgType: MESSAGE_TYPE_BLOCK, - NewState: STATE_STREAMING, + MsgType: MessageTypeBlock, + NewState: StateStreaming, }, { - MsgType: MESSAGE_TYPE_BATCH_DONE, - NewState: STATE_IDLE, + MsgType: MessageTypeBatchDone, + NewState: StateIdle, }, }, }, - STATE_DONE: protocol.StateMapEntry{ + StateDone: protocol.StateMapEntry{ Agency: protocol.AgencyNone, }, } diff --git a/protocol/blockfetch/client.go b/protocol/blockfetch/client.go index 4829ed30..a15c300e 100644 --- a/protocol/blockfetch/client.go +++ b/protocol/blockfetch/client.go @@ -47,18 +47,18 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client { } // Update state map with timeouts stateMap := StateMap.Copy() - if entry, ok := stateMap[STATE_BUSY]; ok { + if entry, ok := stateMap[StateBusy]; ok { entry.Timeout = c.config.BatchStartTimeout - stateMap[STATE_BUSY] = entry + stateMap[StateBusy] = entry } - if entry, ok := stateMap[STATE_STREAMING]; ok { + if entry, ok := stateMap[StateStreaming]; ok { entry.Timeout = c.config.BlockTimeout - stateMap[STATE_STREAMING] = entry + stateMap[StateStreaming] = entry } // Configure underlying Protocol protoConfig := protocol.ProtocolConfig{ - Name: PROTOCOL_NAME, - ProtocolId: PROTOCOL_ID, + Name: ProtocolName, + ProtocolId: ProtocolId, Muxer: protoOptions.Muxer, ErrorChan: protoOptions.ErrorChan, Mode: protoOptions.Mode, @@ -66,7 +66,7 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client { MessageHandlerFunc: c.messageHandler, MessageFromCborFunc: NewMsgFromCbor, StateMap: stateMap, - InitialState: STATE_IDLE, + InitialState: StateIdle, } c.Protocol = protocol.New(protoConfig) // Start goroutine to cleanup resources on protocol shutdown @@ -127,16 +127,16 @@ func (c *Client) GetBlock(point common.Point) (ledger.Block, error) { func (c *Client) messageHandler(msg protocol.Message, isResponse bool) error { var err error switch msg.Type() { - case MESSAGE_TYPE_START_BATCH: + case MessageTypeStartBatch: err = c.handleStartBatch() - case MESSAGE_TYPE_NO_BLOCKS: + case MessageTypeNoBlocks: err = c.handleNoBlocks() - case MESSAGE_TYPE_BLOCK: + case MessageTypeBlock: err = c.handleBlock(msg) - case MESSAGE_TYPE_BATCH_DONE: + case MessageTypeBatchDone: err = c.handleBatchDone() default: - err = fmt.Errorf("%s: received unexpected message type %d", PROTOCOL_NAME, msg.Type()) + err = fmt.Errorf("%s: received unexpected message type %d", ProtocolName, msg.Type()) } return err } @@ -157,7 +157,7 @@ func (c *Client) handleBlock(msgGeneric protocol.Message) error { // Decode only enough to get the block type value var wrappedBlock WrappedBlock if _, err := cbor.Decode(msg.WrappedBlock, &wrappedBlock); err != nil { - return fmt.Errorf("%s: decode error: %s", PROTOCOL_NAME, err) + return fmt.Errorf("%s: decode error: %s", ProtocolName, err) } blk, err := ledger.NewBlockFromCbor(wrappedBlock.Type, wrappedBlock.RawBlock) if err != nil { diff --git a/protocol/blockfetch/messages.go b/protocol/blockfetch/messages.go index 1888672f..7d372d17 100644 --- a/protocol/blockfetch/messages.go +++ b/protocol/blockfetch/messages.go @@ -21,32 +21,32 @@ import ( ) const ( - MESSAGE_TYPE_REQUEST_RANGE = 0 - MESSAGE_TYPE_CLIENT_DONE = 1 - MESSAGE_TYPE_START_BATCH = 2 - MESSAGE_TYPE_NO_BLOCKS = 3 - MESSAGE_TYPE_BLOCK = 4 - MESSAGE_TYPE_BATCH_DONE = 5 + MessageTypeRequestRange = 0 + MessageTypeClientDone = 1 + MessageTypeStartBatch = 2 + MessageTypeNoBlocks = 3 + MessageTypeBlock = 4 + MessageTypeBatchDone = 5 ) func NewMsgFromCbor(msgType uint, data []byte) (protocol.Message, error) { var ret protocol.Message switch msgType { - case MESSAGE_TYPE_REQUEST_RANGE: + case MessageTypeRequestRange: ret = &MsgRequestRange{} - case MESSAGE_TYPE_CLIENT_DONE: + case MessageTypeClientDone: ret = &MsgClientDone{} - case MESSAGE_TYPE_START_BATCH: + case MessageTypeStartBatch: ret = &MsgStartBatch{} - case MESSAGE_TYPE_NO_BLOCKS: + case MessageTypeNoBlocks: ret = &MsgNoBlocks{} - case MESSAGE_TYPE_BLOCK: + case MessageTypeBlock: ret = &MsgBlock{} - case MESSAGE_TYPE_BATCH_DONE: + case MessageTypeBatchDone: ret = &MsgBatchDone{} } if _, err := cbor.Decode(data, ret); err != nil { - return nil, fmt.Errorf("%s: decode error: %s", PROTOCOL_NAME, err) + return nil, fmt.Errorf("%s: decode error: %s", ProtocolName, err) } if ret != nil { // Store the raw message CBOR @@ -64,7 +64,7 @@ type MsgRequestRange struct { func NewMsgRequestRange(start interface{}, end interface{}) *MsgRequestRange { m := &MsgRequestRange{ MessageBase: protocol.MessageBase{ - MessageType: MESSAGE_TYPE_REQUEST_RANGE, + MessageType: MessageTypeRequestRange, }, Start: start, End: end, @@ -79,7 +79,7 @@ type MsgClientDone struct { func NewMsgClientDone() *MsgClientDone { m := &MsgClientDone{ MessageBase: protocol.MessageBase{ - MessageType: MESSAGE_TYPE_CLIENT_DONE, + MessageType: MessageTypeClientDone, }, } return m @@ -92,7 +92,7 @@ type MsgStartBatch struct { func NewMsgStartBatch() *MsgStartBatch { m := &MsgStartBatch{ MessageBase: protocol.MessageBase{ - MessageType: MESSAGE_TYPE_START_BATCH, + MessageType: MessageTypeStartBatch, }, } return m @@ -105,7 +105,7 @@ type MsgNoBlocks struct { func NewMsgNoBlocks() *MsgNoBlocks { m := &MsgNoBlocks{ MessageBase: protocol.MessageBase{ - MessageType: MESSAGE_TYPE_NO_BLOCKS, + MessageType: MessageTypeNoBlocks, }, } return m @@ -119,7 +119,7 @@ type MsgBlock struct { func NewMsgBlock(wrappedBlock []byte) *MsgBlock { m := &MsgBlock{ MessageBase: protocol.MessageBase{ - MessageType: MESSAGE_TYPE_BLOCK, + MessageType: MessageTypeBlock, }, WrappedBlock: wrappedBlock, } @@ -133,7 +133,7 @@ type MsgBatchDone struct { func NewMsgBatchDone() *MsgBatchDone { m := &MsgBatchDone{ MessageBase: protocol.MessageBase{ - MessageType: MESSAGE_TYPE_BATCH_DONE, + MessageType: MessageTypeBatchDone, }, } return m diff --git a/protocol/blockfetch/messages_test.go b/protocol/blockfetch/messages_test.go index d58aac8d..eadac74c 100644 --- a/protocol/blockfetch/messages_test.go +++ b/protocol/blockfetch/messages_test.go @@ -33,7 +33,7 @@ var tests = []testDefinition{ { CborHex: "8105", Message: NewMsgBatchDone(), - MessageType: MESSAGE_TYPE_BATCH_DONE, + MessageType: MessageTypeBatchDone, }, } diff --git a/protocol/blockfetch/server.go b/protocol/blockfetch/server.go index 6661772c..caf2b816 100644 --- a/protocol/blockfetch/server.go +++ b/protocol/blockfetch/server.go @@ -29,8 +29,8 @@ func NewServer(protoOptions protocol.ProtocolOptions, cfg *Config) *Server { config: cfg, } protoConfig := protocol.ProtocolConfig{ - Name: PROTOCOL_NAME, - ProtocolId: PROTOCOL_ID, + Name: ProtocolName, + ProtocolId: ProtocolId, Muxer: protoOptions.Muxer, ErrorChan: protoOptions.ErrorChan, Mode: protoOptions.Mode, @@ -38,7 +38,7 @@ func NewServer(protoOptions protocol.ProtocolOptions, cfg *Config) *Server { MessageHandlerFunc: s.messageHandler, MessageFromCborFunc: NewMsgFromCbor, StateMap: StateMap, - InitialState: STATE_IDLE, + InitialState: StateIdle, } s.Protocol = protocol.New(protoConfig) return s @@ -49,7 +49,7 @@ func (s *Server) messageHandler(msg protocol.Message, isResponse bool) error { // TODO: add cases for messages from client switch msg.Type() { default: - err = fmt.Errorf("%s: received unexpected message type %d", PROTOCOL_NAME, msg.Type()) + err = fmt.Errorf("%s: received unexpected message type %d", ProtocolName, msg.Type()) } return err } diff --git a/protocol/keepalive/client.go b/protocol/keepalive/client.go index 9f7b8947..1c75affc 100644 --- a/protocol/keepalive/client.go +++ b/protocol/keepalive/client.go @@ -39,14 +39,14 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client { } // Update state map with timeout stateMap := StateMap.Copy() - if entry, ok := stateMap[STATE_SERVER]; ok { + if entry, ok := stateMap[StateServer]; ok { entry.Timeout = c.config.Timeout - stateMap[STATE_SERVER] = entry + stateMap[StateServer] = entry } // Configure underlying Protocol protoConfig := protocol.ProtocolConfig{ - Name: PROTOCOL_NAME, - ProtocolId: PROTOCOL_ID, + Name: ProtocolName, + ProtocolId: ProtocolId, Muxer: protoOptions.Muxer, ErrorChan: protoOptions.ErrorChan, Mode: protoOptions.Mode, @@ -54,7 +54,7 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client { MessageHandlerFunc: c.messageHandler, MessageFromCborFunc: NewMsgFromCbor, StateMap: stateMap, - InitialState: STATE_CLIENT, + InitialState: StateClient, } c.Protocol = protocol.New(protoConfig) // Start goroutine to cleanup resources on protocol shutdown @@ -89,10 +89,10 @@ func (c *Client) startTimer() { func (c *Client) messageHandler(msg protocol.Message, isResponse bool) error { var err error switch msg.Type() { - case MESSAGE_TYPE_KEEP_ALIVE_RESPONSE: + case MessageTypeKeepAliveResponse: err = c.handleKeepAliveResponse(msg) default: - err = fmt.Errorf("%s: received unexpected message type %d", PROTOCOL_NAME, msg.Type()) + err = fmt.Errorf("%s: received unexpected message type %d", ProtocolName, msg.Type()) } return err } diff --git a/protocol/keepalive/keepalive.go b/protocol/keepalive/keepalive.go index 2039092f..92b330f9 100644 --- a/protocol/keepalive/keepalive.go +++ b/protocol/keepalive/keepalive.go @@ -21,46 +21,46 @@ import ( ) const ( - PROTOCOL_NAME = "keep-alive" - PROTOCOL_ID uint16 = 8 + ProtocolName = "keep-alive" + ProtocolId uint16 = 8 // Time between keep-alive probes, in seconds - DEFAULT_KEEP_ALIVE_PERIOD = 60 + DefaultKeepAlivePeroid = 60 // Timeout for keep-alive responses, in seconds - DEFAULT_KEEP_ALIVE_TIMEOUT = 10 + DefaultKeepAliveTimeout = 10 ) var ( - STATE_CLIENT = protocol.NewState(1, "Client") - STATE_SERVER = protocol.NewState(2, "Server") - STATE_DONE = protocol.NewState(3, "Done") + StateClient = protocol.NewState(1, "Client") + StateServer = protocol.NewState(2, "Server") + StateDone = protocol.NewState(3, "Done") ) var StateMap = protocol.StateMap{ - STATE_CLIENT: protocol.StateMapEntry{ + StateClient: protocol.StateMapEntry{ Agency: protocol.AgencyClient, Transitions: []protocol.StateTransition{ { - MsgType: MESSAGE_TYPE_KEEP_ALIVE, - NewState: STATE_SERVER, + MsgType: MessageTypeKeepAlive, + NewState: StateServer, }, { - MsgType: MESSAGE_TYPE_DONE, - NewState: STATE_DONE, + MsgType: MessageTypeDone, + NewState: StateDone, }, }, }, - STATE_SERVER: protocol.StateMapEntry{ + StateServer: protocol.StateMapEntry{ Agency: protocol.AgencyServer, Transitions: []protocol.StateTransition{ { - MsgType: MESSAGE_TYPE_KEEP_ALIVE_RESPONSE, - NewState: STATE_CLIENT, + MsgType: MessageTypeKeepAliveResponse, + NewState: StateClient, }, }, }, - STATE_DONE: protocol.StateMapEntry{ + StateDone: protocol.StateMapEntry{ Agency: protocol.AgencyNone, }, } @@ -95,8 +95,8 @@ type KeepAliveOptionFunc func(*Config) func NewConfig(options ...KeepAliveOptionFunc) Config { c := Config{ - Period: DEFAULT_KEEP_ALIVE_PERIOD * time.Second, - Timeout: DEFAULT_KEEP_ALIVE_TIMEOUT * time.Second, + Period: DefaultKeepAlivePeroid * time.Second, + Timeout: DefaultKeepAliveTimeout * time.Second, } // Apply provided options functions for _, option := range options { diff --git a/protocol/keepalive/messages.go b/protocol/keepalive/messages.go index 213016e9..b3bc7bac 100644 --- a/protocol/keepalive/messages.go +++ b/protocol/keepalive/messages.go @@ -21,23 +21,23 @@ import ( ) const ( - MESSAGE_TYPE_KEEP_ALIVE = 0 - MESSAGE_TYPE_KEEP_ALIVE_RESPONSE = 1 - MESSAGE_TYPE_DONE = 2 + MessageTypeKeepAlive = 0 + MessageTypeKeepAliveResponse = 1 + MessageTypeDone = 2 ) func NewMsgFromCbor(msgType uint, data []byte) (protocol.Message, error) { var ret protocol.Message switch msgType { - case MESSAGE_TYPE_KEEP_ALIVE: + case MessageTypeKeepAlive: ret = &MsgKeepAlive{} - case MESSAGE_TYPE_KEEP_ALIVE_RESPONSE: + case MessageTypeKeepAliveResponse: ret = &MsgKeepAliveResponse{} - case MESSAGE_TYPE_DONE: + case MessageTypeDone: ret = &MsgDone{} } if _, err := cbor.Decode(data, ret); err != nil { - return nil, fmt.Errorf("%s: decode error: %s", PROTOCOL_NAME, err) + return nil, fmt.Errorf("%s: decode error: %s", ProtocolName, err) } if ret != nil { // Store the raw message CBOR @@ -54,7 +54,7 @@ type MsgKeepAlive struct { func NewMsgKeepAlive(cookie uint16) *MsgKeepAlive { msg := &MsgKeepAlive{ MessageBase: protocol.MessageBase{ - MessageType: MESSAGE_TYPE_KEEP_ALIVE, + MessageType: MessageTypeKeepAlive, }, Cookie: cookie, } @@ -69,7 +69,7 @@ type MsgKeepAliveResponse struct { func NewMsgKeepAliveResponse(cookie uint16) *MsgKeepAliveResponse { msg := &MsgKeepAliveResponse{ MessageBase: protocol.MessageBase{ - MessageType: MESSAGE_TYPE_KEEP_ALIVE_RESPONSE, + MessageType: MessageTypeKeepAliveResponse, }, Cookie: cookie, } @@ -83,7 +83,7 @@ type MsgDone struct { func NewMsgDone() *MsgDone { m := &MsgDone{ MessageBase: protocol.MessageBase{ - MessageType: MESSAGE_TYPE_DONE, + MessageType: MessageTypeDone, }, } return m diff --git a/protocol/keepalive/messages_test.go b/protocol/keepalive/messages_test.go index 9b033aea..edfee422 100644 --- a/protocol/keepalive/messages_test.go +++ b/protocol/keepalive/messages_test.go @@ -32,17 +32,17 @@ var tests = []testDefinition{ { CborHex: "8200193039", Message: NewMsgKeepAlive(12345), - MessageType: MESSAGE_TYPE_KEEP_ALIVE, + MessageType: MessageTypeKeepAlive, }, { CborHex: "8201193039", Message: NewMsgKeepAliveResponse(12345), - MessageType: MESSAGE_TYPE_KEEP_ALIVE_RESPONSE, + MessageType: MessageTypeKeepAliveResponse, }, { CborHex: "8102", Message: NewMsgDone(), - MessageType: MESSAGE_TYPE_DONE, + MessageType: MessageTypeDone, }, } diff --git a/protocol/keepalive/server.go b/protocol/keepalive/server.go index 23a3b4ef..f0695a26 100644 --- a/protocol/keepalive/server.go +++ b/protocol/keepalive/server.go @@ -29,8 +29,8 @@ func NewServer(protoOptions protocol.ProtocolOptions, cfg *Config) *Server { config: cfg, } protoConfig := protocol.ProtocolConfig{ - Name: PROTOCOL_NAME, - ProtocolId: PROTOCOL_ID, + Name: ProtocolName, + ProtocolId: ProtocolId, Muxer: protoOptions.Muxer, ErrorChan: protoOptions.ErrorChan, Mode: protoOptions.Mode, @@ -38,7 +38,7 @@ func NewServer(protoOptions protocol.ProtocolOptions, cfg *Config) *Server { MessageHandlerFunc: s.messageHandler, MessageFromCborFunc: NewMsgFromCbor, StateMap: StateMap, - InitialState: STATE_CLIENT, + InitialState: StateClient, } s.Protocol = protocol.New(protoConfig) return s @@ -47,12 +47,12 @@ func NewServer(protoOptions protocol.ProtocolOptions, cfg *Config) *Server { func (s *Server) messageHandler(msg protocol.Message, isResponse bool) error { var err error switch msg.Type() { - case MESSAGE_TYPE_KEEP_ALIVE: + case MessageTypeKeepAlive: err = s.handleKeepAlive(msg) - case MESSAGE_TYPE_DONE: + case MessageTypeDone: err = s.handleDone() default: - err = fmt.Errorf("%s: received unexpected message type %d", PROTOCOL_NAME, msg.Type()) + err = fmt.Errorf("%s: received unexpected message type %d", ProtocolName, msg.Type()) } return err }