Skip to content

Commit

Permalink
Rebalancer fixes (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptosharks131 authored Apr 8, 2023
1 parent 75047f4 commit 6d12548
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
7 changes: 5 additions & 2 deletions gui/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1948,8 +1948,11 @@ def rebalance(request):
if Channels.objects.filter(is_active=True, is_open=True, remote_pubkey=form.cleaned_data['last_hop_pubkey']).exists() or form.cleaned_data['last_hop_pubkey'] == '':
chan_ids = [ch.chan_id for ch in form.cleaned_data['outgoing_chan_ids']]
if len(chan_ids) > 0:
target_channel = Channels.objects.filter(is_active=True, is_open=True, remote_pubkey=form.cleaned_data['last_hop_pubkey']).first()
target_alias = target_channel.alias if target_channel.alias != '' else target_channel.remote_pubkey[:12]
if form.cleaned_data['last_hop_pubkey'] != '':
target_channel = Channels.objects.filter(is_active=True, is_open=True, remote_pubkey=form.cleaned_data['last_hop_pubkey']).first()
target_alias = target_channel.alias if target_channel.alias != '' else target_channel.remote_pubkey[:12]
else:
target_alias = ''
fee_limit = round(form.cleaned_data['fee_limit']*form.cleaned_data['value']*0.000001, 3)
Rebalancer(value=form.cleaned_data['value'], fee_limit=fee_limit, outgoing_chan_ids=str(chan_ids).replace('\'', ''), last_hop_pubkey=form.cleaned_data['last_hop_pubkey'], target_alias=target_alias, duration=form.cleaned_data['duration'], manual=True).save()
messages.success(request, 'Rebalancer request created!')
Expand Down
8 changes: 5 additions & 3 deletions rebalancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ def update_channels(stub, incoming_channel, outgoing_channel):
def auto_schedule() -> List[Rebalancer]:
try:
#No rebalancer jobs have been scheduled, lets look for any channels with an auto_rebalance flag and make the best request if we find one
to_schedule = []
if LocalSettings.objects.filter(key='AR-Enabled').exists():
enabled = int(LocalSettings.objects.filter(key='AR-Enabled')[0].value)
else:
Expand Down Expand Up @@ -223,14 +224,13 @@ def auto_schedule() -> List[Rebalancer]:
LocalSettings(key='AR-Target%', value='5').save()
if not LocalSettings.objects.filter(key='AR-MaxCost%').exists():
LocalSettings(key='AR-MaxCost%', value='65').save()
to_schedule = []
for target in inbound_cans:
target_fee_rate = int(target.local_fee_rate * (target.ar_max_cost/100))
if target_fee_rate > 0 and target_fee_rate > target.remote_fee_rate:
target_value = int(target.ar_amt_target+(target.ar_amt_target*((secrets.choice(range(-1000,1001))/1000)*variance/100)))
target_fee = round(target_fee_rate*target_value*0.000001, 3) if target_fee_rate <= max_fee_rate else round(max_fee_rate*target_value*0.000001, 3)
if target_fee == 0:
return []
continue

if LocalSettings.objects.filter(key='AR-Time').exists():
target_time = int(LocalSettings.objects.filter(key='AR-Time')[0].value)
Expand All @@ -253,6 +253,7 @@ def auto_schedule() -> List[Rebalancer]:
return to_schedule
except Exception as e:
print(f"{datetime.now().strftime('%c')} : Error scheduling rebalances: {str(e)}")
return to_schedule

@sync_to_async
def auto_enable():
Expand Down Expand Up @@ -321,6 +322,7 @@ def get_pending_rebals():
shutdown_rebalancer = False
active_rebalances = []
async def async_queue_manager(rebalancer_queue):
global shutdown_rebalancer
print(f"{datetime.now().strftime('%c')} : Queue manager is starting...")
pending_rebalances, rebal_count = await get_pending_rebals()
if rebal_count > 0:
Expand All @@ -340,12 +342,12 @@ async def async_queue_manager(rebalancer_queue):
await rebalancer_queue.put(rebalance)
elif rebalancer_queue.qsize() == 0 and len(active_rebalances) == 0:
print(f"{datetime.now().strftime('%c')} : Queue is still empty, stoping the rebalancer...")
global shutdown_rebalancer
shutdown_rebalancer = True
return
await asyncio.sleep(30)
except Exception as e:
print(f"{datetime.now().strftime('%c')} : Queue manager exception: {str(e)}")
shutdown_rebalancer = True
finally:
print(f"{datetime.now().strftime('%c')} : Queue manager has shut down...")

Expand Down

0 comments on commit 6d12548

Please sign in to comment.