Beispiel #1
0
        public static NetworkChannel Create(ChannelAnnouncementMessage announcement, ChannelUpdateMessage update, NetworkNode node1, NetworkNode node2)
        {
            NetworkChannel networkChannel = new NetworkChannel();

            networkChannel.CreatedEntity     = DateTime.Now;
            networkChannel.Id                = announcement.ShortChannelIdHex;
            networkChannel.Node1             = node1;
            networkChannel.Node2             = node2;
            networkChannel.Node1Id           = node1.Id;
            networkChannel.Node2Id           = node2.Id;
            networkChannel.ChannelShortId    = announcement.ShortChannelId;
            networkChannel.Features          = announcement.Features;
            networkChannel.ChainHash         = announcement.ChainHash;
            networkChannel.NodeSignature1    = announcement.NodeSignature1;
            networkChannel.NodeSignature2    = announcement.NodeSignature2;
            networkChannel.BitcoinSignature1 = announcement.BitcoinSignature1;
            networkChannel.BitcoinSignature2 = announcement.BitcoinSignature2;
            networkChannel.BitcoinKey1       = announcement.BitcoinKey1.PublicKeyCompressed;
            networkChannel.BitcoinKey2       = announcement.BitcoinKey2.PublicKeyCompressed;

            if (update != null)
            {
                networkChannel.Update(update);
            }

            return(networkChannel);
        }
        public void ChannelUpdateMessageTest()
        {
            var message = new ChannelUpdateMessage();

            message.SetProperties(new List <byte[]>
            {
                "111e45454b0978a623f36a10626ef17b27d9ad44e2760f98cfa3efb37924f0220220bd8acd43ecaa916a80bd4f919c495a2c58982ce7c8625112456464451111".HexToByteArray(),
                "6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000".HexToByteArray(),
                "b34fafd163cf765b".HexToByteArray(),
                "11111111".HexToByteArray(),
                new byte[] { 1 },
                new byte[] { 2 },
                "2211".HexToByteArray(),
                "3333666611113333".HexToByteArray(),
                "55558888".HexToByteArray(),
                "66669999".HexToByteArray()
            });

            Assert.Equal(258, message.Definition.TypeId);

            Assert.Equal("111e45454b0978a623f36a10626ef17b27d9ad44e2760f98cfa3efb37924f0220220bd8acd43ecaa916a80bd4f919c495a2c58982ce7c8625112456464451111", message.Signature.ToHex());
            Assert.Equal("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000", message.ChainHash.ToHex());
            Assert.Equal("b34fafd163cf765b", message.ShortChannelId.ToHex());
            Assert.Equal(DateTime.Parse("1979-01-28T01:25:53.0000000+01:00"), message.Timestamp);
            Assert.Equal(1, message.MessageFlags);
            Assert.Equal(2, message.ChannelFlags);
            Assert.Equal(8721, message.CltvExpiryDelta);
            Assert.Equal((ulong)3689405108305605427, message.HtlcMinimumSat);
            Assert.Equal((ulong)1431668872, message.FeeBaseMsat);
            Assert.Equal((ulong)1718000025, message.FeeProportionalMillionths);

            var properties = message.GetProperties();

            Assert.Equal("111e45454b0978a623f36a10626ef17b27d9ad44e2760f98cfa3efb37924f0220220bd8acd43ecaa916a80bd4f919c495a2c58982ce7c8625112456464451111", properties[0].ToHex());
            Assert.Equal("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000", properties[1].ToHex());
            Assert.Equal("b34fafd163cf765b", properties[2].ToHex());
            Assert.Equal("11111111", properties[3].ToHex());
            Assert.Equal("01", properties[4].ToHex());
            Assert.Equal("02", properties[5].ToHex());
            Assert.Equal("2211", properties[6].ToHex());
            Assert.Equal("3333666611113333", properties[7].ToHex());
            Assert.Equal("55558888", properties[8].ToHex());
            Assert.Equal("66669999", properties[9].ToHex());

            Assert.Equal("0102" +
                         "111e45454b0978a623f36a10626ef17b27d9ad44e2760f98cfa3efb37924f0220220bd8acd43ecaa916a80bd4f919c495a2c58982ce7c8625112456464451111" +
                         "6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000" +
                         "b34fafd163cf765b" +
                         "11111111" +
                         "01" +
                         "02" +
                         "2211" +
                         "3333666611113333" +
                         "55558888" +
                         "66669999",
                         message.GetBytes().ToHex());
        }
Beispiel #3
0
 public NetworkChannel Update(ChannelUpdateMessage update)
 {
     LastUpdated = DateTime.Now;
     LastUpdateMessageTimestamp = update.Timestamp;
     ChannelFlags              = update.ChannelFlags;
     MessageFlags              = update.MessageFlags;
     CltvExpiryDelta           = update.CltvExpiryDelta;
     FeeBaseMsat               = update.FeeBaseMsat;
     FeeProportionalMillionths = update.FeeProportionalMillionths;
     HtlcMaximumSat            = update.HtlcMaximumSat;
     HtlcMinimumSat            = update.HtlcMinimumSat;
     return(this);
 }
Beispiel #4
0
        private void Run()
        {
            var channelID = Settings.PublicationChannel.Value;

            _log.Info($"Starting worker thread for {nameof(SummarizeActiveRaids)}");
            while (!_cts.IsCancellationRequested)
            {
                _log.Info("Run");
                try
                {
                    if (DateTime.UtcNow.Hour >= 21 || DateTime.UtcNow.Hour < 4)
                    {
                        _log.Trace($"Skipping summary update cycle because the server rests.");
                        continue;
                    }

                    var publishedRaids = DB.GetCollection <RaidParticipation>().Find(x => x.Raid != null && x.Raid.RaidEndTime >= DateTime.UtcNow && x.Raid.RaidUnlockTime <= (DateTime.UtcNow + TimeSpan.FromHours(1)) && x.IsPublished).ToArray();

                    List <long> channels = new List <long> {
                        Settings.PublicationChannel ?? 0
                    };
                    // (only main channel) channels.AddRange(publishedRaids.SelectMany(r => r.Raid?.Publications?.Select(x => x.ChannelID) ?? new long[] { }));

                    // Restricted to only the main channel. All other channels are managed separately by different code.
                    foreach (var channel in channels.Distinct().ToArray())
                    {
                        StringBuilder message         = new StringBuilder();
                        var           raidsForChannel = publishedRaids.Where(x => (channel == Settings.PublicationChannel) && x.IsPublished).ToArray();
                        foreach (var raid in raidsForChannel.OrderBy(x => x.Raid.RaidEndTime))
                        {
                            string url = $"{Settings.BotAddress}raids/{raid.PublicID}";

                            message.AppendLine($"{TimeService.AsShortTime(raid.Raid.RaidUnlockTime)}: {_HTML_(raid.Raid.Raid)} @ {_HTML_(raid.Raid.Gym)} ({raid.NumberOfParticipants()})\r\n<a href=\"{url}\">Online inschrijven (nieuw)</a>");
                        }

                        var updateRecord = DB.GetCollection <ChannelUpdateMessage>().Find(x => x.ChannelID == channel).FirstOrDefault();
                        if (null == updateRecord)
                        {
                            updateRecord = new ChannelUpdateMessage
                            {
                                ChannelID = channel,
                                MessageID = long.MaxValue,
                                Hash      = string.Empty
                            };
                            DB.GetCollection <ChannelUpdateMessage>().Insert(updateRecord);
                        }

                        var hash = HashUtils.CalculateSHA1Hash(message.ToString());
                        if (!string.Equals(hash, updateRecord.Hash) /*|| (DateTime.UtcNow - updateRecord.LastModificationDate > TimeSpan.FromSeconds(60))*/)
                        {
                            if (NewRaidPosted || updateRecord.MessageID == long.MaxValue)
                            {
                                NewRaidPosted = false;

                                // A new message was posted or the summary was never posted yet, delete the current message and posty a new summary
                                if (updateRecord.MessageID != long.MaxValue)
                                {
                                    try
                                    {
                                        Client.DeleteMessage(channel, updateRecord.MessageID);
                                        updateRecord.MessageID = long.MaxValue;
                                        updateRecord.Hash      = hash;
                                    }
                                    catch (Exception ex)
                                    {
                                        _log.Warn(ex, $"Could not delete summary-message {updateRecord.MessageID} from channel {channel}");
                                    }
                                }

                                try
                                {
                                    var postedMessage = Client.SendMessageToChat(channel, message.ToString(), "HTML", true, true, null, null);
                                    if (null != postedMessage)
                                    {
                                        updateRecord.MessageID = postedMessage.MessageID;
                                    }
                                    else
                                    {
                                        _log.Warn($"Could not post summary-message to channel {channel} - null reply");
                                    }
                                }
                                catch (Exception ex)
                                {
                                    _log.Warn(ex, $"Could not post summary-message to channel {channel}");
                                }
                            }
                            else if (updateRecord.MessageID != long.MaxValue)
                            {
                                // There is no new raid posted, so update the current one
                                Client.EditMessageText($"{channel}", updateRecord.MessageID, null, message.ToString(), "HTML", true, null, "channel");
                                updateRecord.Hash = hash;
                            }
                        }

                        updateRecord.LastModificationDate = DateTime.UtcNow;
                        DB.GetCollection <ChannelUpdateMessage>().Update(updateRecord);
                    }
                }
                catch (ThreadAbortException)
                {
                    _log.Info($"Abort requested for thread.");
                    break;
                }
                catch (Exception ex)
                {
                    _log.Error(ex, "Error in SummarizeActiveRaids thread. Ignoring.");
                }
                finally
                {
                    Thread.Sleep(Interval);
                }
            }
            _log.Info($"Stopped worker thread for {nameof(SummarizeActiveRaids)}");
        }