Skip to content

Commit

Permalink
statemachine: be smarter with the CF fee remainder
Browse files Browse the repository at this point in the history
Signed-off-by: Antoine Poinsot <darosior@protonmail.com>
  • Loading branch information
darosior committed Sep 7, 2021
1 parent 1320527 commit 45f3d79
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions Model/statemachine.py
Expand Up @@ -187,9 +187,7 @@ def Vb(self, block_height):
reserve = self.fee_reserve_per_vault(block_height)
reserve_rate = self._feerate_reserve_per_vault(block_height)
t1 = (reserve - self.Vm(block_height)) / self.O_0_factor
t2 = reserve_rate * P2WPKH_INPUT_SIZE + self._feerate_to_fee(
10, "cancel", 0
)
t2 = reserve_rate * P2WPKH_INPUT_SIZE + self._feerate_to_fee(10, "cancel", 0)
return int(max(t1, t2))

def fb_coins_dist(self, block_height):
Expand Down Expand Up @@ -250,9 +248,7 @@ def is_negligible(self, coin, block_height):
assert isinstance(coin["amount"], int)
# FIXME: What is a reasonable factor of a 'negligible coin'?
reserve_rate = self._feerate_reserve_per_vault(block_height)
t1 = reserve_rate * P2WPKH_INPUT_SIZE + self._feerate_to_fee(
10, "cancel", 0
)
t1 = reserve_rate * P2WPKH_INPUT_SIZE + self._feerate_to_fee(10, "cancel", 0)
t2 = self.Vm(block_height)
minimum = min(t1, t2)
if coin["amount"] <= minimum:
Expand Down Expand Up @@ -383,6 +379,14 @@ def grab_coins_2(self, block_height):
total_to_consume = total_unprocessed + total_unallocated + total_negligible
return total_to_consume, num_inputs

def min_fbcoin_value(self, height):
"""The minimum value for a feebumping coin we create is one that allows
to pay for its inclusion at the maximum feerate AND increase the Cancel
tx fee by at least 5sat/vbyte.
"""
feerate = self._feerate_reserve_per_vault(height)
return int(feerate * P2WPKH_INPUT_SIZE + self._feerate_to_fee(5, "cancel", 0))

def consolidate_fanout(self, block_height):
"""Simulate the WT creating a consolidate-fanout (CF) tx which aims to 1) create coins from
new re-fills that enable accurate feebumping and 2) to consolidate negligible feebump coins
Expand Down Expand Up @@ -451,23 +455,43 @@ def consolidate_fanout(self, block_height):
feerate = self._estimate_smart_feerate(block_height)
except (ValueError, KeyError):
feerate = self._feerate(block_height)

cf_size = cf_tx_size(num_inputs, num_outputs)
cf_tx_fee = int(cf_size * feerate)

# If there is any remainder, use it first to pay the fee for this transaction
remainder = total_to_consume - (num_new_reserves * sum(fb_coins))
assert isinstance(remainder, int)

# Check if remainder would cover the fee for the tx, if so, add the remainder-fee to the final new coin
# If we have more than we need for the CF fee..
if remainder > cf_tx_fee:
# FIXME: i think this can be large
self.fbcoins[-1]["amount"] += remainder - cf_tx_fee
excess = remainder - cf_tx_fee

# .. First try to opportunistically add a new fb coin
# FIXME: maybe add more than one?
added_coin_value = self.min_fbcoin_value(block_height)
if excess > added_coin_value:
cf_size_w_excess = cf_tx_size(num_inputs, num_outputs + 1)
cf_tx_fee_w_excess = int(cf_size_w_excess * feerate)
if excess >= cf_tx_fee_w_excess + added_coin_value:
self.fbcoins.append(
{
"idx": self.fbcoin_count,
"amount": added_coin_value,
"allocation": None,
"processed": block_height,
}
)
self.fbcoin_count += 1
return cf_tx_fee_w_excess

# And fallback to distribute the excess across the created fb coins
increase = excess // num_outputs
for c in self.fbcoins[len(self.fbcoins) - num_outputs :]:
c["amount"] += increase
return cf_tx_fee
else:
if num_new_reserves == 1:
logging.debug(
f" CF Tx failed sice num_new_reserves = 0 (accounting for expected fee)"
f" CF Tx failed since num_new_reserves = 1 (accounting for expected fee)"
)
# Not enough in available coins to fanout to 1 complete fee_reserve, when accounting
# for the fee, so return to initial state and return 0 (as in, 0 fee paid)
Expand Down

0 comments on commit 45f3d79

Please sign in to comment.