Skip to content

Commit

Permalink
cln-grpc: Add generation of grpc protobuf file from schema
Browse files Browse the repository at this point in the history
  • Loading branch information
cdecker committed Feb 28, 2022
1 parent edf6832 commit d01b2c2
Show file tree
Hide file tree
Showing 7 changed files with 431 additions and 30 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ ifneq ($(FUZZING),0)
endif
ifneq ($(RUST),0)
include cln-rpc/Makefile
include cln-grpc/Makefile
endif

# We make pretty much everything depend on these.
Expand Down
11 changes: 11 additions & 0 deletions cln-grpc/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cln-grpc-wrongdir:
$(MAKE) -C .. cln-grpc-all

CLN_GRPC_EXAMPLES :=
CLN_GRPC_GENALL = cln-grpc/proto/node.proto
DEFAULT_TARGETS += $(CLN_GRPC_EXAMPLES) $(CLN_GRPC_GENALL)

$(CLN_GRPC_GENALL): $(JSON_SCHEMA)
PYTHONPATH=contrib/msggen python3 contrib/msggen/msggen/__main__.py

cln-grpc-all: ${CLN_GRPC_GENALL} ${CLN_GRPC_EXAMPLES}
184 changes: 184 additions & 0 deletions cln-grpc/proto/node.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
syntax = "proto3";
package cln;

// This file was automatically derived from the JSON-RPC schemas in
// `doc/schemas`. Do not edit this file manually as it would get
// overwritten.

import "primitives.proto";

service Node {
rpc Getinfo(GetinfoRequest) returns (GetinfoResponse) {}
rpc ListFunds(ListfundsRequest) returns (ListfundsResponse) {}
rpc ListChannels(ListchannelsRequest) returns (ListchannelsResponse) {}
rpc AddGossip(AddgossipRequest) returns (AddgossipResponse) {}
rpc AutoCleanInvoice(AutocleaninvoiceRequest) returns (AutocleaninvoiceResponse) {}
rpc CheckMessage(CheckmessageRequest) returns (CheckmessageResponse) {}
rpc Close(CloseRequest) returns (CloseResponse) {}
}

message GetinfoRequest {
}

message GetinfoResponse {
bytes id = 1;
string alias = 2;
bytes color = 3;
uint32 num_peers = 4;
uint32 num_pending_channels = 5;
uint32 num_active_channels = 6;
uint32 num_inactive_channels = 7;
string version = 8;
string lightning_dir = 9;
uint32 blockheight = 10;
string network = 11;
Amount fees_collected_msat = 12;
repeated GetinfoAddress address = 13;
repeated GetinfoBinding binding = 14;
optional string warning_bitcoind_sync = 15;
optional string warning_lightningd_sync = 16;
}

message GetinfoAddress {
// Getinfo.address[].type
enum GetinfoAddressType {
DNS = 0;
IPV4 = 1;
IPV6 = 2;
TORV2 = 3;
TORV3 = 4;
WEBSOCKET = 5;
}
GetinfoAddressType item_type = 1;
uint32 port = 2;
optional string address = 3;
}

message GetinfoBinding {
// Getinfo.binding[].type
enum GetinfoBindingType {
LOCAL_SOCKET = 0;
IPV4 = 1;
IPV6 = 2;
TORV2 = 3;
TORV3 = 4;
}
GetinfoBindingType item_type = 1;
optional string address = 2;
optional uint32 port = 3;
optional string socket = 4;
}

message ListfundsRequest {
optional bool spent = 1;
}

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

message ListfundsOutputs {
// ListFunds.outputs[].status
enum ListfundsOutputsStatus {
UNCONFIRMED = 0;
CONFIRMED = 1;
SPENT = 2;
}
bytes txid = 1;
uint32 output = 2;
Amount amount_msat = 3;
bytes scriptpubkey = 4;
optional string address = 5;
optional bytes redeemscript = 6;
ListfundsOutputsStatus status = 7;
optional uint32 blockheight = 8;
}

message ListfundsChannels {
bytes peer_id = 1;
Amount our_amount_msat = 2;
Amount amount_msat = 3;
bytes funding_txid = 4;
uint32 funding_output = 5;
bool connected = 6;
ChannelState state = 7;
optional string short_channel_id = 8;
}

message ListchannelsRequest {
optional string short_channel_id = 1;
optional bytes source = 2;
optional bytes destination = 3;
}

message ListchannelsResponse {
repeated ListchannelsChannels channels = 1;
}

message ListchannelsChannels {
bytes source = 1;
bytes destination = 2;
bool public = 3;
Amount amount_msat = 4;
uint32 message_flags = 5;
uint32 channel_flags = 6;
bool active = 7;
uint32 last_update = 8;
uint32 base_fee_millisatoshi = 9;
uint32 fee_per_millionth = 10;
uint32 delay = 11;
Amount htlc_minimum_msat = 12;
optional Amount htlc_maximum_msat = 13;
bytes features = 14;
}

message AddgossipRequest {
bytes message = 1;
}

message AddgossipResponse {
}

message AutocleaninvoiceRequest {
optional uint64 expired_by = 1;
optional uint64 cycle_seconds = 2;
}

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

message CheckmessageRequest {
string message = 1;
string zbase = 2;
optional bytes pubkey = 3;
}

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

message CloseRequest {
bytes id = 1;
optional uint32 unilateraltimeout = 2;
optional string destination = 3;
optional string fee_negotiation_step = 4;
optional bytes wrong_funding = 5;
optional bool force_lease_closed = 6;
}

message CloseResponse {
// Close.type
enum CloseType {
MUTUAL = 0;
UNILATERAL = 1;
UNOPENED = 2;
}
CloseType item_type = 1;
optional bytes tx = 2;
optional bytes txid = 3;
}
33 changes: 33 additions & 0 deletions cln-grpc/proto/primitives.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
syntax = "proto3";
package cln;

message Amount {
oneof unit {
uint64 millisatoshi = 1;
uint64 satoshi = 2;
uint64 bitcoin = 3;
bool all = 4;
bool any = 5;
}
}

enum ChannelSide {
IN = 0;
OUT = 1;
}

enum ChannelState {
Openingd = 0;
ChanneldAwaitingLockin = 1;
ChanneldNormal = 2;
ChanneldShuttingDown = 3;
ClosingdSigexchange = 4;
ClosingdComplete = 5;
AwaitingUnilateral = 6;
FundingSpendSeen = 7;
Onchain = 8;
DualopendOpenInit = 9;
DualopendAwaitingLockin = 10;
}

message ChannelStateChangeCause {}
8 changes: 8 additions & 0 deletions contrib/msggen/msggen/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from msggen.model import Method, CompositeField, Service
from msggen.grpc import GrpcGenerator
from msggen.rust import RustGenerator
from pathlib import Path
import subprocess
Expand Down Expand Up @@ -125,6 +126,12 @@ def load_jsonrpc_service():
return service


def gengrpc(service):
"""Load all mapped RPC methods, wrap them in a Service, and split them into messages.
"""
fname = repo_root() / "cln-grpc" / "proto" / "node.proto"
dest = open(fname, "w")
GrpcGenerator(dest).generate(service)
def genrustjsonrpc(service):
fname = repo_root() / "cln-rpc" / "src" / "model.rs"
dest = open(fname, "w")
Expand All @@ -133,6 +140,7 @@ def genrustjsonrpc(service):

def run():
service = load_jsonrpc_service()
gengrpc(service)
genrustjsonrpc(service)


Expand Down
Loading

0 comments on commit d01b2c2

Please sign in to comment.