2018-05-25 08:03:25 +02:00
|
|
|
from random import Random
|
|
|
|
from typing import List
|
|
|
|
|
2020-01-03 04:18:49 +01:00
|
|
|
from lbry.wallet.transaction import OutputEffectiveAmountEstimator
|
2018-05-25 08:03:25 +02:00
|
|
|
|
|
|
|
MAXIMUM_TRIES = 100000
|
|
|
|
|
2020-06-04 16:18:04 +02:00
|
|
|
STRATEGIES = ['sqlite'] # sqlite coin chooser is in database.py
|
2019-06-18 00:29:17 +02:00
|
|
|
|
2019-06-21 08:15:59 +02:00
|
|
|
|
2019-06-18 00:29:17 +02:00
|
|
|
def strategy(method):
|
|
|
|
STRATEGIES.append(method.__name__)
|
|
|
|
return method
|
|
|
|
|
2018-05-25 08:03:25 +02:00
|
|
|
|
|
|
|
class CoinSelector:
|
|
|
|
|
2019-06-21 08:15:59 +02:00
|
|
|
def __init__(self, target: int, cost_of_change: int, seed: str = None) -> None:
|
2018-05-25 08:03:25 +02:00
|
|
|
self.target = target
|
|
|
|
self.cost_of_change = cost_of_change
|
|
|
|
self.exact_match = False
|
|
|
|
self.tries = 0
|
|
|
|
self.random = Random(seed)
|
2018-07-29 02:52:54 +02:00
|
|
|
if seed is not None:
|
2018-05-25 08:03:25 +02:00
|
|
|
self.random.seed(seed, version=1)
|
|
|
|
|
2019-06-21 08:15:59 +02:00
|
|
|
def select(
|
2020-01-03 04:18:49 +01:00
|
|
|
self, txos: List[OutputEffectiveAmountEstimator],
|
|
|
|
strategy_name: str = None) -> List[OutputEffectiveAmountEstimator]:
|
2019-06-21 08:15:59 +02:00
|
|
|
if not txos:
|
2018-07-29 02:52:54 +02:00
|
|
|
return []
|
2019-06-21 08:15:59 +02:00
|
|
|
available = sum(c.effective_amount for c in txos)
|
|
|
|
if self.target > available:
|
2018-07-29 02:52:54 +02:00
|
|
|
return []
|
2019-06-21 08:15:59 +02:00
|
|
|
return getattr(self, strategy_name or "standard")(txos, available)
|
|
|
|
|
|
|
|
@strategy
|
2020-01-03 04:18:49 +01:00
|
|
|
def prefer_confirmed(self, txos: List[OutputEffectiveAmountEstimator],
|
|
|
|
available: int) -> List[OutputEffectiveAmountEstimator]:
|
2018-08-03 16:41:40 +02:00
|
|
|
return (
|
2019-06-21 08:15:59 +02:00
|
|
|
self.only_confirmed(txos, available) or
|
|
|
|
self.standard(txos, available)
|
2018-08-03 16:41:40 +02:00
|
|
|
)
|
2018-05-25 08:03:25 +02:00
|
|
|
|
2019-06-18 00:29:17 +02:00
|
|
|
@strategy
|
2020-01-03 04:18:49 +01:00
|
|
|
def only_confirmed(self, txos: List[OutputEffectiveAmountEstimator],
|
|
|
|
_) -> List[OutputEffectiveAmountEstimator]:
|
2019-06-21 08:15:59 +02:00
|
|
|
confirmed = [t for t in txos if t.txo.tx_ref and t.txo.tx_ref.height > 0]
|
|
|
|
if not confirmed:
|
|
|
|
return []
|
|
|
|
confirmed_available = sum(c.effective_amount for c in confirmed)
|
|
|
|
if self.target > confirmed_available:
|
|
|
|
return []
|
|
|
|
return self.standard(confirmed, confirmed_available)
|
|
|
|
|
|
|
|
@strategy
|
2020-01-03 04:18:49 +01:00
|
|
|
def standard(self, txos: List[OutputEffectiveAmountEstimator],
|
|
|
|
available: int) -> List[OutputEffectiveAmountEstimator]:
|
2019-06-18 00:29:17 +02:00
|
|
|
return (
|
2019-06-21 08:15:59 +02:00
|
|
|
self.branch_and_bound(txos, available) or
|
|
|
|
self.closest_match(txos, available) or
|
|
|
|
self.random_draw(txos, available)
|
2019-06-18 00:29:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
@strategy
|
2020-01-03 04:18:49 +01:00
|
|
|
def branch_and_bound(self, txos: List[OutputEffectiveAmountEstimator],
|
|
|
|
available: int) -> List[OutputEffectiveAmountEstimator]:
|
2018-05-25 08:03:25 +02:00
|
|
|
# see bitcoin implementation for more info:
|
|
|
|
# https://github.com/bitcoin/bitcoin/blob/master/src/wallet/coinselection.cpp
|
|
|
|
|
2019-06-21 08:15:59 +02:00
|
|
|
txos.sort(reverse=True)
|
2018-05-25 08:03:25 +02:00
|
|
|
|
|
|
|
current_value = 0
|
2019-06-21 08:15:59 +02:00
|
|
|
current_available_value = available
|
2018-07-29 02:52:54 +02:00
|
|
|
current_selection: List[bool] = []
|
2018-05-25 08:03:25 +02:00
|
|
|
best_waste = self.cost_of_change
|
2018-07-29 02:52:54 +02:00
|
|
|
best_selection: List[bool] = []
|
2018-05-25 08:03:25 +02:00
|
|
|
|
|
|
|
while self.tries < MAXIMUM_TRIES:
|
|
|
|
self.tries += 1
|
|
|
|
|
|
|
|
backtrack = False
|
|
|
|
if current_value + current_available_value < self.target or \
|
|
|
|
current_value > self.target + self.cost_of_change:
|
|
|
|
backtrack = True
|
|
|
|
elif current_value >= self.target:
|
|
|
|
new_waste = current_value - self.target
|
|
|
|
if new_waste <= best_waste:
|
|
|
|
best_waste = new_waste
|
|
|
|
best_selection = current_selection[:]
|
|
|
|
backtrack = True
|
|
|
|
|
|
|
|
if backtrack:
|
|
|
|
while current_selection and not current_selection[-1]:
|
|
|
|
current_selection.pop()
|
2019-06-21 08:15:59 +02:00
|
|
|
current_available_value += txos[len(current_selection)].effective_amount
|
2018-05-25 08:03:25 +02:00
|
|
|
|
|
|
|
if not current_selection:
|
|
|
|
break
|
|
|
|
|
|
|
|
current_selection[-1] = False
|
2019-06-21 08:15:59 +02:00
|
|
|
utxo = txos[len(current_selection) - 1]
|
2018-05-25 08:03:25 +02:00
|
|
|
current_value -= utxo.effective_amount
|
|
|
|
|
|
|
|
else:
|
2019-06-21 08:15:59 +02:00
|
|
|
utxo = txos[len(current_selection)]
|
2018-05-25 08:03:25 +02:00
|
|
|
current_available_value -= utxo.effective_amount
|
2019-06-21 08:15:59 +02:00
|
|
|
previous_utxo = txos[len(current_selection) - 1] if current_selection else None
|
2018-07-29 02:52:54 +02:00
|
|
|
if current_selection and not current_selection[-1] and previous_utxo and \
|
2018-05-25 08:03:25 +02:00
|
|
|
utxo.effective_amount == previous_utxo.effective_amount and \
|
|
|
|
utxo.fee == previous_utxo.fee:
|
|
|
|
current_selection.append(False)
|
|
|
|
else:
|
|
|
|
current_selection.append(True)
|
|
|
|
current_value += utxo.effective_amount
|
|
|
|
|
|
|
|
if best_selection:
|
|
|
|
self.exact_match = True
|
|
|
|
return [
|
2019-06-21 08:15:59 +02:00
|
|
|
txos[i] for i, include in enumerate(best_selection) if include
|
2018-05-25 08:03:25 +02:00
|
|
|
]
|
|
|
|
|
2018-07-29 02:52:54 +02:00
|
|
|
return []
|
|
|
|
|
2019-06-18 00:29:17 +02:00
|
|
|
@strategy
|
2020-01-03 04:18:49 +01:00
|
|
|
def closest_match(self, txos: List[OutputEffectiveAmountEstimator],
|
|
|
|
_) -> List[OutputEffectiveAmountEstimator]:
|
2018-08-03 16:41:40 +02:00
|
|
|
""" Pick one UTXOs that is larger than the target but with the smallest change. """
|
|
|
|
target = self.target + self.cost_of_change
|
|
|
|
smallest_change = None
|
|
|
|
best_match = None
|
2019-06-21 08:15:59 +02:00
|
|
|
for txo in txos:
|
2018-08-03 16:41:40 +02:00
|
|
|
if txo.effective_amount >= target:
|
|
|
|
change = txo.effective_amount - target
|
|
|
|
if smallest_change is None or change < smallest_change:
|
|
|
|
smallest_change, best_match = change, txo
|
|
|
|
return [best_match] if best_match else []
|
|
|
|
|
2019-06-18 00:29:17 +02:00
|
|
|
@strategy
|
2020-01-03 04:18:49 +01:00
|
|
|
def random_draw(self, txos: List[OutputEffectiveAmountEstimator],
|
|
|
|
_) -> List[OutputEffectiveAmountEstimator]:
|
2018-08-03 16:41:40 +02:00
|
|
|
""" Accumulate UTXOs at random until there is enough to cover the target. """
|
|
|
|
target = self.target + self.cost_of_change
|
2021-08-21 06:23:27 +02:00
|
|
|
self.random.shuffle(txos, random=self.random.random)
|
2018-05-25 08:03:25 +02:00
|
|
|
selection = []
|
|
|
|
amount = 0
|
2019-06-21 08:15:59 +02:00
|
|
|
for coin in txos:
|
2018-05-25 08:03:25 +02:00
|
|
|
selection.append(coin)
|
|
|
|
amount += coin.effective_amount
|
2018-08-03 16:41:40 +02:00
|
|
|
if amount >= target:
|
2018-05-25 08:03:25 +02:00
|
|
|
return selection
|
2018-07-29 02:52:54 +02:00
|
|
|
return []
|