Skip to content

Commit

Permalink
create helper applog JSON functions for reuse in neo-express (neo-pro…
Browse files Browse the repository at this point in the history
…ject#402)

* create helper applog JSON functions for reuse in neo-express

* Update LogReader.cs (neo-project#401)

* Update LogReader.cs

* update

Co-authored-by: Harry <harrypierson@ngd.neo.org>
  • Loading branch information
2 people authored and joeqian10 committed Apr 7, 2021
1 parent c3cd65b commit 02bdb22
Showing 1 changed file with 54 additions and 35 deletions.
89 changes: 54 additions & 35 deletions src/ApplicationLogs/LogReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,49 +56,47 @@ public JObject GetApplicationLog(JArray _params)
return raw;
}

public void OnPersist(StoreView snapshot, IReadOnlyList<Blockchain.ApplicationExecuted> applicationExecutedList)
public static JObject TxLogToJson(Blockchain.ApplicationExecuted appExec)
{
WriteBatch writeBatch = new WriteBatch();

//processing log for transactions
foreach (var appExec in applicationExecutedList.Where(p => p.Transaction != null))
global::System.Diagnostics.Debug.Assert(appExec.Transaction != null);

var txJson = new JObject();
txJson["txid"] = appExec.Transaction.Hash.ToString();
JObject trigger = new JObject();
trigger["trigger"] = appExec.Trigger;
trigger["vmstate"] = appExec.VMState;
trigger["exception"] = GetExceptionMessage(appExec.Exception);
trigger["gasconsumed"] = appExec.GasConsumed.ToString();
try
{
var txJson = new JObject();
txJson["txid"] = appExec.Transaction.Hash.ToString();
JObject trigger = new JObject();
trigger["trigger"] = appExec.Trigger;
trigger["vmstate"] = appExec.VMState;
trigger["exception"] = GetExceptionMessage(appExec.Exception);
trigger["gasconsumed"] = appExec.GasConsumed.ToString();
trigger["stack"] = appExec.Stack.Select(q => q.ToJson()).ToArray();
}
catch (InvalidOperationException)
{
trigger["stack"] = "error: recursive reference";
}
trigger["notifications"] = appExec.Notifications.Select(q =>
{
JObject notification = new JObject();
notification["contract"] = q.ScriptHash.ToString();
notification["eventname"] = q.EventName;
try
{
trigger["stack"] = appExec.Stack.Select(q => q.ToJson()).ToArray();
notification["state"] = q.State.ToJson();
}
catch (InvalidOperationException)
{
trigger["stack"] = "error: recursive reference";
notification["state"] = "error: recursive reference";
}
trigger["notifications"] = appExec.Notifications.Select(q =>
{
JObject notification = new JObject();
notification["contract"] = q.ScriptHash.ToString();
notification["eventname"] = q.EventName;
try
{
notification["state"] = q.State.ToJson();
}
catch (InvalidOperationException)
{
notification["state"] = "error: recursive reference";
}
return notification;
}).ToArray();
return notification;
}).ToArray();

txJson["executions"] = new List<JObject>() { trigger }.ToArray();
writeBatch.Put(appExec.Transaction.Hash.ToArray(), Utility.StrictUTF8.GetBytes(txJson.ToString()));
}
txJson["executions"] = new List<JObject>() { trigger }.ToArray();
return txJson;
}

//processing log for block
public static JObject BlockLogToJson(StoreView snapshot, IReadOnlyList<Blockchain.ApplicationExecuted> applicationExecutedList)
{
var blocks = applicationExecutedList.Where(p => p.Transaction == null);
if (blocks.Count() > 0)
{
Expand Down Expand Up @@ -138,7 +136,28 @@ public void OnPersist(StoreView snapshot, IReadOnlyList<Blockchain.ApplicationEx
triggerList.Add(trigger);
}
blockJson["executions"] = triggerList.ToArray();
writeBatch.Put(blockHash, Utility.StrictUTF8.GetBytes(blockJson.ToString()));
return blockJson;
}

return null;
}

public void OnPersist(StoreView snapshot, IReadOnlyList<Blockchain.ApplicationExecuted> applicationExecutedList)
{
WriteBatch writeBatch = new WriteBatch();

//processing log for transactions
foreach (var appExec in applicationExecutedList.Where(p => p.Transaction != null))
{
var txJson = TxLogToJson(appExec);
writeBatch.Put(appExec.Transaction.Hash.ToArray(), Utility.StrictUTF8.GetBytes(txJson.ToString()));
}

//processing log for block
var blockJson = BlockLogToJson(snapshot, applicationExecutedList);
if (blockJson != null)
{
writeBatch.Put(snapshot.PersistingBlock.Hash.ToArray(), Utility.StrictUTF8.GetBytes(blockJson.ToString()));
}
db.Write(WriteOptions.Default, writeBatch);
}
Expand All @@ -152,7 +171,7 @@ public bool ShouldThrowExceptionFromCommit(Exception ex)
return false;
}

string GetExceptionMessage(Exception exception)
static string GetExceptionMessage(Exception exception)
{
if (exception == null) return "Engine faulted.";

Expand Down

0 comments on commit 02bdb22

Please sign in to comment.