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

[Remove] ApplicationLogs from Plugin System #3619

Closed
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
6 changes: 5 additions & 1 deletion src/Neo.CLI/CLI/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
using Neo.ConsoleService;
using Neo.Cryptography.ECC;
using Neo.Extensions;
using Neo.IO;
using Neo.Json;
using Neo.Ledger;
using Neo.Network.P2P;
using Neo.Network.P2P.Payloads;
using Neo.Plugins;
using Neo.Plugins.ApplicationLogs;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using Neo.SmartContract.Native;
Expand Down Expand Up @@ -380,6 +380,10 @@ public async void Start(CommandLineOptions options)
{
NeoSystem = new NeoSystem(protocol, Settings.Default.Storage.Engine,
string.Format(Settings.Default.Storage.Path, protocol.Network.ToString("X8")));

// Add old plugins here
var appLogs = new ApplicationLogsPlugin($"{AppContext.BaseDirectory}\\configs", NeoSystem);
RegisterCommand(appLogs, "ApplicationLogsPlugin");
}
catch (DllNotFoundException ex) when (ex.Message.Contains("libleveldb"))
{
Expand Down
7 changes: 7 additions & 0 deletions src/Neo.CLI/Neo.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@
<ProjectReference Include="..\Neo.ConsoleService\Neo.ConsoleService.csproj" />
<ProjectReference Include="..\Neo.Extensions\Neo.Extensions.csproj" />
<ProjectReference Include="..\Neo\Neo.csproj" />
<ProjectReference Include="..\Plugins\ApplicationLogs\ApplicationLogs.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.CommandLine.NamingConventionBinder" Version="2.0.0-beta4.22272.1" />
</ItemGroup>

<ItemGroup>
<None Update="configs\ApplicationLogs.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
{
"PluginConfiguration": {
"ApplicationLogs": {
"AutoStart": false,
"Path": "ApplicationLogs_{0}",
"Network": 860833102,
"MaxStackSize": 65535,
"Debug": false,
"UnhandledExceptionPolicy": "StopPlugin"
},
"Dependency": [
"RpcServer"
]
}
}
6 changes: 0 additions & 6 deletions src/Plugins/ApplicationLogs/ApplicationLogs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
</ProjectReference>
</ItemGroup>

<ItemGroup>
<None Update="ApplicationLogs.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="$(PackageId).Tests" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// LogReader.cs file belongs to the neo project and is free
// ApplicationLogsPlugin.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
Expand All @@ -9,6 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Microsoft.Extensions.Configuration;
using Neo.ConsoleService;
using Neo.Extensions;
using Neo.IEventHandlers;
Expand All @@ -26,63 +27,61 @@

namespace Neo.Plugins.ApplicationLogs
{
public class LogReader : Plugin, ICommittingHandler, ICommittedHandler, ILogHandler
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this affect the function in CLI.Plugin? like when we list the plugins, can we still get the ApplicationLogs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No you will not see ApplicationLogs in the plugins. Future we will not have plugins. It will be features.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't list the plugin by RPC

public class ApplicationLogsPlugin : IDisposable, ICommittingHandler, ICommittedHandler, ILogHandler
{
#region Globals

internal NeoStore _neostore;
private NeoSystem _neosystem;
internal readonly NeoStore NeoStore;
private readonly NeoSystem _neoSystem;
private readonly List<LogEventArgs> _logEvents;

#endregion

public override string Name => "ApplicationLogs";
public override string Description => "Synchronizes smart contract VM executions and notifications (NotifyLog) on blockchain.";
protected override UnhandledExceptionPolicy ExceptionPolicy => Settings.Default.ExceptionPolicy;
public UnhandledExceptionPolicy ExceptionPolicy => Settings.Current.ExceptionPolicy;

#region Ctor

public LogReader()
public ApplicationLogsPlugin(string configDirectory, NeoSystem neoSystem)
{
_logEvents = new();
Blockchain.Committing += ((ICommittingHandler)this).Blockchain_Committing_Handler;
Blockchain.Committed += ((ICommittedHandler)this).Blockchain_Committed_Handler;

var configuration = new ConfigurationBuilder()
.AddJsonFile(Combine(configDirectory, "ApplicationLogs.json"), optional: true)
.Build()
.GetSection("ApplicationLogs");

Settings.Load(configuration);

if (Settings.Current.AutoStart)
{
var path = string.Format(Settings.Current.Path, neoSystem.Settings.Network.ToString("X8"));
var store = neoSystem.LoadStore(GetFullPath(path));
NeoStore = new NeoStore(store);
_neoSystem = neoSystem;
RpcServerPlugin.RegisterMethods(this, neoSystem.Settings.Network);

if (Settings.Current.Debug)
ApplicationEngine.Log += ((ILogHandler)this).ApplicationEngine_Log_Handler;
}

(configuration as IDisposable)?.Dispose();
}

#endregion

#region Override Methods

public override string ConfigFile => Combine(RootPath, "ApplicationLogs.json");

public override void Dispose()
public void Dispose()
{
Blockchain.Committing -= ((ICommittingHandler)this).Blockchain_Committing_Handler;
Blockchain.Committed -= ((ICommittedHandler)this).Blockchain_Committed_Handler;
if (Settings.Default.Debug)
if (Settings.Current.Debug)
ApplicationEngine.Log -= ((ILogHandler)this).ApplicationEngine_Log_Handler;
GC.SuppressFinalize(this);
}

protected override void Configure()
{
Settings.Load(GetConfiguration());
}

protected override void OnSystemLoaded(NeoSystem system)
{
if (system.Settings.Network != Settings.Default.Network)
return;
string path = string.Format(Settings.Default.Path, Settings.Default.Network.ToString("X8"));
var store = system.LoadStore(GetFullPath(path));
_neostore = new NeoStore(store);
_neosystem = system;
RpcServerPlugin.RegisterMethods(this, Settings.Default.Network);

if (Settings.Default.Debug)
ApplicationEngine.Log += ((ILogHandler)this).ApplicationEngine_Log_Handler;
}

#endregion

#region JSON RPC Methods
Expand Down Expand Up @@ -128,7 +127,7 @@ internal void OnGetBlockCommand(string blockHashOrIndex, string eventName = null
UInt256 blockhash;
if (uint.TryParse(blockHashOrIndex, out var blockIndex))
{
blockhash = NativeContract.Ledger.GetBlockHash(_neosystem.StoreView, blockIndex);
blockhash = NativeContract.Ledger.GetBlockHash(_neoSystem.StoreView, blockIndex);
}
else if (UInt256.TryParse(blockHashOrIndex, out blockhash) == false)
{
Expand All @@ -137,11 +136,11 @@ internal void OnGetBlockCommand(string blockHashOrIndex, string eventName = null
}

var blockOnPersist = string.IsNullOrEmpty(eventName) ?
_neostore.GetBlockLog(blockhash, TriggerType.OnPersist) :
_neostore.GetBlockLog(blockhash, TriggerType.OnPersist, eventName);
NeoStore.GetBlockLog(blockhash, TriggerType.OnPersist) :
NeoStore.GetBlockLog(blockhash, TriggerType.OnPersist, eventName);
var blockPostPersist = string.IsNullOrEmpty(eventName) ?
_neostore.GetBlockLog(blockhash, TriggerType.PostPersist) :
_neostore.GetBlockLog(blockhash, TriggerType.PostPersist, eventName);
NeoStore.GetBlockLog(blockhash, TriggerType.PostPersist) :
NeoStore.GetBlockLog(blockhash, TriggerType.PostPersist, eventName);

if (blockOnPersist == null)
ConsoleHelper.Error($"No logs.");
Expand All @@ -157,8 +156,8 @@ internal void OnGetBlockCommand(string blockHashOrIndex, string eventName = null
internal void OnGetTransactionCommand(UInt256 txhash, string eventName = null)
{
var txApplication = string.IsNullOrEmpty(eventName) ?
_neostore.GetTransactionLog(txhash) :
_neostore.GetTransactionLog(txhash, eventName);
NeoStore.GetTransactionLog(txhash) :
NeoStore.GetTransactionLog(txhash, eventName);

if (txApplication == null)
ConsoleHelper.Error($"No logs.");
Expand All @@ -182,8 +181,8 @@ internal void OnGetContractCommand(UInt160 scripthash, uint page = 1, uint pageS
}

var txContract = string.IsNullOrEmpty(eventName) ?
_neostore.GetContractLog(scripthash, TriggerType.Application, page, pageSize) :
_neostore.GetContractLog(scripthash, TriggerType.Application, eventName, page, pageSize);
NeoStore.GetContractLog(scripthash, TriggerType.Application, page, pageSize) :
NeoStore.GetContractLog(scripthash, TriggerType.Application, eventName, page, pageSize);

if (txContract.Count == 0)
ConsoleHelper.Error($"No logs.");
Expand All @@ -198,40 +197,40 @@ internal void OnGetContractCommand(UInt160 scripthash, uint page = 1, uint pageS

void ICommittingHandler.Blockchain_Committing_Handler(NeoSystem system, Block block, DataCache snapshot, IReadOnlyList<Blockchain.ApplicationExecuted> applicationExecutedList)
{
if (system.Settings.Network != Settings.Default.Network)
if (!Settings.Current.AutoStart)
return;

if (_neostore is null)
if (NeoStore is null)
return;
_neostore.StartBlockLogBatch();
_neostore.PutBlockLog(block, applicationExecutedList);
if (Settings.Default.Debug)
NeoStore.StartBlockLogBatch();
NeoStore.PutBlockLog(block, applicationExecutedList);
if (Settings.Current.Debug)
{
foreach (var appEng in applicationExecutedList.Where(w => w.Transaction != null))
{
var logs = _logEvents.Where(w => w.ScriptContainer.Hash == appEng.Transaction.Hash).ToList();
if (logs.Any())
_neostore.PutTransactionEngineLogState(appEng.Transaction.Hash, logs);
NeoStore.PutTransactionEngineLogState(appEng.Transaction.Hash, logs);
}
_logEvents.Clear();
}
}

void ICommittedHandler.Blockchain_Committed_Handler(NeoSystem system, Block block)
{
if (system.Settings.Network != Settings.Default.Network)
if (!Settings.Current.AutoStart)
return;
if (_neostore is null)
if (NeoStore is null)
return;
_neostore.CommitBlockLog();
NeoStore.CommitBlockLog();
}

void ILogHandler.ApplicationEngine_Log_Handler(object sender, LogEventArgs e)
{
if (Settings.Default.Debug == false)
if (Settings.Current.AutoStart == false)
return;

if (_neosystem.Settings.Network != Settings.Default.Network)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We remove the network settings, unexpected change for this PR

if (!Settings.Current.Debug)
return;

if (e.ScriptContainer == null)
Expand Down Expand Up @@ -277,7 +276,7 @@ private void PrintExecutionToConsole(BlockchainExecutionModel model)
ConsoleHelper.Info($" {GetMethodParameterName(notifyItem.ScriptHash, notifyItem.EventName, ncount, i)}: ", $"{notifyItem.State[i].ToJson()}");
}
}
if (Settings.Default.Debug)
if (Settings.Current.Debug)
{
if (model.Logs.Length == 0)
ConsoleHelper.Info("Logs: ", "[]");
Expand Down Expand Up @@ -311,7 +310,7 @@ private void PrintEventModelToConsole(IReadOnlyCollection<(BlockchainEventModel

private string GetMethodParameterName(UInt160 scriptHash, string methodName, uint ncount, int parameterIndex)
{
var contract = NativeContract.ContractManagement.GetContract(_neosystem.StoreView, scriptHash);
var contract = NativeContract.ContractManagement.GetContract(_neoSystem.StoreView, scriptHash);
if (contract == null)
return $"{parameterIndex}";
var contractEvent = contract.Manifest.Abi.Events.SingleOrDefault(s => s.Name == methodName && (uint)s.Parameters.Length == ncount);
Expand All @@ -331,7 +330,7 @@ private JObject EventModelToJObject(BlockchainEventModel model)

private JObject TransactionToJObject(UInt256 txHash)
{
var appLog = _neostore.GetTransactionLog(txHash);
var appLog = NeoStore.GetTransactionLog(txHash);
if (appLog == null)
return null;

Expand All @@ -346,7 +345,7 @@ private JObject TransactionToJObject(UInt256 txHash)

try
{
trigger["stack"] = appLog.Stack.Select(s => s.ToJson(Settings.Default.MaxStackSize)).ToArray();
trigger["stack"] = appLog.Stack.Select(s => s.ToJson(Settings.Current.MaxStackSize)).ToArray();
}
catch (Exception ex)
{
Expand Down Expand Up @@ -375,7 +374,7 @@ private JObject TransactionToJObject(UInt256 txHash)
return notification;
}).ToArray();

if (Settings.Default.Debug)
if (Settings.Current.Debug)
{
trigger["logs"] = appLog.Logs.Select(s =>
{
Expand All @@ -392,8 +391,8 @@ private JObject TransactionToJObject(UInt256 txHash)

private JObject BlockToJObject(UInt256 blockHash)
{
var blockOnPersist = _neostore.GetBlockLog(blockHash, TriggerType.OnPersist);
var blockPostPersist = _neostore.GetBlockLog(blockHash, TriggerType.PostPersist);
var blockOnPersist = NeoStore.GetBlockLog(blockHash, TriggerType.OnPersist);
var blockPostPersist = NeoStore.GetBlockLog(blockHash, TriggerType.PostPersist);

if (blockOnPersist == null && blockPostPersist == null)
return null;
Expand All @@ -419,7 +418,7 @@ private JObject BlockItemToJObject(BlockchainExecutionModel blockExecutionModel)
trigger["gasconsumed"] = blockExecutionModel.GasConsumed.ToString();
try
{
trigger["stack"] = blockExecutionModel.Stack.Select(q => q.ToJson(Settings.Default.MaxStackSize)).ToArray();
trigger["stack"] = blockExecutionModel.Stack.Select(q => q.ToJson(Settings.Current.MaxStackSize)).ToArray();
}
catch (Exception ex)
{
Expand All @@ -445,7 +444,7 @@ private JObject BlockItemToJObject(BlockchainExecutionModel blockExecutionModel)
return notification;
}).ToArray();

if (Settings.Default.Debug)
if (Settings.Current.Debug)
{
trigger["logs"] = blockExecutionModel.Logs.Select(s =>
{
Expand Down
8 changes: 4 additions & 4 deletions src/Plugins/ApplicationLogs/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ namespace Neo.Plugins.ApplicationLogs
{
internal class Settings : PluginSettings
{
public bool AutoStart { get; }
public string Path { get; }
public uint Network { get; }
public int MaxStackSize { get; }

public bool Debug { get; }

public static Settings Default { get; private set; }
public static Settings Current { get; private set; }

private Settings(IConfigurationSection section) : base(section)
{
AutoStart = section.GetValue("AutoStart", false);
Path = section.GetValue("Path", "ApplicationLogs_{0}");
Network = section.GetValue("Network", 5195086u);
MaxStackSize = section.GetValue("MaxStackSize", (int)ushort.MaxValue);
Debug = section.GetValue("Debug", false);
}

public static void Load(IConfigurationSection section)
{
Default = new Settings(section);
Current = new Settings(section);
}
}
}
4 changes: 2 additions & 2 deletions src/Plugins/ApplicationLogs/Store/LogStorageStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ public Guid PutStackItemState(StackItem stackItem)
.ToArray();
try
{
_snapshot.Put(key, BinarySerializer.Serialize(stackItem, ExecutionEngineLimits.Default with { MaxItemSize = (uint)Settings.Default.MaxStackSize }));
_snapshot.Put(key, BinarySerializer.Serialize(stackItem, ExecutionEngineLimits.Default with { MaxItemSize = (uint)Settings.Current.MaxStackSize }));
}
catch
{
_snapshot.Put(key, BinarySerializer.Serialize(StackItem.Null, ExecutionEngineLimits.Default with { MaxItemSize = (uint)Settings.Default.MaxStackSize }));
_snapshot.Put(key, BinarySerializer.Serialize(StackItem.Null, ExecutionEngineLimits.Default with { MaxItemSize = (uint)Settings.Current.MaxStackSize }));
}
return id;
}
Expand Down
Loading
Loading