forked from ElementsProject/peerswap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
51 lines (45 loc) · 1.24 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package elements
import (
"strings"
"time"
"github.com/elementsproject/glightning/gelements"
"github.com/elementsproject/peerswap/log"
)
type ElementsClientBuilder struct {
}
func NewClient(rpcUser, rpcPassword, rpcHost, RpcPasswordFile string, rpcPort uint) (*gelements.Elements, error) {
c := gelements.NewElements(rpcUser, rpcPassword, RpcPasswordFile)
var backoff int64 = 1
for {
err := c.StartUp(rpcHost, rpcPort)
if err != nil {
log.Infof("Could not connect to elements: %s", err.Error())
// Check if error starts with -28 indicating, that we can not connect
// to elementsd as it is starting up and not ready yet.
if strings.HasPrefix(strings.TrimSpace(err.Error()), "-28") {
// wait a bit and try again.
time.Sleep(time.Duration(backoff*10) * time.Second)
backoff *= 2
continue
}
// Other errors fail.
return nil, err
}
break
}
backoff = 1
for {
info, err := c.GetChainInfo()
if err != nil {
return nil, err
}
if info.VerificationProgress < 1. {
// Waiting for block verification to catch up.
log.Infof("Elementsd still syncing, progress: %f", info.VerificationProgress)
time.Sleep(time.Duration(backoff*10) * time.Second)
backoff *= 2
continue
}
return c, nil
}
}