@@ -45,6 +45,7 @@ type Config struct {
4545 GravityChainAPIs []string `yaml:"gravityChainAPIs"`
4646 GravityChainHeightInterval uint64 `yaml:"gravityChainHeightInterval"`
4747 GravityChainStartHeight uint64 `yaml:"gravityChainStartHeight"`
48+ GravityChainCeilingHeight uint64 `yaml:"gravityChainCeilingHeight"`
4849 RegisterContractAddress string `yaml:"registerContractAddress"`
4950 StakingContractAddress string `yaml:"stakingContractAddress"`
5051 PaginationSize uint8 `yaml:"paginationSize"`
@@ -114,8 +115,10 @@ type (
114115 currentHeight uint64
115116 lastUpdateTimestamp int64
116117 terminate chan bool
118+ terminated bool
117119 mutex sync.RWMutex
118120 gravityChainBatchSize uint64
121+ ceilingHeight uint64
119122 }
120123
121124 rawData struct {
@@ -183,7 +186,9 @@ func NewCommittee(archive PollArchive, cfg Config) (Committee, error) {
183186 scoreThreshold : scoreThreshold ,
184187 selfStakingThreshold : selfStakingThreshold ,
185188 terminate : make (chan bool ),
189+ terminated : false ,
186190 startHeight : cfg .GravityChainStartHeight ,
191+ ceilingHeight : cfg .GravityChainCeilingHeight ,
187192 interval : cfg .GravityChainHeightInterval ,
188193 currentHeight : 0 ,
189194 gravityChainBatchSize : gravityChainBatchSize ,
@@ -227,9 +232,15 @@ func (ec *committee) Start(ctx context.Context) (err error) {
227232 for {
228233 select {
229234 case <- ec .terminate :
230- ec .terminate <- true
231235 return
232236 case tip := <- tipChan :
237+ if ec .currentHeight >= ec .ceilingHeight {
238+ if err := ec .Stop (context .Background ()); err != nil {
239+ zap .L ().Error ("failed to stop eth committee" , zap .Error (err ))
240+ }
241+ return
242+ }
243+
233244 zap .L ().Info ("new ethereum block" , zap .Uint64 ("height" , tip ))
234245 if err := ec .Sync (tip ); err != nil {
235246 zap .L ().Error ("failed to sync" , zap .Error (err ))
@@ -245,7 +256,12 @@ func (ec *committee) Start(ctx context.Context) (err error) {
245256func (ec * committee ) Stop (ctx context.Context ) error {
246257 ec .mutex .Lock ()
247258 defer ec .mutex .Unlock ()
248- ec .terminate <- true
259+
260+ defer func () { ec .terminated = true }()
261+ if ec .terminated {
262+ return nil
263+ }
264+ close (ec .terminate )
249265 ec .carrier .Close ()
250266
251267 return ec .archive .Stop (ctx )
0 commit comments