Skip to content

Commit

Permalink
msggen: Map the extratlvs field of keysend
Browse files Browse the repository at this point in the history
Changelog-Added: cln-rpc: `keysend` now exposes the `extratlvs` field
  • Loading branch information
cdecker authored and Christian Decker committed Oct 27, 2022
1 parent e12d5aa commit 356bab6
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 133 deletions.
4 changes: 1 addition & 3 deletions cln-grpc/proto/node.proto

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

11 changes: 10 additions & 1 deletion cln-grpc/proto/primitives.proto
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,13 @@ message Routehint {
}
message RoutehintList {
repeated Routehint hints = 2;
}
}


message TlvEntry {
uint64 type = 1;
bytes value = 2;
}
message TlvStream {
repeated TlvEntry entries = 1;
}
1 change: 1 addition & 0 deletions cln-grpc/src/convert.rs

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

20 changes: 20 additions & 0 deletions cln-grpc/src/pb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,40 @@ impl From<RouteHop> for cln_rpc::primitives::Routehop {
}
}
}

impl From<Routehint> for cln_rpc::primitives::Routehint {
fn from(c: Routehint) -> Self {
Self {
hops: c.hops.into_iter().map(|h| h.into()).collect(),
}
}
}

impl From<RoutehintList> for cln_rpc::primitives::RoutehintList {
fn from(c: RoutehintList) -> Self {
Self {
hints: c.hints.into_iter().map(|h| h.into()).collect(),
}
}
}

impl From<TlvStream> for cln_rpc::primitives::TlvStream {
fn from(s: TlvStream) -> Self {
Self {
entries: s.entries.into_iter().map(|e| e.into()).collect(),
}
}
}

impl From<TlvEntry> for cln_rpc::primitives::TlvEntry {
fn from(e: TlvEntry) -> Self {
Self {
typ: e.r#type,
value: e.value,
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
1 change: 1 addition & 0 deletions cln-grpc/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ fn test_keysend() {
}],
}],
}),
extratlvs: None,
};

let u: cln_rpc::model::KeysendRequest = g.into();
Expand Down
6 changes: 2 additions & 4 deletions cln-rpc/src/model.rs

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

61 changes: 60 additions & 1 deletion cln-rpc/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,19 @@ mod test {
let serialized: String = serde_json::to_string(&od).unwrap();
assert_eq!(a, serialized);
}

#[test]
fn tlvstream() {
let stream = TlvStream {
entries: vec![
TlvEntry { typ: 31337, value: vec![1,2,3,4,5]},
TlvEntry { typ: 42, value: vec![]},
],
};

let res = serde_json::to_string(&stream).unwrap();
assert_eq!(res, "{\"31337\":\"0102030405\",\"42\":\"\"}");
}
}

#[derive(Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -623,4 +636,50 @@ impl Display for RpcError {
}
}

impl std::error::Error for RpcError {}
impl std::error::Error for RpcError {}

#[derive(Clone, Debug)]
pub struct TlvEntry {
pub typ: u64,
pub value: Vec<u8>,
}

#[derive(Clone, Debug)]
pub struct TlvStream {
pub entries: Vec<TlvEntry>,
}

impl<'de> Deserialize<'de> for TlvStream {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let map: std::collections::HashMap<u64, String> = Deserialize::deserialize(deserializer)?;

let entries = map
.iter()
.map(|(k, v)| TlvEntry {
typ: *k,
value: hex::decode(v).unwrap(),
})
.collect();

Ok(TlvStream { entries })
}
}

impl Serialize for TlvStream {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
use serde::ser::SerializeMap;

let mut map = serializer.serialize_map(Some(self.entries.len()))?;
for e in &self.entries {
map.serialize_key(&e.typ)?;
map.serialize_value(&hex::encode(&e.value))?;
}
map.end()
}
}
1 change: 1 addition & 0 deletions contrib/msggen/msggen/gen/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ def generate_composite(self, prefix, field: CompositeField) -> None:
'hash': f'Sha256::from_slice(&c.{name}).unwrap()',
'hash?': f'c.{name}.map(|v| Sha256::from_slice(&v).unwrap())',
'txid': f'hex::encode(&c.{name})',
'TlvStream?': f'c.{name}.map(|s| s.into())',
}.get(
typ,
f'c.{name}' # default to just assignment
Expand Down
17 changes: 16 additions & 1 deletion contrib/msggen/msggen/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,21 @@ def __str__(self):
DatastoreKeyField = ArrayField(itemtype=PrimitiveField("string", None, None), dims=1, path=None, description=None)
InvoiceExposeprivatechannelsField = PrimitiveField("boolean", None, None)
PayExclude = ArrayField(itemtype=PrimitiveField("string", None, None), dims=1, path=None, description=None)
RoutehintListField = PrimitiveField("RoutehintList", None, None)
RoutehintListField = PrimitiveField(
"RoutehintList",
None,
None
)

# TlvStreams are special, they don't have preset dict-keys, rather
# they can specify `u64` keys pointing to hex payloads. So the schema
# has to rely on additionalProperties to make it work.
TlvStreamField = PrimitiveField(
"TlvStream",
None,
None
)

# Override fields with manually managed types, fieldpath -> field mapping
overrides = {
'Invoice.label': InvoiceLabelField,
Expand All @@ -355,6 +369,7 @@ def __str__(self):
'Invoice.exposeprivatechannels': InvoiceExposeprivatechannelsField,
'Pay.exclude': PayExclude,
'KeySend.routehints': RoutehintListField,
'KeySend.extratlvs': TlvStreamField,
}


Expand Down
226 changes: 108 additions & 118 deletions contrib/pyln-testing/pyln/testing/node_pb2.py

Large diffs are not rendered by default.

30 changes: 25 additions & 5 deletions contrib/pyln-testing/pyln/testing/primitives_pb2.py

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

0 comments on commit 356bab6

Please sign in to comment.