public async Task <IActionResult> GetBlocksAsync() { if (!this.ModelState.IsValid) { return(ModelStateErrors.BuildErrorResponse(this.ModelState)); } var pageSize = 10; // Should we allow page size to be set in query? try { var currentDownloadedHeight = this.chainState.BlockStoreTip.Height; var blocks = new List <V2.Models.BlockModel>(); var chainHeader = this.chainState.BlockStoreTip; while (chainHeader != null && blocks.Count < pageSize) { var block = await this.blockStoreCache.GetBlockAsync(chainHeader.HashBlock).ConfigureAwait(false); var blockModel = new V2.Models.BlockModel(block, chainHeader.Height, this.network); blocks.Add(blockModel); chainHeader = chainHeader.Previous; } return(this.Json(blocks)); } catch (Exception e) { this.logger.LogError("Exception occurred: {0}", e.ToString()); return(ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString())); } }
//[ProducesResponseType(typeof(void), 401)] public async Task <IActionResult> GetBlockAsync(string id, [FromQuery] BlockQueryRequest query) { if (!this.ModelState.IsValid) { return(ModelStateErrors.BuildErrorResponse(this.ModelState)); } ChainedHeader chainHeader = null; if (string.IsNullOrWhiteSpace(id)) { throw new ArgumentNullException("id", "id must be block hash or block height"); } // If the id is more than 50 characters, it is likely hash and not height. if (id.Length < 50) { chainHeader = this.chain.GetBlock(int.Parse(id)); } else { chainHeader = this.chain.GetBlock(new uint256(id)); } try { Block block = await this.blockStoreCache.GetBlockAsync(chainHeader.Header.GetHash()).ConfigureAwait(false); if (block == null) { return(new NotFoundObjectResult("Block not found")); } V2.Models.BlockModel blockModel = new V2.Models.BlockModel(block, chainHeader.Height, this.network); return(this.Json(blockModel)); } catch (Exception e) { this.logger.LogError("Exception occurred: {0}", e.ToString()); return(ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString())); } }
public async Task <IActionResult> GetTransactionsAsync() { if (!this.ModelState.IsValid) { return(ModelStateErrors.BuildErrorResponse(this.ModelState)); } var pageSize = 10; // Should we allow page size to be set in query? //this.logger.LogTrace("(Hash:'{1}')", hash); try { var currentDownloadedHeight = this.chainState.BlockStoreTip.Height; var chainHeader = this.chainState.BlockStoreTip; var transactions = new List <TransactionVerboseModel>(); while (chainHeader != null && transactions.Count < pageSize) { var block = await this.blockStoreCache.GetBlockAsync(chainHeader.HashBlock).ConfigureAwait(false); var blockModel = new V2.Models.BlockModel(block, chainHeader.Height, this.network); foreach (var trx in block.Transactions) { // Since we got Chainheader and Tip available, we'll supply those in this query. That means this query will // return more metadata than specific query using transaction ID. transactions.Add(new TransactionVerboseModel(trx, this.network, chainHeader, this.chainState.BlockStoreTip)); } chainHeader = chainHeader.Previous; } return(this.Json(transactions)); } catch (Exception e) { this.logger.LogError("Exception occurred: {0}", e.ToString()); return(ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString())); } }