Skip to content

Commit

Permalink
cln-grpc: Add the connect method
Browse files Browse the repository at this point in the history
  • Loading branch information
cdecker authored and rustyrussell committed Apr 1, 2022
1 parent 646901f commit d90aafe
Show file tree
Hide file tree
Showing 10 changed files with 718 additions and 184 deletions.
456 changes: 291 additions & 165 deletions .msggen.json

Large diffs are not rendered by default.

76 changes: 64 additions & 12 deletions cln-grpc/proto/node.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ service Node {
rpc CheckMessage(CheckmessageRequest) returns (CheckmessageResponse) {}
rpc Close(CloseRequest) returns (CloseResponse) {}
rpc ConnectPeer(ConnectRequest) returns (ConnectResponse) {}
rpc Datastore(DatastoreRequest) returns (DatastoreResponse) {}
rpc DelDatastore(DeldatastoreRequest) returns (DeldatastoreResponse) {}
rpc ListDatastore(ListdatastoreRequest) returns (ListdatastoreResponse) {}
}

message GetinfoRequest {
Expand Down Expand Up @@ -84,7 +87,7 @@ message ListpeersRequest {
}

message ListpeersResponse {
repeated ListpeersPeers peers = 3;
repeated ListpeersPeers peers = 1;
}

message ListpeersPeers {
Expand Down Expand Up @@ -231,8 +234,8 @@ message ListfundsRequest {
}

message ListfundsResponse {
repeated ListfundsOutputs outputs = 2;
repeated ListfundsChannels channels = 3;
repeated ListfundsOutputs outputs = 1;
repeated ListfundsChannels channels = 2;
}

message ListfundsOutputs {
Expand Down Expand Up @@ -270,7 +273,7 @@ message ListchannelsRequest {
}

message ListchannelsResponse {
repeated ListchannelsChannels channels = 4;
repeated ListchannelsChannels channels = 1;
}

message ListchannelsChannels {
Expand Down Expand Up @@ -304,9 +307,9 @@ message AutocleaninvoiceRequest {
}

message AutocleaninvoiceResponse {
bool enabled = 3;
optional uint64 expired_by = 1;
optional uint64 cycle_seconds = 2;
bool enabled = 1;
optional uint64 expired_by = 2;
optional uint64 cycle_seconds = 3;
}

message CheckmessageRequest {
Expand All @@ -316,8 +319,8 @@ message CheckmessageRequest {
}

message CheckmessageResponse {
bool verified = 4;
optional bytes pubkey = 3;
bool verified = 1;
optional bytes pubkey = 2;
}

message CloseRequest {
Expand All @@ -336,9 +339,9 @@ message CloseResponse {
UNILATERAL = 1;
UNOPENED = 2;
}
CloseType item_type = 7;
optional bytes tx = 8;
optional bytes txid = 9;
CloseType item_type = 1;
optional bytes tx = 2;
optional bytes txid = 3;
}

message ConnectRequest {
Expand Down Expand Up @@ -372,3 +375,52 @@ message ConnectAddress {
optional string address = 3;
optional uint32 port = 4;
}

message DatastoreRequest {
// Datastore.mode
enum DatastoreMode {
MUST_CREATE = 0;
MUST_REPLACE = 1;
CREATE_OR_REPLACE = 2;
MUST_APPEND = 3;
CREATE_OR_APPEND = 4;
}
repeated string key = 1;
optional bytes hex = 2;
optional DatastoreMode mode = 3;
optional uint64 generation = 4;
}

message DatastoreResponse {
repeated string key = 1;
optional uint64 generation = 2;
optional bytes hex = 3;
optional string string = 4;
}

message DeldatastoreRequest {
repeated string key = 1;
optional uint64 generation = 2;
}

message DeldatastoreResponse {
repeated string key = 1;
optional uint64 generation = 2;
optional bytes hex = 3;
optional string string = 4;
}

message ListdatastoreRequest {
repeated string key = 1;
}

message ListdatastoreResponse {
repeated ListdatastoreDatastore datastore = 1;
}

message ListdatastoreDatastore {
repeated string key = 1;
optional uint64 generation = 2;
optional bytes hex = 3;
optional string string = 4;
}
78 changes: 77 additions & 1 deletion cln-grpc/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,51 @@ impl From<&responses::ConnectResponse> for pb::ConnectResponse {
}
}

#[allow(unused_variables)]
impl From<&responses::DatastoreResponse> for pb::DatastoreResponse {
fn from(c: &responses::DatastoreResponse) -> Self {
Self {
key: c.key.iter().map(|s| s.into()).collect(),
generation: c.generation.clone(),
hex: c.hex.as_ref().map(|v| hex::decode(&v).unwrap()),
string: c.string.clone(),
}
}
}

#[allow(unused_variables)]
impl From<&responses::DeldatastoreResponse> for pb::DeldatastoreResponse {
fn from(c: &responses::DeldatastoreResponse) -> Self {
Self {
key: c.key.iter().map(|s| s.into()).collect(),
generation: c.generation.clone(),
hex: c.hex.as_ref().map(|v| hex::decode(&v).unwrap()),
string: c.string.clone(),
}
}
}

#[allow(unused_variables)]
impl From<&responses::ListdatastoreDatastore> for pb::ListdatastoreDatastore {
fn from(c: &responses::ListdatastoreDatastore) -> Self {
Self {
key: c.key.iter().map(|s| s.into()).collect(),
generation: c.generation.clone(),
hex: c.hex.as_ref().map(|v| hex::decode(&v).unwrap()),
string: c.string.clone(),
}
}
}

#[allow(unused_variables)]
impl From<&responses::ListdatastoreResponse> for pb::ListdatastoreResponse {
fn from(c: &responses::ListdatastoreResponse) -> Self {
Self {
datastore: c.datastore.iter().map(|s| s.into()).collect(),
}
}
}

#[allow(unused_variables)]
impl From<&pb::GetinfoRequest> for requests::GetinfoRequest {
fn from(c: &pb::GetinfoRequest) -> Self {
Expand Down Expand Up @@ -388,7 +433,38 @@ impl From<&pb::ConnectRequest> for requests::ConnectRequest {
Self {
id: hex::encode(&c.id),
host: c.host.clone(),
port: c.port.map(|i| i as u16),
port: c.port.map(|v| v as u16),
}
}
}

#[allow(unused_variables)]
impl From<&pb::DatastoreRequest> for requests::DatastoreRequest {
fn from(c: &pb::DatastoreRequest) -> Self {
Self {
key: c.key.iter().map(|s| s.into()).collect(),
hex: c.hex.clone().map(|v| hex::encode(v)),
mode: c.mode.map(|v| v.try_into().unwrap()),
generation: c.generation.clone(),
}
}
}

#[allow(unused_variables)]
impl From<&pb::DeldatastoreRequest> for requests::DeldatastoreRequest {
fn from(c: &pb::DeldatastoreRequest) -> Self {
Self {
key: c.key.iter().map(|s| s.into()).collect(),
generation: c.generation.clone(),
}
}
}

#[allow(unused_variables)]
impl From<&pb::ListdatastoreRequest> for requests::ListdatastoreRequest {
fn from(c: &pb::ListdatastoreRequest) -> Self {
Self {
key: c.key.iter().map(|s| s.into()).collect(),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions cln-grpc/src/pb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ impl From<JAmount> for Amount {
}
}

impl From<Amount> for JAmount {
fn from(a: Amount) -> Self {
JAmount::from_msat(a.msat)
impl From<&Amount> for JAmount {
fn from(a: &Amount) -> Self {
JAmount::from_msat(a.msat)
}
}
90 changes: 90 additions & 0 deletions cln-grpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,94 @@ async fn connect_peer(

}

async fn datastore(
&self,
request: tonic::Request<pb::DatastoreRequest>,
) -> Result<tonic::Response<pb::DatastoreResponse>, tonic::Status> {
let req = request.into_inner();
let req: requests::DatastoreRequest = (&req).into();
debug!("Client asked for getinfo");
let mut rpc = ClnRpc::new(&self.rpc_path)
.await
.map_err(|e| Status::new(Code::Internal, e.to_string()))?;
let result = rpc.call(Request::Datastore(req))
.await
.map_err(|e| Status::new(
Code::Unknown,
format!("Error calling method Datastore: {:?}", e)))?;
match result {
Response::Datastore(r) => Ok(
tonic::Response::new((&r).into())
),
r => Err(Status::new(
Code::Internal,
format!(
"Unexpected result {:?} to method call Datastore",
r
)
)),
}

}

async fn del_datastore(
&self,
request: tonic::Request<pb::DeldatastoreRequest>,
) -> Result<tonic::Response<pb::DeldatastoreResponse>, tonic::Status> {
let req = request.into_inner();
let req: requests::DeldatastoreRequest = (&req).into();
debug!("Client asked for getinfo");
let mut rpc = ClnRpc::new(&self.rpc_path)
.await
.map_err(|e| Status::new(Code::Internal, e.to_string()))?;
let result = rpc.call(Request::DelDatastore(req))
.await
.map_err(|e| Status::new(
Code::Unknown,
format!("Error calling method DelDatastore: {:?}", e)))?;
match result {
Response::DelDatastore(r) => Ok(
tonic::Response::new((&r).into())
),
r => Err(Status::new(
Code::Internal,
format!(
"Unexpected result {:?} to method call DelDatastore",
r
)
)),
}

}

async fn list_datastore(
&self,
request: tonic::Request<pb::ListdatastoreRequest>,
) -> Result<tonic::Response<pb::ListdatastoreResponse>, tonic::Status> {
let req = request.into_inner();
let req: requests::ListdatastoreRequest = (&req).into();
debug!("Client asked for getinfo");
let mut rpc = ClnRpc::new(&self.rpc_path)
.await
.map_err(|e| Status::new(Code::Internal, e.to_string()))?;
let result = rpc.call(Request::ListDatastore(req))
.await
.map_err(|e| Status::new(
Code::Unknown,
format!("Error calling method ListDatastore: {:?}", e)))?;
match result {
Response::ListDatastore(r) => Ok(
tonic::Response::new((&r).into())
),
r => Err(Status::new(
Code::Internal,
format!(
"Unexpected result {:?} to method call ListDatastore",
r
)
)),
}

}

}
Loading

0 comments on commit d90aafe

Please sign in to comment.