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 oracle Request/Response event #2036

Merged
merged 14 commits into from
Nov 5, 2020
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