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 all commits
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
7 changes: 7 additions & 0 deletions neo-modules.sln
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SQLiteWallet", "src\SQLiteW
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StorageDumper", "src\StorageDumper\StorageDumper.csproj", "{938D86EA-0F48-436B-9255-4AD9A8E6B9AC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WsRpcJsonServer", "src\WebSocketServer\WsRpcJsonServer.csproj", "{BAB59C2B-BBD6-4977-9CEA-FFAD45150204}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -121,6 +123,10 @@ Global
{938D86EA-0F48-436B-9255-4AD9A8E6B9AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{938D86EA-0F48-436B-9255-4AD9A8E6B9AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{938D86EA-0F48-436B-9255-4AD9A8E6B9AC}.Release|Any CPU.Build.0 = Release|Any CPU
{BAB59C2B-BBD6-4977-9CEA-FFAD45150204}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BAB59C2B-BBD6-4977-9CEA-FFAD45150204}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BAB59C2B-BBD6-4977-9CEA-FFAD45150204}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BAB59C2B-BBD6-4977-9CEA-FFAD45150204}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -144,6 +150,7 @@ Global
{8D2EE375-2E2D-45FE-A4E9-0254D12C7554} = {59D802AB-C552-422A-B9C3-64D329FBCDCC}
{D121D57A-512E-4F74-ADA1-24482BF5C42B} = {97E81C78-1637-481F-9485-DA1225E94C23}
{938D86EA-0F48-436B-9255-4AD9A8E6B9AC} = {97E81C78-1637-481F-9485-DA1225E94C23}
{BAB59C2B-BBD6-4977-9CEA-FFAD45150204} = {97E81C78-1637-481F-9485-DA1225E94C23}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {61D3ADE6-BBFC-402D-AB42-1C71C9F9EDE3}
Expand Down
41 changes: 41 additions & 0 deletions src/WebSocketServer/Models/NeoUtilityLogResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// NeoUtilityLogResult.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Json;

namespace Neo.Plugins.WsRpcJsonServer.Models
{
internal class NeoUtilityLogResult
{
public string? SourceName { get; init; }
public LogLevel LogLevel { get; init; }
public object? Message { get; init; }

public static NeoUtilityLogResult Create(string sourceName, LogLevel level, object message) =>
new()
{
SourceName = sourceName,
LogLevel = level,
Message = message
};

public override string ToString() =>
$"{ToJson()}";

public JToken ToJson() =>
new JObject()
{
["source"] = SourceName,
["level"] = $"{LogLevel}",
["message"] = $"{Message}",
};
}
}
34 changes: 34 additions & 0 deletions src/WebSocketServer/Models/RpcJsonRequestMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// RpcJsonRequestMessage.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Json;
using System;

namespace Neo.Plugins.WsRpcJsonServer.Models
{
internal class RpcJsonRequestMessage
{
public Version? Version { get; set; }
public int Id { get; set; }
public string? Method { get; set; }
public JArray? Params { get; set; }

public static RpcJsonRequestMessage FromJson(JToken? message) =>
new()
{
Version = message?["jsonrpc"] == null ? null : new(message["jsonrpc"]!.AsString()),
Id = message?["id"] == null ? 0 : unchecked((int)message["id"]!.AsNumber()),
Method = message?["method"]?.AsString(),
Params = message?["params"] == null ? null : message["params"]! as JArray,
};

}
}
66 changes: 66 additions & 0 deletions src/WebSocketServer/Models/RpcJsonResponseMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// RpcJsonResponseMessage.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Json;
using System;
using System.Text;

namespace Neo.Plugins.Models.WsRpcJsonServer
{
internal class RpcJsonResponseMessage : IEquatable<RpcJsonResponseMessage>
{
public Version Version { get; private init; } = new("2.0");
public int Id { get; private init; }
public JToken? Result { get; private init; }

internal static RpcJsonResponseMessage Create(int id, JToken result) =>
new()
{
Id = id,
Result = result,
};

public JToken ToJson() =>
new JObject()
{
["jsonrpc"] = $"{Version}",
["id"] = Id,
["result"] = Result,
};

public static RpcJsonResponseMessage FromJson(JToken message) =>
new()
{
Version = message["jsonrpc"] == null ? new("1.0") : new(message["jsonrpc"]!.AsString()),
Id = message["id"] == null ? 0 : unchecked((int)message["id"]!.AsNumber()),
Result = message["result"],
};

public override string ToString() =>
$"{ToJson()}";

public byte[] ToArray() =>
Encoding.UTF8.GetBytes(ToString());

public bool Equals(RpcJsonResponseMessage? other) =>
other != null && other.Id == Id &&
other.Version == Version && other.Result == Result;

public override bool Equals(object? obj)
{
if (ReferenceEquals(this, obj)) return true;
return Equals(obj as RpcJsonResponseMessage);
}

public override int GetHashCode() =>
HashCode.Combine(this, Version, Id, Result);
}
}
32 changes: 32 additions & 0 deletions src/WebSocketServer/WalletSession.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// WalletSession.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Wallets;
using System;

namespace Neo.Plugins.WsRpcJsonServer
{
public class WalletSession
{
public Wallet Wallet { get; private init; }
public DateTime Expires { get; private set; }

public WalletSession(
Wallet wallet)
{
Wallet = wallet;
ResetExpiration();
}

public void ResetExpiration() =>
Expires = DateTime.UtcNow.AddSeconds(WsRpcJsonKestrelSettings.Current?.WalletSessionTimeout ?? WsRpcJsonKestrelSettings.Default.WalletSessionTimeout);
}
}
43 changes: 43 additions & 0 deletions src/WebSocketServer/WalletSessionManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// WalletSessionManager.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Neo.Plugins.WsRpcJsonServer
{
public class WalletSessionManager : ConcurrentDictionary<Guid, WalletSession>
{
private readonly PeriodicTimer _timer;

public WalletSessionManager()
{
_timer = new(TimeSpan.FromSeconds(1));
_ = Task.Run(SessionTimeoutAsync);
}

private async Task SessionTimeoutAsync()
{
while (await _timer.WaitForNextTickAsync())
{
var killAll = this.Where(w => w.Value.Expires <= DateTime.UtcNow)
.Select(s => Task.Run(() =>
{
TryRemove(s);
}));
await Task.WhenAll(killAll);
}
}
}
}
28 changes: 28 additions & 0 deletions src/WebSocketServer/WebSocketChannel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// WebSocketChannel.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Json;

namespace Neo.Plugins.WsRpcJsonServer
{
internal class WebSocketChannel
{
public WebSocketChannelType? ChannelType { get; private init; }
public JToken? Params { get; private init; }

public static WebSocketChannel Create(WebSocketChannelType channelType, JToken? args) =>
new()
{
ChannelType = channelType,
Params = args ?? JToken.Null,
};
}
}
24 changes: 24 additions & 0 deletions src/WebSocketServer/WebSocketChannelType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// WebSocketChannelType.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

namespace Neo.Plugins.WsRpcJsonServer
{
public enum WebSocketChannelType : byte
{
Block = 0x02, // Blockchain.Committed
MemoryPool = 0x03, // MemoryPool
Transaction = 0x05, // Transaction
ContractNotify = 0x14, // ApplicationEngine.Notify
AppLog = 0x15, // ApplicationEngine.Log
DebugLog = 0x16, // Utility.Log
Error = 0x19, // Error(s)
}
}
Loading
Loading