Skip to content

Commit

Permalink
Sink upstream context into WaitTillHeight
Browse files Browse the repository at this point in the history
  • Loading branch information
devfans committed Sep 23, 2021
1 parent 21009fa commit db80454
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions chains/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package chains

import (
"context"
"fmt"
"strings"
"sync"
Expand All @@ -44,7 +45,7 @@ type SDK interface {

type Nodes interface {
Height() uint64
WaitTillHeight(uint64, time.Duration) uint64
WaitTillHeight(context.Context, uint64, time.Duration) (uint64, bool)
Available() bool
Node() SDK
}
Expand Down Expand Up @@ -77,7 +78,7 @@ func (s *ChainSDK) Height() uint64 {
return s.height
}

func (s *ChainSDK) WaitTillHeight(height uint64, interval time.Duration) uint64 {
func (s *ChainSDK) WaitTillHeight(ctx context.Context, height uint64, interval time.Duration) (uint64, bool) {
if interval == 0 {
interval = s.interval
}
Expand All @@ -86,9 +87,13 @@ func (s *ChainSDK) WaitTillHeight(height uint64, interval time.Duration) uint64
if err != nil {
log.Error("Failed to get chain latest height err ", "chain", s.ChainID, "err", err)
} else if h >= height {
return h
return h, true
}
select {
case <-ctx.Done():
return h, false
case <-time.After(interval):
}
time.Sleep(interval)
}
}

Expand Down

0 comments on commit db80454

Please sign in to comment.