11package chainsync
22
33import (
4- "fmt"
5- "github.com/cloudstruct/go-cardano-ledger"
64 "github.com/cloudstruct/go-ouroboros-network/protocol"
75)
86
@@ -87,8 +85,8 @@ var StateMap = protocol.StateMap{
8785}
8886
8987type ChainSync struct {
90- * protocol. Protocol
91- config * Config
88+ Client * Client
89+ Server * Server
9290}
9391
9492type Config struct {
@@ -108,154 +106,10 @@ type IntersectFoundFunc func(interface{}, interface{}) error
108106type IntersectNotFoundFunc func (interface {}) error
109107type DoneFunc func () error
110108
111- func New (options protocol.ProtocolOptions , cfg * Config ) * ChainSync {
112- // Use node-to-client protocol ID
113- protocolId := PROTOCOL_ID_NTC
114- msgFromCborFunc := NewMsgFromCborNtC
115- if options .Mode == protocol .ProtocolModeNodeToNode {
116- // Use node-to-node protocol ID
117- protocolId = PROTOCOL_ID_NTN
118- msgFromCborFunc = NewMsgFromCborNtN
119- }
109+ func New (protoOptions protocol.ProtocolOptions , cfg * Config ) * ChainSync {
120110 c := & ChainSync {
121- config : cfg ,
122- }
123- protoConfig := protocol.ProtocolConfig {
124- Name : PROTOCOL_NAME ,
125- ProtocolId : protocolId ,
126- Muxer : options .Muxer ,
127- ErrorChan : options .ErrorChan ,
128- Mode : options .Mode ,
129- Role : options .Role ,
130- MessageHandlerFunc : c .messageHandler ,
131- MessageFromCborFunc : msgFromCborFunc ,
132- StateMap : StateMap ,
133- InitialState : STATE_IDLE ,
111+ Client : NewClient (protoOptions , cfg ),
112+ Server : NewServer (protoOptions , cfg ),
134113 }
135- c .Protocol = protocol .New (protoConfig )
136114 return c
137115}
138-
139- func (c * ChainSync ) Start () {
140- c .Protocol .Start ()
141- }
142-
143- func (c * ChainSync ) messageHandler (msg protocol.Message , isResponse bool ) error {
144- var err error
145- switch msg .Type () {
146- case MESSAGE_TYPE_AWAIT_REPLY :
147- err = c .handleAwaitReply ()
148- case MESSAGE_TYPE_ROLL_FORWARD :
149- err = c .handleRollForward (msg )
150- case MESSAGE_TYPE_ROLL_BACKWARD :
151- err = c .handleRollBackward (msg )
152- case MESSAGE_TYPE_INTERSECT_FOUND :
153- err = c .handleIntersectFound (msg )
154- case MESSAGE_TYPE_INTERSECT_NOT_FOUND :
155- err = c .handleIntersectNotFound (msg )
156- case MESSAGE_TYPE_DONE :
157- err = c .handleDone ()
158- default :
159- err = fmt .Errorf ("%s: received unexpected message type %d" , PROTOCOL_NAME , msg .Type ())
160- }
161- return err
162- }
163-
164- func (c * ChainSync ) RequestNext () error {
165- msg := NewMsgRequestNext ()
166- return c .SendMessage (msg )
167- }
168-
169- func (c * ChainSync ) FindIntersect (points []Point ) error {
170- msg := NewMsgFindIntersect (points )
171- return c .SendMessage (msg )
172- }
173-
174- func (c * ChainSync ) handleAwaitReply () error {
175- if c .config .AwaitReplyFunc == nil {
176- return fmt .Errorf ("received chain-sync AwaitReply message but no callback function is defined" )
177- }
178- // Call the user callback function
179- return c .config .AwaitReplyFunc ()
180- }
181-
182- func (c * ChainSync ) handleRollForward (msgGeneric protocol.Message ) error {
183- if c .config .RollForwardFunc == nil {
184- return fmt .Errorf ("received chain-sync RollForward message but no callback function is defined" )
185- }
186- if c .Mode () == protocol .ProtocolModeNodeToNode {
187- msg := msgGeneric .(* MsgRollForwardNtN )
188- var blockHeader interface {}
189- var blockType uint
190- blockEra := msg .WrappedHeader .Era
191- switch blockEra {
192- case ledger .BLOCK_HEADER_TYPE_BYRON :
193- blockType = msg .WrappedHeader .ByronType ()
194- var err error
195- blockHeader , err = ledger .NewBlockHeaderFromCbor (blockType , msg .WrappedHeader .HeaderCbor ())
196- if err != nil {
197- return err
198- }
199- default :
200- // Map block header types to block types
201- blockTypeMap := map [uint ]uint {
202- ledger .BLOCK_HEADER_TYPE_SHELLEY : ledger .BLOCK_TYPE_SHELLEY ,
203- ledger .BLOCK_HEADER_TYPE_ALLEGRA : ledger .BLOCK_TYPE_ALLEGRA ,
204- ledger .BLOCK_HEADER_TYPE_MARY : ledger .BLOCK_TYPE_MARY ,
205- ledger .BLOCK_HEADER_TYPE_ALONZO : ledger .BLOCK_TYPE_ALONZO ,
206- ledger .BLOCK_HEADER_TYPE_BABBAGE : ledger .BLOCK_TYPE_BABBAGE ,
207- }
208- blockType = blockTypeMap [blockEra ]
209- var err error
210- blockHeader , err = ledger .NewBlockHeaderFromCbor (blockType , msg .WrappedHeader .HeaderCbor ())
211- if err != nil {
212- return err
213- }
214- }
215- // Call the user callback function
216- return c .config .RollForwardFunc (blockType , blockHeader )
217- } else {
218- msg := msgGeneric .(* MsgRollForwardNtC )
219- blk , err := ledger .NewBlockFromCbor (msg .BlockType (), msg .BlockCbor ())
220- if err != nil {
221- return err
222- }
223- // Call the user callback function
224- return c .config .RollForwardFunc (msg .BlockType (), blk )
225- }
226- }
227-
228- func (c * ChainSync ) handleRollBackward (msgGeneric protocol.Message ) error {
229- if c .config .RollBackwardFunc == nil {
230- return fmt .Errorf ("received chain-sync RollBackward message but no callback function is defined" )
231- }
232- msg := msgGeneric .(* MsgRollBackward )
233- // Call the user callback function
234- return c .config .RollBackwardFunc (msg .Point , msg .Tip )
235- }
236-
237- func (c * ChainSync ) handleIntersectFound (msgGeneric protocol.Message ) error {
238- if c .config .IntersectFoundFunc == nil {
239- return fmt .Errorf ("received chain-sync IntersectFound message but no callback function is defined" )
240- }
241- msg := msgGeneric .(* MsgIntersectFound )
242- // Call the user callback function
243- return c .config .IntersectFoundFunc (msg .Point , msg .Tip )
244- }
245-
246- func (c * ChainSync ) handleIntersectNotFound (msgGeneric protocol.Message ) error {
247- if c .config .IntersectNotFoundFunc == nil {
248- return fmt .Errorf ("received chain-sync IntersectNotFound message but no callback function is defined" )
249- }
250- msg := msgGeneric .(* MsgIntersectNotFound )
251- // Call the user callback function
252- return c .config .IntersectNotFoundFunc (msg .Tip )
253- }
254-
255- func (c * ChainSync ) handleDone () error {
256- if c .config .DoneFunc == nil {
257- return fmt .Errorf ("received chain-sync Done message but no callback function is defined" )
258- }
259- // Call the user callback function
260- return c .config .DoneFunc ()
261- }
0 commit comments