-
Notifications
You must be signed in to change notification settings - Fork 663
Expand file tree
/
Copy pathXdcProtocolHandler.cs
More file actions
166 lines (144 loc) · 5.18 KB
/
XdcProtocolHandler.cs
File metadata and controls
166 lines (144 loc) · 5.18 KB
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
162
163
164
165
166
// SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using Nethermind.Blockchain;
using Nethermind.Consensus;
using Nethermind.Consensus.Scheduler;
using Nethermind.Core.Caching;
using Nethermind.Core.Crypto;
using Nethermind.Logging;
using Nethermind.Network;
using Nethermind.Network.P2P;
using Nethermind.Network.P2P.ProtocolHandlers;
using Nethermind.Network.P2P.Subprotocols.Eth.V62.Messages;
using Nethermind.Network.P2P.Subprotocols.Eth.V65;
using Nethermind.Network.Rlpx;
using Nethermind.Stats;
using Nethermind.Synchronization;
using Nethermind.TxPool;
using Nethermind.Xdc.Types;
using System;
namespace Nethermind.Xdc.P2P;
internal class XdcProtocolHandler(
ITimeoutCertificateManager timeoutCertificateManager,
IVotesManager votesManager,
ISyncInfoManager syncInfoManager,
IBlockTree blockTree,
ISession session,
IMessageSerializationService serializer,
INodeStatsManager nodeStatsManager,
ISyncServer syncServer,
IBackgroundTaskScheduler backgroundTaskScheduler,
ITxPool txPool,
IGossipPolicy gossipPolicy,
IForkInfo forkInfo,
ILogManager logManager,
ITxGossipPolicy? transactionsGossipPolicy = null) : Eth65ProtocolHandler(session, serializer, nodeStatsManager, syncServer, backgroundTaskScheduler, txPool, gossipPolicy, forkInfo, logManager, transactionsGossipPolicy), IStaticProtocolInfo
{
private readonly ITimeoutCertificateManager _timeoutCertificateManager = timeoutCertificateManager;
private readonly IVotesManager _votesManager = votesManager;
private readonly IBlockTree _blockTree = blockTree;
private ClockKeyCache<ValueHash256> _notifiedVotes = new(MemoryAllowance.MemPoolSize / 2);
private ClockKeyCache<ValueHash256> _notifiedTimeouts = new(MemoryAllowance.MemPoolSize / 2);
// cspell:disable-next-line
public override string Name => "xdpos2";
public static byte Version => 100;
public override byte ProtocolVersion => Version;
public override int MessageIdSpaceSize => XdcMessageCode.SyncInfoMsg + 1;
protected override TimeSpan InitTimeout => base.InitTimeout;
public override void HandleMessage(ZeroPacket message)
{
var size = message.Content.ReadableBytes;
int packetType = message.PacketType;
(bool isSyncing, _, _) = _blockTree.IsSyncing();
if (isSyncing) // ignore XDC updates while syncing
{
base.HandleMessage(message);
return;
}
switch (packetType)
{
case XdcMessageCode.VoteMsg:
{
using VoteMsg voteMsg = Deserialize<VoteMsg>(message.Content);
ReportIn(voteMsg, size);
Handle(voteMsg);
break;
}
case XdcMessageCode.TimeoutMsg:
{
using TimeoutMsg timeoutMsg = Deserialize<TimeoutMsg>(message.Content);
ReportIn(timeoutMsg, size);
Handle(timeoutMsg);
break;
}
case XdcMessageCode.SyncInfoMsg:
{
using SyncInfoMsg syncInfoMsg = Deserialize<SyncInfoMsg>(message.Content);
ReportIn(syncInfoMsg, size);
Handle(syncInfoMsg);
break;
}
default:
{
base.HandleMessage(message);
break;
}
}
}
protected override void EnrichStatusMessage(StatusMessage statusMessage)
{
// We do not want to add ForkId to status message in XDPoS
}
private void Handle(VoteMsg voteMsg)
{
_ = _votesManager.OnReceiveVote(voteMsg.Vote);
}
private void Handle(TimeoutMsg timeoutMsg)
{
_timeoutCertificateManager.OnReceiveTimeout(timeoutMsg.Timeout);
}
private void Handle(SyncInfoMsg syncInfoMsg)
{
if (!syncInfoManager.VerifySyncInfo(syncInfoMsg.SyncInfo, out string error))
{
//TODO Disconnect peer?
if (Logger.IsDebug) Logger.Debug($"Received useless SyncInfo from peer {Session.RemoteNodeId}: {error}");
return;
}
syncInfoManager.ProcessSyncInfo(syncInfoMsg.SyncInfo);
}
public void SendVote(Vote vote)
{
if (!ShouldNotifyVote(vote))
return;
Send(new VoteMsg() { Vote = vote });
}
public void SendTimeout(Timeout timeout)
{
if (!ShouldNotifyTimeout(timeout))
return;
Send(new TimeoutMsg() { Timeout = timeout });
}
public void SendSyncInfo(SyncInfo syncInfo)
{
Send(new SyncInfoMsg() { SyncInfo = syncInfo });
}
private bool ShouldNotifyVote(Vote vote)
{
if (vote.IsMyVote)
return true;
if (_notifiedVotes.Contains(vote.Hash))
return false;
_notifiedVotes.Set(vote.Hash);
return true;
}
private bool ShouldNotifyTimeout(Timeout timeout)
{
if (timeout.IsMyVote)
return true;
if (_notifiedTimeouts.Contains(timeout.Hash))
return false;
_notifiedTimeouts.Set(timeout.Hash);
return true;
}
}