-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathInitialBlockDownloadState.cs
More file actions
77 lines (61 loc) · 2.87 KB
/
InitialBlockDownloadState.cs
File metadata and controls
77 lines (61 loc) · 2.87 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
70
71
72
73
74
75
76
77
using Blockcore.Configuration.Settings;
using Blockcore.Consensus;
using Blockcore.Consensus.Checkpoints;
using Blockcore.Interfaces;
using Blockcore.NBitcoin;
using Blockcore.Networks;
using Blockcore.Utilities;
using Microsoft.Extensions.Logging;
namespace Blockcore.Base
{
/// <summary>
/// Provides IBD (Initial Block Download) state.
/// </summary>
/// <seealso cref="IInitialBlockDownloadState" />
public class InitialBlockDownloadState : IInitialBlockDownloadState
{
/// <summary>A provider of the date and time.</summary>
private readonly IDateTimeProvider dateTimeProvider;
/// <summary>Provider of block header hash checkpoints.</summary>
private readonly ICheckpoints checkpoints;
/// <summary>Information about node's chain.</summary>
private readonly IChainState chainState;
/// <summary>Instance logger.</summary>
private readonly ILogger logger;
/// <summary>Specification of the network the node runs on - regtest/testnet/mainnet.</summary>
private readonly Network network;
/// <summary>User defined consensus settings.</summary>
private readonly ConsensusSettings consensusSettings;
private int lastCheckpointHeight;
private uint256 minimumChainWork;
public InitialBlockDownloadState(IChainState chainState, Network network, ConsensusSettings consensusSettings, ICheckpoints checkpoints, ILoggerFactory loggerFactory, IDateTimeProvider dateTimeProvider)
{
Guard.NotNull(chainState, nameof(chainState));
this.network = network;
this.consensusSettings = consensusSettings;
this.chainState = chainState;
this.checkpoints = checkpoints;
this.dateTimeProvider = dateTimeProvider;
this.lastCheckpointHeight = this.checkpoints.GetLastCheckpointHeight();
this.minimumChainWork = this.network.Consensus.MinimumChainWork ?? uint256.Zero;
this.logger = loggerFactory.CreateLogger(this.GetType().FullName);
}
/// <inheritdoc />
public bool IsInitialBlockDownload()
{
// The HTTP server starts up before the blockchain database is loaded, this means ConsensusTip can at startup be null. Simply
// return IBD as true if ConsensusTip is null.
if (this.chainState.ConsensusTip == null)
{
return true;
}
if (this.lastCheckpointHeight > this.chainState.ConsensusTip.Height)
return true;
if (this.chainState.ConsensusTip.Header.BlockTime < (this.dateTimeProvider.GetUtcNow().AddSeconds(-this.consensusSettings.MaxTipAge)))
return true;
if (this.chainState.ConsensusTip.ChainWork < this.minimumChainWork)
return true;
return false;
}
}
}