lntest: refactor WaitForBlockchainSync

This commit is contained in:
yyforyongyu 2020-11-21 19:52:50 +08:00
parent a215c55186
commit 96c3a64316
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868

@ -1304,50 +1304,31 @@ func (hn *HarnessNode) WaitForNetworkChannelClose(ctx context.Context,
} }
} }
// WaitForBlockchainSync will block until the target nodes has fully // WaitForBlockchainSync waits for the target node to be fully synchronized with
// synchronized with the blockchain. If the passed context object has a set // the blockchain. If the passed context object has a set timeout, it will
// timeout, then the goroutine will continually poll until the timeout has // continually poll until the timeout has elapsed. In the case that the chain
// elapsed. In the case that the chain isn't synced before the timeout is up, // isn't synced before the timeout is up, this function will return an error.
// then this function will return an error.
func (hn *HarnessNode) WaitForBlockchainSync(ctx context.Context) error { func (hn *HarnessNode) WaitForBlockchainSync(ctx context.Context) error {
errChan := make(chan error, 1) ticker := time.NewTicker(time.Millisecond * 100)
retryDelay := time.Millisecond * 100 defer ticker.Stop()
go func() { for {
for { resp, err := hn.GetInfo(ctx, &lnrpc.GetInfoRequest{})
select { if err != nil {
case <-ctx.Done(): return err
case <-hn.quit: }
return if resp.SyncedToChain {
default: return nil
}
getInfoReq := &lnrpc.GetInfoRequest{}
getInfoResp, err := hn.GetInfo(ctx, getInfoReq)
if err != nil {
errChan <- err
return
}
if getInfoResp.SyncedToChain {
errChan <- nil
return
}
select {
case <-ctx.Done():
return
case <-time.After(retryDelay):
}
} }
}()
select { select {
case <-hn.quit: case <-ctx.Done():
return nil return fmt.Errorf("timeout while waiting for " +
case err := <-errChan: "blockchain sync")
return err case <-hn.quit:
case <-ctx.Done(): return nil
return fmt.Errorf("timeout while waiting for blockchain sync") case <-ticker.C:
}
} }
} }