Skip to content

Commit

Permalink
Merge pull request ElementsProject#206 from nepet/202306-add-sync-che…
Browse files Browse the repository at this point in the history
…ck-for-elements

Wait for Elementsd to be synced
  • Loading branch information
wtogami committed Jul 14, 2023
2 parents ab93669 + a5581d7 commit 21471ae
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 21 deletions.
19 changes: 8 additions & 11 deletions cmd/peerswap-plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"time"

"github.com/elementsproject/peerswap/elements"
"github.com/elementsproject/peerswap/isdev"
"github.com/elementsproject/peerswap/log"
"github.com/elementsproject/peerswap/version"
Expand Down Expand Up @@ -176,7 +177,13 @@ func run(ctx context.Context, lightningPlugin *clightning.ClightningClient) erro
config.Liquid.RpcPort,
config.Liquid.RpcHost,
)
liquidCli, err = getElementsClient(lightningPlugin.GetLightningRpc(), config)
// This call is blocking, waiting for elements to come alive and sync.
liquidCli, err = elements.NewClient(
config.Liquid.RpcUser,
config.Liquid.RpcPassword,
config.Liquid.RpcHost,
config.Liquid.RpcPort,
)
if err != nil {
return err
}
Expand Down Expand Up @@ -420,16 +427,6 @@ func getBitcoinChain(li *glightning.Lightning) (*chaincfg.Params, error) {
}
}

func getElementsClient(li *glightning.Lightning, config *clightning.Config) (*gelements.Elements, error) {
elementsCli := gelements.NewElements(config.Liquid.RpcUser, config.Liquid.RpcPassword)
err := elementsCli.StartUp(config.Liquid.RpcHost, config.Liquid.RpcPort)
if err != nil {
log.Infof("Could not connect to elements: %s", err.Error())
return nil, err
}

return elementsCli, nil
}
func getBitcoinClient(li *glightning.Lightning, pluginConfig *clightning.Config) (*gbitcoin.Bitcoin, error) {
rpcUser := pluginConfig.Bitcoin.RpcUser
rpcPassword := pluginConfig.Bitcoin.RpcPassword
Expand Down
20 changes: 10 additions & 10 deletions cmd/peerswaplnd/peerswapd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"syscall"
"time"

"github.com/elementsproject/peerswap/elements"
"github.com/elementsproject/peerswap/isdev"
"github.com/elementsproject/peerswap/lnd"
"github.com/elementsproject/peerswap/log"
Expand Down Expand Up @@ -197,20 +198,19 @@ func run() error {
if cfg.LiquidEnabled {
supportedAssets = append(supportedAssets, "lbtc")
log.Infof("Liquid swaps enabled")
// blockchaincli
liquidConfig := cfg.ElementsConfig
liquidCli = gelements.NewElements(liquidConfig.RpcUser, liquidConfig.RpcPassword)
err = liquidCli.StartUp(liquidConfig.RpcHost, liquidConfig.RpcPort)
if err != nil {
return err
}
// Wallet
liquidWalletCli := gelements.NewElements(liquidConfig.RpcUser, liquidConfig.RpcPassword)
err = liquidWalletCli.StartUp(liquidConfig.RpcHost, liquidConfig.RpcPort)

// This call is blocking, waiting for elements to come alive and sync.
liquidCli, err = elements.NewClient(
liquidConfig.RpcUser,
liquidConfig.RpcPassword,
liquidConfig.RpcHost,
liquidConfig.RpcPort,
)
if err != nil {
return err
}
liquidRpcWallet, err = wallet.NewRpcWallet(liquidWalletCli, liquidConfig.RpcWallet)
liquidRpcWallet, err = wallet.NewRpcWallet(liquidCli, liquidConfig.RpcWallet)
if err != nil {
return err
}
Expand Down
51 changes: 51 additions & 0 deletions elements/client.go
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
}
}

0 comments on commit 21471ae

Please sign in to comment.