-
Notifications
You must be signed in to change notification settings - Fork 100
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
add logs to execution #737
Changes from 14 commits
a634f59
042db65
86a68cc
a89b625
f7eefd3
5b731eb
724fc54
4546018
ac65c60
ea6d01a
e23f226
c45f703
8888011
46aa190
7b9723e
397458c
391abe0
c4b093d
f7afd69
12ded50
0bd5c11
0aa880b
c0f35ec
8a83e5c
6d15ff0
c0e72ac
9c724dd
68f35f6
5783063
0e3ccb6
12a36c6
b899935
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,6 @@ | |
using Microsoft.AspNetCore.ResponseCompression; | ||
using Microsoft.AspNetCore.Server.Kestrel.Https; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Neo.IO; | ||
using Neo.IO.Json; | ||
using Neo.Network.P2P; | ||
using System; | ||
|
@@ -49,6 +48,7 @@ public RpcServer(NeoSystem system, RpcServerSettings settings) | |
Initialize_SmartContract(); | ||
} | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like the variable name change in LogReader.cs, these kind of changes make it harder to review PRs. We should do changes like this in a dedicated PR like neo-project/neo#2785 |
||
private bool CheckAuth(HttpContext context) | ||
{ | ||
if (string.IsNullOrEmpty(settings.RpcUser)) return true; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,9 @@ | |
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Neo.Ledger; | ||
using Neo.Network.P2P.Payloads; | ||
using Neo.SmartContract; | ||
|
||
namespace Neo.Plugins | ||
{ | ||
|
@@ -22,6 +25,16 @@ public class RpcServerPlugin : Plugin | |
private static readonly Dictionary<uint, RpcServer> servers = new(); | ||
private static readonly Dictionary<uint, List<object>> handlers = new(); | ||
|
||
/// <summary> | ||
/// Public interface of _logEvents. | ||
/// </summary> | ||
public static Dictionary<UInt256, Queue<LogEventArgs>> LogEvents { get; } = new(); | ||
|
||
/// <summary> | ||
/// Maximum number of events to be logged per contract | ||
/// </summary> | ||
private const int MaxLogEvents = 50; | ||
|
||
protected override void Configure() | ||
{ | ||
settings = new Settings(GetConfiguration()); | ||
|
@@ -30,11 +43,9 @@ protected override void Configure() | |
server.UpdateSettings(s); | ||
} | ||
|
||
public override void Dispose() | ||
private void OnCommitted(NeoSystem system, Block block) | ||
{ | ||
foreach (var (_, server) in servers) | ||
server.Dispose(); | ||
base.Dispose(); | ||
LogEvents.Clear(); | ||
} | ||
|
||
protected override void OnSystemLoaded(NeoSystem system) | ||
|
@@ -54,6 +65,34 @@ protected override void OnSystemLoaded(NeoSystem system) | |
|
||
server.StartRpcServer(); | ||
servers.TryAdd(s.Network, server); | ||
|
||
// It is potentially possible to have dos attack by sending a lot of transactions and logs. | ||
// To prevent this, we limit the number of logs to be logged per contract. | ||
// If the number of logs is greater than MAX_LOG_EVENTS, we remove the oldest log. | ||
void Ev(object _, LogEventArgs e) | ||
{ | ||
if (e.ScriptContainer is not Transaction tx) return; | ||
if (!LogEvents.TryGetValue(tx.Hash, out var _logs)) | ||
{ | ||
_logs = new Queue<LogEventArgs>(); | ||
LogEvents.Add(tx.Hash, _logs); | ||
} | ||
if (LogEvents[tx.Hash].Count >= MaxLogEvents) | ||
{ | ||
_logs.Dequeue(); | ||
} | ||
_logs.Enqueue(e); | ||
} | ||
ApplicationEngine.Log += Ev; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method will be called multiple times. |
||
Blockchain.Committed += OnCommitted; | ||
} | ||
|
||
public override void Dispose() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like we could revert all the changes to this file in this PR |
||
{ | ||
Blockchain.Committed -= OnCommitted; | ||
foreach (var (_, server) in servers) | ||
server.Dispose(); | ||
base.Dispose(); | ||
} | ||
|
||
public static void RegisterMethods(object handler, uint network) | ||
|
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.
Changes like this make it much harder to review PRs. Reviewers need to check each change to see if it is substantive or name change.