Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Blockcore/Base/BaseFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ public sealed class BaseFeature : FullNodeFeature
/// <inheritdoc cref="IPartialValidator"/>
private readonly IPartialValidator partialValidator;

private readonly NodeDeployments nodeDeployments;

private DeploymentFlags deploymentFlags;

public BaseFeature(NodeSettings nodeSettings,
DataFolder dataFolder,
INodeLifetime nodeLifetime,
Expand All @@ -151,6 +155,7 @@ public BaseFeature(NodeSettings nodeSettings,
IBlockStore blockStore,
Network network,
ITipsManager tipsManager,
NodeDeployments nodeDeployments,
IKeyValueRepository keyValueRepo,
INodeStats nodeStats,
IBroadcasterManager broadcasterManager,
Expand All @@ -176,6 +181,7 @@ public BaseFeature(NodeSettings nodeSettings,
this.peerBanning = Guard.NotNull(peerBanning, nameof(peerBanning));
this.tipsManager = Guard.NotNull(tipsManager, nameof(tipsManager));
this.keyValueRepo = Guard.NotNull(keyValueRepo, nameof(keyValueRepo));
this.nodeDeployments = nodeDeployments;

this.peerAddressManager = Guard.NotNull(peerAddressManager, nameof(peerAddressManager));
this.peerAddressManager.PeerFilePath = this.dataFolder;
Expand Down Expand Up @@ -278,7 +284,21 @@ private async Task StartChainAsync()
await this.chainRepository.SaveAsync(this.chainIndexer).ConfigureAwait(false);

if (this.provenBlockHeaderStore != null)
{
await this.provenBlockHeaderStore.SaveAsync().ConfigureAwait(false);
}

// Get latest flags (often cached) and persist.
var flags = this.nodeDeployments.GetFlags(this.consensusManager.Tip);

if (this.deploymentFlags == null || flags.ScriptFlags != this.deploymentFlags.ScriptFlags)
{
// Update the persistent disk cache of Flags when we retrieve it.
this.keyValueRepo.SaveValueJson("deploymentflags", flags);

// Update the local cached copy used to validate against. We don't want to persist to disk unless the flags actually has changed.
this.deploymentFlags = flags;
}
},
this.nodeLifetime.ApplicationStopping,
repeatEvery: TimeSpan.FromMinutes(1.0),
Expand Down
17 changes: 15 additions & 2 deletions src/Features/Blockcore.Features.Consensus/ConsensusFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Blockcore.Networks;
using Blockcore.P2P.Protocol.Payloads;
using Blockcore.Signals;
using Blockcore.Utilities.Store;
using NBitcoin;

[assembly: InternalsVisibleTo("Blockcore.Features.Miner.Tests")]
Expand All @@ -30,27 +31,39 @@ public class ConsensusFeature : FullNodeFeature

private readonly NodeDeployments nodeDeployments;

private readonly IKeyValueRepository keyValueRepository;

public ConsensusFeature(
Network network,
IChainState chainState,
IConnectionManager connectionManager,
ISignals signals,
IConsensusManager consensusManager,
NodeDeployments nodeDeployments)
NodeDeployments nodeDeployments,
IKeyValueRepository keyValueRepository)
{
this.chainState = chainState;
this.connectionManager = connectionManager;
this.signals = signals;
this.consensusManager = consensusManager;
this.nodeDeployments = nodeDeployments;
this.keyValueRepository = keyValueRepository;

this.chainState.MaxReorgLength = network.Consensus.MaxReorgLength;
}

/// <inheritdoc />
public override Task InitializeAsync()
{
DeploymentFlags flags = this.nodeDeployments.GetFlags(this.consensusManager.Tip);
DeploymentFlags flags = this.keyValueRepository.LoadValueJson<DeploymentFlags>("deploymentflags");

if (flags == null)
{
flags = this.nodeDeployments.GetFlags(this.consensusManager.Tip);

// Update the cache of Flags when we retrieve it.
this.keyValueRepository.SaveValueJson("deploymentflags", flags);
}

if (flags.ScriptFlags.HasFlag(ScriptVerify.Witness))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Blockcore.Networks;
using Blockcore.P2P.Peer;
using Blockcore.Signals;
using Blockcore.Utilities.Store;
using Microsoft.Extensions.Logging;
using NBitcoin;

Expand Down Expand Up @@ -51,7 +52,9 @@ public PosConsensusFeature(
ILoggerFactory loggerFactory,
ICheckpoints checkpoints,
IProvenBlockHeaderStore provenBlockHeaderStore,
ConnectionManagerSettings connectionManagerSettings) : base(network, chainState, connectionManager, signals, consensusManager, nodeDeployments)
ConnectionManagerSettings connectionManagerSettings,
IKeyValueRepository keyValueRepository
) : base(network, chainState, connectionManager, signals, consensusManager, nodeDeployments, keyValueRepository)
{
this.network = network;
this.chainState = chainState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Blockcore.Interfaces;
using Blockcore.Networks;
using Blockcore.Signals;
using Blockcore.Utilities.Store;
using Microsoft.Extensions.Logging;
using NBitcoin;

Expand Down Expand Up @@ -38,7 +39,8 @@ public PowConsensusFeature(
IInitialBlockDownloadState initialBlockDownloadState,
IPeerBanning peerBanning,
ISignals signals,
ILoggerFactory loggerFactory) : base(network, chainState, connectionManager, signals, consensusManager, nodeDeployments)
ILoggerFactory loggerFactory,
IKeyValueRepository keyValueRepository) : base(network, chainState, connectionManager, signals, consensusManager, nodeDeployments, keyValueRepository)
{
this.chainState = chainState;
this.connectionManager = connectionManager;
Expand Down