-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable programmatic configuration of RpcServer plugin (#206)
* seperate out plugin from rpcServer functionality * Update src/RpcServer/RpcServer.cs * CR Feedback * fix encoding * fix build break Co-authored-by: Harry Pierson <[email protected]> Co-authored-by: Shargon <[email protected]>
- Loading branch information
1 parent
f451ca8
commit d62b18b
Showing
10 changed files
with
174 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Neo.Plugins | ||
{ | ||
public sealed class RpcServerPlugin : Plugin | ||
{ | ||
static List<object> handlers = new List<object>(); | ||
RpcServer server; | ||
RpcServerSettings settings; | ||
|
||
protected override void Configure() | ||
{ | ||
var loadedSettings = new RpcServerSettings(GetConfiguration()); | ||
if (this.settings == null) | ||
{ | ||
this.settings = loadedSettings; | ||
} | ||
else | ||
{ | ||
this.settings.UpdateSettings(loadedSettings); | ||
} | ||
} | ||
|
||
public override void Dispose() | ||
{ | ||
base.Dispose(); | ||
if (server != null) | ||
{ | ||
server.Dispose(); | ||
server = null; | ||
} | ||
} | ||
|
||
protected override void OnPluginsLoaded() | ||
{ | ||
this.server = new RpcServer(System, settings); | ||
|
||
foreach (var handler in handlers) | ||
{ | ||
this.server.RegisterMethods(handler); | ||
} | ||
handlers.Clear(); | ||
|
||
server.StartRpcServer(); | ||
} | ||
|
||
public static void RegisterMethods(object handler) | ||
{ | ||
// if RpcServerPlugin is already loaded, call RegisterMethods directly | ||
foreach (var plugin in Plugin.Plugins) | ||
{ | ||
if (plugin is RpcServerPlugin rpcServerPlugin) | ||
{ | ||
rpcServerPlugin.server.RegisterMethods(handler); | ||
return; | ||
} | ||
} | ||
|
||
// otherwise, save the handler for use during RpcServerPlugin load | ||
handlers.Add(handler); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Neo.SmartContract.Native; | ||
using System; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Numerics; | ||
|
||
namespace Neo.Plugins | ||
{ | ||
public class RpcServerSettings | ||
{ | ||
// These read-only properties are used to in the creation | ||
// of the WebHost RPC endpoint. These cannot be changed | ||
// while the server is running | ||
|
||
public IPAddress BindAddress { get; } | ||
public ushort Port { get; } | ||
public string SslCert { get; } | ||
public string SslCertPassword { get; } | ||
public string[] TrustedAuthorities { get; } | ||
public int MaxConcurrentConnections { get; } | ||
|
||
// these read-write properties can be changed while | ||
// the server is running via auto-reconfiguration | ||
|
||
public string RpcUser { get; private set; } | ||
public string RpcPass { get; private set; } | ||
public long MaxGasInvoke { get; private set; } | ||
public long MaxFee { get; private set; } | ||
public string[] DisabledMethods { get; private set; } | ||
|
||
public RpcServerSettings(IPAddress bindAddress = null, | ||
ushort port = 10332, | ||
string sslCert = "", | ||
string sslCertPassword = "", | ||
string[] trustedAuthorities = null, | ||
string rpcUser = "", | ||
string rpcPass = "", | ||
decimal maxGasInvoke = 10, | ||
decimal maxFee = (decimal)0.1, | ||
string[] disabledMethods = null, | ||
int maxConcurrentConnections = 40) | ||
{ | ||
this.BindAddress = bindAddress ?? IPAddress.Loopback; | ||
this.Port = port; | ||
this.SslCert = sslCert; | ||
this.SslCertPassword = sslCertPassword; | ||
this.TrustedAuthorities = trustedAuthorities ?? Array.Empty<string>(); | ||
this.RpcUser = rpcUser; | ||
this.RpcPass = rpcPass; | ||
this.MaxGasInvoke = (long)BigDecimal.Parse(maxGasInvoke.ToString(), NativeContract.GAS.Decimals).Value; | ||
this.MaxFee = (long)BigDecimal.Parse(maxFee.ToString(), NativeContract.GAS.Decimals).Value; | ||
this.DisabledMethods = disabledMethods ?? Array.Empty<string>(); | ||
this.MaxConcurrentConnections = maxConcurrentConnections; | ||
} | ||
|
||
public RpcServerSettings(IConfigurationSection section) | ||
{ | ||
this.BindAddress = IPAddress.Parse(section.GetSection("BindAddress").Value); | ||
this.Port = ushort.Parse(section.GetSection("Port").Value); | ||
this.SslCert = section.GetSection("SslCert").Value; | ||
this.SslCertPassword = section.GetSection("SslCertPassword").Value; | ||
this.TrustedAuthorities = section.GetSection("TrustedAuthorities").GetChildren().Select(p => p.Get<string>()).ToArray(); | ||
this.RpcUser = section.GetSection("RpcUser").Value; | ||
this.RpcPass = section.GetSection("RpcPass").Value; | ||
this.MaxGasInvoke = (long)BigDecimal.Parse(section.GetValue("MaxGasInvoke", "10"), NativeContract.GAS.Decimals).Value; | ||
this.MaxFee = (long)BigDecimal.Parse(section.GetValue("MaxFee", "0.1"), NativeContract.GAS.Decimals).Value; | ||
this.DisabledMethods = section.GetSection("DisabledMethods").GetChildren().Select(p => p.Get<string>()).ToArray(); | ||
this.MaxConcurrentConnections = section.GetValue("MaxConcurrentConnections", 40); | ||
} | ||
|
||
public void UpdateSettings(RpcServerSettings settings) | ||
{ | ||
this.RpcUser = settings.RpcUser; | ||
this.RpcPass = settings.RpcPass; | ||
this.MaxGasInvoke = settings.MaxGasInvoke; | ||
this.MaxFee = settings.MaxFee; | ||
this.DisabledMethods = settings.DisabledMethods; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.