Skip to content

Commit

Permalink
Merge pull request ElementsProject#183 from nepet/glightning-msat-purge
Browse files Browse the repository at this point in the history
Glightning msat purge
  • Loading branch information
nepet committed May 25, 2023
2 parents cbee898 + 5c6c534 commit 7a6e177
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 60 deletions.
19 changes: 13 additions & 6 deletions clightning/clightning.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type SendPayPartWaiter interface {
// ClightningClient is the main driver behind c-lightnings plugins system
// it handles rpc calls and messages
type ClightningClient struct {
version string
glightning *glightning.Lightning
Plugin *glightning.Plugin

Expand Down Expand Up @@ -97,6 +98,12 @@ type ClightningClient struct {
peerswapConfig PeerswapClightningConfig
}

// Version returns the version of the core-lightning node, as reported
// by `getinfo`.
func (cl *ClightningClient) Version() string {
return cl.version
}

func (cl *ClightningClient) SetReady() {
cl.isReady = true
}
Expand Down Expand Up @@ -207,7 +214,7 @@ func (cl *ClightningClient) CheckChannel(channelId string, amountSat uint64) err
return errors.New("fundingChannels not found")
}

if fundingChannels.ChannelSatoshi < amountSat {
if fundingChannels.OurAmountMilliSatoshi.MSat() < amountSat*1000 {
return errors.New("not enough outbound capacity to perform swapOut")
}
if !fundingChannels.Connected {
Expand Down Expand Up @@ -335,7 +342,7 @@ func (cl *ClightningClient) DecodePayreq(payreq string) (paymentHash string, amo
if err != nil {
return "", 0, 0, err
}
return res.PaymentHash, res.MilliSatoshis, int64(res.MinFinalCltvExpiry), nil
return res.PaymentHash, res.AmountMsat.MSat(), int64(res.MinFinalCltvExpiry), nil
}

// PayInvoice tries to pay a Bolt11 Invoice
Expand Down Expand Up @@ -367,15 +374,14 @@ func (cl *ClightningClient) PayInvoiceViaChannel(payreq string, scid string) (pr
{
Id: bolt11.Payee,
ShortChannelId: scid,
MilliSatoshi: bolt11.MilliSatoshis,
AmountMsat: fmt.Sprintf("%dmsat", bolt11.MilliSatoshis),
Delay: uint(bolt11.MinFinalCltvExpiry + 1),
AmountMsat: bolt11.AmountMsat,
Delay: uint32(bolt11.MinFinalCltvExpiry + 1),
Direction: 0,
},
},
bolt11.PaymentHash,
label,
nil,
bolt11.AmountMsat.MSat(),
payreq,
bolt11.PaymentSecret,
0,
Expand Down Expand Up @@ -428,6 +434,7 @@ func (cl *ClightningClient) onInit(plugin *glightning.Plugin, options map[string
log2.Fatalf("getinfo err %v", err)
}
cl.nodeId = getInfo.Id
cl.version = getInfo.Version
cl.initChan <- true
}

Expand Down
6 changes: 3 additions & 3 deletions clightning/clightning_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (l *SwapOut) Call() (jrpc2.Result, error) {
return nil, errors.New("fundingChannels not found")
}

if fundingChannels.ChannelSatoshi < (l.SatAmt + 5000) {
if fundingChannels.AmountMilliSatoshi.MSat() < (l.SatAmt+5000)*1000 {
return nil, errors.New("not enough outbound capacity to perform swapOut")
}
if !fundingChannels.Connected {
Expand Down Expand Up @@ -347,7 +347,7 @@ func (l *SwapIn) Call() (jrpc2.Result, error) {
if fundingChannels == nil {
return nil, errors.New("fundingChannels not found")
}
if fundingChannels.ChannelTotalSatoshi-fundingChannels.ChannelSatoshi < l.SatAmt {
if fundingChannels.AmountMilliSatoshi.MSat()-fundingChannels.OurAmountMilliSatoshi.MSat() < (l.SatAmt * 1000) {
return nil, errors.New("not enough inbound capacity to perform swap")
}
if !fundingChannels.Connected {
Expand Down Expand Up @@ -386,7 +386,7 @@ func (l *SwapIn) Call() (jrpc2.Result, error) {
}
sats := uint64(0)
for _, v := range funds.Outputs {
sats += v.Value
sats += v.AmountMilliSatoshi.MSat() / 1000
}

if sats < l.SatAmt+2000 {
Expand Down
18 changes: 17 additions & 1 deletion clightning/clightning_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/elementsproject/peerswap/lightning"
"github.com/elementsproject/peerswap/onchain"
"github.com/elementsproject/peerswap/swap"
"github.com/elementsproject/peerswap/version"
)

func (cl *ClightningClient) CreateOpeningTransaction(swapParams *swap.OpeningParams) (unpreparedTxHex string, fee uint64, vout uint32, err error) {
Expand All @@ -28,6 +29,21 @@ func (cl *ClightningClient) CreateOpeningTransaction(swapParams *swap.OpeningPar
return "", 0, 0, err
}

// Backwards compatibility layer. Since `v23.05`, `preparetx` returns a
// psbt v2 instead of v0. We still want to support `v23.02` so we skip the
// conversion of the psbt (from v2 to v0).
isV2, err := version.CompareVersionStrings(cl.Version(), "v23.05")
if err != nil {
return "", 0, 0, err
}
if isV2 {
res, err := cl.glightning.SetPSBTVersion(prepRes.Psbt, 0)
if err != nil {
return "", 0, 0, err
}
prepRes.Psbt = res.Psbt
}

fee, err = cl.bitcoinChain.GetFeeSatsFromTx(prepRes.Psbt, prepRes.UnsignedTx)
if err != nil {
return "", 0, 0, err
Expand Down Expand Up @@ -208,7 +224,7 @@ func (cl *ClightningClient) GetOnchainBalance() (uint64, error) {

var totalBalance uint64
for _, output := range funds.Outputs {
totalBalance += output.Value
totalBalance += output.AmountMilliSatoshi.MSat() / 1000
}
return totalBalance, nil
}
Expand Down
71 changes: 35 additions & 36 deletions clightning/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,33 @@ func PeerSwapFallback() Processor {
}
}

func SetBitcoinNetwork(client *ClightningClient) Processor {
return func(c *Config) (*Config, error) {
if c.Bitcoin.Network == "" {
// No network is set, we fetch it from cln.
// Set bitcoin network via getinfo return value
// Network could not be extracted, try `getinfo`.
info, err := client.glightning.GetInfo()
if err != nil {
return nil, err
}
// Hack to rewrite core-lightnings network names to
// the common internal variants.
switch info.Network {
case "bitcoin":
c.Bitcoin.Network = "mainnet"
case "testnet":
c.Bitcoin.Network = "testnet3"
case "":
return nil, fmt.Errorf("could not detect bitcoin network")
default:
c.Bitcoin.Network = info.Network
}
}
return c, nil
}
}

// BitcoinFallbackFromClnConfig
// if no bitcoin config is set at all, try to fall back to cln bitcoin config.
func BitcoinFallbackFromClnConfig(client *ClightningClient) Processor {
Expand Down Expand Up @@ -214,36 +241,7 @@ func BitcoinFallbackFromClnConfig(client *ClightningClient) Processor {
// Extract settings from the `bcli` plugin.
for _, plugin := range listConfigResponse.ImportantPlugins {
if plugin.Name == "bcli" {
// Extract network.
if v, ok := plugin.Options["network"]; ok {
if v != nil {
c.Bitcoin.Network = v.(string)
}
}
if _, ok := plugin.Options["mainnet"]; ok {
c.Bitcoin.Network = "mainnet"
}
if _, ok := plugin.Options["testnet"]; ok {
c.Bitcoin.Network = "testnet"
}
if _, ok := plugin.Options["signet"]; ok {
c.Bitcoin.Network = "signet"
}
if c.Bitcoin.Network == "" {
// Network could not be extracted, try `getinfo`.
info, err := client.glightning.GetInfo()
if err != nil {
return nil, err
}
c.Bitcoin.Network = info.Network
// Ugly hack to rewrite core-lightnings network name to
// the internal variant: bitcoin ~ mainnet.
if c.Bitcoin.Network == "bitcoin" {
c.Bitcoin.Network = "mainnet"
}
}

// Extract rest of the bitcoind config
// Extract the bitcoind config
if v, ok := plugin.Options["bitcoin-datadir"]; ok {
if v != nil {
c.Bitcoin.DataDir = v.(string)
Expand Down Expand Up @@ -403,6 +401,7 @@ func GetConfig(client *ClightningClient) (*Config, error) {
Add(ReadFromFile()).
Add(PeerSwapFallback()).
Add(BitcoinFallbackFromClnConfig(client)).
Add(SetBitcoinNetwork(client)).
Add(BitcoinFallback()).
Add(ElementsFallback()).
Add(BitcoinCookieConnect()).
Expand Down Expand Up @@ -438,7 +437,7 @@ func defaultBitcoinRpcPort(network string) uint {
switch network {
case "signet":
return 38332
case "testnet":
case "testnet", "testnet3":
return 18332
case "regtest":
return 18443
Expand All @@ -461,12 +460,12 @@ func defaultElementsRpcPort(network string) uint {

func bitcoinNetDir(network string) (string, error) {
switch network {
case "mainnet":
case "mainnet", "bitcoin":
return "", nil
case "signet":
return "signet", nil
case "testnet3":
return "testnet", nil
case "testnet3", "testnet":
return "testnet3", nil
case "regtest":
return "regtest", nil
default:
Expand All @@ -476,9 +475,9 @@ func bitcoinNetDir(network string) (string, error) {

func liquidNetDir(network string) (string, error) {
switch network {
case "mainnet":
case "mainnet", "bitcoin":
return "liquidv1", nil
case "testnet3", "simnet", "signet":
case "testnet3", "simnet", "signet", "testnet":
return "liquidtestnet", nil
case "regtest":
return "liquidregtest", nil
Expand Down
6 changes: 3 additions & 3 deletions cmd/peerswap-plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var supportedAssets = []string{}
var GitCommit string

const (
minClnVersion = "0.12.1"
minClnVersion = "23.02"
)

func main() {
Expand Down Expand Up @@ -141,8 +141,8 @@ func run(ctx context.Context, lightningPlugin *clightning.ClightningClient) erro
if err != nil {
return err
}
log.Infof("Using core-lightning version %s", nodeInfo.Version)
ok, err := checkClnVersion(nodeInfo.Network, nodeInfo.Version)
log.Infof("Using core-lightning version %s", lightningPlugin.Version())
ok, err := checkClnVersion(nodeInfo.Network, lightningPlugin.Version())
if err != nil {
log.Debugf("Could not compare version: %s", err.Error())
return err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,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-20221013194807-73978c84cee8
github.com/elementsproject/glightning v0.0.0-20230525134205-ef34d849f564
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
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,10 @@ github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dvyukov/go-fuzz v0.0.0-20220726122315-1d375ef9f9f6 h1:sE4tvxWw01v7K3MAHwKF2UF3xQbgy23PRURntuV1CkU=
github.com/dvyukov/go-fuzz v0.0.0-20220726122315-1d375ef9f9f6/go.mod h1:11Gm+ccJnvAhCNLlf5+cS9KjtbaD5I5zaZpFMsTHWTw=
github.com/elementsproject/glightning v0.0.0-20221013194807-73978c84cee8 h1:hGFieFW9zJavM+nQ9mKoPfPY5ag3L6JREAp0ED1W5wU=
github.com/elementsproject/glightning v0.0.0-20221013194807-73978c84cee8/go.mod h1:YAdIeSyx8VEhDCtEaGOJLmWNpPaQ3x4vYSAj9Vrppdo=
github.com/elementsproject/glightning v0.0.0-20230508201707-c2410b204731 h1:OAY0lNrZiCO1lwlaGiVH4htwrih1AAxxpuWRA9qQYwI=
github.com/elementsproject/glightning v0.0.0-20230508201707-c2410b204731/go.mod h1:YAdIeSyx8VEhDCtEaGOJLmWNpPaQ3x4vYSAj9Vrppdo=
github.com/elementsproject/glightning v0.0.0-20230525134205-ef34d849f564 h1:orSb6yU9D1Ow9epbRZJVAfGF6vC1HxaPqg9zLWga6Hw=
github.com/elementsproject/glightning v0.0.0-20230525134205-ef34d849f564/go.mod h1:YAdIeSyx8VEhDCtEaGOJLmWNpPaQ3x4vYSAj9Vrppdo=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down
16 changes: 8 additions & 8 deletions testframework/clightning.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ func (n *CLightningNode) GetBtcBalanceSat() (sats uint64, err error) {

var sum uint64
for _, output := range r.Outputs {
// Value seems to be already in sat.
sum += output.Value
// AmountMilliSatoshi is in msat.
sum += output.AmountMilliSatoshi.MSat() / 1000
}
return sum, nil
}
Expand All @@ -206,7 +206,7 @@ func (n *CLightningNode) GetChannelBalanceSat(scid string) (sats uint64, err err

for _, ch := range funds.Channels {
if ch.ShortChannelId == scid {
return ch.ChannelSatoshi, nil
return ch.OurAmountMilliSatoshi.MSat() / 1000, nil
}
}

Expand Down Expand Up @@ -463,15 +463,15 @@ func (n *CLightningNode) SendPay(bolt11, scid string) error {
{
Id: decodedBolt11.Payee,
ShortChannelId: scid,
MilliSatoshi: decodedBolt11.MilliSatoshis,
AmountMsat: decodedBolt11.AmountMsat,
Delay: uint(decodedBolt11.MinFinalCltvExpiry + 1),
Direction: 0,
// MilliSatoshi: decodedBolt11.MilliSatoshis,
AmountMsat: decodedBolt11.AmountMsat,
Delay: uint32(decodedBolt11.MinFinalCltvExpiry + 1),
Direction: 0,
},
},
decodedBolt11.PaymentHash,
"",
&decodedBolt11.MilliSatoshis,
decodedBolt11.AmountMsat.MSat(),
bolt11,
decodedBolt11.PaymentSecret,
0,
Expand Down

0 comments on commit 7a6e177

Please sign in to comment.