Skip to content

Commit 11eeb36

Browse files
authored
Move ProtocolVersion to the Consensus class (#126)
* use uint instead of ProtocolVersion unem * Creating a ConsensusProtocol type * Remove default ProtocolVersion from BitcoinSerializable * Act on review * Move ConsensusFactory to the BitcoinStream constructor
1 parent dd28205 commit 11eeb36

92 files changed

Lines changed: 689 additions & 853 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Blockcore/BlockPulling/BlockPuller.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public BlockPuller(IChainState chainState, NodeSettings nodeSettings, IDateTimeP
225225

226226
this.networkPeerRequirement = new NetworkPeerRequirement
227227
{
228-
MinVersion = nodeSettings.MinProtocolVersion ?? nodeSettings.ProtocolVersion,
228+
MinVersion = nodeSettings.MinProtocolVersion ?? nodeSettings.Network.Consensus.ConsensusProtocol.ProtocolVersion,
229229
RequiredServices = NetworkPeerServices.Network
230230
};
231231

src/Blockcore/Configuration/NodeSettings.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ public static string NormalizeDirectorySeparator(this string path)
3535
/// </summary>
3636
public class NodeSettings : IDisposable
3737
{
38-
/// <summary>The version of the protocol supported by the current implementation of the Full Node.</summary>
39-
public const ProtocolVersion SupportedProtocolVersion = ProtocolVersion.SENDHEADERS_VERSION;
40-
4138
/// <summary>A factory responsible for creating a Full Node logger instance.</summary>
4239
public ILoggerFactory LoggerFactory { get; private set; }
4340

@@ -76,11 +73,8 @@ public class NodeSettings : IDisposable
7673
/// </summary>
7774
public TextFileConfiguration ConfigReader { get; private set; }
7875

79-
/// <summary>The version of the protocol supported by the Full Node.</summary>
80-
public ProtocolVersion ProtocolVersion { get; private set; }
81-
8276
/// <summary>The lowest version of the protocol which the Full Node supports.</summary>
83-
public ProtocolVersion? MinProtocolVersion { get; set; }
77+
public uint? MinProtocolVersion { get; set; }
8478

8579
/// <summary>The network which the node is configured to run on. The network can be a "mainnet", "testnet",
8680
/// or "regtest" network. All three network configurations can be defined, and one is selected at the command
@@ -126,23 +120,22 @@ public class NodeSettings : IDisposable
126120
/// - Alternatively, if the file name is not supplied then a network-specific file
127121
/// name would be determined. In this case we first need to determine the network.
128122
/// </remarks>
129-
public NodeSettings(Network network = null, ProtocolVersion protocolVersion = SupportedProtocolVersion,
130-
string agent = "Blockcore", string[] args = null, NetworksSelector networksSelector = null)
123+
public NodeSettings(Network network = null, string agent = "Blockcore",
124+
string[] args = null, NetworksSelector networksSelector = null)
131125
{
132126
// Create the default logger factory and logger.
133127
var loggerFactory = ExtendedLoggerFactory.Create();
134128
this.Logger = loggerFactory.CreateLogger(typeof(NodeSettings).FullName);
135129

136130
// Record arguments.
137131
this.Network = network;
138-
this.ProtocolVersion = protocolVersion;
139132
this.Agent = agent;
140133
this.ConfigReader = new TextFileConfiguration(args ?? new string[] { });
141134

142135
// Log arguments.
143136
this.Logger.LogDebug("Arguments: network='{0}', protocolVersion='{1}', agent='{2}', args='{3}'.",
144137
this.Network == null ? "(None)" : this.Network.Name,
145-
this.ProtocolVersion,
138+
this.Network?.Consensus.ConsensusProtocol.ProtocolVersion,
146139
this.Agent,
147140
args == null ? "(None)" : string.Join(" ", args));
148141

@@ -251,9 +244,9 @@ public bool PrintHelpAndExit
251244
/// <param name="network">Specification of the network the node runs on - regtest/testnet/mainnet.</param>
252245
/// <param name="protocolVersion">Supported protocol version for which to create the configuration.</param>
253246
/// <returns>Default node configuration.</returns>
254-
public static NodeSettings Default(Network network, ProtocolVersion protocolVersion = SupportedProtocolVersion)
247+
public static NodeSettings Default(Network network)
255248
{
256-
return new NodeSettings(network, protocolVersion);
249+
return new NodeSettings(network);
257250
}
258251

259252
/// <summary>

src/Blockcore/Connection/ConnectionManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public ConnectionManager(IDateTimeProvider dateTimeProvider,
134134

135135
this.Parameters.UserAgent = $"{this.ConnectionSettings.Agent}:{versionProvider.GetVersion()}";
136136

137-
this.Parameters.Version = this.NodeSettings.ProtocolVersion;
137+
this.Parameters.Version = this.NodeSettings.Network.Consensus.ConsensusProtocol.ProtocolVersion;
138138

139139
nodeStats.RegisterStats(this.AddComponentStats, StatsType.Component, this.GetType().Name, 1100);
140140
}

src/Blockcore/Controllers/NodeController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public IActionResult Status()
141141
var model = new StatusModel
142142
{
143143
Version = this.fullNode.Version?.ToString() ?? "0",
144-
ProtocolVersion = (uint)(this.nodeSettings.ProtocolVersion),
144+
ProtocolVersion = (uint)(this.nodeSettings.Network.Consensus.ConsensusProtocol.ProtocolVersion),
145145
Difficulty = GetNetworkDifficulty(this.networkDifficulty)?.Difficulty ?? 0,
146146
Agent = this.connectionManager.ConnectionSettings.Agent,
147147
ExternalAddress = this.selfEndpointTracker.MyExternalAddress.Address.ToString(),

src/Blockcore/P2P/Peer/INetworkPeer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public interface INetworkPeer : IDisposable
5454
/// <summary>
5555
/// The negotiated protocol version (minimum of supported version between <see cref="MyVersion"/> and the <see cref="PeerVersion"/>).
5656
/// </summary>
57-
ProtocolVersion Version { get; }
57+
uint Version { get; }
5858

5959
/// <summary><c>true</c> if the connection to the peer is considered active, <c>false</c> otherwise, including any case of error.</summary>
6060
bool IsConnected { get; }

src/Blockcore/P2P/Peer/NetworkPeer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class NetworkPeerDisconnectReason
5959
public class NetworkPeerRequirement
6060
{
6161
/// <summary>Minimal protocol version that the peer must support or <c>null</c> if there is no requirement for minimal protocol version.</summary>
62-
public ProtocolVersion? MinVersion { get; set; }
62+
public uint? MinVersion { get; set; }
6363

6464
/// <summary>Specification of network services that the peer must provide.</summary>
6565
public NetworkPeerServices RequiredServices { get; set; }
@@ -179,14 +179,14 @@ public PerformanceCounter Counter
179179
}
180180

181181
/// <inheritdoc/>
182-
public ProtocolVersion Version
182+
public uint Version
183183
{
184184
get
185185
{
186-
ProtocolVersion peerVersion = this.PeerVersion == null ? this.MyVersion.Version : this.PeerVersion.Version;
187-
ProtocolVersion myVersion = this.MyVersion.Version;
186+
uint peerVersion = this.PeerVersion == null ? this.MyVersion.Version : this.PeerVersion.Version;
187+
uint myVersion = this.MyVersion.Version;
188188
uint min = Math.Min((uint)peerVersion, (uint)myVersion);
189-
return (ProtocolVersion)min;
189+
return min;
190190
}
191191
}
192192

src/Blockcore/P2P/Peer/NetworkPeerConnection.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,9 @@ public async Task ConnectAsync(IPEndPoint endPoint, CancellationToken cancellati
250250

251251
using (var ms = new MemoryStream())
252252
{
253-
message.ReadWrite(new BitcoinStream(ms, true)
253+
message.ReadWrite(new BitcoinStream(ms, true, this.network.Consensus.ConsensusFactory, this.peer.Version)
254254
{
255-
ProtocolVersion = this.peer.Version,
256255
TransactionOptions = this.peer.SupportedTransactionOptions,
257-
ConsensusFactory = this.network.Consensus.ConsensusFactory
258256
});
259257

260258
byte[] bytes = ms.ToArray();
@@ -337,13 +335,13 @@ public async Task ConnectAsync(IPEndPoint endPoint, CancellationToken cancellati
337335
/// <returns>Binary message received from the connected counterparty.</returns>
338336
/// <exception cref="OperationCanceledException">Thrown if the operation was cancelled or the end of the stream was reached.</exception>
339337
/// <exception cref="ProtocolViolationException">Thrown if the incoming message is too big.</exception>
340-
private async Task<byte[]> ReadMessageAsync(ProtocolVersion protocolVersion, CancellationToken cancellation = default(CancellationToken))
338+
private async Task<byte[]> ReadMessageAsync(uint protocolVersion, CancellationToken cancellation = default(CancellationToken))
341339
{
342340
// First find and read the magic.
343341
await this.ReadMagicAsync(this.network.MagicBytes, cancellation).ConfigureAwait(false);
344342

345343
// Then read the header, which is formed of command, length, and possibly also a checksum.
346-
int checksumSize = protocolVersion >= ProtocolVersion.MEMPOOL_GD_VERSION ? Message.ChecksumSize : 0;
344+
int checksumSize = Message.ChecksumSize;
347345
int headerSize = Message.CommandSize + Message.LengthSize + checksumSize;
348346

349347
var messageHeader = new byte[headerSize];
@@ -455,7 +453,7 @@ private async Task ReadMagicAsync(byte[] magic, CancellationToken cancellation)
455453
/// for parsing the message from binary data. That method need stream to read from, so to achieve that we create a memory stream from our data,
456454
/// which is not efficient. This should be improved.
457455
/// </remarks>
458-
private async Task<(Message Message, int RawMessageSize)> ReadAndParseMessageAsync(ProtocolVersion protocolVersion, CancellationToken cancellation)
456+
private async Task<(Message Message, int RawMessageSize)> ReadAndParseMessageAsync(uint protocolVersion, CancellationToken cancellation)
459457
{
460458
Message message = null;
461459

src/Blockcore/P2P/Peer/NetworkPeerConnectionParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class NetworkPeerConnectionParameters
1515
/// <summary>Send addr unsollicited message of the AddressFrom peer when passing to Handshaked state.</summary>
1616
public bool Advertize { get; set; }
1717

18-
public ProtocolVersion Version { get; set; }
18+
public uint Version { get; set; }
1919

2020
/// <summary>If true, the node will receive all incoming transactions if no bloomfilter are set.</summary>
2121
public bool IsRelay { get; set; }

src/Blockcore/P2P/Peer/NetworkPeerFactory.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public interface INetworkPeerFactory
3838
/// <param name="cancellation">Cancallation token that allows to interrupt establishing of the connection.</param>
3939
/// <param name="networkPeerDisposer">Maintains a list of connected peers and ensures their proper disposal. Or <c>null</c> if case disposal should be handled from user code.</param>
4040
/// <returns>Network peer connected to the specified counterparty.</returns>
41-
Task<INetworkPeer> CreateConnectedNetworkPeerAsync(string endPoint, ProtocolVersion myVersion = ProtocolVersion.PROTOCOL_VERSION, bool isRelay = true, CancellationToken cancellation = default(CancellationToken), NetworkPeerDisposer networkPeerDisposer = null);
41+
Task<INetworkPeer> CreateConnectedNetworkPeerAsync(string endPoint, uint myVersion = ProtocolVersion.PROTOCOL_VERSION, bool isRelay = true, CancellationToken cancellation = default(CancellationToken), NetworkPeerDisposer networkPeerDisposer = null);
4242

4343
/// <summary>
4444
/// Creates a new network peer which is connected to a specified counterparty.
@@ -57,7 +57,7 @@ public interface INetworkPeerFactory
5757
/// <param name="externalEndPoint">IP address and port that the server is reachable from the Internet on.</param>
5858
/// <param name="version">Version of the network protocol that the server should run.</param>
5959
/// <returns>Newly created network peer server, which is ready to be started.</returns>
60-
NetworkPeerServer CreateNetworkPeerServer(IPEndPoint localEndPoint, IPEndPoint externalEndPoint, ProtocolVersion version = ProtocolVersion.PROTOCOL_VERSION);
60+
NetworkPeerServer CreateNetworkPeerServer(IPEndPoint localEndPoint, IPEndPoint externalEndPoint, uint version = ProtocolVersion.PROTOCOL_VERSION);
6161

6262
/// <summary>
6363
/// Creates a new representation of the network connection using TCP client object.
@@ -102,6 +102,7 @@ public class NetworkPeerFactory : INetworkPeerFactory
102102

103103
/// <summary>Configuration related to incoming and outgoing connections.</summary>
104104
private readonly ConnectionManagerSettings connectionManagerSettings;
105+
105106
private readonly IAsyncProvider asyncProvider;
106107

107108
/// <summary>Callback that is invoked just before a message is to be sent to a peer, or <c>null</c> when nothing needs to be called.</summary>
@@ -165,7 +166,7 @@ public INetworkPeer CreateNetworkPeer(TcpClient client, NetworkPeerConnectionPar
165166
/// <inheritdoc/>
166167
public async Task<INetworkPeer> CreateConnectedNetworkPeerAsync(
167168
string endPoint,
168-
ProtocolVersion myVersion = ProtocolVersion.PROTOCOL_VERSION,
169+
uint myVersion = ProtocolVersion.PROTOCOL_VERSION,
169170
bool isRelay = true,
170171
CancellationToken cancellation = default(CancellationToken),
171172
NetworkPeerDisposer networkPeerDisposer = null)
@@ -214,7 +215,7 @@ public async Task<INetworkPeer> CreateConnectedNetworkPeerAsync(
214215
}
215216

216217
/// <inheritdoc/>
217-
public NetworkPeerServer CreateNetworkPeerServer(IPEndPoint localEndPoint, IPEndPoint externalEndPoint, ProtocolVersion version = ProtocolVersion.PROTOCOL_VERSION)
218+
public NetworkPeerServer CreateNetworkPeerServer(IPEndPoint localEndPoint, IPEndPoint externalEndPoint, uint version = ProtocolVersion.PROTOCOL_VERSION)
218219
{
219220
Guard.NotNull(localEndPoint, nameof(localEndPoint));
220221
Guard.NotNull(externalEndPoint, nameof(externalEndPoint));

src/Blockcore/P2P/Peer/NetworkPeerServer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class NetworkPeerServer : IDisposable
2929
public Network Network { get; private set; }
3030

3131
/// <summary>Version of the protocol that the server is running.</summary>
32-
public ProtocolVersion Version { get; private set; }
32+
public uint Version { get; private set; }
3333

3434
/// <summary>The parameters that will be cloned and applied for each peer connecting to <see cref="NetworkPeerServer"/>.</summary>
3535
public NetworkPeerConnectionParameters InboundNetworkPeerConnectionParameters { get; set; }
@@ -82,7 +82,7 @@ public class NetworkPeerServer : IDisposable
8282
public NetworkPeerServer(Network network,
8383
IPEndPoint localEndPoint,
8484
IPEndPoint externalEndPoint,
85-
ProtocolVersion version,
85+
uint version,
8686
ILoggerFactory loggerFactory,
8787
INetworkPeerFactory networkPeerFactory,
8888
IInitialBlockDownloadState initialBlockDownloadState,

0 commit comments

Comments
 (0)