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

[Compiler Fix + Framework Add] add oracle interface and fix issue #959

Merged
merged 4 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 0 additions & 3 deletions src/Neo.Compiler.CSharp/CompilationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,6 @@ private void ProcessClass(SemanticModel model, INamedTypeSymbol symbol)
{
return;
}
// Process the target contract
// add this compilation context
_engine.Contexts.Add(symbol, this);

foreach (var attribute in symbol.GetAttributesWithInherited())
{
Expand Down
14 changes: 11 additions & 3 deletions src/Neo.Compiler.CSharp/CompilationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@ private List<CompilationContext> CompileProjectContracts(Compilation compilation
foreach (var classNode in classNodes)
{
var classSymbol = semanticModel.GetDeclaredSymbol(classNode);
if (classSymbol != null && IsDerivedFromSmartContract(classSymbol, "Neo.SmartContract.Framework.SmartContract", semanticModel))
if (classSymbol is { IsAbstract: false, DeclaredAccessibility: Accessibility.Public } && IsDerivedFromSmartContract(classSymbol, "Neo.SmartContract.Framework.SmartContract", semanticModel))
{
allSmartContracts.Add(classSymbol);
classDependencies[classSymbol] = new List<INamedTypeSymbol>();
foreach (var member in classSymbol.GetMembers())
{
var memberTypeSymbol = (member as IFieldSymbol)?.Type ?? (member as IPropertySymbol)?.Type;
if (memberTypeSymbol is INamedTypeSymbol namedTypeSymbol && allSmartContracts.Contains(namedTypeSymbol))
if (memberTypeSymbol is INamedTypeSymbol namedTypeSymbol && allSmartContracts.Contains(namedTypeSymbol) && !namedTypeSymbol.IsAbstract)
{
classDependencies[classSymbol].Add(namedTypeSymbol);
}
Expand All @@ -138,7 +138,15 @@ private List<CompilationContext> CompileProjectContracts(Compilation compilation
if (classDependencies.Count == 0) throw new FormatException("No valid neo SmartContract found. Please make sure your contract is subclass of SmartContract and is not abstract.");
// Check contract dependencies, make sure there is no cycle in the dependency graph
var sortedClasses = TopologicalSort(classDependencies);
sortedClasses.ForEach(c => new CompilationContext(this, c).Compile());

sortedClasses.ForEach(c =>
{
var context = new CompilationContext(this, c);
context.Compile();
// Process the target contract add this compilation context
this.Contexts.Add(c, context);
});

return Contexts.Select(p => p.Value).ToList();
}

Expand Down
30 changes: 30 additions & 0 deletions src/Neo.SmartContract.Framework/Interfaces/IOracle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Neo.SmartContract.Framework.Native;

namespace Neo.SmartContract.Framework.Interfaces;

/// <summary>
/// Interface of oracle callback method
/// </summary>
public interface IOracle
{
/// <summary>
/// This method is called after the Oracle receives response from requested URL
/// </summary>
/// <param name="requestedUrl">The url of the data source</param>
/// <param name="userData">Extra oracle request data specified by user</param>
/// <param name="oracleResponse">Oracle response code <see cref="OracleResponseCode"/> from data source</param>
/// <param name="jsonString">The oracle response data in format of json string</param>
///
/// <example>
/// public static void OnOracleResponse(string url, object userData, OracleResponseCode code, string result)
/// {
/// if (Runtime.CallingScriptHash != Oracle.Hash) throw new Exception("Unauthorized!");
/// Storage.Put(Storage.CurrentContext, PreData, result);
/// }
/// </example>
public void OnOracleResponse(
Copy link
Member

Choose a reason for hiding this comment

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

Can we add:

if (Runtime.CallingScriptHash != Oracle.Hash)
throw new System.Exception("Unauthorized!");

in the interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Its an interface, no implementation can be added.

string requestedUrl,
object userData,
OracleResponseCode oracleResponse,
string jsonString);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Neo.SmartContract.Framework.Interfaces;
using Neo.SmartContract.Framework.Native;
using Neo.SmartContract.Framework.Services;

namespace Neo.SmartContract.Framework.UnitTests.TestClasses
{
public class Contract_IOracle : SmartContract, IOracle
{
private const string PreData = "PreData";

public void OnOracleResponse(string url, object userData, OracleResponseCode code, string result)
{
if (Runtime.CallingScriptHash != Oracle.Hash)
throw new System.Exception("Unauthorized!");
Storage.Put(Storage.CurrentContext, PreData, result);
}
}
}
30 changes: 30 additions & 0 deletions tests/Neo.SmartContract.Framework.UnitTests/Services/OracleTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Security.Cryptography;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract.TestEngine;
using Neo.VM;
using Neo.Wallets;

namespace Neo.SmartContract.Framework.UnitTests.Services
{
[TestClass]
public class OracleTest
{
private TestEngine.TestEngine _engine;

[TestInitialize]
public void Init()
{
_engine = new TestEngine.TestEngine();
_engine.AddEntryScript(Utils.Extensions.TestContractRoot + "Contract_IOracle.cs");
}

[TestMethod]
public void Test_OracleResponse()
{
_engine.Reset();
var result = _engine.ExecuteTestCaseStandard("onOracleResponse", "http://127.0.0.1", "test", 0x14, "{}");
Assert.AreEqual(VMState.FAULT, _engine.State);
}
}
}
Loading