Skip to content

Commit

Permalink
Add an interface to override services from the network class (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangershony authored Apr 15, 2023
1 parent 729ec43 commit faa5f75
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Blockcore/Builder/FullNodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ private IServiceCollection BuildServices()
featureRegistration.BuildFeature(this.Services);
}

this.Network.FullNodeBuilderServiceOverride?.OverrideServices(this);

return this.Services;
}

Expand Down
20 changes: 20 additions & 0 deletions src/Blockcore/Builder/IFullNodeBuilderServiceOverride.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using Blockcore.Builder.Feature;
using Blockcore.Configuration;
using Blockcore.Networks;
using Blockcore.Persistence;
using Microsoft.Extensions.DependencyInjection;

namespace Blockcore.Builder
{
/// <summary>
/// Allow specific network implementation to override services.
/// </summary>
public interface IFullNodeBuilderServiceOverride
{
/// <summary>
/// Intercept the builder to override services.
/// </summary>
void OverrideServices(IFullNodeBuilder builder);
}
}
6 changes: 6 additions & 0 deletions src/Blockcore/Networks/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Threading;
using Blockcore.Builder;
using Blockcore.Consensus;
using Blockcore.Consensus.BlockInfo;
using Blockcore.Consensus.Checkpoints;
Expand Down Expand Up @@ -276,6 +277,11 @@ public byte[] MagicBytes
/// </summary>
public IStandardScriptsRegistry StandardScriptsRegistry { get; protected set; }

/// <summary>
/// Allow the DI to override services.
/// </summary>
public IFullNodeBuilderServiceOverride FullNodeBuilderServiceOverride { get; protected set; }

/// <summary>
/// Mines a new genesis block, to use with a new network.
/// Typically, 3 such genesis blocks need to be created when bootstrapping a new coin: for Main, Test and Reg networks.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Blockcore.Builder;
using Blockcore.Features.Miner.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Blockcore.Networks.Strax.Staking
{
/// <summary>
/// Full node builder allows constructing a full node using specific components.
/// </summary>
public class MiningServiceOverride : IFullNodeBuilderServiceOverride
{
public void OverrideServices(IFullNodeBuilder builder)
{
var replace = ServiceDescriptor.Singleton<IPosMinting, StraxMinting>();

builder.Services.Replace(replace);
}
}
}
3 changes: 3 additions & 0 deletions src/Networks/Blockcore.Networks.Strax/StraxMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Blockcore.Networks.Strax.Deployments;
using Blockcore.Networks.Strax.Federation;
using Blockcore.Networks.Strax.Policies;
using Blockcore.Networks.Strax.Staking;
using Blockcore.P2P;

namespace Blockcore.Networks.Strax
Expand Down Expand Up @@ -189,6 +190,8 @@ public StraxMain()

this.StandardScriptsRegistry = new StraxStandardScriptsRegistry();

this.FullNodeBuilderServiceOverride = new MiningServiceOverride();

Assert(this.DefaultBanTimeSeconds <= this.Consensus.MaxReorgLength * this.Consensus.TargetSpacing.TotalSeconds / 2);

Assert(this.Consensus.HashGenesisBlock == uint256.Parse("0xebe158d09325c470276619ebc5f7f87c98c0ed4b211c46a17a6457655811d082"));
Expand Down
3 changes: 3 additions & 0 deletions src/Networks/Blockcore.Networks.Strax/StraxRegTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Blockcore.Networks.Strax.Federation;
using Blockcore.Networks.Strax.Policies;
using Blockcore.P2P;
using Blockcore.Networks.Strax.Staking;

namespace Blockcore.Networks.Strax
{
Expand Down Expand Up @@ -174,6 +175,8 @@ public StraxRegTest()

this.StandardScriptsRegistry = new StraxStandardScriptsRegistry();

this.FullNodeBuilderServiceOverride = new MiningServiceOverride();

Assert(this.DefaultBanTimeSeconds <= this.Consensus.MaxReorgLength * this.Consensus.TargetSpacing.TotalSeconds / 2);

Assert(this.Consensus.HashGenesisBlock == uint256.Parse("0x77283cca51b83fe3bda9ce8966248613036b0dc55a707ce76ca7b79aaa9962e4"));
Expand Down
3 changes: 3 additions & 0 deletions src/Networks/Blockcore.Networks.Strax/StraxTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Blockcore.Networks.Strax.Federation;
using Blockcore.Networks.Strax.Policies;
using Blockcore.P2P;
using Blockcore.Networks.Strax.Staking;

namespace Blockcore.Networks.Strax
{
Expand Down Expand Up @@ -178,6 +179,8 @@ public StraxTest()

this.StandardScriptsRegistry = new StraxStandardScriptsRegistry();

this.FullNodeBuilderServiceOverride = new MiningServiceOverride();

Assert(this.DefaultBanTimeSeconds <= this.Consensus.MaxReorgLength * this.Consensus.TargetSpacing.TotalSeconds / 2);

Assert(this.Consensus.HashGenesisBlock == uint256.Parse("0x0000db68ff9e74fbaf7654bab4fa702c237318428fa9186055c243ddde6354ca"));
Expand Down

0 comments on commit faa5f75

Please sign in to comment.