Skip to content

Commit 42a816b

Browse files
committed
Add current height response in fetch block RPC
1 parent dc94e60 commit 42a816b

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

sqlchain/chain.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,10 @@ func (c *Chain) syncHead() (err error) {
626626
atomic.AddUint32(&succCount, 1)
627627
if resp.Block == nil {
628628
ile.Debug("fetch block request reply: no such block")
629+
// If block is nil, resp.Height returns the current head height of the remote peer
630+
if resp.Height <= req.Height {
631+
atomic.AddUint32(&initiatingCount, 1)
632+
}
629633
return
630634
}
631635

@@ -695,8 +699,8 @@ func (c *Chain) mainCycle(ctx context.Context) {
695699
if err := c.syncHead(); err != nil {
696700
if err != ErrInitiating {
697701
c.logEntry().WithError(err).Error("failed to sync head")
702+
continue
698703
}
699-
continue
700704
}
701705
if t, d := c.rt.nextTick(); d > 0 {
702706
time.Sleep(d)
@@ -1217,3 +1221,7 @@ func (c *Chain) updateMetrics() {
12171221

12181222
c.expVars.Get(mwMinerChainBlockTimestamp).(*expvar.String).Set(b.Timestamp().String())
12191223
}
1224+
1225+
func (c *Chain) getCurrentHeight() int32 {
1226+
return c.rt.getHead().Height
1227+
}

sqlchain/rpc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,8 @@ func (s *ChainRPCService) AdviseNewBlock(req *AdviseNewBlockReq, resp *AdviseNew
5757
func (s *ChainRPCService) FetchBlock(req *FetchBlockReq, resp *FetchBlockResp) (err error) {
5858
resp.Height = req.Height
5959
resp.Block, err = s.chain.FetchBlock(req.Height)
60+
if err == nil && resp.Block == nil {
61+
resp.Height = s.chain.getCurrentHeight()
62+
}
6063
return
6164
}

0 commit comments

Comments
 (0)