Skip to content

Commit

Permalink
Merge pull request C-Otto#36 from wamde/master
Browse files Browse the repository at this point in the history
 Added support for new flag -r/--ratio
  • Loading branch information
wamde authored Jan 21, 2019
2 parents 278249f + 0f50a4f commit da1c6a6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ $ pip install grpcio googleapis-common-protos

### Command line arguments
```
usage: rebalance.py [-h] [-l] [-o | -i] [-f CHANNEL] [-t CHANNEL] [-a AMOUNT]
usage: rebalance.py [-h] [-r RATIO] [-l] [-o | -i] [-f CHANNEL] [-t CHANNEL]
[-a AMOUNT]
optional arguments:
-h, --help show this help message and exit
-r RATIO, --ratio RATIO
ratio for channel imbalance between 1 and 50%, eg. 45
for 45%
list candidates:
Show the unbalanced channels.
Expand Down Expand Up @@ -52,9 +56,11 @@ rebalance:

### List of channels
Run `rebalance.py -l` (or `rebalance.py -l -i`) to see a list of channels which can be rebalanced.
This list only contains channels where more than 50% of the total funds are on the remote side of the channel.
This list only contains channels where more than 50% of the total funds are on the remote side of the channel.

You can also see the list of channels where more than 50% of the total funds are on the local side of the channel by running `rebalancy.py -l -o`.
You can also see the list of channels where more than 50% of the total funds are on the local side of the channel by running `rebalance.py -l -o`.

Use `-r/--ratio` to configure the sensitivity ratio (default is 50%).

As an example the following indicates a channel with around 17.7% of the funds on the local side:

Expand Down
7 changes: 5 additions & 2 deletions logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ def debug(message):


class Logic:
def __init__(self, lnd, first_hop_channel_id, last_hop_channel, amount):
def __init__(self, lnd, first_hop_channel_id, last_hop_channel, amount, channel_ratio):
self.lnd = lnd
self.first_hop_channel_id = first_hop_channel_id
self.last_hop_channel = last_hop_channel
self.amount = amount
self.channel_ratio = channel_ratio

def rebalance(self):
debug(("Sending {:,} satoshis to rebalance to channel with ID %d"
% self.last_hop_channel.chan_id).format(self.amount))
if self.channel_ratio != 0.5:
debug("Channel ratio used is %d%%" % int(self.channel_ratio*100))
if self.first_hop_channel_id:
debug("Forced first channel has ID %d" % self.first_hop_channel_id)

Expand Down Expand Up @@ -76,7 +79,7 @@ def low_local_ratio_after_sending(self, first_hop, total_amount):
remote = channel.remote_balance + total_amount
local = channel.local_balance - total_amount
ratio = float(local) / (remote + local)
return ratio < 0.5
return ratio < self.channel_ratio

def target_is_first_hop(self, first_hop):
return first_hop.chan_id == self.last_hop_channel.chan_id
Expand Down
26 changes: 15 additions & 11 deletions rebalance.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ def main():
first_hop_channel_id = vars(arguments)['from']
to_channel = arguments.to

channel_ratio = float(arguments.ratio) / 100

if arguments.incoming is not None and not arguments.list_candidates:
print("--outgoing and --incoming only work in conjunction with --list-candidates")
sys.exit()

if arguments.list_candidates:
incoming = arguments.incoming is None or arguments.incoming
if incoming:
list_incoming_candidates()
list_incoming_candidates(channel_ratio)
else:
list_outgoing_candidates()
list_outgoing_candidates(channel_ratio)
sys.exit()

if to_channel is None:
Expand All @@ -52,7 +54,7 @@ def main():
print("Amount is 0, nothing to do")
sys.exit()

response = Logic(lnd, first_hop_channel_id, last_hop_channel, amount).rebalance()
response = Logic(lnd, first_hop_channel_id, last_hop_channel, amount, channel_ratio).rebalance()
if response:
print(response)

Expand Down Expand Up @@ -83,6 +85,8 @@ def get_channel_for_channel_id(channel_id):

def get_argument_parser():
parser = argparse.ArgumentParser()
parser.add_argument("-r", "--ratio", action="store", type=int, dest="ratio", default=50,
help="ratio for channel imbalance between 1 and 50%%, eg. 45 for 45%%")
list_group = parser.add_argument_group("list candidates", "Show the unbalanced channels.")
list_group.add_argument("-l", "--list-candidates", action="store_true",
help="list candidate channels for rebalance")
Expand Down Expand Up @@ -121,13 +125,13 @@ def get_argument_parser():
return parser


def list_incoming_candidates():
candidates = get_incoming_rebalance_candidates()
def list_incoming_candidates(channel_ratio):
candidates = get_incoming_rebalance_candidates(channel_ratio)
list_candidates(candidates)


def list_outgoing_candidates():
candidates = get_outgoing_rebalance_candidates()
def list_outgoing_candidates(channel_ratio):
candidates = get_outgoing_rebalance_candidates(channel_ratio)
list_candidates(candidates)


Expand Down Expand Up @@ -155,14 +159,14 @@ def get_rebalance_amount(channel):
return abs(int(math.ceil(float(get_remote_surplus(channel)) / 2)))


def get_incoming_rebalance_candidates():
low_local = list(filter(lambda c: get_local_ratio(c) < 0.5, lnd.get_channels()))
def get_incoming_rebalance_candidates(channel_ratio):
low_local = list(filter(lambda c: get_local_ratio(c) < channel_ratio, lnd.get_channels()))
low_local = list(filter(lambda c: get_rebalance_amount(c) > 0, low_local))
return sorted(low_local, key=get_remote_surplus, reverse=False)


def get_outgoing_rebalance_candidates():
high_local = list(filter(lambda c: get_local_ratio(c) > 0.5, lnd.get_channels()))
def get_outgoing_rebalance_candidates(channel_ratio):
high_local = list(filter(lambda c: get_local_ratio(c) > 1 - channel_ratio, lnd.get_channels()))
high_local = list(filter(lambda c: get_rebalance_amount(c) > 0, high_local))
return sorted(high_local, key=get_remote_surplus, reverse=True)

Expand Down

0 comments on commit da1c6a6

Please sign in to comment.