From bfc5e1a06dbdab748db9d556f62825316d378eb9 Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Thu, 29 Oct 2020 17:40:51 +0800 Subject: [PATCH 01/11] add event --- .../Native/Oracle/OracleContract.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/neo/SmartContract/Native/Oracle/OracleContract.cs b/src/neo/SmartContract/Native/Oracle/OracleContract.cs index 6f94698b45..5be7756a75 100644 --- a/src/neo/SmartContract/Native/Oracle/OracleContract.cs +++ b/src/neo/SmartContract/Native/Oracle/OracleContract.cs @@ -1,6 +1,7 @@ #pragma warning disable IDE0051 using Neo.Cryptography; +using Neo.IO; using Neo.Ledger; using Neo.Network.P2P.Payloads; using Neo.Persistence; @@ -34,6 +35,34 @@ public sealed class OracleContract : NativeContract internal OracleContract() { Manifest.Features = ContractFeatures.HasStorage; + + var events = new List(Manifest.Abi.Events) + { + new ContractEventDescriptor + { + Name = "Response", + Parameters = new ContractParameterDefinition[] + { + new ContractParameterDefinition() + { + Name = "OriginalTx", + Type = ContractParameterType.Hash160 + }, + new ContractParameterDefinition() + { + Name = "RequestId", + Type = ContractParameterType.Integer + }, + new ContractParameterDefinition() + { + Name = "ResponseTx", + Type = ContractParameterType.Hash160 + } + } + } + }; + + Manifest.Abi.Events = events.ToArray(); } [ContractMethod(0, CallFlags.AllowModifyStates)] @@ -113,6 +142,8 @@ protected override void PostPersist(ApplicationEngine engine) int index = (int)(response.Id % (ulong)nodes.Length); nodes[index].GAS += OracleRequestPrice; } + + engine.SendNotification(Hash, "Response", new VM.Types.Array { request.OriginalTxid.ToArray(), response.Id, tx.Hash.ToArray() }); } if (nodes != null) { From 245d8ed5f39793b85353a69ddf84b8f027199bd3 Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Thu, 29 Oct 2020 18:16:35 +0800 Subject: [PATCH 02/11] add Request event --- .../Native/Oracle/OracleContract.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/neo/SmartContract/Native/Oracle/OracleContract.cs b/src/neo/SmartContract/Native/Oracle/OracleContract.cs index 5be7756a75..115a4e5f35 100644 --- a/src/neo/SmartContract/Native/Oracle/OracleContract.cs +++ b/src/neo/SmartContract/Native/Oracle/OracleContract.cs @@ -38,6 +38,28 @@ internal OracleContract() var events = new List(Manifest.Abi.Events) { + new ContractEventDescriptor + { + Name = "Request", + Parameters = new ContractParameterDefinition[] + { + new ContractParameterDefinition() + { + Name = "RequestId", + Type = ContractParameterType.Integer + }, + new ContractParameterDefinition() + { + Name = "Url", + Type = ContractParameterType.String + }, + new ContractParameterDefinition() + { + Name = "Filter", + Type = ContractParameterType.String + } + } + }, new ContractEventDescriptor { Name = "Response", @@ -192,6 +214,8 @@ private void Request(ApplicationEngine engine, string url, string filter, string if (list.Count >= 256) throw new InvalidOperationException("There are too many pending responses for this url"); list.Add(id); + + engine.SendNotification(Hash, "Request", new VM.Types.Array { id, url, filter }); } [ContractMethod(0_01000000, CallFlags.None)] From 5d8f45fda8ea9822451d898d040db4c7f918be63 Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Thu, 29 Oct 2020 18:24:20 +0800 Subject: [PATCH 03/11] move Reponse into Finish method --- src/neo/SmartContract/Native/Oracle/OracleContract.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/neo/SmartContract/Native/Oracle/OracleContract.cs b/src/neo/SmartContract/Native/Oracle/OracleContract.cs index 115a4e5f35..a0abdb3e0a 100644 --- a/src/neo/SmartContract/Native/Oracle/OracleContract.cs +++ b/src/neo/SmartContract/Native/Oracle/OracleContract.cs @@ -95,6 +95,7 @@ private void Finish(ApplicationEngine engine) if (response == null) throw new ArgumentException("Oracle response was not found"); OracleRequest request = GetRequest(engine.Snapshot, response.Id); if (request == null) throw new ArgumentException("Oracle request was not found"); + engine.SendNotification(Hash, "Response", new VM.Types.Array { request.OriginalTxid.ToArray(), response.Id, tx.Hash.ToArray() }); StackItem userData = BinarySerializer.Deserialize(request.UserData, engine.Limits.MaxStackSize, engine.Limits.MaxItemSize, engine.ReferenceCounter); engine.CallFromNativeContract(null, request.CallbackContract, request.CallbackMethod, request.Url, userData, (int)response.Code, response.Result); } @@ -163,9 +164,7 @@ protected override void PostPersist(ApplicationEngine engine) { int index = (int)(response.Id % (ulong)nodes.Length); nodes[index].GAS += OracleRequestPrice; - } - - engine.SendNotification(Hash, "Response", new VM.Types.Array { request.OriginalTxid.ToArray(), response.Id, tx.Hash.ToArray() }); + } } if (nodes != null) { From 96d034bf6bf5032966a5120184d9eb4f52f0acf6 Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Thu, 29 Oct 2020 18:26:12 +0800 Subject: [PATCH 04/11] format --- src/neo/SmartContract/Native/Oracle/OracleContract.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/SmartContract/Native/Oracle/OracleContract.cs b/src/neo/SmartContract/Native/Oracle/OracleContract.cs index a0abdb3e0a..17801672ad 100644 --- a/src/neo/SmartContract/Native/Oracle/OracleContract.cs +++ b/src/neo/SmartContract/Native/Oracle/OracleContract.cs @@ -164,7 +164,7 @@ protected override void PostPersist(ApplicationEngine engine) { int index = (int)(response.Id % (ulong)nodes.Length); nodes[index].GAS += OracleRequestPrice; - } + } } if (nodes != null) { From ba0ca4a304ecaed4d7c98ac50ad9fb1b98b61616 Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Thu, 29 Oct 2020 18:30:49 +0800 Subject: [PATCH 05/11] optimize --- src/neo/SmartContract/Native/Oracle/OracleContract.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/neo/SmartContract/Native/Oracle/OracleContract.cs b/src/neo/SmartContract/Native/Oracle/OracleContract.cs index 17801672ad..6bfefc380b 100644 --- a/src/neo/SmartContract/Native/Oracle/OracleContract.cs +++ b/src/neo/SmartContract/Native/Oracle/OracleContract.cs @@ -74,11 +74,6 @@ internal OracleContract() { Name = "RequestId", Type = ContractParameterType.Integer - }, - new ContractParameterDefinition() - { - Name = "ResponseTx", - Type = ContractParameterType.Hash160 } } } @@ -95,7 +90,7 @@ private void Finish(ApplicationEngine engine) if (response == null) throw new ArgumentException("Oracle response was not found"); OracleRequest request = GetRequest(engine.Snapshot, response.Id); if (request == null) throw new ArgumentException("Oracle request was not found"); - engine.SendNotification(Hash, "Response", new VM.Types.Array { request.OriginalTxid.ToArray(), response.Id, tx.Hash.ToArray() }); + engine.SendNotification(Hash, "Response", new VM.Types.Array { request.OriginalTxid.ToArray(), response.Id }); StackItem userData = BinarySerializer.Deserialize(request.UserData, engine.Limits.MaxStackSize, engine.Limits.MaxItemSize, engine.ReferenceCounter); engine.CallFromNativeContract(null, request.CallbackContract, request.CallbackMethod, request.Url, userData, (int)response.Code, response.Result); } From b7eaea1256c2cefd6210bb8f152946680b351aeb Mon Sep 17 00:00:00 2001 From: Luchuan Date: Tue, 3 Nov 2020 20:51:13 +0800 Subject: [PATCH 06/11] Update src/neo/SmartContract/Native/Oracle/OracleContract.cs Co-authored-by: Erik Zhang --- src/neo/SmartContract/Native/Oracle/OracleContract.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/SmartContract/Native/Oracle/OracleContract.cs b/src/neo/SmartContract/Native/Oracle/OracleContract.cs index 6bfefc380b..599cae807c 100644 --- a/src/neo/SmartContract/Native/Oracle/OracleContract.cs +++ b/src/neo/SmartContract/Native/Oracle/OracleContract.cs @@ -40,7 +40,7 @@ internal OracleContract() { new ContractEventDescriptor { - Name = "Request", + Name = "OracleRequest", Parameters = new ContractParameterDefinition[] { new ContractParameterDefinition() From e733795e96f1885eb03256fb2bdb83992a2f0fc9 Mon Sep 17 00:00:00 2001 From: Luchuan Date: Tue, 3 Nov 2020 20:51:25 +0800 Subject: [PATCH 07/11] Update src/neo/SmartContract/Native/Oracle/OracleContract.cs Co-authored-by: Erik Zhang --- src/neo/SmartContract/Native/Oracle/OracleContract.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/SmartContract/Native/Oracle/OracleContract.cs b/src/neo/SmartContract/Native/Oracle/OracleContract.cs index 599cae807c..eb9d175cc1 100644 --- a/src/neo/SmartContract/Native/Oracle/OracleContract.cs +++ b/src/neo/SmartContract/Native/Oracle/OracleContract.cs @@ -62,7 +62,7 @@ internal OracleContract() }, new ContractEventDescriptor { - Name = "Response", + Name = "OracleResponse", Parameters = new ContractParameterDefinition[] { new ContractParameterDefinition() From 847daddc1e7caebd5640a6a361146bac20cfaa3d Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Wed, 4 Nov 2020 18:09:35 +0800 Subject: [PATCH 08/11] fix --- src/neo/SmartContract/Native/Oracle/OracleContract.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/neo/SmartContract/Native/Oracle/OracleContract.cs b/src/neo/SmartContract/Native/Oracle/OracleContract.cs index eb9d175cc1..3d0c939f47 100644 --- a/src/neo/SmartContract/Native/Oracle/OracleContract.cs +++ b/src/neo/SmartContract/Native/Oracle/OracleContract.cs @@ -90,7 +90,7 @@ private void Finish(ApplicationEngine engine) if (response == null) throw new ArgumentException("Oracle response was not found"); OracleRequest request = GetRequest(engine.Snapshot, response.Id); if (request == null) throw new ArgumentException("Oracle request was not found"); - engine.SendNotification(Hash, "Response", new VM.Types.Array { request.OriginalTxid.ToArray(), response.Id }); + engine.SendNotification(Hash, "OracleResponse", new VM.Types.Array { request.OriginalTxid.ToArray(), response.Id }); StackItem userData = BinarySerializer.Deserialize(request.UserData, engine.Limits.MaxStackSize, engine.Limits.MaxItemSize, engine.ReferenceCounter); engine.CallFromNativeContract(null, request.CallbackContract, request.CallbackMethod, request.Url, userData, (int)response.Code, response.Result); } @@ -209,7 +209,7 @@ private void Request(ApplicationEngine engine, string url, string filter, string throw new InvalidOperationException("There are too many pending responses for this url"); list.Add(id); - engine.SendNotification(Hash, "Request", new VM.Types.Array { id, url, filter }); + engine.SendNotification(Hash, "OracleRequest", new VM.Types.Array { id, url, filter }); } [ContractMethod(0_01000000, CallFlags.None)] From 6081a38dce3609beee7cd4ecf18c917e0c85e64a Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Thu, 5 Nov 2020 15:07:37 +0800 Subject: [PATCH 09/11] apply erik's feedback --- .../SmartContract/Native/Oracle/OracleContract.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/neo/SmartContract/Native/Oracle/OracleContract.cs b/src/neo/SmartContract/Native/Oracle/OracleContract.cs index 3d0c939f47..ae55f78cda 100644 --- a/src/neo/SmartContract/Native/Oracle/OracleContract.cs +++ b/src/neo/SmartContract/Native/Oracle/OracleContract.cs @@ -45,7 +45,7 @@ internal OracleContract() { new ContractParameterDefinition() { - Name = "RequestId", + Name = "Id", Type = ContractParameterType.Integer }, new ContractParameterDefinition() @@ -67,13 +67,13 @@ internal OracleContract() { new ContractParameterDefinition() { - Name = "OriginalTx", - Type = ContractParameterType.Hash160 + Name = "Id", + Type = ContractParameterType.Integer }, new ContractParameterDefinition() { - Name = "RequestId", - Type = ContractParameterType.Integer + Name = "OriginalTx", + Type = ContractParameterType.Hash160 } } } @@ -90,7 +90,7 @@ private void Finish(ApplicationEngine engine) if (response == null) throw new ArgumentException("Oracle response was not found"); OracleRequest request = GetRequest(engine.Snapshot, response.Id); if (request == null) throw new ArgumentException("Oracle request was not found"); - engine.SendNotification(Hash, "OracleResponse", new VM.Types.Array { request.OriginalTxid.ToArray(), response.Id }); + engine.SendNotification(Hash, "OracleResponse", new VM.Types.Array { response.Id, request.OriginalTxid.ToArray() }); StackItem userData = BinarySerializer.Deserialize(request.UserData, engine.Limits.MaxStackSize, engine.Limits.MaxItemSize, engine.ReferenceCounter); engine.CallFromNativeContract(null, request.CallbackContract, request.CallbackMethod, request.Url, userData, (int)response.Code, response.Result); } From 9fa747507345d5775e6bbd6d00e8c0a25427f030 Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Thu, 5 Nov 2020 17:58:44 +0800 Subject: [PATCH 10/11] add RequestContract --- src/neo/SmartContract/Native/Oracle/OracleContract.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/neo/SmartContract/Native/Oracle/OracleContract.cs b/src/neo/SmartContract/Native/Oracle/OracleContract.cs index ae55f78cda..a05391ec42 100644 --- a/src/neo/SmartContract/Native/Oracle/OracleContract.cs +++ b/src/neo/SmartContract/Native/Oracle/OracleContract.cs @@ -49,6 +49,11 @@ internal OracleContract() Type = ContractParameterType.Integer }, new ContractParameterDefinition() + { + Name = "RequestContract", + Type = ContractParameterType.Hash160 + }, + new ContractParameterDefinition() { Name = "Url", Type = ContractParameterType.String @@ -209,7 +214,7 @@ private void Request(ApplicationEngine engine, string url, string filter, string throw new InvalidOperationException("There are too many pending responses for this url"); list.Add(id); - engine.SendNotification(Hash, "OracleRequest", new VM.Types.Array { id, url, filter }); + engine.SendNotification(Hash, "OracleRequest", new VM.Types.Array { id, engine.CallingScriptHash.ToArray(), url, filter }); } [ContractMethod(0_01000000, CallFlags.None)] From 55a89ad2f35c4cc44e5bfbcc33294275f7089722 Mon Sep 17 00:00:00 2001 From: Luchuan Date: Thu, 5 Nov 2020 18:10:12 +0800 Subject: [PATCH 11/11] Update src/neo/SmartContract/Native/Oracle/OracleContract.cs Co-authored-by: Erik Zhang --- src/neo/SmartContract/Native/Oracle/OracleContract.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/SmartContract/Native/Oracle/OracleContract.cs b/src/neo/SmartContract/Native/Oracle/OracleContract.cs index a05391ec42..8fe55459ee 100644 --- a/src/neo/SmartContract/Native/Oracle/OracleContract.cs +++ b/src/neo/SmartContract/Native/Oracle/OracleContract.cs @@ -78,7 +78,7 @@ internal OracleContract() new ContractParameterDefinition() { Name = "OriginalTx", - Type = ContractParameterType.Hash160 + Type = ContractParameterType.Hash256 } } }