Skip to content

Commit

Permalink
Merge pull request #334 from ElementsProject/bump-nix
Browse files Browse the repository at this point in the history
nix: nixpkgs input to the latest revision
  • Loading branch information
grubles authored Dec 19, 2024
2 parents 4a17a06 + b7e205c commit 9d41480
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 107 deletions.
13 changes: 11 additions & 2 deletions clightning/clightning.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/elementsproject/peerswap/poll"
"github.com/elementsproject/peerswap/swap"
"github.com/elementsproject/peerswap/wallet"
goerrors "github.com/go-errors/errors"
"github.com/samber/lo"
)

var methods = []peerswaprpcMethod{
Expand Down Expand Up @@ -378,9 +380,14 @@ func (cl *ClightningClient) Start() error {
// SendMessage sends a hexmessage to a peer
func (cl *ClightningClient) SendMessage(peerId string, message []byte, messageType int) error {
msg := messages.MessageTypeToHexString(messages.MessageType(messageType)) + hex.EncodeToString(message)
if peerId == "" {
ierr := goerrors.New("failed to send custom message: peerId is empty")
log.Debugf("SendMessage: %v", ierr)
return ierr
}
res, err := cl.glightning.SendCustomMessage(peerId, msg)
if err != nil {
return err
return goerrors.Errorf("failed to send custom message: %v", err)
}
if res.Code != 0 {
return errors.New(res.Message)
Expand Down Expand Up @@ -541,7 +548,9 @@ func (cl *ClightningClient) OnConnect(connectEvent *glightning.ConnectEvent) {
for {
time.Sleep(10 * time.Second)
if cl.pollService != nil {
cl.pollService.RequestPoll(connectEvent.PeerId)
cl.pollService.RequestPoll(
lo.Ternary(connectEvent.PeerId != "",
connectEvent.PeerId, connectEvent.Conn.PeerId))
return
}
}
Expand Down
177 changes: 114 additions & 63 deletions clightning/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,85 +334,136 @@ func SetBitcoinNetwork(client *ClightningClient) Processor {
}
}

// Structs for parsed CLN configs
// ParsedClnConfig represents the configuration for cln (Core Lightning).
// It includes fields for versions before and after v24.11.
// For versions before v24.11, ImportantPlugins holds plugin configurations.
// For versions v24.11 and later, Configs contains detailed config settings.
type ParsedClnConfig struct {
ImportantPlugins []*PluginConfig `json:"important-plugins"`
Configs map[string]ConfigEntry `json:"configs"`
}

type PluginConfig struct {
Path string `json:"path"`
Name string `json:"name"`
Options map[string]interface{} `json:"options"`
}

type ConfigEntry struct {
ValueStr string `json:"value_str"`
ValueInt int `json:"value_int"`
}

// BitcoinFallbackFromClnConfig
// if no bitcoin config is set at all, try to fall back to cln bitcoin config.
// If no bitcoin config is set at all, try to fall back to CLN bitcoin config.
func BitcoinFallbackFromClnConfig(client *ClightningClient) Processor {
return func(c *Config) (*Config, error) {
if c.Bitcoin.RpcUser == "" && c.Bitcoin.RpcPassword == "" &&
c.Bitcoin.RpcPasswordFile == "" && c.Bitcoin.RpcHost == "" &&
c.Bitcoin.RpcPort == 0 {
// No bitcoin config is set, we try to fetch it from CLN.
if isBitcoinConfigEmpty(c.Bitcoin) {
conf, err := client.glightning.ListConfigs()
if err != nil {
return nil, err
}

// Parse interface data into struct.
data, err := json.Marshal(conf)
parsedConfig, err := parseClnConfig(conf)
if err != nil {
return nil, err
}
updateBitcoinConfig(c, parsedConfig)
}
return c, nil
}
}

var listConfigResponse struct {
ImportantPlugins []*struct {
Path string
Name string
Options map[string]interface{}
} `json:"important-plugins"`
}
err = json.Unmarshal(data, &listConfigResponse)
if err != nil {
return nil, err
}
func isBitcoinConfigEmpty(bitcoin *BitcoinConf) bool {
return bitcoin.RpcUser == "" && bitcoin.RpcPassword == "" &&
bitcoin.RpcPasswordFile == "" && bitcoin.RpcHost == "" && bitcoin.RpcPort == 0
}

// Extract settings from the `bcli` plugin.
for _, plugin := range listConfigResponse.ImportantPlugins {
if plugin.Name == "bcli" {
// Extract the bitcoind config
if v, ok := plugin.Options["bitcoin-datadir"]; ok {
if v != nil {
c.Bitcoin.DataDir = v.(string)
}
}
if v, ok := plugin.Options["bitcoin-rpcuser"]; ok {
if v != nil {
c.Bitcoin.RpcUser = v.(string)
}
}
if v, ok := plugin.Options["bitcoin-rpcpassword"]; ok {
if v != nil {
c.Bitcoin.RpcPassword = v.(string)
}
}
if v, ok := plugin.Options["bitcoin-rpcconnect"]; ok {
if v != nil {
c.Bitcoin.RpcHost = v.(string)
}
}
if v, ok := plugin.Options["bitcoin-rpcport"]; ok {
if v != nil {
// detect if type is string (CLN < v23.08)
switch p := v.(type) {
case string:
port, err := strconv.Atoi(p)
if err != nil {
return nil, err
}
c.Bitcoin.RpcPort = uint(port)
case float64:
c.Bitcoin.RpcPort = uint(p)
default:
return nil, fmt.Errorf("Bitcoind rpcport type %T not handled", v)
}
}
}
}
}
func parseClnConfig(conf interface{}) (*ParsedClnConfig, error) {
data, err := json.Marshal(conf)
if err != nil {
return nil, err
}

var parsedConfig ParsedClnConfig
if err := json.Unmarshal(data, &parsedConfig); err != nil {
return nil, err
}

return &parsedConfig, nil
}

func updateBitcoinConfig(c *Config, parsedConfig *ParsedClnConfig) {
if parsedConfig.Configs != nil {
applyConfigMap(c, parsedConfig.Configs)
}
applyPluginConfig(c, parsedConfig.ImportantPlugins)
}

func applyConfigMap(c *Config, configs map[string]ConfigEntry) {
if v, ok := configs["bitcoin-datadir"]; ok {
c.Bitcoin.DataDir = v.ValueStr
}
if v, ok := configs["bitcoin-rpcuser"]; ok {
c.Bitcoin.RpcUser = v.ValueStr
}
if v, ok := configs["bitcoin-rpcpassword"]; ok {
c.Bitcoin.RpcPassword = v.ValueStr
}
if v, ok := configs["bitcoin-rpcconnect"]; ok {
c.Bitcoin.RpcHost = v.ValueStr
}
if v, ok := configs["bitcoin-rpcport"]; ok {
c.Bitcoin.RpcPort = uint(v.ValueInt)
}
}

func applyPluginConfig(c *Config, plugins []*PluginConfig) {
for _, plugin := range plugins {
if plugin.Name == "bcli" {
applyBcliOptions(c, plugin.Options)
}
return c, nil
}
}

func applyBcliOptions(c *Config, options map[string]interface{}) {
if v, ok := options["bitcoin-datadir"]; ok {
c.Bitcoin.DataDir = toString(v)
}
if v, ok := options["bitcoin-rpcuser"]; ok {
c.Bitcoin.RpcUser = toString(v)
}
if v, ok := options["bitcoin-rpcpassword"]; ok {
c.Bitcoin.RpcPassword = toString(v)
}
if v, ok := options["bitcoin-rpcconnect"]; ok {
c.Bitcoin.RpcHost = toString(v)
}
if v, ok := options["bitcoin-rpcport"]; ok {
c.Bitcoin.RpcPort = toUint(v)
}
}

func toString(v interface{}) string {
if str, ok := v.(string); ok {
return str
}
return ""
}

func toUint(v interface{}) uint {
switch val := v.(type) {
case string:
port, err := strconv.Atoi(val)
if err == nil {
return uint(port)
}
case float64:
return uint(val)
}
return 0
}

// BitcoinFallback sets default values for empty config options.
func BitcoinFallback() Processor {
return func(c *Config) (*Config, error) {
Expand Down
8 changes: 4 additions & 4 deletions flake.lock

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

8 changes: 4 additions & 4 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
inputs = {
# Pinning to revision 6915a163f351c32bd4557518d047725665e83d37
# - cln v24.08.02
# Pinning to revision 4f0dadbf38ee4cf4cc38cbc232b7708fddf965bc
# - cln v24.11
# - lnd v0.18.3-beta
# - bitcoin v28.0
# - elements v23.2.1
nixpkgs.url = "github:NixOS/nixpkgs/6915a163f351c32bd4557518d047725665e83d37";
# - elements v23.2.4
nixpkgs.url = "github:NixOS/nixpkgs/4f0dadbf38ee4cf4cc38cbc232b7708fddf965bc";
flake-utils.url = "github:numtide/flake-utils";
# blockstream-electrs: init at 0.4.1 #299761
# https://github.com/NixOS/nixpkgs/pull/299761/commits/680d27ad847801af781e0a99e4b87ed73965c69a
Expand Down
21 changes: 11 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/btcsuite/btcd/btcutil v1.1.2
github.com/btcsuite/btcd/btcutil/psbt v1.1.5
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1
github.com/elementsproject/glightning v0.0.0-20241120003711-1fdc5d319d74
github.com/elementsproject/glightning v0.0.0-20241218232330-5d69c660a74c
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3
github.com/jessevdk/go-flags v1.5.0
Expand Down Expand Up @@ -53,7 +53,7 @@ require (
github.com/dvyukov/go-fuzz v0.0.0-20220726122315-1d375ef9f9f6 // indirect
github.com/fergusstrange/embedded-postgres v1.19.0 // indirect
github.com/form3tech-oss/jwt-go v3.2.5+incompatible // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-errors/errors v1.4.2
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-macaroon-bakery/macaroonpb v1.0.0 // indirect
Expand Down Expand Up @@ -123,15 +123,15 @@ require (
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.23.0
golang.org/x/crypto v0.0.0-20221010152910-d6f0a8c073c2 // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/net v0.1.0 // indirect
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0 // indirect
golang.org/x/sys v0.1.0
golang.org/x/term v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.10.0
golang.org/x/sys v0.20.0
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af // indirect
golang.org/x/tools v0.2.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/genproto v0.0.0-20221010155953-15ba04fc1c0e // indirect
gopkg.in/errgo.v1 v1.0.1 // indirect
gopkg.in/macaroon-bakery.v2 v2.3.0 // indirect
Expand All @@ -156,6 +156,7 @@ require (
github.com/lightningnetwork/lnd/queue v1.1.0 // indirect
github.com/lightningnetwork/lnd/ticker v1.1.0 // indirect
github.com/lightningnetwork/lnd/tlv v1.0.3 // indirect
github.com/samber/lo v1.47.0
)

// This fork contains some options we need to reconnect to lnd.
Expand Down
Loading

0 comments on commit 9d41480

Please sign in to comment.