From fc0921d9256c91cf0549cc3b450232a4a0736d1b Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Thu, 12 Oct 2017 14:10:58 +0800 Subject: [PATCH] record notifications --- neo-cli/Shell/MainService.cs | 40 +++++++++++++++++++++++++++++++++--- neo-cli/neo-cli.csproj | 4 ++-- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/neo-cli/Shell/MainService.cs b/neo-cli/Shell/MainService.cs index 3eeef5237..47e183669 100644 --- a/neo-cli/Shell/MainService.cs +++ b/neo-cli/Shell/MainService.cs @@ -3,10 +3,12 @@ using Neo.Implementations.Blockchains.LevelDB; using Neo.Implementations.Wallets.EntityFramework; using Neo.IO; +using Neo.IO.Json; using Neo.Network; using Neo.Network.RPC; using Neo.Services; using Neo.SmartContract; +using Neo.VM; using Neo.Wallets; using System; using System.Collections.Generic; @@ -738,11 +740,27 @@ protected internal override void OnStart(string[] args) File.Delete(acc_zip_path); } LocalNode.Start(Settings.Default.NodePort, Settings.Default.WsPort); - if (args.Length >= 1 && args[0] == "/rpc") + bool recordNotifications = false; + for (int i = 0; i < args.Length; i++) { - rpc = new RpcServerWithWallet(LocalNode); - rpc.Start(Settings.Default.UriPrefix.OfType().ToArray(), Settings.Default.SslCert, Settings.Default.SslCertPassword); + switch (args[i]) + { + case "/rpc": + case "--rpc": + case "-r": + if (rpc == null) + { + rpc = new RpcServerWithWallet(LocalNode); + rpc.Start(Settings.Default.UriPrefix.OfType().ToArray(), Settings.Default.SslCert, Settings.Default.SslCertPassword); + } + break; + case "--record-notifications": + recordNotifications = true; + break; + } } + if (recordNotifications) + Blockchain.Notify += Blockchain_Notify; }); } @@ -816,5 +834,21 @@ private bool OnUpgradeWalletCommand(string[] args) Console.WriteLine($"Wallet file upgrade complete. Old file has been auto-saved at: {path_old}"); return true; } + + private void Blockchain_Notify(object sender, BlockNotifyEventArgs e) + { + JArray jArray = new JArray(e.Notifications.Select(p => + { + JObject json = new JObject(); + json["txid"] = ((Transaction)p.ScriptContainer).Hash.ToString(); + json["contract"] = p.ScriptHash.ToString(); + json["state"] = p.State.ToParameter().ToJson(); + return json; + })); + string path = Path.Combine(AppContext.BaseDirectory, "Notifications"); + Directory.CreateDirectory(path); + path = Path.Combine(path, $"block-{e.Block.Index}.json"); + File.WriteAllText(path, jArray.ToString()); + } } } diff --git a/neo-cli/neo-cli.csproj b/neo-cli/neo-cli.csproj index dc7e2966a..a46353db4 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.3.3 + 2.3.4 The Neo Project netcoreapp1.0 neo-cli @@ -31,7 +31,7 @@ - +