Skip to content

Commit fe3c4f9

Browse files
Wi1l-B0tcschuchardt88
authored andcommitted
Fix: some default values not matched (neo-project#4134)
1 parent ec4e75b commit fe3c4f9

File tree

3 files changed

+120
-74
lines changed

3 files changed

+120
-74
lines changed

src/Plugins/RpcServer/RcpServerSettings.cs

Lines changed: 78 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -33,78 +33,87 @@ public RpcServerSettings(IConfigurationSection section)
3333

3434
public record RpcServersSettings
3535
{
36-
public uint Network { get; init; }
37-
public IPAddress BindAddress { get; init; }
38-
public ushort Port { get; init; }
39-
public string SslCert { get; init; }
40-
public string SslCertPassword { get; init; }
41-
public string[] TrustedAuthorities { get; init; }
42-
public int MaxConcurrentConnections { get; init; }
43-
public int MaxRequestBodySize { get; init; }
44-
public string RpcUser { get; init; }
45-
public string RpcPass { get; init; }
46-
public bool EnableCors { get; init; }
47-
public string[] AllowOrigins { get; init; }
48-
public int KeepAliveTimeout { get; init; }
49-
public uint RequestHeadersTimeout { get; init; }
50-
// In the unit of datoshi, 1 GAS = 10^8 datoshi
51-
public long MaxGasInvoke { get; init; }
52-
// In the unit of datoshi, 1 GAS = 10^8 datoshi
53-
public long MaxFee { get; init; }
54-
public int MaxIteratorResultItems { get; init; }
55-
public int MaxStackSize { get; init; }
56-
public string[] DisabledMethods { get; init; }
57-
public bool SessionEnabled { get; init; }
58-
public TimeSpan SessionExpirationTime { get; init; }
59-
public int FindStoragePageSize { get; init; }
36+
public uint Network { get; init; } = 5195086u;
37+
public IPAddress BindAddress { get; init; } = IPAddress.Loopback;
38+
public ushort Port { get; init; } = 10332;
39+
public string SslCert { get; init; } = string.Empty;
40+
public string SslCertPassword { get; init; } = string.Empty;
41+
public string[] TrustedAuthorities { get; init; } = [];
42+
public int MaxConcurrentConnections { get; init; } = 40;
43+
public int MaxRequestBodySize { get; init; } = 5 * 1024 * 1024;
44+
public string RpcUser { get; init; } = string.Empty;
45+
public string RpcPass { get; init; } = string.Empty;
46+
public bool EnableCors { get; init; } = true;
47+
public string[] AllowOrigins { get; init; } = [];
6048

61-
public static RpcServersSettings Default { get; } = new RpcServersSettings
49+
/// <summary>
50+
/// The maximum time in seconds allowed for the keep-alive connection to be idle.
51+
/// </summary>
52+
public int KeepAliveTimeout { get; init; } = 60;
53+
54+
/// <summary>
55+
/// The maximum time in seconds allowed for the request headers to be read.
56+
/// </summary>
57+
public uint RequestHeadersTimeout { get; init; } = 15;
58+
59+
/// <summary>
60+
/// In the unit of datoshi, 1 GAS = 10^8 datoshi
61+
/// </summary>
62+
public long MaxGasInvoke { get; init; } = (long)new BigDecimal(10M, NativeContract.GAS.Decimals).Value;
63+
64+
/// <summary>
65+
/// In the unit of datoshi, 1 GAS = 10^8 datoshi
66+
/// </summary>
67+
public long MaxFee { get; init; } = (long)new BigDecimal(0.1M, NativeContract.GAS.Decimals).Value;
68+
public int MaxIteratorResultItems { get; init; } = 100;
69+
public int MaxStackSize { get; init; } = ushort.MaxValue;
70+
public string[] DisabledMethods { get; init; } = [];
71+
public bool SessionEnabled { get; init; } = false;
72+
public TimeSpan SessionExpirationTime { get; init; } = TimeSpan.FromSeconds(60);
73+
public int FindStoragePageSize { get; init; } = 50;
74+
75+
public static RpcServersSettings Default { get; } = new();
76+
77+
public static RpcServersSettings Load(IConfigurationSection section)
6278
{
63-
Network = 5195086u,
64-
BindAddress = IPAddress.None,
65-
SslCert = string.Empty,
66-
SslCertPassword = string.Empty,
67-
MaxGasInvoke = (long)new BigDecimal(10M, NativeContract.GAS.Decimals).Value,
68-
MaxFee = (long)new BigDecimal(0.1M, NativeContract.GAS.Decimals).Value,
69-
TrustedAuthorities = Array.Empty<string>(),
70-
EnableCors = true,
71-
AllowOrigins = Array.Empty<string>(),
72-
KeepAliveTimeout = 60,
73-
RequestHeadersTimeout = 15,
74-
MaxIteratorResultItems = 100,
75-
MaxStackSize = ushort.MaxValue,
76-
DisabledMethods = Array.Empty<string>(),
77-
MaxConcurrentConnections = 40,
78-
MaxRequestBodySize = 5 * 1024 * 1024,
79-
SessionEnabled = false,
80-
SessionExpirationTime = TimeSpan.FromSeconds(60),
81-
FindStoragePageSize = 50
82-
};
79+
var @default = Default;
80+
return new()
81+
{
82+
Network = section.GetValue("Network", @default.Network),
83+
BindAddress = IPAddress.Parse(section.GetValue("BindAddress", @default.BindAddress.ToString())),
84+
Port = section.GetValue("Port", @default.Port),
85+
SslCert = section.GetValue("SslCert", string.Empty),
86+
SslCertPassword = section.GetValue("SslCertPassword", string.Empty),
87+
TrustedAuthorities = GetStrings(section, "TrustedAuthorities"),
88+
RpcUser = section.GetValue("RpcUser", @default.RpcUser),
89+
RpcPass = section.GetValue("RpcPass", @default.RpcPass),
90+
EnableCors = section.GetValue(nameof(EnableCors), @default.EnableCors),
91+
AllowOrigins = GetStrings(section, "AllowOrigins"),
92+
KeepAliveTimeout = section.GetValue(nameof(KeepAliveTimeout), @default.KeepAliveTimeout),
93+
RequestHeadersTimeout = section.GetValue(nameof(RequestHeadersTimeout), @default.RequestHeadersTimeout),
94+
MaxGasInvoke = (long)new BigDecimal(section.GetValue<decimal>("MaxGasInvoke", @default.MaxGasInvoke), NativeContract.GAS.Decimals).Value,
95+
MaxFee = (long)new BigDecimal(section.GetValue<decimal>("MaxFee", @default.MaxFee), NativeContract.GAS.Decimals).Value,
96+
MaxIteratorResultItems = section.GetValue("MaxIteratorResultItems", @default.MaxIteratorResultItems),
97+
MaxStackSize = section.GetValue("MaxStackSize", @default.MaxStackSize),
98+
DisabledMethods = GetStrings(section, "DisabledMethods"),
99+
MaxConcurrentConnections = section.GetValue("MaxConcurrentConnections", @default.MaxConcurrentConnections),
100+
MaxRequestBodySize = section.GetValue("MaxRequestBodySize", @default.MaxRequestBodySize),
101+
SessionEnabled = section.GetValue("SessionEnabled", @default.SessionEnabled),
102+
SessionExpirationTime = TimeSpan.FromSeconds(section.GetValue("SessionExpirationTime", (long)@default.SessionExpirationTime.TotalSeconds)),
103+
FindStoragePageSize = section.GetValue("FindStoragePageSize", @default.FindStoragePageSize)
104+
};
105+
}
83106

84-
public static RpcServersSettings Load(IConfigurationSection section) => new()
107+
private static string[] GetStrings(IConfigurationSection section, string key)
85108
{
86-
Network = section.GetValue("Network", Default.Network),
87-
BindAddress = IPAddress.Parse(section.GetSection("BindAddress").Value),
88-
Port = ushort.Parse(section.GetSection("Port").Value),
89-
SslCert = section.GetSection("SslCert").Value,
90-
SslCertPassword = section.GetSection("SslCertPassword").Value,
91-
TrustedAuthorities = section.GetSection("TrustedAuthorities").GetChildren().Select(p => p.Get<string>()).ToArray(),
92-
RpcUser = section.GetSection("RpcUser").Value,
93-
RpcPass = section.GetSection("RpcPass").Value,
94-
EnableCors = section.GetValue(nameof(EnableCors), Default.EnableCors),
95-
AllowOrigins = section.GetSection(nameof(AllowOrigins)).GetChildren().Select(p => p.Get<string>()).ToArray(),
96-
KeepAliveTimeout = section.GetValue(nameof(KeepAliveTimeout), Default.KeepAliveTimeout),
97-
RequestHeadersTimeout = section.GetValue(nameof(RequestHeadersTimeout), Default.RequestHeadersTimeout),
98-
MaxGasInvoke = (long)new BigDecimal(section.GetValue<decimal>("MaxGasInvoke", Default.MaxGasInvoke), NativeContract.GAS.Decimals).Value,
99-
MaxFee = (long)new BigDecimal(section.GetValue<decimal>("MaxFee", Default.MaxFee), NativeContract.GAS.Decimals).Value,
100-
MaxIteratorResultItems = section.GetValue("MaxIteratorResultItems", Default.MaxIteratorResultItems),
101-
MaxStackSize = section.GetValue("MaxStackSize", Default.MaxStackSize),
102-
DisabledMethods = section.GetSection("DisabledMethods").GetChildren().Select(p => p.Get<string>()).ToArray(),
103-
MaxConcurrentConnections = section.GetValue("MaxConcurrentConnections", Default.MaxConcurrentConnections),
104-
MaxRequestBodySize = section.GetValue("MaxRequestBodySize", Default.MaxRequestBodySize),
105-
SessionEnabled = section.GetValue("SessionEnabled", Default.SessionEnabled),
106-
SessionExpirationTime = TimeSpan.FromSeconds(section.GetValue("SessionExpirationTime", (int)Default.SessionExpirationTime.TotalSeconds)),
107-
FindStoragePageSize = section.GetValue("FindStoragePageSize", Default.FindStoragePageSize)
108-
};
109+
List<string> list = [];
110+
foreach (var child in section.GetSection(key).GetChildren())
111+
{
112+
var value = child.Get<string>();
113+
if (value is null) throw new ArgumentException($"Invalid value for {key}");
114+
list.Add(value);
115+
}
116+
return list.ToArray();
117+
}
109118
}
110119
}

src/Plugins/RpcServer/RpcServer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public RpcServer(NeoSystem system, RpcServersSettings settings)
5656
this.system = system;
5757
this.settings = settings;
5858

59-
_rpcUser = settings.RpcUser is not null ? Encoding.UTF8.GetBytes(settings.RpcUser) : [];
60-
_rpcPass = settings.RpcPass is not null ? Encoding.UTF8.GetBytes(settings.RpcPass) : [];
59+
_rpcUser = string.IsNullOrEmpty(settings.RpcUser) ? [] : Encoding.UTF8.GetBytes(settings.RpcUser);
60+
_rpcPass = string.IsNullOrEmpty(settings.RpcPass) ? [] : Encoding.UTF8.GetBytes(settings.RpcPass);
6161

6262
var addressVersion = system.Settings.AddressVersion;
6363
ParameterConverter.RegisterConversion<SignersAndWitnesses>(token => token.ToSignersAndWitnesses(addressVersion));

tests/Neo.Plugins.RpcServer.Tests/UT_RpcServer.cs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// modifications are permitted.
1111

1212
using Microsoft.AspNetCore.Http;
13+
using Microsoft.Extensions.Configuration;
1314
using Microsoft.VisualStudio.TestTools.UnitTesting;
1415
using Neo.Json;
1516
using Neo.Persistence.Providers;
@@ -21,6 +22,7 @@
2122
using System;
2223
using System.IO;
2324
using System.Linq;
25+
using System.Net;
2426
using System.Text;
2527
using System.Threading.Tasks;
2628

@@ -37,9 +39,6 @@ public partial class UT_RpcServer
3739
private readonly NEP6Wallet _wallet = TestUtils.GenerateTestWallet("123");
3840
private WalletAccount _walletAccount;
3941

40-
const byte NativePrefixAccount = 20;
41-
const byte NativePrefixTotalSupply = 11;
42-
4342
[TestInitialize]
4443
public void TestSetup()
4544
{
@@ -280,5 +279,43 @@ public async Task TestRegisterMethods()
280279
Assert.AreEqual("mock", responseJson["result"].AsString());
281280
Assert.AreEqual(200, context.Response.StatusCode);
282281
}
282+
283+
[TestMethod]
284+
public void TestRpcServerSettings_Load()
285+
{
286+
var config = new ConfigurationBuilder()
287+
.AddJsonFile("RpcServer.json")
288+
.Build()
289+
.GetSection("PluginConfiguration")
290+
.GetSection("Servers")
291+
.GetChildren()
292+
.First();
293+
294+
var settings = RpcServersSettings.Load(config);
295+
Assert.AreEqual(860833102u, settings.Network);
296+
Assert.AreEqual(10332, settings.Port);
297+
Assert.AreEqual(IPAddress.Parse("127.0.0.1"), settings.BindAddress);
298+
Assert.AreEqual(string.Empty, settings.SslCert);
299+
Assert.AreEqual(string.Empty, settings.SslCertPassword);
300+
Assert.AreEqual(0, settings.TrustedAuthorities.Length);
301+
Assert.AreEqual(string.Empty, settings.RpcUser);
302+
Assert.AreEqual(string.Empty, settings.RpcPass);
303+
Assert.AreEqual(true, settings.EnableCors);
304+
Assert.AreEqual(20_00000000, settings.MaxGasInvoke);
305+
Assert.AreEqual(TimeSpan.FromSeconds(60), settings.SessionExpirationTime);
306+
Assert.AreEqual(false, settings.SessionEnabled);
307+
Assert.AreEqual(true, settings.EnableCors);
308+
Assert.AreEqual(0, settings.AllowOrigins.Length);
309+
Assert.AreEqual(60, settings.KeepAliveTimeout);
310+
Assert.AreEqual(15u, settings.RequestHeadersTimeout);
311+
Assert.AreEqual(1000_0000, settings.MaxFee); // 0.1 * 10^8
312+
Assert.AreEqual(100, settings.MaxIteratorResultItems);
313+
Assert.AreEqual(65535, settings.MaxStackSize);
314+
Assert.AreEqual(1, settings.DisabledMethods.Length);
315+
Assert.AreEqual("openwallet", settings.DisabledMethods[0]);
316+
Assert.AreEqual(40, settings.MaxConcurrentConnections);
317+
Assert.AreEqual(5 * 1024 * 1024, settings.MaxRequestBodySize);
318+
Assert.AreEqual(50, settings.FindStoragePageSize);
319+
}
283320
}
284321
}

0 commit comments

Comments
 (0)