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

trigger type for getapplicationlog #380

Merged
merged 31 commits into from
Nov 16, 2020
Merged
Changes from 27 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4b18582
Merge pull request #4 from neo-project/master
Oct 13, 2020
330b158
Merge pull request #5 from neo-project/master
Oct 15, 2020
120f52c
Merge pull request #6 from neo-project/master
Oct 21, 2020
c7997a9
fixed-bug-1021
Oct 21, 2020
7c9f02b
Update src/RpcServer/RpcServer.SmartContract.cs
shargon Oct 21, 2020
1079aa3
😂
Oct 21, 2020
aa8e120
Keeping up to date with neo
Oct 29, 2020
928f3f4
Keeping up to date with neo
Oct 29, 2020
2c8cf6e
Revert "Keeping up to date with neo"
Oct 29, 2020
0b5473a
Prevent create key if not null
shargon Oct 29, 2020
cd4645e
dotnet format
shargon Oct 29, 2020
7d93776
Query application log via blockhash
Oct 30, 2020
319328d
Merge branch 'TriggerType' of https://github.com/chenzhitong/neo-modu…
Oct 30, 2020
dbc0f5a
Merge branch 'master' into TriggerType
Oct 30, 2020
1bdeb72
update
Nov 1, 2020
3fdc740
Merge branch 'TriggerType' of https://github.com/chenzhitong/neo-modu…
Nov 1, 2020
88554dd
Merge branch 'master' into TriggerType
Nov 2, 2020
582a785
Modifying the Json storage structure
Nov 2, 2020
43d0814
Merge branch 'TriggerType' of https://github.com/chenzhitong/neo-modu…
Nov 2, 2020
2e194b2
Modifying the JSON storage structure 2
Nov 2, 2020
745196d
Additional optional "trigger" parameter to getapplicationlog for clie…
Nov 3, 2020
6bf5a8d
Re-run checks
Nov 3, 2020
4e8b86e
Merge branch 'master' into TriggerType
Nov 4, 2020
2b58d37
Merge branch 'master' into TriggerType
Nov 9, 2020
24744d1
Merge branch 'master' into TriggerType
Nov 11, 2020
edf8e5b
StrictUTF8
Nov 11, 2020
934dd90
Merge branch 'TriggerType' of https://github.com/chenzhitong/neo-modu…
Nov 11, 2020
7a232fc
Update src/ApplicationLogs/LogReader.cs
shargon Nov 11, 2020
823c0f2
Merge branch 'master' into TriggerType
vncoelho Nov 12, 2020
ff729ec
Merge branch 'master' into TriggerType
superboyiii Nov 13, 2020
2b303d1
Optimize
shargon Nov 13, 2020
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
90 changes: 78 additions & 12 deletions src/ApplicationLogs/LogReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Neo.IO.Json;
using Neo.Ledger;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.VM;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -37,30 +38,50 @@ public JObject GetApplicationLog(JArray _params)
UInt256 hash = UInt256.Parse(_params[0].AsString());
byte[] value = db.Get(ReadOptions.Default, hash.ToArray());
if (value is null)
throw new RpcException(-100, "Unknown transaction");
return JObject.Parse(Encoding.UTF8.GetString(value));
throw new RpcException(-100, "Unknown transaction/blockhash");

//Additional optional "trigger" parameter to getapplicationlog for clients to be able to get just one execution result for a block.
if (_params.Count >= 2 && Enum.TryParse(_params[1].AsString(), true, out TriggerType trigger))
{
var raw = JObject.Parse(Utility.StrictUTF8.GetString(value));
var executions = raw["executions"] as JArray;
for (int i = 0; i < executions.Count;)
{
if (!executions[i]["trigger"].AsString().Equals(trigger.ToString(), StringComparison.OrdinalIgnoreCase))
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it supports. If want to query All, just do not set the trigger parameter.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the user may not know it, they're more likely to think that TriggerType.All is all 😅.

Copy link
Member

Choose a reason for hiding this comment

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

I think that we should not allow All

Copy link
Contributor

Choose a reason for hiding this comment

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

But why? It's the default anyway and I don't see any problem with specifying All.

Copy link
Member

Choose a reason for hiding this comment

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

Because neo never execute the contract with All

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, given the way triggers are defined I was expecting something more like

              if aer.Trigger&trig != 0 {
                      result = append(result, *aer)
              }

here (which is what we did in neo-go, for us specifying All is like not specifying anything). But I see now that ApplicationLogs plugin saves data using JSON in the DB and this doesn't allow for this trick (at least not that easy).

Copy link
Member

Choose a reason for hiding this comment

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

Trigger it's a flag because it can be used for filters (Interops) but we always execute the contract as 'non flag'.

executions.RemoveAt(i);
else
i++;
}
return raw;
}
else
{
return JObject.Parse(Utility.StrictUTF8.GetString(value));
}
}

public void OnPersist(StoreView snapshot, IReadOnlyList<Blockchain.ApplicationExecuted> applicationExecutedList)
{
WriteBatch writeBatch = new WriteBatch();

foreach (var appExec in applicationExecutedList)
//processing log for transactions
foreach (var appExec in applicationExecutedList.Where(p => p.Transaction != null))
{
JObject json = new JObject();
json["txid"] = appExec.Transaction?.Hash.ToString();
json["trigger"] = appExec.Trigger;
json["vmstate"] = appExec.VMState;
json["gasconsumed"] = appExec.GasConsumed.ToString();
var txJson = new JObject();
txJson["txid"] = appExec.Transaction.Hash.ToString();
JObject trigger = new JObject();
trigger["trigger"] = appExec.Trigger;
trigger["vmstate"] = appExec.VMState;
trigger["gasconsumed"] = appExec.GasConsumed.ToString();
try
{
json["stack"] = appExec.Stack.Select(q => q.ToJson()).ToArray();
trigger["stack"] = appExec.Stack.Select(q => q.ToJson()).ToArray();
}
catch (InvalidOperationException)
{
json["stack"] = "error: recursive reference";
trigger["stack"] = "error: recursive reference";
}
json["notifications"] = appExec.Notifications.Select(q =>
trigger["notifications"] = appExec.Notifications.Select(q =>
{
JObject notification = new JObject();
notification["contract"] = q.ScriptHash.ToString();
Expand All @@ -75,7 +96,52 @@ public void OnPersist(StoreView snapshot, IReadOnlyList<Blockchain.ApplicationEx
}
return notification;
}).ToArray();
writeBatch.Put((appExec.Transaction?.Hash ?? snapshot.PersistingBlock.Hash).ToArray(), Encoding.UTF8.GetBytes(json.ToString()));

txJson["executions"] = new List<JObject>() { trigger }.ToArray();
writeBatch.Put(appExec.Transaction.Hash.ToArray(), Utility.StrictUTF8.GetBytes(txJson.ToString()));
}

//processing log for block
var blocks = applicationExecutedList.Where(p => p.Transaction == null);
if (blocks.Count() > 0)
{
var blockJson = new JObject();
var blockHash = snapshot.PersistingBlock.Hash.ToArray();
blockJson["blockhash"] = "0x" + blockHash.Reverse().ToArray().ToHexString();
shargon marked this conversation as resolved.
Show resolved Hide resolved
var triggerList = new List<JObject>();
foreach (var appExec in blocks)
{
JObject trigger = new JObject();
trigger["trigger"] = appExec.Trigger;
trigger["vmstate"] = appExec.VMState;
trigger["gasconsumed"] = appExec.GasConsumed.ToString();
try
{
trigger["stack"] = appExec.Stack.Select(q => q.ToJson()).ToArray();
}
catch (InvalidOperationException)
{
trigger["stack"] = "error: recursive reference";
}
trigger["notifications"] = appExec.Notifications.Select(q =>
{
JObject notification = new JObject();
notification["contract"] = q.ScriptHash.ToString();
notification["eventname"] = q.EventName;
try
{
notification["state"] = q.State.ToJson();
}
catch (InvalidOperationException)
{
notification["state"] = "error: recursive reference";
}
return notification;
}).ToArray();
triggerList.Add(trigger);
}
blockJson["executions"] = triggerList.ToArray();
writeBatch.Put(blockHash, Utility.StrictUTF8.GetBytes(blockJson.ToString()));
}
db.Write(WriteOptions.Default, writeBatch);
}
Expand Down