コード例 #1
0
def deanonymize_c(G, target, path, amt, fuzz):
    pq = PriorityQueue()
    cost_function = pf.c_cost_fun(fuzz)
    delays = {}
    costs = {}
    paths = nd.nested_dict()
    paths1 = nd.nested_dict()
    dists = {}
    visited = set()
    previous = {}
    done = {}
    # prob = {}
    sources = []
    pre = path[0]
    adv = path[1]
    nxt = path[2]
    for node in G.nodes():
        previous[node] = -1
        delays[node] = -1
        costs[node] = max
        paths[node] = []
        dists[node] = max
        done[node] = 0
        paths1[node] = []
        # prob[node] = 1
    dists[target] = 0
    paths[target] = [target]
    costs[target] = amt
    delays[target] = 0
    pq.put((dists[target], target))
    flag1 = 0
    flag2 = 0
    while (0 != pq.qsize()):
        curr_cost, curr = pq.get()
        if curr_cost > dists[curr]:
            continue
        visited.add(curr)
        for [v, curr] in G.in_edges(curr):
            if (G.edges[v, curr]["Balance"] + G.edges[curr, v]["Balance"] >=
                    costs[curr]) and v not in visited:
                if done[v] == 0 and G.nodes[v]["Tech"] == 1:
                    paths1[v] = [v] + paths[curr]
                    done[v] = 1
                cost = dists[curr] + cost_function(G, costs[curr], curr, v)
                if cost < dists[v]:
                    paths[v] = [v] + paths[curr]
                    dists[v] = cost
                    delays[v] = delays[curr] + G.edges[v, curr]["Delay"]
                    costs[v] = costs[curr] + G.edges[v, curr][
                        "BaseFee"] + costs[curr] * G.edges[v, curr]["FeeRate"]
                    # prob[v] = pf.edge_prob(G.edges[v,curr]["LastFailure"])*prob[curr]
                    pq.put((dists[v], v))
        if (curr in path[1:]):
            ind = path.index(curr)
            if (paths[curr] != path[ind:]):
                return []
            if curr == adv:
                flag1 = 1
        # if flag1 == 1:
        #     print("path", paths[adv])
        if (curr == pre):
            if paths[pre] != path:
                return [pre]
            else:
                sources.append(pre)
            flag2 = 1
        if flag1 == 1 and flag2 == 1:
            if pre in paths[curr]:
                for [v, curr] in G.in_edges(curr):
                    if v not in paths[curr]:
                        sources.append(v)
    sources = list(set(sources))
    return sources
 if (i % 5 == 1):
     amt = rn.randint(1, 10)
 elif (i % 5 == 2):
     amt = rn.randint(10, 100)
 elif (i % 5 == 3):
     amt = rn.randint(100, 1000)
 elif (i % 5 == 4):
     amt = rn.randint(1000, 10000)
 else:
     amt = rn.randint(10000, 100000)
 #print(u, v, amt,G1.nodes[u]["Tech"])
 if (G1.nodes[u]["Tech"] == 0):
     path, delay, amount, dist = pf.Dijkstra(G1, u, v, amt, pf.lnd_cost_fun)
 elif (G1.nodes[u]["Tech"] == 1):
     fuzz = rn.uniform(-1, 1)
     path, delay, amount, dist = pf.Dijkstra(G1, u, v, amt, pf.c_cost_fun(fuzz))
 else:
     paths = pf.Dijkstra_general(G1, u, v, amt, pf.eclair_cost_fun)
     if (paths[0] == []):
         continue
     if len(paths[0]) == 2:
         path = paths[0]
         r = 0
     else:
         r = rn.randint(0, 2)
         path = paths[r]
     delay = 0
     amount = amt
     #print(path, r)
     if (len(path) > 2):
         for m in range(len(path) - 2, 0, -1):
コード例 #3
0
def deanonymize_c(G, target, path, amt, fuzz):
    pq = PriorityQueue()
    cost_function = pf.c_cost_fun(fuzz)
    delays = {}
    costs = {}
    paths = nd.nested_dict()
    paths1 = nd.nested_dict()
    dists = {}
    visited = set()
    previous = {}
    done = {}
    # prob = {}
    sources = []
    pre = path[0]
    adv = path[1]
    nxt = path[2]
    for node in G.nodes():
        previous[node] = -1
        delays[node] = -1
        costs[node] = max
        paths[node] = []
        dists[node] = max
        done[node] = 0
        paths1[node] = []
        # prob[node] = 1
    dists[target] = 0
    paths[target] = [target]
    costs[target] = amt
    delays[target] = 0
    pq.put((dists[target], target))
    flag1 = 0
    flag2 = 0
    while (0 != pq.qsize()):
        curr_cost, curr = pq.get()
        if curr_cost > dists[curr]:
            continue
        visited.add(curr)
        for [v, curr] in G.in_edges(curr):
            if (G.edges[v, curr]["Balance"] + G.edges[curr, v]["Balance"] >=
                    costs[curr]) and v not in visited:
                if done[v] == 0 and G.nodes[v]["Tech"] == 1:
                    paths1[v] = [v] + paths[curr]
                    done[v] = 1
                cost = dists[curr] + cost_function(G, costs[curr], curr, v)
                if cost < dists[v]:
                    paths[v] = [v] + paths[curr]
                    dists[v] = cost
                    delays[v] = delays[curr] + G.edges[v, curr]["Delay"]
                    costs[v] = costs[curr] + G.edges[v, curr][
                        "BaseFee"] + costs[curr] * G.edges[v, curr]["FeeRate"]
                    # prob[v] = pf.edge_prob(G.edges[v,curr]["LastFailure"])*prob[curr]
                    pq.put((dists[v], v))
        # If at any point the sub-path found is not found to be optimal, this is definetely not the destination if using lnd since the sub-path from an intermediary to
        # the destination has to be the cheapest path from the intermediary to the destination.
        if (curr in path[1:]):
            ind = path.index(curr)
            if (paths[curr] != path[ind:]):
                return []
            if curr == adv:
                flag1 = 1
        if (curr == pre):
            # If pre is the source, the path from pre need to not match the path found since, the cost from the source to the second node is computed differently.
            # Moreover, the source would not choose the absolute cheapest path since the first hop may not have sufficient forward balance.
            # Thus, pre has to be the source if the paths dont match, since the paths would only match if pre is an intermediary.
            if paths[pre] != path:
                return [pre]
            else:
                # if the paths do match, pre is just one possible source
                sources.append(pre)
            flag2 = 1
        if flag1 == 1 and flag2 == 1:
            # since if pre is in the path from curr, the path from pre has to match the path we had found as it is the cheapest path from pre. This measns that curr
            # is a valid second node. So, all neighbors of curr that have not occured in the path are potential sources.
            if pre in paths[curr]:
                for [v, curr] in G.in_edges(curr):
                    if v not in paths[curr] and G.nodes[v]["Tech"] == 1:
                        sources.append(v)
    sources = list(set(sources))
    return sources