msggen: Map arrays of hashes and add HtlcState enum

This commit is contained in:
Christian Decker 2023-05-03 14:51:58 +02:00 committed by Rusty Russell
parent db843159ea
commit 0031f1160b
4 changed files with 55 additions and 3 deletions

View File

@ -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 {

View File

@ -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<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

View File

@ -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

View File

@ -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: