Make lazy things lazy in SmartCoin
MODIFY
WalletWasabi/Blockchain/TransactionOutputs/SmartCoin.cs
WalletWasabi/Blockchain/TransactionOutputs/SmartCoin.cs
@@ -15,8 +15,6 @@ namespace WalletWasabi.Blockchain.TransactionOutputs
/// </summary>
public class SmartCoin : NotifyPropertyChangedBase, IEquatable<SmartCoin>
{
- #region Fields
-
private Height _height;
private int _anonymitySet;
private SmartTransaction? _spenderTransaction;
@@ -31,15 +29,24 @@ namespace WalletWasabi.Blockchain.TransactionOutputs
private bool _confirmed;
private bool _isBanned;
- #endregion Fields
-
- #region Constructors
+ private Lazy<uint256> _transactionId;
+ private Lazy<OutPoint> _outPoint;
+ private Lazy<TxOut> _txOut;
+ private Lazy<Coin> _coin;
+ private Lazy<int> _hashCode;
public SmartCoin(SmartTransaction transaction, uint outputIndex, HdPubKey pubKey, int anonymitySet)
{
Transaction = transaction;
- Coin = new Coin(transaction.Transaction, outputIndex);
- HashCode = (TransactionId, Index).GetHashCode();
+ Index = outputIndex;
+ _transactionId = new Lazy<uint256>(() => Transaction.GetHash(), true);
+
+ _outPoint = new Lazy<OutPoint>(() => new OutPoint(TransactionId, Index), true);
+ _txOut = new Lazy<TxOut>(() => Transaction.Transaction.Outputs[Index], true);
+ _coin = new Lazy<Coin>(() => new Coin(OutPoint, TxOut), true);
+
+ _hashCode = new Lazy<int>(() => OutPoint.GetHashCode(), true);
+
Height = transaction.Height;
AnonymitySet = Guard.InRangeAndNotNull(nameof(anonymitySet), anonymitySet, 1, int.MaxValue);
@@ -48,24 +55,18 @@ namespace WalletWasabi.Blockchain.TransactionOutputs
_cluster = new Cluster(this);
}
- #endregion Constructors
-
- #region Properties
-
public SmartTransaction Transaction { get; }
+ public uint Index { get; }
+ public uint256 TransactionId => _transactionId.Value;
- public Coin Coin { get; }
-
- public Script ScriptPubKey => Coin.ScriptPubKey;
-
- public Money Amount => Coin.Amount;
+ public OutPoint OutPoint => _outPoint.Value;
+ public TxOut TxOut => _txOut.Value;
+ public Coin Coin => _coin.Value;
- public OutPoint OutPoint => Coin.Outpoint;
+ public Script ScriptPubKey => TxOut.ScriptPubKey;
+ public Money Amount => TxOut.Value;
- public uint256 TransactionId => OutPoint.Hash;
-
- public uint Index => OutPoint.N;
- private int HashCode { get; }
+ private int HashCode => _hashCode.Value;
public Height Height
{
@@ -137,8 +138,6 @@ namespace WalletWasabi.Blockchain.TransactionOutputs
set => RaiseAndSetIfChanged(ref _cluster, value);
}
- #region DependentProperties
-
public bool Confirmed
{
get => _confirmed;
@@ -151,21 +150,11 @@ namespace WalletWasabi.Blockchain.TransactionOutputs
private set => RaiseAndSetIfChanged(ref _isBanned, value);
}
- #endregion DependentProperties
-
- #region PropertySetters
-
public void SetIsBanned()
{
IsBanned = BannedUntilUtc is { } && BannedUntilUtc > DateTimeOffset.UtcNow;
}
- #endregion PropertySetters
-
- #endregion Properties
-
- #region Methods
-
public bool IsSpent() => SpenderTransaction is { };
/// <summary>
@@ -175,8 +164,6 @@ namespace WalletWasabi.Blockchain.TransactionOutputs
public bool IsReplaceable() => Transaction.IsRBF;
- #endregion Methods
-
#region EqualityAndComparison
public override bool Equals(object? obj) => Equals(obj as SmartCoin);

Make lazy things lazy in SmartCoin