Skip to content

Commit

Permalink
msggen: Make ChannelTypeName a native enum
Browse files Browse the repository at this point in the history
It fails because this is an array of enum values, which is a
combination we hadn't seen before. Replacing this with a manually
managed enum, since it is likely we'll want to reuse it in the future.
  • Loading branch information
cdecker authored and rustyrussell committed Jan 29, 2024
1 parent 0fd9680 commit 45b298f
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 12 deletions.
22 changes: 22 additions & 0 deletions .msggen.json
Original file line number Diff line number Diff line change
Expand Up @@ -736,9 +736,14 @@
"FetchInvoice.invoice": 1,
"FetchInvoice.next_period": 3
},
"FundchannelChannel_type": {
"FundChannel.channel_type.bits[]": 1,
"FundChannel.channel_type.names[]": 2
},
"FundchannelRequest": {
"FundChannel.amount": 1,
"FundChannel.announce": 3,
"FundChannel.channel_type[]": 14,
"FundChannel.close_to": 6,
"FundChannel.compact_lease": 8,
"FundChannel.feerate": 2,
Expand All @@ -753,6 +758,7 @@
},
"FundchannelResponse": {
"FundChannel.channel_id": 4,
"FundChannel.channel_type": 7,
"FundChannel.close_to": 5,
"FundChannel.mindepth": 6,
"FundChannel.outnum": 3,
Expand Down Expand Up @@ -3026,6 +3032,22 @@
"added": "pre-v0.10.1",
"deprecated": false
},
"FundChannel.channel_type": {
"added": "v24.02",
"deprecated": false
},
"FundChannel.channel_type.bits[]": {
"added": "v24.02",
"deprecated": false
},
"FundChannel.channel_type.names[]": {
"added": "v24.02",
"deprecated": false
},
"FundChannel.channel_type[]": {
"added": "v24.02",
"deprecated": false
},
"FundChannel.close_to": {
"added": "pre-v0.10.1",
"deprecated": false
Expand Down
9 changes: 9 additions & 0 deletions cln-grpc/proto/primitives.proto
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ message TlvEntry {
uint64 type = 1;
bytes value = 2;
}

message TlvStream {
repeated TlvEntry entries = 1;
}

enum ChannelTypeName {
static_remotekey_even = 0;
anchor_outputs_even = 1;
anchors_zero_fee_htlc_tx_even = 2;
scid_alias_even = 3;
zeroconf_even = 4;
}
85 changes: 74 additions & 11 deletions cln-rpc/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ pub enum HtlcState {
SENT_REMOVE_ACK_REVOCATION = 19,
}

#[derive(Copy, Clone, Serialize, Deserialize, Debug)]
#[allow(non_camel_case_types)]
pub enum ChannelTypeName {
#[serde(rename = "static_remotekey/even")]
STATIC_REMOTEKEY_EVEN = 0,
#[serde(rename = "anchor_outputs/even")]
ANCHOR_OUTPUTS_EVEN = 1,
#[serde(rename = "anchors_zero_fee_htlc_tx/even")]
ANCHORS_ZERO_FEE_HTLC_TX_EVEN = 2,
#[serde(rename = "scid_alias/even")]
SCID_ALIAS_EVEN = 3,
#[serde(rename = "zeroconf/even")]
ZEROCONF_EVEN = 4,
}

#[derive(Copy, Clone, Serialize, Deserialize, Debug)]
#[allow(non_camel_case_types)]
#[serde(rename_all = "lowercase")]
Expand Down Expand Up @@ -318,6 +333,31 @@ impl TryFrom<i32> for ChannelState {
}
}

impl From<i32> for ChannelTypeName {
fn from(value: i32) -> Self {
match value {
0 => ChannelTypeName::STATIC_REMOTEKEY_EVEN,
1 => ChannelTypeName::ANCHOR_OUTPUTS_EVEN,
2 => ChannelTypeName::ANCHORS_ZERO_FEE_HTLC_TX_EVEN,
3 => ChannelTypeName::SCID_ALIAS_EVEN,
4 => ChannelTypeName::ZEROCONF_EVEN,
o => panic!("Unmapped ChannelTypeName {}", o),
}
}
}

impl From<ChannelTypeName> for i32 {
fn from(value: ChannelTypeName) -> Self {
match value {
ChannelTypeName::STATIC_REMOTEKEY_EVEN => 0,
ChannelTypeName::ANCHOR_OUTPUTS_EVEN => 1,
ChannelTypeName::ANCHORS_ZERO_FEE_HTLC_TX_EVEN => 2,
ChannelTypeName::SCID_ALIAS_EVEN => 3,
ChannelTypeName::ZEROCONF_EVEN => 4,
}
}
}

impl From<i32> for HtlcState {
fn from(value: i32) -> Self {
match value {
Expand Down Expand Up @@ -470,13 +510,13 @@ impl TryFrom<&str> for Amount {

impl From<Amount> for String {
fn from(a: Amount) -> String {
// Best effort msat to sat conversion, for methods that accept
// sats but not msats
if a.msat % 1000 == 0 {
format!("{}sat", a.msat / 1000)
} else {
// Best effort msat to sat conversion, for methods that accept
// sats but not msats
if a.msat % 1000 == 0 {
format!("{}sat", a.msat / 1000)
} else {
format!("{}msat", a.msat)
}
}
}
}

Expand Down Expand Up @@ -553,6 +593,8 @@ impl Serialize for Feerate {

#[cfg(test)]
mod test {
use crate::model::responses::FundchannelResponse;

use super::*;

#[test]
Expand All @@ -564,11 +606,7 @@ mod test {

let tests = vec![
("{\"amount\": \"10msat\"}", Amount { msat: 10 }, "10msat"),
(
"{\"amount\": \"42sat\"}",
Amount { msat: 42_000 },
"42sat",
),
("{\"amount\": \"42sat\"}", Amount { msat: 42_000 }, "42sat"),
(
"{\"amount\": \"31337btc\"}",
Amount {
Expand Down Expand Up @@ -669,6 +707,31 @@ mod test {
let res = serde_json::to_string(&stream).unwrap();
assert_eq!(res, "{\"31337\":\"0102030405\",\"42\":\"\"}");
}

#[test]
fn test_fundchannel() {
let r = serde_json::json!({
"tx": "0000000000000000000000000000000000000000000000000000000000000000",
"txid": "0000000000000000000000000000000000000000000000000000000000000000",
"outnum": 0,
"channel_id": "0000000000000000000000000000000000000000000000000000000000000000",
"channel_type": {
"bits": [1, 3, 5],
"names": [
"static_remotekey/even",
"anchor_outputs/even",
"anchors_zero_fee_htlc_tx/even",
"scid_alias/even",
"zeroconf/even"
]
},
"close_to": "bc1qd23gerv2mn0qdecrmulsjsmkv8lz6t6m0770tg",
"mindepth": 1,
});

let p: FundchannelResponse = serde_json::from_value(r).unwrap();
assert_eq!(p.channel_type.unwrap().bits, Some(vec![1,3,5]));
}
}

#[derive(Clone, Debug, PartialEq)]
Expand Down
1 change: 1 addition & 0 deletions contrib/msggen/msggen/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class OverridePatch(Patch):
'ListPeers.peers[].channels[].htlcs[].state': "HtlcState",
'ListPeerChannels.channels[].htlcs[].state': "HtlcState",
'ListHtlcs.htlcs[].state': "HtlcState",
'FundChannel.channel_type.names[]': 'ChannelTypeName',
}

def visit(self, f: model.Field) -> None:
Expand Down
4 changes: 3 additions & 1 deletion contrib/pyln-grpc-proto/pyln/grpc/primitives_pb2.py

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

0 comments on commit 45b298f

Please sign in to comment.