-
Notifications
You must be signed in to change notification settings - Fork 487
/
MempoolBehavior.cs
161 lines (142 loc) · 4.29 KB
/
MempoolBehavior.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using NBitcoin;
using NBitcoin.Protocol;
using NBitcoin.Protocol.Behaviors;
using System;
using System.Linq;
using System.Threading.Tasks;
using WalletWasabi.Helpers;
using WalletWasabi.Logging;
using WalletWasabi.Models;
namespace WalletWasabi.Services
{
public class MempoolBehavior : NodeBehavior
{
private const int MaxInvSize = 50000;
public MempoolService MempoolService { get; }
public MempoolBehavior(MempoolService mempoolService)
{
MempoolService = Guard.NotNull(nameof(mempoolService), mempoolService);
}
protected override void AttachCore()
{
AttachedNode.MessageReceived += AttachedNode_MessageReceivedAsync;
}
protected override void DetachCore()
{
AttachedNode.MessageReceived -= AttachedNode_MessageReceivedAsync;
}
private async void AttachedNode_MessageReceivedAsync(Node node, IncomingMessage message)
{
try
{
if (message.Message.Payload is GetDataPayload getDataPayload)
{
await ProcessGetDataAsync(node, getDataPayload);
return;
}
if (message.Message.Payload is TxPayload txPayload)
{
ProcessTx(txPayload);
return;
}
if (message.Message.Payload is InvPayload invPayload)
{
await ProcessInvAsync(node, invPayload);
return;
}
}
catch (OperationCanceledException ex)
{
Logger.LogDebug<MempoolBehavior>(ex);
}
catch (Exception ex)
{
Logger.LogInfo<MempoolBehavior>($"Ignoring {ex.GetType()}: {ex.Message}");
Logger.LogDebug<MempoolBehavior>(ex);
}
}
private async Task ProcessGetDataAsync(Node node, GetDataPayload payload)
{
if (payload.Inventory.Count > MaxInvSize)
{
Logger.LogDebug<MempoolBehavior>($"Received inventory too big. {nameof(MaxInvSize)}: {MaxInvSize}, Node: {node.RemoteSocketEndpoint}");
return;
}
foreach (var inv in payload.Inventory.Where(inv => inv.Type.HasFlag(InventoryType.MSG_TX)))
{
if (MempoolService.TryGetFromBroadcastStore(inv.Hash, out TransactionBroadcastEntry entry)) // If we have the transaction to be broadcasted then broadcast it now.
{
if (entry.NodeRemoteSocketEndpoint != node.RemoteSocketEndpoint.ToString())
{
continue; // Would be strange. It could be some kind of attack.
}
try
{
var txPayload = new TxPayload(entry.Transaction);
if (!node.IsConnected)
{
Logger.LogInfo<MempoolBehavior>($"Could not serve transaction. Node ({node.RemoteSocketEndpoint}) is not connected anymore: {entry.TransactionId}.");
}
else
{
await node.SendMessageAsync(txPayload);
entry.MakeBroadcasted();
Logger.LogInfo<MempoolBehavior>($"Successfully served transaction to node ({node.RemoteSocketEndpoint}): {entry.TransactionId}.");
}
}
catch (Exception ex)
{
Logger.LogInfo<MempoolBehavior>(ex);
}
}
}
}
private async Task ProcessInvAsync(Node node, InvPayload payload)
{
if (payload.Inventory.Count > MaxInvSize)
{
Logger.LogDebug<MempoolBehavior>($"Received inventory too big. {nameof(MaxInvSize)}: {MaxInvSize}, Node: {node.RemoteSocketEndpoint}");
return;
}
var getDataPayload = new GetDataPayload();
foreach (var inv in payload.Inventory.Where(inv => inv.Type.HasFlag(InventoryType.MSG_TX)))
{
if (MempoolService.TryGetFromBroadcastStore(inv.Hash, out TransactionBroadcastEntry entry)) // If we have the transaction then adjust confirmation.
{
try
{
if (entry.NodeRemoteSocketEndpoint == node.RemoteSocketEndpoint.ToString())
{
continue; // Wtf, why are you trying to broadcast it back to us?
}
entry.ConfirmPropagationOnce();
}
catch (Exception ex)
{
Logger.LogInfo<MempoolBehavior>(ex);
}
}
// if we already have it continue;
if (!MempoolService.TransactionHashes.TryAdd(inv.Hash))
{
continue;
}
getDataPayload.Inventory.Add(inv);
}
if (getDataPayload.Inventory.Any() && node.IsConnected)
{
// ask for the whole transaction
await node.SendMessageAsync(getDataPayload);
}
}
private void ProcessTx(TxPayload payload)
{
Transaction transaction = payload.Object;
MempoolService.OnTransactionReceived(new SmartTransaction(transaction, Height.Mempool));
}
public override object Clone()
{
return new MempoolBehavior(MempoolService);
}
}
}