Skip to content

Commit

Permalink
Merge pull request #394 from blinklabs-io/feat/proto-consistent-naming
Browse files Browse the repository at this point in the history
feat: consistent naming for protocol constants
  • Loading branch information
agaffney authored Oct 12, 2023
2 parents 50e9efa + 324a3fc commit 5a96a5d
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 103 deletions.
44 changes: 22 additions & 22 deletions protocol/blockfetch/blockfetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand Down
26 changes: 13 additions & 13 deletions protocol/blockfetch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,26 @@ 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,
Role: protocol.ProtocolRoleClient,
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
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
38 changes: 19 additions & 19 deletions protocol/blockfetch/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -79,7 +79,7 @@ type MsgClientDone struct {
func NewMsgClientDone() *MsgClientDone {
m := &MsgClientDone{
MessageBase: protocol.MessageBase{
MessageType: MESSAGE_TYPE_CLIENT_DONE,
MessageType: MessageTypeClientDone,
},
}
return m
Expand All @@ -92,7 +92,7 @@ type MsgStartBatch struct {
func NewMsgStartBatch() *MsgStartBatch {
m := &MsgStartBatch{
MessageBase: protocol.MessageBase{
MessageType: MESSAGE_TYPE_START_BATCH,
MessageType: MessageTypeStartBatch,
},
}
return m
Expand All @@ -105,7 +105,7 @@ type MsgNoBlocks struct {
func NewMsgNoBlocks() *MsgNoBlocks {
m := &MsgNoBlocks{
MessageBase: protocol.MessageBase{
MessageType: MESSAGE_TYPE_NO_BLOCKS,
MessageType: MessageTypeNoBlocks,
},
}
return m
Expand All @@ -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,
}
Expand All @@ -133,7 +133,7 @@ type MsgBatchDone struct {
func NewMsgBatchDone() *MsgBatchDone {
m := &MsgBatchDone{
MessageBase: protocol.MessageBase{
MessageType: MESSAGE_TYPE_BATCH_DONE,
MessageType: MessageTypeBatchDone,
},
}
return m
Expand Down
2 changes: 1 addition & 1 deletion protocol/blockfetch/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var tests = []testDefinition{
{
CborHex: "8105",
Message: NewMsgBatchDone(),
MessageType: MESSAGE_TYPE_BATCH_DONE,
MessageType: MessageTypeBatchDone,
},
}

Expand Down
8 changes: 4 additions & 4 deletions protocol/blockfetch/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ 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,
Role: protocol.ProtocolRoleServer,
MessageHandlerFunc: s.messageHandler,
MessageFromCborFunc: NewMsgFromCbor,
StateMap: StateMap,
InitialState: STATE_IDLE,
InitialState: StateIdle,
}
s.Protocol = protocol.New(protoConfig)
return s
Expand All @@ -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
}
14 changes: 7 additions & 7 deletions protocol/keepalive/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,22 @@ 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,
Role: protocol.ProtocolRoleClient,
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
Expand Down Expand Up @@ -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
}
Expand Down
36 changes: 18 additions & 18 deletions protocol/keepalive/keepalive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 5a96a5d

Please sign in to comment.