Skip to content

Commit 689dd28

Browse files
committed
funding: enable push_msat
it's that time of year (merry xmas!) enables the ability to push_msat on fundchannel Changelog-Added: RPC: `fundchannel` and `fundchannel_start` can now accept an optional parameter, `push_msat`, which will gift that amount of satoshis to the peer at channel open.
1 parent 051b5bd commit 689dd28

File tree

8 files changed

+75
-8
lines changed

8 files changed

+75
-8
lines changed

contrib/pyln-client/pyln/client/lightning.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,14 +548,15 @@ def fundchannel(self, node_id, *args, **kwargs):
548548
if 'satoshi' in kwargs:
549549
return self._deprecated_fundchannel(node_id, *args, **kwargs)
550550

551-
def _fundchannel(node_id, amount, feerate=None, announce=True, minconf=None, utxos=None):
551+
def _fundchannel(node_id, amount, feerate=None, announce=True, minconf=None, utxos=None, push_msat=None):
552552
payload = {
553553
"id": node_id,
554554
"amount": amount,
555555
"feerate": feerate,
556556
"announce": announce,
557557
"minconf": minconf,
558-
"utxos": utxos
558+
"utxos": utxos,
559+
"push_msat": push_msat
559560
}
560561
return self.call("fundchannel", payload)
561562

doc/lightning-fundchannel.7

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/lightning-fundchannel.7.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ SYNOPSIS
55
--------
66

77
**fundchannel** *id* *amount* \[*feerate* *announce*\] \[*minconf*\]
8-
\[*utxos*\]
8+
\[*utxos*\] \[*push_msat*\]
99

1010
DESCRIPTION
1111
-----------
@@ -49,6 +49,11 @@ outputs should have. Default is 1.
4949
*utxos* specifies the utxos to be used to fund the channel, as an array
5050
of "txid:vout".
5151

52+
*push_msat* is the amount of millisatoshis to push to the channel peer at
53+
open. Note that this is a gift to the peer -- these satoshis are
54+
added to the initial balance of the peer at channel start and are largely
55+
unrecoverable once pushed.
56+
5257
RETURN VALUE
5358
------------
5459

doc/lightning-fundchannel_start.7

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/lightning-fundchannel_start.7.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ lightning-fundchannel\_start -- Command for initiating channel establishment for
44
SYNOPSIS
55
--------
66

7-
**fundchannel\_start** *id* *amount* \[*feerate* *announce* *close_to*\]
7+
**fundchannel\_start** *id* *amount* \[*feerate* *announce* *close_to* *push_msat*\]
88

99
DESCRIPTION
1010
-----------
@@ -27,6 +27,11 @@ commitment transactions: see **fundchannel**.
2727
on close. Only valid if both peers have negotiated `option_upfront_shutdown_script`.
2828
Returns `close_to` set to closing script iff is negotiated.
2929

30+
*push_msat* is the amount of millisatoshis to push to the channel peer at
31+
open. Note that this is a gift to the peer -- these satoshis are
32+
added to the initial balance of the peer at channel start and are largely
33+
unrecoverable once pushed.
34+
3035
Note that the funding transaction MUST NOT be broadcast until after
3136
channel establishment has been successfully completed by running
3237
`fundchannel_complete`, as the commitment transactions for this channel

lightningd/opening_control.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,7 @@ static struct command_result *json_fund_channel_start(struct command *cmd,
11051105

11061106
u8 *msg = NULL;
11071107
struct amount_sat *amount;
1108+
struct amount_msat *push_msat;
11081109

11091110
fc->cmd = cmd;
11101111
fc->cancels = tal_arr(fc, struct command *, 0);
@@ -1119,6 +1120,7 @@ static struct command_result *json_fund_channel_start(struct command *cmd,
11191120
p_opt("feerate", param_feerate, &feerate_per_kw),
11201121
p_opt_def("announce", param_bool, &announce_channel, true),
11211122
p_opt("close_to", param_bitcoin_address, &fc->our_upfront_shutdown_script),
1123+
p_opt("push_msat", param_msat, &push_msat),
11221124
NULL))
11231125
return command_param_failed();
11241126
} else {
@@ -1132,6 +1134,7 @@ static struct command_result *json_fund_channel_start(struct command *cmd,
11321134
p_opt("satoshi", param_sat, &satoshi),
11331135
p_opt("feerate", param_feerate, &feerate_per_kw),
11341136
p_opt_def("announce", param_bool, &announce_channel, true),
1137+
p_opt("push_msat", param_msat, &push_msat),
11351138
NULL))
11361139
return command_param_failed();
11371140

@@ -1153,6 +1156,13 @@ static struct command_result *json_fund_channel_start(struct command *cmd,
11531156
type_to_string(tmpctx, struct amount_sat,
11541157
&chainparams->max_funding));
11551158

1159+
if (push_msat && amount_msat_greater_sat(*push_msat, *amount))
1160+
return command_fail(cmd, FUND_CANNOT_AFFORD,
1161+
"Requested to push_msat of %s is greater than "
1162+
"available funding amount %s",
1163+
type_to_string(tmpctx, struct amount_msat, push_msat),
1164+
type_to_string(tmpctx, struct amount_sat, amount));
1165+
11561166
fc->funding = *amount;
11571167
if (!feerate_per_kw) {
11581168
feerate_per_kw = tal(cmd, u32);
@@ -1192,8 +1202,7 @@ static struct command_result *json_fund_channel_start(struct command *cmd,
11921202
return command_fail(cmd, LIGHTNINGD, "Already funding channel");
11931203
}
11941204

1195-
/* FIXME: Support push_msat? */
1196-
fc->push = AMOUNT_MSAT(0);
1205+
fc->push = push_msat ? *push_msat : AMOUNT_MSAT(0);
11971206
fc->channel_flags = OUR_CHANNEL_FLAGS;
11981207
if (!*announce_channel) {
11991208
fc->channel_flags &= ~CHANNEL_FLAGS_ANNOUNCE_CHANNEL;

plugins/fundchannel.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct funding_req {
2222
const char *funding_str;
2323
const char *utxo_str;
2424
bool funding_all;
25+
struct amount_msat *push_msat;
2526

2627
bool *announce_channel;
2728
u32 *minconf;
@@ -322,6 +323,9 @@ static struct command_result *fundchannel_start(struct command *cmd,
322323
json_out_addstr(ret, "feerate", fr->feerate_str);
323324
if (fr->announce_channel)
324325
json_out_addbool(ret, "announce", *fr->announce_channel);
326+
if (fr->push_msat)
327+
json_out_addstr(ret, "push_msat",
328+
type_to_string(tmpctx, struct amount_msat, fr->push_msat));
325329

326330
json_out_end(ret, '}');
327331
json_out_finished(ret);
@@ -450,6 +454,7 @@ static struct command_result *json_fundchannel(struct command *cmd,
450454
p_opt_def("announce", param_bool, &fr->announce_channel, true),
451455
p_opt_def("minconf", param_number, &fr->minconf, 1),
452456
p_opt("utxos", param_string, &fr->utxo_str),
457+
p_opt("push_msat", param_msat, &fr->push_msat),
453458
NULL))
454459
return command_param_failed();
455460
} else {
@@ -462,6 +467,7 @@ static struct command_result *json_fundchannel(struct command *cmd,
462467
p_opt_def("announce", param_bool, &fr->announce_channel, true),
463468
p_opt_def("minconf", param_number, &fr->minconf, 1),
464469
p_opt("utxos", param_string, &fr->utxo_str),
470+
p_opt("push_msat", param_msat, &fr->push_msat),
465471
NULL))
466472
return command_param_failed();
467473

tests/test_connection.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,35 @@ def test_funding_toolarge(node_factory, bitcoind):
911911
l1.rpc.fundchannel(l2.info['id'], amount)
912912

913913

914+
def test_funding_push(node_factory, bitcoind):
915+
""" Try to push peer some sats """
916+
l1 = node_factory.get_node()
917+
l2 = node_factory.get_node()
918+
919+
l1.rpc.connect(l2.info['id'], 'localhost', l2.port)
920+
921+
# Send funds.
922+
amount = 2**24
923+
push_sat = 20000
924+
bitcoind.rpc.sendtoaddress(l1.rpc.newaddr()['bech32'], amount / 10**8 + 0.01)
925+
bitcoind.generate_block(1)
926+
927+
# Wait for it to arrive.
928+
wait_for(lambda: len(l1.rpc.listfunds()['outputs']) > 0)
929+
930+
# Fail to open (try to push too much)
931+
with pytest.raises(RpcError, match=r'Requested to push_msat of 20000000msat is greater than available funding amount 10000sat'):
932+
l1.rpc.fundchannel(l2.info['id'], 10000, push_msat=push_sat * 1000)
933+
934+
# This should work.
935+
amount = amount - 1
936+
l1.rpc.fundchannel(l2.info['id'], amount, push_msat=push_sat * 1000)
937+
bitcoind.generate_block(1)
938+
sync_blockheight(bitcoind, [l1])
939+
funds = only_one(l1.rpc.listfunds()['channels'])
940+
assert funds['channel_sat'] + push_sat == funds['channel_total_sat']
941+
942+
914943
def test_funding_by_utxos(node_factory, bitcoind):
915944
"""Fund a channel with specific utxos"""
916945
l1, l2, l3 = node_factory.line_graph(3, fundchannel=False)

0 commit comments

Comments
 (0)