Skip to content

Commit 4c312a9

Browse files
committed
docs: code docs for protocol package
This also includes some rearranging/renaming of things to make the docs look better Fixes #154
1 parent 02217fa commit 4c312a9

File tree

10 files changed

+77
-49
lines changed

10 files changed

+77
-49
lines changed

protocol/blockfetch/blockfetch.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var (
2020

2121
var StateMap = protocol.StateMap{
2222
STATE_IDLE: protocol.StateMapEntry{
23-
Agency: protocol.AGENCY_CLIENT,
23+
Agency: protocol.AgencyClient,
2424
Transitions: []protocol.StateTransition{
2525
{
2626
MsgType: MESSAGE_TYPE_REQUEST_RANGE,
@@ -33,7 +33,7 @@ var StateMap = protocol.StateMap{
3333
},
3434
},
3535
STATE_BUSY: protocol.StateMapEntry{
36-
Agency: protocol.AGENCY_SERVER,
36+
Agency: protocol.AgencyServer,
3737
Transitions: []protocol.StateTransition{
3838
{
3939
MsgType: MESSAGE_TYPE_START_BATCH,
@@ -46,7 +46,7 @@ var StateMap = protocol.StateMap{
4646
},
4747
},
4848
STATE_STREAMING: protocol.StateMapEntry{
49-
Agency: protocol.AGENCY_SERVER,
49+
Agency: protocol.AgencyServer,
5050
Transitions: []protocol.StateTransition{
5151
{
5252
MsgType: MESSAGE_TYPE_BLOCK,
@@ -59,7 +59,7 @@ var StateMap = protocol.StateMap{
5959
},
6060
},
6161
STATE_DONE: protocol.StateMapEntry{
62-
Agency: protocol.AGENCY_NONE,
62+
Agency: protocol.AgencyNone,
6363
},
6464
}
6565

protocol/chainsync/chainsync.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var (
2323

2424
var StateMap = protocol.StateMap{
2525
STATE_IDLE: protocol.StateMapEntry{
26-
Agency: protocol.AGENCY_CLIENT,
26+
Agency: protocol.AgencyClient,
2727
Transitions: []protocol.StateTransition{
2828
{
2929
MsgType: MESSAGE_TYPE_REQUEST_NEXT,
@@ -40,7 +40,7 @@ var StateMap = protocol.StateMap{
4040
},
4141
},
4242
STATE_CAN_AWAIT: protocol.StateMapEntry{
43-
Agency: protocol.AGENCY_SERVER,
43+
Agency: protocol.AgencyServer,
4444
Transitions: []protocol.StateTransition{
4545
{
4646
MsgType: MESSAGE_TYPE_AWAIT_REPLY,
@@ -57,7 +57,7 @@ var StateMap = protocol.StateMap{
5757
},
5858
},
5959
STATE_INTERSECT: protocol.StateMapEntry{
60-
Agency: protocol.AGENCY_SERVER,
60+
Agency: protocol.AgencyServer,
6161
Transitions: []protocol.StateTransition{
6262
{
6363
MsgType: MESSAGE_TYPE_INTERSECT_FOUND,
@@ -70,7 +70,7 @@ var StateMap = protocol.StateMap{
7070
},
7171
},
7272
STATE_MUST_REPLY: protocol.StateMapEntry{
73-
Agency: protocol.AGENCY_SERVER,
73+
Agency: protocol.AgencyServer,
7474
Transitions: []protocol.StateTransition{
7575
{
7676
MsgType: MESSAGE_TYPE_ROLL_FORWARD,
@@ -83,7 +83,7 @@ var StateMap = protocol.StateMap{
8383
},
8484
},
8585
STATE_DONE: protocol.StateMapEntry{
86-
Agency: protocol.AGENCY_NONE,
86+
Agency: protocol.AgencyNone,
8787
},
8888
}
8989

protocol/handshake/handshake.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var (
2222

2323
var StateMap = protocol.StateMap{
2424
STATE_PROPOSE: protocol.StateMapEntry{
25-
Agency: protocol.AGENCY_CLIENT,
25+
Agency: protocol.AgencyClient,
2626
Transitions: []protocol.StateTransition{
2727
{
2828
MsgType: MESSAGE_TYPE_PROPOSE_VERSIONS,
@@ -31,7 +31,7 @@ var StateMap = protocol.StateMap{
3131
},
3232
},
3333
STATE_CONFIRM: protocol.StateMapEntry{
34-
Agency: protocol.AGENCY_SERVER,
34+
Agency: protocol.AgencyServer,
3535
Transitions: []protocol.StateTransition{
3636
{
3737
MsgType: MESSAGE_TYPE_ACCEPT_VERSION,
@@ -44,7 +44,7 @@ var StateMap = protocol.StateMap{
4444
},
4545
},
4646
STATE_DONE: protocol.StateMapEntry{
47-
Agency: protocol.AGENCY_NONE,
47+
Agency: protocol.AgencyNone,
4848
},
4949
}
5050

protocol/keepalive/keepalive.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var (
2525

2626
var StateMap = protocol.StateMap{
2727
STATE_CLIENT: protocol.StateMapEntry{
28-
Agency: protocol.AGENCY_CLIENT,
28+
Agency: protocol.AgencyClient,
2929
Transitions: []protocol.StateTransition{
3030
{
3131
MsgType: MESSAGE_TYPE_KEEP_ALIVE,
@@ -38,7 +38,7 @@ var StateMap = protocol.StateMap{
3838
},
3939
},
4040
STATE_SERVER: protocol.StateMapEntry{
41-
Agency: protocol.AGENCY_SERVER,
41+
Agency: protocol.AgencyServer,
4242
Transitions: []protocol.StateTransition{
4343
{
4444
MsgType: MESSAGE_TYPE_KEEP_ALIVE_RESPONSE,
@@ -47,7 +47,7 @@ var StateMap = protocol.StateMap{
4747
},
4848
},
4949
STATE_DONE: protocol.StateMapEntry{
50-
Agency: protocol.AGENCY_NONE,
50+
Agency: protocol.AgencyNone,
5151
},
5252
}
5353

protocol/localstatequery/localstatequery.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var (
2121

2222
var StateMap = protocol.StateMap{
2323
STATE_IDLE: protocol.StateMapEntry{
24-
Agency: protocol.AGENCY_CLIENT,
24+
Agency: protocol.AgencyClient,
2525
Transitions: []protocol.StateTransition{
2626
{
2727
MsgType: MESSAGE_TYPE_ACQUIRE,
@@ -38,7 +38,7 @@ var StateMap = protocol.StateMap{
3838
},
3939
},
4040
STATE_ACQUIRING: protocol.StateMapEntry{
41-
Agency: protocol.AGENCY_SERVER,
41+
Agency: protocol.AgencyServer,
4242
Transitions: []protocol.StateTransition{
4343
{
4444
MsgType: MESSAGE_TYPE_FAILURE,
@@ -51,7 +51,7 @@ var StateMap = protocol.StateMap{
5151
},
5252
},
5353
STATE_ACQUIRED: protocol.StateMapEntry{
54-
Agency: protocol.AGENCY_CLIENT,
54+
Agency: protocol.AgencyClient,
5555
Transitions: []protocol.StateTransition{
5656
{
5757
MsgType: MESSAGE_TYPE_QUERY,
@@ -72,7 +72,7 @@ var StateMap = protocol.StateMap{
7272
},
7373
},
7474
STATE_QUERYING: protocol.StateMapEntry{
75-
Agency: protocol.AGENCY_SERVER,
75+
Agency: protocol.AgencyServer,
7676
Transitions: []protocol.StateTransition{
7777
{
7878
MsgType: MESSAGE_TYPE_RESULT,
@@ -81,7 +81,7 @@ var StateMap = protocol.StateMap{
8181
},
8282
},
8383
STATE_DONE: protocol.StateMapEntry{
84-
Agency: protocol.AGENCY_NONE,
84+
Agency: protocol.AgencyNone,
8585
},
8686
}
8787

protocol/localtxsubmission/localtxsubmission.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var (
1919

2020
var StateMap = protocol.StateMap{
2121
STATE_IDLE: protocol.StateMapEntry{
22-
Agency: protocol.AGENCY_CLIENT,
22+
Agency: protocol.AgencyClient,
2323
Transitions: []protocol.StateTransition{
2424
{
2525
MsgType: MESSAGE_TYPE_SUBMIT_TX,
@@ -28,7 +28,7 @@ var StateMap = protocol.StateMap{
2828
},
2929
},
3030
STATE_BUSY: protocol.StateMapEntry{
31-
Agency: protocol.AGENCY_SERVER,
31+
Agency: protocol.AgencyServer,
3232
Transitions: []protocol.StateTransition{
3333
{
3434
MsgType: MESSAGE_TYPE_ACCEPT_TX,
@@ -41,7 +41,7 @@ var StateMap = protocol.StateMap{
4141
},
4242
},
4343
STATE_DONE: protocol.StateMapEntry{
44-
Agency: protocol.AGENCY_NONE,
44+
Agency: protocol.AgencyNone,
4545
},
4646
}
4747

protocol/message.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
package protocol
22

3-
// Provide a common interface for message utility functions
3+
// Message provides a common interface for message utility functions
44
type Message interface {
55
SetCbor([]byte)
66
Cbor() []byte
77
Type() uint8
88
}
99

10+
// MessageBase is the minimum implementation for a mini-protocol message
1011
type MessageBase struct {
1112
// Tells the CBOR decoder to convert to/from a struct and a CBOR array
1213
_ struct{} `cbor:",toarray"`
1314
rawCbor []byte
1415
MessageType uint8
1516
}
1617

18+
// SetCbor stores the original CBOR that was parsed
1719
func (m *MessageBase) SetCbor(data []byte) {
1820
m.rawCbor = make([]byte, len(data))
1921
copy(m.rawCbor, data)
2022
}
2123

24+
// Cbor returns the original CBOR that was parsed
2225
func (m *MessageBase) Cbor() []byte {
2326
return m.rawCbor
2427
}
2528

29+
// Type returns the message type
2630
func (m *MessageBase) Type() uint8 {
2731
return m.MessageType
2832
}

protocol/protocol.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package protocol provides the common functionality for mini-protocols
12
package protocol
23

34
import (
@@ -13,11 +14,10 @@ import (
1314
"github.com/fxamacker/cbor/v2"
1415
)
1516

16-
const (
17-
// This is completely arbitrary, but the line had to be drawn somewhere
18-
MAX_MESSAGES_PER_SEGMENT = 20
19-
)
17+
// This is completely arbitrary, but the line had to be drawn somewhere
18+
const maxMessagesPerSegment = 20
2019

20+
// Protocol implements the base functionality of an Ouroboros mini-protocol
2121
type Protocol struct {
2222
config ProtocolConfig
2323
muxerSendChan chan *muxer.Segment
@@ -34,6 +34,7 @@ type Protocol struct {
3434
stateTransitionTimer *time.Timer
3535
}
3636

37+
// ProtocolConfig provides the configuration for Protocol
3738
type ProtocolConfig struct {
3839
Name string
3940
ProtocolId uint16
@@ -47,23 +48,25 @@ type ProtocolConfig struct {
4748
InitialState State
4849
}
4950

51+
// ProtocolMode is an enum of the protocol modes
5052
type ProtocolMode uint
5153

5254
const (
53-
ProtocolModeNone ProtocolMode = 0
54-
ProtocolModeNodeToClient ProtocolMode = 1
55-
ProtocolModeNodeToNode ProtocolMode = 2
55+
ProtocolModeNone ProtocolMode = 0 // Default (invalid) protocol mode
56+
ProtocolModeNodeToClient ProtocolMode = 1 // Node-to-client protocol mode
57+
ProtocolModeNodeToNode ProtocolMode = 2 // Node-to-node protocol mode
5658
)
5759

60+
// ProtocolRole is an enum of the protocol roles
5861
type ProtocolRole uint
5962

6063
const (
61-
ProtocolRoleNone ProtocolRole = 0
62-
ProtocolRoleClient ProtocolRole = 1
63-
ProtocolRoleServer ProtocolRole = 2
64+
ProtocolRoleNone ProtocolRole = 0 // Default (invalid) protocol role
65+
ProtocolRoleClient ProtocolRole = 1 // Client protocol role
66+
ProtocolRoleServer ProtocolRole = 2 // Server protocol role
6467
)
6568

66-
// Common arguments for individual mini-protocols
69+
// ProtocolOptions provides common arguments for all mini-protocols
6770
type ProtocolOptions struct {
6871
Muxer *muxer.Muxer
6972
ErrorChan chan error
@@ -73,9 +76,13 @@ type ProtocolOptions struct {
7376
Version uint16
7477
}
7578

79+
// MessageHandlerFunc represents a function that handles an incoming message
7680
type MessageHandlerFunc func(Message, bool) error
81+
82+
// MessageFromCborFunc represents a function that parses a mini-protocol message
7783
type MessageFromCborFunc func(uint, []byte) (Message, error)
7884

85+
// New returns a new Protocol object
7986
func New(config ProtocolConfig) *Protocol {
8087
p := &Protocol{
8188
config: config,
@@ -84,6 +91,7 @@ func New(config ProtocolConfig) *Protocol {
8491
return p
8592
}
8693

94+
// Start initializes the mini-protocol
8795
func (p *Protocol) Start() {
8896
// Register protocol with muxer
8997
p.muxerSendChan, p.muxerRecvChan, p.muxerDoneChan = p.config.Muxer.RegisterProtocol(p.config.ProtocolId)
@@ -108,23 +116,28 @@ func (p *Protocol) Start() {
108116
go p.sendLoop()
109117
}
110118

119+
// Mode returns the protocol mode
111120
func (p *Protocol) Mode() ProtocolMode {
112121
return p.config.Mode
113122
}
114123

124+
// Role understands the protocol role
115125
func (p *Protocol) Role() ProtocolRole {
116126
return p.config.Role
117127
}
118128

129+
// DoneChan returns the channel used to signal protocol shutdown
119130
func (p *Protocol) DoneChan() chan bool {
120131
return p.doneChan
121132
}
122133

134+
// SendMessage appends a message to the send queue
123135
func (p *Protocol) SendMessage(msg Message) error {
124136
p.sendQueueChan <- msg
125137
return nil
126138
}
127139

140+
// SendError sends an error to the handler in the Ouroboros object
128141
func (p *Protocol) SendError(err error) {
129142
p.config.ErrorChan <- err
130143
}
@@ -198,8 +211,8 @@ func (p *Protocol) sendLoop() {
198211
}
199212
setNewState = true
200213
}
201-
// We don't want more than MAX_MESSAGES_PER_SEGMENT messages in a segment
202-
if msgCount >= MAX_MESSAGES_PER_SEGMENT {
214+
// We don't want more than maxMessagesPerSegment messages in a segment
215+
if msgCount >= maxMessagesPerSegment {
203216
break
204217
}
205218
// We don't want to add more messages once we spill over into a second segment
@@ -357,14 +370,14 @@ func (p *Protocol) setState(state State) {
357370
p.state = state
358371
// Mark protocol as ready to send/receive based on role and agency of the new state
359372
switch p.config.StateMap[p.state].Agency {
360-
case AGENCY_CLIENT:
373+
case AgencyClient:
361374
switch p.config.Role {
362375
case ProtocolRoleClient:
363376
p.sendReadyChan <- true
364377
case ProtocolRoleServer:
365378
p.recvReadyChan <- true
366379
}
367-
case AGENCY_SERVER:
380+
case AgencyServer:
368381
switch p.config.Role {
369382
case ProtocolRoleServer:
370383
p.sendReadyChan <- true

0 commit comments

Comments
 (0)