def capgraph(): capgraph = nx.graph.Graph() edge_data = zero_edge_data() set_balance(edge_data, 1, 2, 500) set_creditline(edge_data, 2, 1, 1000) # creditline given from 2 to 1 is 1000 capgraph.add_edge(1, 2, **edge_data) return capgraph
def build_graph(*, addresses: List, creditlines: List, balances: List): """build a simple graph with zero balances and creditlines between each consecutive pairs in addresses. creditlines[i] is set as the creditline given from addresses[i+1] to addresses[i]. I.e. this is setup in a way that a payment from addresses[0] to addresses[-1] could work, given that sufficient creditlines are available at each step. """ assert len(creditlines) == len(addresses) - 1 gr = nx.graph.Graph() for a, b, creditline, balance in zip(addresses, addresses[1:], creditlines, balances): edge = zero_edge_data() set_creditline(edge, b, a, creditline) # creditline given by b to a set_balance(edge, a, b, balance) gr.add_edge(a, b, **edge) return gr
def transfer_path(self, path, value, expected_fees, timestamp=0): assert value > 0 cost_accumulator = SenderPaysCostAccumulatorSnapshot( timestamp=timestamp, value=value, capacity_imbalance_fee_divisor=self.capacity_imbalance_fee_divisor, ) cost = cost_accumulator.zero() path = list(reversed(path)) for source, target in zip(path, path[1:]): edge_data = self.graph.get_edge_data(source, target) cost = cost_accumulator.total_cost_from_start_to_dst( cost, source, target, edge_data ) if cost is None: raise nx.NetworkXNoPath("no path found") new_balance = get_balance(edge_data, target, source) - value - cost[0] set_balance(edge_data, target, source, new_balance) assert expected_fees == cost[0] return cost[0]
def test_balance(data): set_balance(data, a, b, 100) assert get_balance(data, a, b) == 100 assert get_balance(data, b, a) == -100
def balance(self, balance: int): set_balance(self.data, self.a, self.b, balance)