-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathConsensusRPCController.cs
More file actions
69 lines (61 loc) · 2.65 KB
/
Copy pathConsensusRPCController.cs
File metadata and controls
69 lines (61 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using Blockcore.Base;
using Blockcore.Consensus;
using Blockcore.Consensus.Chain;
using Blockcore.Controllers;
using Blockcore.Utilities;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NBitcoin;
namespace Blockcore.Features.Consensus
{
/// <summary>
/// A <see cref="FeatureController"/> that provides API and RPC methods from the consensus loop.
/// </summary>
public class ConsensusRPCController : FeatureController
{
/// <summary>Instance logger.</summary>
private readonly ILogger logger;
public ConsensusRPCController(
ILoggerFactory loggerFactory,
IChainState chainState,
IConsensusManager consensusManager,
ChainIndexer chainIndexer)
: base(chainState: chainState, consensusManager: consensusManager, chainIndexer: chainIndexer)
{
Guard.NotNull(loggerFactory, nameof(loggerFactory));
Guard.NotNull(chainIndexer, nameof(chainIndexer));
Guard.NotNull(chainState, nameof(chainState));
this.logger = loggerFactory.CreateLogger(this.GetType().FullName);
}
/// <summary>
/// Implements the getbestblockhash RPC call.
/// </summary>
/// <returns>A <see cref="uint256"/> hash of the block at the consensus tip.</returns>
[ActionName("getbestblockhash")]
[ActionDescription("Get the hash of the block at the consensus tip.")]
public uint256 GetBestBlockHash()
{
return this.ChainState.ConsensusTip?.HashBlock;
}
/// <summary>
/// Implements the getblockhash RPC call.
/// </summary>
/// <param name="height">The requested block height.</param>
/// <returns>A <see cref="uint256"/> hash of the block at the given height. <c>Null</c> if block not found.</returns>
[ActionName("getblockhash")]
[ActionDescription("Gets the hash of the block at the given height.")]
public uint256 GetBlockHash(int height)
{
this.logger.LogDebug("GetBlockHash {0}", height);
uint256 bestBlockHash = this.ConsensusManager.Tip?.HashBlock;
ChainedHeader bestBlock = bestBlockHash == null ? null : this.ChainIndexer.GetHeader(bestBlockHash);
if (bestBlock == null)
return null;
ChainedHeader block = this.ChainIndexer.GetHeader(height);
uint256 hash = block == null || block.Height > bestBlock.Height ? null : block.HashBlock;
if (hash == null)
throw new BlockNotFoundException($"No block found at height {height}");
return hash;
}
}
}