Esempio n. 1
0
    def bucket_candidates(self, buckets, sufficient_funds):
        '''Returns a list of bucket sets.'''
        candidates = set()

        # Add all singletons
        for n, bucket in enumerate(buckets):
            if sufficient_funds([bucket]):
                candidates.add((n, ))

        # And now some random ones
        attempts = min(100, (len(buckets) - 1) * 10 + 1)
        permutation = range(len(buckets))
        for i in range(attempts):
            # Get a random permutation of the buckets, and
            # incrementally combine buckets until sufficient
            self.p.shuffle(permutation)
            bkts = []
            for count, index in enumerate(permutation):
                bkts.append(buckets[index])
                if sufficient_funds(bkts):
                    candidates.add(tuple(sorted(permutation[:count + 1])))
                    break
            else:
                raise NotEnoughFunds()

        candidates = [[buckets[n] for n in c] for c in candidates]
        return [strip_unneeded(c, sufficient_funds) for c in candidates]
Esempio n. 2
0
    def choose_buckets(self, buckets, spent_amount, fee):
        '''Spend the oldest buckets first.'''
        # Unconfirmed coins are young, not old
        adj_height = lambda height: 99999999 if height == 0 else height
        buckets.sort(
            key=lambda b: max(adj_height(coin['height']) for coin in b.coins))
        selected, value, size = [], 0, 0
        for bucket in buckets:
            selected.append(bucket)
            value += bucket.value
            size += bucket.size
            if value >= spent_amount + fee(size):
                break
        else:
            raise NotEnoughFunds()

        # Remove unneeded inputs starting with the smallest.
        selected.sort(key=lambda b: b.value)
        dropped = []
        for bucket in selected:
            if value - bucket.value >= spent_amount + fee(size - bucket.size):
                value -= bucket.value
                size -= bucket.size
                dropped.append(bucket)

        return [bucket for bucket in selected if bucket not in dropped]
Esempio n. 3
0
 def choose_buckets(self, buckets, sufficient_funds, penalty_func):
     '''Spend the oldest buckets first.'''
     # Unconfirmed coins are young, not old
     adj_height = lambda height: 99999999 if height == 0 else height
     buckets.sort(
         key=lambda b: max(adj_height(coin['height']) for coin in b.coins))
     selected = []
     for bucket in buckets:
         selected.append(bucket)
         if sufficient_funds(selected):
             return strip_unneeded(selected, sufficient_funds)
     else:
         raise NotEnoughFunds()