-
Notifications
You must be signed in to change notification settings - Fork 1
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
Worknet plugins/extensions #7
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8677f26
wrote a basic worknet plugin
cloudmation ddbcabd
also seek plugins in ~/.neo folder
cloudmation c3205bf
Merge branch 'main' into worknetPlugins
cloudmation df8144d
added console out logging when plugins are loaded
cloudmation 6482eb3
renamed sample logger and added more events to listen to
cloudmation a283e03
added more info to console out onCommitting
cloudmation dbdae3c
updated Extending NEO Worknet readme
cloudmation 0af5f72
-updated worknet plugin sample to read from config file and write out…
cloudmation 4dec8aa
made plugin handler a static class
cloudmation 681a50f
- updated comments and readme
cloudmation faf26fb
updated readme to include configfile override
cloudmation File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Neo; | ||
using Neo.Ledger; | ||
using Neo.Network.P2P.Payloads; | ||
using Neo.Persistence; | ||
using Neo.Plugins; | ||
using Neo.SmartContract; | ||
using Neo.SmartContract.Native; | ||
|
||
namespace WorkNetExt; | ||
|
||
public class WorkNetLogger : Plugin | ||
ngdentdev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
private string _logFile = "./logger.log"; | ||
|
||
NeoSystem? neoSystem; | ||
ngdentdev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public WorkNetLogger() | ||
{ | ||
Blockchain.Committing += OnCommitting; | ||
ApplicationEngine.Log += OnAppEngineLog!; | ||
Neo.Utility.Logging += OnNeoUtilityLog; | ||
|
||
} | ||
|
||
public override void Dispose() | ||
{ | ||
Neo.Utility.Logging -= OnNeoUtilityLog; | ||
ApplicationEngine.Log -= OnAppEngineLog!; | ||
Blockchain.Committing -= OnCommitting; | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
// If Worknet plugins require custom configuration. The plugins are required to override the ConfigFile property so GetConfiguration() method can locate the config.json file. | ||
// Without this override, the default ConfigFile value is read from the PluginsDirectory property, which is relative to the assembly location. | ||
// However, WorkNet plugins are loaded from either ~/.neo/plugins folder or /plugins folder in the worknet exe directory. | ||
public override string ConfigFile => System.IO.Path.Combine(AppContext.BaseDirectory, "plugins", "config.json"); | ||
|
||
protected override void Configure() | ||
{ | ||
IConfigurationSection config = GetConfiguration(); | ||
_logFile = config.GetValue<string>("LogFile", _logFile); | ||
|
||
} | ||
protected override void OnSystemLoaded(NeoSystem system) | ||
{ | ||
if (neoSystem is not null) throw new Exception($"{nameof(OnSystemLoaded)} already called"); | ||
neoSystem = system; | ||
base.OnSystemLoaded(system); | ||
} | ||
|
||
void OnAppEngineLog(object sender, LogEventArgs args) | ||
{ | ||
var container = args.ScriptContainer is null | ||
? string.Empty | ||
: $" [{args.ScriptContainer.GetType().Name}]"; | ||
|
||
WriteToFile($"{GetContractName(args.ScriptHash)} Log: \"{args.Message}\" {container}"); | ||
} | ||
|
||
void OnNeoUtilityLog(string source, LogLevel level, object message) | ||
{ | ||
WriteToFile($"{DateTimeOffset.Now:HH:mm:ss.ff} {source} {level} {message}"); | ||
} | ||
|
||
void OnCommitting(NeoSystem system, Block block, DataCache snapshot, IReadOnlyList<Blockchain.ApplicationExecuted> applicationExecutedList) | ||
{ | ||
WriteToFile($"Blockchain Committing: {block.Hash} {block.Index} {block.Timestamp} {block.Transactions.Length} txs"); | ||
} | ||
|
||
protected string GetContractName(UInt160 scriptHash) | ||
{ | ||
if (neoSystem is not null) | ||
{ | ||
var contract = NativeContract.ContractManagement.GetContract(neoSystem.StoreView, scriptHash); | ||
if (contract is not null) | ||
{ | ||
return contract.Manifest.Name; | ||
} | ||
} | ||
|
||
return scriptHash.ToString(); | ||
} | ||
|
||
private void WriteToFile(string logMessage) | ||
{ | ||
using(StreamWriter writer = new StreamWriter(_logFile, true)) | ||
{ | ||
writer.WriteLine(logMessage); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"PluginConfiguration": { | ||
"LogFile": "./samplePlugLogger.log" | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<AssemblyName>worknet-ext-filelogger</AssemblyName> | ||
<OutputType>library</OutputType> | ||
<PackageId>WorkNetExt.FileLogger</PackageId> | ||
<PackAsTool>true</PackAsTool> | ||
<RootNamespace>WorkNetExt</RootNamespace> | ||
<PackageIcon /> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="../../neo-logo-72.png" Pack="true" Visible="false" PackagePath=""/> | ||
</ItemGroup> | ||
|
||
<Choose> | ||
<When Condition=" '$(BlockchainToolkitLibraryVersion)' == 'local'"> | ||
<ItemGroup> | ||
<ProjectReference Include="$(BlockchainToolkitLibraryLocalPath)\src\bctklib\bctklib.csproj" /> | ||
<ProjectReference Include="$(NeoMonorepoPath)\modules\src\DBFTPlugin\DBFTPlugin.csproj" /> | ||
<ProjectReference Include="$(NeoMonorepoPath)\modules\src\RpcServer\RpcServer.csproj" /> | ||
</ItemGroup> | ||
</When> | ||
<Otherwise> | ||
<ItemGroup> | ||
<PackageReference Include="Neo.BlockchainToolkit.Library" | ||
Version="$(BlockchainToolkitLibraryVersion)" /> | ||
<PackageReference Include="Neo.Consensus.DBFT" Version="$(NeoVersion)" /> | ||
<PackageReference Include="Neo.Plugins.RpcServer" Version="$(NeoVersion)" /> | ||
</ItemGroup> | ||
</Otherwise> | ||
</Choose> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Reflection; | ||
using Neo.Plugins; | ||
|
||
static class PluginHandler | ||
{ | ||
public static void LoadPlugins(string directory, TextWriter? writer = null) | ||
{ | ||
if (!Directory.Exists(directory)) return; | ||
List<Assembly> assemblies = new(); | ||
|
||
foreach (var filename in Directory.EnumerateFiles(directory, "*.dll", SearchOption.TopDirectoryOnly)) | ||
{ | ||
try | ||
{ | ||
assemblies.Add(Assembly.Load(File.ReadAllBytes(filename))); | ||
writer?.WriteLine($"Loaded plugin: {filename}"); | ||
} | ||
catch { } | ||
} | ||
|
||
foreach (Assembly assembly in assemblies) | ||
{ | ||
LoadPlugin(assembly, writer); | ||
} | ||
} | ||
|
||
public static void LoadPlugin(Assembly assembly, TextWriter? writer = null) | ||
{ | ||
foreach (Type type in assembly.ExportedTypes) | ||
{ | ||
if (!type.IsSubclassOf(typeof(Plugin))) continue; | ||
if (type.IsAbstract) continue; | ||
|
||
ConstructorInfo? constructor = type.GetConstructor(Type.EmptyTypes); | ||
try | ||
{ | ||
constructor?.Invoke(null); | ||
} | ||
catch (Exception ex) | ||
{ | ||
writer?.WriteLine($"Failed to load plugin: {type.FullName}"); | ||
writer?.WriteLine(ex.Message); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we also mention the need to override
ConfigFile
here in the readme?