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

Add ping/pong for updating node height #673

Merged
merged 4 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
44 changes: 44 additions & 0 deletions neo/Network/P2P/Payloads/PingPayload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Neo.IO;
using System;
using System.IO;

namespace Neo.Network.P2P.Payloads
{
public class PingPayload : ISerializable
{
public uint LastBlockIndex;
public uint Timestamp;
public uint Nonce;

public int Size =>
sizeof(uint) + //LastBlockIndex
sizeof(uint) + //Timestamp
sizeof(uint); //Nonce


public static PingPayload Create(uint height)
{
Random rand = new Random();
return new PingPayload
{
LastBlockIndex = height,
Timestamp = DateTime.UtcNow.ToTimestamp(),
Nonce = (uint)rand.Next()
};
}

void ISerializable.Deserialize(BinaryReader reader)
{
LastBlockIndex = reader.ReadUInt32();
Timestamp = reader.ReadUInt32();
Nonce = reader.ReadUInt32();
}

void ISerializable.Serialize(BinaryWriter writer)
{
writer.Write(LastBlockIndex);
writer.Write(Timestamp);
writer.Write(Nonce);
}
}
}
25 changes: 19 additions & 6 deletions neo/Network/P2P/ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ namespace Neo.Network.P2P
{
internal class ProtocolHandler : UntypedActor
{
public class SetVersion { public VersionPayload Version; }
public class SetVerack { }
public class SetFilter { public BloomFilter Filter; }

private readonly NeoSystem system;
Expand Down Expand Up @@ -95,6 +93,12 @@ protected override void OnReceive(object message)
case "mempool":
OnMemPoolMessageReceived();
break;
case "ping":
OnPingMessageReceived(msg.GetPayload<PingPayload>());
break;
case "pong":
OnPongMessageReceived(msg.GetPayload<PingPayload>());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it correct @ixje @erikzhang msg.GetPayload<PingPayload> for Pong?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there is only 1 payload for both.

@erikzhang I don't know what your intention of the added nonce field was, but should that maybe have been included in the response as a way of confirming that the pong is a response to our ping?

e.g
-> ping generate nonce e.g. 123
<- pong with nonce set to the received PingPayload.nonce e.g. 123 in this example

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but should that maybe have been included in the response as a way of confirming that the pong is a response to our ping?

Good idea!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there is only 1 payload for both.

Thanks for the clarification. So, perhaps rename to PingPongPayload, just for clarity? That really looked like a typo me :)

break;
case "tx":
if (msg.Payload.Length <= Transaction.MaxTransactionSize)
OnInventoryReceived(msg.GetTransaction());
Expand All @@ -105,8 +109,6 @@ protected override void OnReceive(object message)
case "alert":
case "merkleblock":
case "notfound":
case "ping":
case "pong":
case "reject":
default:
//暂时忽略
Expand Down Expand Up @@ -272,16 +274,27 @@ private void OnMemPoolMessageReceived()
Context.Parent.Tell(Message.Create("inv", payload));
}

private void OnPingMessageReceived(PingPayload payload)
{
Context.Parent.Tell(payload);
Context.Parent.Tell(Message.Create("pong", PingPayload.Create(Blockchain.Singleton.Height)));
}

private void OnPongMessageReceived(PingPayload payload)
{
Context.Parent.Tell(payload);
}

private void OnVerackMessageReceived()
{
verack = true;
Context.Parent.Tell(new SetVerack());
Context.Parent.Tell("verack");
}

private void OnVersionMessageReceived(VersionPayload payload)
{
version = payload;
Context.Parent.Tell(new SetVersion { Version = payload });
Context.Parent.Tell(payload);
}

public static Props Props(NeoSystem system)
Expand Down
25 changes: 19 additions & 6 deletions neo/Network/P2P/RemoteNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal class Relay { public IInventory Inventory; }
public IPEndPoint Listener => new IPEndPoint(Remote.Address, ListenerPort);
public override int ListenerPort => Version?.Port ?? 0;
public VersionPayload Version { get; private set; }
public uint LastBlockIndex { get; private set; }

public RemoteNode(NeoSystem system, object connection, IPEndPoint remote, IPEndPoint local)
: base(connection, remote, local)
Expand Down Expand Up @@ -63,6 +64,8 @@ private void EnqueueMessage(Message message)
case "getblocks":
case "getheaders":
case "mempool":
case "ping":
case "pong":
is_single = true;
break;
}
Expand Down Expand Up @@ -114,18 +117,27 @@ protected override void OnReceive(object message)
case Relay relay:
OnRelay(relay.Inventory);
break;
case ProtocolHandler.SetVersion setVersion:
OnSetVersion(setVersion.Version);
case VersionPayload payload:
OnVersionPayload(payload);
break;
case ProtocolHandler.SetVerack _:
OnSetVerack();
case "verack":
OnVerack();
break;
case ProtocolHandler.SetFilter setFilter:
OnSetFilter(setFilter.Filter);
break;
case PingPayload payload:
OnPingPayload(payload);
break;
}
}

private void OnPingPayload(PingPayload payload)
{
if (payload.LastBlockIndex > LastBlockIndex)
LastBlockIndex = payload.LastBlockIndex;
}

private void OnRelay(IInventory inventory)
{
if (Version?.Relay != true) return;
Expand Down Expand Up @@ -153,16 +165,17 @@ private void OnSetFilter(BloomFilter filter)
bloom_filter = filter;
}

private void OnSetVerack()
private void OnVerack()
{
verack = true;
system.TaskManager.Tell(new TaskManager.Register { Version = Version });
CheckMessageQueue();
}

private void OnSetVersion(VersionPayload version)
private void OnVersionPayload(VersionPayload version)
{
this.Version = version;
this.LastBlockIndex = Version.StartHeight;
if (version.Nonce == LocalNode.Nonce)
{
Disconnect(true);
Expand Down