Skip to content

Commit bb88708

Browse files
Improve Monero.Common code quality
1 parent 6180f8e commit bb88708

14 files changed

Lines changed: 661 additions & 204 deletions

Monero/Common/GenUtils.cs

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Org.BouncyCastle.Utilities;
1+
using System.Text;
2+
using Org.BouncyCastle.Utilities;
23

34
namespace Monero.Common
45
{
@@ -12,26 +13,39 @@ public static string GetGuid()
1213
public static T? Reconcile<T>(T? val1, T? val2, bool? resolveDefined = null, bool? resolveTrue = null, bool? resolveMax = null)
1314
{
1415
// check for same reference
15-
if (ReferenceEquals(val1, val2)) return val1;
16+
if (ReferenceEquals(val1, val2))
17+
{
18+
return val1;
19+
}
1620

1721
int? comparison = null;
1822

1923
// check for BigInteger equality
2024
if (val1 is ulong b1 && val2 is ulong b2)
2125
{
2226
comparison = b1.CompareTo(b2);
23-
if (comparison == 0) return val1;
27+
if (comparison == 0)
28+
{
29+
return val1;
30+
}
2431
}
2532

2633
if (val1 is bool bool1 && val2 is bool bool2)
2734
{
28-
if (bool1 == bool2) return val1;
35+
if (bool1 == bool2)
36+
{
37+
return val1;
38+
}
2939
}
3040

3141
// resolve one value null
3242
if (val1 == null || val2 == null)
3343
{
34-
if (resolveDefined == false) return default!;
44+
if (resolveDefined == false)
45+
{
46+
return default!;
47+
}
48+
3549
return val1 == null ? val2 : val1;
3650
}
3751

@@ -100,27 +114,29 @@ public static string GetGuid()
100114

101115
public static int[]? Subarray(int[]? array, int startIndexInclusive, int endIndexExclusive)
102116
{
103-
if (array == null) return null;
117+
if (array == null)
118+
{
119+
return null;
120+
}
121+
104122
if (startIndexInclusive < 0) startIndexInclusive = 0;
105123
if (endIndexExclusive > array.Length) endIndexExclusive = array.Length;
106124

107125
int newSize = endIndexExclusive - startIndexInclusive;
108-
if (newSize <= 0) return [];
126+
if (newSize <= 0)
127+
{
128+
return [];
129+
}
109130

110131
int[] subarray = new int[newSize];
111132
Array.Copy(array, startIndexInclusive, subarray, 0, newSize);
112133
return subarray;
113134
}
114-
115-
public static void WaitFor(ulong durationMs) {
116-
WaitFor((int)durationMs);
117-
}
118-
135+
119136
public static void WaitFor(int durationMs)
120137
{
121138
try
122139
{
123-
// brutto, non mi piace
124140
Thread.Sleep(durationMs);
125141
}
126142
catch (ThreadInterruptedException)
@@ -129,15 +145,23 @@ public static void WaitFor(int durationMs)
129145
}
130146
}
131147

132-
public static string KvLine(object? key, object? value, int indent, bool newline = true, bool ignoreUndefined = true) {
133-
if (value == null && ignoreUndefined) return "";
148+
public static string KvLine(string key, object? value, int indent, bool newline = true, bool ignoreUndefined = true) {
149+
if (value == null && ignoreUndefined)
150+
{
151+
return "";
152+
}
153+
134154
return GetIndent(indent) + key + ": " + value + (newline ? '\n' : "");
135155
}
136156

137-
public static string GetIndent(int length) {
138-
string str = "";
139-
for (int i = 0; i < length; i++) str += " "; // two spaces
140-
return str;
157+
public static string GetIndent(int length)
158+
{
159+
var sb = new StringBuilder(length * 2);
160+
for (int i = 0; i < length; i++)
161+
{
162+
sb.Append(" "); // two spaces
163+
}
164+
return sb.ToString();
141165
}
142166
}
143167
}

Monero/Common/MoneroBlock.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,24 @@ public MoneroBlock(MoneroBlockHeader header) : base(header)
2121
public MoneroBlock(MoneroBlock block) : base(block)
2222
{
2323
_hex = block.GetHex();
24-
if (block._minerTx != null) _minerTx = block._minerTx.Clone().SetBlock(this);
24+
if (block._minerTx != null)
25+
{
26+
_minerTx = block._minerTx.Clone().SetBlock(this);
27+
}
28+
2529
if (block._txs != null)
2630
{
27-
_txs = new List<MoneroTx>();
28-
foreach (MoneroTx tx in block._txs) _txs.Add(tx.Clone().SetBlock(this));
31+
_txs = [];
32+
foreach (MoneroTx tx in block._txs)
33+
{
34+
_txs.Add(tx.Clone().SetBlock(this));
35+
}
36+
}
37+
38+
if (block.GetTxHashes() != null)
39+
{
40+
_txHashes = [..block.GetTxHashes()!];
2941
}
30-
if (block.GetTxHashes() != null) _txHashes = [..block.GetTxHashes()!];
3142
}
3243

3344
#region Override Base Methods
@@ -184,8 +195,16 @@ public MoneroBlock SetTxs(List<MoneroTx>? txs)
184195

185196
public MoneroBlock AddTx(MoneroTx? tx)
186197
{
187-
if (tx == null) throw new ArgumentNullException(nameof(tx), "Transaction cannot be null");
188-
if (_txs == null) _txs = [];
198+
if (tx == null)
199+
{
200+
throw new ArgumentNullException(nameof(tx), "Transaction cannot be null");
201+
}
202+
203+
if (_txs == null)
204+
{
205+
_txs = [];
206+
}
207+
189208
_txs.Add(tx.SetBlock(this));
190209
return this;
191210
}

Monero/Common/MoneroBlockHeader.cs

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public MoneroBlockHeader(MoneroBlockHeader header)
5252

5353
public virtual MoneroBlockHeader SetHash(string? hash)
5454
{
55-
this._hash = hash;
55+
_hash = hash;
5656
return this;
5757
}
5858

@@ -63,7 +63,7 @@ public virtual MoneroBlockHeader SetHash(string? hash)
6363

6464
public virtual MoneroBlockHeader SetHeight(ulong? height)
6565
{
66-
this._height = height;
66+
_height = height;
6767
return this;
6868
}
6969

@@ -74,7 +74,7 @@ public virtual MoneroBlockHeader SetHeight(ulong? height)
7474

7575
public virtual MoneroBlockHeader SetTimestamp(ulong? timestamp)
7676
{
77-
this._timestamp = timestamp;
77+
_timestamp = timestamp;
7878
return this;
7979
}
8080

@@ -85,7 +85,7 @@ public virtual MoneroBlockHeader SetTimestamp(ulong? timestamp)
8585

8686
public virtual MoneroBlockHeader SetSize(ulong? size)
8787
{
88-
this._size = size;
88+
_size = size;
8989
return this;
9090
}
9191

@@ -96,7 +96,7 @@ public virtual MoneroBlockHeader SetSize(ulong? size)
9696

9797
public virtual MoneroBlockHeader SetWeight(ulong? weight)
9898
{
99-
this._weight = weight;
99+
_weight = weight;
100100
return this;
101101
}
102102

@@ -107,7 +107,7 @@ public virtual MoneroBlockHeader SetWeight(ulong? weight)
107107

108108
public virtual MoneroBlockHeader SetLongTermWeight(ulong? longTermWeight)
109109
{
110-
this._longTermWeight = longTermWeight;
110+
_longTermWeight = longTermWeight;
111111
return this;
112112
}
113113

@@ -118,7 +118,7 @@ public virtual MoneroBlockHeader SetLongTermWeight(ulong? longTermWeight)
118118

119119
public virtual MoneroBlockHeader SetDepth(ulong? depth)
120120
{
121-
this._depth = depth;
121+
_depth = depth;
122122
return this;
123123
}
124124

@@ -129,7 +129,7 @@ public virtual MoneroBlockHeader SetDepth(ulong? depth)
129129

130130
public virtual MoneroBlockHeader SetDifficulty(ulong? difficulty)
131131
{
132-
this._difficulty = difficulty;
132+
_difficulty = difficulty;
133133
return this;
134134
}
135135

@@ -140,7 +140,7 @@ public virtual MoneroBlockHeader SetDifficulty(ulong? difficulty)
140140

141141
public virtual MoneroBlockHeader SetCumulativeDifficulty(ulong? cumulativeDifficulty)
142142
{
143-
this._cumulativeDifficulty = cumulativeDifficulty;
143+
_cumulativeDifficulty = cumulativeDifficulty;
144144
return this;
145145
}
146146

@@ -151,7 +151,7 @@ public virtual MoneroBlockHeader SetCumulativeDifficulty(ulong? cumulativeDiffic
151151

152152
public virtual MoneroBlockHeader SetMajorVersion(uint? majorVersion)
153153
{
154-
this._majorVersion = majorVersion;
154+
_majorVersion = majorVersion;
155155
return this;
156156
}
157157

@@ -162,7 +162,7 @@ public virtual MoneroBlockHeader SetMajorVersion(uint? majorVersion)
162162

163163
public virtual MoneroBlockHeader SetMinorVersion(uint? minorVersion)
164164
{
165-
this._minorVersion = minorVersion;
165+
_minorVersion = minorVersion;
166166
return this;
167167
}
168168

@@ -173,7 +173,7 @@ public virtual MoneroBlockHeader SetMinorVersion(uint? minorVersion)
173173

174174
public virtual MoneroBlockHeader SetNonce(ulong? nonce)
175175
{
176-
this._nonce = nonce;
176+
_nonce = nonce;
177177
return this;
178178
}
179179

@@ -184,7 +184,7 @@ public virtual MoneroBlockHeader SetNonce(ulong? nonce)
184184

185185
public virtual MoneroBlockHeader SetMinerTxHash(string? minerTxHash)
186186
{
187-
this._minerTxHash = minerTxHash;
187+
_minerTxHash = minerTxHash;
188188
return this;
189189
}
190190

@@ -195,7 +195,7 @@ public virtual MoneroBlockHeader SetMinerTxHash(string? minerTxHash)
195195

196196
public virtual MoneroBlockHeader SetNumTxs(uint? numTxs)
197197
{
198-
this._numTxs = numTxs;
198+
_numTxs = numTxs;
199199
return this;
200200
}
201201

@@ -206,7 +206,7 @@ public virtual MoneroBlockHeader SetNumTxs(uint? numTxs)
206206

207207
public virtual MoneroBlockHeader SetOrphanStatus(bool? orphanStatus)
208208
{
209-
this._orphanStatus = orphanStatus;
209+
_orphanStatus = orphanStatus;
210210
return this;
211211
}
212212

@@ -217,7 +217,7 @@ public virtual MoneroBlockHeader SetOrphanStatus(bool? orphanStatus)
217217

218218
public virtual MoneroBlockHeader SetPrevHash(string? prevHash)
219219
{
220-
this._prevHash = prevHash;
220+
_prevHash = prevHash;
221221
return this;
222222
}
223223

@@ -228,7 +228,7 @@ public virtual MoneroBlockHeader SetPrevHash(string? prevHash)
228228

229229
public virtual MoneroBlockHeader SetReward(ulong? reward)
230230
{
231-
this._reward = reward;
231+
_reward = reward;
232232
return this;
233233
}
234234

@@ -239,38 +239,54 @@ public virtual MoneroBlockHeader SetReward(ulong? reward)
239239

240240
public virtual MoneroBlockHeader SetPowHash(string? powHash)
241241
{
242-
this._powHash = powHash;
242+
_powHash = powHash;
243243
return this;
244244
}
245245

246246
public virtual MoneroBlockHeader Merge(MoneroBlockHeader? header)
247247
{
248-
if (header == null) throw new ArgumentNullException(nameof(header), "Cannot merge null header into block header");
249-
if (this == header) return this;
250-
this.SetHash(GenUtils.Reconcile(this.GetHash(), header.GetHash()));
251-
this.SetHeight(GenUtils.Reconcile(this.GetHeight(), header.GetHeight(), null, null, true)); // height can increase
252-
this.SetTimestamp(GenUtils.Reconcile(this.GetTimestamp(), header.GetTimestamp(), null, null, true)); // block timestamp can increase
253-
this.SetSize(GenUtils.Reconcile(this.GetSize(), header.GetSize()));
254-
this.SetWeight(GenUtils.Reconcile(this.GetWeight(), header.GetWeight()));
255-
this.SetDepth(GenUtils.Reconcile(this.GetDepth(), header.GetDepth()));
256-
this.SetDifficulty(GenUtils.Reconcile(this.GetDifficulty(), header.GetDifficulty()));
257-
this.SetCumulativeDifficulty(GenUtils.Reconcile(this.GetCumulativeDifficulty(), header.GetCumulativeDifficulty()));
258-
this.SetMajorVersion(GenUtils.Reconcile(this.GetMajorVersion(), header.GetMajorVersion()));
259-
this.SetMinorVersion(GenUtils.Reconcile(this.GetMinorVersion(), header.GetMinorVersion()));
260-
this.SetNonce(GenUtils.Reconcile(this.GetNonce(), header.GetNonce()));
261-
this.SetMinerTxHash(GenUtils.Reconcile(this.GetMinerTxHash(), header.GetMinerTxHash()));
262-
this.SetNumTxs(GenUtils.Reconcile(this.GetNumTxs(), header.GetNumTxs()));
263-
this.SetOrphanStatus(GenUtils.Reconcile(this.GetOrphanStatus(), header.GetOrphanStatus()));
264-
this.SetPrevHash(GenUtils.Reconcile(this.GetPrevHash(), header.GetPrevHash()));
265-
this.SetReward(GenUtils.Reconcile(this.GetReward(), header.GetReward()));
266-
this.SetPowHash(GenUtils.Reconcile(this.GetPowHash(), header.GetPowHash()));
248+
if (header == null)
249+
{
250+
throw new ArgumentNullException(nameof(header), "Cannot merge null header into block header");
251+
}
252+
253+
if (this == header)
254+
{
255+
return this;
256+
}
257+
258+
SetHash(GenUtils.Reconcile(GetHash(), header.GetHash()));
259+
SetHeight(GenUtils.Reconcile(GetHeight(), header.GetHeight(), null, null, true)); // height can increase
260+
SetTimestamp(GenUtils.Reconcile(GetTimestamp(), header.GetTimestamp(), null, null, true)); // block timestamp can increase
261+
SetSize(GenUtils.Reconcile(GetSize(), header.GetSize()));
262+
SetWeight(GenUtils.Reconcile(GetWeight(), header.GetWeight()));
263+
SetDepth(GenUtils.Reconcile(GetDepth(), header.GetDepth()));
264+
SetDifficulty(GenUtils.Reconcile(GetDifficulty(), header.GetDifficulty()));
265+
SetCumulativeDifficulty(GenUtils.Reconcile(GetCumulativeDifficulty(), header.GetCumulativeDifficulty()));
266+
SetMajorVersion(GenUtils.Reconcile(GetMajorVersion(), header.GetMajorVersion()));
267+
SetMinorVersion(GenUtils.Reconcile(GetMinorVersion(), header.GetMinorVersion()));
268+
SetNonce(GenUtils.Reconcile(GetNonce(), header.GetNonce()));
269+
SetMinerTxHash(GenUtils.Reconcile(GetMinerTxHash(), header.GetMinerTxHash()));
270+
SetNumTxs(GenUtils.Reconcile(GetNumTxs(), header.GetNumTxs()));
271+
SetOrphanStatus(GenUtils.Reconcile(GetOrphanStatus(), header.GetOrphanStatus()));
272+
SetPrevHash(GenUtils.Reconcile(GetPrevHash(), header.GetPrevHash()));
273+
SetReward(GenUtils.Reconcile(GetReward(), header.GetReward()));
274+
SetPowHash(GenUtils.Reconcile(GetPowHash(), header.GetPowHash()));
267275
return this;
268276
}
269277

270278
public virtual bool Equals(MoneroBlockHeader? other)
271279
{
272-
if (other == null) return false;
273-
if (other == this) return true;
280+
if (other == null)
281+
{
282+
return false;
283+
}
284+
285+
if (other == this)
286+
{
287+
return true;
288+
}
289+
274290
return _hash == other._hash &&
275291
_height == other._height &&
276292
_timestamp == other._timestamp &&

0 commit comments

Comments
 (0)