22using System . Collections . Generic ;
33using System . Linq ;
44using System . Threading . Tasks ;
5- using Blockcore . Connection ;
6- using Blockcore . Features . MemoryPool ;
7- using Blockcore . Features . Wallet . Interfaces ;
5+ using Blockcore . Interfaces ;
86using Blockcore . P2P . Peer ;
97using Blockcore . P2P . Protocol . Payloads ;
8+ using Blockcore . Signals ;
109using Blockcore . Utilities ;
1110using ConcurrentCollections ;
1211using NBitcoin ;
1312
14- namespace Blockcore . Features . Wallet . Broadcasting
13+ namespace Blockcore . Connection . Broadcasting
1514{
16- public abstract class BroadcasterManagerBase : IBroadcasterManager
15+ public class BroadcasterManager : IBroadcasterManager
1716 {
18- public event EventHandler < TransactionBroadcastEntry > TransactionStateChanged ;
19-
20- /// <summary> Connection manager for managing node connections.</summary>
2117 protected readonly IConnectionManager connectionManager ;
18+ private readonly ISignals signals ;
19+ private readonly IEnumerable < IBroadcastCheck > broadcastChecks ;
2220
23- public BroadcasterManagerBase ( IConnectionManager connectionManager )
21+ public BroadcasterManager ( IConnectionManager connectionManager , ISignals signals , IEnumerable < IBroadcastCheck > broadcastChecks )
2422 {
2523 Guard . NotNull ( connectionManager , nameof ( connectionManager ) ) ;
2624
2725 this . connectionManager = connectionManager ;
28- this . Broadcasts = new ConcurrentHashSet < TransactionBroadcastEntry > ( ) ;
26+ this . signals = signals ;
27+ this . broadcastChecks = broadcastChecks ;
28+ this . Broadcasts = new ConcurrentHashSet < BroadcastTransactionStateChanedEntry > ( ) ;
2929 }
3030
31- public void OnTransactionStateChanged ( TransactionBroadcastEntry entry )
31+ public void OnTransactionStateChanged ( BroadcastTransactionStateChanedEntry entry )
3232 {
33- this . TransactionStateChanged ? . Invoke ( this , entry ) ;
33+ this . signals . Publish ( new TransactionBroadcastEvent ( this , entry ) ) ;
3434 }
3535
3636 /// <summary>Transactions to broadcast.</summary>
37- private ConcurrentHashSet < TransactionBroadcastEntry > Broadcasts { get ; }
37+ private ConcurrentHashSet < BroadcastTransactionStateChanedEntry > Broadcasts { get ; }
3838
3939 /// <summary>Retrieves a transaction with provided hash from the collection of transactions to broadcast.</summary>
4040 /// <param name="transactionHash">Hash of the transaction to retrieve.</param>
41- public TransactionBroadcastEntry GetTransaction ( uint256 transactionHash )
41+ public BroadcastTransactionStateChanedEntry GetTransaction ( uint256 transactionHash )
4242 {
43- TransactionBroadcastEntry txEntry = this . Broadcasts . FirstOrDefault ( x => x . Transaction . GetHash ( ) == transactionHash ) ;
43+ BroadcastTransactionStateChanedEntry txEntry = this . Broadcasts . FirstOrDefault ( x => x . Transaction . GetHash ( ) == transactionHash ) ;
4444 return txEntry ?? null ;
4545 }
4646
4747 /// <summary>Adds or updates a transaction from the collection of transactions to broadcast.</summary>
48- public void AddOrUpdate ( Transaction transaction , TransactionBroadcastState transactionBroadcastState , MempoolError mempoolError = null )
48+ public void AddOrUpdate ( Transaction transaction , TransactionBroadcastState transactionBroadcastState , string errorMessage = null )
4949 {
50- TransactionBroadcastEntry broadcastEntry = this . Broadcasts . FirstOrDefault ( x => x . Transaction . GetHash ( ) == transaction . GetHash ( ) ) ;
50+ BroadcastTransactionStateChanedEntry broadcastEntry = this . Broadcasts . FirstOrDefault ( x => x . Transaction . GetHash ( ) == transaction . GetHash ( ) ) ;
5151
5252 if ( broadcastEntry == null )
5353 {
54- broadcastEntry = new TransactionBroadcastEntry ( transaction , transactionBroadcastState , mempoolError ) ;
54+ broadcastEntry = new BroadcastTransactionStateChanedEntry ( transaction , transactionBroadcastState , errorMessage ) ;
5555 this . Broadcasts . Add ( broadcastEntry ) ;
5656 this . OnTransactionStateChanged ( broadcastEntry ) ;
5757 }
@@ -62,7 +62,28 @@ public void AddOrUpdate(Transaction transaction, TransactionBroadcastState trans
6262 }
6363 }
6464
65- public abstract Task BroadcastTransactionAsync ( Transaction transaction ) ;
65+ public async Task BroadcastTransactionAsync ( Transaction transaction )
66+ {
67+ Guard . NotNull ( transaction , nameof ( transaction ) ) ;
68+
69+ if ( this . IsPropagated ( transaction ) )
70+ {
71+ return ;
72+ }
73+
74+ foreach ( IBroadcastCheck broadcastCheck in this . broadcastChecks )
75+ {
76+ string error = await broadcastCheck . CheckTransaction ( transaction ) . ConfigureAwait ( false ) ;
77+
78+ if ( ! string . IsNullOrEmpty ( error ) )
79+ {
80+ this . AddOrUpdate ( transaction , TransactionBroadcastState . CantBroadcast , error ) ;
81+ return ;
82+ }
83+ }
84+
85+ await this . PropagateTransactionToPeersAsync ( transaction , this . connectionManager . ConnectedPeers . ToList ( ) ) . ConfigureAwait ( false ) ;
86+ }
6687
6788 /// <summary>
6889 /// Sends transaction to peers.
@@ -90,8 +111,8 @@ protected async Task PropagateTransactionToPeersAsync(Transaction transaction, L
90111 /// <summary>Checks if transaction was propagated to any peers on the network.</summary>
91112 protected bool IsPropagated ( Transaction transaction )
92113 {
93- TransactionBroadcastEntry broadcastEntry = this . GetTransaction ( transaction . GetHash ( ) ) ;
114+ BroadcastTransactionStateChanedEntry broadcastEntry = this . GetTransaction ( transaction . GetHash ( ) ) ;
94115 return ( broadcastEntry != null ) && ( broadcastEntry . TransactionBroadcastState == TransactionBroadcastState . Propagated ) ;
95116 }
96117 }
97- }
118+ }
0 commit comments