Skip to content
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
2 changes: 1 addition & 1 deletion src/Neo/IEventHandlers/ILogHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public interface ILogHandler
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="logEventArgs">The arguments <see cref="LogEventArgs"/> of the log.</param>
void ApplicationEngine_Log_Handler(object sender, LogEventArgs logEventArgs);
void ApplicationEngine_Log_Handler(ApplicationEngine sender, LogEventArgs logEventArgs);
Copy link
Member

Choose a reason for hiding this comment

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

IApplicationEngineProvider is better?

Copy link
Member Author

Choose a reason for hiding this comment

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

Is not the provider, is the instance

}
}
18 changes: 15 additions & 3 deletions src/Neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,24 @@ public partial class ApplicationEngine : ExecutionEngine
/// </summary>
public const long TestModeGas = 20_00000000;

public delegate void OnInstanceHandlerEvent(ApplicationEngine engine);
public delegate void OnLogEvent(ApplicationEngine engine, LogEventArgs args);
public delegate void OnNotifyEvent(ApplicationEngine engine, NotifyEventArgs args);

/// <summary>
/// Triggered when a contract calls System.Runtime.Notify.
/// </summary>
public static event EventHandler<NotifyEventArgs> Notify;
public event OnNotifyEvent Notify;

/// <summary>
/// Triggered when a contract calls System.Runtime.Log.
/// </summary>
public static event EventHandler<LogEventArgs> Log;
public event OnLogEvent Log;

/// <summary>
/// On Application Engine
/// </summary>
public static OnInstanceHandlerEvent InstanceHandler;

private static Dictionary<uint, InteropDescriptor> services;
// Total amount of GAS spent to execute.
Expand Down Expand Up @@ -435,8 +444,11 @@ public static ApplicationEngine Create(TriggerType trigger, IVerifiable containe
// Adjust jump table according persistingBlock

var jumpTable = settings == null || settings.IsHardforkEnabled(Hardfork.HF_Echidna, index) ? DefaultJumpTable : NotEchidnaJumpTable;
return Provider?.Create(trigger, container, snapshot, persistingBlock, settings, gas, diagnostic, jumpTable)
var engine = Provider?.Create(trigger, container, snapshot, persistingBlock, settings, gas, diagnostic, jumpTable)
?? new ApplicationEngine(trigger, container, snapshot, persistingBlock, settings, gas, diagnostic, jumpTable);

InstanceHandler?.Invoke(engine);
return engine;
}

/// <summary>
Expand Down
11 changes: 8 additions & 3 deletions src/Plugins/ApplicationLogs/LogReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,15 @@ public override void Dispose()
Blockchain.Committing -= ((ICommittingHandler)this).Blockchain_Committing_Handler;
Blockchain.Committed -= ((ICommittedHandler)this).Blockchain_Committed_Handler;
if (Settings.Default.Debug)
ApplicationEngine.Log -= ((ILogHandler)this).ApplicationEngine_Log_Handler;
ApplicationEngine.InstanceHandler -= ConfigureAppEngine;
GC.SuppressFinalize(this);
}

private void ConfigureAppEngine(ApplicationEngine engine)
{
engine.Log += ((ILogHandler)this).ApplicationEngine_Log_Handler;
}

protected override void Configure()
{
Settings.Load(GetConfiguration());
Expand All @@ -82,7 +87,7 @@ protected override void OnSystemLoaded(NeoSystem system)
RpcServerPlugin.RegisterMethods(this, Settings.Default.Network);

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

#endregion
Expand Down Expand Up @@ -240,7 +245,7 @@ void ICommittedHandler.Blockchain_Committed_Handler(NeoSystem system, Block bloc
_neostore.CommitBlockLog();
}

void ILogHandler.ApplicationEngine_Log_Handler(object sender, LogEventArgs e)
void ILogHandler.ApplicationEngine_Log_Handler(ApplicationEngine sender, LogEventArgs e)
Copy link
Member

Choose a reason for hiding this comment

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

You know you can use IApplicationEngineProvider for this instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

Is not the provider, is the instance

{
if (Settings.Default.Debug == false)
return;
Expand Down
26 changes: 24 additions & 2 deletions tests/Neo.UnitTests/Extensions/NativeContractExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,34 @@ public static void DeleteContract(this DataCache snapshot, UInt160 hash)

public static StackItem Call(this NativeContract contract, DataCache snapshot, string method, params ContractParameter[] args)
{
return Call(contract, snapshot, null, null, method, args);
return Call(contract, snapshot, null, null, method, null, args);
}

public static StackItem Call(this NativeContract contract, DataCache snapshot, IVerifiable container, Block persistingBlock, string method, params ContractParameter[] args)
public static StackItem Call(this NativeContract contract, DataCache snapshot, string method, ApplicationEngine.OnNotifyEvent onNotify, params ContractParameter[] args)
{
return Call(contract, snapshot, null, null, method, onNotify, args);
}

public static StackItem Call(
this NativeContract contract, DataCache snapshot, IVerifiable container,
Block persistingBlock, string method, params ContractParameter[] args
)
{
return Call(contract, snapshot, container, persistingBlock, method, null, args);
}

public static StackItem Call(
this NativeContract contract, DataCache snapshot, IVerifiable container,
Block persistingBlock, string method, ApplicationEngine.OnNotifyEvent onNotify, params ContractParameter[] args
)
{
using var engine = ApplicationEngine.Create(TriggerType.Application, container, snapshot, persistingBlock, settings: TestProtocolSettings.Default);

if (onNotify != null)
{
engine.Notify += onNotify;
}

return Call(contract, engine, method, args);
}

Expand Down
10 changes: 4 additions & 6 deletions tests/Neo.UnitTests/SmartContract/Native/UT_RoleManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,18 @@ public void TestSetAndGet()
foreach (var role in roles)
{
var snapshot1 = _snapshotCache.CloneCache();
UInt160 committeeMultiSigAddr = NativeContract.NEO.GetCommitteeAddress(snapshot1);
List<NotifyEventArgs> notifications = new List<NotifyEventArgs>();
EventHandler<NotifyEventArgs> ev = (o, e) => notifications.Add(e);
ApplicationEngine.Notify += ev;
var committeeMultiSigAddr = NativeContract.NEO.GetCommitteeAddress(snapshot1);
List<NotifyEventArgs> notifications = [];
void Ev(ApplicationEngine o, NotifyEventArgs e) => notifications.Add(e);
var ret = NativeContract.RoleManagement.Call(
snapshot1,
new Nep17NativeContractExtensions.ManualWitness(committeeMultiSigAddr),
new Block { Header = new Header() },
"designateAsRole",
"designateAsRole", Ev,
new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)role) },
new ContractParameter(ContractParameterType.Array) { Value = publicKeys.Select(p => new ContractParameter(ContractParameterType.ByteArray) { Value = p.ToArray() }).ToList() }
);
snapshot1.Commit();
ApplicationEngine.Notify -= ev;
Assert.AreEqual(1, notifications.Count);
Assert.AreEqual("Designation", notifications[0].EventName);
var snapshot2 = _snapshotCache.CloneCache();
Expand Down
8 changes: 4 additions & 4 deletions tests/Neo.UnitTests/SmartContract/UT_ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ public void TestNotify()
var snapshotCache = TestBlockchain.GetTestSnapshotCache();
using var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshotCache, settings: TestProtocolSettings.Default);
engine.LoadScript(System.Array.Empty<byte>());
ApplicationEngine.Notify += Test_Notify1;
engine.Notify += Test_Notify1;
const string notifyEvent = "TestEvent";

engine.SendNotification(UInt160.Zero, notifyEvent, new Array());
Assert.AreEqual(notifyEvent, eventName);

ApplicationEngine.Notify += Test_Notify2;
engine.Notify += Test_Notify2;
engine.SendNotification(UInt160.Zero, notifyEvent, new Array());
Assert.IsNull(eventName);

eventName = notifyEvent;
ApplicationEngine.Notify -= Test_Notify1;
engine.Notify -= Test_Notify1;
engine.SendNotification(UInt160.Zero, notifyEvent, new Array());
Assert.IsNull(eventName);

ApplicationEngine.Notify -= Test_Notify2;
engine.Notify -= Test_Notify2;
engine.SendNotification(UInt160.Zero, notifyEvent, new Array());
Assert.IsNull(eventName);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Neo.UnitTests/SmartContract/UT_InteropService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,10 @@ public void TestRuntime_Log()
{
var engine = GetEngine(true);
var message = "hello";
ApplicationEngine.Log += LogEvent;
engine.Log += LogEvent;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
engine.Log += LogEvent;
engine.Log += OnLogEvent;

engine.RuntimeLog(Encoding.UTF8.GetBytes(message));
Assert.AreEqual(new byte[] { 0x01, 0x02, 0x03 }.ToHexString(), ((Transaction)engine.ScriptContainer).Script.Span.ToHexString());
ApplicationEngine.Log -= LogEvent;
engine.Log -= LogEvent;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
engine.Log -= LogEvent;
engine.Log -= OnLogEvent;

}

[TestMethod]
Expand Down