Skip to content

Commit

Permalink
cln+lnd: liveness check
Browse files Browse the repository at this point in the history
add a plugin hook in the case of core lightning and
an interceptor in the case of lnd, and pre-install
a dead/alive monitor for each daemon.
  • Loading branch information
YusukeShimizu committed Jun 11, 2024
1 parent 3d45569 commit 28b4c90
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 1 deletion.
23 changes: 23 additions & 0 deletions clightning/clightning.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func NewClightningClient(ctx context.Context) (*ClightningClient, <-chan interfa
cl.Plugin = glightning.NewPlugin(cl.onInit)
err := cl.Plugin.RegisterHooks(&glightning.Hooks{
CustomMsgReceived: cl.OnCustomMsg,
RpcCommand: cl.OnRPCCommand,
})
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -408,6 +409,28 @@ func (cl *ClightningClient) OnCustomMsg(event *glightning.CustomMsgReceivedEvent
return event.Continue(), nil
}

func (cl *ClightningClient) OnRPCCommand(event *glightning.RpcCommandEvent) (*glightning.RpcCommandResponse, error) {
if cl.gbitcoin != nil {
ok, err := cl.gbitcoin.Ping()
if err != nil {
return event.ReturnError(err.Error(), 0)
}
if !ok {
return event.ReturnError("failed to ping", 0)
}
}
if cl.liquidWallet != nil {
ok, err := cl.liquidWallet.Ping()
if err != nil {
return event.ReturnError(err.Error(), 0)
}
if !ok {
return event.ReturnError("failed to ping", 0)
}
}
return event.Continue(), nil
}

// AddMessageHandler adds a listener for incoming peermessages
func (cl *ClightningClient) AddMessageHandler(f func(peerId string, msgType string, payload []byte) error) {
cl.msgHandlers = append(cl.msgHandlers, f)
Expand Down
19 changes: 18 additions & 1 deletion cmd/peerswaplnd/peerswapd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,9 @@ func run() error {
}
defer lis.Close()

grpcSrv := grpc.NewServer()
grpcSrv := grpc.NewServer(
grpc.UnaryInterceptor(livenessCheckInterceptor(liquidCli)),
)

peerswaprpc.RegisterPeerSwapServer(grpcSrv, peerswaprpcServer)

Expand Down Expand Up @@ -439,6 +441,21 @@ func run() error {
return nil
}

func livenessCheckInterceptor(liquidCli *gelements.Elements) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
if liquidCli != nil {
ok, err := liquidCli.Ping()
if err != nil {
return nil, err
}
if !ok {
return nil, errors.New("liquid rpc not reachable")
}
}
return handler(ctx, req)
}
}

func getBitcoinChain(ctx context.Context, li lnrpc.LightningClient) (*chaincfg.Params, error) {
gi, err := li.GetInfo(ctx, &lnrpc.GetInfoRequest{})
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions electrum/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,7 @@ func (c *electrumClient) GetFee(ctx context.Context, target uint32) (float32, er
}
return c.client.GetFee(ctx, target)
}

func (c *electrumClient) Ping(ctx context.Context) error {
return c.reconnect(ctx)
}
1 change: 1 addition & 0 deletions electrum/electrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ type RPC interface {
GetRawTransaction(ctx context.Context, txHash string) (string, error)
BroadcastTransaction(ctx context.Context, rawTx string) (string, error)
GetFee(ctx context.Context, target uint32) (float32, error)
Ping(ctx context.Context) error
}
14 changes: 14 additions & 0 deletions electrum/mock/electrum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions lwk/lwkwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,10 @@ func (r *LWKRpcWallet) SetLabel(txID, address, label string) error {
// TODO: call set label
return nil
}

func (r *LWKRpcWallet) Ping() (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
defer cancel()
err := r.electrumClient.Ping(ctx)
return err == nil, err
}
5 changes: 5 additions & 0 deletions wallet/elementsrpcwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type RpcClient interface {
SendRawTx(txHex string) (string, error)
EstimateFee(blocks uint32, mode string) (*gelements.FeeResponse, error)
SetLabel(address, label string) error
Ping() (bool, error)
}

// ElementsRpcWallet uses the elementsd rpc wallet
Expand Down Expand Up @@ -191,3 +192,7 @@ func satsToAmountString(sats uint64) string {
bitcoinAmt := float64(sats) / 100000000
return fmt.Sprintf("%f", bitcoinAmt)
}

func (r *ElementsRpcWallet) Ping() (bool, error) {
return r.rpcClient.Ping()
}
1 change: 1 addition & 0 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ type Wallet interface {
SendRawTx(rawTx string) (txid string, err error)
GetFee(txSize int64) (uint64, error)
SetLabel(txID, address, label string) error
Ping() (bool, error)
}

0 comments on commit 28b4c90

Please sign in to comment.