Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contrib/api/generate-python-docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@

def generate_docs(output_dir: Path, repo_root: Path):
"""Generate documentation for all packages."""
print(f"Generating Python documentation for all workspace packages...")
print("Generating Python documentation for all workspace packages...")
print(f"Output directory: {output_dir}")

# Clean and create output directory
Expand Down
26 changes: 13 additions & 13 deletions contrib/msggen/msggen/gen/grpc/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ def generate_composite(self, prefix, field: CompositeField, override=None):
# The inner conversion applied to each element in the
# array. The current item is called `i`
mapping = {
"hex": f"hex::decode(i).unwrap()",
"secret": f"i.to_vec()",
"hash": f"<Sha256 as AsRef<[u8]>>::as_ref(&i).to_vec()",
"short_channel_id": f"i.to_string()",
"short_channel_id_dir": f"i.to_string()",
"pubkey": f"i.serialize().to_vec()",
"txid": f"hex::decode(i).unwrap()",
}.get(typ, f"i.into()")
"hex": "hex::decode(i).unwrap()",
"secret": "i.to_vec()",
"hash": "<Sha256 as AsRef<[u8]>>::as_ref(&i).to_vec()",
"short_channel_id": "i.to_string()",
"short_channel_id_dir": "i.to_string()",
"pubkey": "i.serialize().to_vec()",
"txid": "hex::decode(i).unwrap()",
}.get(typ, "i.into()")

self.write(f"// Field: {f.path}\n", numindent=3)
if not f.optional:
Expand Down Expand Up @@ -143,7 +143,7 @@ def generate_composite(self, prefix, field: CompositeField, override=None):
)

if f.deprecated:
self.write(f"#[allow(deprecated)]\n", numindent=3)
self.write("#[allow(deprecated)]\n", numindent=3)
self.write(f"{name}: {rhs}, // Rule #2 for type {typ}\n", numindent=3)

elif isinstance(f, CompositeField):
Expand All @@ -154,10 +154,10 @@ def generate_composite(self, prefix, field: CompositeField, override=None):
rhs = f"c.{name}.map(|v| v.into())"
self.write(f"{name}: {rhs},\n", numindent=3)
self.write(
f"""\
}}
}}
}}
"""\
}
}
}

"""
)
Expand Down
4 changes: 2 additions & 2 deletions contrib/msggen/msggen/gen/grpc/proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def generate_service(self, service: Service) -> None:
)

self.write(
f"""}}
"""}
"""
)

Expand Down Expand Up @@ -228,7 +228,7 @@ def generate_message(self, message: CompositeField, typename_override=None):

def generate(self, service: Service) -> None:
"""Generate the GRPC protobuf file and write to `dest`"""
self.write(f"""syntax = "proto3";\npackage cln;\n""")
self.write("""syntax = "proto3";\npackage cln;\n""")
self.write(
"""
// This file was automatically derived from the JSON-RPC schemas in
Expand Down
74 changes: 37 additions & 37 deletions contrib/msggen/msggen/gen/grpc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,91 +28,91 @@ def write(self, text: str, numindent: Optional[int] = None):

def generate(self, service: Service) -> None:
self.write(
f"""\
"""\
use crate::pb::node_server::Node;
use crate::pb;
use cln_rpc::{{Request, Response, ClnRpc}};
use cln_rpc::{Request, Response, ClnRpc};
use cln_rpc::notifications::Notification;
use anyhow::Result;
use std::path::{{Path, PathBuf}};
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::task::{{Context, Poll}};
use std::task::{Context, Poll};
use cln_rpc::model::requests;
use log::{{debug, trace}};
use tonic::{{Code, Status}};
use log::{debug, trace};
use tonic::{Code, Status};
use tokio::sync::broadcast;
use tokio_stream::wrappers::errors::BroadcastStreamRecvError;
use tokio_stream::wrappers::BroadcastStream;


#[derive(Clone)]
pub struct Server
{{
{
rpc_path: PathBuf,
events : broadcast::Sender<Notification>
}}
}

impl Server
{{
{
pub async fn new(
path: &Path,
events : broadcast::Sender<Notification>
) -> Result<Self>
{{
Ok(Self {{
{
Ok(Self {
rpc_path: path.to_path_buf(),
events : events
}})
}}
}}
})
}
}

pub struct NotificationStream<T> {{
pub struct NotificationStream<T> {
inner : Pin<Box<BroadcastStream<Notification>>>,
fn_filter_map : fn(Notification) -> Option<T>
}}
}

impl<T : 'static + Send + Clone> tokio_stream::Stream for NotificationStream<T> {{
impl<T : 'static + Send + Clone> tokio_stream::Stream for NotificationStream<T> {

type Item = Result<T, tonic::Status>;

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {{
while let Poll::Ready(result) = self.inner.as_mut().poll_next(cx) {{
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
while let Poll::Ready(result) = self.inner.as_mut().poll_next(cx) {
// None is used here to signal that we have reached the end of stream
// If inner ends the stream by returning None we do the same
if result.is_none() {{
if result.is_none() {
return Poll::Ready(None)
}}
}
let result: Result<cln_rpc::Notification, BroadcastStreamRecvError> = result.unwrap();

match result {{
Err(BroadcastStreamRecvError::Lagged(lag)) => {{
match result {
Err(BroadcastStreamRecvError::Lagged(lag)) => {
// In this error case we've missed some notifications
// We log the error to core lightning and forward
// this information to the client
log::warn!("Due to lag the grpc-server skipped {{}} notifications", lag);
log::warn!("Due to lag the grpc-server skipped {} notifications", lag);
return Poll::Ready(Some(Err(
Status::data_loss(
format!("Skipped up to {{}} notifications", lag)))))
}}
Ok(notification) => {{
format!("Skipped up to {} notifications", lag)))))
}
Ok(notification) => {
let filtered = (self.fn_filter_map)(notification);
match filtered {{
match filtered {
Some(n) => return Poll::Ready(Some(Ok(n))),
None => {{
None => {
// We ignore the message if it isn't a match.
// e.g: A `ChannelOpenedStream` will ignore `CustomMsgNotifications`
}}
}}
}}
}}
}}
}
}
}
}
}
Poll::Pending
}}
}}
}
}

#[tonic::async_trait]
impl Node for Server
{{
{
""",
numindent=0,
)
Expand Down
28 changes: 14 additions & 14 deletions contrib/msggen/msggen/gen/grpc/unconvert.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ def generate_composite(self, prefix, field: CompositeField, override=None) -> No
if isinstance(f, ArrayField):
typ = f.itemtype.typename
mapping = {
"hex": f"hex::encode(s)",
"u32": f"s",
"secret": f"s.try_into().unwrap()",
"hash": f"Sha256::from_slice(&s).unwrap()",
"short_channel_id": f"cln_rpc::primitives::ShortChannelId::from_str(&s).unwrap()",
"short_channel_id_dir": f"cln_rpc::primitives::ShortChannelIdDir::from_str(&s).unwrap()",
"pubkey": f"PublicKey::from_slice(&s).unwrap()",
"txid": f"hex::encode(s)",
}.get(typ, f"s.into()")
"hex": "hex::encode(s)",
"u32": "s",
"secret": "s.try_into().unwrap()",
"hash": "Sha256::from_slice(&s).unwrap()",
"short_channel_id": "cln_rpc::primitives::ShortChannelId::from_str(&s).unwrap()",
"short_channel_id_dir": "cln_rpc::primitives::ShortChannelIdDir::from_str(&s).unwrap()",
"pubkey": "PublicKey::from_slice(&s).unwrap()",
"txid": "hex::encode(s)",
}.get(typ, "s.into()")

# TODO fix properly
if typ in ["ListtransactionsTransactionsType"]:
continue
if name == "state_changes":
self.write(f" state_changes: None,")
self.write(" state_changes: None,")
continue

if not f.optional:
Expand Down Expand Up @@ -152,10 +152,10 @@ def generate_composite(self, prefix, field: CompositeField, override=None) -> No
self.write(f"{name}: {rhs},\n", numindent=3)

self.write(
f"""\
}}
}}
}}
"""\
}
}
}

"""
)
2 changes: 1 addition & 1 deletion contrib/msggen/msggen/gen/grpc2py.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def {converter_name}(m):
cleanup=False,
)

self.write(f" }})\n", cleanup=False)
self.write(" })\n", cleanup=False)

# Add ourselves to the converters so if we were generated as a
# dependency for a composite they can find us again. We have
Expand Down
32 changes: 16 additions & 16 deletions contrib/msggen/msggen/gen/rpc/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ def gen_enum(e, meta, override):
norm = v.normalized()
decl += f' {typename}::{norm} => "{norm}",\n'
decl += dedent(
f"""\
}}.to_string()
}}
}}
"""\
}.to_string()
}
}

"""
)
Expand All @@ -203,7 +203,7 @@ def gen_enum(e, meta, override):
defi += rename_if_necessary(str(e.name), e.name.normalized())
defi += f" pub {e.name.normalized()}: {typename},\n"
else:
defi = f' #[serde(skip_serializing_if = "Option::is_none")]\n'
defi = ' #[serde(skip_serializing_if = "Option::is_none")]\n'
defi += f" pub {e.name.normalized()}: Option<{typename}>,\n"

return defi, decl
Expand All @@ -230,7 +230,7 @@ def rename_if_necessary(original, name):
if str(original) != str(name):
return f' #[serde(rename = "{original}")]\n'
else:
return f""
return ""


def gen_array(a, meta, override=None):
Expand Down Expand Up @@ -312,7 +312,7 @@ def generate_requests(self, service: Service):
#[allow(unused_imports)]
use crate::primitives::*;
#[allow(unused_imports)]
use serde::{{Deserialize, Serialize}};
use serde::{Deserialize, Serialize};
use core::fmt::Debug;
use std::collections::HashMap;
use super::{IntoRequest, Request, TypedRequest};
Expand Down Expand Up @@ -360,7 +360,7 @@ def generate_responses(self, service: Service):
#[allow(unused_imports)]
use crate::primitives::*;
#[allow(unused_imports)]
use serde::{{Deserialize, Serialize}};
use serde::{Deserialize, Serialize};
use super::{TryFromResponseError, Response};

"""
Expand Down Expand Up @@ -397,13 +397,13 @@ def generate_response_trait_impl(self, method: Method):
def generate_enums(self, service: Service):
"""The Request and Response enums serve as parsing primitives."""
self.write(
f"""\
use serde::{{Deserialize, Serialize}};
"""\
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "method", content = "params")]
#[serde(rename_all = "lowercase")]
pub enum Request {{
pub enum Request {
"""
)

Expand All @@ -417,13 +417,13 @@ def generate_enums(self, service: Service):
)

self.write(
f"""\
}}
"""\
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "method", content = "result")]
#[serde(rename_all = "lowercase")]
pub enum Response {{
pub enum Response {
"""
)

Expand All @@ -437,8 +437,8 @@ def generate_enums(self, service: Service):
)

self.write(
f"""\
}}
"""\
}

"""
)
Expand Down
Loading