Skip to content

Commit

Permalink
Neo.VM.3.0.0-CI00230 (#1725)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored Jun 23, 2020
1 parent 815180c commit cd988e8
Show file tree
Hide file tree
Showing 26 changed files with 143 additions and 231 deletions.
2 changes: 1 addition & 1 deletion src/neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ internal object Convert(StackItem item, InteropParameterDescriptor descriptor)
}
else
{
int count = (int)item.GetBigInteger();
int count = (int)item.GetInteger();
if (count > MaxStackSize) throw new InvalidOperationException();
av = Array.CreateInstance(descriptor.Type.GetElementType(), count);
for (int i = 0; i < av.Length; i++)
Expand Down
32 changes: 23 additions & 9 deletions src/neo/SmartContract/BinarySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ namespace Neo.SmartContract
{
internal static class BinarySerializer
{
private class ContainerPlaceholder : StackItem
{
public override StackItemType Type { get; }
public int ElementCount { get; }

public ContainerPlaceholder(StackItemType type, int count)
{
Type = type;
ElementCount = count;
}

public override bool Equals(StackItem other) => throw new NotSupportedException();

public override int GetHashCode() => throw new NotSupportedException();

public override bool GetBoolean() => throw new NotSupportedException();
}

public static StackItem Deserialize(byte[] data, uint maxArraySize, uint maxItemSize, ReferenceCounter referenceCounter = null)
{
using MemoryStream ms = new MemoryStream(data, false);
Expand Down Expand Up @@ -137,16 +155,12 @@ private static void Serialize(StackItem item, BinaryWriter writer, uint maxSize)
case Null _:
break;
case Boolean _:
writer.Write(item.ToBoolean());
break;
case Integer integer:
writer.WriteVarBytes(integer.Span);
break;
case ByteString bytes:
writer.WriteVarBytes(bytes.Span);
writer.Write(item.GetBoolean());
break;
case Buffer buffer:
writer.WriteVarBytes(buffer.InnerBuffer);
case Integer _:
case ByteString _:
case Buffer _:
writer.WriteVarBytes(item.GetSpan());
break;
case Array array:
if (serialized.Any(p => ReferenceEquals(p, array)))
Expand Down
23 changes: 0 additions & 23 deletions src/neo/SmartContract/ContainerPlaceholder.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/neo/SmartContract/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ internal static bool VerifyWitnesses(this IVerifiable verifiable, StoreView snap
engine.LoadScript(verification, CallFlags.ReadOnly).InstructionPointer = offset;
engine.LoadScript(verifiable.Witnesses[i].InvocationScript, CallFlags.None);
if (engine.Execute() == VMState.FAULT) return false;
if (engine.ResultStack.Count != 1 || !engine.ResultStack.Pop().ToBoolean()) return false;
if (engine.ResultStack.Count != 1 || !engine.ResultStack.Pop().GetBoolean()) return false;
gas -= engine.GasConsumed;
}
}
Expand Down
21 changes: 10 additions & 11 deletions src/neo/SmartContract/InteropParameterDescriptor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Neo.Cryptography.ECC;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Collections.Generic;
Expand All @@ -23,16 +22,16 @@ internal class InteropParameterDescriptor
[typeof(VM.Types.Pointer)] = p => p,
[typeof(VM.Types.Array)] = p => p,
[typeof(InteropInterface)] = p => p,
[typeof(bool)] = p => p.ToBoolean(),
[typeof(sbyte)] = p => (sbyte)p.GetBigInteger(),
[typeof(byte)] = p => (byte)p.GetBigInteger(),
[typeof(short)] = p => (short)p.GetBigInteger(),
[typeof(ushort)] = p => (ushort)p.GetBigInteger(),
[typeof(int)] = p => (int)p.GetBigInteger(),
[typeof(uint)] = p => (uint)p.GetBigInteger(),
[typeof(long)] = p => (long)p.GetBigInteger(),
[typeof(ulong)] = p => (ulong)p.GetBigInteger(),
[typeof(BigInteger)] = p => p.GetBigInteger(),
[typeof(bool)] = p => p.GetBoolean(),
[typeof(sbyte)] = p => (sbyte)p.GetInteger(),
[typeof(byte)] = p => (byte)p.GetInteger(),
[typeof(short)] = p => (short)p.GetInteger(),
[typeof(ushort)] = p => (ushort)p.GetInteger(),
[typeof(int)] = p => (int)p.GetInteger(),
[typeof(uint)] = p => (uint)p.GetInteger(),
[typeof(long)] = p => (long)p.GetInteger(),
[typeof(ulong)] = p => (ulong)p.GetInteger(),
[typeof(BigInteger)] = p => p.GetInteger(),
[typeof(byte[])] = p => p.IsNull ? null : p.GetSpan().ToArray(),
[typeof(string)] = p => p.IsNull ? null : p.GetString(),
[typeof(UInt160)] = p => p.IsNull ? null : new UInt160(p.GetSpan()),
Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/Iterators/ByteArrayWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal class ByteArrayWrapper : IIterator

public ByteArrayWrapper(PrimitiveType value)
{
this.array = value.Span.ToArray();
this.array = value.GetSpan().ToArray();
}

public void Dispose() { }
Expand Down
14 changes: 7 additions & 7 deletions src/neo/SmartContract/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public static JObject Serialize(StackItem item)
}
case Integer num:
{
var integer = num.GetBigInteger();
var integer = num.GetInteger();
if (integer > JNumber.MAX_SAFE_INTEGER || integer < JNumber.MIN_SAFE_INTEGER)
return integer.ToString();
return (double)num.GetBigInteger();
throw new InvalidOperationException();
return (double)integer;
}
case Boolean boolean:
{
return boolean.ToBoolean();
return boolean.GetBoolean();
}
case Map map:
{
Expand Down Expand Up @@ -96,14 +96,14 @@ public static byte[] SerializeToByteArray(StackItem item, uint maxSize)
break;
case Integer num:
{
var integer = num.GetBigInteger();
var integer = num.GetInteger();
if (integer > JNumber.MAX_SAFE_INTEGER || integer < JNumber.MIN_SAFE_INTEGER)
throw new InvalidOperationException();
writer.WriteNumberValue((double)num.GetBigInteger());
writer.WriteNumberValue((double)integer);
break;
}
case Boolean boolean:
writer.WriteBooleanValue(boolean.ToBoolean());
writer.WriteBooleanValue(boolean.GetBoolean());
break;
case Map map:
writer.WriteStartObject();
Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/Native/NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ internal void Invoke(ApplicationEngine engine)
{
if (!engine.CurrentScriptHash.Equals(Hash))
throw new InvalidOperationException("It is not allowed to use Neo.Native.Call directly to call native contracts. System.Contract.Call should be used.");
string operation = engine.PopString();
string operation = engine.Pop().GetString();
Array args = engine.Pop<Array>();
ContractMethodMetadata method = methods[operation];
ExecutionContextState state = engine.CurrentContext.GetState<ExecutionContextState>();
Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/Native/Tokens/AccountState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class AccountState : IInteroperable

public virtual void FromStackItem(StackItem stackItem)
{
Balance = ((Struct)stackItem)[0].GetBigInteger();
Balance = ((Struct)stackItem)[0].GetInteger();
}

public virtual StackItem ToStackItem(ReferenceCounter referenceCounter)
Expand Down
6 changes: 3 additions & 3 deletions src/neo/SmartContract/Native/Tokens/NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public override void FromStackItem(StackItem stackItem)
{
base.FromStackItem(stackItem);
Struct @struct = (Struct)stackItem;
BalanceHeight = (uint)@struct[1].GetBigInteger();
BalanceHeight = (uint)@struct[1].GetInteger();
VoteTo = @struct[2].IsNull ? null : @struct[2].GetSpan().AsSerializable<ECPoint>();
}

Expand All @@ -263,8 +263,8 @@ internal class CandidateState : IInteroperable
public void FromStackItem(StackItem stackItem)
{
Struct @struct = (Struct)stackItem;
Registered = @struct[0].ToBoolean();
Votes = @struct[1].GetBigInteger();
Registered = @struct[0].GetBoolean();
Votes = @struct[1].GetInteger();
}

public StackItem ToStackItem(ReferenceCounter referenceCounter)
Expand Down
52 changes: 8 additions & 44 deletions src/neo/VM/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using Array = Neo.VM.Types.Array;
using Boolean = Neo.VM.Types.Boolean;
using Buffer = Neo.VM.Types.Buffer;
Expand Down Expand Up @@ -168,39 +167,6 @@ public static ScriptBuilder EmitSysCall(this ScriptBuilder sb, uint method, para
return sb.EmitSysCall(method);
}

public static BigInteger GetBigInteger(this StackItem item)
{
if (!(item is PrimitiveType primitive))
throw new ArgumentException();
return primitive.ToBigInteger();
}

public static int GetByteLength(this StackItem item)
{
return item switch
{
PrimitiveType p => p.Size,
Buffer b => b.Size,
Null _ => 0,
_ => throw new ArgumentException(),
};
}

public static ReadOnlySpan<byte> GetSpan(this StackItem item)
{
return item switch
{
PrimitiveType p => p.Span,
Buffer b => b.InnerBuffer,
_ => throw new ArgumentException(),
};
}

public static string GetString(this StackItem item)
{
return Utility.StrictUTF8.GetString(item.GetSpan());
}

/// <summary>
/// Generate scripts to call a specific method from a specific contract.
/// </summary>
Expand Down Expand Up @@ -237,16 +203,14 @@ private static JObject ToJson(StackItem item, HashSet<StackItem> context)
json["value"] = new JArray(array.Select(p => ToJson(p, context)));
break;
case Boolean boolean:
json["value"] = boolean.ToBoolean();
break;
case Buffer buffer:
json["value"] = Convert.ToBase64String(buffer.InnerBuffer);
json["value"] = boolean.GetBoolean();
break;
case ByteString byteString:
json["value"] = Convert.ToBase64String(byteString.Span);
case Buffer _:
case ByteString _:
json["value"] = Convert.ToBase64String(item.GetSpan());
break;
case Integer integer:
json["value"] = integer.ToBigInteger().ToString();
json["value"] = integer.GetInteger().ToString();
break;
case Map map:
context ??= new HashSet<StackItem>(ReferenceEqualityComparer.Default);
Expand Down Expand Up @@ -305,21 +269,21 @@ private static ContractParameter ToParameter(StackItem item, List<(StackItem, Co
parameter = new ContractParameter
{
Type = ContractParameterType.Boolean,
Value = item.ToBoolean()
Value = item.GetBoolean()
};
break;
case ByteString array:
parameter = new ContractParameter
{
Type = ContractParameterType.ByteArray,
Value = array.Span.ToArray()
Value = array.GetSpan().ToArray()
};
break;
case Integer i:
parameter = new ContractParameter
{
Type = ContractParameterType.Integer,
Value = i.ToBigInteger()
Value = i.GetInteger()
};
break;
case InteropInterface _:
Expand Down
2 changes: 1 addition & 1 deletion src/neo/Wallets/AssetDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public AssetDescriptor(UInt160 asset_id)
if (engine.State.HasFlag(VMState.FAULT)) throw new ArgumentException();
this.AssetId = asset_id;
this.AssetName = engine.ResultStack.Pop().GetString();
this.Decimals = (byte)engine.ResultStack.Pop().GetBigInteger();
this.Decimals = (byte)engine.ResultStack.Pop().GetInteger();
}

public override string ToString()
Expand Down
6 changes: 3 additions & 3 deletions src/neo/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ public BigDecimal GetBalance(UInt160 asset_id, params UInt160[] accounts)
using ApplicationEngine engine = ApplicationEngine.Run(script, extraGAS: 20000000L * accounts.Length);
if (engine.State.HasFlag(VMState.FAULT))
return new BigDecimal(0, 0);
byte decimals = (byte)engine.ResultStack.Pop().GetBigInteger();
BigInteger amount = engine.ResultStack.Pop().GetBigInteger();
byte decimals = (byte)engine.ResultStack.Pop().GetInteger();
BigInteger amount = engine.ResultStack.Pop().GetInteger();
return new BigDecimal(amount, decimals);
}

Expand Down Expand Up @@ -251,7 +251,7 @@ public Transaction MakeTransaction(TransferOutput[] outputs, UInt160 from = null
{
if (engine.State.HasFlag(VMState.FAULT))
throw new InvalidOperationException($"Execution for {assetId.ToString()}.balanceOf('{account.ToString()}' fault");
BigInteger value = engine.ResultStack.Pop().GetBigInteger();
BigInteger value = engine.ResultStack.Pop().GetInteger();
if (value.Sign > 0) balances.Add((account, value));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/neo/neo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<PackageReference Include="Microsoft.AspNetCore.WebSockets" Version="2.2.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.3" />
<PackageReference Include="Neo.VM" Version="3.0.0-CI00227" />
<PackageReference Include="Neo.VM" Version="3.0.0-CI00230" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static bool Transfer(this NativeContract contract, StoreView snapshot, by
var result = engine.ResultStack.Pop();
result.Should().BeOfType(typeof(VM.Types.Boolean));

return result.ToBoolean();
return result.GetBoolean();
}

public static string[] SupportedStandards(this NativeContract contract)
Expand Down Expand Up @@ -107,7 +107,7 @@ public static BigInteger TotalSupply(this NativeContract contract, StoreView sna
var result = engine.ResultStack.Pop();
result.Should().BeOfType(typeof(VM.Types.Integer));

return (result as VM.Types.Integer).GetBigInteger();
return result.GetInteger();
}

public static BigInteger BalanceOf(this NativeContract contract, StoreView snapshot, byte[] account)
Expand All @@ -128,7 +128,7 @@ public static BigInteger BalanceOf(this NativeContract contract, StoreView snaps
var result = engine.ResultStack.Pop();
result.Should().BeOfType(typeof(VM.Types.Integer));

return (result as VM.Types.Integer).GetBigInteger();
return result.GetInteger();
}

public static BigInteger Decimals(this NativeContract contract)
Expand All @@ -148,7 +148,7 @@ public static BigInteger Decimals(this NativeContract contract)
var result = engine.ResultStack.Pop();
result.Should().BeOfType(typeof(VM.Types.Integer));

return (result as VM.Types.Integer).GetBigInteger();
return result.GetInteger();
}

public static string Symbol(this NativeContract contract)
Expand Down
Loading

0 comments on commit cd988e8

Please sign in to comment.