From 0031f1160bf479906195ceca3628b0c6c090380b Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 3 May 2023 14:51:58 +0200 Subject: [PATCH] msggen: Map arrays of hashes and add HtlcState enum --- cln-grpc/proto/primitives.proto | 14 +++++++++++++ cln-rpc/src/primitives.rs | 35 +++++++++++++++++++++++++++++++ contrib/msggen/msggen/gen/grpc.py | 5 ++++- contrib/msggen/msggen/patch.py | 4 ++-- 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/cln-grpc/proto/primitives.proto b/cln-grpc/proto/primitives.proto index 72959865f..e60999a13 100644 --- a/cln-grpc/proto/primitives.proto +++ b/cln-grpc/proto/primitives.proto @@ -38,6 +38,20 @@ enum ChannelState { DualopendAwaitingLockin = 10; } +enum HtlcState { + SentAddHtlc = 0; + SentAddCommit = 1; + RcvdAddRevocation = 2; + RcvdAddAckCommit = 3; + SentAddAckRevocation = 4; + RcvdAddAckRevocation = 5; + RcvdRemoveHtlc = 6; + RcvdRemoveCommit = 7; + SentRemoveRevocation = 8; + SentRemoveAckCommit = 9; + RcvdRemoveAckRevocation = 10; +} + message ChannelStateChangeCause {} message Outpoint { diff --git a/cln-rpc/src/primitives.rs b/cln-rpc/src/primitives.rs index 074a1c5d6..064c56208 100644 --- a/cln-rpc/src/primitives.rs +++ b/cln-rpc/src/primitives.rs @@ -26,6 +26,22 @@ pub enum ChannelState { DUALOPEND_AWAITING_LOCKIN = 10, } +#[derive(Copy, Clone, Serialize, Deserialize, Debug)] +#[allow(non_camel_case_types)] +pub enum HtlcState { + SENT_ADD_HTLC = 0, + SENT_ADD_COMMIT = 1, + RCVD_ADD_REVOCATION = 2, + RCVD_ADD_ACK_COMMIT = 3, + SENT_ADD_ACK_REVOCATION = 4, + RCVD_ADD_ACK_REVOCATION = 5, + RCVD_REMOVE_HTLC = 6, + RCVD_REMOVE_COMMIT = 7, + SENT_REMOVE_REVOCATION = 8, + SENT_REMOVE_ACK_COMMIT = 9, + RCVD_REMOVE_ACK_REVOCATION = 10, +} + #[derive(Copy, Clone, Serialize, Deserialize, Debug)] #[allow(non_camel_case_types)] #[serde(rename_all = "lowercase")] @@ -289,6 +305,25 @@ impl TryFrom for ChannelState { } } +impl From for HtlcState { + fn from(value: i32) -> Self { + match value { + 0 => HtlcState::SENT_ADD_HTLC, + 1 => HtlcState::SENT_ADD_COMMIT, + 2 => HtlcState::RCVD_ADD_REVOCATION, + 3 => HtlcState::RCVD_ADD_ACK_COMMIT, + 4 => HtlcState::SENT_ADD_ACK_REVOCATION, + 5 => HtlcState::RCVD_ADD_ACK_REVOCATION, + 6 => HtlcState::RCVD_REMOVE_HTLC, + 7 => HtlcState::RCVD_REMOVE_COMMIT, + 8 => HtlcState::SENT_REMOVE_REVOCATION, + 9 => HtlcState::SENT_REMOVE_ACK_COMMIT, + 10 => HtlcState::RCVD_REMOVE_ACK_REVOCATION, + n => panic!("Unmapped HtlcState variant: {}", n), + } + } +} + impl<'de> Deserialize<'de> for Amount { fn deserialize(deserializer: D) -> Result where diff --git a/contrib/msggen/msggen/gen/grpc.py b/contrib/msggen/msggen/gen/grpc.py index 2219097d1..f6466c54c 100644 --- a/contrib/msggen/msggen/gen/grpc.py +++ b/contrib/msggen/msggen/gen/grpc.py @@ -268,8 +268,10 @@ class GrpcConverterGenerator(IGenerator): mapping = { 'hex': f'hex::decode(i).unwrap()', 'secret': f'i.to_vec()', + 'hash': f'i.to_vec()', }.get(typ, f'i.into()') + self.write(f"// Field: {f.path}\n", numindent=3) if not f.optional: self.write(f"{name}: c.{name}.into_iter().map(|i| {mapping}).collect(), // Rule #3 for type {typ}\n", numindent=3) else: @@ -423,7 +425,8 @@ class GrpcUnconverterGenerator(GrpcConverterGenerator): mapping = { 'hex': f'hex::encode(s)', 'u32': f's', - 'secret': f's.try_into().unwrap()' + 'secret': f's.try_into().unwrap()', + 'hash': f'Sha256::from_slice(&s).unwrap()', }.get(typ, f's.into()') # TODO fix properly diff --git a/contrib/msggen/msggen/patch.py b/contrib/msggen/msggen/patch.py index fea5a1260..22d1fa8eb 100644 --- a/contrib/msggen/msggen/patch.py +++ b/contrib/msggen/msggen/patch.py @@ -154,9 +154,7 @@ class OverridePatch(Patch): 'ListClosedChannels.closedchannels[].channel_type', 'ListPeerChannels.channels[].channel_type', 'ListPeerChannels.channels[].features[]', - 'ListPeerChannels.channels[].htlcs[].state', 'ListPeerChannels.channels[].state_changes[]', - 'ListPeers.peers[].channels[].htlcs[].state', 'ListPeers.peers[].channels[].state_changes[]', 'ListTransactions.transactions[].type[]', ] @@ -176,6 +174,8 @@ class OverridePatch(Patch): 'ListPeers.peers[].channels[].state_changes[].cause': "ChannelStateChangeCause", 'ListPeers.peers[].channels[].state_changes[].old_state': "ChannelState", 'ListPeers.peers[].channels[].state_changes[].old_state': "ChannelState", + 'ListPeers.peers[].channels[].htlcs[].state': "HtlcState", + 'ListPeerChannels.channels[].htlcs[].state': "HtlcState", } def visit(self, f: model.Field) -> None: