forked from WalletWasabi/WalletWasabi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More resilient broadcasting mechanism (WalletWasabi#13381)
- Loading branch information
Showing
7 changed files
with
103 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 35 additions & 13 deletions
48
WalletWasabi/Blockchain/Transactions/TransactionBroadcastEntry.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,60 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using NBitcoin; | ||
|
||
namespace WalletWasabi.Blockchain.Transactions; | ||
|
||
public class TransactionBroadcastEntry | ||
{ | ||
public TransactionBroadcastEntry(SmartTransaction transaction, string nodeRemoteSocketEndpoint) | ||
public TransactionBroadcastEntry(SmartTransaction transaction) | ||
{ | ||
Transaction = transaction; | ||
TransactionId = Transaction.GetHash(); | ||
NodeRemoteSocketEndpoint = nodeRemoteSocketEndpoint; | ||
BroadcastCompleted = new TaskCompletionSource(); | ||
PropagationConfirmed = new TaskCompletionSource(); | ||
BroadcastCompleted = new TaskCompletionSource<EndPoint[]>(); | ||
PropagationConfirmed = new TaskCompletionSource<EndPoint[]>(); | ||
} | ||
|
||
public SmartTransaction Transaction { get; } | ||
public uint256 TransactionId { get; } | ||
public string NodeRemoteSocketEndpoint { get; } | ||
|
||
public TaskCompletionSource BroadcastCompleted { get; } | ||
public TaskCompletionSource PropagationConfirmed { get; } | ||
public TaskCompletionSource<EndPoint[]> BroadcastCompleted { get; } | ||
public TaskCompletionSource<EndPoint[]> PropagationConfirmed { get; } | ||
|
||
public void MakeBroadcasted() | ||
private readonly HashSet<EndPoint> _broadcastedTo = new(); | ||
private readonly HashSet<EndPoint> _confirmedBy = new(); | ||
private readonly object _syncObj = new(); | ||
|
||
public void BroadcastedTo(EndPoint nodeEndpoint) | ||
{ | ||
lock (_syncObj) | ||
{ | ||
if (_broadcastedTo.Add(nodeEndpoint) && _broadcastedTo.Count > 1) | ||
{ | ||
BroadcastCompleted.TrySetResult(_broadcastedTo.ToArray()); | ||
} | ||
} | ||
} | ||
|
||
public bool WasBroadcastedTo(EndPoint nodeEndpoint) | ||
{ | ||
BroadcastCompleted.TrySetResult(); | ||
return _broadcastedTo.Contains(nodeEndpoint); | ||
} | ||
|
||
public void ConfirmPropagationOnce() | ||
public void ConfirmPropagationOnce(EndPoint nodeEndpoint) | ||
{ | ||
PropagationConfirmed.TrySetResult(); | ||
lock (_syncObj) | ||
{ | ||
if (_confirmedBy.Add(nodeEndpoint) && _confirmedBy.Count > 1) | ||
{ | ||
PropagationConfirmed.TrySetResult(_confirmedBy.ToArray()); | ||
} | ||
} | ||
} | ||
|
||
public void ConfirmPropagationForGood() | ||
public void ConfirmPropagationForGood(EndPoint nodeEndpoint) | ||
{ | ||
PropagationConfirmed.TrySetResult(); | ||
PropagationConfirmed.TrySetResult([nodeEndpoint]); | ||
} | ||
} |