Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Channel endpoint improvements (#199) #214

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions app/lightning/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
from fastapi import HTTPException, status


class OpenChannelPushAmountError(HTTPException):
"""Raised when a the push amount is lower than the channel size."""

def __init__(self, local_funding, push_amt):
self.status_code = status.HTTP_400_BAD_REQUEST
self.detail = (
f"Push amount {push_amt} must be lower than "
f"local funding amount {local_funding}"
)


class NodeNotFoundError(HTTPException):
"""Raised when a node is not found in the graph."""

Expand Down
23 changes: 18 additions & 5 deletions app/lightning/impl/cln_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,12 +901,17 @@ async def peer_resolve_alias(self, node_pub: bytes) -> str:

@logger.catch(exclude=(HTTPException,))
async def channel_open(
self, local_funding_amount: int, node_URI: str, target_confs: int
self,
local_funding_amount: int,
node_URI: str,
target_confs: int,
push_amount_sat: int,
) -> str:
logger.trace(
(
f"channel_open(local_funding_amount={local_funding_amount}, "
f"node_URI={node_URI}, target_confs={target_confs})"
f"node_URI={node_URI}, target_confs={target_confs}, "
f"push_amount_sat={push_amount_sat})"
)
)

Expand All @@ -924,8 +929,11 @@ async def channel_open(
h = bytes.fromhex(node_URI.split("@")[0])
req = ln.FundchannelRequest(
id=h,
amount=lnp.AmountOrAll(amount=lnp.Amount(msat=local_funding_amount)),
amount=lnp.AmountOrAll(
amount=lnp.Amount(msat=local_funding_amount * 1000)
),
feerate=fee_rate,
push_msat=lnp.Amount(msat=push_amount_sat * 1000),
)
except TypeError as e:
logger.error(f"channel_open() failed at ln.FundchannelRequest(): {e}")
Expand Down Expand Up @@ -979,10 +987,15 @@ async def channel_open(
raise HTTPException(status.HTTP_500_INTERNAL_SERVER_ERROR, detail=details)

@logger.catch(exclude=(HTTPException,))
async def channel_list(self) -> List[Channel]:
logger.trace("channel_list()")
async def channel_list(
self,
include_closed: bool,
peer_alias_lookup: bool,
) -> List[Channel]:
logger.trace(f"channel_list({include_closed}, {peer_alias_lookup})")

try:
# TODO: switch to ListPeerChannels
res = await self._cln_stub.ListFunds(ln.ListfundsRequest())
peer_ids = [c.peer_id for c in res.channels]
peer_res = await asyncio.gather(
Expand Down
44 changes: 32 additions & 12 deletions app/lightning/impl/cln_jrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,21 +724,35 @@ async def listen_forward_events(self) -> AsyncGenerator[ForwardSuccessEvent, Non

@logger.catch(exclude=(HTTPException,))
async def channel_open(
self, local_funding_amount: int, node_URI: str, target_confs: int
self,
local_funding_amount: int,
node_URI: str,
target_confs: int,
push_amount_sat: int,
) -> str:
logger.trace(
(
f"channel_open(local_funding_amount={local_funding_amount}, "
f"node_URI={node_URI}, target_confs={target_confs})"
f"logger.channel_open(local_funding_amount={local_funding_amount}, "
f"node_URI={node_URI}, target_confs={target_confs}, "
f"push_amount_sat={push_amount_sat})"
)
)
# https://lightning.readthedocs.io/lightning-fundchannel.7.html
# fundchannel id amount [feerate] [announce] [minconf] [utxos] [push_msat]
# [close_to] [request_amt] [compact_lease] [reserve]

await self.connect_peer(node_URI)
fee_rate = calc_fee_rate_str(None, target_confs)
pub = node_URI.split("@")[0]
params = {"id": pub, "amount": local_funding_amount, "feerate": fee_rate}
params = {
"id": pub,
"amount": local_funding_amount,
"feerate": fee_rate,
}

if push_amount_sat is not None and push_amount_sat > 0:
params["push_msat"] = push_amount_sat * 1000

res = await self._send_request("fundchannel", params)

if "error" in res:
Expand Down Expand Up @@ -821,20 +835,26 @@ async def peer_resolve_alias(self, node_pub: str) -> str:
return str(nodes[0]["alias"])

@logger.catch(exclude=(HTTPException,))
async def channel_list(self) -> List[Channel]:
logger.trace("channel_list()")
async def channel_list(
self,
include_closed: bool,
peer_alias_lookup: bool,
) -> List[Channel]:
logger.trace(f"channel_list({include_closed}, {peer_alias_lookup})")

res = await self._send_request("listfunds")
res = await self._send_request("listpeerchannels")
if "error" in res:
self._raise_internal_server_error("listing channels", res)

res = res["result"]

peer_ids = [c["peer_id"] for c in res["channels"]]
peers = await asyncio.gather(
*[alias_or_empty(self.peer_resolve_alias, p) for p in peer_ids],
return_exceptions=True,
)
peers = [""] * len(res["channels"])
if peer_alias_lookup:
peer_ids = [c["peer_id"] for c in res["channels"]]
peers = await asyncio.gather(
*[alias_or_empty(self.peer_resolve_alias, p) for p in peer_ids],
return_exceptions=True,
)

channels = []
for c, p in zip(res["channels"], peers):
Expand Down
12 changes: 10 additions & 2 deletions app/lightning/impl/ln_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ async def listen_forward_events(self) -> ForwardSuccessEvent:

@abstractmethod
async def channel_open(
self, local_funding_amount: int, node_URI: str, target_confs: int
self,
local_funding_amount: int,
node_URI: str,
target_confs: int,
push_amount_sat: int,
) -> str:
raise NotImplementedError()

Expand All @@ -125,7 +129,11 @@ async def peer_resolve_alias(self, node_pub: str) -> str:
raise NotImplementedError()

@abstractmethod
async def channel_list(self) -> List[Channel]:
async def channel_list(
self,
include_closed: bool,
peer_alias_lookup: bool,
) -> List[Channel]:
raise NotImplementedError()

@abstractmethod
Expand Down
Loading