def get_balance(self, node, dst, edge_data): return balance_with_interests( get_balance(edge_data, node, dst), get_interest_rate(edge_data, node, dst), get_interest_rate(edge_data, dst, node), self.timestamp - get_mtime(edge_data), )
def total_cost_from_start_to_dst( self, cost_from_start_to_node, node, dst, edge_data ): if dst == self.ignore or node == self.ignore: return None if get_is_frozen(edge_data): return None sum_fees, num_hops = cost_from_start_to_node if num_hops + 1 > self.max_hops: return None # fee computation has been inlined here, since the comment in # Graph._get_fee suggests it should be as fast as possible. This means # we do have some code duplication, but I couldn't use some of the # methods in Graph anyway, because they don't take a timestamp argument # and rather use the current time. # # We should profile this at some point in time. # # We do the pathfinding in reverse, when the sender pays. In this # method this means that the payment is done from dst to node, i.e. the # order of arguments node and dst is reversed in the following code pre_balance = balance_with_interests( get_balance(edge_data, dst, node), get_interest_rate(edge_data, dst, node), get_interest_rate(edge_data, node, dst), self.timestamp - get_mtime(edge_data), ) if num_hops == 0: fee = 0 else: fee = calculate_fees_reverse( imbalance_generated=imbalance_generated( value=self.value + sum_fees, balance=pre_balance ), capacity_imbalance_fee_divisor=self.capacity_imbalance_fee_divisor, ) if sum_fees + fee > self.max_fees: return None # check that we don't exceed the creditline capacity = pre_balance + get_creditline(edge_data, node, dst) if self.value + sum_fees + fee > capacity: # creditline exceeded return None return self.Cost(fees=sum_fees + fee, num_hops=num_hops + 1)
def total_cost_from_start_to_dst( self, cost_from_start_to_node: Cost, node, dst, edge_data ): if dst == self.ignore or node == self.ignore: return None if get_is_frozen(edge_data): return None # For this case the pathfinding is not done in reverse. # # we maintain the computed fee for the previous hop, since that is only # 'paid out' when we jump to the next hop The first element in this # tuple has to be the sum of the fees not including the fee for the # previous hop, since the graph finding algorithm needs to sort by that # and not by what would be paid out if there is another hop sum_fees, num_hops, previous_hop_fee = cost_from_start_to_node if num_hops + 1 > self.max_hops: return None pre_balance = balance_with_interests( get_balance(edge_data, node, dst), get_interest_rate(edge_data, node, dst), get_interest_rate(edge_data, dst, node), self.timestamp - get_mtime(edge_data), ) fee = calculate_fees( imbalance_generated=imbalance_generated( value=self.value - sum_fees - previous_hop_fee, balance=pre_balance ), capacity_imbalance_fee_divisor=self.capacity_imbalance_fee_divisor, ) if sum_fees + previous_hop_fee > self.max_fees: return None # check that we don't exceed the creditline capacity = pre_balance + get_creditline(edge_data, dst, node) if self.value - sum_fees - previous_hop_fee > capacity: # creditline exceeded return None return self.Cost( fees=sum_fees + previous_hop_fee, num_hops=num_hops + 1, previous_hop_fee=fee, )
def test_interests(data): set_interest_rate(data, a, b, 100) set_interest_rate(data, b, a, 200) assert get_interest_rate(data, a, b) == 100 assert get_interest_rate(data, b, a) == 200
def reverse_interest_rate(self): return get_interest_rate(self.data, self.b, self.a)
def interest_rate(self): return get_interest_rate(self.data, self.a, self.b)