@@ -16,7 +16,10 @@ package chainsync
1616
1717import (
1818 "fmt"
19+
20+ "github.com/blinklabs-io/gouroboros/ledger"
1921 "github.com/blinklabs-io/gouroboros/protocol"
22+ "github.com/blinklabs-io/gouroboros/protocol/common"
2023)
2124
2225// Server implements the ChainSync server
@@ -54,6 +57,31 @@ func NewServer(protoOptions protocol.ProtocolOptions, cfg *Config) *Server {
5457 return s
5558}
5659
60+ func (s * Server ) RollBackward (point common.Point , tip Tip ) error {
61+ msg := NewMsgRollBackward (point , tip )
62+ return s .SendMessage (msg )
63+ }
64+
65+ func (s * Server ) RollForward (blockType uint , blockData []byte , tip Tip ) error {
66+ if s .Mode () == protocol .ProtocolModeNodeToNode {
67+ eraId := ledger .BlockToBlockHeaderTypeMap [blockType ]
68+ msg := NewMsgRollForwardNtN (
69+ eraId ,
70+ 0 ,
71+ blockData ,
72+ tip ,
73+ )
74+ return s .SendMessage (msg )
75+ } else {
76+ msg := NewMsgRollForwardNtC (
77+ blockType ,
78+ blockData ,
79+ tip ,
80+ )
81+ return s .SendMessage (msg )
82+ }
83+ }
84+
5785func (s * Server ) messageHandler (msg protocol.Message ) error {
5886 var err error
5987 switch msg .Type () {
@@ -74,12 +102,34 @@ func (s *Server) messageHandler(msg protocol.Message) error {
74102}
75103
76104func (s * Server ) handleRequestNext (msg protocol.Message ) error {
77- // TODO
78- return nil
105+ if s .config == nil || s .config .RequestNextFunc == nil {
106+ return fmt .Errorf (
107+ "received chain-sync RequestNext message but no callback function is defined" ,
108+ )
109+ }
110+ return s .config .RequestNextFunc ()
79111}
80112
81113func (s * Server ) handleFindIntersect (msg protocol.Message ) error {
82- // TODO
114+ if s .config == nil || s .config .FindIntersectFunc == nil {
115+ return fmt .Errorf (
116+ "received chain-sync FindIntersect message but no callback function is defined" ,
117+ )
118+ }
119+ msgFindIntersect := msg .(* MsgFindIntersect )
120+ point , tip , err := s .config .FindIntersectFunc (msgFindIntersect .Points )
121+ if err != nil {
122+ if err == IntersectNotFoundError {
123+ msgResp := NewMsgIntersectNotFound (tip )
124+ if err := s .SendMessage (msgResp ); err != nil {
125+ return err
126+ }
127+ }
128+ }
129+ msgResp := NewMsgIntersectFound (point , tip )
130+ if err := s .SendMessage (msgResp ); err != nil {
131+ return err
132+ }
83133 return nil
84134}
85135
0 commit comments