Skip to content

Commit

Permalink
Neo.VM v3.0.0-CI00174 (#1320)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored and vncoelho committed Dec 4, 2019
1 parent 714621b commit 5cba80a
Show file tree
Hide file tree
Showing 21 changed files with 140 additions and 154 deletions.
13 changes: 3 additions & 10 deletions src/neo/Ledger/ContractState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Neo.VM.Types;
using System;
using System.IO;
using Array = Neo.VM.Types.Array;

namespace Neo.Ledger
{
Expand Down Expand Up @@ -76,17 +77,9 @@ public static ContractState FromJson(JObject json)
return contractState;
}

public StackItem ToStackItem()
public StackItem ToStackItem(ReferenceCounter referenceCounter)
{
return new VM.Types.Array
(
new StackItem[]
{
new ByteArray(Script),
new VM.Types.Boolean(HasStorage),
new VM.Types.Boolean(Payable),
}
);
return new Array(referenceCounter, new StackItem[] { Script, HasStorage, Payable });
}
}
}
42 changes: 19 additions & 23 deletions src/neo/Network/P2P/Payloads/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
using Neo.IO.Json;
using Neo.Ledger;
using Neo.SmartContract;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Array = Neo.VM.Types.Array;

namespace Neo.Network.P2P.Payloads
{
Expand Down Expand Up @@ -132,30 +134,24 @@ public TrimmedBlock Trim()
};
}

public StackItem ToStackItem()
public StackItem ToStackItem(ReferenceCounter referenceCounter)
{
return new VM.Types.Array
(
new StackItem[]
{
// Computed properties
new ByteArray(Hash.ToArray()),

// BlockBase properties
new Integer(Version),
new ByteArray(PrevHash.ToArray()),
new ByteArray(MerkleRoot.ToArray()),
new Integer(Timestamp),
new Integer(Index),
new ByteArray(NextConsensus.ToArray()),
// Witness

// Block properties
// Count
// ConsensusData
new Integer(Transactions.Length)
}
);
return new Array(referenceCounter, new StackItem[]
{
// Computed properties
Hash.ToArray(),

// BlockBase properties
Version,
PrevHash.ToArray(),
MerkleRoot.ToArray(),
Timestamp,
Index,
NextConsensus.ToArray(),

// Block properties
Transactions.Length
});
}
}
}
38 changes: 17 additions & 21 deletions src/neo/Network/P2P/Payloads/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.VM.Types;
using Neo.Wallets;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using Array = Neo.VM.Types.Array;

namespace Neo.Network.P2P.Payloads
{
Expand Down Expand Up @@ -300,28 +302,22 @@ public RelayResultReason VerifyParallelParts(StoreView snapshot)
return RelayResultReason.Succeed;
}

public StackItem ToStackItem()
public StackItem ToStackItem(ReferenceCounter referenceCounter)
{
return new VM.Types.Array
(
new StackItem[]
{
// Computed properties
new ByteArray(Hash.ToArray()),

// Transaction properties
new Integer(Version),
new Integer(Nonce),
new ByteArray(Sender.ToArray()),
new Integer(SystemFee),
new Integer(NetworkFee),
new Integer(ValidUntilBlock),
// Attributes
// Cosigners
new ByteArray(Script),
// Witnesses
}
);
return new Array(referenceCounter, new StackItem[]
{
// Computed properties
Hash.ToArray(),

// Transaction properties
(int)Version,
Nonce,
Sender.ToArray(),
SystemFee,
NetworkFee,
ValidUntilBlock,
Script,
});
}
}
}
3 changes: 2 additions & 1 deletion src/neo/SmartContract/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Buffers.Binary;
using System.Text;
Expand Down Expand Up @@ -118,7 +119,7 @@ internal static bool VerifyWitnesses(this IVerifiable verifiable, StoreView snap
engine.LoadScript(verification);
engine.LoadScript(verifiable.Witnesses[i].InvocationScript);
if (engine.Execute() == VMState.FAULT) return false;
if (!engine.ResultStack.TryPop(out var result) || !result.ToBoolean()) return false;
if (!engine.ResultStack.TryPop(out StackItem result) || !result.ToBoolean()) return false;
}
}
return true;
Expand Down
3 changes: 2 additions & 1 deletion src/neo/SmartContract/IInteroperable.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Neo.VM;
using Neo.VM.Types;

namespace Neo.SmartContract
{
public interface IInteroperable
{
StackItem ToStackItem();
StackItem ToStackItem(ReferenceCounter referenceCounter);
}
}
7 changes: 3 additions & 4 deletions src/neo/SmartContract/InteropDescriptor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Neo.VM;
using Neo.VM.Types;
using System;

namespace Neo.SmartContract
Expand All @@ -10,7 +9,7 @@ internal class InteropDescriptor
public uint Hash { get; }
public Func<ApplicationEngine, bool> Handler { get; }
public long Price { get; }
public Func<RandomAccessStack<StackItem>, long> PriceCalculator { get; }
public Func<EvaluationStack, long> PriceCalculator { get; }
public TriggerType AllowedTriggers { get; }

public InteropDescriptor(string method, Func<ApplicationEngine, bool> handler, long price, TriggerType allowedTriggers)
Expand All @@ -19,7 +18,7 @@ public InteropDescriptor(string method, Func<ApplicationEngine, bool> handler, l
this.Price = price;
}

public InteropDescriptor(string method, Func<ApplicationEngine, bool> handler, Func<RandomAccessStack<StackItem>, long> priceCalculator, TriggerType allowedTriggers)
public InteropDescriptor(string method, Func<ApplicationEngine, bool> handler, Func<EvaluationStack, long> priceCalculator, TriggerType allowedTriggers)
: this(method, handler, allowedTriggers)
{
this.PriceCalculator = priceCalculator;
Expand All @@ -33,7 +32,7 @@ private InteropDescriptor(string method, Func<ApplicationEngine, bool> handler,
this.AllowedTriggers = allowedTriggers;
}

public long GetPrice(RandomAccessStack<StackItem> stack)
public long GetPrice(EvaluationStack stack)
{
return PriceCalculator is null ? Price : PriceCalculator(stack);
}
Expand Down
6 changes: 3 additions & 3 deletions src/neo/SmartContract/InteropService.NEO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static InteropService()
Register(contract.ServiceName, contract.Invoke, contract.GetPrice, TriggerType.System | TriggerType.Application);
}

private static long GetECDsaCheckMultiSigPrice(RandomAccessStack<StackItem> stack)
private static long GetECDsaCheckMultiSigPrice(EvaluationStack stack)
{
if (stack.Count < 2) return 0;
var item = stack.Peek(1);
Expand All @@ -54,7 +54,7 @@ private static long GetECDsaCheckMultiSigPrice(RandomAccessStack<StackItem> stac
return GetPrice(Neo_Crypto_ECDsaVerify, stack) * n;
}

private static long GetDeploymentPrice(RandomAccessStack<StackItem> stack)
private static long GetDeploymentPrice(EvaluationStack stack)
{
int size = stack.Peek(0).GetByteLength() + stack.Peek(1).GetByteLength();
return GasPerByte * size;
Expand Down Expand Up @@ -378,7 +378,7 @@ private static bool Json_Deserialize(ApplicationEngine engine)
{
var json = engine.CurrentContext.EvaluationStack.Pop().GetString();
var obj = JObject.Parse(json, 10);
var item = JsonSerializer.Deserialize(obj);
var item = JsonSerializer.Deserialize(obj, engine.ReferenceCounter);

engine.CurrentContext.EvaluationStack.Push(item);
return true;
Expand Down
22 changes: 11 additions & 11 deletions src/neo/SmartContract/InteropService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private static bool CheckStorageContext(ApplicationEngine engine, StorageContext
return true;
}

public static long GetPrice(uint hash, RandomAccessStack<StackItem> stack)
public static long GetPrice(uint hash, EvaluationStack stack)
{
return methods[hash].GetPrice(stack);
}
Expand All @@ -122,7 +122,7 @@ public static Dictionary<uint, string> SupportedMethods()
return methods.ToDictionary(p => p.Key, p => p.Value.Method);
}

private static long GetStoragePrice(RandomAccessStack<StackItem> stack)
private static long GetStoragePrice(EvaluationStack stack)
{
return (stack.Peek(1).GetByteLength() + stack.Peek(2).GetByteLength()) * GasPerByte;
}
Expand All @@ -143,7 +143,7 @@ private static uint Register(string method, Func<ApplicationEngine, bool> handle
return descriptor.Hash;
}

private static uint Register(string method, Func<ApplicationEngine, bool> handler, Func<RandomAccessStack<StackItem>, long> priceCalculator, TriggerType allowedTriggers)
private static uint Register(string method, Func<ApplicationEngine, bool> handler, Func<EvaluationStack, long> priceCalculator, TriggerType allowedTriggers)
{
InteropDescriptor descriptor = new InteropDescriptor(method, handler, priceCalculator, allowedTriggers);
methods.Add(descriptor.Hash, descriptor);
Expand All @@ -153,7 +153,7 @@ private static uint Register(string method, Func<ApplicationEngine, bool> handle
private static bool ExecutionEngine_GetScriptContainer(ApplicationEngine engine)
{
engine.CurrentContext.EvaluationStack.Push(
engine.ScriptContainer is IInteroperable value ? value.ToStackItem() :
engine.ScriptContainer is IInteroperable value ? value.ToStackItem(engine.ReferenceCounter) :
StackItem.FromInterface(engine.ScriptContainer));
return true;
}
Expand Down Expand Up @@ -291,8 +291,8 @@ private static bool Runtime_GetNotifications(ApplicationEngine engine)
notifications = notifications.Where(p => p.ScriptHash == hash);
}

if (!engine.CheckArraySize(notifications.Count())) return false;
engine.CurrentContext.EvaluationStack.Push(notifications.Select(u => new VM.Types.Array(new StackItem[] { u.ScriptHash.ToArray(), u.State })).ToArray());
if (notifications.Count() > engine.MaxStackSize) return false;
engine.Push(new Array(engine.ReferenceCounter, notifications.Select(u => new Array(engine.ReferenceCounter, new[] { u.ScriptHash.ToArray(), u.State }))));
return true;
}

Expand All @@ -312,7 +312,7 @@ private static bool Runtime_Deserialize(ApplicationEngine engine)
StackItem item;
try
{
item = StackItemSerializer.Deserialize(engine.CurrentContext.EvaluationStack.Pop().GetSpan(), engine.MaxArraySize, engine.MaxItemSize);
item = StackItemSerializer.Deserialize(engine.CurrentContext.EvaluationStack.Pop().GetSpan(), engine.MaxItemSize, engine.ReferenceCounter);
}
catch (FormatException)
{
Expand Down Expand Up @@ -347,7 +347,7 @@ private static bool Blockchain_GetBlock(ApplicationEngine engine)
if (block == null)
engine.CurrentContext.EvaluationStack.Push(StackItem.Null);
else
engine.CurrentContext.EvaluationStack.Push(block.ToStackItem());
engine.CurrentContext.EvaluationStack.Push(block.ToStackItem(engine.ReferenceCounter));
return true;
}

Expand All @@ -358,7 +358,7 @@ private static bool Blockchain_GetTransaction(ApplicationEngine engine)
if (tx == null)
engine.CurrentContext.EvaluationStack.Push(StackItem.Null);
else
engine.CurrentContext.EvaluationStack.Push(tx.ToStackItem());
engine.CurrentContext.EvaluationStack.Push(tx.ToStackItem(engine.ReferenceCounter));
return true;
}

Expand Down Expand Up @@ -395,7 +395,7 @@ private static bool Blockchain_GetTransactionFromBlock(ApplicationEngine engine)
if (tx == null)
engine.CurrentContext.EvaluationStack.Push(StackItem.Null);
else
engine.CurrentContext.EvaluationStack.Push(tx.ToStackItem());
engine.CurrentContext.EvaluationStack.Push(tx.ToStackItem(engine.ReferenceCounter));
}
return true;
}
Expand All @@ -407,7 +407,7 @@ private static bool Blockchain_GetContract(ApplicationEngine engine)
if (contract == null)
engine.CurrentContext.EvaluationStack.Push(StackItem.Null);
else
engine.CurrentContext.EvaluationStack.Push(contract.ToStackItem());
engine.CurrentContext.EvaluationStack.Push(contract.ToStackItem(engine.ReferenceCounter));
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/neo/SmartContract/Iterators/ArrayWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ namespace Neo.SmartContract.Iterators
{
internal class ArrayWrapper : IIterator
{
private readonly IList<StackItem> array;
private readonly IReadOnlyList<StackItem> array;
private int index = -1;

public ArrayWrapper(IList<StackItem> array)
public ArrayWrapper(IReadOnlyList<StackItem> array)
{
this.array = array;
}
Expand Down
Loading

0 comments on commit 5cba80a

Please sign in to comment.