Skip to content

Commit 9d523b3

Browse files
committed
Improve performance by avoiding methods
- Avoid calling ToHex and ToString (ID) in the constructor. - Calling the ToString to get ID only in the get method, which means it will only be called during serialization or upon request. - Related to #269
1 parent 5114f10 commit 9d523b3

3 files changed

Lines changed: 15 additions & 18 deletions

File tree

src/Blockcore/Connection/Broadcasting/BroadcastTransactionStateChanedEntry.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ namespace Blockcore.Connection.Broadcasting
77
{
88
public class BroadcastTransactionStateChanedEntry
99
{
10-
[JsonIgnore]
10+
[JsonIgnore] // The "Transaction" cannot serialize for Web Socket.
1111
public Transaction Transaction { get; }
1212

13-
public string TransactionHex { get; }
14-
15-
public string TransactionId { get; }
13+
/// <summary>
14+
/// Makes the transaction ID available for Web Socket consumers.
15+
/// </summary>
16+
public string TransactionId { get { return this.Transaction.ToString(); } }
1617

1718
[JsonConverter(typeof(StringEnumConverter))]
1819
public TransactionBroadcastState TransactionBroadcastState { get; set; }
@@ -24,8 +25,6 @@ public class BroadcastTransactionStateChanedEntry
2425
public BroadcastTransactionStateChanedEntry(Transaction transaction, TransactionBroadcastState transactionBroadcastState, string errorMessage)
2526
{
2627
this.Transaction = transaction ?? throw new ArgumentNullException(nameof(transaction));
27-
this.TransactionHex = transaction.ToHex();
28-
this.TransactionId = transaction.ToString();
2928
this.TransactionBroadcastState = transactionBroadcastState;
3029
this.ErrorMessage = (errorMessage == null) ? string.Empty : errorMessage;
3130
}

src/Blockcore/EventBus/CoreEvents/TransactionReceived.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,17 @@ namespace Blockcore.EventBus.CoreEvents
1111
/// <seealso cref="EventBase" />
1212
public class TransactionReceived : EventBase
1313
{
14-
[JsonIgnore]
14+
[JsonIgnore] // The "Transaction" cannot serialize for Web Socket.
1515
public Transaction ReceivedTransaction { get; }
1616

17-
public string TransactionHex { get; set; }
18-
19-
public string TransactionId { get; set; }
17+
/// <summary>
18+
/// Makes the transaction ID available for Web Socket consumers.
19+
/// </summary>
20+
public string TransactionId { get { return this.ReceivedTransaction.ToString(); } }
2021

2122
public TransactionReceived(Transaction receivedTransaction)
2223
{
2324
this.ReceivedTransaction = receivedTransaction;
24-
this.TransactionHex = receivedTransaction.ToHex();
25-
this.TransactionId = receivedTransaction.ToString();
2625
}
2726
}
2827
}

src/Features/Blockcore.Features.Wallet/Events/TransactionFound.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@ namespace Blockcore.Features.Wallet.Events
1414
/// <seealso cref="Blockcore.EventBus.EventBase" />
1515
public class TransactionFound : EventBase
1616
{
17-
[JsonIgnore]
17+
[JsonIgnore] // The "Transaction" cannot serialize for Web Socket.
1818
public Transaction FoundTransaction { get; }
1919

20-
public string TransactionHex { get; set; }
21-
22-
public string TransactionId { get; set; }
20+
/// <summary>
21+
/// Makes the transaction ID available for Web Socket consumers.
22+
/// </summary>
23+
public string TransactionId { get { return this.FoundTransaction.ToString(); } }
2324

2425
public TransactionFound(Transaction foundTransaction)
2526
{
2627
this.FoundTransaction = foundTransaction;
27-
this.TransactionHex = foundTransaction.ToHex();
28-
this.TransactionId = foundTransaction.ToString();
2928
}
3029
}
3130
}

0 commit comments

Comments
 (0)