Skip to content

Commit

Permalink
spenderp: add channel_type parameter to fundchannel / multifundchannel.
Browse files Browse the repository at this point in the history
And add a request schema for multifundchannel.

Changelog-Added: JSON-RPC: `fundchannel` and `multifundchannel` now take an optional `channel_type` parameter.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Jan 29, 2024
1 parent e749aeb commit 3c2a575
Show file tree
Hide file tree
Showing 18 changed files with 284 additions and 104 deletions.
1 change: 1 addition & 0 deletions cln-grpc/proto/node.proto

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

3 changes: 3 additions & 0 deletions cln-grpc/src/convert.rs

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

2 changes: 2 additions & 0 deletions cln-rpc/src/model.rs

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

13 changes: 13 additions & 0 deletions common/json_channel_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <common/json_channel_type.h>
#include <common/json_command.h>
#include <common/json_param.h>
#include <common/json_stream.h>

struct command_result *param_channel_type(struct command *cmd,
const char *name,
Expand Down Expand Up @@ -30,3 +31,15 @@ struct command_result *param_channel_type(struct command *cmd,
*ctype = channel_type_from(cmd, take(features));
return NULL;
}

void json_add_channel_type_arr(struct json_stream *response,
const char *fieldname,
const struct channel_type *ctype)
{
json_array_start(response, fieldname);
for (size_t i = 0; i < tal_bytelen(ctype->features)*8; i++) {
if (feature_is_set(ctype->features, i))
json_add_u32(response, NULL, i);
}
json_array_end(response);
}
5 changes: 5 additions & 0 deletions common/json_channel_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

struct command;
struct channel_type;
struct json_stream;

/* Parse [1,2] as a channel_type */
struct command_result *param_channel_type(struct command *cmd,
Expand All @@ -14,4 +15,8 @@ struct command_result *param_channel_type(struct command *cmd,
const jsmntok_t *tok,
struct channel_type **ctype);

/* Adds [1, 5]-style JSON array. */
void json_add_channel_type_arr(struct json_stream *response,
const char *fieldname,
const struct channel_type *ctype);
#endif /* LIGHTNING_COMMON_JSON_CHANNEL_TYPE_H */
4 changes: 3 additions & 1 deletion contrib/pyln-client/pyln/client/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,8 @@ def fundchannel(self, node_id, amount, feerate=None, announce=True,
minconf=None, utxos=None, push_msat=None, close_to=None,
request_amt=None, compact_lease=None,
mindepth: Optional[int] = None,
reserve: Optional[str] = None):
reserve: Optional[str] = None,
channel_type=None):
"""
Fund channel with {id} using {amount} satoshis with feerate
of {feerate} (uses default feerate if unset).
Expand All @@ -783,6 +784,7 @@ def fundchannel(self, node_id, amount, feerate=None, announce=True,
"compact_lease": compact_lease,
"mindepth": mindepth,
"reserve": reserve,
"channel_type": channel_type,
}
return self.call("fundchannel", payload)

Expand Down
196 changes: 98 additions & 98 deletions contrib/pyln-grpc-proto/pyln/grpc/node_pb2.py

Large diffs are not rendered by default.

22 changes: 20 additions & 2 deletions doc/lightning-fundchannel.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ SYNOPSIS

**fundchannel** *id* *amount* [*feerate*] [*announce*] [*minconf*]
[*utxos*] [*push\_msat*] [*close\_to*] [*request\_amt*] [*compact\_lease*]
[*reserve*]
[*reserve*] [*channel\_type*]

DESCRIPTION
-----------
Expand Down Expand Up @@ -72,7 +72,25 @@ Default is 1% of the funding amount. It can be a whole number, a whole number
ending in *sat*, a whole number ending in *000msat*, or a number with 1 to 8
decimal places ending in *btc*.


*channel\_type* *(added v24.02)* is an array of bit numbers, representing the explicit
channel type to request. There is currently no sanity checking on
this value so if you use strange values and your channel breaks, you
get to keep both pieces. BOLT 2 defines the following value types:

```
The currently defined basic types are:
- no features (no bits set)
- `option_static_remotekey` (bit 12)
- `option_anchor_outputs` and `option_static_remotekey` (bits 20 and 12)
- `option_anchors_zero_fee_htlc_tx` and `option_static_remotekey` (bits 22 and 12)
Each basic type has the following variations allowed:
- `option_scid_alias` (bit 46)
- `option_zeroconf` (bit 50)
```

EXAMPLE
-------

This example shows how to use lightning-cli to open new channel with peer 03f...fc1 from one whole utxo bcc1...39c:0
(you can use **listfunds** command to get txid and vout):
Expand Down
21 changes: 20 additions & 1 deletion doc/lightning-multifundchannel.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ lightning-multifundchannel -- Command for establishing many lightning channels
SYNOPSIS
--------

**multifundchannel** *destinations* [*feerate*] [*minconf*] [*utxos*] [*minchannels*] [*commitment\_feerate*]
**multifundchannel** *destinations* [*feerate*] [*minconf*] [*utxos*] [*minchannels*] [*commitment\_feerate*] [*channel\_type*]

DESCRIPTION
-----------
Expand Down Expand Up @@ -84,6 +84,25 @@ the funding process.
*commitment\_feerate* is the initial feerate for commitment and HTLC
transactions. See *feerate* for valid values.

*channel\_type* *(added v24.02)* is an array of bit numbers, representing the explicit
channel type to request. BOLT 2 defines the following value types:

```
*channel\_type* *(added v24.02)* is an array of bit numbers, representing the explicit
channel type to request. BOLT 2 defines the following value types:
```
The currently defined basic types are:
- no features (no bits set) `[]`
- `option_static_remotekey` (`[12]`)
- `option_anchor_outputs` and `option_static_remotekey` (`[20, 12]`)
- `option_anchors_zero_fee_htlc_tx` and `option_static_remotekey` ([22, 12])

Each basic type has the following variations allowed:
- `option_scid_alias` ([46])
- `option_zeroconf` ([50])
```
RETURN VALUE
------------
Expand Down
7 changes: 7 additions & 0 deletions doc/schemas/fundchannel.request.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
"reserve": {
"type": "msat",
"description": "The amount we want the peer to maintain on its side"
},
"channel_type": {
"added": "v24.02",
"type": "array",
"items": {
"type": "u32"
}
}
}
}
76 changes: 76 additions & 0 deletions doc/schemas/multifundchannel.request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"additionalProperties": false,
"required": [
"destinations"
],
"properties": {
"destinations": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"required": [
"id",
"amount"
],
"properties": {
"id": {
"type": "string"
},
"amount": {
"type": "msat_or_all"
},
"announce": {
"type": "boolean"
},
"push_msat": {
"type": "msat"
},
"close_to": {
"type": "string"
},
"request_amt": {
"type": "msat"
},
"compact_lease": {
"type": "string"
},
"mindepth": {
"description": "Number of confirmations required before we consider the channel active",
"type": "u32"
},
"reserve": {
"type": "msat",
"description": "The amount we want the peer to maintain on its side"
},
"channel_type": {
"type": "array",
"items": {
"type": "u32"
}
}
}
}
},
"feerate": {
"type": "feerate"
},
"minconf": {
"type": "u32"
},
"utxos": {
"type": "array",
"items": {
"type": "outpoint"
}
},
"minchannels": {
"type": "u32"
},
"commitment_feerate": {
"type": "feerate"
}
}
}
2 changes: 1 addition & 1 deletion plugins/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ plugins/bcli: $(PLUGIN_BCLI_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) $(JSM
plugins/keysend: wire/tlvstream.o wire/onion_wiregen.o $(PLUGIN_KEYSEND_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_PAY_LIB_OBJS) $(PLUGIN_COMMON_OBJS) $(JSMN_OBJS) common/gossmap.o common/fp16.o common/route.o common/dijkstra.o common/blindedpay.o common/blindedpath.o common/hmac.o common/blinding.o common/onion_encode.o common/gossmods_listpeerchannels.o
$(PLUGIN_KEYSEND_OBJS): $(PLUGIN_PAY_LIB_HEADER)

plugins/spenderp: bitcoin/block.o bitcoin/preimage.o bitcoin/psbt.o common/psbt_open.o wire/peer${EXP}_wiregen.o $(PLUGIN_SPENDER_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) $(JSMN_OBJS)
plugins/spenderp: bitcoin/block.o bitcoin/preimage.o bitcoin/psbt.o common/psbt_open.o common/json_channel_type.o common/channel_type.o common/features.o wire/peer${EXP}_wiregen.o $(PLUGIN_SPENDER_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) $(JSMN_OBJS)

plugins/offers: $(PLUGIN_OFFERS_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) common/addr.o common/bolt12.o common/bolt12_merkle.o common/bolt11_json.o common/iso4217.o $(WIRE_OBJS) $(WIRE_BOLT12_OBJS) bitcoin/block.o common/channel_id.o bitcoin/preimage.o common/blindedpath.o common/invoice_path_id.o common/blinding.o common/hmac.o $(JSMN_OBJS)

Expand Down
4 changes: 4 additions & 0 deletions plugins/spender/fundchannel.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ json_fundchannel(struct command *cmd,
const jsmntok_t *compact_lease;
const jsmntok_t *mindepth;
const jsmntok_t *reserve;
const jsmntok_t *channel_type;

struct out_req *req;

Expand All @@ -71,6 +72,7 @@ json_fundchannel(struct command *cmd,
p_opt("compact_lease", param_tok, &compact_lease),
p_opt("mindepth", param_tok, &mindepth),
p_opt("reserve", param_tok, &reserve),
p_opt("channel_type", param_tok, &channel_type),
NULL))
return command_param_failed();

Expand Down Expand Up @@ -103,6 +105,8 @@ json_fundchannel(struct command *cmd,

if (reserve)
json_add_tok(req->js, "reserve", reserve, buf);
if (channel_type)
json_add_tok(req->js, "channel_type", channel_type, buf);

json_object_end(req->js);
json_array_end(req->js);
Expand Down
8 changes: 8 additions & 0 deletions plugins/spender/multifundchannel.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <ccan/array_size/array_size.h>
#include <ccan/tal/str/str.h>
#include <common/addr.h>
#include <common/channel_type.h>
#include <common/json_channel_type.h>
#include <common/json_param.h>
#include <common/json_stream.h>
#include <common/memleak.h>
Expand Down Expand Up @@ -1142,6 +1144,11 @@ fundchannel_start_dest(struct multifundchannel_destination *dest)
if (dest->mindepth)
json_add_u32(req->js, "mindepth", *dest->mindepth);

if (dest->channel_type) {
json_add_channel_type_arr(req->js,
"channel_type", dest->channel_type);
}

if (dest->reserve)
json_add_string(
req->js, "reserve",
Expand Down Expand Up @@ -1861,6 +1868,7 @@ param_destinations_array(struct command *cmd, const char *name,
p_opt("compact_lease", param_lease_hex, &rates),
p_opt("mindepth", param_u32, &dest->mindepth),
p_opt("reserve", param_sat, &dest->reserve),
p_opt("channel_type", param_channel_type, &dest->channel_type),
NULL))
return command_param_failed();

Expand Down
3 changes: 3 additions & 0 deletions plugins/spender/multifundchannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ struct multifundchannel_destination {

/* Number of blocks to wait before sending `channel_ready`. */
u32 *mindepth;

/* Channel type to ask for (NULL if default OK) */
struct channel_type *channel_type;
};


Expand Down
5 changes: 5 additions & 0 deletions plugins/spender/openchannel.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <bitcoin/psbt.h>
#include <ccan/ccan/array_size/array_size.h>
#include <ccan/ccan/mem/mem.h>
#include <common/json_channel_type.h>
#include <common/json_stream.h>
#include <common/lease_rates.h>
#include <common/psbt_open.h>
Expand Down Expand Up @@ -1053,6 +1054,10 @@ openchannel_init_dest(struct multifundchannel_destination *dest)
lease_rates_tohex(tmpctx, dest->rates));
}

if (dest->channel_type) {
json_add_channel_type_arr(req->js,
"channel_type", dest->channel_type);
}
return send_outreq(cmd->plugin, req);
}

Expand Down
1 change: 1 addition & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2152,6 +2152,7 @@ def test_multifunding_param_failures(node_factory):
l1.rpc.multifundchannel([])

# Required destination fields missing.
l1.rpc.check_request_schemas = False
destinations = [{"id": '{}@localhost:{}'.format(l2.info['id'], l2.port),
"amount": 50000},
{"id": '{}@localhost:{}'.format(l3.info['id'], l3.port)}]
Expand Down
15 changes: 14 additions & 1 deletion tests/test_opening.py
Original file line number Diff line number Diff line change
Expand Up @@ -2585,7 +2585,7 @@ def test_fundchannel_utxo_too_small(bitcoind, node_factory):


@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_opening_explicit_channel_type(node_factory):
def test_opening_explicit_channel_type(node_factory, bitcoind):
plugin_path = Path(__file__).parent / "plugins" / "zeroconf-selective.py"
l1, l2, l3, l4 = node_factory.get_nodes(4,
opts=[{'experimental-dual-fund': None,
Expand Down Expand Up @@ -2635,3 +2635,16 @@ def test_opening_explicit_channel_type(node_factory):

with pytest.raises(RpcError, match=r'channel_type not supported'):
l1.rpc.openchannel_init(l3.info['id'], FUNDAMOUNT - 1000, psbt, channel_type=[STATIC_REMOTEKEY, ANCHORS_OLD])

l1.rpc.unreserveinputs(psbt)

# Works with fundchannel / multifundchannel
l1.rpc.fundchannel(l2.info['id'], FUNDAMOUNT // 3, channel_type=[STATIC_REMOTEKEY])
# FIXME: Check type is actually correct!

# Mine that so we can spend change.
bitcoind.generate_block(1, wait_for_mempool=1)
wait_for(lambda: len(l1.rpc.listfunds()['outputs']) == 1)

# FIXME: Check type is actually correct!
l1.rpc.fundchannel(l3.info['id'], FUNDAMOUNT // 3, channel_type=[STATIC_REMOTEKEY])

0 comments on commit 3c2a575

Please sign in to comment.