Skip to content
Open
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
18 changes: 16 additions & 2 deletions plugins/RpcServer/RpcServer.Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private static JObject GetRelayResult(VerifyResult reason, UInt256 hash)
/// "result": {
/// "tcpport": 10333, // The TCP port,
/// "nonce": 1, // The nonce,
/// "useragent": "The user agent",
/// "useragent": "/Neo:3.10.2/", // Node assembly version in the existing useragent field,
/// "rpc": {
/// "maxiteratorresultitems": 100, // The maximum number of items in the iterator result,
/// "sessionenabled": false // Whether session is enabled,
Expand Down Expand Up @@ -152,7 +152,8 @@ protected internal virtual JToken GetVersion()
JObject json = new();
json["tcpport"] = localNode.ListenerTcpPort;
json["nonce"] = LocalNode.Nonce;
json["useragent"] = LocalNode.UserAgent;
var nodeVersion = typeof(RpcServer).Assembly.GetName().Version?.ToString(3) ?? "0.0.0";
json["useragent"] = UserAgentWithVersion(LocalNode.UserAgent, nodeVersion);
// rpc settings
JObject rpc = new();
rpc["maxiteratorresultitems"] = settings.MaxIteratorResultItems;
Expand Down Expand Up @@ -183,6 +184,19 @@ protected internal virtual JToken GetVersion()
return json;
}

/// <summary>
/// Keeps the P2P useragent product name and replaces the version with <paramref name="version"/>.
/// <c>/Neo:3.10.1/</c> plus <c>3.10.2</c> becomes <c>/Neo:3.10.2/</c>.
/// </summary>
internal static string UserAgentWithVersion(string userAgent, string version)
{
var colon = userAgent.IndexOf(':');
var slash = userAgent.LastIndexOf('/');
if (colon >= 0 && slash > colon)
return string.Concat(userAgent.AsSpan(0, colon + 1), version, userAgent.AsSpan(slash));
return $"/Neo:{version}/";
}

/// <summary>
/// Removes a specified prefix from a string if it exists.
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions tests/Neo.Plugins.RpcServer.Tests/UT_RpcServer.Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ public void TestGetVersion()
Assert.IsTrue(json.ContainsProperty("tcpport"));
Assert.IsTrue(json.ContainsProperty("nonce"));
Assert.IsTrue(json.ContainsProperty("useragent"));
Assert.IsFalse(json.ContainsProperty("version"));
var nodeVersion = _rpcServer.GetType().Assembly.GetName().Version?.ToString(3) ?? "0.0.0";
Assert.AreEqual(RpcServer.UserAgentWithVersion(LocalNode.UserAgent, nodeVersion), json["useragent"]!.AsString());
Assert.Contains($":{nodeVersion}/", json["useragent"]!.AsString());

Assert.IsTrue(json.ContainsProperty("rpc"));
var rpc = (JObject)json["rpc"]!;
Assert.IsTrue(rpc.ContainsProperty("maxiteratorresultitems"));
Assert.IsTrue(rpc.ContainsProperty("sessionenabled"));
Assert.AreEqual(_rpcServerSettings.MaxIteratorResultItems, rpc["maxiteratorresultitems"]!.AsNumber());
Assert.AreEqual(_rpcServerSettings.SessionEnabled, rpc["sessionenabled"]!.AsBoolean());

Assert.IsTrue(json.ContainsProperty("protocol"));
var protocol = (JObject)json["protocol"];
Expand All @@ -132,6 +143,15 @@ public void TestGetVersion()
Assert.IsTrue(protocol.ContainsProperty("seedlist"));
}

[TestMethod]
public void TestUserAgentWithVersion_ReplacesVersionKeepsProduct()
{
Assert.AreEqual("/Neo:3.10.2/", RpcServer.UserAgentWithVersion("/Neo:3.10.1/", "3.10.2"));
Assert.AreEqual("/MyNode:3.10.2/", RpcServer.UserAgentWithVersion("/MyNode:1.0.0/", "3.10.2"));
Assert.AreEqual("/Neo:3.10.2/", RpcServer.UserAgentWithVersion("not-a-ua", "3.10.2"));
Assert.AreEqual("/Neo:3.10.2/", RpcServer.UserAgentWithVersion(string.Empty, "3.10.2"));
}

[TestMethod]
public void TestGetVersion_HardforksStructure()
{
Expand Down
Loading