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

add logs to execution #737

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a634f59
add logs to execution
Jim8y Jun 25, 2022
042db65
fix issue
Jim8y Jun 25, 2022
86a68cc
optimise code style
Jim8y Jun 25, 2022
a89b625
optimise code
Jim8y Jun 25, 2022
f7eefd3
fix log message issue
Jim8y Jun 25, 2022
5b731eb
add log to transaction log
Jim8y Jul 1, 2022
724fc54
remove unused import
Jim8y Jul 1, 2022
4546018
Update LogReader.cs
erikzhang Jul 2, 2022
ac65c60
Merge branch 'master' into log-in-execution
Jim8y Jul 2, 2022
ea6d01a
make initial more clear
Jim8y Jul 3, 2022
e23f226
revert change to rpc client
Jim8y Jul 3, 2022
c45f703
add logs to the rpcserver plugin
Jim8y Jul 3, 2022
8888011
use queue to manage logs
Jim8y Jul 3, 2022
46aa190
update JArray
Jim8y Jul 4, 2022
7b9723e
remove log event on dispose
Jim8y Jul 5, 2022
397458c
revert _db change and move log to rpcserver
Jim8y Jul 11, 2022
391abe0
fix error
Jim8y Jul 11, 2022
c4b093d
update the function name
Jim8y Jul 11, 2022
f7afd69
Merge branch 'master' into log-in-execution
Jim8y Jul 13, 2022
12ded50
revert name change
Jim8y Jul 13, 2022
0bd5c11
Merge branch 'log-in-execution' of github.com:Liaojinghui/neo-modules…
Jim8y Jul 13, 2022
0aa880b
revert csproj change
Jim8y Jul 13, 2022
c0f35ec
revert order change
Jim8y Jul 13, 2022
8a83e5c
remove unused use
Jim8y Jul 13, 2022
6d15ff0
Merge branch 'master' into log-in-execution
Jim8y Sep 3, 2022
c0e72ac
Merge branch 'master' into log-in-execution
shargon Sep 12, 2022
9c724dd
Merge branch 'master' into log-in-execution
superboyiii Oct 9, 2022
68f35f6
Update RpcServerPlugin.cs
shargon Oct 10, 2022
5783063
make log size configurable
Jim8y Oct 10, 2022
0e3ccb6
Merge branch 'master' into log-in-execution
Jim8y Oct 26, 2022
12a36c6
Merge branch 'master' into log-in-execution
superboyiii Nov 7, 2022
b899935
Merge branch 'master' into log-in-execution
Jim8y Feb 21, 2023
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
31 changes: 29 additions & 2 deletions src/ApplicationLogs/LogReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public class LogReader : Plugin
public override string Name => "ApplicationLogs";
public override string Description => "Synchronizes the smart contract log with the NativeContract log (Notify)";

/// <summary>
/// LogEvents is a list of events that are logged by the smart contract.
/// </summary>
private readonly Dictionary<UInt256, List<LogEventArgs>> _logEvents = new();

public LogReader()
{
Blockchain.Committing += OnCommitting;
Expand All @@ -54,6 +59,19 @@ protected override void OnSystemLoaded(NeoSystem system)
{
if (system.Settings.Network != Settings.Default.Network) return;
RpcServerPlugin.RegisterMethods(this, Settings.Default.Network);

// It is possible to have dos attack by sending a lot of transactions.
void Ev(object _, LogEventArgs e)
{
if (e.ScriptContainer is not Transaction tx) return;
if (!_logEvents.TryGetValue(tx.Hash, out var list))
{
list = new();
_logEvents.Add(tx.Hash, list);
}
list.Add(e);
}
ApplicationEngine.Log += Ev;
Copy link
Member

Choose a reason for hiding this comment

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

Should we add a setting in config.json to control whether logs are saved or not? I think maybe not all nodes want to do this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The term log and notify is abusively miss used, I don't think the user could tell which is log and which is notify.

}

[RpcMethod]
Expand Down Expand Up @@ -114,8 +132,7 @@ public static JObject TxLogToJson(Blockchain.ApplicationExecuted appExec)
}
return notification;
}).ToArray();

txJson["executions"] = new List<JObject>() { trigger }.ToArray();
txJson["executions"] = new JArray(trigger);
return txJson;
}

Expand Down Expand Up @@ -176,8 +193,18 @@ private void OnCommitting(NeoSystem system, Block block, DataCache snapshot, IRe
foreach (var appExec in applicationExecutedList.Where(p => p.Transaction != null))
{
var txJson = TxLogToJson(appExec);
if (_logEvents.TryGetValue(appExec.Transaction.Hash, out var list))
{
var logs = list.Select(q => new JObject
{
["contract"] = q.ScriptHash.ToString(),
["message"] = q.Message
}).ToArray();
txJson["logs"] = logs;
}
Put(appExec.Transaction.Hash.ToArray(), Neo.Utility.StrictUTF8.GetBytes(txJson.ToString()));
}
_logEvents.Clear();

//processing log for block
var blockJson = BlockLogToJson(block, applicationExecutedList);
Expand Down
49 changes: 40 additions & 9 deletions src/RpcClient/Models/RpcApplicationLog.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2015-2021 The Neo Project.
// Copyright (C) 2015-2022 The Neo Project.
//
// The Neo.Network.RPC is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
Expand Down Expand Up @@ -61,15 +61,20 @@ public class Execution

public List<RpcNotifyEventArgs> Notifications { get; set; }

public List<RpcLogEventArgs> Logs { get; set; }

public JObject ToJson()
{
JObject json = new();
json["trigger"] = Trigger;
json["vmstate"] = VMState;
json["gasconsumed"] = GasConsumed.ToString();
json["exception"] = ExceptionMessage;
json["stack"] = Stack.Select(q => q.ToJson()).ToArray();
json["notifications"] = Notifications.Select(q => q.ToJson()).ToArray();
JObject json = new()
{
["trigger"] = Trigger,
["vmstate"] = VMState,
["gasconsumed"] = GasConsumed.ToString(),
["exception"] = ExceptionMessage,
["stack"] = Stack.Select(q => q.ToJson()).ToArray(),
["notifications"] = Notifications.Select(q => q.ToJson()).ToArray(),
["logs"] = Logs.Select(q => q.ToJson()).ToArray()
};
return json;
}

Expand All @@ -82,7 +87,8 @@ public static Execution FromJson(JObject json, ProtocolSettings protocolSettings
GasConsumed = long.Parse(json["gasconsumed"].AsString()),
ExceptionMessage = json["exception"]?.AsString(),
Stack = ((JArray)json["stack"]).Select(p => Utility.StackItemFromJson(p)).ToList(),
Notifications = ((JArray)json["notifications"]).Select(p => RpcNotifyEventArgs.FromJson(p, protocolSettings)).ToList()
Notifications = ((JArray)json["notifications"]).Select(p => RpcNotifyEventArgs.FromJson(p, protocolSettings)).ToList(),
Logs = ((JArray)json["logs"]).Select(p => RpcLogEventArgs.FromJson(p, protocolSettings)).ToList()
};
}
}
Expand Down Expand Up @@ -114,4 +120,29 @@ public static RpcNotifyEventArgs FromJson(JObject json, ProtocolSettings protoco
};
}
}

public class RpcLogEventArgs
{
public UInt160 Contract { get; set; }

public string Message { get; set; }

public JObject ToJson()
{
return new JObject
{
["contract"] = Contract.ToString(),
["message"] = Message
};
}

public static RpcLogEventArgs FromJson(JObject json, ProtocolSettings protocolSettings)
{
return new RpcLogEventArgs
{
Contract = json["contract"].ToScriptHash(protocolSettings),
Message = json["message"].AsString()
};
}
}
}
8 changes: 8 additions & 0 deletions src/RpcServer/RpcServer.SmartContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ private JObject GetInvokeResult(byte[] script, Signer[] signers = null, Witness[
obj["state"] = ToJson(n.State, session);
return obj;
}));
json["logs"] = new JArray(session.Engine.Notifications.Select(n =>
Copy link
Member

Choose a reason for hiding this comment

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

This is incorrect.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This logic was when i try to add the log in the core, checking and removing useless code.

{
var obj = new JObject();
obj["eventname"] = n.EventName;
obj["contract"] = n.ScriptHash.ToString();
obj["state"] = ToJson(n.State, session);
return obj;
}));
if (useDiagnostic)
{
Diagnostic diagnostic = (Diagnostic)session.Engine.Diagnostic;
Expand Down