public static IActionResult Run(HttpRequestMessage req, CloudTable chainTable, string network, int height, TraceWriter log)
        {
            if (!ValidNetwork(network))
            {
                return(new NotFoundObjectResult(Resources.NetworkNotFound));
            }

            var rowKey = RowKeyHelper.HeightToString(height);

            var filter = TableQuery.CombineFilters(
                TableQuery.GenerateFilterCondition(PartitionKey, QueryComparisons.Equal, network.ToLowerInvariant()),
                TableOperators.And,
                TableQuery.GenerateFilterCondition(RowKey, QueryComparisons.Equal, rowKey));

            var tableQuery = new TableQuery()
            {
                FilterString = filter,
                TakeCount    = 1
            };

            var results     = chainTable.ExecuteQuery(tableQuery);
            var chainHeader = results.Select(e => new ChainHeaderModel(e)).FirstOrDefault();

            if (chainHeader == null)
            {
                return(new NotFoundObjectResult(Resources.BlockNotFound));
            }

            var block = chainHeader.BlockHeaders.Last();

            return(new JsonPrettyResult(new BlockHeaderModel(block)));
        }
Example #2
0
        public static async Task <IActionResult> Run(HttpRequestMessage req, CloudTable summaryTable, string network, int take, int skip, TraceWriter log)
        {
            if (!ValidNetwork(network))
            {
                return(new NotFoundObjectResult(Resources.NetworkNotFound));
            }

            var tip = GetTableHeight(summaryTable);

            if (tip == null)
            {
                return(new NotFoundResult());
            }

            var fromOffset = Math.Max(tip.Height - skip, 0);
            var toOffset   = Math.Max(fromOffset - (take - 1), 0);

            var from = RowKeyHelper.HeightToString(fromOffset);
            var to   = RowKeyHelper.HeightToString(toOffset);

            var partitionFilter = TableQuery.GenerateFilterCondition(PartitionKey, QueryComparisons.Equal, network.ToLowerInvariant());

            var pageFilter = TableQuery.CombineFilters(
                TableQuery.GenerateFilterCondition(RowKey, QueryComparisons.GreaterThanOrEqual, from), TableOperators.And,
                TableQuery.GenerateFilterCondition(RowKey, QueryComparisons.LessThanOrEqual, to));

            var pageQuery = new TableQuery()
            {
                FilterString = TableQuery.CombineFilters(partitionFilter, TableOperators.And, pageFilter),
                TakeCount    = take
            };

            var pageResult = await summaryTable.ExecuteQueryAsync(pageQuery);

            var results = pageResult.Select(e => new BlockSummaryModel(e))
                          .ToList();

            return(new JsonPrettyResult(results));
        }