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

RPC-JSON over WebSockets #847

Closed
wants to merge 50 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
0f70f8d
init project websocketserver
cschuchardt88 Nov 22, 2023
e2de661
Updated json
cschuchardt88 Nov 22, 2023
35d7c70
dotnet format
cschuchardt88 Nov 23, 2023
983d543
Removed unused code
cschuchardt88 Nov 23, 2023
528e1ca
Fixed codeql
cschuchardt88 Nov 23, 2023
cf968be
Added EnableBasicAuthentication, User, Pass, AllowOrigins and Concurr…
cschuchardt88 Nov 23, 2023
5a76fab
Updated SslCert to SslCertFile
cschuchardt88 Nov 23, 2023
9410d92
fix codeql again
cschuchardt88 Nov 23, 2023
665e4f5
revert *.csproj files configuation
cschuchardt88 Nov 23, 2023
e5a0e29
code clean up
cschuchardt88 Nov 25, 2023
8c8f611
fixed format
cschuchardt88 Nov 25, 2023
8e1c9f7
format again
cschuchardt88 Nov 25, 2023
e20e84c
one more time
cschuchardt88 Nov 25, 2023
ec72689
one more (project files) dotnet format dont work on them
cschuchardt88 Nov 25, 2023
8d10b0c
Add new features
cschuchardt88 Nov 26, 2023
5df1de0
changed function to static
cschuchardt88 Nov 26, 2023
03015d7
dotnet format
cschuchardt88 Nov 26, 2023
5acf717
Merge branch 'master' into WebSocketServer
cschuchardt88 Dec 3, 2023
2f8d05f
Fixed disconnect issue
cschuchardt88 Dec 3, 2023
67e0f91
Build out basic blockchain methods for getting data
cschuchardt88 Dec 3, 2023
fc67dee
INIT PROJECT
cschuchardt88 Dec 3, 2023
f21c286
Fixed MERGE
cschuchardt88 Dec 3, 2023
4bc67df
Fixed sln file
cschuchardt88 Dec 3, 2023
2597889
Added basic protocol methods for the node information.
cschuchardt88 Dec 3, 2023
8b240b5
Add mempool and code clean up
cschuchardt88 Dec 5, 2023
207fe70
Merge branch 'master' into WebSocketServer
cschuchardt88 Dec 6, 2023
f54ed8f
Code clean up
cschuchardt88 Dec 12, 2023
86d915d
Touched up code and reuseable with ws protocol changes
cschuchardt88 Dec 22, 2023
3d999cb
Updated SendAllJson, added pluginId
cschuchardt88 Dec 22, 2023
baaccad
Added Invoke Contract
cschuchardt88 Dec 25, 2023
b7af661
Fixed exception null in InvokeContract
cschuchardt88 Dec 25, 2023
19b8da6
Fixed gasconsumed in InvokeContract method, was output as string, now…
cschuchardt88 Dec 25, 2023
987dc46
Remove virtual methods from WebSocketConnection
cschuchardt88 Dec 25, 2023
8e53db8
Fixed IEquatable for WebSocketResponseMessage
cschuchardt88 Dec 25, 2023
f6ff6a8
Add some wallet methods to the api, not completed yet.
cschuchardt88 Dec 26, 2023
53cefdf
Added more wallet methods
cschuchardt88 Dec 29, 2023
3bb4306
Add more wallet methods
cschuchardt88 Dec 29, 2023
1832fd8
finsished the wallet api
cschuchardt88 Dec 30, 2023
8dcb91e
Update WalletSessionManager.cs
cschuchardt88 Dec 30, 2023
074649f
Remove TClient and null checks
shargon Jan 3, 2024
8927853
Prevent problems with date seasons changes
shargon Jan 3, 2024
32a867e
Revert "Remove TClient and null checks"
cschuchardt88 Jan 3, 2024
1ec7644
Fixed WebSocketClient
cschuchardt88 Jan 3, 2024
ecbb806
Fixed error
cschuchardt88 Jan 3, 2024
307d263
Made project nullable
cschuchardt88 Jan 3, 2024
23d9a34
Update BlockchainMethods.cs
cschuchardt88 Jan 3, 2024
ac6526f
Merge branch 'master' into WebSocketServer
cschuchardt88 Jan 12, 2024
580b864
format
cschuchardt88 Jan 12, 2024
7d8f492
RPC-JSON added still needs programming
cschuchardt88 Jan 12, 2024
35675e2
Added event system
cschuchardt88 Jan 13, 2024
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
8 changes: 4 additions & 4 deletions src/WebSocketServer/WebSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Neo.Plugins
{
internal class WebSocketClient : IDisposable, IEquatable<WebSocketClient>
{
public WebSocket Socket { get; init; }
public WebSocket? Socket { get; init; }
Copy link
Member

Choose a reason for hiding this comment

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

We don't need this with ?, only is needed because you want a T type that we don't need

Copy link
Member Author

@cschuchardt88 cschuchardt88 Jan 3, 2024

Choose a reason for hiding this comment

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

I don't understand. Referring to T type; we do need it! This a BASE CLASS. NOT DOING SO WILL LIMIT AND FLAW EXPANDABLE OF AND POINT OF THIS LIBRARY. If we want to keep limiting the api we can do that. Just look at RpcServer is outdated and cannot be expanded or updated easily. Which is why no one touches it and in the state it's in now.


public bool IsConnected =>
Socket != null &&
Expand All @@ -25,7 +25,7 @@ public async Task SendJsonAsync(JToken message)
{
if (IsConnected)
{
await Socket.SendAsync(
await Socket!.SendAsync(
new(Encoding.UTF8.GetBytes(message.ToString())),
WebSocketMessageType.Text,
true,
Expand All @@ -50,13 +50,13 @@ public async Task CloseAsync(WebSocketCloseStatus status)

#region IEquatable

public bool Equals(WebSocketClient other) =>
public bool Equals(WebSocketClient? other) =>
ReferenceEquals(Socket, other?.Socket);

public override int GetHashCode() =>
HashCode.Combine(this, Socket);

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (ReferenceEquals(obj, this))
return true;
Expand Down
16 changes: 8 additions & 8 deletions src/WebSocketServer/WebSocketConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ internal class WebSocketConnection<TClient> : IDictionary<Guid, TClient>, IDispo
{
private readonly ConcurrentDictionary<Guid, TClient> _clients;

public static event WebSocketMessageSent OnMessageSent;
public static event WebSocketMessageReceived OnMessageReceived;
public static event WebSocketConnect OnConnect;
public static event WebSocketDisconnect OnDisconnect;
public static event WebSocketMessageSent? OnMessageSent;
public static event WebSocketMessageReceived? OnMessageReceived;
public static event WebSocketConnect? OnConnect;
public static event WebSocketDisconnect? OnDisconnect;

public WebSocketConnection()
{
Expand Down Expand Up @@ -125,10 +125,10 @@ public async Task ProcessClientAsync(WebSocket client, TaskCompletionSource tcs)
{
requestId = message.RequestId;

if (WebSocketServerPlugin.Methods.TryGetValue(message.Method, out var callMethod) == false)
if (WebSocketServerPlugin.Methods.TryGetValue(message.Method!, out var callMethod) == false)
throw new WebSocketException(-32601, "Method not found");

var obj = callMethod(message.Params);
var obj = callMethod(message.Params!);

if (obj is Task<JToken> responseTask)
obj = await responseTask.ConfigureAwait(false);
Expand Down Expand Up @@ -173,8 +173,8 @@ private async Task SendJsonAndCleanUpAsync(KeyValuePair<Guid, TClient> kvp, JTok

private static async Task<WebSocketRequestMessage> ReceiveMessageAsync(Guid clientId, WebSocket client)
{
var buffer = new byte[WebSocketServerSettings.Current.MessageSize]; // 4096 bytes
WebSocketReceiveResult receiveResult = null;
var buffer = new byte[WebSocketServerSettings.Current?.MessageSize ?? WebSocketServerSettings.Default.MessageSize]; // 4096 bytes
WebSocketReceiveResult? receiveResult = null;

using var ms = new MemoryStream();
while (receiveResult == null || receiveResult.EndOfMessage == false)
Expand Down
4 changes: 2 additions & 2 deletions src/WebSocketServer/WebSocketErrorResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace Neo.Plugins
internal class WebSocketErrorResult
{
public int Code { get; init; }
public string Message { get; init; }
public string Message { get; init; } = string.Empty;
#if DEBUG
public string StackTrace { get; init; }
public string? StackTrace { get; init; }
#endif

public static WebSocketErrorResult Create(Exception exception) =>
Expand Down
4 changes: 3 additions & 1 deletion src/WebSocketServer/WebSocketExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public static JToken ToJson(this NotifyEventArgs args, bool showTxHash = true)
{
["scripthash"] = $"{args?.ScriptHash}",
["eventname"] = $"{args?.EventName}",
["state"] = new JArray(args?.State?.Select(s => s.ToJson())),
["state"] = args?.State?.Count > 0 ?
new JArray(args!.State.Select(s => s.ToJson())) :
Array.Empty<JToken?>(),
};

if (showTxHash)
Expand Down
2 changes: 1 addition & 1 deletion src/WebSocketServer/WebSocketMethodAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Neo.Plugins
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
internal class WebSocketMethodAttribute : Attribute
{
public string Name { get; set; }
public string Name { get; set; } = string.Empty;
}
}
16 changes: 8 additions & 8 deletions src/WebSocketServer/WebSocketRequestMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ namespace Neo.Plugins
{
internal class WebSocketRequestMessage
{
public Version Version { get; set; }
public Version? Version { get; set; }
public int RequestId { get; set; }
public string Method { get; set; }
public JArray Params { get; set; }
public string? Method { get; set; }
public JArray? Params { get; set; }

public static WebSocketRequestMessage FromJson(JToken message) =>
public static WebSocketRequestMessage FromJson(JToken? message) =>
new()
{
Version = new(message["version"].AsString()),
RequestId = checked((int)message["requestid"].AsNumber()),
Method = message["method"].AsString(),
Params = (JArray)message["params"],
Version = message?["version"] == null ? null : new(message["version"]!.AsString()),
RequestId = message?["requestid"] == null ? 0 : checked((int)message["requestid"]!.AsNumber()),
Method = message?["method"]?.AsString(),
Params = message?["params"] == null ? null : (JArray)message["params"]!,
};

}
Expand Down
12 changes: 6 additions & 6 deletions src/WebSocketServer/WebSocketResponseMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class WebSocketResponseMessage : IEquatable<WebSocketResponseMessage>
public Version Version { get; private init; } = new("1.0");
public int RequestId { get; private init; }
public byte EventId { get; private init; }
public JToken Result { get; private init; }
public JToken? Result { get; private init; }

internal static WebSocketResponseMessage Create(int requestId, JToken result, WebSocketResponseMessageEvent eventId) =>
new()
Expand Down Expand Up @@ -39,9 +39,9 @@ public JToken ToJson() =>
public static WebSocketResponseMessage FromJson(JToken message) =>
new()
{
Version = new(message["version"].AsString()),
RequestId = checked((int)message["requestid"].AsNumber()),
EventId = (byte)message["eventid"].AsNumber(),
Version = message["version"] == null ? new("1.0") : new(message["version"]!.AsString()),
RequestId = message["requestid"] == null ? 0 : checked((int)message["requestid"]!.AsNumber()),
EventId = message["eventid"] == null ? (byte)0 : unchecked((byte)message["eventid"]!.AsNumber()),
Result = message["result"],
};

Expand All @@ -51,11 +51,11 @@ public override string ToString() =>
public byte[] ToArray() =>
Encoding.UTF8.GetBytes(ToString());

public bool Equals(WebSocketResponseMessage other) =>
public bool Equals(WebSocketResponseMessage? other) =>
other != null && other.EventId == EventId && other.RequestId == RequestId &&
other.Version == Version && other.Result == Result;

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (ReferenceEquals(this, obj)) return true;
return Equals(obj as WebSocketResponseMessage);
Expand Down
3 changes: 2 additions & 1 deletion src/WebSocketServer/WebSocketServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

<PropertyGroup>
<PackageId>Neo.Plugins.WebSocketServer</PackageId>
<RootNamespace>Neo.Plugins</RootNamespace>
<RootNamespace>Neo.Plugins</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
38 changes: 19 additions & 19 deletions src/WebSocketServer/WebSocketServerPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ namespace Neo.Plugins
public delegate void WebSocketDisconnect(Guid clientId);
public delegate void WebSocketRequest(HttpContext httpContext);
public delegate void WebSocketServerStarted();
public delegate void WebSocketMessageReceived(Guid clientId, JToken json);
public delegate void WebSocketMessageSent(Guid clientId, JToken json);
public delegate void WebSocketMessageReceived(Guid clientId, JToken? json);
public delegate void WebSocketMessageSent(Guid clientId, JToken? json);

public class WebSocketServerPlugin : Plugin
{
Expand All @@ -35,19 +35,19 @@ public class WebSocketServerPlugin : Plugin

internal static readonly Dictionary<string, Func<JArray, object>> Methods;
private static readonly WebSocketConnection<WebSocketClient> _connections;
public static event WebSocketRequest OnRequest;
public static event WebSocketServerStarted OnServerStarted;
public static event WebSocketRequest? OnRequest;
public static event WebSocketServerStarted? OnServerStarted;

#endregion

private readonly WebSocketOptions _webSocketOptions;
private readonly List<NotifyEventArgs> _notifyEvents;

private BlockchainMethods blockchainMethods;
private NeoSystem _neoSystem;
private BlockchainMethods? blockchainMethods;
private NeoSystem? _neoSystem;


private IWebHost _host;
private IWebHost? _host;

#endregion

Expand Down Expand Up @@ -86,7 +86,7 @@ public override void Dispose()
_host?.Dispose();
_connections?.Dispose();
if (_neoSystem != null)
cschuchardt88 marked this conversation as resolved.
Show resolved Hide resolved
_neoSystem.MemPool.TransactionAdded -= OnMemPoolTransactionAdded;
_neoSystem.MemPool.TransactionAdded -= OnMemPoolTransactionAdded!;
Blockchain.Committing -= OnBlockchainCommitting;
Blockchain.Committed -= OnBlockchainCommitted;
GC.SuppressFinalize(this);
Expand All @@ -106,11 +106,11 @@ protected override void OnSystemLoaded(NeoSystem system)

if (WebSocketServerSettings.Current.DebugMode)
{
ApplicationEngine.Log += OnApplicationEngineLog;
ApplicationEngine.Log += OnApplicationEngineLog!;
Utility.Logging += OnUtilityLogging;
}

_neoSystem.MemPool.TransactionAdded += OnMemPoolTransactionAdded;
_neoSystem.MemPool.TransactionAdded += OnMemPoolTransactionAdded!;
//_neoSystem.MemPool.TransactionRemoved += OnMemPoolTransactionRemoved;

blockchainMethods = new BlockchainMethods(system);
Expand All @@ -130,7 +130,7 @@ private void OnMemPoolTransactionAdded(object sender, Network.P2P.Payloads.Trans
await _connections.SendAllJsonAsync(
WebSocketResponseMessage.Create(
RandomNumberGenerator.GetInt32(2, int.MaxValue),
e.ToJson(_neoSystem.Settings),
e.ToJson(_neoSystem?.Settings),
WebSocketResponseMessageEvent.MemoryPool)
.ToJson())
.ConfigureAwait(false));
Expand Down Expand Up @@ -303,9 +303,9 @@ private void StartWebSocketServer()
.UseKestrel(options =>
{
options.AddServerHeader = false;
options.Listen(WebSocketServerSettings.Current.BindAddress, WebSocketServerSettings.Current.Port, config =>
options.Listen(WebSocketServerSettings.Current?.BindAddress ?? WebSocketServerSettings.Default.BindAddress, WebSocketServerSettings.Current?.Port ?? WebSocketServerSettings.Default.Port, config =>
{
if (string.IsNullOrEmpty(WebSocketServerSettings.Current.SslCertFile))
if (string.IsNullOrEmpty(WebSocketServerSettings.Current?.SslCertFile))
return;
config.UseHttps(WebSocketServerSettings.Current.SslCertFile, WebSocketServerSettings.Current.SslCertPassword, httpsConnectionAdapterOptions =>
{
Expand All @@ -316,8 +316,8 @@ private void StartWebSocketServer()
{
if (err != SslPolicyErrors.None)
return false;
shargon marked this conversation as resolved.
Show resolved Hide resolved
X509Certificate2 authority = chain.ChainElements[^1].Certificate;
return WebSocketServerSettings.Current.TrustedAuthorities.Contains(authority.Thumbprint);
X509Certificate2 authority = chain!.ChainElements[^1].Certificate;
return WebSocketServerSettings.Current.TrustedAuthorities.Contains(authority!.Thumbprint);
};
});
});
Expand All @@ -328,9 +328,9 @@ private void StartWebSocketServer()
//})
.Configure(app =>
{
_webSocketOptions.KeepAliveInterval = TimeSpan.FromSeconds(WebSocketServerSettings.Current.ConcurrentProxyTimeout);
_webSocketOptions.KeepAliveInterval = TimeSpan.FromSeconds(WebSocketServerSettings.Current?.ConcurrentProxyTimeout ?? WebSocketServerSettings.Default.ConcurrentProxyTimeout);

foreach (var origin in WebSocketServerSettings.Current.AllowOrigins)
foreach (var origin in WebSocketServerSettings.Current?.AllowOrigins ?? WebSocketServerSettings.Default.AllowOrigins)
_webSocketOptions.AllowedOrigins.Add(origin);

app.UseWebSockets(_webSocketOptions);
Expand All @@ -345,7 +345,7 @@ private void StartWebSocketServer()

private async Task ProcessRequestsAsync(HttpContext context)
{
if (WebSocketServerSettings.Current.EnableBasicAuthentication)
if (WebSocketServerSettings.Current?.EnableBasicAuthentication ?? WebSocketServerSettings.Default.EnableBasicAuthentication)
{
if (IsAuthorized(context) == false)
{
Expand Down Expand Up @@ -383,7 +383,7 @@ private static bool IsAuthorized(HttpContext context)
{
var decodedParams = Encoding.UTF8.GetString(Convert.FromBase64String(authValue.Parameter));
var creds = decodedParams.Split(':', 2);
if (creds[0] == WebSocketServerSettings.Current.User && creds[1] == WebSocketServerSettings.Current.Pass)
if (creds[0] == WebSocketServerSettings.Current?.User && creds[1] == WebSocketServerSettings.Current?.Pass)
return true;

}
Expand Down
16 changes: 8 additions & 8 deletions src/WebSocketServer/WebSocketServerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ namespace Neo.Plugins
public class WebSocketServerSettings
{
public uint Network { get; private init; }
public IPAddress BindAddress { get; private set; }
public IPAddress BindAddress { get; private set; } = IPAddress.Loopback;
public int Port { get; private init; }
public string SslCertFile { get; private init; }
public string SslCertPassword { get; private init; }
public string[] TrustedAuthorities { get; private init; }
public string? SslCertFile { get; private init; }
public string? SslCertPassword { get; private init; }
public string[] TrustedAuthorities { get; private init; } = Array.Empty<string>();
public bool EnableBasicAuthentication { get; private init; }
public string User { get; private init; }
public string Pass { get; private init; }
public string[] AllowOrigins { get; private init; }
public string? User { get; private init; }
public string? Pass { get; private init; }
public string[] AllowOrigins { get; private init; } = Array.Empty<string>();
public uint ConcurrentProxyTimeout { get; private init; }
public uint MessageSize { get; private init; }
public long MaxGasInvoke { get; private init; }
Expand All @@ -41,7 +41,7 @@ public class WebSocketServerSettings
DebugMode = false,
};

public static WebSocketServerSettings Current { get; private set; }
public static WebSocketServerSettings? Current { get; private set; }

internal static void Load(IConfigurationSection section)
{
Expand Down
4 changes: 2 additions & 2 deletions src/WebSocketServer/WebSocketUtilityLogResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace Neo.Plugins
{
internal class WebSocketUtilityLogResult
{
public string SourceName { get; init; }
public string? SourceName { get; init; }
public LogLevel LogLevel { get; init; }
public object Message { get; init; }
public object? Message { get; init; }

public static WebSocketUtilityLogResult Create(string sourceName, LogLevel level, object message) =>
new()
Expand Down
Loading