Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
add RPC command getapplicationlog
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Zhang authored and erikzhang committed Jan 26, 2018
1 parent 2223d62 commit 020b394
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 28 deletions.
9 changes: 9 additions & 0 deletions neo-cli/Network/RPC/RpcServerWithWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Neo.SmartContract;
using Neo.Wallets;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Neo.Network.RPC
Expand All @@ -20,6 +21,14 @@ protected override JObject Process(string method, JArray _params)
{
switch (method)
{
case "getapplicationlog":
{
UInt256 hash = UInt256.Parse(_params[0].AsString());
string path = Path.Combine(Settings.Default.Paths.ApplicationLogs, $"{hash}.json");
return File.Exists(path)
? JObject.Parse(File.ReadAllText(path))
: throw new RpcException(-100, "Unknown transaction");
}
case "getbalance":
if (Program.Wallet == null)
throw new RpcException(-400, "Access denied.");
Expand Down
7 changes: 5 additions & 2 deletions neo-cli/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Microsoft.Extensions.Configuration;
using Neo.Network;
using System;
using System.IO;

namespace Neo
{
Expand Down Expand Up @@ -27,12 +30,12 @@ public Settings(IConfigurationSection section)
internal class PathsSettings
{
public string Chain { get; }
public string Notifications { get; }
public string ApplicationLogs { get; }

public PathsSettings(IConfigurationSection section)
{
this.Chain = section.GetSection("Chain").Value;
this.Notifications = section.GetSection("Notifications").Value;
this.ApplicationLogs = Path.Combine(AppContext.BaseDirectory, $"ApplicationLogs_{Message.Magic:X8}");
}
}

Expand Down
39 changes: 21 additions & 18 deletions neo-cli/Shell/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ protected internal override void OnStart(string[] args)
File.Delete(acc_zip_path);
}
LocalNode.Start(Settings.Default.P2P.Port, Settings.Default.P2P.WsPort);
bool recordNotifications = false;
bool log = false;
for (int i = 0; i < args.Length; i++)
{
switch (args[i])
Expand All @@ -777,13 +777,14 @@ protected internal override void OnStart(string[] args)
rpc.Start(Settings.Default.RPC.Port, Settings.Default.RPC.SslCert, Settings.Default.RPC.SslCertPassword);
}
break;
case "--record-notifications":
recordNotifications = true;
case "-l":
case "--log":
log = true;
break;
}
}
if (recordNotifications)
Blockchain.Notify += Blockchain_Notify;
if (log)
LevelDBBlockchain.ApplicationExecuted += LevelDBBlockchain_ApplicationExecuted;
});
}

Expand Down Expand Up @@ -866,21 +867,23 @@ private bool OnUpgradeWalletCommand(string[] args)
return true;
}

private void Blockchain_Notify(object sender, BlockNotifyEventArgs e)
private void LevelDBBlockchain_ApplicationExecuted(object sender, ApplicationExecutedEventArgs e)
{
JArray jArray = new JArray(e.Notifications.Select(p =>
JObject json = new JObject();
json["txid"] = e.Transaction.Hash.ToString();
json["vmstate"] = e.VMState;
json["gas_consumed"] = e.GasConsumed.ToString();
json["stack"] = e.Stack.Select(p => p.ToParameter().ToJson()).ToArray();
json["notifications"] = e.Notifications.Select(p =>
{
JObject json = new JObject();
json["txid"] = ((Transaction)p.ScriptContainer).Hash.ToString();
json["time"] = e.Block.Timestamp;
json["contract"] = p.ScriptHash.ToString();
json["state"] = p.State.ToParameter().ToJson();
return json;
}));
string path = Path.Combine(AppContext.BaseDirectory, Settings.Default.Paths.Notifications);
Directory.CreateDirectory(path);
path = Path.Combine(path, $"block-{e.Block.Index}.json");
File.WriteAllText(path, jArray.ToString());
JObject notification = new JObject();
notification["contract"] = p.ScriptHash.ToString();
notification["state"] = p.State.ToParameter().ToJson();
return notification;
}).ToArray();
Directory.CreateDirectory(Settings.Default.Paths.ApplicationLogs);
string path = Path.Combine(Settings.Default.Paths.ApplicationLogs, $"{e.Transaction.Hash}.json");
File.WriteAllText(path, json.ToString());
}
}
}
3 changes: 1 addition & 2 deletions neo-cli/config.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"ApplicationConfiguration": {
"Paths": {
"Chain": "Chain",
"Notifications": "Notifications"
"Chain": "Chain"
},
"P2P": {
"Port": 10333,
Expand Down
3 changes: 1 addition & 2 deletions neo-cli/config.mainnet.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"ApplicationConfiguration": {
"Paths": {
"Chain": "Chain",
"Notifications": "Notifications"
"Chain": "Chain"
},
"P2P": {
"Port": 10333,
Expand Down
3 changes: 1 addition & 2 deletions neo-cli/config.testnet.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"ApplicationConfiguration": {
"Paths": {
"Chain": "ChainTestNet",
"Notifications": "NotificationsTestNet"
"Chain": "ChainTestNet"
},
"P2P": {
"Port": 20333,
Expand Down
4 changes: 2 additions & 2 deletions neo-cli/neo-cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Copyright>2016-2017 The Neo Project</Copyright>
<AssemblyTitle>Neo.CLI</AssemblyTitle>
<Version>2.6.0</Version>
<Version>2.7.0</Version>
<Authors>The Neo Project</Authors>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssemblyName>neo-cli</AssemblyName>
Expand All @@ -28,7 +28,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Neo" Version="2.6.0" />
<PackageReference Include="Neo" Version="2.7.0" />
</ItemGroup>

</Project>

0 comments on commit 020b394

Please sign in to comment.