Skip to content

Commit

Permalink
Add EventName to NotifyEventArgs (neo-project#267)
Browse files Browse the repository at this point in the history
* Add EventName to NotifyEventArgs

* Update UT cases

* Change to all lower case

* Fix UTs

* Fix format

* Fix format again
  • Loading branch information
joeqian10 authored and ProDog committed Jul 13, 2020
1 parent d55d88c commit a2667be
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/ApplicationLogs/LogReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void OnPersist(StoreView snapshot, IReadOnlyList<Blockchain.ApplicationEx
json["txid"] = appExec.Transaction?.Hash.ToString();
json["trigger"] = appExec.Trigger;
json["vmstate"] = appExec.VMState;
json["gas_consumed"] = appExec.GasConsumed.ToString();
json["gasconsumed"] = appExec.GasConsumed.ToString();
try
{
json["stack"] = appExec.Stack.Select(q => q.ToJson()).ToArray();
Expand All @@ -64,6 +64,7 @@ public void OnPersist(StoreView snapshot, IReadOnlyList<Blockchain.ApplicationEx
{
JObject notification = new JObject();
notification["contract"] = q.ScriptHash.ToString();
notification["eventname"] = q.EventName;
try
{
notification["state"] = q.State.ToJson();
Expand Down
8 changes: 6 additions & 2 deletions src/RpcClient/Models/RpcApplicationLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public JObject ToJson()
json["txid"] = TxId?.ToString();
json["trigger"] = Trigger;
json["vmstate"] = VMState;
json["gas_consumed"] = GasConsumed.ToString();
json["gasconsumed"] = GasConsumed.ToString();
json["stack"] = Stack.Select(q => q.ToJson()).ToArray();
json["notifications"] = Notifications.Select(q => q.ToJson()).ToArray();
return json;
Expand All @@ -38,7 +38,7 @@ public static RpcApplicationLog FromJson(JObject json)
log.TxId = json["txid"] is null ? null : UInt256.Parse(json["txid"].AsString());
log.Trigger = json["trigger"].TryGetEnum<TriggerType>();
log.VMState = json["vmstate"].TryGetEnum<VMState>();
log.GasConsumed = long.Parse(json["gas_consumed"].AsString());
log.GasConsumed = long.Parse(json["gasconsumed"].AsString());
log.Stack = ((JArray)json["stack"]).Select(p => ContractParameter.FromJson(p)).ToList();
log.Notifications = ((JArray)json["notifications"]).Select(p => RpcNotifyEventArgs.FromJson(p)).ToList();
return log;
Expand All @@ -49,12 +49,15 @@ public class RpcNotifyEventArgs
{
public UInt160 Contract { get; set; }

public string EventName { get; set; }

public ContractParameter State { get; set; }

public JObject ToJson()
{
JObject json = new JObject();
json["contract"] = Contract.ToString();
json["eventname"] = EventName;
json["state"] = State.ToJson();
return json;
}
Expand All @@ -64,6 +67,7 @@ public static RpcNotifyEventArgs FromJson(JObject json)
return new RpcNotifyEventArgs
{
Contract = UInt160.Parse(json["contract"].AsString()),
EventName = json["eventname"].AsString(),
State = ContractParameter.FromJson(json["state"])
};
}
Expand Down
22 changes: 10 additions & 12 deletions src/RpcNep5Tracker/RpcNep5Tracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,31 +100,29 @@ private void RecordTransferHistory(StoreView snapshot, UInt160 scriptHash, UInt1
transferIndex++;
}

private void HandleNotification(StoreView snapshot, IVerifiable scriptContainer, UInt160 scriptHash,
private void HandleNotification(StoreView snapshot, IVerifiable scriptContainer, UInt160 scriptHash, string eventName,
VM.Types.Array stateItems,
Dictionary<Nep5BalanceKey, Nep5Balance> nep5BalancesChanged, ref ushort transferIndex)
{
if (stateItems.Count == 0) return;
// Event name should be encoded as a byte array.
if (!(stateItems[0] is VM.Types.ByteString)) return;
var eventName = stateItems[0].GetString();
if (eventName != "Transfer") return;
if (stateItems.Count < 4) return;
if (stateItems.Count < 3) return;

if (!(stateItems[1].IsNull) && !(stateItems[1] is VM.Types.ByteString))
if (!(stateItems[0].IsNull) && !(stateItems[0] is VM.Types.ByteString))
return;
if (!(stateItems[2].IsNull) && !(stateItems[2] is VM.Types.ByteString))
if (!(stateItems[1].IsNull) && !(stateItems[1] is VM.Types.ByteString))
return;
var amountItem = stateItems[3];
var amountItem = stateItems[2];
if (!(amountItem is VM.Types.ByteString || amountItem is VM.Types.Integer))
return;
byte[] fromBytes = stateItems[1].IsNull ? null : stateItems[1].GetSpan().ToArray();
byte[] fromBytes = stateItems[0].IsNull ? null : stateItems[0].GetSpan().ToArray();
if (fromBytes != null && fromBytes.Length != UInt160.Length)
return;
byte[] toBytes = stateItems[2].IsNull ? null : stateItems[2].GetSpan().ToArray();
byte[] toBytes = stateItems[1].IsNull ? null : stateItems[1].GetSpan().ToArray();
if (toBytes != null && toBytes.Length != UInt160.Length)
return;
if (fromBytes == null && toBytes == null) return;

var from = UInt160.Zero;
var to = UInt160.Zero;

Expand Down Expand Up @@ -162,8 +160,8 @@ public void OnPersist(StoreView snapshot, IReadOnlyList<Blockchain.ApplicationEx
{
if (!(notifyEventArgs?.State is VM.Types.Array stateItems) || stateItems.Count == 0)
continue;
HandleNotification(snapshot, notifyEventArgs.ScriptContainer, notifyEventArgs.ScriptHash, stateItems,
nep5BalancesChanged, ref transferIndex);
HandleNotification(snapshot, notifyEventArgs.ScriptContainer, notifyEventArgs.ScriptHash, notifyEventArgs.EventName,
stateItems, nep5BalancesChanged, ref transferIndex);
}
}

Expand Down
12 changes: 3 additions & 9 deletions tests/Neo.Network.RPC.Tests/RpcTestCases.json
Original file line number Diff line number Diff line change
Expand Up @@ -1131,18 +1131,15 @@
"txid": "0x183cd84359cd9f8b956afcd02403ec07361c1dba55f0800241b4ef2b28e88bbb",
"trigger": "Application",
"vmstate": "HALT",
"gas_consumed": "9007810",
"gasconsumed": "9007810",
"stack": [],
"notifications": [
{
"contract": "0x8c23f196d8a1bfd103a9dcb1f9ccf0c611377d3b",
"eventname": "Transfer",
"state": {
"type": "Array",
"value": [
{
"type": "ByteArray",
"value": "VHJhbnNmZXI="
},
{
"type": "Any"
},
Expand All @@ -1159,13 +1156,10 @@
},
{
"contract": "0x9bde8f209c88dd0e7ca3bf0af0f476cdd8207789",
"eventname": "Transfer",
"state": {
"type": "Array",
"value": [
{
"type": "ByteArray",
"value": "VHJhbnNmZXI="
},
{
"type": "ByteArray",
"value": "eU9We10tADBFvus3caGe5hjcaNE="
Expand Down

0 comments on commit a2667be

Please sign in to comment.