forked from ElementsProject/peerswap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ElementsProject#206 from nepet/202306-add-sync-che…
…ck-for-elements Wait for Elementsd to be synced
- Loading branch information
Showing
3 changed files
with
69 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package elements | ||
|
||
import ( | ||
"strings" | ||
"time" | ||
|
||
"github.com/elementsproject/glightning/gelements" | ||
"github.com/elementsproject/peerswap/log" | ||
) | ||
|
||
type ElementsClientBuilder struct { | ||
} | ||
|
||
func NewClient(rpcUser, rpcPassword, rpcHost string, rpcPort uint) (*gelements.Elements, error) { | ||
c := gelements.NewElements(rpcUser, rpcPassword) | ||
|
||
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: %d", info.VerificationProgress) | ||
time.Sleep(time.Duration(backoff*10) * time.Second) | ||
backoff *= 2 | ||
continue | ||
} | ||
return c, nil | ||
} | ||
} |