Skip to content

Commit

Permalink
msggen: Map arrays of hashes and add HtlcState enum
Browse files Browse the repository at this point in the history
  • Loading branch information
cdecker committed May 3, 2023
1 parent b961e23 commit b11d90d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
13 changes: 13 additions & 0 deletions cln-grpc/proto/primitives.proto
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ enum ChannelState {
DualopendAwaitingLockin = 10;
}

enum HtlcState {
SentAddHtlc = 0;
SentAddCommit = 1;
RcvdAddRevocation = 2;
RcvdAddAckCommit = 3;
SentAddAckRevocation = 4;
RcvdRemoveHtlc = 5;
RcvdRemoveCommit = 6;
SentRemoveRevocation = 7;
SentRemoveAckCommit = 8;
RcvdRemoveAckRevocation = 9;
}

message ChannelStateChangeCause {}

message Outpoint {
Expand Down
35 changes: 35 additions & 0 deletions cln-rpc/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -289,6 +305,25 @@ impl TryFrom<i32> for ChannelState {
}
}

impl From<i32> 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<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down
5 changes: 4 additions & 1 deletion contrib/msggen/msggen/gen/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,10 @@ def generate_composite(self, prefix, field: CompositeField):
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:
Expand Down Expand Up @@ -423,7 +425,8 @@ def generate_composite(self, prefix, field: CompositeField) -> None:
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
Expand Down
4 changes: 2 additions & 2 deletions contrib/msggen/msggen/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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[]',
]
Expand All @@ -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:
Expand Down

0 comments on commit b11d90d

Please sign in to comment.