1616package chainsync
1717
1818import (
19+ "sync"
1920 "time"
2021
2122 "github.com/blinklabs-io/gouroboros/connection"
@@ -44,8 +45,9 @@ var StateMap = protocol.StateMap{
4445 Agency : protocol .AgencyClient ,
4546 Transitions : []protocol.StateTransition {
4647 {
47- MsgType : MessageTypeRequestNext ,
48- NewState : stateCanAwait ,
48+ MsgType : MessageTypeRequestNext ,
49+ NewState : stateCanAwait ,
50+ MatchFunc : IncrementPipelineCount ,
4951 },
5052 {
5153 MsgType : MessageTypeFindIntersect ,
@@ -60,17 +62,34 @@ var StateMap = protocol.StateMap{
6062 stateCanAwait : protocol.StateMapEntry {
6163 Agency : protocol .AgencyServer ,
6264 Transitions : []protocol.StateTransition {
65+ {
66+ MsgType : MessageTypeRequestNext ,
67+ NewState : stateCanAwait ,
68+ MatchFunc : IncrementPipelineCount ,
69+ },
6370 {
6471 MsgType : MessageTypeAwaitReply ,
6572 NewState : stateMustReply ,
6673 },
6774 {
68- MsgType : MessageTypeRollForward ,
69- NewState : stateIdle ,
75+ MsgType : MessageTypeRollForward ,
76+ NewState : stateIdle ,
77+ MatchFunc : DecrementPipelineCountAndIsEmpty ,
7078 },
7179 {
72- MsgType : MessageTypeRollBackward ,
73- NewState : stateIdle ,
80+ MsgType : MessageTypeRollForward ,
81+ NewState : stateCanAwait ,
82+ MatchFunc : DecrementPipelineCountAndIsNotEmpty ,
83+ },
84+ {
85+ MsgType : MessageTypeRollBackward ,
86+ NewState : stateIdle ,
87+ MatchFunc : DecrementPipelineCountAndIsEmpty ,
88+ },
89+ {
90+ MsgType : MessageTypeRollBackward ,
91+ NewState : stateCanAwait ,
92+ MatchFunc : DecrementPipelineCountAndIsNotEmpty ,
7493 },
7594 },
7695 },
@@ -91,12 +110,24 @@ var StateMap = protocol.StateMap{
91110 Agency : protocol .AgencyServer ,
92111 Transitions : []protocol.StateTransition {
93112 {
94- MsgType : MessageTypeRollForward ,
95- NewState : stateIdle ,
113+ MsgType : MessageTypeRollForward ,
114+ NewState : stateIdle ,
115+ MatchFunc : DecrementPipelineCountAndIsEmpty ,
96116 },
97117 {
98- MsgType : MessageTypeRollBackward ,
99- NewState : stateIdle ,
118+ MsgType : MessageTypeRollForward ,
119+ NewState : stateCanAwait ,
120+ MatchFunc : DecrementPipelineCountAndIsNotEmpty ,
121+ },
122+ {
123+ MsgType : MessageTypeRollBackward ,
124+ NewState : stateIdle ,
125+ MatchFunc : DecrementPipelineCountAndIsEmpty ,
126+ },
127+ {
128+ MsgType : MessageTypeRollBackward ,
129+ NewState : stateCanAwait ,
130+ MatchFunc : DecrementPipelineCountAndIsNotEmpty ,
100131 },
101132 },
102133 },
@@ -105,6 +136,60 @@ var StateMap = protocol.StateMap{
105136 },
106137}
107138
139+ type StateContext struct {
140+ mu sync.Mutex
141+ pipelineCount int
142+ }
143+
144+ var IncrementPipelineCount = func (context interface {}, msg protocol.Message ) bool {
145+ s := context .(* StateContext )
146+ s .mu .Lock ()
147+ defer s .mu .Unlock ()
148+
149+ s .pipelineCount ++
150+ return true
151+ }
152+
153+ var DecrementPipelineCountAndIsEmpty = func (context interface {}, msg protocol.Message ) bool {
154+ s := context .(* StateContext )
155+ s .mu .Lock ()
156+ defer s .mu .Unlock ()
157+
158+ if s .pipelineCount == 1 {
159+ s .pipelineCount --
160+ return true
161+ }
162+ return false
163+ }
164+
165+ var DecrementPipelineCountAndIsNotEmpty = func (context interface {}, msg protocol.Message ) bool {
166+ s := context .(* StateContext )
167+ s .mu .Lock ()
168+ defer s .mu .Unlock ()
169+
170+ if s .pipelineCount > 1 {
171+ s .pipelineCount --
172+ return true
173+ }
174+ return false
175+ }
176+
177+ var PipelineIsEmtpy = func (context interface {}, msg protocol.Message ) bool {
178+ s := context .(* StateContext )
179+ s .mu .Lock ()
180+ defer s .mu .Unlock ()
181+
182+ return s .pipelineCount == 0
183+ }
184+
185+ var PipelineIsNotEmpty = func (context interface {}, msg protocol.Message ) bool {
186+ s := context .(* StateContext )
187+ s .mu .Lock ()
188+ defer s .mu .Unlock ()
189+
190+ return s .pipelineCount > 0
191+ }
192+
108193// ChainSync is a wrapper object that holds the client and server instances
109194type ChainSync struct {
110195 Client * Client
@@ -137,9 +222,11 @@ type RequestNextFunc func(CallbackContext) error
137222
138223// New returns a new ChainSync object
139224func New (protoOptions protocol.ProtocolOptions , cfg * Config ) * ChainSync {
225+ stateContext := & StateContext {}
226+
140227 c := & ChainSync {
141- Client : NewClient (protoOptions , cfg ),
142- Server : NewServer (protoOptions , cfg ),
228+ Client : NewClient (stateContext , protoOptions , cfg ),
229+ Server : NewServer (stateContext , protoOptions , cfg ),
143230 }
144231 return c
145232}
0 commit comments