1+ // Package chainsync implements the Ouroboros chain-sync protocol
12package chainsync
23
34import (
@@ -7,91 +8,95 @@ import (
78 "github.com/cloudstruct/go-ouroboros-network/protocol/common"
89)
910
11+ // Protocol identifiers
1012const (
11- PROTOCOL_NAME = "chain-sync"
12- PROTOCOL_ID_NTN uint16 = 2
13- PROTOCOL_ID_NTC uint16 = 5
13+ protocolName = "chain-sync"
14+ protocolIdNtN uint16 = 2
15+ protocolIdNtC uint16 = 5
1416)
1517
1618var (
17- STATE_IDLE = protocol .NewState (1 , "Idle" )
18- STATE_CAN_AWAIT = protocol .NewState (2 , "CanAwait" )
19- STATE_MUST_REPLY = protocol .NewState (3 , "MustReply" )
20- STATE_INTERSECT = protocol .NewState (4 , "Intersect" )
21- STATE_DONE = protocol .NewState (5 , "Done" )
19+ stateIdle = protocol .NewState (1 , "Idle" )
20+ stateCanAwait = protocol .NewState (2 , "CanAwait" )
21+ stateMustReply = protocol .NewState (3 , "MustReply" )
22+ stateIntersect = protocol .NewState (4 , "Intersect" )
23+ stateDone = protocol .NewState (5 , "Done" )
2224)
2325
26+ // ChainSync protocol state machine
2427var StateMap = protocol.StateMap {
25- STATE_IDLE : protocol.StateMapEntry {
28+ stateIdle : protocol.StateMapEntry {
2629 Agency : protocol .AgencyClient ,
2730 Transitions : []protocol.StateTransition {
2831 {
29- MsgType : MESSAGE_TYPE_REQUEST_NEXT ,
30- NewState : STATE_CAN_AWAIT ,
32+ MsgType : MessageTypeRequestNext ,
33+ NewState : stateCanAwait ,
3134 },
3235 {
33- MsgType : MESSAGE_TYPE_FIND_INTERSECT ,
34- NewState : STATE_INTERSECT ,
36+ MsgType : MessageTypeFindIntersect ,
37+ NewState : stateIntersect ,
3538 },
3639 {
37- MsgType : MESSAGE_TYPE_DONE ,
38- NewState : STATE_DONE ,
40+ MsgType : MessageTypeDone ,
41+ NewState : stateDone ,
3942 },
4043 },
4144 },
42- STATE_CAN_AWAIT : protocol.StateMapEntry {
45+ stateCanAwait : protocol.StateMapEntry {
4346 Agency : protocol .AgencyServer ,
4447 Transitions : []protocol.StateTransition {
4548 {
46- MsgType : MESSAGE_TYPE_AWAIT_REPLY ,
47- NewState : STATE_MUST_REPLY ,
49+ MsgType : MessageTypeAwaitReply ,
50+ NewState : stateMustReply ,
4851 },
4952 {
50- MsgType : MESSAGE_TYPE_ROLL_FORWARD ,
51- NewState : STATE_IDLE ,
53+ MsgType : MessageTypeRollForward ,
54+ NewState : stateIdle ,
5255 },
5356 {
54- MsgType : MESSAGE_TYPE_ROLL_BACKWARD ,
55- NewState : STATE_IDLE ,
57+ MsgType : MessageTypeRollBackward ,
58+ NewState : stateIdle ,
5659 },
5760 },
5861 },
59- STATE_INTERSECT : protocol.StateMapEntry {
62+ stateIntersect : protocol.StateMapEntry {
6063 Agency : protocol .AgencyServer ,
6164 Transitions : []protocol.StateTransition {
6265 {
63- MsgType : MESSAGE_TYPE_INTERSECT_FOUND ,
64- NewState : STATE_IDLE ,
66+ MsgType : MessageTypeIntersectFound ,
67+ NewState : stateIdle ,
6568 },
6669 {
67- MsgType : MESSAGE_TYPE_INTERSECT_NOT_FOUND ,
68- NewState : STATE_IDLE ,
70+ MsgType : MessageTypeIntersectNotFound ,
71+ NewState : stateIdle ,
6972 },
7073 },
7174 },
72- STATE_MUST_REPLY : protocol.StateMapEntry {
75+ stateMustReply : protocol.StateMapEntry {
7376 Agency : protocol .AgencyServer ,
7477 Transitions : []protocol.StateTransition {
7578 {
76- MsgType : MESSAGE_TYPE_ROLL_FORWARD ,
77- NewState : STATE_IDLE ,
79+ MsgType : MessageTypeRollForward ,
80+ NewState : stateIdle ,
7881 },
7982 {
80- MsgType : MESSAGE_TYPE_ROLL_BACKWARD ,
81- NewState : STATE_IDLE ,
83+ MsgType : MessageTypeRollBackward ,
84+ NewState : stateIdle ,
8285 },
8386 },
8487 },
85- STATE_DONE : protocol.StateMapEntry {
88+ stateDone : protocol.StateMapEntry {
8689 Agency : protocol .AgencyNone ,
8790 },
8891}
8992
93+ // ChainSync is a wrapper object that holds the client and server instances
9094type ChainSync struct {
9195 Client * Client
9296 Server * Server
9397}
9498
99+ // Config is used to configure the ChainSync protocol instance
95100type Config struct {
96101 RollBackwardFunc RollBackwardFunc
97102 RollForwardFunc RollForwardFunc
@@ -103,6 +108,7 @@ type Config struct {
103108type RollBackwardFunc func (common.Point , Tip ) error
104109type RollForwardFunc func (uint , interface {}, Tip ) error
105110
111+ // New returns a new ChainSync object
106112func New (protoOptions protocol.ProtocolOptions , cfg * Config ) * ChainSync {
107113 c := & ChainSync {
108114 Client : NewClient (protoOptions , cfg ),
@@ -111,8 +117,10 @@ func New(protoOptions protocol.ProtocolOptions, cfg *Config) *ChainSync {
111117 return c
112118}
113119
120+ // ChainSyncOptionFunc represents a function used to modify the ChainSync protocol config
114121type ChainSyncOptionFunc func (* Config )
115122
123+ // NewConfig returns a new ChainSync config object with the provided options
116124func NewConfig (options ... ChainSyncOptionFunc ) Config {
117125 c := Config {
118126 IntersectTimeout : 5 * time .Second ,
@@ -129,24 +137,28 @@ func NewConfig(options ...ChainSyncOptionFunc) Config {
129137 return c
130138}
131139
140+ // WithRollBackwardFunc specifies the RollBackward callback function
132141func WithRollBackwardFunc (rollBackwardFunc RollBackwardFunc ) ChainSyncOptionFunc {
133142 return func (c * Config ) {
134143 c .RollBackwardFunc = rollBackwardFunc
135144 }
136145}
137146
147+ // WithRollForwardFunc specifies the RollForward callback function
138148func WithRollForwardFunc (rollForwardFunc RollForwardFunc ) ChainSyncOptionFunc {
139149 return func (c * Config ) {
140150 c .RollForwardFunc = rollForwardFunc
141151 }
142152}
143153
154+ // WithIntersectTimeout specifies the timeout for intersect operations
144155func WithIntersectTimeout (timeout time.Duration ) ChainSyncOptionFunc {
145156 return func (c * Config ) {
146157 c .IntersectTimeout = timeout
147158 }
148159}
149160
161+ // WithBlockTimeout specifies the timeout for block fetch operations
150162func WithBlockTimeout (timeout time.Duration ) ChainSyncOptionFunc {
151163 return func (c * Config ) {
152164 c .BlockTimeout = timeout
0 commit comments