public IActionResult GetBlockHeader([FromQuery] string hash, bool isJsonFormat = true) { try { Guard.NotEmpty(hash, nameof(hash)); this.logger.LogDebug("GetBlockHeader {0}", hash); if (!isJsonFormat) { this.logger.LogError("Binary serialization is not supported."); throw new NotImplementedException(); } BlockHeaderModel model = null; BlockHeader blockHeader = this.chainIndexer?.GetHeader(uint256.Parse(hash))?.Header; if (blockHeader != null) { model = new BlockHeaderModel(blockHeader); } return(this.Json(model)); } catch (Exception e) { this.logger.LogError("Exception occurred: {0}", e.ToString()); return(ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString())); } }
public object GetBlockHeader(string hash, bool isJsonFormat = true) { Guard.NotNull(hash, nameof(hash)); this.logger.LogDebug("RPC GetBlockHeader {0}", hash); BlockHeaderModel model = null; if (this.ChainIndexer != null) { BlockHeader blockHeader = this.ChainIndexer.GetHeader(uint256.Parse(hash))?.Header; if (blockHeader != null) { if (isJsonFormat) { return(new BlockHeaderModel(blockHeader)); } else { return(new HexModel(blockHeader.ToHex(this.Network))); } } } return(null); }
public BlockHeaderModel GetBlockHeader(string hash, bool isJsonFormat = true) { Guard.NotNull(hash, nameof(hash)); this.logger.LogDebug("RPC GetBlockHeader {0}", hash); if (!isJsonFormat) { this.logger.LogError("Binary serialization is not supported for RPC '{0}'.", nameof(this.GetBlockHeader)); throw new NotImplementedException(); } BlockHeaderModel model = null; if (this.Chain != null) { var blockHeader = this.Chain.GetBlock(uint256.Parse(hash))?.Header; if (blockHeader != null) { model = new BlockHeaderModel(blockHeader); } } return(model); }
public void GetBlockHeader_ChainNull_ReturnsNull() { this.chain = null; this.controller = new FullNodeController(this.LoggerFactory.Object, this.pooledTransaction.Object, this.pooledGetUnspentTransaction.Object, this.getUnspentTransaction.Object, this.networkDifficulty.Object, this.consensusLoop.Object, this.fullNode.Object, this.nodeSettings, this.network, this.chain, this.chainState.Object, this.connectionManager.Object); BlockHeaderModel result = this.controller.GetBlockHeader("", true); Assert.Null(result); }
public void GetBlockHeader_BlockHeaderNotFound_ReturnsNull() { string hash = new uint256(2562).ToString(); bool isJsonFormat = true; var json = (JsonResult)this.controller.GetBlockHeader(hash, isJsonFormat); BlockHeaderModel resultModel = (BlockHeaderModel)json.Value; Assert.Null(resultModel); }
public void GetBlockHeader_BlockHeaderFound_ReturnsBlockHeaderModel() { ChainedHeader block = this.chain.GetBlock(2); string bits = GetBlockHeaderBits(block.Header); BlockHeaderModel result = this.controller.GetBlockHeader(block.HashBlock.ToString(), true); Assert.NotNull(result); Assert.Equal((uint)block.Header.Version, result.Version); Assert.Equal(block.Header.HashPrevBlock.ToString(), result.PreviousBlockHash); Assert.Equal(block.Header.HashMerkleRoot.ToString(), result.MerkleRoot); Assert.Equal(block.Header.Time, result.Time); Assert.Equal((int)block.Header.Nonce, result.Nonce); Assert.Equal(bits, result.Bits); }
public void GetBlockHeader_BlockHeaderFound_ReturnsBlockHeaderModel() { var block = this.chain.GetBlock(2); var bits = GetBlockHeaderBits(block.Header); string hash = block.HashBlock.ToString(); bool isJsonFormat = true; var json = (JsonResult)this.controller.GetBlockHeader(hash, isJsonFormat); BlockHeaderModel resultModel = (BlockHeaderModel)json.Value; Assert.NotNull(resultModel); Assert.Equal((uint)block.Header.Version, resultModel.Version); Assert.Equal(block.Header.HashPrevBlock.ToString(), resultModel.PreviousBlockHash); Assert.Equal(block.Header.HashMerkleRoot.ToString(), resultModel.MerkleRoot); Assert.Equal(block.Header.Time, resultModel.Time); Assert.Equal((int)block.Header.Nonce, resultModel.Nonce); Assert.Equal(bits, resultModel.Bits); }
public BlockHeaderModel GetBlockHeader([FromQuery] string hash) { try { Guard.NotEmpty(hash, nameof(hash)); this.logger.LogDebug("GetBlockHeader {0}", hash); BlockHeaderModel model = null; BlockHeader blockHeader = this.chainIndexer?.GetHeader(uint256.Parse(hash))?.Header; if (blockHeader != null) { model = new BlockHeaderModel(blockHeader); } return(model); } catch (Exception e) { this.logger.LogError("Exception occurred: {0}", e.ToString()); return(null); } }
public void GetBlockHeader_BlockHeaderNotFound_ReturnsNull() { BlockHeaderModel result = this.controller.GetBlockHeader(new uint256(2562).ToString(), true); Assert.Null(result); }