Пример #1
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)
Пример #2
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)