Skip to content

Commit

Permalink
Rename SYSCALLs (neo-project#1362)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored and Tommo-L committed Jun 22, 2020
1 parent 203aa9f commit ae66b59
Show file tree
Hide file tree
Showing 31 changed files with 1,256 additions and 1,131 deletions.
2 changes: 1 addition & 1 deletion src/neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private static Transaction DeployNativeContracts()
byte[] script;
using (ScriptBuilder sb = new ScriptBuilder())
{
sb.EmitSysCall(InteropService.Neo_Native_Deploy);
sb.EmitSysCall(InteropService.Native.Deploy);
script = sb.ToArray();
}
return new Transaction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Neo.SmartContract
{
internal static class StackItemSerializer
internal static class BinarySerializer
{
public static StackItem Deserialize(byte[] data, uint maxItemSize, ReferenceCounter referenceCounter = null)
{
Expand Down
4 changes: 2 additions & 2 deletions src/neo/SmartContract/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static byte[] CreateMultiSigRedeemScript(int m, params ECPoint[] publicKe
}
sb.EmitPush(publicKeys.Length);
sb.Emit(OpCode.PUSHNULL);
sb.EmitSysCall(InteropService.Neo_Crypto_ECDsaCheckMultiSig);
sb.EmitSysCall(InteropService.Crypto.ECDsaCheckMultiSig);
return sb.ToArray();
}
}
Expand All @@ -102,7 +102,7 @@ public static byte[] CreateSignatureRedeemScript(ECPoint publicKey)
{
sb.EmitPush(publicKey.EncodePoint(true));
sb.Emit(OpCode.PUSHNULL);
sb.EmitSysCall(InteropService.Neo_Crypto_ECDsaVerify);
sb.EmitSysCall(InteropService.Crypto.ECDsaVerify);
return sb.ToArray();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/neo/SmartContract/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static bool IsMultiSigContract(this byte[] script, out int m, out int n)
if (script[i++] != (byte)OpCode.PUSHNULL) return false;
if (script[i++] != (byte)OpCode.SYSCALL) return false;
if (script.Length != i + 4) return false;
if (BitConverter.ToUInt32(script, i) != InteropService.Neo_Crypto_ECDsaCheckMultiSig)
if (BitConverter.ToUInt32(script, i) != InteropService.Crypto.ECDsaCheckMultiSig)
return false;
return true;
}
Expand All @@ -74,7 +74,7 @@ public static bool IsSignatureContract(this byte[] script)
|| script[1] != 33
|| script[35] != (byte)OpCode.PUSHNULL
|| script[36] != (byte)OpCode.SYSCALL
|| BitConverter.ToUInt32(script, 37) != InteropService.Neo_Crypto_ECDsaVerify)
|| BitConverter.ToUInt32(script, 37) != InteropService.Crypto.ECDsaVerify)
return false;
return true;
}
Expand Down
13 changes: 9 additions & 4 deletions src/neo/SmartContract/InteropDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@

namespace Neo.SmartContract
{
internal class InteropDescriptor
public class InteropDescriptor
{
public string Method { get; }
public uint Hash { get; }
public Func<ApplicationEngine, bool> Handler { get; }
internal Func<ApplicationEngine, bool> Handler { get; }
public long Price { get; }
public Func<EvaluationStack, long> PriceCalculator { get; }
public TriggerType AllowedTriggers { get; }

public InteropDescriptor(string method, Func<ApplicationEngine, bool> handler, long price, TriggerType allowedTriggers)
internal InteropDescriptor(string method, Func<ApplicationEngine, bool> handler, long price, TriggerType allowedTriggers)
: this(method, handler, allowedTriggers)
{
this.Price = price;
}

public InteropDescriptor(string method, Func<ApplicationEngine, bool> handler, Func<EvaluationStack, long> priceCalculator, TriggerType allowedTriggers)
internal InteropDescriptor(string method, Func<ApplicationEngine, bool> handler, Func<EvaluationStack, long> priceCalculator, TriggerType allowedTriggers)
: this(method, handler, allowedTriggers)
{
this.PriceCalculator = priceCalculator;
Expand All @@ -36,5 +36,10 @@ public long GetPrice(EvaluationStack stack)
{
return PriceCalculator is null ? Price : PriceCalculator(stack);
}

public static implicit operator uint(InteropDescriptor descriptor)
{
return descriptor.Hash;
}
}
}
50 changes: 50 additions & 0 deletions src/neo/SmartContract/InteropService.Binary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Neo.VM;
using Neo.VM.Types;
using System;
using System.IO;

namespace Neo.SmartContract
{
partial class InteropService
{
public static class Binary
{
public static readonly InteropDescriptor Serialize = Register("System.Binary.Serialize", Binary_Serialize, 0_00100000, TriggerType.All);
public static readonly InteropDescriptor Deserialize = Register("System.Binary.Deserialize", Binary_Deserialize, 0_00500000, TriggerType.All);

private static bool Binary_Serialize(ApplicationEngine engine)
{
byte[] serialized;
try
{
serialized = BinarySerializer.Serialize(engine.CurrentContext.EvaluationStack.Pop(), engine.MaxItemSize);
}
catch
{
return false;
}
engine.CurrentContext.EvaluationStack.Push(serialized);
return true;
}

private static bool Binary_Deserialize(ApplicationEngine engine)
{
StackItem item;
try
{
item = BinarySerializer.Deserialize(engine.CurrentContext.EvaluationStack.Pop().GetSpan(), engine.MaxItemSize, engine.ReferenceCounter);
}
catch (FormatException)
{
return false;
}
catch (IOException)
{
return false;
}
engine.CurrentContext.EvaluationStack.Push(item);
return true;
}
}
}
}
107 changes: 107 additions & 0 deletions src/neo/SmartContract/InteropService.Blockchain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Numerics;

namespace Neo.SmartContract
{
partial class InteropService
{
public static class Blockchain
{
public static readonly InteropDescriptor GetHeight = Register("System.Blockchain.GetHeight", Blockchain_GetHeight, 0_00000400, TriggerType.Application);
public static readonly InteropDescriptor GetBlock = Register("System.Blockchain.GetBlock", Blockchain_GetBlock, 0_02500000, TriggerType.Application);
public static readonly InteropDescriptor GetTransaction = Register("System.Blockchain.GetTransaction", Blockchain_GetTransaction, 0_01000000, TriggerType.Application);
public static readonly InteropDescriptor GetTransactionHeight = Register("System.Blockchain.GetTransactionHeight", Blockchain_GetTransactionHeight, 0_01000000, TriggerType.Application);
public static readonly InteropDescriptor GetTransactionFromBlock = Register("System.Blockchain.GetTransactionFromBlock", Blockchain_GetTransactionFromBlock, 0_01000000, TriggerType.Application);
public static readonly InteropDescriptor GetContract = Register("System.Blockchain.GetContract", Blockchain_GetContract, 0_01000000, TriggerType.Application);

private static bool Blockchain_GetHeight(ApplicationEngine engine)
{
engine.CurrentContext.EvaluationStack.Push(engine.Snapshot.Height);
return true;
}

private static bool Blockchain_GetBlock(ApplicationEngine engine)
{
ReadOnlySpan<byte> data = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
UInt256 hash;
if (data.Length <= 5)
hash = Ledger.Blockchain.Singleton.GetBlockHash((uint)new BigInteger(data));
else if (data.Length == 32)
hash = new UInt256(data);
else
return false;

Block block = hash != null ? engine.Snapshot.GetBlock(hash) : null;
if (block == null)
engine.CurrentContext.EvaluationStack.Push(StackItem.Null);
else
engine.CurrentContext.EvaluationStack.Push(block.ToStackItem(engine.ReferenceCounter));
return true;
}

private static bool Blockchain_GetTransaction(ApplicationEngine engine)
{
ReadOnlySpan<byte> hash = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
Transaction tx = engine.Snapshot.GetTransaction(new UInt256(hash));
if (tx == null)
engine.CurrentContext.EvaluationStack.Push(StackItem.Null);
else
engine.CurrentContext.EvaluationStack.Push(tx.ToStackItem(engine.ReferenceCounter));
return true;
}

private static bool Blockchain_GetTransactionHeight(ApplicationEngine engine)
{
ReadOnlySpan<byte> hash = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
var tx = engine.Snapshot.Transactions.TryGet(new UInt256(hash));
engine.CurrentContext.EvaluationStack.Push(tx != null ? new BigInteger(tx.BlockIndex) : BigInteger.MinusOne);
return true;
}

private static bool Blockchain_GetTransactionFromBlock(ApplicationEngine engine)
{
ReadOnlySpan<byte> data = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
UInt256 hash;
if (data.Length <= 5)
hash = Ledger.Blockchain.Singleton.GetBlockHash((uint)new BigInteger(data));
else if (data.Length == 32)
hash = new UInt256(data);
else
return false;

TrimmedBlock block = hash != null ? engine.Snapshot.Blocks.TryGet(hash) : null;
if (block == null)
{
engine.CurrentContext.EvaluationStack.Push(StackItem.Null);
}
else
{
int index = (int)engine.CurrentContext.EvaluationStack.Pop().GetBigInteger();
if (index < 0 || index >= block.Hashes.Length - 1) return false;

Transaction tx = engine.Snapshot.GetTransaction(block.Hashes[index + 1]);
if (tx == null)
engine.CurrentContext.EvaluationStack.Push(StackItem.Null);
else
engine.CurrentContext.EvaluationStack.Push(tx.ToStackItem(engine.ReferenceCounter));
}
return true;
}

private static bool Blockchain_GetContract(ApplicationEngine engine)
{
UInt160 hash = new UInt160(engine.CurrentContext.EvaluationStack.Pop().GetSpan());
ContractState contract = engine.Snapshot.Contracts.TryGet(hash);
if (contract == null)
engine.CurrentContext.EvaluationStack.Push(StackItem.Null);
else
engine.CurrentContext.EvaluationStack.Push(contract.ToStackItem(engine.ReferenceCounter));
return true;
}
}
}
}
Loading

0 comments on commit ae66b59

Please sign in to comment.