Skip to content

Commit

Permalink
Add oracle Request/Response event (neo-project#2036)
Browse files Browse the repository at this point in the history
* add event

* add Request event

* move Reponse into Finish method

* format

* optimize

* Update src/neo/SmartContract/Native/Oracle/OracleContract.cs

Co-authored-by: Erik Zhang <erik@neo.org>

* Update src/neo/SmartContract/Native/Oracle/OracleContract.cs

Co-authored-by: Erik Zhang <erik@neo.org>

* fix

* apply erik's feedback

* add RequestContract

* Update src/neo/SmartContract/Native/Oracle/OracleContract.cs

Co-authored-by: Erik Zhang <erik@neo.org>

Co-authored-by: Tommo-L <luchuan@neo.org>
Co-authored-by: Erik Zhang <erik@neo.org>
Co-authored-by: Shargon <shargon@gmail.com>
  • Loading branch information
4 people authored and Shawn committed Jan 8, 2021
1 parent 8b3d518 commit 6044357
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/neo/SmartContract/Native/Oracle/OracleContract.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -34,6 +35,56 @@ public sealed class OracleContract : NativeContract
internal OracleContract()
{
Manifest.Features = ContractFeatures.HasStorage;

var events = new List<ContractEventDescriptor>(Manifest.Abi.Events)
{
new ContractEventDescriptor
{
Name = "OracleRequest",
Parameters = new ContractParameterDefinition[]
{
new ContractParameterDefinition()
{
Name = "Id",
Type = ContractParameterType.Integer
},
new ContractParameterDefinition()
{
Name = "RequestContract",
Type = ContractParameterType.Hash160
},
new ContractParameterDefinition()
{
Name = "Url",
Type = ContractParameterType.String
},
new ContractParameterDefinition()
{
Name = "Filter",
Type = ContractParameterType.String
}
}
},
new ContractEventDescriptor
{
Name = "OracleResponse",
Parameters = new ContractParameterDefinition[]
{
new ContractParameterDefinition()
{
Name = "Id",
Type = ContractParameterType.Integer
},
new ContractParameterDefinition()
{
Name = "OriginalTx",
Type = ContractParameterType.Hash256
}
}
}
};

Manifest.Abi.Events = events.ToArray();
}

[ContractMethod(0, CallFlags.AllowModifyStates)]
Expand All @@ -44,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, "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);
}
Expand Down Expand Up @@ -161,6 +213,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, "OracleRequest", new VM.Types.Array { id, engine.CallingScriptHash.ToArray(), url, filter });
}

[ContractMethod(0_01000000, CallFlags.None)]
Expand Down

0 comments on commit 6044357

Please sign in to comment.