Skip to content
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

Use IPersistencePlugin and batch Application Log writes. #62

Merged
merged 3 commits into from
Mar 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 63 additions & 5 deletions ApplicationLogs/LogReader.cs
Original file line number Diff line number Diff line change
@@ -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.