Skip to content

Commit 324a3fc

Browse files
committed
feat: consistent naming for protocol constants
1 parent cf8f5bf commit 324a3fc

File tree

10 files changed

+103
-103
lines changed

10 files changed

+103
-103
lines changed

protocol/blockfetch/blockfetch.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,58 +23,58 @@ import (
2323
)
2424

2525
const (
26-
PROTOCOL_NAME = "block-fetch"
27-
PROTOCOL_ID uint16 = 3
26+
ProtocolName = "block-fetch"
27+
ProtocolId uint16 = 3
2828
)
2929

3030
var (
31-
STATE_IDLE = protocol.NewState(1, "Idle")
32-
STATE_BUSY = protocol.NewState(2, "Busy")
33-
STATE_STREAMING = protocol.NewState(3, "Streaming")
34-
STATE_DONE = protocol.NewState(4, "Done")
31+
StateIdle = protocol.NewState(1, "Idle")
32+
StateBusy = protocol.NewState(2, "Busy")
33+
StateStreaming = protocol.NewState(3, "Streaming")
34+
StateDone = protocol.NewState(4, "Done")
3535
)
3636

3737
var StateMap = protocol.StateMap{
38-
STATE_IDLE: protocol.StateMapEntry{
38+
StateIdle: protocol.StateMapEntry{
3939
Agency: protocol.AgencyClient,
4040
Transitions: []protocol.StateTransition{
4141
{
42-
MsgType: MESSAGE_TYPE_REQUEST_RANGE,
43-
NewState: STATE_BUSY,
42+
MsgType: MessageTypeRequestRange,
43+
NewState: StateBusy,
4444
},
4545
{
46-
MsgType: MESSAGE_TYPE_CLIENT_DONE,
47-
NewState: STATE_DONE,
46+
MsgType: MessageTypeClientDone,
47+
NewState: StateDone,
4848
},
4949
},
5050
},
51-
STATE_BUSY: protocol.StateMapEntry{
51+
StateBusy: protocol.StateMapEntry{
5252
Agency: protocol.AgencyServer,
5353
Transitions: []protocol.StateTransition{
5454
{
55-
MsgType: MESSAGE_TYPE_START_BATCH,
56-
NewState: STATE_STREAMING,
55+
MsgType: MessageTypeStartBatch,
56+
NewState: StateStreaming,
5757
},
5858
{
59-
MsgType: MESSAGE_TYPE_NO_BLOCKS,
60-
NewState: STATE_IDLE,
59+
MsgType: MessageTypeNoBlocks,
60+
NewState: StateIdle,
6161
},
6262
},
6363
},
64-
STATE_STREAMING: protocol.StateMapEntry{
64+
StateStreaming: protocol.StateMapEntry{
6565
Agency: protocol.AgencyServer,
6666
Transitions: []protocol.StateTransition{
6767
{
68-
MsgType: MESSAGE_TYPE_BLOCK,
69-
NewState: STATE_STREAMING,
68+
MsgType: MessageTypeBlock,
69+
NewState: StateStreaming,
7070
},
7171
{
72-
MsgType: MESSAGE_TYPE_BATCH_DONE,
73-
NewState: STATE_IDLE,
72+
MsgType: MessageTypeBatchDone,
73+
NewState: StateIdle,
7474
},
7575
},
7676
},
77-
STATE_DONE: protocol.StateMapEntry{
77+
StateDone: protocol.StateMapEntry{
7878
Agency: protocol.AgencyNone,
7979
},
8080
}

protocol/blockfetch/client.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,26 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
4747
}
4848
// Update state map with timeouts
4949
stateMap := StateMap.Copy()
50-
if entry, ok := stateMap[STATE_BUSY]; ok {
50+
if entry, ok := stateMap[StateBusy]; ok {
5151
entry.Timeout = c.config.BatchStartTimeout
52-
stateMap[STATE_BUSY] = entry
52+
stateMap[StateBusy] = entry
5353
}
54-
if entry, ok := stateMap[STATE_STREAMING]; ok {
54+
if entry, ok := stateMap[StateStreaming]; ok {
5555
entry.Timeout = c.config.BlockTimeout
56-
stateMap[STATE_STREAMING] = entry
56+
stateMap[StateStreaming] = entry
5757
}
5858
// Configure underlying Protocol
5959
protoConfig := protocol.ProtocolConfig{
60-
Name: PROTOCOL_NAME,
61-
ProtocolId: PROTOCOL_ID,
60+
Name: ProtocolName,
61+
ProtocolId: ProtocolId,
6262
Muxer: protoOptions.Muxer,
6363
ErrorChan: protoOptions.ErrorChan,
6464
Mode: protoOptions.Mode,
6565
Role: protocol.ProtocolRoleClient,
6666
MessageHandlerFunc: c.messageHandler,
6767
MessageFromCborFunc: NewMsgFromCbor,
6868
StateMap: stateMap,
69-
InitialState: STATE_IDLE,
69+
InitialState: StateIdle,
7070
}
7171
c.Protocol = protocol.New(protoConfig)
7272
// Start goroutine to cleanup resources on protocol shutdown
@@ -127,16 +127,16 @@ func (c *Client) GetBlock(point common.Point) (ledger.Block, error) {
127127
func (c *Client) messageHandler(msg protocol.Message, isResponse bool) error {
128128
var err error
129129
switch msg.Type() {
130-
case MESSAGE_TYPE_START_BATCH:
130+
case MessageTypeStartBatch:
131131
err = c.handleStartBatch()
132-
case MESSAGE_TYPE_NO_BLOCKS:
132+
case MessageTypeNoBlocks:
133133
err = c.handleNoBlocks()
134-
case MESSAGE_TYPE_BLOCK:
134+
case MessageTypeBlock:
135135
err = c.handleBlock(msg)
136-
case MESSAGE_TYPE_BATCH_DONE:
136+
case MessageTypeBatchDone:
137137
err = c.handleBatchDone()
138138
default:
139-
err = fmt.Errorf("%s: received unexpected message type %d", PROTOCOL_NAME, msg.Type())
139+
err = fmt.Errorf("%s: received unexpected message type %d", ProtocolName, msg.Type())
140140
}
141141
return err
142142
}
@@ -157,7 +157,7 @@ func (c *Client) handleBlock(msgGeneric protocol.Message) error {
157157
// Decode only enough to get the block type value
158158
var wrappedBlock WrappedBlock
159159
if _, err := cbor.Decode(msg.WrappedBlock, &wrappedBlock); err != nil {
160-
return fmt.Errorf("%s: decode error: %s", PROTOCOL_NAME, err)
160+
return fmt.Errorf("%s: decode error: %s", ProtocolName, err)
161161
}
162162
blk, err := ledger.NewBlockFromCbor(wrappedBlock.Type, wrappedBlock.RawBlock)
163163
if err != nil {

protocol/blockfetch/messages.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,32 @@ import (
2121
)
2222

2323
const (
24-
MESSAGE_TYPE_REQUEST_RANGE = 0
25-
MESSAGE_TYPE_CLIENT_DONE = 1
26-
MESSAGE_TYPE_START_BATCH = 2
27-
MESSAGE_TYPE_NO_BLOCKS = 3
28-
MESSAGE_TYPE_BLOCK = 4
29-
MESSAGE_TYPE_BATCH_DONE = 5
24+
MessageTypeRequestRange = 0
25+
MessageTypeClientDone = 1
26+
MessageTypeStartBatch = 2
27+
MessageTypeNoBlocks = 3
28+
MessageTypeBlock = 4
29+
MessageTypeBatchDone = 5
3030
)
3131

3232
func NewMsgFromCbor(msgType uint, data []byte) (protocol.Message, error) {
3333
var ret protocol.Message
3434
switch msgType {
35-
case MESSAGE_TYPE_REQUEST_RANGE:
35+
case MessageTypeRequestRange:
3636
ret = &MsgRequestRange{}
37-
case MESSAGE_TYPE_CLIENT_DONE:
37+
case MessageTypeClientDone:
3838
ret = &MsgClientDone{}
39-
case MESSAGE_TYPE_START_BATCH:
39+
case MessageTypeStartBatch:
4040
ret = &MsgStartBatch{}
41-
case MESSAGE_TYPE_NO_BLOCKS:
41+
case MessageTypeNoBlocks:
4242
ret = &MsgNoBlocks{}
43-
case MESSAGE_TYPE_BLOCK:
43+
case MessageTypeBlock:
4444
ret = &MsgBlock{}
45-
case MESSAGE_TYPE_BATCH_DONE:
45+
case MessageTypeBatchDone:
4646
ret = &MsgBatchDone{}
4747
}
4848
if _, err := cbor.Decode(data, ret); err != nil {
49-
return nil, fmt.Errorf("%s: decode error: %s", PROTOCOL_NAME, err)
49+
return nil, fmt.Errorf("%s: decode error: %s", ProtocolName, err)
5050
}
5151
if ret != nil {
5252
// Store the raw message CBOR
@@ -64,7 +64,7 @@ type MsgRequestRange struct {
6464
func NewMsgRequestRange(start interface{}, end interface{}) *MsgRequestRange {
6565
m := &MsgRequestRange{
6666
MessageBase: protocol.MessageBase{
67-
MessageType: MESSAGE_TYPE_REQUEST_RANGE,
67+
MessageType: MessageTypeRequestRange,
6868
},
6969
Start: start,
7070
End: end,
@@ -79,7 +79,7 @@ type MsgClientDone struct {
7979
func NewMsgClientDone() *MsgClientDone {
8080
m := &MsgClientDone{
8181
MessageBase: protocol.MessageBase{
82-
MessageType: MESSAGE_TYPE_CLIENT_DONE,
82+
MessageType: MessageTypeClientDone,
8383
},
8484
}
8585
return m
@@ -92,7 +92,7 @@ type MsgStartBatch struct {
9292
func NewMsgStartBatch() *MsgStartBatch {
9393
m := &MsgStartBatch{
9494
MessageBase: protocol.MessageBase{
95-
MessageType: MESSAGE_TYPE_START_BATCH,
95+
MessageType: MessageTypeStartBatch,
9696
},
9797
}
9898
return m
@@ -105,7 +105,7 @@ type MsgNoBlocks struct {
105105
func NewMsgNoBlocks() *MsgNoBlocks {
106106
m := &MsgNoBlocks{
107107
MessageBase: protocol.MessageBase{
108-
MessageType: MESSAGE_TYPE_NO_BLOCKS,
108+
MessageType: MessageTypeNoBlocks,
109109
},
110110
}
111111
return m
@@ -119,7 +119,7 @@ type MsgBlock struct {
119119
func NewMsgBlock(wrappedBlock []byte) *MsgBlock {
120120
m := &MsgBlock{
121121
MessageBase: protocol.MessageBase{
122-
MessageType: MESSAGE_TYPE_BLOCK,
122+
MessageType: MessageTypeBlock,
123123
},
124124
WrappedBlock: wrappedBlock,
125125
}
@@ -133,7 +133,7 @@ type MsgBatchDone struct {
133133
func NewMsgBatchDone() *MsgBatchDone {
134134
m := &MsgBatchDone{
135135
MessageBase: protocol.MessageBase{
136-
MessageType: MESSAGE_TYPE_BATCH_DONE,
136+
MessageType: MessageTypeBatchDone,
137137
},
138138
}
139139
return m

protocol/blockfetch/messages_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var tests = []testDefinition{
3333
{
3434
CborHex: "8105",
3535
Message: NewMsgBatchDone(),
36-
MessageType: MESSAGE_TYPE_BATCH_DONE,
36+
MessageType: MessageTypeBatchDone,
3737
},
3838
}
3939

protocol/blockfetch/server.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ func NewServer(protoOptions protocol.ProtocolOptions, cfg *Config) *Server {
2929
config: cfg,
3030
}
3131
protoConfig := protocol.ProtocolConfig{
32-
Name: PROTOCOL_NAME,
33-
ProtocolId: PROTOCOL_ID,
32+
Name: ProtocolName,
33+
ProtocolId: ProtocolId,
3434
Muxer: protoOptions.Muxer,
3535
ErrorChan: protoOptions.ErrorChan,
3636
Mode: protoOptions.Mode,
3737
Role: protocol.ProtocolRoleServer,
3838
MessageHandlerFunc: s.messageHandler,
3939
MessageFromCborFunc: NewMsgFromCbor,
4040
StateMap: StateMap,
41-
InitialState: STATE_IDLE,
41+
InitialState: StateIdle,
4242
}
4343
s.Protocol = protocol.New(protoConfig)
4444
return s
@@ -49,7 +49,7 @@ func (s *Server) messageHandler(msg protocol.Message, isResponse bool) error {
4949
// TODO: add cases for messages from client
5050
switch msg.Type() {
5151
default:
52-
err = fmt.Errorf("%s: received unexpected message type %d", PROTOCOL_NAME, msg.Type())
52+
err = fmt.Errorf("%s: received unexpected message type %d", ProtocolName, msg.Type())
5353
}
5454
return err
5555
}

protocol/keepalive/client.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,22 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
3939
}
4040
// Update state map with timeout
4141
stateMap := StateMap.Copy()
42-
if entry, ok := stateMap[STATE_SERVER]; ok {
42+
if entry, ok := stateMap[StateServer]; ok {
4343
entry.Timeout = c.config.Timeout
44-
stateMap[STATE_SERVER] = entry
44+
stateMap[StateServer] = entry
4545
}
4646
// Configure underlying Protocol
4747
protoConfig := protocol.ProtocolConfig{
48-
Name: PROTOCOL_NAME,
49-
ProtocolId: PROTOCOL_ID,
48+
Name: ProtocolName,
49+
ProtocolId: ProtocolId,
5050
Muxer: protoOptions.Muxer,
5151
ErrorChan: protoOptions.ErrorChan,
5252
Mode: protoOptions.Mode,
5353
Role: protocol.ProtocolRoleClient,
5454
MessageHandlerFunc: c.messageHandler,
5555
MessageFromCborFunc: NewMsgFromCbor,
5656
StateMap: stateMap,
57-
InitialState: STATE_CLIENT,
57+
InitialState: StateClient,
5858
}
5959
c.Protocol = protocol.New(protoConfig)
6060
// Start goroutine to cleanup resources on protocol shutdown
@@ -89,10 +89,10 @@ func (c *Client) startTimer() {
8989
func (c *Client) messageHandler(msg protocol.Message, isResponse bool) error {
9090
var err error
9191
switch msg.Type() {
92-
case MESSAGE_TYPE_KEEP_ALIVE_RESPONSE:
92+
case MessageTypeKeepAliveResponse:
9393
err = c.handleKeepAliveResponse(msg)
9494
default:
95-
err = fmt.Errorf("%s: received unexpected message type %d", PROTOCOL_NAME, msg.Type())
95+
err = fmt.Errorf("%s: received unexpected message type %d", ProtocolName, msg.Type())
9696
}
9797
return err
9898
}

protocol/keepalive/keepalive.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,46 @@ import (
2121
)
2222

2323
const (
24-
PROTOCOL_NAME = "keep-alive"
25-
PROTOCOL_ID uint16 = 8
24+
ProtocolName = "keep-alive"
25+
ProtocolId uint16 = 8
2626

2727
// Time between keep-alive probes, in seconds
28-
DEFAULT_KEEP_ALIVE_PERIOD = 60
28+
DefaultKeepAlivePeroid = 60
2929

3030
// Timeout for keep-alive responses, in seconds
31-
DEFAULT_KEEP_ALIVE_TIMEOUT = 10
31+
DefaultKeepAliveTimeout = 10
3232
)
3333

3434
var (
35-
STATE_CLIENT = protocol.NewState(1, "Client")
36-
STATE_SERVER = protocol.NewState(2, "Server")
37-
STATE_DONE = protocol.NewState(3, "Done")
35+
StateClient = protocol.NewState(1, "Client")
36+
StateServer = protocol.NewState(2, "Server")
37+
StateDone = protocol.NewState(3, "Done")
3838
)
3939

4040
var StateMap = protocol.StateMap{
41-
STATE_CLIENT: protocol.StateMapEntry{
41+
StateClient: protocol.StateMapEntry{
4242
Agency: protocol.AgencyClient,
4343
Transitions: []protocol.StateTransition{
4444
{
45-
MsgType: MESSAGE_TYPE_KEEP_ALIVE,
46-
NewState: STATE_SERVER,
45+
MsgType: MessageTypeKeepAlive,
46+
NewState: StateServer,
4747
},
4848
{
49-
MsgType: MESSAGE_TYPE_DONE,
50-
NewState: STATE_DONE,
49+
MsgType: MessageTypeDone,
50+
NewState: StateDone,
5151
},
5252
},
5353
},
54-
STATE_SERVER: protocol.StateMapEntry{
54+
StateServer: protocol.StateMapEntry{
5555
Agency: protocol.AgencyServer,
5656
Transitions: []protocol.StateTransition{
5757
{
58-
MsgType: MESSAGE_TYPE_KEEP_ALIVE_RESPONSE,
59-
NewState: STATE_CLIENT,
58+
MsgType: MessageTypeKeepAliveResponse,
59+
NewState: StateClient,
6060
},
6161
},
6262
},
63-
STATE_DONE: protocol.StateMapEntry{
63+
StateDone: protocol.StateMapEntry{
6464
Agency: protocol.AgencyNone,
6565
},
6666
}
@@ -95,8 +95,8 @@ type KeepAliveOptionFunc func(*Config)
9595

9696
func NewConfig(options ...KeepAliveOptionFunc) Config {
9797
c := Config{
98-
Period: DEFAULT_KEEP_ALIVE_PERIOD * time.Second,
99-
Timeout: DEFAULT_KEEP_ALIVE_TIMEOUT * time.Second,
98+
Period: DefaultKeepAlivePeroid * time.Second,
99+
Timeout: DefaultKeepAliveTimeout * time.Second,
100100
}
101101
// Apply provided options functions
102102
for _, option := range options {

0 commit comments

Comments
 (0)