Skip to content

Commit

Permalink
implement spentaccordingtobackend (both for cj response and send resp…
Browse files Browse the repository at this point in the history
…onse)
  • Loading branch information
nopara73 committed Nov 18, 2018
1 parent 0ae7e6d commit a603610
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
10 changes: 10 additions & 0 deletions WalletWasabi.Gui/Controls/WalletExplorer/CoinViewModel.cs
Expand Up @@ -44,6 +44,11 @@ public CoinViewModel(SmartCoin model)
this.RaisePropertyChanged(nameof(BannedCoinToolTip));
});

model.WhenAnyValue(x => x.SpentAccordingToBackend).ObserveOn(RxApp.MainThreadScheduler).Subscribe(_ =>
{
this.RaisePropertyChanged(nameof(Status));
});

Global.IndexDownloader.WhenAnyValue(x => x.BestHeight).ObserveOn(RxApp.MainThreadScheduler).Subscribe(_ =>
{
this.RaisePropertyChanged(nameof(Confirmations));
Expand Down Expand Up @@ -142,6 +147,11 @@ public SmartCoinStatus Status
}
}

if (Model.SpentAccordingToBackend)
{
return SmartCoinStatus.SpentAccordingToBackend;
}

if (Model.Confirmed)
{
if (Model.CoinJoinInProgress)
Expand Down
36 changes: 24 additions & 12 deletions WalletWasabi/Models/SmartCoin.cs
Expand Up @@ -19,6 +19,7 @@ public class SmartCoin : IEquatable<SmartCoin>, INotifyPropertyChanged
private uint256 _spenderTransactionId;
private bool _coinJoinInProgress;
private DateTimeOffset? _bannedUntilUtc;
private bool _spentAccordingToBackend;

[JsonProperty(Order = 1)]
[JsonConverter(typeof(Uint256JsonConverter))]
Expand Down Expand Up @@ -157,14 +158,8 @@ public bool CoinJoinInProgress
}
}

/// <summary>
/// If the backend thinks it's spent, but Wasabi doesn't yet know.
/// </summary>
[JsonProperty(Order = 13)]
public bool SpentAccordingToBackend { get; set; }

public bool SpentOrCoinJoinInProgress => !(SpenderTransactionId is null) || CoinJoinInProgress || SpentAccordingToBackend;
public bool Unspent => SpenderTransactionId is null && !SpentAccordingToBackend;
public bool SpentOrCoinJoinInProgress => !Unspent || CoinJoinInProgress;
public bool Unspent => SpenderTransactionId is null;
public bool Confirmed => Height != Height.MemPool && Height != Height.Unknown;
public bool IsBanned => BannedUntilUtc != null && BannedUntilUtc > DateTimeOffset.UtcNow;

Expand All @@ -178,8 +173,24 @@ public bool CoinJoinInProgress
/// </summary>
public ISecret Secret { get; set; }

/// <summary>
/// If the backend thinks it's spent, but Wasabi doesn't yet know.
/// </summary>
public bool SpentAccordingToBackend
{
get { return _spentAccordingToBackend; }
set
{
if (value != _spentAccordingToBackend)
{
_spentAccordingToBackend = value;
OnPropertyChanged(nameof(SpentAccordingToBackend));
}
}
}

[JsonConstructor]
public SmartCoin(uint256 transactionId, uint index, Script scriptPubKey, Money amount, TxoRef[] spentOutputs, Height height, bool rbf, int mixin, string label = "", uint256 spenderTransactionId = null, bool coinJoinInProgress = false, bool spentAccordingToBackend = false)
public SmartCoin(uint256 transactionId, uint index, Script scriptPubKey, Money amount, TxoRef[] spentOutputs, Height height, bool rbf, int mixin, string label = "", uint256 spenderTransactionId = null, bool coinJoinInProgress = false)
{
TransactionId = Guard.NotNull(nameof(transactionId), transactionId);
Index = Guard.NotNull(nameof(index), index);
Expand All @@ -193,11 +204,11 @@ public SmartCoin(uint256 transactionId, uint index, Script scriptPubKey, Money a
RBF = rbf;
CoinJoinInProgress = coinJoinInProgress;
Secret = null;
SpentAccordingToBackend = false;
BannedUntilUtc = null;
SpentAccordingToBackend = spentAccordingToBackend;
}

public SmartCoin(Coin coin, TxoRef[] spentOutputs, Height height, bool rbf, int mixin, string label = "", uint256 spenderTransactionId = null, bool coinJoinInProgress = false, bool spentAccordingToBackend = false)
public SmartCoin(Coin coin, TxoRef[] spentOutputs, Height height, bool rbf, int mixin, string label = "", uint256 spenderTransactionId = null, bool coinJoinInProgress = false)
{
Guard.NotNull(nameof(coin), coin);
TransactionId = coin.Outpoint.Hash;
Expand All @@ -212,7 +223,8 @@ public SmartCoin(Coin coin, TxoRef[] spentOutputs, Height height, bool rbf, int
RBF = rbf;
CoinJoinInProgress = coinJoinInProgress;
Secret = null;
SpentAccordingToBackend = spentAccordingToBackend;
SpentAccordingToBackend = false;
BannedUntilUtc = null;
}

public event PropertyChangedEventHandler PropertyChanged;
Expand Down
2 changes: 1 addition & 1 deletion WalletWasabi/Services/CcjClient.cs
Expand Up @@ -525,7 +525,7 @@ private async Task TryRegisterCoinsAsync(CcjClientRound inputRegistrableRound)
await DequeueCoinsFromMixNoLockAsync(coinReference);
return;
}
catch (HttpRequestException ex) when (ex.Message.StartsWith("Provided input is not unspent", StringComparison.InvariantCultureIgnoreCase))
catch (HttpRequestException ex) when (ex.Message.Contains("Provided input is not unspent", StringComparison.InvariantCultureIgnoreCase))
{
string[] parts = ex.Message.Split(new[] { "Provided input is not unspent: " }, StringSplitOptions.RemoveEmptyEntries);
string spentInputString = parts[1].TrimEnd('.');
Expand Down
19 changes: 18 additions & 1 deletion WalletWasabi/Services/WalletService.cs
Expand Up @@ -21,6 +21,7 @@
using Newtonsoft.Json;
using System.Collections.Concurrent;
using NBitcoin.DataEncoders;
using System.Net.Http;

namespace WalletWasabi.Services
{
Expand Down Expand Up @@ -1029,7 +1030,23 @@ public async Task SendTransactionAsync(SmartTransaction transaction)
{
using (var client = new WasabiClient(IndexDownloader.WasabiClient.TorClient.DestinationUri, IndexDownloader.WasabiClient.TorClient.TorSocks5EndPoint))
{
await client.BroadcastAsync(transaction);
try
{
await client.BroadcastAsync(transaction);
}
catch (HttpRequestException ex) when (ex.Message.Contains("txn-mempool-conflict", StringComparison.InvariantCultureIgnoreCase))
{
if (transaction.Transaction.Inputs.Count == 1)
{
OutPoint input = transaction.Transaction.Inputs.First().PrevOut;
SmartCoin coin = Coins.FirstOrDefault(x => x.TransactionId == input.Hash && x.Index == input.N);
if (coin != default)
{
coin.SpentAccordingToBackend = true;
}
}
throw;
}
}

ProcessTransaction(new SmartTransaction(transaction.Transaction, Height.MemPool));
Expand Down

0 comments on commit a603610

Please sign in to comment.