-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add debug mode to the applicationlog to get log info #842
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,8 +24,9 @@ namespace Neo.Plugins | |
{ | ||
public class LogReader : Plugin | ||
{ | ||
private IStore _db; | ||
private ISnapshot _snapshot; | ||
private static IStore _db; | ||
private static ISnapshot _snapshot; | ||
private static readonly byte[] LogPrefix = { 0x12 }; | ||
|
||
public override string Name => "ApplicationLogs"; | ||
public override string Description => "Synchronizes the smart contract log with the NativeContract log (Notify)"; | ||
|
@@ -34,12 +35,16 @@ public LogReader() | |
{ | ||
Blockchain.Committing += OnCommitting; | ||
Blockchain.Committed += OnCommitted; | ||
if (Settings.Default.Debug) | ||
ApplicationEngine.Log += ApplicationEngine_Log; | ||
} | ||
|
||
public override void Dispose() | ||
{ | ||
Blockchain.Committing -= OnCommitting; | ||
Blockchain.Committed -= OnCommitted; | ||
if (Settings.Default.Debug) | ||
ApplicationEngine.Log -= ApplicationEngine_Log; | ||
} | ||
|
||
protected override void Configure() | ||
|
@@ -76,6 +81,12 @@ public JToken GetApplicationLog(JArray _params) | |
i++; | ||
} | ||
} | ||
if (!Settings.Default.Debug) return raw; | ||
byte[] logs = _db.TryGet(LogPrefix.Concat(hash.ToArray()).ToArray()); | ||
if (logs != null) | ||
{ | ||
raw["executions"]["logs"] = (JArray)JToken.Parse(Neo.Utility.StrictUTF8.GetString(value)); | ||
} | ||
return raw; | ||
} | ||
|
||
|
@@ -186,6 +197,33 @@ private void OnCommitting(NeoSystem system, Block block, DataCache snapshot, IRe | |
} | ||
} | ||
|
||
private static void ApplicationEngine_Log(object sender, LogEventArgs args) | ||
{ | ||
if (!Settings.Default.Debug) return; | ||
|
||
var tx = ((Transaction)args.ScriptContainer).Hash; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can be not only a transaction, right? E.g. block for OnPersist and PostPersist execution results. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think it can only be transaction, but i might be wrong. May you please confirm @shargon @roman-khimov |
||
|
||
byte[] value = _db.TryGet(LogPrefix.Concat(tx.ToArray()).ToArray()); | ||
|
||
JArray logList = null; | ||
if (value is null) | ||
{ | ||
logList = new JArray(); | ||
} | ||
else | ||
{ | ||
logList = (JArray)JToken.Parse(Neo.Utility.StrictUTF8.GetString(value)); | ||
} | ||
var logJson = new JObject | ||
{ | ||
["contract"] = args.ScriptHash.ToString(), | ||
["message"] = args.Message | ||
}; | ||
logList?.Add(logJson); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It is possible since in the |
||
|
||
_snapshot.Put(LogPrefix.Concat(tx.ToArray()).ToArray(), Neo.Utility.StrictUTF8.GetBytes(logList?.ToString()!)); | ||
} | ||
|
||
private void OnCommitted(NeoSystem system, Block block) | ||
{ | ||
if (system.Settings.Network != Settings.Default.Network) return; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,13 +18,16 @@ internal class Settings | |
public uint Network { get; } | ||
public int MaxStackSize { get; } | ||
|
||
public bool Debug { get; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we planning to enable this setting in public networks? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could, after solid test and monorepo. but now this is only to answer the need of @cschuchardt88 on neo-express. |
||
|
||
public static Settings Default { get; private set; } | ||
|
||
private Settings(IConfigurationSection section) | ||
{ | ||
this.Path = section.GetValue("Path", "ApplicationLogs_{0}"); | ||
this.Network = section.GetValue("Network", 5195086u); | ||
this.MaxStackSize = section.GetValue("MaxStackSize", (int)ushort.MaxValue); | ||
this.Debug = section.GetValue("Debug", false); | ||
} | ||
|
||
public static void Load(IConfigurationSection section) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a nice feature. How about adding the same ability to add logs to execution result for
invokescript
,invokefunction
and etc. in debug mode? This change is more invasive and requires changing the core library as far, but it might be useful for someone.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AnnaShaleva Also #807 (comment)