Skip to content

Commit 10d8936

Browse files
authored
block sync interval usage logic (#4751)
* improve block sync * height check before sending block sync request * set lastTipTime as block mint time
1 parent 4196cad commit 10d8936

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

blocksync/blocksync.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ type (
7878
syncStageHeight uint64
7979
syncBlockIncrease uint64
8080

81-
startingHeight uint64 // block number this node started to synchronise from
82-
lastTip uint64
83-
lastTipUpdateTime time.Time
84-
targetHeight uint64 // block number of the highest block header this node has received from peers
85-
mu sync.RWMutex
81+
startingHeight uint64 // block number this node started to synchronise from
82+
lastTip uint64
83+
lastTipTime time.Time
84+
targetHeight uint64 // block number of the highest block header this node has received from peers
85+
mu sync.RWMutex
8686
}
8787

8888
peerBlock struct {
@@ -144,7 +144,7 @@ func NewBlockSyncer(
144144
) (BlockSync, error) {
145145
bs := &blockSyncer{
146146
cfg: cfg,
147-
lastTipUpdateTime: time.Now(),
147+
lastTipTime: time.Time{},
148148
buf: newBlockBuffer(cfg.BufferSize, cfg.IntervalSize),
149149
tipHeightHandler: tipHeightHandler,
150150
blockByHeightHandler: blockByHeightHandler,
@@ -171,6 +171,10 @@ func (bs *blockSyncer) commitBlocks(blks []*peerBlock) bool {
171171
err := bs.commitBlockHandler(blk.block)
172172
switch errors.Cause(err) {
173173
case nil:
174+
if blk.block.Height() > bs.lastTip {
175+
bs.lastTip = blk.block.Height()
176+
bs.lastTipTime = blk.block.Timestamp()
177+
}
174178
return true
175179
case blockdao.ErrRemoteHeightTooLow:
176180
log.L().Info("remote height too low", zap.Uint64("height", blk.block.Height()))
@@ -188,7 +192,7 @@ func (bs *blockSyncer) flushInfo() (time.Time, uint64) {
188192
bs.mu.Lock()
189193
defer bs.mu.Unlock()
190194

191-
return bs.lastTipUpdateTime, bs.targetHeight
195+
return bs.lastTipTime, bs.targetHeight
192196
}
193197

194198
func (bs *blockSyncer) sync() {
@@ -307,10 +311,6 @@ func (bs *blockSyncer) ProcessBlock(ctx context.Context, peer string, blk *block
307311
syncedHeight++
308312
}
309313
log.L().Debug("flush blocks", zap.Uint64("start", tip), zap.Uint64("end", syncedHeight))
310-
if syncedHeight > bs.lastTip {
311-
bs.lastTip = syncedHeight
312-
bs.lastTipUpdateTime = time.Now()
313-
}
314314
return nil
315315
}
316316

0 commit comments

Comments
 (0)