Exemple #1
0
def list(aggr=False):
    events_response = stub.ForwardingHistory(
        ln.ForwardingHistoryRequest(
            start_time=0,
            end_time=int(time.time()),
            index_offset=0,
            num_max_events=100000,
        )
    )
    events = []
    events_aggr = []

    for index,event in enumerate(events_response.forwarding_events):
        chan_info_in = stub.GetChanInfo(ln.ChanInfoRequest(chan_id=event.chan_id_in))
        chan_info_out = stub.GetChanInfo(ln.ChanInfoRequest(chan_id=event.chan_id_out))
        node_in,chan_in_id = getAlias(chan_info_in)
        node_out,chan_out_id = getAlias(chan_info_out)
        #print(event)
        events.append(
            {
                'index': index,
                'time':event.timestamp,
                'chan_in':chan_in_id,
                'node_in':node_in,
                'chan_out':chan_out_id,
                'node_out':node_out,
                'amt':event.amt_out
            }
        )

        events_filter = [x for x in events_aggr if x['chan_in'] == chan_in_id and x['chan_out'] == chan_out_id]

        if len(events_filter) > 0:
            index = events_aggr.index(events_filter[0])
            events_aggr[index]["count"] = events_aggr[index]["count"] + 1
            events_aggr[index]["amt"] = events_aggr[index]["amt"] + event.amt_out
        else:
            events_aggr.append(
                {
                    'count':1,
                    'chan_in':chan_in_id,
                    'node_in':node_in,
                    'chan_out':chan_out_id,
                    'node_out':node_out,
                    'amt':event.amt_out
                }
            )

    if aggr:
        for e in events_aggr:
            print(str(e['count']) + ' ' + str(e['chan_in']) + ' ' + e['node_in'] + ' --> [ ] --> ' + str(e['chan_out']) + ' ' + e['node_out'] + ' ' + str(e['amt']))
    else:
        for e in events:
            print(str(e['index'] + 1) + ' ' + datetime.datetime.fromtimestamp(e['time']).strftime("%d-%m-%Y %H:%M") + ': ' + str(e['chan_in']) + ' '  + e['node_in'] + \
            ' --> [ ] --> ' + str(e['chan_out']) + ' '  + e['node_out'] + ' amount: ' + str(e['amt']))
Exemple #2
0
def query():
    nodes = stub.DescribeGraph(ln.ChannelGraphRequest()).nodes

    content = {"nodes": nodes, "node_count": len(nodes), "routes": []}

    if request.method == "POST":
        map_data = {"edges": [], "nodes": []}

        # Add Local Node to map data
        local_node = stub.GetInfo(ln.GetInfoRequest())
        local_node_info = stub.GetNodeInfo(
            ln.NodeInfoRequest(pub_key=local_node.identity_pubkey))
        map_data["nodes"].append({
            "id": local_node_info.node.pub_key,
            "label": local_node_info.node.alias,
            "color": local_node_info.node.color,
        })

        routes_list = stub.QueryRoutes(
            ln.QueryRoutesRequest(
                pub_key=request.form["node-result"],
                amt=int(request.form["amount"]),
                num_routes=10,
            )).routes

        for route in routes_list:
            hops = []

            for hop in route.hops:
                node_info = stub.GetNodeInfo(
                    ln.NodeInfoRequest(pub_key=hop.pub_key))
                chan_info = stub.GetChanInfo(
                    ln.ChanInfoRequest(chan_id=hop.chan_id))

                node = {
                    "id": node_info.node.pub_key,
                    "label": node_info.node.alias,
                    "color": node_info.node.color,
                }
                if hop.pub_key == chan_info.node1_pub:
                    edge = {
                        "from": chan_info.node2_pub,
                        "to": chan_info.node1_pub,
                        "label": chan_info.channel_id,
                        "arrows": "to",
                        "font": "{align: 'middle'}",
                    }
                else:
                    edge = {
                        "from": chan_info.node1_pub,
                        "to": chan_info.node2_pub,
                        "label": chan_info.channel_id,
                        "arrows": "to",
                        "font": "{align: 'middle'}",
                    }

                if node not in map_data["nodes"]:
                    map_data["nodes"].append(node)
                if edge not in map_data["edges"]:
                    map_data["edges"].append(edge)

                hops.append({
                    "chan_id": hop.chan_id,
                    "chan_capacity": hop.chan_capacity,
                    "amt_to_forward": hop.amt_to_forward_msat / 1000,
                    "fee": hop.fee_msat / 1000,
                    "pub_key": hop.pub_key,
                    "node_alias": node_info.node.alias,
                    "node_color": node_info.node.color,
                })

            content["routes"].append({
                "hops":
                hops,
                "total_amt":
                route.total_amt_msat / 1000,
                "total_fees":
                route.total_fees_msat / 1000,
            })

        content.update({"map_data": json.dumps(map_data)})

        return render_template("query-success.html", **content)

    return render_template("query.html", **content)
Exemple #3
0
def channels():
    peers = []
    total_capacity = 0
    total_local_balance = 0
    total_remote_balance = 0
    total_sent = 0
    total_received = 0
    total_commit = 0
    bytes_sent = 0
    bytes_received = 0
    channel_count = 0
    scatterPlotCapacity = {
        "ids": [],
        "x": [],
        "y": [],
        "mode": "markers",
        "type": "scatter",
        "hovertext": [],
        "marker": {
            "size": 12,
            "color": []
        },
    }
    scatterPlotActivity = {
        "ids": [],
        "x": [],
        "y": [],
        "mode": "markers",
        "type": "scatter",
        "hovertext": [],
        "marker": {
            "size": 12,
            "color": []
        },
    }

    peers_response = stub.ListPeers(ln.ListPeersRequest())
    channels_response = stub.ListChannels(
        ln.ListChannelsRequest(active_only=True))

    for peer in peers_response.peers:
        try:
            node_info_response = stub.GetNodeInfo(
                ln.NodeInfoRequest(pub_key=peer.pub_key))
            node_alias = node_info_response.node.alias
            node_color = node_info_response.node.color
        except grpc.RpcError:
            node_alias = ""
            node_color = ""

        peers.append({
            "alias": node_alias,
            "color": node_color,
            "pub_key": peer.pub_key,
            "address": peer.address,
            "bytes_sent": peer.bytes_sent,
            "bytes_received": peer.bytes_recv,
            "sats_sent": peer.sat_sent,
            "sats_received": peer.sat_recv,
            "ping_time": peer.ping_time,
            "channels": [],
        })

        bytes_sent += peer.bytes_sent
        bytes_received += peer.bytes_recv

    for channel in channels_response.channels:
        peer_filter = [
            x for x in peers if x["pub_key"] == channel.remote_pubkey
        ]
        index = peers.index(peer_filter[0])

        scatterPlotCapacity["ids"].append(channel.chan_id)
        scatterPlotCapacity["y"].append(int(channel.capacity))
        scatterPlotCapacity["x"].append(
            round(100.0 * channel.local_balance / channel.capacity, 2))
        scatterPlotCapacity["hovertext"].append(peers[index]["alias"])
        scatterPlotCapacity["marker"]["color"].append(
            str(peers[index]["color"]))

        scatterPlotActivity["ids"].append(channel.chan_id)
        scatterPlotActivity["y"].append(int(channel.capacity))
        scatterPlotActivity["x"].append(
            round(
                100.0 * (channel.total_satoshis_sent +
                         channel.total_satoshis_received) / channel.capacity,
                2,
            ))
        scatterPlotActivity["hovertext"].append(peers[index]["alias"])
        scatterPlotActivity["marker"]["color"].append(
            str(peers[index]["color"]))

        try:
            chan_info = stub.GetChanInfo(
                ln.ChanInfoRequest(chan_id=channel.chan_id))
        except grpc.RpcError as e:
            e.details()

        if chan_info.node1_pub == channel.remote_pubkey:
            remote_policy = {
                "min_htlc": chan_info.node1_policy.min_htlc,
                "fee_base_msat": chan_info.node1_policy.fee_base_msat,
                "fee_rate": chan_info.node1_policy.fee_rate_milli_msat,
                "time_lock_delta": chan_info.node1_policy.time_lock_delta,
            }
            local_policy = {
                "min_htlc": chan_info.node2_policy.min_htlc,
                "fee_base_msat": chan_info.node2_policy.fee_base_msat,
                "fee_rate": chan_info.node2_policy.fee_rate_milli_msat,
                "time_lock_delta": chan_info.node2_policy.time_lock_delta,
            }
        else:
            remote_policy = {
                "min_htlc": chan_info.node2_policy.min_htlc,
                "fee_base_msat": chan_info.node2_policy.fee_base_msat,
                "fee_rate": chan_info.node2_policy.fee_rate_milli_msat,
                "time_lock_delta": chan_info.node2_policy.time_lock_delta,
            }
            local_policy = {
                "min_htlc": chan_info.node1_policy.min_htlc,
                "fee_base_msat": chan_info.node1_policy.fee_base_msat,
                "fee_rate": chan_info.node1_policy.fee_rate_milli_msat,
                "time_lock_delta": chan_info.node1_policy.time_lock_delta,
            }

        peers[index]["channels"].append({
            "active": channel.active,
            "chan_id": channel.chan_id,
            "capacity": channel.capacity,
            "commit_fee": channel.commit_fee,
            "local_balance": channel.local_balance,
            "remote_balance": channel.remote_balance,
            "sent": channel.total_satoshis_sent,
            "received": channel.total_satoshis_received,
            "local_policy": local_policy,
            "remote_policy": remote_policy,
        })

        total_capacity += channel.capacity
        total_local_balance += channel.local_balance
        total_remote_balance += channel.remote_balance
        total_sent += channel.total_satoshis_sent
        total_received += channel.total_satoshis_received
        total_commit += channel.commit_fee

    content = {
        "peers": sorted(peers, key=lambda x: x["alias"].lower()),
        "stats": {
            "total_capacity": total_capacity,
            "total_local_balance": total_local_balance,
            "total_remote_balance": total_remote_balance,
            "total_sent": total_sent,
            "total_received": total_received,
            "total_commit": total_commit,
            "bytes_sent": bytes_sent,
            "bytes_received": bytes_received,
            "channel_count": len(channels_response.channels),
            "peer_count": len(peers_response.peers),
        },
        "scatterPlotCapacity": json.dumps(scatterPlotCapacity),
        "scatterPlotActivity": json.dumps(scatterPlotActivity),
    }

    return render_template("channels.html", **content)
Exemple #4
0
def channels():
    peers = []
    total_capacity = 0
    total_local_balance = 0
    total_remote_balance = 0
    total_sent = 0
    total_received = 0
    bytes_sent = 0
    bytes_received = 0
    channel_count = 0

    peers_response = stub.ListPeers(ln.ListPeersRequest())
    channels_response = stub.ListChannels(ln.ListChannelsRequest(active_only=True))

    for peer in peers_response.peers:
        try:
            node_info_response = stub.GetNodeInfo(ln.NodeInfoRequest(pub_key=peer.pub_key))
            node_alias = node_info_response.node.alias
            node_color = node_info_response.node.color
        except grpc.RpcError:
            node_alias = ''
            node_color = ''

        peers.append({
            'alias': node_alias,
            'color': node_color,
            'pub_key': peer.pub_key,
            'address': peer.address,
            'bytes_sent': peer.bytes_sent,
            'bytes_received': peer.bytes_recv,
            'sats_sent': peer.sat_sent,
            'sats_received': peer.sat_recv,
            'ping_time': peer.ping_time,
            'channels': [],
        })

        bytes_sent += peer.bytes_sent
        bytes_received += peer.bytes_recv

    for channel in channels_response.channels:
        peer_filter = filter(lambda x: x['pub_key'] == channel.remote_pubkey, peers)
        index = peers.index(peer_filter[0])

         try:
            chan_info = stub.GetChanInfo(ln.ChanInfoRequest(chan_id=channel.chan_id))
        except grpc.RpcError as e:
            e.details()

        if chan_info.node1_pub == channel.remote_pubkey:
            remote_policy = {
                'min_htlc': chan_info.node1_policy.min_htlc,
                'fee_base_msat': chan_info.node1_policy.fee_base_msat,
                'fee_rate': chan_info.node1_policy.fee_rate_milli_msat,
                'time_lock_delta': chan_info.node1_policy.time_lock_delta,
            }
            local_policy = {
                'min_htlc': chan_info.node2_policy.min_htlc,
                'fee_base_msat': chan_info.node2_policy.fee_base_msat,
                'fee_rate': chan_info.node2_policy.fee_rate_milli_msat,
                'time_lock_delta': chan_info.node2_policy.time_lock_delta,
            }
        else:
            remote_policy = {
                'min_htlc': chan_info.node2_policy.min_htlc,
                'fee_base_msat': chan_info.node2_policy.fee_base_msat,
                'fee_rate': chan_info.node2_policy.fee_rate_milli_msat,
                'time_lock_delta': chan_info.node2_policy.time_lock_delta,
            }
            local_policy = {
                'min_htlc': chan_info.node1_policy.min_htlc,
                'fee_base_msat': chan_info.node1_policy.fee_base_msat,
                'fee_rate': chan_info.node1_policy.fee_rate_milli_msat,
                'time_lock_delta': chan_info.node1_policy.time_lock_delta,
            }

        peers[index]['channels'].append({
            'active': channel.active,
            'chan_id': channel.chan_id,
            'capacity': channel.capacity,
            'local_balance': channel.local_balance,
            'remote_balance': channel.remote_balance,
            'sent': channel.total_satoshis_sent,
            'received': channel.total_satoshis_received,
            'local_policy': local_policy,
            'remote_policy': remote_policy,
        })

        total_capacity +=  channel.capacity
        total_local_balance +=  channel.local_balance
        total_remote_balance += channel.remote_balance
        total_sent += channel.total_satoshis_sent
        total_received += channel.total_satoshis_received

    content = {
        'peers': sorted(peers, key=lambda x: x['alias'].lower()),
        'stats': {
            'total_capacity': total_capacity,
            'total_local_balance': total_local_balance,
            'total_remote_balance': total_remote_balance,
            'total_sent': total_sent,
            'total_received': total_received,
            'bytes_sent': bytes_sent,
            'bytes_received': bytes_received,
            'channel_count': len(channels_response.channels),
            'peer_count': len(peers_response.peers),
        },
    }

    return render_template('channels.html', **content)