Skip to content

Commit

Permalink
Merge pull request #671 from AElfProject/enhancement-monitor
Browse files Browse the repository at this point in the history
Enhancement monitor
  • Loading branch information
rosona authored Dec 6, 2018
2 parents 9f8fc24 + 3b2735d commit a727ae1
Show file tree
Hide file tree
Showing 38 changed files with 310 additions and 193 deletions.
110 changes: 54 additions & 56 deletions AElf.ChainController.Rpc/ChainControllerRpcService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using AElf.Node.AElfChain;
using AElf.RPC;
using AElf.SmartContract;
using AElf.Synchronization.BlockSynchronization;
using Community.AspNetCore.JsonRpc;
using Easy.MessageHub;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -43,6 +44,8 @@ public class ChainControllerRpcService : IJsonRpcService
public INodeService MainchainNodeService { get; set; }
public ICrossChainInfo CrossChainInfo { get; set; }
public IKeyValueDatabase KeyValueDatabase { get; set; }
public IBlockSet BlockSet { get; set; }
public IBlockSynchronizer BlockSynchronizer { get; set; }

#endregion Properties

Expand Down Expand Up @@ -565,7 +568,7 @@ public async Task<JObject> ProGetBlockInfo(string blockHeight, bool includeTxs =
}

[JsonRpcMethod("get_txpool_size")]
public async Task<JObject> ProGetTxPoolSize()
public async Task<JObject> GetTxPoolSize()
{
var transactionPoolSize = await this.GetTransactionPoolSize();
var response = new JObject
Expand All @@ -577,7 +580,7 @@ public async Task<JObject> ProGetTxPoolSize()
}

[JsonRpcMethod("dpos_isalive")]
public async Task<JObject> ProIsDPoSAlive()
public async Task<JObject> IsDPoSAlive()
{
var isAlive = MainchainNodeService.IsDPoSAlive();
var response = new JObject
Expand All @@ -589,7 +592,7 @@ public async Task<JObject> ProIsDPoSAlive()
}

[JsonRpcMethod("node_isforked")]
public async Task<JObject> ProNodeIsForked()
public async Task<JObject> NodeIsForked()
{
var isForked = MainchainNodeService.IsForked();
var response = new JObject
Expand All @@ -600,36 +603,58 @@ public async Task<JObject> ProNodeIsForked()
return JObject.FromObject(response);
}

#region Admin

[JsonRpcMethod("set_block_volume", "minimal", "maximal")]
public async Task<JObject> ProcSetBlockVolume(string minimal, string maximal)
[JsonRpcMethod("get_invalid_block")]
public async Task<JObject> InvalidBlockCount()
{
/* TODO: This is a privileged method, need:
* 1. Optional enabling of this method (maybe separate endpoint), and/or
* 2. Authentication / authorization
*/
try
var invalidBlockCount = await this.GetInvalidBlockCount();

var response = new JObject
{
var min = int.Parse(minimal);
var max = int.Parse(maximal);
this.SetBlockVolume(min, max);
return await Task.FromResult(new JObject
{
["result"] = "Success"
});
}
catch (Exception e)
["InvalidBlockCount"] = invalidBlockCount
};

return JObject.FromObject(response);
}

[JsonRpcMethod("get_rollback_times")]
public async Task<JObject> RollBackTimes()
{
var rollBackTimes = await this.GetRollBackTimes();

var response = new JObject
{
_logger.Error(e, "Exception while ProcSetBlockVolume.");
return await Task.FromResult(new JObject
{
["error"] = "Failed"
});
}
["RollBackTimes"] = rollBackTimes
};

return JObject.FromObject(response);
}

#endregion Admin
// [JsonRpcMethod("set_block_volume", "minimal", "maximal")]
// public async Task<JObject> ProcSetBlockVolume(string minimal, string maximal)
// {
// /* TODO: This is a privileged method, need:
// * 1. Optional enabling of this method (maybe separate endpoint), and/or
// * 2. Authentication / authorization
// */
// try
// {
// var min = int.Parse(minimal);
// var max = int.Parse(maximal);
// this.SetBlockVolume(min, max);
// return await Task.FromResult(new JObject
// {
// ["result"] = "Success"
// });
// }
// catch (Exception e)
// {
// _logger.Error(e, "Exception while ProcSetBlockVolume.");
// return await Task.FromResult(new JObject
// {
// ["error"] = "Failed"
// });
// }
// }

[JsonRpcMethod("get_db_value","key")]
public async Task<JObject> GetDbValue(string key)
Expand Down Expand Up @@ -660,7 +685,7 @@ public async Task<JObject> GetDbValue(string key)
type = keyObj.Type;
id = JObject.Parse(keyObj.ToString());
var valueBytes = KeyValueDatabase.GetAsync(type,key).Result;
var obj = GetInstance(type);
var obj = this.GetInstance(type);
obj.MergeFrom(valueBytes);
value = obj;
}
Expand All @@ -685,33 +710,6 @@ public async Task<JObject> GetDbValue(string key)
}
}

private IMessage GetInstance(string type)
{
switch (type)
{
case "MerklePath":
return new MerklePath();
case "BinaryMerkleTree":
return new BinaryMerkleTree ();
case "BlockHeader":
return new BlockHeader();
case "BlockBody":
return new BlockBody();
case "Hash":
return new Hash();
case "SmartContractRegistration":
return new SmartContractRegistration();
case "Transaction":
return new Transaction();
case "TransactionResult":
return new TransactionResult();
case "TransactionTrace":
return new TransactionTrace();
default:
throw new ArgumentException($"[{type}] not found");
}
}

#endregion Methods
}
}
47 changes: 42 additions & 5 deletions AElf.ChainController.Rpc/ServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,11 @@ internal static async Task<ulong> GetTransactionPoolSize(this Svc s)
return (ulong)(await s.TxHub.GetReceiptsOfExecutablesAsync()).Count;
}

internal static void SetBlockVolume(this Svc s, int minimal, int maximal)
{
// TODO: Maybe control this in miner
// s.TxPool.SetBlockVolume(minimal, maximal);
}
// internal static void SetBlockVolume(this Svc s, int minimal, int maximal)
// {
// // TODO: Maybe control this in miner
//// s.TxPool.SetBlockVolume(minimal, maximal);
// }

internal static async Task<byte[]> CallReadOnly(this Svc s, Transaction tx)
{
Expand Down Expand Up @@ -272,6 +272,43 @@ internal static async Task<Block> GetBlock(this Svc s, Hash blockHash)
var blockchain = s.ChainService.GetBlockChain(Hash.LoadHex(ChainConfig.Instance.ChainId));
return (Block) await blockchain.GetBlockByHashAsync(blockHash);
}

internal static async Task<int> GetInvalidBlockCount(this Svc s)
{
return s.BlockSet.InvalidBlockCount;
}

internal static IMessage GetInstance(this Svc s,string type)
{
switch (type)
{
case "MerklePath":
return new MerklePath();
case "BinaryMerkleTree":
return new BinaryMerkleTree ();
case "BlockHeader":
return new BlockHeader();
case "BlockBody":
return new BlockBody();
case "Hash":
return new Hash();
case "SmartContractRegistration":
return new SmartContractRegistration();
case "Transaction":
return new Transaction();
case "TransactionResult":
return new TransactionResult();
case "TransactionTrace":
return new TransactionTrace();
default:
throw new ArgumentException($"[{type}] not found");
}
}

internal static async Task<int> GetRollBackTimes(this Svc s)
{
return s.BlockSynchronizer.RollBackTimes;
}
}

}
5 changes: 3 additions & 2 deletions AElf.Management.Website/Controllers/ActorController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AElf.Management.Interfaces;
using AElf.Management.Models;
using AElf.Management.Website.Models;
Expand All @@ -19,9 +20,9 @@ public ActorController(IAkkaService akkaService)

[HttpGet]
[Route("state/{chainId}")]
public ApiResult<List<ActorStateResult>> State(string chainId)
public async Task<ApiResult<List<ActorStateResult>>> State(string chainId)
{
var result = _akkaService.GetState(chainId);
var result = await _akkaService.GetState(chainId);
return new ApiResult<List<ActorStateResult>>(result);
}
}
Expand Down
9 changes: 5 additions & 4 deletions AElf.Management.Website/Controllers/ChainController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AElf.Management.Interfaces;
using AElf.Management.Models;
using AElf.Management.Website.Models;
Expand All @@ -20,22 +21,22 @@ public ChainController(IChainService chainService)

[HttpGet]
[Route("list")]
public ApiResult<List<ChainResult>> List()
public async Task<ApiResult<List<ChainResult>>> List()
{
var result = _chainService.GetAllChains();
var result = await _chainService.GetAllChains();
return new ApiResult<List<ChainResult>>(result);
}

[HttpPost]
public ApiEmptyResult Deploy([FromBody] DeployArg arg)
public async Task<ApiEmptyResult> Deploy([FromBody] DeployArg arg)
{
_chainService.DeployMainChain(arg);
return ApiEmptyResult.Default;
}

[HttpDelete]
[Route("{chainId}")]
public ApiEmptyResult Remove(string chainId)
public async Task<ApiEmptyResult> Remove(string chainId)
{
_chainService.RemoveMainChain(chainId);
return ApiEmptyResult.Default;
Expand Down
5 changes: 3 additions & 2 deletions AElf.Management.Website/Controllers/LauncherController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AElf.Management.Interfaces;
using AElf.Management.Models;
using AElf.Management.Website.Models;
Expand All @@ -19,9 +20,9 @@ public LauncherController(ILauncherService auncherService)

[HttpGet]
[Route("list/{chainId}")]
public ApiResult<List<LauncherResult>> List(string chainId)
public async Task<ApiResult<List<LauncherResult>>> List(string chainId)
{
var result = _launcherService.GetAllLaunchers(chainId);
var result = await _launcherService.GetAllLaunchers(chainId);
return new ApiResult<List<LauncherResult>>(result);
}
}
Expand Down
5 changes: 3 additions & 2 deletions AElf.Management.Website/Controllers/LighthouseController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AElf.Management.Interfaces;
using AElf.Management.Models;
using AElf.Management.Website.Models;
Expand All @@ -19,9 +20,9 @@ public LighthouseController(ILighthouseService lighthouseService)

[HttpGet]
[Route("list/{chainId}")]
public ApiResult<List<LighthouseResult>> List(string chainId)
public async Task<ApiResult<List<LighthouseResult>>> List(string chainId)
{
var result = _lighthouseService.GetAllLighthouses(chainId);
var result = await _lighthouseService.GetAllLighthouses(chainId);
return new ApiResult<List<LighthouseResult>>(result);
}
}
Expand Down
5 changes: 3 additions & 2 deletions AElf.Management.Website/Controllers/NetworkController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AElf.Management.Interfaces;
using AElf.Management.Models;
using AElf.Management.Website.Models;
Expand All @@ -19,9 +20,9 @@ public NetworkController(INetworkService networkService)

[HttpGet]
[Route("peers/{chainId}")]
public ApiResult<PeerResult> Peers(string chainId)
public async Task<ApiResult<PeerResult>> Peers(string chainId)
{
var result = _networkService.GetPeers(chainId);
var result = await _networkService.GetPeers(chainId);

return new ApiResult<PeerResult>(result);
}
Expand Down
13 changes: 7 additions & 6 deletions AElf.Management.Website/Controllers/NodeController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AElf.Management.Interfaces;
using AElf.Management.Models;
using AElf.Management.Website.Models;
Expand All @@ -19,27 +20,27 @@ public NodeController(INodeService nodeService)

[HttpGet]
[Route("isalive/{chainId}")]
public ApiResult<bool> IsAlive(string chainId)
public async Task<ApiResult<bool>> IsAlive(string chainId)
{
var result = _nodeService.IsAlive(chainId);
var result = await _nodeService.IsAlive(chainId);

return new ApiResult<bool>(result);
}

[HttpGet]
[Route("isforked/{chainId}")]
public ApiResult<bool> IsForked(string chainId)
public async Task<ApiResult<bool>> IsForked(string chainId)
{
var result = _nodeService.IsForked(chainId);
var result = await _nodeService.IsForked(chainId);

return new ApiResult<bool>(result);
}

[HttpGet]
[Route("statehistory/{chainId}")]
public ApiResult<List<NodeStateHistory>> StateHistory(string chainId)
public async Task<ApiResult<List<NodeStateHistory>>> StateHistory(string chainId)
{
var result = _nodeService.GetHistoryState(chainId);
var result = await _nodeService.GetHistoryState(chainId);

return new ApiResult<List<NodeStateHistory>>(result);
}
Expand Down
Loading

0 comments on commit a727ae1

Please sign in to comment.