Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VersionPayload optimization #755

Merged
merged 60 commits into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
f884a17
Udp support
shargon May 10, 2019
b8b814e
Fix unbind
shargon May 10, 2019
f7a4958
typo
shargon May 10, 2019
c5de11a
Merge branch 'master' into udp-server
vncoelho May 10, 2019
0170488
Unify GetPeers
shargon May 10, 2019
d59271a
Merge remote-tracking branch 'shargon/udp-server' into udp-server
shargon May 10, 2019
1192b3f
Clean Peer
shargon May 10, 2019
e284c37
Fix clean
shargon May 10, 2019
fe42fcd
Refactor VersionPayload
shargon May 10, 2019
5904954
Merge branch 'master' into udp-server
shargon May 10, 2019
b466f9d
Fix
shargon May 10, 2019
a1972c4
Merge remote-tracking branch 'shargon/udp-server' into udp-server
shargon May 10, 2019
3a54118
Unit test
shargon May 10, 2019
54a3087
Clean code
shargon May 10, 2019
2c4fc41
Clean line
shargon May 10, 2019
ee2c9f0
Fix ut
shargon May 10, 2019
77a0b1c
Simplify VersionPayload capabilities
shargon May 10, 2019
2215a55
Refactor VersionPayload capabilities
shargon May 11, 2019
ace3153
rename NodeConfig to ChannelsStartConfig
shargon May 11, 2019
72b82cf
Clean code
shargon May 11, 2019
2e8d558
Clean code
shargon May 11, 2019
be1b889
UPnP for Udp port
shargon May 11, 2019
765d6cf
Merge remote-tracking branch 'shargon/udp-server' into udp-server
shargon May 11, 2019
9485a03
Fx for ListennerTcpPort
shargon May 11, 2019
6d80ec1
Combine NodeCapabilities with ChannelType
shargon May 12, 2019
d0c1d7a
Merge branch 'master' into udp-server
erikzhang May 12, 2019
56d8d64
Summarize ServerCapability and remove reflection
shargon May 12, 2019
f238e46
Move Capabilities
shargon May 12, 2019
e9c9097
OnStart with ChannelsStartConfig
shargon May 12, 2019
c9447b6
Remove EndPointConfig
shargon May 12, 2019
1a86ae7
Move udp logic to ProtocolHandler
shargon May 12, 2019
766f589
Rename
shargon May 12, 2019
fd12eaf
Merge branch 'master' into udp-server
shargon May 12, 2019
c53f574
Remove Services and StartHeight from VersionPayload
shargon May 12, 2019
bb76f79
Merge remote-tracking branch 'shargon/udp-server' into udp-server
shargon May 12, 2019
f8ebcc0
Merge branch 'master' into udp-server
shargon May 13, 2019
fee3f86
Remove AcceptRelay
shargon May 13, 2019
311cd31
Clean code
shargon May 13, 2019
b55d75e
Parse message in LocalNode
shargon May 14, 2019
c8fc819
Merge branch 'master' into udp-server
shargon May 14, 2019
279babe
Merge branch 'master' into udp-server
shargon May 14, 2019
35489a8
Merge branch 'master' into udp-server
shargon May 16, 2019
32db242
Merge branch 'master' into udp-server
shargon May 16, 2019
64f6c4e
Prevent PeekChar
shargon May 16, 2019
10694fe
Fix Serialize
shargon May 16, 2019
957d52e
Merge branch 'master' into udp-server
shargon May 16, 2019
d8eaf7d
Clean code
shargon May 16, 2019
6762a8e
Merge remote-tracking branch 'shargon/udp-server' into udp-server
shargon May 16, 2019
485693e
Move `ChannelsStartConfig` into `Neo.Network.P2P`
erikzhang May 17, 2019
c425700
Merge branch 'master' into udp-server
shargon May 17, 2019
68cb16c
Optimize capabilities
erikzhang May 17, 2019
499fb92
Rename
erikzhang May 17, 2019
cefc6ae
Update NodeCapabilityType.cs
shargon May 17, 2019
e0fb9b8
Optimize `RemoteNode`
erikzhang May 17, 2019
373cf31
Remove udp
shargon May 17, 2019
6f97f55
format
erikzhang May 17, 2019
2e214cc
Remove port from `NetworkAddressWithTime`
erikzhang May 17, 2019
5830fc6
Merge branch 'master' into remove-udp
shargon May 17, 2019
cd75d0d
rename
erikzhang May 17, 2019
0b5521e
Merge branch 'master' into remove-udp
erikzhang May 17, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion neo.UnitTests/UT_P2PMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Neo.Network.P2P;
using Neo.Network.P2P.Capabilities;
using Neo.Network.P2P.Payloads;
using System.Collections.Generic;

namespace Neo.UnitTests
{
Expand Down
4 changes: 2 additions & 2 deletions neo/NeoSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public void StartConsensus(Wallet wallet, Store consensus_store = null, bool ign
Consensus.Tell(new ConsensusService.Start { IgnoreRecoveryLogs = ignoreRecoveryLogs }, Blockchain);
}

public void StartNode(ChannelsStartConfig cfg)
public void StartNode(ChannelsStartConfig config)
{
start_message = cfg;
start_message = config;

if (!suspend)
{
Expand Down
8 changes: 3 additions & 5 deletions neo/Network/P2P/Capabilities/NodeCapabilityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
{
public enum NodeCapabilityType : byte
{
#region Servers

//Servers
TcpServer = 0x01,
WsServer = 0x02,

#endregion

//Others
FullNode = 0x10
}
}
}
2 changes: 1 addition & 1 deletion neo/Network/P2P/Capabilities/ServerCapability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ protected override void SerializeWithoutType(BinaryWriter writer)
writer.Write(Port);
}
}
}
}
17 changes: 1 addition & 16 deletions neo/Network/P2P/LocalNode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Akka.Actor;
using Akka.IO;
using Neo.IO;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
Expand Down Expand Up @@ -54,7 +53,6 @@ public LocalNode(NeoSystem system)
{
if (singleton != null)
throw new InvalidOperationException();

this.system = system;
singleton = this;
}
Expand All @@ -77,7 +75,7 @@ private static IPEndPoint GetIPEndpointFromHostPort(string hostNameOrAddress, in
IPHostEntry entry;
try
{
entry = System.Net.Dns.GetHostEntry(hostNameOrAddress);
entry = Dns.GetHostEntry(hostNameOrAddress);
}
catch (SocketException)
{
Expand Down Expand Up @@ -158,19 +156,6 @@ protected override void OnReceive(object message)
}
}

public NetworkAddressWithTime[] GetPeers()
{
Random rand = new Random();

return RemoteNodes.Values
.Where(p => p.ListenerTcpPort > 0)
.GroupBy(p => p.Remote.Address, (k, g) => g.First())
.OrderBy(p => rand.Next())
.Take(AddrPayload.MaxCountToSend)
.Select(p => NetworkAddressWithTime.Create(p.Listener, p.Version.Timestamp, p.Version.Capabilities))
.ToArray();
}

private void OnRelay(IInventory inventory)
{
if (inventory is Transaction transaction)
Expand Down
5 changes: 2 additions & 3 deletions neo/Network/P2P/Payloads/NetworkAddressWithTime.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Neo.IO;
using Neo.Network.P2P.Capabilities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
Expand All @@ -16,13 +15,13 @@ public class NetworkAddressWithTime : ISerializable

public int Size => sizeof(uint) + 16 + sizeof(ushort) + Capabilities.GetVarSize();

public static NetworkAddressWithTime Create(IPEndPoint endpoint, uint timestamp, IEnumerable<NodeCapability> capabilities)
public static NetworkAddressWithTime Create(IPEndPoint endpoint, uint timestamp, params NodeCapability[] capabilities)
{
return new NetworkAddressWithTime
{
Timestamp = timestamp,
EndPoint = endpoint,
Capabilities = capabilities.ToArray()
Capabilities = capabilities
};
}

Expand Down
21 changes: 8 additions & 13 deletions neo/Network/P2P/Payloads/VersionPayload.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Neo.IO;
using Neo.Network.P2P.Capabilities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Neo.Network.P2P.Payloads
{
Expand All @@ -14,19 +12,19 @@ public class VersionPayload : ISerializable
public uint Magic;
public uint Version;
public uint Timestamp;
public NodeCapability[] Capabilities;
public uint Nonce;
public string UserAgent;
public NodeCapability[] Capabilities;

public int Size =>
sizeof(uint) + // Magic
sizeof(uint) + // Version
sizeof(uint) + // Timestamp
Capabilities.GetVarSize() + // Capabilities
sizeof(uint) + // Nonce
UserAgent.GetVarSize(); // UserAgent
UserAgent.GetVarSize() + // UserAgent
Capabilities.GetVarSize(); // Capabilities

public static VersionPayload Create(uint nonce, string userAgent, IEnumerable<NodeCapability> capabilities)
public static VersionPayload Create(uint nonce, string userAgent, params NodeCapability[] capabilities)
{
return new VersionPayload
{
Expand All @@ -35,7 +33,7 @@ public static VersionPayload Create(uint nonce, string userAgent, IEnumerable<No
Timestamp = DateTime.Now.ToTimestamp(),
Nonce = nonce,
UserAgent = userAgent,
Capabilities = capabilities.ToArray(),
Capabilities = capabilities,
};
}

Expand All @@ -44,26 +42,23 @@ void ISerializable.Deserialize(BinaryReader reader)
Magic = reader.ReadUInt32();
Version = reader.ReadUInt32();
Timestamp = reader.ReadUInt32();
Nonce = reader.ReadUInt32();
UserAgent = reader.ReadVarString(1024);

// Capabilities

Capabilities = new NodeCapability[reader.ReadVarInt(MaxCapabilities)];

for (int x = 0, max = Capabilities.Length; x < max; x++)
Capabilities[x] = NodeCapability.DeserializeFrom(reader);

Nonce = reader.ReadUInt32();
UserAgent = reader.ReadVarString(1024);
}

void ISerializable.Serialize(BinaryWriter writer)
{
writer.Write(Magic);
writer.Write(Version);
writer.Write(Timestamp);
writer.Write(Capabilities);
writer.Write(Nonce);
writer.WriteVarString(UserAgent);
writer.Write(Capabilities);
}
}
}
5 changes: 2 additions & 3 deletions neo/Network/P2P/Peer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ private class WsConnected { public WebSocket Socket; public IPEndPoint Remote; p
public const int DefaultMaxConnections = DefaultMinDesiredConnections * 4;

private static readonly IActorRef tcp_manager = Context.System.Tcp();

private IActorRef tcp_listener;
private IWebHost ws_host;
private ICancelable timer;
Expand Down Expand Up @@ -135,8 +134,8 @@ protected override void OnReceive(object message)

private void OnStart(ChannelsStartConfig config)
{
ListenerTcpPort = config.Tcp == null ? 0 : config.Tcp.Port;
ListenerWsPort = config.WebSocket == null ? 0 : config.WebSocket.Port;
ListenerTcpPort = config.Tcp?.Port ?? 0;
ListenerWsPort = config.WebSocket?.Port ?? 0;

MinDesiredConnections = config.MinDesiredConnections;
MaxConnections = config.MaxConnections;
Expand Down
169 changes: 84 additions & 85 deletions neo/Network/P2P/ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.Plugins;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -35,91 +36,83 @@ public ProtocolHandler(NeoSystem system)

protected override void OnReceive(object message)
{
switch (message)
if (!(message is Message msg)) return;
foreach (IP2PPlugin plugin in Plugin.P2PPlugins)
if (!plugin.OnP2PMessage(msg))
return;
if (version == null)
{
case Message msg:
{
foreach (IP2PPlugin plugin in Plugin.P2PPlugins)
if (!plugin.OnP2PMessage(msg))
return;

if (version == null)
{
if (msg.Command != MessageCommand.Version)
throw new ProtocolViolationException();
OnVersionMessageReceived((VersionPayload)msg.Payload);
return;
}
if (!verack)
{
if (msg.Command != MessageCommand.Verack)
throw new ProtocolViolationException();
OnVerackMessageReceived();
return;
}
switch (msg.Command)
{
case MessageCommand.Addr:
OnAddrMessageReceived((AddrPayload)msg.Payload);
break;
case MessageCommand.Block:
OnInventoryReceived((Block)msg.Payload);
break;
case MessageCommand.Consensus:
OnInventoryReceived((ConsensusPayload)msg.Payload);
break;
case MessageCommand.FilterAdd:
OnFilterAddMessageReceived((FilterAddPayload)msg.Payload);
break;
case MessageCommand.FilterClear:
OnFilterClearMessageReceived();
break;
case MessageCommand.FilterLoad:
OnFilterLoadMessageReceived((FilterLoadPayload)msg.Payload);
break;
case MessageCommand.GetAddr:
OnGetAddrMessageReceived();
break;
case MessageCommand.GetBlocks:
OnGetBlocksMessageReceived((GetBlocksPayload)msg.Payload);
break;
case MessageCommand.GetData:
OnGetDataMessageReceived((InvPayload)msg.Payload);
break;
case MessageCommand.GetHeaders:
OnGetHeadersMessageReceived((GetBlocksPayload)msg.Payload);
break;
case MessageCommand.Headers:
OnHeadersMessageReceived((HeadersPayload)msg.Payload);
break;
case MessageCommand.Inv:
OnInvMessageReceived((InvPayload)msg.Payload);
break;
case MessageCommand.Mempool:
OnMemPoolMessageReceived();
break;
case MessageCommand.Ping:
OnPingMessageReceived((PingPayload)msg.Payload);
break;
case MessageCommand.Pong:
OnPongMessageReceived((PingPayload)msg.Payload);
break;
case MessageCommand.Transaction:
if (msg.Payload.Size <= Transaction.MaxTransactionSize)
OnInventoryReceived((Transaction)msg.Payload);
break;
case MessageCommand.Verack:
case MessageCommand.Version:
throw new ProtocolViolationException();
case MessageCommand.Alert:
case MessageCommand.MerkleBlock:
case MessageCommand.NotFound:
case MessageCommand.Reject:
default: break;
}

break;
}
if (msg.Command != MessageCommand.Version)
throw new ProtocolViolationException();
OnVersionMessageReceived((VersionPayload)msg.Payload);
return;
}
if (!verack)
{
if (msg.Command != MessageCommand.Verack)
throw new ProtocolViolationException();
OnVerackMessageReceived();
return;
}
switch (msg.Command)
{
case MessageCommand.Addr:
OnAddrMessageReceived((AddrPayload)msg.Payload);
break;
case MessageCommand.Block:
OnInventoryReceived((Block)msg.Payload);
break;
case MessageCommand.Consensus:
OnInventoryReceived((ConsensusPayload)msg.Payload);
break;
case MessageCommand.FilterAdd:
OnFilterAddMessageReceived((FilterAddPayload)msg.Payload);
break;
case MessageCommand.FilterClear:
OnFilterClearMessageReceived();
break;
case MessageCommand.FilterLoad:
OnFilterLoadMessageReceived((FilterLoadPayload)msg.Payload);
break;
case MessageCommand.GetAddr:
OnGetAddrMessageReceived();
break;
case MessageCommand.GetBlocks:
OnGetBlocksMessageReceived((GetBlocksPayload)msg.Payload);
break;
case MessageCommand.GetData:
OnGetDataMessageReceived((InvPayload)msg.Payload);
break;
case MessageCommand.GetHeaders:
OnGetHeadersMessageReceived((GetBlocksPayload)msg.Payload);
break;
case MessageCommand.Headers:
OnHeadersMessageReceived((HeadersPayload)msg.Payload);
break;
case MessageCommand.Inv:
OnInvMessageReceived((InvPayload)msg.Payload);
break;
case MessageCommand.Mempool:
OnMemPoolMessageReceived();
break;
case MessageCommand.Ping:
OnPingMessageReceived((PingPayload)msg.Payload);
break;
case MessageCommand.Pong:
OnPongMessageReceived((PingPayload)msg.Payload);
break;
case MessageCommand.Transaction:
if (msg.Payload.Size <= Transaction.MaxTransactionSize)
OnInventoryReceived((Transaction)msg.Payload);
break;
case MessageCommand.Verack:
case MessageCommand.Version:
throw new ProtocolViolationException();
case MessageCommand.Alert:
case MessageCommand.MerkleBlock:
case MessageCommand.NotFound:
case MessageCommand.Reject:
default: break;
}
}

Expand Down Expand Up @@ -151,7 +144,13 @@ private void OnFilterLoadMessageReceived(FilterLoadPayload payload)

private void OnGetAddrMessageReceived()
{
NetworkAddressWithTime[] networkAddresses = LocalNode.Singleton.GetPeers();
Random rand = new Random();
IEnumerable<RemoteNode> peers = LocalNode.Singleton.RemoteNodes.Values
.Where(p => p.ListenerTcpPort > 0)
.GroupBy(p => p.Remote.Address, (k, g) => g.First())
.OrderBy(p => rand.Next())
.Take(AddrPayload.MaxCountToSend);
NetworkAddressWithTime[] networkAddresses = peers.Select(p => NetworkAddressWithTime.Create(p.Listener, p.Version.Timestamp, p.Version.Capabilities)).ToArray();
if (networkAddresses.Length == 0) return;
Context.Parent.Tell(Message.Create(MessageCommand.Addr, AddrPayload.Create(networkAddresses)));
}
Expand Down
Loading