Skip to content

Commit

Permalink
use leveldb in ApplicationLogs
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang committed Jul 3, 2018
1 parent 60fce60 commit 90d1d63
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
6 changes: 3 additions & 3 deletions ApplicationLogs/ApplicationLogs.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>3.0.0-preview2-02</Version>
<Version>3.0.0-preview2-03</Version>
<TargetFrameworks>netstandard2.0;net47</TargetFrameworks>
<RootNamespace>Neo.Plugins</RootNamespace>
</PropertyGroup>
Expand All @@ -14,7 +14,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Neo" Version="3.0.0-preview2-02" />
<PackageReference Include="Neo" Version="3.0.0-preview2-03" />
</ItemGroup>

</Project>
13 changes: 7 additions & 6 deletions ApplicationLogs/LogReader.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
using Microsoft.AspNetCore.Http;
using Neo.IO.Data.LevelDB;
using Neo.IO.Json;
using Neo.Network.RPC;
using System.IO;

namespace Neo.Plugins
{
public class LogReader : Plugin, IRpcPlugin
{
private readonly DB db = DB.Open(Settings.Default.Path, new Options { CreateIfMissing = true });

public override string Name => "ApplicationLogs";

public LogReader()
{
System.ActorSystem.ActorOf(Logger.Props(System.Blockchain));
System.ActorSystem.ActorOf(Logger.Props(System.Blockchain, db));
}

public JObject OnProcess(HttpContext context, string method, JArray _params)
{
if (method != "getapplicationlog") return null;
UInt256 hash = UInt256.Parse(_params[0].AsString());
string path = Path.Combine(Settings.Default.Path, $"{hash}.json");
return File.Exists(path)
? JObject.Parse(File.ReadAllText(path))
: throw new RpcException(-100, "Unknown transaction");
if (!db.TryGet(ReadOptions.Default, hash.ToArray(), out Slice value))
throw new RpcException(-100, "Unknown transaction");
return JObject.Parse(value.ToString());
}
}
}
16 changes: 9 additions & 7 deletions ApplicationLogs/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
using Akka.Actor;
using Neo.IO;
using Neo.IO.Data.LevelDB;
using Neo.IO.Json;
using Neo.Ledger;
using Neo.VM;
using System.IO;
using System.Linq;

namespace Neo.Plugins
{
internal class Logger : UntypedActor
{
public Logger(IActorRef blockchain)
private readonly DB db;

public Logger(IActorRef blockchain, DB db)
{
this.db = db;
blockchain.Tell(new Blockchain.Register());
}

Expand All @@ -37,15 +41,13 @@ protected override void OnReceive(object message)
}).ToArray();
return execution;
}).ToArray();
Directory.CreateDirectory(Settings.Default.Path);
string path = Path.Combine(Settings.Default.Path, $"{e.Transaction.Hash}.json");
File.WriteAllText(path, json.ToString());
db.Put(WriteOptions.Default, e.Transaction.Hash.ToArray(), json.ToString());
}
}

public static Props Props(IActorRef blockchain)
public static Props Props(IActorRef blockchain, DB db)
{
return Akka.Actor.Props.Create(() => new Logger(blockchain));
return Akka.Actor.Props.Create(() => new Logger(blockchain, db));
}
}
}

0 comments on commit 90d1d63

Please sign in to comment.