public BlockModel GetBlock([FromQuery] SearchByHashRequest query)
        {
            if (!this.ModelState.IsValid)
            {
                return(null);
            }

            try
            {
                uint256 blockId = uint256.Parse(query.Hash);

                ChainedHeader chainedHeader = this.chainIndexer.GetHeader(blockId);

                if (chainedHeader == null)
                {
                    return(null);
                }

                Block block = chainedHeader.Block ?? this.blockStore.GetBlock(blockId);

                // In rare occasions a block that is found in the
                // indexer may not have been pushed to the store yet.
                if (block == null)
                {
                    return(null);
                }

                BlockModel blockModel = query.ShowTransactionDetails
                    ? new BlockTransactionDetailsModel(block, chainedHeader, this.chainIndexer.Tip, this.network)
                    : new BlockModel(block, chainedHeader, this.chainIndexer.Tip, this.network);

                if (this.network.Consensus.IsProofOfStake)
                {
                    var posBlock = block as PosBlock;

                    blockModel.PosBlockSignature = posBlock.BlockSignature.ToHex(this.network);
                    blockModel.PosBlockTrust     = new Target(chainedHeader.GetBlockTarget()).ToUInt256().ToString();
                    blockModel.PosChainTrust     = chainedHeader.ChainWork.ToString(); // this should be similar to ChainWork

                    if (this.stakeChain != null)
                    {
                        BlockStake blockStake = this.stakeChain.Get(blockId);

                        blockModel.PosModifierv2 = blockStake?.StakeModifierV2.ToString();
                        blockModel.PosFlags      = blockStake?.Flags == BlockFlag.BLOCK_PROOF_OF_STAKE ? "proof-of-stake" : "proof-of-work";
                        blockModel.PosHashProof  = blockStake?.HashProof?.ToString();
                    }
                }

                return(blockModel);
            }
            catch (Exception e)
            {
                this.logger.LogError("Exception occurred: {0}", e.ToString());
                return(null);
            }
        }
Esempio n. 2
0
        public IActionResult GetBlock([FromQuery] SearchByHashRequest query)
        {
            if (!this.ModelState.IsValid)
            {
                return(ModelStateErrors.BuildErrorResponse(this.ModelState));
            }

            try
            {
                uint256 blockId = uint256.Parse(query.Hash);

                ChainedHeader chainedHeader = this.chainIndexer.GetHeader(blockId);

                if (chainedHeader == null)
                {
                    return(this.Ok("Block not found"));
                }

                Block block = chainedHeader.Block ?? this.blockStore.GetBlock(blockId);

                // In rare occasions a block that is found in the
                // indexer may not have been pushed to the store yet.
                if (block == null)
                {
                    return(this.Ok("Block not found"));
                }

                if (!query.OutputJson)
                {
                    return(this.Json(block));
                }

                BlockModel blockModel = query.ShowTransactionDetails
                    ? new BlockTransactionDetailsModel(block, chainedHeader, this.chainIndexer.Tip, this.network)
                    : new BlockModel(block, chainedHeader, this.chainIndexer.Tip, this.network);

                if (this.network.Consensus.IsProofOfStake)
                {
                    var posBlock = block as PosBlock;

                    blockModel.PosBlockSignature = posBlock.BlockSignature.ToHex(this.network.Consensus.ConsensusFactory);
                    blockModel.PosBlockTrust     = new Target(chainedHeader.GetBlockTarget()).ToUInt256().ToString();
                    blockModel.PosChainTrust     = chainedHeader.ChainWork.ToString(); // this should be similar to ChainWork
                }

                return(this.Json(blockModel));
            }
            catch (Exception e)
            {
                this.logger.LogError("Exception occurred: {0}", e.ToString());
                return(ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString()));
            }
        }
Esempio n. 3
0
        public object GetBlock(string blockHash, int verbosity = 1)
        {
            uint256 blockId = uint256.Parse(blockHash);

            // Does the block exist.
            ChainedHeader chainedHeader = this.ChainIndexer.GetHeader(blockId);

            if (chainedHeader == null)
            {
                return(null);
            }

            Block block = chainedHeader.Block ?? this.blockStore?.GetBlock(blockId);

            // In rare occasions a block that is found in the
            // indexer may not have been pushed to the store yet.
            if (block == null)
            {
                return(null);
            }

            if (verbosity == 0)
            {
                return(new HexModel(block.ToHex(this.Network.Consensus.ConsensusFactory)));
            }

            var blockModel = new BlockModel(block, chainedHeader, this.ChainIndexer.Tip, this.Network, verbosity);

            if (this.Network.Consensus.IsProofOfStake)
            {
                var posBlock = block as PosBlock;

                blockModel.PosBlockSignature = posBlock.BlockSignature.ToHex(this.Network.Consensus.ConsensusFactory);
                blockModel.PosBlockTrust     = new Target(chainedHeader.GetBlockTarget()).ToUInt256().ToString();
                blockModel.PosChainTrust     = chainedHeader.ChainWork.ToString(); // this should be similar to ChainWork

                if (this.stakeChain != null)
                {
                    BlockStake blockStake = this.stakeChain.Get(blockId);

                    blockModel.PosModifierv2 = blockStake?.StakeModifierV2.ToString();
                    blockModel.PosFlags      = blockStake?.Flags == BlockFlag.BLOCK_PROOF_OF_STAKE ? "proof-of-stake" : "proof-of-work";
                    blockModel.PosHashProof  = blockStake?.HashProof.ToString();
                }
            }

            return(blockModel);
        }