コード例 #1
0
        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           blocks      = new List <PosBlockModel>();
                ChainedHeader chainHeader = this.chain.Tip;

                while (chainHeader != null && blocks.Count < pageSize)
                {
                    var block = this.blockStoreCache.GetBlock(chainHeader.HashBlock);

                    var blockModel = new PosBlockModel(block, this.chain);
                    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()));
            }
        }
コード例 #2
0
        //[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.GetHeader(int.Parse(id));
            }
            else
            {
                chainHeader = this.chain.GetHeader(new uint256(id));
            }

            if (chainHeader == null)
            {
                return(new NotFoundObjectResult("Block not found"));
            }

            try
            {
                BlockStake blockStake = this.stakeChain.Get(chainHeader.HashBlock);
                Block      block      = this.blockStoreCache.GetBlock(chainHeader.Header.GetHash());

                if (block == null)
                {
                    return(new NotFoundObjectResult("Block not found"));
                }

                PosBlockModel blockModel = new PosBlockModel(block, this.chain);

                if (blockStake != null)
                {
                    blockModel.StakeTime       = blockStake.StakeTime;
                    blockModel.StakeModifierV2 = blockStake.StakeModifierV2;
                    blockModel.HashProof       = blockStake.HashProof;
                }

                return(this.Json(blockModel));
            }
            catch (Exception e)
            {
                this.logger.LogError("Exception occurred: {0}", e.ToString());
                return(ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString()));
            }
        }
コード例 #3
0
        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
            {
                ChainedHeader chainHeader = this.chain.Tip;

                var transactions = new List <TransactionVerboseModel>();

                while (chainHeader != null && transactions.Count < pageSize)
                {
                    Block block = this.blockStoreCache.GetBlock(chainHeader.HashBlock);

                    var blockModel = new PosBlockModel(block, this.chain);

                    foreach (Transaction 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(Json(transactions));
            }
            catch (Exception e)
            {
                this.logger.LogError("Exception occurred: {0}", e.ToString());
                return(ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString()));
            }
        }