From dea762aa1122f9f927937c53bc532289035893e2 Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Fri, 26 Jan 2018 14:00:25 +0800 Subject: [PATCH] add RPC command `getapplicationlog` --- neo-cli/Network/RPC/RpcServerWithWallet.cs | 9 +++++ neo-cli/Settings.cs | 7 ++-- neo-cli/Shell/MainService.cs | 39 ++++++++++++---------- neo-cli/config.json | 3 +- neo-cli/config.mainnet.json | 3 +- neo-cli/config.testnet.json | 3 +- neo-cli/neo-cli.csproj | 4 +-- 7 files changed, 40 insertions(+), 28 deletions(-) diff --git a/neo-cli/Network/RPC/RpcServerWithWallet.cs b/neo-cli/Network/RPC/RpcServerWithWallet.cs index af99d7f7a..f95a14786 100644 --- a/neo-cli/Network/RPC/RpcServerWithWallet.cs +++ b/neo-cli/Network/RPC/RpcServerWithWallet.cs @@ -5,6 +5,7 @@ using Neo.SmartContract; using Neo.Wallets; using System.Collections.Generic; +using System.IO; using System.Linq; namespace Neo.Network.RPC @@ -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."); diff --git a/neo-cli/Settings.cs b/neo-cli/Settings.cs index a489d2daa..d7daf39e3 100644 --- a/neo-cli/Settings.cs +++ b/neo-cli/Settings.cs @@ -1,4 +1,7 @@ using Microsoft.Extensions.Configuration; +using Neo.Network; +using System; +using System.IO; namespace Neo { @@ -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}"); } } diff --git a/neo-cli/Shell/MainService.cs b/neo-cli/Shell/MainService.cs index abd4ae7f3..944bb7748 100644 --- a/neo-cli/Shell/MainService.cs +++ b/neo-cli/Shell/MainService.cs @@ -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]) @@ -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; }); } @@ -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()); } } } diff --git a/neo-cli/config.json b/neo-cli/config.json index 20ee44881..aef142130 100644 --- a/neo-cli/config.json +++ b/neo-cli/config.json @@ -1,8 +1,7 @@ { "ApplicationConfiguration": { "Paths": { - "Chain": "Chain", - "Notifications": "Notifications" + "Chain": "Chain" }, "P2P": { "Port": 10333, diff --git a/neo-cli/config.mainnet.json b/neo-cli/config.mainnet.json index 20ee44881..aef142130 100644 --- a/neo-cli/config.mainnet.json +++ b/neo-cli/config.mainnet.json @@ -1,8 +1,7 @@ { "ApplicationConfiguration": { "Paths": { - "Chain": "Chain", - "Notifications": "Notifications" + "Chain": "Chain" }, "P2P": { "Port": 10333, diff --git a/neo-cli/config.testnet.json b/neo-cli/config.testnet.json index 18f96fc2c..293945920 100644 --- a/neo-cli/config.testnet.json +++ b/neo-cli/config.testnet.json @@ -1,8 +1,7 @@ { "ApplicationConfiguration": { "Paths": { - "Chain": "ChainTestNet", - "Notifications": "NotificationsTestNet" + "Chain": "ChainTestNet" }, "P2P": { "Port": 20333, diff --git a/neo-cli/neo-cli.csproj b/neo-cli/neo-cli.csproj index 6d7a164d3..5ae136a65 100644 --- a/neo-cli/neo-cli.csproj +++ b/neo-cli/neo-cli.csproj @@ -3,7 +3,7 @@ 2016-2017 The Neo Project Neo.CLI - 2.6.0 + 2.7.0 The Neo Project netcoreapp2.0 neo-cli @@ -28,7 +28,7 @@ - +