@@ -14,6 +14,8 @@ type Client struct {
1414 busyMutex sync.Mutex
1515 intersectResultChan chan error
1616 readyForNextBlockChan chan bool
17+ wantCurrentTip bool
18+ currentTipChan chan Tip
1719}
1820
1921func NewClient (protoOptions protocol.ProtocolOptions , cfg * Config ) * Client {
@@ -29,6 +31,7 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
2931 config : cfg ,
3032 intersectResultChan : make (chan error ),
3133 readyForNextBlockChan : make (chan bool ),
34+ currentTipChan : make (chan Tip ),
3235 }
3336 protoConfig := protocol.ProtocolConfig {
3437 Name : PROTOCOL_NAME ,
@@ -75,6 +78,19 @@ func (c *Client) Stop() error {
7578 return nil
7679}
7780
81+ func (c * Client ) GetCurrentTip () (* Tip , error ) {
82+ c .busyMutex .Lock ()
83+ defer c .busyMutex .Unlock ()
84+ c .wantCurrentTip = true
85+ msg := NewMsgFindIntersect ([]common.Point {})
86+ if err := c .SendMessage (msg ); err != nil {
87+ return nil , err
88+ }
89+ tip := <- c .currentTipChan
90+ c .wantCurrentTip = false
91+ return & tip , nil
92+ }
93+
7894func (c * Client ) Sync (intersectPoints []common.Point ) error {
7995 c .busyMutex .Lock ()
8096 defer c .busyMutex .Unlock ()
@@ -178,11 +194,21 @@ func (c *Client) handleRollBackward(msgGeneric protocol.Message) error {
178194}
179195
180196func (c * Client ) handleIntersectFound (msgGeneric protocol.Message ) error {
181- c .intersectResultChan <- nil
197+ if c .wantCurrentTip {
198+ msgIntersectFound := msgGeneric .(* MsgIntersectFound )
199+ c .currentTipChan <- msgIntersectFound .Tip
200+ } else {
201+ c .intersectResultChan <- nil
202+ }
182203 return nil
183204}
184205
185206func (c * Client ) handleIntersectNotFound (msgGeneric protocol.Message ) error {
186- c .intersectResultChan <- IntersectNotFoundError {}
207+ if c .wantCurrentTip {
208+ msgIntersectNotFound := msgGeneric .(* MsgIntersectNotFound )
209+ c .currentTipChan <- msgIntersectNotFound .Tip
210+ } else {
211+ c .intersectResultChan <- IntersectNotFoundError {}
212+ }
187213 return nil
188214}
0 commit comments