Exemplo n.º 1
0
 public Dataset(string source, TxoutSetInfo set)
 {
     Consensus = new List <string> {
         source
     };
     Set = set;
 }
Exemplo n.º 2
0
        public static string JsonString(this TxoutSetInfo set)
        {
            var res = JsonConvert.SerializeObject(set, Formatting.Indented);

            res = res.Replace("-1", "xXx");

            var tweet = res.Replace("{", "").Replace("}", "").Replace("  ", "").Trim();

            return(tweet);
        }
Exemplo n.º 3
0
        internal void AddSet(string key, TxoutSetInfo set)
        {
            lock (_syncLock)
            {
                if (!AggregatedData.ContainsKey(set.height))
                {
                    var obj = Startup.GetService <AggregatedDataset>();
                    obj.Height = set.height;
                    AggregatedData.Add(set.height, obj);
                    _logger.Debug("Creating dataset for block {Block}, initiated by {ApiKeysName}", obj.Height, key);
                }

                AggregatedData[set.height].Add(key, set);
            }
        }
Exemplo n.º 4
0
        public IActionResult Post([FromHeader(Name = "ApiKey")] string key, [FromBody] TxoutSetInfo obj)
        {
            var serverKey = _zonfig.ApiKeys.SingleOrDefault(a => a.Key == key);

            if (serverKey == null)
            {
                return(Unauthorized());
            }

            // censoring differences
            //obj.bogosize = -1;
            //obj.disk_size = -1;

            _state.AddSet(serverKey.Name, obj);

            return(Ok());
        }
Exemplo n.º 5
0
        public void Add(string senderKey, TxoutSetInfo set)
        {
            lock (_syncLock)
            {
                // already tweeted out this round, now add consensus tweet here
                if (Completed.HasValue)
                {
                    if (!Sets.ContainsKey(senderKey))
                    {
                        Tweet.PublishTweetInReplyTo(senderKey, TweetId);
                    }
                    Sets.Add(senderKey, set);
                    return;
                }

                if (Sets.ContainsKey(senderKey))
                {
                    Sets[senderKey] = set;
                }
                else
                {
                    Sets.Add(senderKey, set);
                }

                _logger.Debug("{ApiKeysName} for {Block}, {ApiKeyCurrent} out of {ApiKeysTotal}: {TxoutSetInfo}",
                              senderKey, set.height, Sets.Count, _zonfig.ApiKeys.Count, set.JsonString());

                if (Sets.Count == _zonfig.ApiKeys.Count)
                {
                    tweetout();
                }
                else
                {
                    // start task that will tweet after timeout if other results don't arrive
                    if (RoundTimeout == DateTimeOffset.MaxValue)
                    {
                        RoundTimeout = DateTimeOffset.UtcNow.AddSeconds(_zonfig.AggregationRoundSecs);
                        _logger.Debug("Round timeout set to {RoundTimeout}", RoundTimeout);
                    }
                }
            }
        }
Exemplo n.º 6
0
        private static void executeProcessing()
        {
            var uri  = new Uri(_zonfig.BitcoindUri);
            var cred = RPCCredentialString.Parse(_zonfig.BitcoindCred);

            var rpcClient = new RPCClient(cred, uri, Network.Main);

            try
            {
                var checkConnection = rpcClient.GetBlockCount();
            }
            catch (Exception ex) when(
                ex is HttpRequestException ||
                (ex is AggregateException && ex.InnerException is HttpRequestException)
                )
            {
                Console.WriteLine("Bitcoind not running on specified uri...");
                return;
            }

            var blocks = rpcClient.GetBlockchainInfo();

            if (blocks.Headers > blocks.Blocks)
            {
                Console.WriteLine("Bitcoind still syncing, please try again later...");
                return;
            }

            var blocksFilePath = Directory.GetCurrentDirectory() + "\\.blocks";

            if (File.Exists(blocksFilePath))
            {
                var executedForBlocks = Convert.ToUInt64(File.ReadAllText(blocksFilePath));
                if (executedForBlocks >= blocks.Blocks)
                {
                    Console.WriteLine($"We already executed Fetched for block {executedForBlocks}");
                    return;
                }
            }

            // Fetch
            var resTxoutset = rpcClient.GetTxoutSetInfo();
            var res         = new TxoutSetInfo
            {
                bestblock         = resTxoutset.Bestblock.ToString(),
                hash_serialized_2 = resTxoutset.HashSerialized2,
                height            = resTxoutset.Height,
                total_amount      = resTxoutset.TotalAmount.ToDecimal(MoneyUnit.BTC),
                transactions      = resTxoutset.Transactions,
                txouts            = resTxoutset.Txouts
            };

            var strBody = JsonConvert.SerializeObject(res, Formatting.Indented);

            //var strBody = "{\"height\":570092,\"bestblock\":\"00000000000000000026960d36e9ffe255e4bde8656a843cea2f32612b1f4b12\",\"transactions\":28714080,\"txouts\":52713092,\"hash_serialized_2\":\"914d9ebf51eac4b5875e87dc2a8ebb0c17fa188dfe4984d3416b20d9a03578fa\",\"total_amount\":17625979.82662823}";
            Console.WriteLine($"Sending to publisher:\n{strBody}");

            if (_zonfig.PublisherUrl != null)
            {
                using (var httpClientHandler = new HttpClientHandler())
                {
                    httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return(true); };
                    using (var client = new HttpClient(httpClientHandler))
                    {
                        var resp = sendTxoutSetInfo(client, strBody);
                    }
                }
            }

            File.WriteAllText(blocksFilePath, res.height.ToString());
        }