Skip to content

Commit

Permalink
cln-grpc: Map AmountOrAll and AmountOrAny
Browse files Browse the repository at this point in the history
  • Loading branch information
cdecker authored and rustyrussell committed Apr 1, 2022
1 parent 04e7e28 commit bba68e2
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 10 deletions.
4 changes: 2 additions & 2 deletions cln-grpc/proto/node.proto
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ message DelinvoiceResponse {
}

message InvoiceRequest {
Amount msatoshi = 1;
AmountOrAny msatoshi = 1;
string description = 2;
string label = 3;
repeated string fallbacks = 4;
Expand Down Expand Up @@ -910,7 +910,7 @@ message NewaddrResponse {

message WithdrawRequest {
bytes destination = 1;
optional Amount satoshi = 2;
optional AmountOrAll satoshi = 2;
optional Feerate feerate = 5;
optional uint32 minconf = 3;
repeated Utxo utxos = 4;
Expand Down
14 changes: 14 additions & 0 deletions cln-grpc/proto/primitives.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ message Amount {
uint64 msat = 1;
}

message AmountOrAll {
oneof value {
Amount amount = 1;
bool all = 2;
}
}

message AmountOrAny {
oneof value {
Amount amount = 1;
bool any = 2;
}
}

enum ChannelSide {
IN = 0;
OUT = 1;
Expand Down
4 changes: 2 additions & 2 deletions cln-grpc/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ impl From<&pb::DelinvoiceRequest> for requests::DelinvoiceRequest {
impl From<&pb::InvoiceRequest> for requests::InvoiceRequest {
fn from(c: &pb::InvoiceRequest) -> Self {
Self {
msatoshi: c.msatoshi.as_ref().unwrap().into(), // Rule #1 for type msat
msatoshi: c.msatoshi.as_ref().unwrap().into(), // Rule #1 for type msat|any
description: c.description.clone(), // Rule #1 for type string
label: c.label.clone(), // Rule #1 for type string
fallbacks: c.fallbacks.iter().map(|s| s.into()).collect(),
Expand Down Expand Up @@ -1156,7 +1156,7 @@ impl From<&pb::WithdrawRequest> for requests::WithdrawRequest {
fn from(c: &pb::WithdrawRequest) -> Self {
Self {
destination: hex::encode(&c.destination), // Rule #1 for type pubkey
satoshi: c.satoshi.as_ref().map(|a| a.into()), // Rule #1 for type msat?
satoshi: c.satoshi.as_ref().map(|a| a.into()), // Rule #1 for type msat|all?
feerate: c.feerate.as_ref().map(|a| a.into()), // Rule #1 for type feerate?
minconf: c.minconf.map(|v| v as u16), // Rule #1 for type u16?
utxos: c.utxos.iter().map(|s| s.into()).collect(),
Expand Down
48 changes: 47 additions & 1 deletion cln-grpc/src/pb.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
tonic::include_proto!("cln");

use cln_rpc::primitives::{
Amount as JAmount, Feerate as JFeerate, OutputDesc as JOutputDesc, Utxo as JUtxo,
Amount as JAmount, AmountOrAll as JAmountOrAll, AmountOrAny as JAmountOrAny,
Feerate as JFeerate, OutputDesc as JOutputDesc, Utxo as JUtxo,
};

impl From<JAmount> for Amount {
Expand Down Expand Up @@ -55,3 +56,48 @@ impl From<&OutputDesc> for JOutputDesc {
}
}
}

impl From<JAmountOrAll> for AmountOrAll {
fn from(a: JAmountOrAll) -> Self {
match a {
JAmountOrAll::Amount(a) => AmountOrAll {
value: Some(amount_or_all::Value::Amount(a.into())),
},
JAmountOrAll::All => AmountOrAll {
value: Some(amount_or_all::Value::All(true)),
},
}
}
}

impl From<&AmountOrAll> for JAmountOrAll {
fn from(a: &AmountOrAll) -> Self {
match &a.value {
Some(amount_or_all::Value::Amount(a)) => JAmountOrAll::Amount(a.into()),
Some(amount_or_all::Value::All(_)) => JAmountOrAll::All,
None => panic!("AmountOrAll is neither amount nor all: {:?}", a),
}
}
}

impl From<JAmountOrAny> for AmountOrAny {
fn from(a: JAmountOrAny) -> Self {
match a {
JAmountOrAny::Amount(a) => AmountOrAny {
value: Some(amount_or_any::Value::Amount(a.into())),
},
JAmountOrAny::Any => AmountOrAny {
value: Some(amount_or_any::Value::Any(true)),
},
}
}
}
impl From<&AmountOrAny> for JAmountOrAny {
fn from(a: &AmountOrAny) -> Self {
match &a.value {
Some(amount_or_any::Value::Amount(a)) => JAmountOrAny::Amount(a.into()),
Some(amount_or_any::Value::Any(_)) => JAmountOrAny::Any,
None => panic!("AmountOrAll is neither amount nor any: {:?}", a),
}
}
}
2 changes: 1 addition & 1 deletion cln-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl ClnRpc {
// serde_json knows which variant of [`Request`] should be
// used.
response["method"] = req2["method"].clone();

log::warn!("XXX {:?}", response);
serde_json::from_value(response).context("converting response into enum")
}
}
Expand Down
4 changes: 2 additions & 2 deletions cln-rpc/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ pub mod requests {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct InvoiceRequest {
#[serde(alias = "msatoshi")]
pub msatoshi: Amount,
pub msatoshi: AmountOrAny,
#[serde(alias = "description")]
pub description: String,
#[serde(alias = "label")]
Expand Down Expand Up @@ -477,7 +477,7 @@ pub mod requests {
#[serde(alias = "destination")]
pub destination: String,
#[serde(alias = "satoshi", skip_serializing_if = "Option::is_none")]
pub satoshi: Option<Amount>,
pub satoshi: Option<AmountOrAll>,
#[serde(alias = "feerate", skip_serializing_if = "Option::is_none")]
pub feerate: Option<Feerate>,
#[serde(alias = "minconf", skip_serializing_if = "Option::is_none")]
Expand Down
6 changes: 6 additions & 0 deletions contrib/msggen/msggen/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
'boolean': 'bool',
'hex': 'bytes',
'msat': 'Amount',
'msat|all': 'AmountOrAll',
'msat|any': 'AmountOrAny',
'number': 'sint64',
'pubkey': 'bytes',
'short_channel_id': 'string',
Expand Down Expand Up @@ -395,6 +397,10 @@ def generate_composite(self, prefix, field: CompositeField) -> None:
'pubkey?': f'c.{name}.clone().map(|v| hex::encode(v))',
'msat': f'c.{name}.as_ref().unwrap().into()',
'msat?': f'c.{name}.as_ref().map(|a| a.into())',
'msat|all': f'c.{name}.as_ref().unwrap().into()',
'msat|all?': f'c.{name}.as_ref().map(|a| a.into())',
'msat|any': f'c.{name}.as_ref().unwrap().into()',
'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())',
}.get(
Expand Down
2 changes: 2 additions & 0 deletions contrib/msggen/msggen/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ class PrimitiveField(Field):
"pubkey",
"signature",
"msat",
"msat|any",
"msat|all",
"hex",
"short_channel_id",
"txid",
Expand Down
2 changes: 2 additions & 0 deletions contrib/msggen/msggen/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
'boolean': 'bool',
'hex': 'String',
'msat': 'Amount',
'msat|all': 'AmountOrAll',
'msat|any': 'AmountOrAny',
'number': 'i64',
'pubkey': 'String',
'short_channel_id': 'String',
Expand Down
2 changes: 1 addition & 1 deletion doc/schemas/invoice.request.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"properties": {
"msatoshi": {
"type": "msat",
"type": "msat|any",
"description": ""
},
"description": {
Expand Down
2 changes: 1 addition & 1 deletion doc/schemas/withdraw.request.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"type": "pubkey"
},
"satoshi": {
"type": "msat"
"type": "msat|all"
},
"feerate": {
"type": "feerate"
Expand Down
10 changes: 10 additions & 0 deletions tests/test_cln_rs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ephemeral_port_reserve import reserve
import grpc
import node_pb2 as nodepb
from primitives_pb2 import AmountOrAny
import pytest
import subprocess

Expand Down Expand Up @@ -101,6 +102,15 @@ def test_grpc_connect(node_factory):
response = stub.ListFunds(nodepb.ListfundsRequest())
print(response)

inv = stub.Invoice(nodepb.InvoiceRequest(
msatoshi=AmountOrAny(any=True),
description="hello",
label="lbl1",
preimage=b"\x00" * 32,
cltv=24
))
print(inv)


def test_grpc_generate_certificate(node_factory):
"""Test whether we correctly generate the certificates.
Expand Down

0 comments on commit bba68e2

Please sign in to comment.