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

Plugin CoreMetrics #68

Merged
merged 4 commits into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
90 changes: 90 additions & 0 deletions CoreMetrics/CoreMetrics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Microsoft.AspNetCore.Http;
using Neo.IO.Json;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;

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

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

public JObject OnProcess(HttpContext context, string method, JArray _params)
{
switch (method)
{
case "getmetricblocktimestamp":
{

uint nBlocks = (uint)_params[0].AsNumber();
uint lastHeight = _params.Count >= 2 ? lastHeight = (uint)_params[1].AsNumber() : 0;
return GetBlocksTime(nBlocks, lastHeight);
}
default:
return null;
}
}

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

private JObject GetBlocksTime(uint nBlocks, uint lastHeight)
{
// It is currently limited to query blocks generated in the last 24hours (86400 seconds)
uint maxNBlocksPerDay = 86400 / Blockchain.SecondsPerBlock;
if (lastHeight != 0)
{
if (lastHeight >= Blockchain.Singleton.Height)
{
JObject json = new JObject();
return json["error"] = "Requested height to start " + lastHeight + " exceeds " + Blockchain.Singleton.Height;
}

if (nBlocks > lastHeight)
{
JObject json = new JObject();
return json["error"] = "Requested " + nBlocks + " blocks timestamps is greater than starting at height " + lastHeight;
}
}

if (nBlocks > maxNBlocksPerDay)
{
JObject json = new JObject();
return json["error"] = "Requested number of blocks timestamps exceeds " + maxNBlocksPerDay;
}

if (nBlocks >= Blockchain.Singleton.Height)
{
JObject json = new JObject();
return json["error"] = "Requested number of blocks timestamps exceeds quantity of known blocks " + Blockchain.Singleton.Height;
}

if (nBlocks <= 0)
{
JObject json = new JObject();
return json["error"] = "Requested number of block times can not be <= 0";
}

JArray array = new JArray();
uint heightToBegin = lastHeight > 0 ? lastHeight - nBlocks : Blockchain.Singleton.Height - nBlocks;
for (uint i = heightToBegin; i <= Blockchain.Singleton.HeaderHeight; i++)
{
JObject json = new JObject();
Header header = Blockchain.Singleton.Store.GetHeader(i);
json["timestamp"] = header.Timestamp;
json["height"] = i;
array.Add(json);
}

return array;
}
}
}
13 changes: 13 additions & 0 deletions CoreMetrics/CoreMetrics.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>2.10.0</Version>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>Neo.Plugins</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Neo" Version="2.10.0" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions neo-plugins.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RpcWallet", "RpcWallet\RpcW
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RpcNep5Tracker", "RpcNep5Tracker\RpcNep5Tracker.csproj", "{BBE8AC15-12DF-4AF0-ABC1-F1557EB5DC8E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoreMetrics", "CoreMetrics\CoreMetrics.csproj", "{AEFFF003-3500-416B-AD9B-8C838C33C1F4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -56,6 +58,10 @@ Global
{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
{BBE8AC15-12DF-4AF0-ABC1-F1557EB5DC8E}.Release|Any CPU.Build.0 = Release|Any CPU
{AEFFF003-3500-416B-AD9B-8C838C33C1F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AEFFF003-3500-416B-AD9B-8C838C33C1F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AEFFF003-3500-416B-AD9B-8C838C33C1F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AEFFF003-3500-416B-AD9B-8C838C33C1F4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down