Skip to content

Commit

Permalink
clightning: config parsing deprecated plugins
Browse files Browse the repository at this point in the history
- Refactor the BitcoinFallbackFromClnConfig function to use the new
  `configs` structure instead of `important-plugins`.
- Improve error handling in `clnclnSetupWithConfig` by checking for
  "Exited with error" log.
- https://docs.corelightning.org/reference/lightning-listconfigs
  • Loading branch information
YusukeShimizu committed Dec 17, 2024
1 parent ecf1b6e commit a71e572
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 52 deletions.
67 changes: 19 additions & 48 deletions clightning/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -354,59 +353,31 @@ func BitcoinFallbackFromClnConfig(client *ClightningClient) Processor {
}

var listConfigResponse struct {
ImportantPlugins []*struct {
Path string
Name string
Options map[string]interface{}
} `json:"important-plugins"`
Configs map[string]struct {
ValueStr string `json:"value_str"`
ValueInt int `json:"value_int"`
} `json:"configs"`
}
err = json.Unmarshal(data, &listConfigResponse)
if err != nil {
return nil, err
}

// 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)
}
}
}
}
// Extract settings from the `configs` sub-object.
if v, ok := listConfigResponse.Configs["bitcoin-datadir"]; ok {
c.Bitcoin.DataDir = v.ValueStr
}
if v, ok := listConfigResponse.Configs["bitcoin-rpcuser"]; ok {
c.Bitcoin.RpcUser = v.ValueStr
}
if v, ok := listConfigResponse.Configs["bitcoin-rpcpassword"]; ok {
c.Bitcoin.RpcPassword = v.ValueStr
}
if v, ok := listConfigResponse.Configs["bitcoin-rpcconnect"]; ok {
c.Bitcoin.RpcHost = v.ValueStr
}
if v, ok := listConfigResponse.Configs["bitcoin-rpcport"]; ok {
c.Bitcoin.RpcPort = uint(v.ValueInt)
}
}
return c, nil
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ require (
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/sync v0.10.0 // indirect
golang.org/x/sys v0.1.0
golang.org/x/term v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,8 @@ golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0 h1:cu5kTvlzcw1Q5S9f5ip1/cpiB4nXvw1XYzFPGgzLUOY=
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
20 changes: 17 additions & 3 deletions test/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/elementsproject/peerswap/testframework"
"github.com/pelletier/go-toml/v2"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
)

const (
Expand Down Expand Up @@ -102,9 +103,22 @@ func clnclnSetupWithConfig(t *testing.T, fundAmt, pushAmt uint64,
if err != nil {
t.Fatalf("lightningd.Run() got err %v", err)
}
err = lightningd.WaitForLog("peerswap initialized", testframework.TIMEOUT)
if err != nil {
t.Fatalf("lightningd.WaitForLog() got err %v", err)
var g errgroup.Group

g.Go(func() error {
return lightningd.WaitForLog("peerswap initialized", testframework.TIMEOUT)
})

g.Go(func() error {
err := lightningd.WaitForLog("Exited with error", 30)
if err == nil {
return fmt.Errorf("lightningd exited with error")
}
return nil
})

if err := g.Wait(); err != nil {
t.Fatalf("WaitForLog() got err %v", err)
}
}

Expand Down

0 comments on commit a71e572

Please sign in to comment.