forked from neo-project/neo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Asynchronous Oracle (neo-project#1738)
- Loading branch information
Showing
9 changed files
with
415 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using Neo.IO; | ||
using Neo.IO.Json; | ||
using Neo.Ledger; | ||
using Neo.Persistence; | ||
using Neo.SmartContract.Native; | ||
using Neo.SmartContract.Native.Oracle; | ||
using Neo.VM; | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace Neo.Network.P2P.Payloads | ||
{ | ||
public class OracleResponse : TransactionAttribute | ||
{ | ||
private const int MaxResultSize = 1024; | ||
|
||
public static readonly byte[] FixedScript; | ||
|
||
public ulong Id; | ||
public OracleResponseCode Code; | ||
public byte[] Result; | ||
|
||
public override TransactionAttributeType Type => TransactionAttributeType.OracleResponse; | ||
public override bool AllowMultiple => false; | ||
|
||
public override int Size => base.Size + | ||
sizeof(ulong) + //Id | ||
sizeof(OracleResponseCode) + //ResponseCode | ||
Result.GetVarSize(); //Result | ||
|
||
static OracleResponse() | ||
{ | ||
using ScriptBuilder sb = new ScriptBuilder(); | ||
sb.EmitAppCall(NativeContract.Oracle.Hash, "finish"); | ||
FixedScript = sb.ToArray(); | ||
} | ||
|
||
protected override void DeserializeWithoutType(BinaryReader reader) | ||
{ | ||
Id = reader.ReadUInt64(); | ||
Code = (OracleResponseCode)reader.ReadByte(); | ||
if (!Enum.IsDefined(typeof(OracleResponseCode), Code)) | ||
throw new FormatException(); | ||
Result = reader.ReadVarBytes(MaxResultSize); | ||
if (Code != OracleResponseCode.Success && Result.Length > 0) | ||
throw new FormatException(); | ||
} | ||
|
||
protected override void SerializeWithoutType(BinaryWriter writer) | ||
{ | ||
writer.Write(Id); | ||
writer.Write((byte)Code); | ||
writer.WriteVarBytes(Result); | ||
} | ||
|
||
public override JObject ToJson() | ||
{ | ||
JObject json = base.ToJson(); | ||
json["id"] = Id; | ||
json["code"] = Code; | ||
json["result"] = Convert.ToBase64String(Result); | ||
return json; | ||
} | ||
|
||
public override bool Verify(StoreView snapshot, Transaction tx) | ||
{ | ||
if (tx.Signers.Any(p => p.Scopes != WitnessScope.None)) return false; | ||
if (!tx.Script.AsSpan().SequenceEqual(FixedScript)) return false; | ||
OracleRequest request = NativeContract.Oracle.GetRequest(snapshot, Id); | ||
if (request is null) return false; | ||
if (tx.NetworkFee + tx.SystemFee != request.GasForResponse) return false; | ||
UInt160 oracleAccount = Blockchain.GetConsensusAddress(NativeContract.Oracle.GetOracleNodes(snapshot)); | ||
return tx.Signers.Any(p => p.Account.Equals(oracleAccount)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Neo.Network.P2P.Payloads | ||
{ | ||
public enum OracleResponseCode : byte | ||
{ | ||
Success = 0x00, | ||
|
||
NotFound = 0x10, | ||
Timeout = 0x12, | ||
Forbidden = 0x14, | ||
|
||
Error = 0xff | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/neo/SmartContract/Native/Oracle/OracleContract.Lists.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Neo.Cryptography.ECC; | ||
using Neo.IO; | ||
using Neo.VM; | ||
using Neo.VM.Types; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Neo.SmartContract.Native.Oracle | ||
{ | ||
partial class OracleContract | ||
{ | ||
private class IdList : List<ulong>, IInteroperable | ||
{ | ||
public void FromStackItem(StackItem stackItem) | ||
{ | ||
foreach (StackItem item in (Array)stackItem) | ||
Add((ulong)item.GetInteger()); | ||
} | ||
|
||
public StackItem ToStackItem(ReferenceCounter referenceCounter) | ||
{ | ||
return new Array(referenceCounter, this.Select(p => (Integer)p)); | ||
} | ||
} | ||
|
||
private class NodeList : List<ECPoint>, IInteroperable | ||
{ | ||
public void FromStackItem(StackItem stackItem) | ||
{ | ||
foreach (StackItem item in (Array)stackItem) | ||
Add(ECPoint.DecodePoint(item.GetSpan(), ECCurve.Secp256r1)); | ||
} | ||
|
||
public StackItem ToStackItem(ReferenceCounter referenceCounter) | ||
{ | ||
return new Array(referenceCounter, this.Select(p => (StackItem)p.ToArray())); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.