Skip to content

Commit f0f487c

Browse files
authored
restart the service if the height stays the same for one minute (#4623)
1 parent b0cbb5f commit f0f487c

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

chainservice/chainservice.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package chainservice
77

88
import (
99
"context"
10+
"sync/atomic"
11+
"time"
1012

1113
"github.com/libp2p/go-libp2p/core/peer"
1214
"github.com/pkg/errors"
@@ -79,11 +81,37 @@ type ChainService struct {
7981
apiStats *nodestats.APILocalStats
8082
actionsync *actsync.ActionSync
8183
minter *factory.Minter
84+
85+
lastReceivedBlockHeight uint64
8286
}
8387

8488
// Start starts the server
8589
func (cs *ChainService) Start(ctx context.Context) error {
86-
return cs.lifecycle.OnStartSequentially(ctx)
90+
if err := cs.lifecycle.OnStartSequentially(ctx); err != nil {
91+
return errors.Wrap(err, "failed to start chain service")
92+
}
93+
go func() {
94+
ticker := time.NewTicker(time.Minute)
95+
defer ticker.Stop()
96+
97+
var lastHeight uint64
98+
var lastReceivedBlockHeight uint64
99+
for {
100+
select {
101+
case <-ctx.Done():
102+
return
103+
case <-ticker.C:
104+
currentHeight := cs.chain.TipHeight()
105+
lrbh := atomic.LoadUint64(&cs.lastReceivedBlockHeight)
106+
if currentHeight == lastHeight && lastReceivedBlockHeight != lrbh {
107+
log.L().Panic("Blockchain height has not changed, restarting service")
108+
}
109+
lastHeight = currentHeight
110+
lastReceivedBlockHeight = lrbh
111+
}
112+
}
113+
}()
114+
return nil
87115
}
88116

89117
// Stop stops the server
@@ -150,6 +178,9 @@ func (cs *ChainService) HandleBlock(ctx context.Context, peer string, pbBlock *i
150178
if err != nil {
151179
return err
152180
}
181+
if atomic.LoadUint64(&cs.lastReceivedBlockHeight) < blk.Height() {
182+
atomic.StoreUint64(&cs.lastReceivedBlockHeight, blk.Height())
183+
}
153184
return cs.blocksync.ProcessBlock(ctx, peer, blk)
154185
}
155186

0 commit comments

Comments
 (0)