Skip to content

Commit 806c9bd

Browse files
authored
Merge pull request #140 from cloudstruct/feat/chainsync-current-tip
feat: support for getting chain tip via chain-sync
2 parents a244b79 + 3e19237 commit 806c9bd

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

cmd/go-ouroboros-network/chainsync.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ var syncState chainSyncState
2525
type chainSyncFlags struct {
2626
flagset *flag.FlagSet
2727
startEra string
28+
tip bool
2829
}
2930

3031
func newChainSyncFlags() *chainSyncFlags {
3132
f := &chainSyncFlags{
3233
flagset: flag.NewFlagSet("chain-sync", flag.ExitOnError),
3334
}
3435
f.flagset.StringVar(&f.startEra, "start-era", "genesis", "era which to start chain-sync at")
36+
f.flagset.BoolVar(&f.tip, "tip", false, "start chain-sync at current chain tip")
3537
return f
3638
}
3739

@@ -135,7 +137,14 @@ func testChainSync(f *globalFlags) {
135137
syncState.oConn = o
136138
syncState.nodeToNode = f.ntnProto
137139
var point common.Point
138-
if len(eraIntersect[f.networkMagic][chainSyncFlags.startEra]) > 0 {
140+
if chainSyncFlags.tip {
141+
tip, err := o.ChainSync.Client.GetCurrentTip()
142+
if err != nil {
143+
fmt.Printf("ERROR: failed to get current tip: %s\n", err)
144+
os.Exit(1)
145+
}
146+
point = tip.Point
147+
} else if len(eraIntersect[f.networkMagic][chainSyncFlags.startEra]) > 0 {
139148
// Slot
140149
slot := uint64(eraIntersect[f.networkMagic][chainSyncFlags.startEra][0].(int))
141150
// Block hash

protocol/chainsync/client.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1921
func 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+
7894
func (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

180196
func (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

185206
func (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

Comments
 (0)