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

Add RpcServer #160

Merged
merged 18 commits into from
Dec 15, 2019
Merged
Show file tree
Hide file tree
Changes from 13 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
21 changes: 7 additions & 14 deletions neo-modules.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{59D802AB
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApplicationLogs", "src\ApplicationLogs\ApplicationLogs.csproj", "{84DA8EA6-EF60-4FCD-B1C6-65C1A8323B3F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RpcSecurity", "src\RpcSecurity\RpcSecurity.csproj", "{6800D782-8EC0-49E9-98C4-195C8F781A1F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StatesDumper", "src\StatesDumper\StatesDumper.csproj", "{86531DB1-A231-46C4-823F-BE60972F7523}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RpcWallet", "src\RpcWallet\RpcWallet.csproj", "{EF32A7E5-EDF9-438C-8041-8DA6E675A7FD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RpcNep5Tracker", "src\RpcNep5Tracker\RpcNep5Tracker.csproj", "{BBE8AC15-12DF-4AF0-ABC1-F1557EB5DC8E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoreMetrics", "src\CoreMetrics\CoreMetrics.csproj", "{AEFFF003-3500-416B-AD9B-8C838C33C1F4}"
Expand All @@ -26,6 +22,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RocksDBStore", "src\RocksDB
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RpcClient", "src\RpcClient\RpcClient.csproj", "{8DC57A45-A192-4953-81B1-6907FB7C28D2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RpcServer", "src\RpcServer\RpcServer.csproj", "{1403FFE9-4265-4269-8E3D-5A79EFD108CA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.Network.RPC.Tests", "tests\Neo.Network.RPC.Tests\Neo.Network.RPC.Tests.csproj", "{D52460B3-AB5C-4D07-B400-9E7ADCB01FF5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.Plugins.Storage.Tests", "tests\Neo.Plugins.Storage.Tests\Neo.Plugins.Storage.Tests.csproj", "{9E7EA895-302A-4C0C-BA9B-54F9A67AD75C}"
Expand All @@ -40,18 +38,10 @@ Global
{84DA8EA6-EF60-4FCD-B1C6-65C1A8323B3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{84DA8EA6-EF60-4FCD-B1C6-65C1A8323B3F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{84DA8EA6-EF60-4FCD-B1C6-65C1A8323B3F}.Release|Any CPU.Build.0 = Release|Any CPU
{6800D782-8EC0-49E9-98C4-195C8F781A1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6800D782-8EC0-49E9-98C4-195C8F781A1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6800D782-8EC0-49E9-98C4-195C8F781A1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6800D782-8EC0-49E9-98C4-195C8F781A1F}.Release|Any CPU.Build.0 = Release|Any CPU
{86531DB1-A231-46C4-823F-BE60972F7523}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{86531DB1-A231-46C4-823F-BE60972F7523}.Debug|Any CPU.Build.0 = Debug|Any CPU
{86531DB1-A231-46C4-823F-BE60972F7523}.Release|Any CPU.ActiveCfg = Release|Any CPU
{86531DB1-A231-46C4-823F-BE60972F7523}.Release|Any CPU.Build.0 = Release|Any CPU
{EF32A7E5-EDF9-438C-8041-8DA6E675A7FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EF32A7E5-EDF9-438C-8041-8DA6E675A7FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF32A7E5-EDF9-438C-8041-8DA6E675A7FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF32A7E5-EDF9-438C-8041-8DA6E675A7FD}.Release|Any CPU.Build.0 = Release|Any CPU
{BBE8AC15-12DF-4AF0-ABC1-F1557EB5DC8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BBE8AC15-12DF-4AF0-ABC1-F1557EB5DC8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BBE8AC15-12DF-4AF0-ABC1-F1557EB5DC8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -76,6 +66,10 @@ Global
{8DC57A45-A192-4953-81B1-6907FB7C28D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8DC57A45-A192-4953-81B1-6907FB7C28D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8DC57A45-A192-4953-81B1-6907FB7C28D2}.Release|Any CPU.Build.0 = Release|Any CPU
{1403FFE9-4265-4269-8E3D-5A79EFD108CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1403FFE9-4265-4269-8E3D-5A79EFD108CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1403FFE9-4265-4269-8E3D-5A79EFD108CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1403FFE9-4265-4269-8E3D-5A79EFD108CA}.Release|Any CPU.Build.0 = Release|Any CPU
{D52460B3-AB5C-4D07-B400-9E7ADCB01FF5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D52460B3-AB5C-4D07-B400-9E7ADCB01FF5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D52460B3-AB5C-4D07-B400-9E7ADCB01FF5}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -90,15 +84,14 @@ Global
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{84DA8EA6-EF60-4FCD-B1C6-65C1A8323B3F} = {97E81C78-1637-481F-9485-DA1225E94C23}
{6800D782-8EC0-49E9-98C4-195C8F781A1F} = {97E81C78-1637-481F-9485-DA1225E94C23}
{86531DB1-A231-46C4-823F-BE60972F7523} = {97E81C78-1637-481F-9485-DA1225E94C23}
{EF32A7E5-EDF9-438C-8041-8DA6E675A7FD} = {97E81C78-1637-481F-9485-DA1225E94C23}
{BBE8AC15-12DF-4AF0-ABC1-F1557EB5DC8E} = {97E81C78-1637-481F-9485-DA1225E94C23}
{AEFFF003-3500-416B-AD9B-8C838C33C1F4} = {97E81C78-1637-481F-9485-DA1225E94C23}
{14DB62D5-0EA1-4A98-8656-1AA2D0345206} = {97E81C78-1637-481F-9485-DA1225E94C23}
{C66214CD-0B97-4EA5-B7A2-164F54346F19} = {97E81C78-1637-481F-9485-DA1225E94C23}
{0E2AAF05-C55A-4B36-8750-F55743FBE4B3} = {97E81C78-1637-481F-9485-DA1225E94C23}
{8DC57A45-A192-4953-81B1-6907FB7C28D2} = {97E81C78-1637-481F-9485-DA1225E94C23}
{1403FFE9-4265-4269-8E3D-5A79EFD108CA} = {97E81C78-1637-481F-9485-DA1225E94C23}
{D52460B3-AB5C-4D07-B400-9E7ADCB01FF5} = {59D802AB-C552-422A-B9C3-64D329FBCDCC}
{9E7EA895-302A-4C0C-BA9B-54F9A67AD75C} = {59D802AB-C552-422A-B9C3-64D329FBCDCC}
EndGlobalSection
Expand Down
1 change: 1 addition & 0 deletions src/ApplicationLogs/ApplicationLogs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<ItemGroup>
<ProjectReference Include="..\LevelDBStore\LevelDBStore.csproj" />
<ProjectReference Include="..\RpcServer\RpcServer.csproj" />
</ItemGroup>

</Project>
17 changes: 4 additions & 13 deletions src/ApplicationLogs/LogReader.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Microsoft.AspNetCore.Http;
using Neo.IO;
using Neo.IO.Data.LevelDB;
using Neo.IO.Json;
using Neo.Ledger;
using Neo.Network.RPC;
using Neo.Persistence;
using Neo.VM;
using System;
Expand All @@ -14,7 +12,7 @@

namespace Neo.Plugins
{
public class LogReader : Plugin, IRpcPlugin, IPersistencePlugin
public class LogReader : Plugin, IPersistencePlugin
{
private readonly DB db;

Expand All @@ -23,31 +21,24 @@ public class LogReader : Plugin, IRpcPlugin, IPersistencePlugin
public LogReader()
{
db = DB.Open(GetFullPath(Settings.Default.Path), new Options { CreateIfMissing = true });
RpcServer.RegisterMethods(this);
shargon marked this conversation as resolved.
Show resolved Hide resolved
}

protected override void Configure()
{
Settings.Load(GetConfiguration());
}

public void PreProcess(HttpContext context, string method, JArray _params)
[RpcMethod]
public JObject GetApplicationLog(JArray _params)
{
}

public JObject OnProcess(HttpContext context, string method, JArray _params)
{
if (method != "getapplicationlog") return null;
UInt256 hash = UInt256.Parse(_params[0].AsString());
byte[] value = db.Get(ReadOptions.Default, hash.ToArray());
if (value is null)
throw new RpcException(-100, "Unknown transaction");
return JObject.Parse(Encoding.UTF8.GetString(value));
}

public void PostProcess(HttpContext context, string method, JArray _params, JObject result)
{
}

public void OnPersist(StoreView snapshot, IReadOnlyList<Blockchain.ApplicationExecuted> applicationExecutedList)
{
WriteBatch writeBatch = new WriteBatch();
Expand Down
28 changes: 7 additions & 21 deletions src/CoreMetrics/CoreMetrics.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
using Microsoft.AspNetCore.Http;
using Neo.IO.Json;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;

namespace Neo.Plugins
{
public class CoreMetrics : Plugin, IRpcPlugin
public class CoreMetrics : Plugin
{
protected override void Configure() { }

public void PreProcess(HttpContext context, string method, JArray _params) { }

public JObject OnProcess(HttpContext context, string method, JArray _params)
public CoreMetrics()
{
switch (method)
{
case "getmetricblocktimestamp":
{
uint nBlocks = (uint)_params[0].AsNumber();
uint lastHeight = _params.Count >= 2 ? (uint)_params[1].AsNumber() : 0;
return GetBlocksTime(nBlocks, lastHeight);
}
default:
return null;
}
RpcServer.RegisterMethods(this);
}

public void PostProcess(HttpContext context, string method, JArray _params, JObject result) { }

private JObject GetBlocksTime(uint nBlocks, uint lastHeight)
[RpcMethod]
public JObject GetMetricBlockTimestamp(JArray _params)
{
uint nBlocks = (uint)_params[0].AsNumber();
uint lastHeight = _params.Count >= 2 ? (uint)_params[1].AsNumber() : 0;
// It is currently limited to query blocks generated in the last 24hours (86400 seconds)
uint maxNBlocksPerDay = 86400 / (Blockchain.MillisecondsPerBlock / 1000);
if (lastHeight != 0)
Expand Down
2 changes: 1 addition & 1 deletion src/CoreMetrics/CoreMetrics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Neo" Version="3.0.0-CI00817" />
<ProjectReference Include="..\RpcServer\RpcServer.csproj" />
</ItemGroup>

</Project>
30 changes: 12 additions & 18 deletions src/RpcNep5Tracker/RpcNep5Tracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Neo.IO.Json;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Network.RPC;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.VM;
Expand All @@ -18,7 +17,7 @@

namespace Neo.Plugins
{
public class RpcNep5Tracker : Plugin, IPersistencePlugin, IRpcPlugin
public class RpcNep5Tracker : Plugin, IPersistencePlugin
{
private const byte Nep5BalancePrefix = 0xf8;
private const byte Nep5TransferSentPrefix = 0xf9;
Expand All @@ -33,6 +32,11 @@ public class RpcNep5Tracker : Plugin, IPersistencePlugin, IRpcPlugin
private uint _maxResults;
private Neo.IO.Data.LevelDB.Snapshot _levelDbSnapshot;

public RpcNep5Tracker()
{
RpcServer.RegisterMethods(this);
}

protected override void Configure()
{
if (_db == null)
Expand Down Expand Up @@ -237,8 +241,11 @@ private UInt160 GetScriptHashFromParam(string addressOrScriptHash)
return addressOrScriptHash.Length < 40 ?
addressOrScriptHash.ToScriptHash() : UInt160.Parse(addressOrScriptHash);
}
private JObject GetNep5Transfers(JArray _params)

[RpcMethod]
public JObject GetNep5Transfers(JArray _params)
{
if (!_shouldTrackHistory) throw new RpcException(-32601, "Method not found");
UInt160 userScriptHash = GetScriptHashFromParam(_params[0].AsString());
// If start time not present, default to 1 week of history.
ulong startTime = _params.Count > 1 ? (ulong)_params[1].AsNumber() :
Expand All @@ -258,7 +265,8 @@ private JObject GetNep5Transfers(JArray _params)
return json;
}

private JObject GetNep5Balances(JArray _params)
[RpcMethod]
public JObject GetNep5Balances(JArray _params)
{
UInt160 userScriptHash = GetScriptHashFromParam(_params[0].AsString());

Expand All @@ -278,19 +286,5 @@ private JObject GetNep5Balances(JArray _params)
}
return json;
}

public void PreProcess(HttpContext context, string method, JArray _params)
{
}

public JObject OnProcess(HttpContext context, string method, JArray _params)
{
if (_shouldTrackHistory && method == "getnep5transfers") return GetNep5Transfers(_params);
return method == "getnep5balances" ? GetNep5Balances(_params) : null;
}

public void PostProcess(HttpContext context, string method, JArray _params, JObject result)
{
}
}
}
1 change: 1 addition & 0 deletions src/RpcNep5Tracker/RpcNep5Tracker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LevelDBStore\LevelDBStore.csproj" />
<ProjectReference Include="..\RpcServer\RpcServer.csproj" />
</ItemGroup>
</Project>
59 changes: 0 additions & 59 deletions src/RpcSecurity/RpcSecurity.cs

This file was deleted.

7 changes: 0 additions & 7 deletions src/RpcSecurity/RpcSecurity/config.json

This file was deleted.

26 changes: 0 additions & 26 deletions src/RpcSecurity/Settings.cs

This file was deleted.

12 changes: 12 additions & 0 deletions src/RpcServer/RpcException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Neo.Plugins
{
public class RpcException : Exception
{
public RpcException(int code, string message) : base(message)
{
HResult = code;
}
}
}
10 changes: 10 additions & 0 deletions src/RpcServer/RpcMethodAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace Neo.Plugins
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class RpcMethodAttribute : Attribute
{
public string Name { get; set; }
}
}
Loading