-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Plugin coreMetrics. * Organize project files * metric prefix
- Loading branch information
Showing
3 changed files
with
109 additions
and
0 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
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; | ||
} | ||
} | ||
} |
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,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> |
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