示例#1
0
        private static LnNode GetOrCreateNode(LndRpcClient lndClient, string pubkey, CoinpanicContext db)
        {
            var    nodeFind = db.LnNodes.Where(n => n.PubKey == pubkey).Include("Channels");
            LnNode theNode;

            if (nodeFind.Count() < 1)
            {
                // no record yet of node!
                var nodeInfo = lndClient.GetNodeInfo(pubkey);
                if (nodeInfo.node == null)
                {
                    return(null);
                }

                theNode = new LnNode()
                {
                    Alias       = nodeInfo.node.alias,
                    Color       = nodeInfo.node.color,
                    last_update = nodeInfo.node.last_update,
                    PubKey      = nodeInfo.node.pub_key,
                };
                theNode.Channels = new HashSet <LnChannel>();
                db.LnNodes.Add(theNode);
                db.SaveChanges();
            }
            else
            {
                theNode = nodeFind.First();
            }

            return(theNode);
        }
示例#2
0
        private static List <LnChannelConnectionPoints> GetChanHist(LndRpcClient lndClient, CoinpanicContext db, Channel c)
        {
            List <LnChannelConnectionPoints> chanHist;
            Int64 otherchanId = Convert.ToInt64(c.chan_id);
            var   ch          = db.LnChannelHistory.Where(h => h.ChanId == otherchanId);

            if (ch.Count() > 0)
            {
                // already known - check status
                chanHist = ch.OrderByDescending(h => h.Timestamp).AsNoTracking().ToList();
            }
            else
            {
                LnNode remoteNode = GetOrCreateNode(lndClient, c.remote_pubkey, db);
                // new channel history
                LnChannelConnectionPoints newChanHist = new LnChannelConnectionPoints()
                {
                    IsConnected   = c.active,
                    LocalBalance  = Convert.ToInt64(c.local_balance),
                    RemoteBalance = Convert.ToInt64(c.remote_balance),
                    Timestamp     = DateTime.UtcNow,
                    RemoteNode    = remoteNode,
                    ChanId        = Convert.ToInt64(c.chan_id),
                };
                db.LnChannelHistory.Add(newChanHist);
                db.SaveChanges();
                chanHist = new List <LnChannelConnectionPoints>()
                {
                    newChanHist
                };
            }

            return(chanHist);
        }
        private static Function createLnFunction()
        {
            Declaration    declaration = new Declaration("ln(x)");
            EvaluationNode node        = new LnNode();
            EvaluationTree tree        = new EvaluationTree(node);

            return(new Function(declaration, tree));
        }
示例#4
0
        public List <LnNode> ExpandNetwork(List <LnNode> network, int size = 1, int param = 1)
        {
            List <LnNode> res = new List <LnNode>();
            Random        rnd = new Random();

            for (int i = 0; i < size; i++)
            {
                List <LnNode> tmp = new List <LnNode>();
                network.ForEach(n => tmp.Add(new LnNode(n.Id, n.Connections)));
                tmp.ForEach(n =>
                {
                    n.Id = $"{n.Id}C{i}";
                    List <string> nCons = new List <string>();
                    n.Connections.ForEach(c =>
                    {
                        nCons.Add($"{c}C{i}");
                    });
                    n.Connections = nCons;
                });

                int connectionsNeeded = (int)(res.Count * (param / 100d));

                for (int j = 0; j < connectionsNeeded; j++)
                {
                    LnNode netNode     = res[rnd.Next(res.Count)];
                    LnNode clusterNode = tmp[rnd.Next(tmp.Count)];

                    if (netNode.Connections.Contains(clusterNode.Id))
                    {
                        j--;
                    }
                    else
                    {
                        netNode.Connections.Add(clusterNode.Id);
                        clusterNode.Connections.Add(netNode.Id);
                    }
                }

                res = res.Concat(tmp).ToList();
            }

            return(res);
        }
示例#5
0
        public ActionResult NodeChannels()
        {
            if (DateTime.Now - LastNodeChannelsUpdate > StatusCacheTimeout)
            {
                Guid       taskid     = Guid.NewGuid();
                UpdateTask updateTask = new UpdateTask()
                {
                    id   = taskid,
                    task = new Task(() =>
                    {
                        try
                        {
                            bool useTestnet        = GetUseTestnet();
                            LndRpcClient lndClient = GetLndClient(useTestnet);
                            string pubkey          = nodeURIViewModel.Node_Pubkey;
                            if (pubkey == "") // If not already known
                            {
                                var info                     = lndClient.GetInfo();
                                pubkey                       = info.identity_pubkey;
                                nodeURIViewModel.URI         = info.uris.First();
                                nodeURIViewModel.Alias       = info.alias;
                                nodeURIViewModel.Node_Pubkey = info.identity_pubkey;
                            }

                            var channels = lndClient.GetChannels();

                            nodeChannelViewModel.channels = new List <LnChannelInfoModel>(); // Clear cache

                            using (CoinpanicContext db = new CoinpanicContext())
                            {
                                LnNode myNode = GetOrCreateNode(lndClient, nodeURIViewModel.Node_Pubkey, db);

                                //Check each channel
                                foreach (var c in channels.channels)
                                {
                                    LnChannelInfoModel channelViewModel = new LnChannelInfoModel();

                                    // Check if this is a new channel
                                    if (myNode.Channels.Where(ch => ch.ChannelId == c.chan_id).Count() < 1)
                                    {
                                        try
                                        {
                                            LnChannel thisChannel = GetOrCreateChannel(lndClient, db, c);

                                            if (thisChannel != null && !myNode.Channels.Contains(thisChannel))
                                            {
                                                myNode.Channels.Add(thisChannel);
                                                db.SaveChanges();
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            // TODO - manage errors reading channels
                                            LnChannel thisChannel = null;
                                        }
                                    }

                                    // Check if there is a history for the channel
                                    //List<LnChannelConnectionPoints> chanHist = GetChanHist(lndClient, db, c);
                                    DateTime cutoff          = DateTime.UtcNow - TimeSpan.FromDays(30);
                                    Int64 otherchanid        = Convert.ToInt64(c.chan_id);
                                    channelViewModel.History = db.LnChannelHistory
                                                               .Where(ch => ch.ChanId == otherchanid)
                                                               .Where(ch => ch.Timestamp > cutoff)
                                                               .OrderByDescending(ch => ch.Timestamp)
                                                               .Include("RemoteNode")
                                                               .Take(30)
                                                               .AsNoTracking()
                                                               .ToList();

                                    LnChannelConnectionPoints prevChanHist;
                                    if (channelViewModel.History.Count() > 0)
                                    {
                                        prevChanHist = channelViewModel.History.First();
                                    }
                                    else
                                    {
                                        prevChanHist = new LnChannelConnectionPoints()
                                        {
                                            Timestamp = DateTime.UtcNow,
                                        };
                                    }

                                    // check for changes
                                    if (prevChanHist.IsConnected != c.active ||
                                        prevChanHist.LocalBalance != Convert.ToInt64(c.local_balance) ||
                                        prevChanHist.RemoteBalance != Convert.ToInt64(c.remote_balance) ||
                                        DateTime.UtcNow - prevChanHist.Timestamp > TimeSpan.FromHours(6))
                                    {
                                        // update
                                        LnNode remoteNode = GetOrCreateNode(lndClient, c.remote_pubkey, db);
                                        LnChannelConnectionPoints newChanHist = new LnChannelConnectionPoints()
                                        {
                                            IsConnected   = c.active,
                                            LocalBalance  = Convert.ToInt64(c.local_balance),
                                            RemoteBalance = Convert.ToInt64(c.remote_balance),
                                            Timestamp     = DateTime.UtcNow,
                                            RemoteNode    = remoteNode,
                                            ChanId        = Convert.ToInt64(c.chan_id),
                                        };
                                        prevChanHist.RemoteNode = remoteNode;
                                        db.LnChannelHistory.Add(newChanHist);
                                        db.SaveChanges();
                                    }
                                    if (c.remote_balance is null)
                                    {
                                        c.remote_balance = "0";
                                    }
                                    if (c.local_balance is null)
                                    {
                                        c.local_balance = "0";
                                    }
                                    channelViewModel.ChanInfo   = c;
                                    channelViewModel.RemoteNode = prevChanHist.RemoteNode;
                                    nodeChannelViewModel.channels.Add(channelViewModel);
                                }
                            }

                            // Updates to channelinfo
                            nodeSummaryViewModel.NumChannels          = channels.channels.Count;
                            nodeSummaryViewModel.Capacity             = Convert.ToDouble(channels.channels.Sum(c => Convert.ToInt64(c.capacity))) / 100000000.0;
                            nodeSummaryViewModel.LocalCapacity        = Convert.ToDouble(channels.channels.Sum(n => Convert.ToInt64(n.local_balance))) / 100000000.0;
                            nodeSummaryViewModel.RemoteCapacity       = Convert.ToDouble(channels.channels.Sum(n => Convert.ToInt64(n.remote_balance))) / 100000000.0;
                            nodeSummaryViewModel.ActiveCapacity       = Convert.ToDouble(channels.channels.Where(c => c.active).Sum(c => Convert.ToInt64(c.capacity))) / 100000000.0;
                            nodeSummaryViewModel.ActiveLocalCapacity  = Convert.ToDouble(channels.channels.Where(c => c.active).Sum(n => Convert.ToInt64(n.local_balance))) / 100000000.0;
                            nodeSummaryViewModel.ActiveRemoteCapacity = Convert.ToDouble(channels.channels.Where(c => c.active).Sum(n => Convert.ToInt64(n.remote_balance))) / 100000000.0;

                            UpdateTaskComplete(taskid);
                        }
                        catch (Exception e)
                        {
                            // Try again on next refresh
                            LastNodeChannelsUpdate = DateTime.Now - StatusCacheTimeout;
                        }
                    }),
                };
                updateTasks.TryAdd(taskid, updateTask);
                updateTask.task.Start();

                LastNodeChannelsUpdate = DateTime.Now;
            }

            return(PartialView("NodeChannels", nodeChannelViewModel));
        }