Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Use IPersistencePlugin and batch Application Log writes. (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsolman authored and erikzhang committed Mar 17, 2019
1 parent 08cda72 commit 685f448
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 73 deletions.
68 changes: 63 additions & 5 deletions ApplicationLogs/LogReader.cs
@@ -1,21 +1,26 @@
using Microsoft.AspNetCore.Http;
using Neo.IO.Data.LevelDB;
using Neo.IO.Json;
using Neo.Ledger;
using Neo.Network.RPC;
using Neo.VM;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Snapshot = Neo.Persistence.Snapshot;

namespace Neo.Plugins
{
public class LogReader : Plugin, IRpcPlugin
public class LogReader : Plugin, IRpcPlugin, IPersistencePlugin
{
private readonly DB db;

public override string Name => "ApplicationLogs";

public LogReader()
{
this.db = DB.Open(Path.GetFullPath(Settings.Default.Path), new Options { CreateIfMissing = true });
System.ActorSystem.ActorOf(Logger.Props(System.Blockchain, db));
db = DB.Open(Path.GetFullPath(Settings.Default.Path), new Options { CreateIfMissing = true });
}

public override void Configure()
Expand All @@ -26,7 +31,7 @@ public override void Configure()
public void PreProcess(HttpContext context, string method, JArray _params)
{
}

public JObject OnProcess(HttpContext context, string method, JArray _params)
{
if (method != "getapplicationlog") return null;
Expand All @@ -35,9 +40,62 @@ public JObject OnProcess(HttpContext context, string method, JArray _params)
throw new RpcException(-100, "Unknown transaction");
return JObject.Parse(value.ToString());
}

public void PostProcess(HttpContext context, string method, JArray _params, JObject result)
{
}

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

foreach (var appExec in applicationExecutedList)
{
JObject json = new JObject();
json["txid"] = appExec.Transaction.Hash.ToString();
json["executions"] = appExec.ExecutionResults.Select(p =>
{
JObject execution = new JObject();
execution["trigger"] = p.Trigger;
execution["contract"] = p.ScriptHash.ToString();
execution["vmstate"] = p.VMState;
execution["gas_consumed"] = p.GasConsumed.ToString();
try
{
execution["stack"] = p.Stack.Select(q => q.ToParameter().ToJson()).ToArray();
}
catch (InvalidOperationException)
{
execution["stack"] = "error: recursive reference";
}
execution["notifications"] = p.Notifications.Select(q =>
{
JObject notification = new JObject();
notification["contract"] = q.ScriptHash.ToString();
try
{
notification["state"] = q.State.ToParameter().ToJson();
}
catch (InvalidOperationException)
{
notification["state"] = "error: recursive reference";
}
return notification;
}).ToArray();
return execution;
}).ToArray();
writeBatch.Put(appExec.Transaction.Hash.ToArray(), json.ToString());
}
db.Write(WriteOptions.Default, writeBatch);
}

public void OnCommit(Snapshot snapshot)
{
}

public bool ShouldThrowExceptionFromCommit(Exception ex)
{
return false;
}
}
}
68 changes: 0 additions & 68 deletions ApplicationLogs/Logger.cs

This file was deleted.

0 comments on commit 685f448

Please sign in to comment.