Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swap: calculate required balance including fee #303

Merged
merged 3 commits into from
Jul 26, 2024
Merged

Conversation

YusukeShimizu
Copy link
Contributor

During swap-out, conduct a pre-swap check, including the payment of the fee invoice.

During swap-out, conduct a pre-swap check,
including the payment of the fee invoice.
@grubles
Copy link
Collaborator

grubles commented Jul 18, 2024

LND swap-out is canceled but the error peerswap prints is unclear:

$ ./builds/peerswap/out/pscli swapout --sat_amt 9001400 --channel_id 214039729557274624 --asset lbtc                                                                                                

2024/07/18 11:05:42 rpc error: code = Unknown desc = BuildRoute() rpc error: code = Unknown desc = no matching outgoing channel available for node 0 (035ca2fe4793a5e789ce846062eb4834f573c060d9200ce77544a29b48a0aa5923)

LND logs:

2024-07-18 11:05:36.273 [WRN] CRTR: ShortChannelID=194668:21:0: cannot add outgoing htlc: commitment transaction dips peer below chan reserve: our balance below chan reserve
2024-07-18 11:05:36.273 [ERR] CRTR: Cannot find policy with amt=9001400000 mSAT for node 035ca2fe4793a5e789ce846062eb4834f573c060d9200ce77544a29b48a0aa5923
2024-07-18 11:05:36.273 [ERR] RPCS: [/routerrpc.Router/BuildRoute]: no matching outgoing channel available for node 0 (035ca2fe4793a5e789ce846062eb4834f573c060d9200ce77544a29b48a0aa5923)
2024-07-18 11:05:36.791 [WRN] CRTR: ShortChannelID=194668:21:0: cannot add outgoing htlc: commitment transaction dips peer below chan reserve: our balance below chan reserve
2024-07-18 11:05:36.791 [ERR] CRTR: Cannot find policy with amt=9001400000 mSAT for node 035ca2fe4793a5e789ce846062eb4834f573c060d9200ce77544a29b48a0aa5923
2024-07-18 11:05:36.791 [ERR] RPCS: [/routerrpc.Router/BuildRoute]: no matching outgoing channel available for node 0 (035ca2fe4793a5e789ce846062eb4834f573c060d9200ce77544a29b48a0aa5923)
2024-07-18 11:05:37.678 [WRN] CRTR: ShortChannelID=194668:21:0: cannot add outgoing htlc: commitment transaction dips peer below chan reserve: our balance below chan reserve
2024-07-18 11:05:37.678 [ERR] CRTR: Cannot find policy with amt=9001400000 mSAT for node 035ca2fe4793a5e789ce846062eb4834f573c060d9200ce77544a29b48a0aa5923
2024-07-18 11:05:37.678 [ERR] RPCS: [/routerrpc.Router/BuildRoute]: no matching outgoing channel available for node 0 (035ca2fe4793a5e789ce846062eb4834f573c060d9200ce77544a29b48a0aa5923)
2024-07-18 11:05:38.478 [WRN] CRTR: ShortChannelID=194668:21:0: cannot add outgoing htlc: commitment transaction dips peer below chan reserve: our balance below chan reserve
2024-07-18 11:05:38.478 [ERR] CRTR: Cannot find policy with amt=9001400000 mSAT for node 035ca2fe4793a5e789ce846062eb4834f573c060d9200ce77544a29b48a0aa5923
2024-07-18 11:05:38.479 [ERR] RPCS: [/routerrpc.Router/BuildRoute]: no matching outgoing channel available for node 0 (035ca2fe4793a5e789ce846062eb4834f573c060d9200ce77544a29b48a0aa5923)
2024-07-18 11:05:39.489 [WRN] CRTR: ShortChannelID=194668:21:0: cannot add outgoing htlc: commitment transaction dips peer below chan reserve: our balance below chan reserve
2024-07-18 11:05:39.489 [ERR] CRTR: Cannot find policy with amt=9001400000 mSAT for node 035ca2fe4793a5e789ce846062eb4834f573c060d9200ce77544a29b48a0aa5923
2024-07-18 11:05:39.490 [ERR] RPCS: [/routerrpc.Router/BuildRoute]: no matching outgoing channel available for node 0 (035ca2fe4793a5e789ce846062eb4834f573c060d9200ce77544a29b48a0aa5923)
2024-07-18 11:05:42.432 [WRN] CRTR: ShortChannelID=194668:21:0: cannot add outgoing htlc: commitment transaction dips peer below chan reserve: our balance below chan reserve
2024-07-18 11:05:42.432 [ERR] CRTR: Cannot find policy with amt=9001400000 mSAT for node 035ca2fe4793a5e789ce846062eb4834f573c060d9200ce77544a29b48a0aa5923
2024-07-18 11:05:42.433 [ERR] RPCS: [/routerrpc.Router/BuildRoute]: no matching outgoing channel available for node 0 (035ca2fe4793a5e789ce846062eb4834f573c060d9200ce77544a29b48a0aa5923)

CLN swap-out is also canceled and the error message is more clear:

$ lightning-cli --signet peerswap-swap-out 194668x21x0 188986293 lbtc
{
   "code": -1,
   "message": "swap canceled, reason: not enough spendable msat: 188986294000, expected: 188986593000"
}

The prepayment probe, which includes the expected payment amount
for the fee invoice, is conducted at the start of the swapout.
The prepayment probe at this stage is inaccurate
and may cause unnecessary communication, so it will be removed.
Fixes the calculation of the SpendableMsat and ReceivableMsat.
@YusukeShimizu
Copy link
Contributor Author

Thank you.
There was an error in calculating the spendable amount in LND.

I confirmed that the behavior is as expected on our end.

./bin/lncli lnd1 listchannels | jq '.channels[] | {local_balance, local_chan_reserve_sat}'
{
  "local_balance": "99996530",
  "local_chan_reserve_sat": "1000000"
}
result=$(./bin/lncli lnd1 listchannels | jq -r '.channels[] | (.local_balance | tonumber) - (.local_chan_reserve_sat | tonumber)')
AMOUNT=$((result - 200))
./out/pscli swapout --channel_id 127543348887553 --sat_amt $AMOUNT --asset lbtc
2024/07/19 11:07:50 rpc error: code = Unknown desc = not enough spendable msat: 98996530000, expected: 98996630000

@grubles
Copy link
Collaborator

grubles commented Jul 22, 2024

It looks like my error slightly differs from yours?

Yours:

2024/07/19 11:07:50 rpc error: code = Unknown desc = not enough spendable msat: 98996530000, expected: 98996630000

Mine:

2024/07/22 09:53:08 rpc error: code = Unknown desc = exceeding spendable amount_msat: 6108700000

@YusukeShimizu
Copy link
Contributor Author

YusukeShimizu commented Jul 23, 2024

The cause is due to the difference in where the error is detected.
Your error is detected at the check stage of SwapOut start, where the fee invoice is not considered. This is because the amount of the fee invoice is unknown at this stage.
Therefore, it seems that the amount is insufficient even without including the fee invoice.
https://github.com/ElementsProject/peerswap/blob/pre-swapcheck-fee/swap/service.go#L390

My error passes the check at the SwapOut start stage and is detected at the PayFeeInvoiceAction stage, which is part of the recent changes.
At this stage, the fee invoice is also taken into account.
https://github.com/ElementsProject/peerswap/blob/pre-swapcheck-fee/swap/actions.go#L609

Copy link
Contributor

@nepet nepet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm!
ACK 76fa0cb

@@ -131,7 +131,7 @@ func (l *Client) SpendableMsat(scid string) (uint64, error) {
return 0, err
}
spendable := (uint64(ch.GetLocalBalance()) -
ch.GetLocalConstraints().GetChanReserveSat()*1000)
ch.GetLocalConstraints().GetChanReserveSat()) * 1000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

@grubles grubles merged commit e825b73 into master Jul 26, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants