cln-grpc: Map AmountOrAll and AmountOrAny

This commit is contained in:
Christian Decker 2022-04-01 14:42:45 +10:30 committed by Rusty Russell
parent 04e7e285d7
commit bba68e2136
12 changed files with 84 additions and 4 deletions

Binary file not shown.

View File

@ -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;

Binary file not shown.

View File

@ -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 {
@ -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),
}
}
}

View File

@ -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")
}
}

Binary file not shown.

View File

@ -10,6 +10,8 @@ typemap = {
'boolean': 'bool',
'hex': 'bytes',
'msat': 'Amount',
'msat|all': 'AmountOrAll',
'msat|any': 'AmountOrAny',
'number': 'sint64',
'pubkey': 'bytes',
'short_channel_id': 'string',
@ -395,6 +397,10 @@ class GrpcUnconverterGenerator(GrpcConverterGenerator):
'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(

View File

@ -224,6 +224,8 @@ class PrimitiveField(Field):
"pubkey",
"signature",
"msat",
"msat|any",
"msat|all",
"hex",
"short_channel_id",
"txid",

View File

@ -32,6 +32,8 @@ typemap = {
'boolean': 'bool',
'hex': 'String',
'msat': 'Amount',
'msat|all': 'AmountOrAll',
'msat|any': 'AmountOrAny',
'number': 'i64',
'pubkey': 'String',
'short_channel_id': 'String',

View File

@ -9,7 +9,7 @@
],
"properties": {
"msatoshi": {
"type": "msat",
"type": "msat|any",
"description": ""
},
"description": {

View File

@ -9,7 +9,7 @@
"type": "pubkey"
},
"satoshi": {
"type": "msat"
"type": "msat|all"
},
"feerate": {
"type": "feerate"

View File

@ -5,6 +5,7 @@ from pyln.testing.utils import env, TEST_NETWORK, wait_for
from ephemeral_port_reserve import reserve
import grpc
import node_pb2 as nodepb
from primitives_pb2 import AmountOrAny
import pytest
import subprocess
@ -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.