Skip to content

Commit 1d23a93

Browse files
committed
- async event bus
- code style fixes
1 parent 2b1144a commit 1d23a93

File tree

84 files changed

+348
-288
lines changed

Some content is hidden

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

84 files changed

+348
-288
lines changed

.editorconfig

+6-3
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ csharp_style_pattern_local_over_anonymous_function = true : sugge
101101
csharp_style_pattern_matching_over_as_with_null_check = true : error
102102
csharp_style_pattern_matching_over_is_with_cast_check = true : error
103103
csharp_style_throw_expression = true : silent
104-
csharp_style_var_elsewhere = false : error
105-
csharp_style_var_for_built_in_types = false : error
104+
csharp_style_var_elsewhere = false : suggestion
105+
csharp_style_var_for_built_in_types = false : suggestion
106106
csharp_style_var_when_type_is_apparent = true : suggestion
107107

108108

@@ -158,7 +158,7 @@ dotnet_diagnostic.CA1040.severity = suggestion
158158
dotnet_diagnostic.CA1801.severity = suggestion
159159

160160
# Default severity for all analyzer diagnostics
161-
dotnet_analyzer_diagnostic.severity = silent
161+
#dotnet_analyzer_diagnostic.severity = silent
162162

163163
# IDE1006: Naming Styles
164164
dotnet_diagnostic.IDE1006.severity = error
@@ -168,3 +168,6 @@ dotnet_diagnostic.CS1591.severity = silent
168168

169169
# CS1587: XML comment is not placed on a valid language element
170170
dotnet_diagnostic.CS1587.severity = silent
171+
172+
# IDE0090: Use 'new(...)'
173+
csharp_style_implicit_object_creation_when_type_is_apparent = true: warning

benchmarks/MithrilShards.P2P.Benchmark/Benchmarks/DataTypes/MithrilShards/Uint256Candidates.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace MithrilShards.P2P.Benchmark.Benchmarks.DataTypes.MithrilShards
66
public class UnsafeUInt256
77
{
88
private const int EXPECTED_SIZE = 32;
9-
private static readonly NBitcoin.DataEncoders.HexEncoder _encoder = new NBitcoin.DataEncoders.HexEncoder();
9+
private static readonly NBitcoin.DataEncoders.HexEncoder _encoder = new();
1010

1111
private readonly byte[] _bytes;
1212

@@ -36,7 +36,7 @@ public override string ToString()
3636
public class UInt256As4Long
3737
{
3838
private const int EXPECTED_SIZE = 32;
39-
private static readonly NBitcoin.DataEncoders.HexEncoder _encoder = new NBitcoin.DataEncoders.HexEncoder();
39+
private static readonly NBitcoin.DataEncoders.HexEncoder _encoder = new();
4040

4141
private readonly long[] _data;
4242

@@ -67,7 +67,7 @@ public override string ToString()
6767
public class UInt256As4Jhon
6868
{
6969
private const int EXPECTED_SIZE = 32;
70-
private static readonly NBitcoin.DataEncoders.HexEncoder _encoder = new NBitcoin.DataEncoders.HexEncoder();
70+
private static readonly NBitcoin.DataEncoders.HexEncoder _encoder = new();
7171

7272
private readonly ulong _part1;
7373
private readonly ulong _part2;
@@ -97,7 +97,7 @@ public override string ToString()
9797
public class UnsafeUInt256As4Long
9898
{
9999
private const int EXPECTED_SIZE = 32;
100-
private static readonly NBitcoin.DataEncoders.HexEncoder _encoder = new NBitcoin.DataEncoders.HexEncoder();
100+
private static readonly NBitcoin.DataEncoders.HexEncoder _encoder = new();
101101

102102
private readonly ulong _data1;
103103
private readonly ulong _data2;

benchmarks/MithrilShards.P2P.Benchmark/Benchmarks/DataTypes/Neo/Helper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace MithrilShards.P2P.Benchmark.Benchmarks.DataTypes.Neo
1414
{
1515
public static class Helper
1616
{
17-
private static readonly DateTime _unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
17+
private static readonly DateTime _unixEpoch = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
1818

1919
internal static byte[] ToArray(this SecureString s)
2020
{

benchmarks/MithrilShards.P2P.Benchmark/Benchmarks/DataTypes/Neo/NEO_UInt256.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class NEO_UInt256 : UIntBase, IComparable<NEO_UInt256>, IEquatable<NEO_UI
1212
{
1313

1414
public const int LENGTH = 32;
15-
public static readonly NEO_UInt256 Zero = new NEO_UInt256();
15+
public static readonly NEO_UInt256 Zero = new();
1616

1717
private ulong _value1;
1818
private ulong _value2;
@@ -196,7 +196,7 @@ public static bool TryParse(string s, out NEO_UInt256 result)
196196

197197

198198

199-
private static readonly NBitcoin.DataEncoders.HexEncoder _encoder = new NBitcoin.DataEncoders.HexEncoder();
199+
private static readonly NBitcoin.DataEncoders.HexEncoder _encoder = new();
200200
public override string ToString()
201201
{
202202
ulong[] arr = new ulong[] { _value1, _value2, _value3, _value4 };

benchmarks/MithrilShards.P2P.Benchmark/Benchmarks/DataTypes/Neo/UInt160.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace MithrilShards.P2P.Benchmark.Benchmarks.DataTypes.Neo
1010
public class UInt160 : UIntBase, IComparable<UInt160>, IEquatable<UInt160>
1111
{
1212
public const int LENGTH = 20;
13-
public static readonly UInt160 Zero = new UInt160();
13+
public static readonly UInt160 Zero = new();
1414

1515
private ulong _value1;
1616
private ulong _value2;

benchmarks/MithrilShards.P2P.Benchmark/Benchmarks/Spot/CompareUInt256.cs

+10-14
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,20 @@ public bool AsBytes()
5050

5151
public static ReadOnlySpan<byte> DoubleSha512AsBytes(ReadOnlySpan<byte> data)
5252
{
53-
using (var sha = new SHA512Managed())
54-
{
55-
Span<byte> result = new byte[64];
56-
sha.TryComputeHash(data, result, out _);
57-
sha.TryComputeHash(result, result, out _);
58-
return result.Slice(0, 32);
59-
}
53+
using var sha = new SHA512Managed();
54+
Span<byte> result = new byte[64];
55+
sha.TryComputeHash(data, result, out _);
56+
sha.TryComputeHash(result, result, out _);
57+
return result.Slice(0, 32);
6058
}
6159

6260
public static Core.DataTypes.UInt256 DoubleSha512AsUInt256(ReadOnlySpan<byte> data)
6361
{
64-
using (var sha = new SHA512Managed())
65-
{
66-
Span<byte> result = stackalloc byte[64];
67-
sha.TryComputeHash(data, result, out _);
68-
sha.TryComputeHash(result, result, out _);
69-
return new Core.DataTypes.UInt256(result.Slice(0, 32));
70-
}
62+
using var sha = new SHA512Managed();
63+
Span<byte> result = stackalloc byte[64];
64+
sha.TryComputeHash(data, result, out _);
65+
sha.TryComputeHash(result, result, out _);
66+
return new Core.DataTypes.UInt256(result.Slice(0, 32));
7167
}
7268
}
7369
}

benchmarks/MithrilShards.P2P.Benchmark/Benchmarks/Spot/ComputeBlockHash.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ComputeBlockHash
1616
[Params(100, 1_000, 2_000)]
1717
public int N;
1818

19-
BlockHeaderHashCalculator _hashCalculator = new BlockHeaderHashCalculator(new BlockHeaderSerializer(new UInt256Serializer()));
19+
BlockHeaderHashCalculator _hashCalculator = new(new BlockHeaderSerializer(new UInt256Serializer()));
2020

2121
BlockHeader[] _headers;
2222

benchmarks/MithrilShards.P2P.Benchmark/Benchmarks/Spot/MethodInfo_vs_Delegate.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public ValueTask<bool> ProcessMessageAsync(Message2 message, CancellationToken c
3939
private MethodInfo _method;
4040
private Delegate _computedDelegate;
4141
private Func<object, object[], object> _lambdaWrapper;
42-
private readonly Message1 _messageInstance = new Message1();
43-
private readonly Message2 _messageInstance2 = new Message2();
42+
private readonly Message1 _messageInstance = new();
43+
private readonly Message2 _messageInstance2 = new();
4444

4545

4646

benchmarks/MithrilShards.P2P.Benchmark/Benchmarks/Target/Target_Study.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class Target_Study
3131
uint _blockTime = 1262152739;
3232
uint _bits = 0x1d00ffff;
3333

34-
ProofOfWorkCalculator _powCalculator = new ProofOfWorkCalculator(
34+
ProofOfWorkCalculator _powCalculator = new(
3535
new NullLogger<ProofOfWorkCalculator>(),
3636
new BitcoinMainDefinition(new BlockHeaderHashCalculator(new BlockHeaderSerializer(new UInt256Serializer()))).ConfigureConsensus(),
3737
null

benchmarks/MithrilShards.P2P.Benchmark/GlobalSuppressions.cs

+20-13
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@
33
// Project-level suppressions either have no target or are given
44
// a specific target and scoped to a namespace, type, member, etc.
55

6-
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "<Pending>")]
7-
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0008:Use explicit type", Justification = "<Pending>")]
8-
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0011:Add braces", Justification = "<Pending>")]
9-
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0009:Member access should be qualified.", Justification = "<Pending>")]
10-
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "<Pending>")]
11-
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1305:Specify IFormatProvider", Justification = "<Pending>")]
12-
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "<Pending>")]
13-
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2208:Instantiate argument exceptions correctly", Justification = "<Pending>")]
14-
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Code Quality", "IDE0051:Remove unused private members", Justification = "<Pending>")]
15-
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1823: Avoid unused private fields")]
16-
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "<Pending>")]
17-
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "CS8632:The annotation for nullable reference types should only be used in code within a nullable")]
18-
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Compiler Warning", "CS1573")]
6+
using System.Diagnostics.CodeAnalysis;
7+
8+
[assembly: SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "<Pending>")]
9+
[assembly: SuppressMessage("Style", "IDE0008:Use explicit type", Justification = "<Pending>")]
10+
[assembly: SuppressMessage("Style", "IDE0011:Add braces", Justification = "<Pending>")]
11+
[assembly: SuppressMessage("Style", "IDE0009:Member access should be qualified.", Justification = "<Pending>")]
12+
[assembly: SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "<Pending>")]
13+
[assembly: SuppressMessage("Globalization", "CA1305:Specify IFormatProvider", Justification = "<Pending>")]
14+
[assembly: SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "<Pending>")]
15+
[assembly: SuppressMessage("Usage", "CA2208:Instantiate argument exceptions correctly", Justification = "<Pending>")]
16+
[assembly: SuppressMessage("Code Quality", "IDE0051:Remove unused private members", Justification = "<Pending>")]
17+
[assembly: SuppressMessage("Performance", "CA1823: Avoid unused private fields")]
18+
[assembly: SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "<Pending>")]
19+
[assembly: SuppressMessage("Style", "CS8632:The annotation for nullable reference types should only be used in code within a nullable")]
20+
[assembly: SuppressMessage("Compiler Warning", "CS1573")]
21+
[assembly: SuppressMessage("Style", "IDE0041:Use 'is null' check", Justification = "<Pending>")]
22+
[assembly: SuppressMessage("Style", "IDE0032:Use auto property", Justification = "<Pending>")]
23+
[assembly: SuppressMessage("Style", "IDE0090:Use 'new(...)'", Justification = "<Pending>")]
24+
[assembly: SuppressMessage("Style", "IDE0056:Use index operator", Justification = "<Pending>")]
25+
[assembly: SuppressMessage("Style", "IDE0047:Remove unnecessary parentheses", Justification = "<Pending>")]

docs/mithril-shards/default-forge.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ After all shards are initialized, they are started by invoking their `StartAsync
1717

1818
From this moment, the forge is running.
1919

20-
When the application lifetime runs out (in the default scenario by pressing CTRL+C when running in console) `StopAsync` method is called and it calls `StopAsync` on all running shards allowing them to close properly.
20+
When the application lifetime ends (in the default scenario by pressing CTRL+C when running in console) `StopAsync` method is called, which calls `StopAsync` on all running shards allowing them to close properly.

docs/mithril-shards/event-bus.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Event Bus
3+
description: Mithril Shards implementation, Event Bus
4+
5+
---
6+
7+
--8<-- "refs.txt"
8+
9+
Event Bus is a simple implementation of the [publish/subscribe pattern](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern){:target="_blank"} which is a messaging pattern that allows two or more actors to publish messages and handle these messages without having a direct relationship between them.
10+
11+
Publishers don't know if the message they are publishing is handled and by whom, while subscribers do not know who was the publisher of a specific event: this means that components among Mithril Shards can be [loosely coupled](https://en.wikipedia.org/wiki/Loose_coupling){:target="_blank"}
12+

docs/mithril-shards/forge-builder.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ description: Mithril Shards implementation, ForgeBuilder class
77

88
ForgeBuilder class represents the entry point of a Mithril Shards application, it allows to add a shard by calling the generic `AddShard` method, with different overloads that accept an optional strongly typed shard setting file with an optional setting file validator.
99

10-
By using `ConfigureLogging` it's possible to configure logging, it's basically a wrapper on the inner hostbiulder ConfigureLogging method, you could use it to have a finer control over logging configuration and available providers, but the easier way to log is by using the available [SerilogShard] that uses Serilog to configure the logging and relies on a configurable setting file where you can specify which sink to use.
10+
By using `ConfigureLogging` it's possible to configure logging, it's basically a wrapper on the inner [HostBuilder ConfigureLogging](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.hosting.webhostbuilder.configurelogging?view=aspnetcore-1.1&viewFallbackFrom=aspnetcore-5.0){:target="_blank"} method, you could use it to have a finer control over logging configuration and available providers, but the easier way to log is by using the available [SerilogShard] that uses Serilog to configure the logging and relies on a configurable setting file where you can specify which sink to use.
1111
You can find more details on its specific documentation page and an example of its usage in the [example project]
1212

1313
After declaring an instance we have to specify which implementation of Forge we want to use.
1414
Actually the only available implementation is [DefaultForge] class.
1515

1616

1717

18-

mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ nav:
107107
- mithril-shards/shards.md
108108
- mithril-shards/forge-builder.md
109109
- mithril-shards/default-forge.md
110+
- mithril-shards/event-bus.md
110111
- Shards:
111112
- BedrockNetwork:
112113
- shards/bedrock-network/index.md

src/MithrilShards.Chain.Bitcoin.Dev/Controllers/BlockFetcherManagerController.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class BlockFetcherManagerController : MithrilControllerBase
1414
private readonly ILogger<ConsensusController> _logger;
1515
readonly IBlockFetcherManager _blockFetcherManager;
1616

17-
public BlockFetcherManagerController(ILogger<ConsensusController> logger, IEventBus eventBus, IBlockFetcherManager blockFetcherManager)
17+
public BlockFetcherManagerController(ILogger<ConsensusController> logger, IBlockFetcherManager blockFetcherManager)
1818
{
1919
_logger = logger;
2020
_blockFetcherManager = blockFetcherManager;

0 commit comments

Comments
 (0)