Synchronize private channels from lnd to the channels table every hour

This commit is contained in:
Yaacov Akiba Slama
2021-03-19 13:47:36 +02:00
parent 76f51e2ba7
commit 8ca6ca87fd
3 changed files with 38 additions and 1 deletions

3
db.go
View File

@@ -74,7 +74,8 @@ func insertChannel(chanID uint64, channelPoint string, nodeID []byte) error {
_, err := pgxPool.Exec(context.Background(),
`INSERT INTO
channels (chanid, channel_point, nodeid)
VALUES ($1, $2, $3)`,
VALUES ($1, $2, $3)
ON CONFLICT (chanid) DO NOTHING`,
chanID, channelPoint, nodeID)
if err != nil {
return fmt.Errorf("insertChannel(%v, %s, %x) error: %w",

View File

@@ -2,6 +2,7 @@ package main
import (
"context"
"encoding/hex"
"fmt"
"log"
"os"
@@ -35,6 +36,40 @@ func (cfe *copyFromEvents) Err() error {
return cfe.err
}
func channelsSynchronize() {
for {
err := channelsSynchronizeOnce()
log.Printf("channelsSynchronizeOnce() err: %v", err)
time.Sleep(1 * time.Hour)
}
}
func channelsSynchronizeOnce() error {
log.Printf("channelsSynchronizeOnce - begin")
clientCtx := metadata.AppendToOutgoingContext(context.Background(), "macaroon", os.Getenv("LND_MACAROON_HEX"))
channels, err := client.ListChannels(clientCtx, &lnrpc.ListChannelsRequest{PrivateOnly: true})
if err != nil {
log.Printf("ListChannels error: %v", err)
return fmt.Errorf("client.ListChannels() error: %w", err)
}
log.Printf("channelsSynchronizeOnce - received channels")
for _, c := range channels.Channels {
nodeID, err := hex.DecodeString(c.RemotePubkey)
if err != nil {
log.Printf("hex.DecodeString in channelsSynchronizeOnce error: %v", err)
continue
}
err = insertChannel(c.ChanId, c.ChannelPoint, nodeID)
if err != nil {
log.Printf("insertChannel(%v, %v, %x) in channelsSynchronizeOnce error: %v", c.ChanId, c.ChannelPoint, nodeID, err)
continue
}
}
log.Printf("channelsSynchronizeOnce - done")
return nil
}
func forwardingHistorySynchronize() {
for {
err := forwardingHistorySynchronizeOnce()

View File

@@ -379,6 +379,7 @@ func main() {
go intercept()
go forwardingHistorySynchronize()
go channelsSynchronize()
s := grpc.NewServer(
grpc_middleware.WithUnaryServerChain(func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {