msggen: Add RoutehintList as a primitive

This commit is contained in:
Christian Decker 2022-04-01 14:43:34 +10:30 committed by Rusty Russell
parent ec5cd92580
commit ef145c7900
6 changed files with 65 additions and 4 deletions

View file

@ -58,4 +58,18 @@ message Feerate {
message OutputDesc {
string address = 1;
Amount amount = 2;
}
message RouteHop {
bytes id = 1;
string short_channel_id = 2;
Amount feebase = 3;
uint32 feeprop = 4;
uint32 expirydelta = 5;
}
message Routehint {
repeated RouteHop hops = 1;
}
message RoutehintList {
repeated Routehint hints = 2;
}

View file

@ -2,7 +2,7 @@ tonic::include_proto!("cln");
use cln_rpc::primitives::{
Amount as JAmount, AmountOrAll as JAmountOrAll, AmountOrAny as JAmountOrAny,
Feerate as JFeerate, OutputDesc as JOutputDesc, Outpoint as JOutpoint,
Feerate as JFeerate, Outpoint as JOutpoint, OutputDesc as JOutputDesc,
};
impl From<JAmount> for Amount {
@ -101,3 +101,28 @@ impl From<&AmountOrAny> for JAmountOrAny {
}
}
}
impl From<RouteHop> for cln_rpc::primitives::Routehop {
fn from(c: RouteHop) -> Self {
Self {
id: hex::encode(c.id),
scid: c.short_channel_id,
feebase: c.feebase.as_ref().unwrap().into(),
feeprop: c.feeprop,
expirydelta: c.expirydelta as u16,
}
}
}
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(),
}
}
}

View file

@ -284,7 +284,7 @@ impl Serialize for Feerate {
where
S: Serializer,
{
let s: String = self.into();
let s: String = self.into();
serializer.serialize_str(&s)
}
}
@ -425,3 +425,22 @@ impl Serialize for OutputDesc {
map.end()
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Routehop {
pub id: String,
pub scid: String,
pub feebase: Amount,
pub feeprop: u32,
pub expirydelta: u16,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Routehint {
pub hops: Vec<Routehop>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RoutehintList {
pub hints: Vec<Routehint>,
}

View file

@ -377,7 +377,7 @@ class GrpcUnconverterGenerator(GrpcConverterGenerator):
if isinstance(f, ArrayField):
typ = f.itemtype.typename
mapping = {
'hex': f'hex::decode(s).unwrap()',
'hex': f'hex::encode(s)',
'u32': f's.clone()',
}.get(typ, f's.into()')
self.write(f"{name}: c.{name}.iter().map(|s| {mapping}).collect(),\n", numindent=3)
@ -410,6 +410,7 @@ class GrpcUnconverterGenerator(GrpcConverterGenerator):
'msat|any?': f'c.{name}.as_ref().map(|a| a.into())',
'feerate': f'c.{name}.as_ref().unwrap().into()',
'feerate?': f'c.{name}.as_ref().map(|a| a.into())',
'RoutehintList?': f'c.{name}.clone().map(|rl| rl.into())',
}.get(
typ,
f'c.{name}.clone()' # default to just assignment

View file

@ -340,6 +340,7 @@ InvoiceLabelField = PrimitiveField("string", None, None)
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)
# Override fields with manually managed types, fieldpath -> field mapping
overrides = {
'Invoice.label': InvoiceLabelField,
@ -350,6 +351,7 @@ overrides = {
'ListDatastore.key': DatastoreKeyField,
'Invoice.exposeprivatechannels': InvoiceExposeprivatechannelsField,
'Pay.exclude': PayExclude,
'KeySend.routehints': RoutehintListField,
}

View file

@ -165,7 +165,7 @@ def gen_array(a):
return ("", "") # Override said not to include
itemtype = typemap.get(itemtype, itemtype)
alias = a.name.normalized()[:-2] # Strip the `[]` suffix for arrays.
alias = a.name.normalized()
defi = f" #[serde(alias = \"{alias}\")]\n pub {name}: {'Vec<'*a.dims}{itemtype}{'>'*a.dims},\n"
return (defi, decl)