Skip to content

Commit

Permalink
Update vm (#2225)
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon authored Jan 15, 2021
1 parent df79b42 commit 56bc2b1
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 28 deletions.
9 changes: 4 additions & 5 deletions src/neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private ExecutionContext CallContractInternal(ContractState contract, ContractMe

if (args.Count != method.Parameters.Length) throw new InvalidOperationException($"Method {method} Expects {method.Parameters.Length} Arguments But Receives {args.Count} Arguments");
if (hasReturnValue ^ (method.ReturnType != ContractParameterType.Void)) throw new InvalidOperationException("The return value type does not match.");
ExecutionContext context_new = LoadContract(contract, method.Name, flags & callingFlags, hasReturnValue, (ushort)args.Count);
ExecutionContext context_new = LoadContract(contract, method.Name, flags & callingFlags, hasReturnValue);
state = context_new.GetState<ExecutionContextState>();
state.CallingScriptHash = callingScriptHash;

Expand Down Expand Up @@ -163,13 +163,12 @@ protected override void LoadContext(ExecutionContext context)
base.LoadContext(context);
}

public ExecutionContext LoadContract(ContractState contract, string method, CallFlags callFlags, bool hasReturnValue, ushort pcount)
public ExecutionContext LoadContract(ContractState contract, string method, CallFlags callFlags, bool hasReturnValue)
{
ContractMethodDescriptor md = contract.Manifest.Abi.GetMethod(method);
if (md is null) return null;

ExecutionContext context = LoadScript(contract.Script,
pcount: pcount,
rvcount: hasReturnValue ? 1 : 0,
initialPosition: md.Offset,
configureState: p =>
Expand All @@ -189,10 +188,10 @@ public ExecutionContext LoadContract(ContractState contract, string method, Call
return context;
}

public ExecutionContext LoadScript(Script script, ushort pcount = 0, int rvcount = -1, int initialPosition = 0, Action<ExecutionContextState> configureState = null)
public ExecutionContext LoadScript(Script script, int rvcount = -1, int initialPosition = 0, Action<ExecutionContextState> configureState = null)
{
// Create and configure context
ExecutionContext context = CreateContext(script, pcount, rvcount, initialPosition);
ExecutionContext context = CreateContext(script, rvcount, initialPosition);
configureState?.Invoke(context.GetState<ExecutionContextState>());
// Load context
LoadContext(context);
Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ internal static bool VerifyWitness(this IVerifiable verifiable, StoreView snapsh
{
ContractState cs = NativeContract.ContractManagement.GetContract(snapshot, hash);
if (cs is null) return false;
if (engine.LoadContract(cs, "verify", callFlags, true, 0) is null)
if (engine.LoadContract(cs, "verify", callFlags, true) is null)
return false;
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/neo/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public long CalculateNetworkFee(StoreView snapshot, Transaction tx)

// Check verify cost
using ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, tx, snapshot.Clone());
if (engine.LoadContract(contract, "verify", CallFlags.None, true, 0) is null)
if (engine.LoadContract(contract, "verify", CallFlags.None, true) is null)
throw new ArgumentException($"The smart contract {contract.Hash} haven't got verify method");
if (NativeContract.IsNative(hash)) engine.Push("verify");
if (engine.Execute() == VMState.FAULT) throw new ArgumentException($"Smart contract {contract.Hash} verification fault.");
Expand Down
2 changes: 1 addition & 1 deletion src/neo/neo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<PackageReference Include="BouncyCastle.NetCore" Version="1.8.8" />
<PackageReference Include="K4os.Compression.LZ4" Version="1.2.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.1" />
<PackageReference Include="Neo.VM" Version="3.0.0-CI00259" />
<PackageReference Include="Neo.VM" Version="3.0.0-CI00260" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion tests/neo.UnitTests/Extensions/NativeContractExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static StackItem Call(this NativeContract contract, StoreView snapshot, I
script.EmitPush(args[i]);

script.EmitPush(method);
engine.LoadContract(contractState, method, CallFlags.All, contract.Manifest.Abi.GetMethod(method).ReturnType != ContractParameterType.Void, (ushort)args.Length);
engine.LoadContract(contractState, method, CallFlags.All, contract.Manifest.Abi.GetMethod(method).ReturnType != ContractParameterType.Void);
engine.LoadScript(script.ToArray());

if (engine.Execute() != VMState.HALT)
Expand Down
10 changes: 5 additions & 5 deletions tests/neo.UnitTests/Extensions/Nep17NativeContractExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static bool Transfer(this NativeContract contract, StoreView snapshot, by
var engine = ApplicationEngine.Create(TriggerType.Application,
new ManualWitness(signFrom ? new UInt160(from) : null), snapshot, persistingBlock);

engine.LoadScript(contract.Script, pcount: 4, configureState: p => p.ScriptHash = contract.Hash);
engine.LoadScript(contract.Script, configureState: p => p.ScriptHash = contract.Hash);

var script = new ScriptBuilder();
script.Emit(OpCode.PUSHNULL);
Expand All @@ -65,7 +65,7 @@ public static BigInteger TotalSupply(this NativeContract contract, StoreView sna
{
var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);

engine.LoadScript(contract.Script, pcount: 0, configureState: p => p.ScriptHash = contract.Hash);
engine.LoadScript(contract.Script, configureState: p => p.ScriptHash = contract.Hash);

var script = new ScriptBuilder();
script.EmitPush("totalSupply");
Expand All @@ -83,7 +83,7 @@ public static BigInteger BalanceOf(this NativeContract contract, StoreView snaps
{
var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);

engine.LoadScript(contract.Script, pcount: 1, configureState: p => p.ScriptHash = contract.Hash);
engine.LoadScript(contract.Script, configureState: p => p.ScriptHash = contract.Hash);

var script = new ScriptBuilder();
script.EmitPush(account);
Expand All @@ -102,7 +102,7 @@ public static BigInteger Decimals(this NativeContract contract, StoreView snapsh
{
var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);

engine.LoadScript(contract.Script, pcount: 0, configureState: p => p.ScriptHash = contract.Hash);
engine.LoadScript(contract.Script, configureState: p => p.ScriptHash = contract.Hash);

var script = new ScriptBuilder();
script.EmitPush("decimals");
Expand All @@ -120,7 +120,7 @@ public static string Symbol(this NativeContract contract, StoreView snapshot)
{
var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);

engine.LoadScript(contract.Script, pcount: 0, configureState: p => p.ScriptHash = contract.Hash);
engine.LoadScript(contract.Script, configureState: p => p.ScriptHash = contract.Hash);

var script = new ScriptBuilder();
script.EmitPush("symbol");
Expand Down
14 changes: 7 additions & 7 deletions tests/neo.UnitTests/SmartContract/Native/UT_NameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ internal static bool Check_DeleteRecord(StoreView snapshot, string name, RecordT
new ContractParameter(ContractParameterType.String) { Value = name },
new ContractParameter(ContractParameterType.Integer) { Value = (int)type }
});
engine.LoadScript(script.ToArray(), 0, -1, 0);
engine.LoadScript(script.ToArray());

if (engine.Execute() == VMState.FAULT)
{
Expand All @@ -185,7 +185,7 @@ internal static bool Check_SetRecord(StoreView snapshot, string name, RecordType
new ContractParameter(ContractParameterType.Integer) { Value = (int)type },
new ContractParameter(ContractParameterType.String) { Value = data }
});
engine.LoadScript(script.ToArray(), 0, -1, 0);
engine.LoadScript(script.ToArray());

if (engine.Execute() == VMState.FAULT)
{
Expand All @@ -202,7 +202,7 @@ internal static BigInteger Check_Renew(StoreView snapshot, string name, UInt160
script.EmitDynamicCall(NativeContract.NameService.Hash, "renew", new ContractParameter[] {
new ContractParameter(ContractParameterType.String) { Value = name }
});
engine.LoadScript(script.ToArray(), 0, -1, 0);
engine.LoadScript(script.ToArray());

if (engine.Execute() == VMState.FAULT)
{
Expand All @@ -223,7 +223,7 @@ internal static bool Check_SetAdmin(StoreView snapshot, string name, UInt160 adm
new ContractParameter(ContractParameterType.String) { Value = name },
new ContractParameter(ContractParameterType.Hash160) { Value = admin }
});
engine.LoadScript(script.ToArray(), 0, -1, 0);
engine.LoadScript(script.ToArray());

if (engine.Execute() == VMState.FAULT)
{
Expand All @@ -241,7 +241,7 @@ internal static bool Check_Register(StoreView snapshot, string name, UInt160 own
new ContractParameter(ContractParameterType.String) { Value = name },
new ContractParameter(ContractParameterType.Hash160) { Value = owner }
});
engine.LoadScript(script.ToArray(), 0, -1, 0);
engine.LoadScript(script.ToArray());

if (engine.Execute() == VMState.FAULT)
{
Expand All @@ -259,7 +259,7 @@ internal static bool Check_SetPrice(StoreView snapshot, UInt160 signedBy, long p
using var engine = ApplicationEngine.Create(TriggerType.Application, new Nep17NativeContractExtensions.ManualWitness(signedBy), snapshot, persistingBlock);
using var script = new ScriptBuilder();
script.EmitDynamicCall(NativeContract.NameService.Hash, "setPrice", new ContractParameter[] { new ContractParameter(ContractParameterType.Integer) { Value = price } });
engine.LoadScript(script.ToArray(), 0, -1, 0);
engine.LoadScript(script.ToArray());

if (engine.Execute() == VMState.FAULT)
{
Expand All @@ -274,7 +274,7 @@ internal static bool Check_AddRoot(StoreView snapshot, UInt160 signedBy, string
using var engine = ApplicationEngine.Create(TriggerType.Application, new Nep17NativeContractExtensions.ManualWitness(signedBy), snapshot, persistingBlock);
using var script = new ScriptBuilder();
script.EmitDynamicCall(NativeContract.NameService.Hash, "addRoot", new ContractParameter[] { new ContractParameter(ContractParameterType.String) { Value = root } });
engine.LoadScript(script.ToArray(), 0, -1, 0);
engine.LoadScript(script.ToArray());

if (engine.Execute() == VMState.FAULT)
{
Expand Down
14 changes: 7 additions & 7 deletions tests/neo.UnitTests/SmartContract/Native/UT_NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ internal static bool Check_PostPersist(StoreView snapshot, Block persistingBlock
internal static (BigInteger Value, bool State) Check_GetGasPerBlock(StoreView snapshot, Block persistingBlock)
{
using var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, persistingBlock);
engine.LoadScript(NativeContract.NEO.Script, pcount: 0, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);
engine.LoadScript(NativeContract.NEO.Script, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);

using var script = new ScriptBuilder();
script.EmitPush("getGasPerBlock");
Expand All @@ -890,7 +890,7 @@ internal static (VM.Types.Boolean Value, bool State) Check_SetGasPerBlock(StoreV
UInt160 committeeMultiSigAddr = NativeContract.NEO.GetCommitteeAddress(snapshot);
using var engine = ApplicationEngine.Create(TriggerType.Application, new Nep17NativeContractExtensions.ManualWitness(committeeMultiSigAddr), snapshot, persistingBlock);

engine.LoadScript(NativeContract.NEO.Script, pcount: 1, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);
engine.LoadScript(NativeContract.NEO.Script, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);

var script = new ScriptBuilder();
script.EmitPush(gasPerBlock);
Expand All @@ -913,7 +913,7 @@ internal static (bool State, bool Result) Check_Vote(StoreView snapshot, byte[]
using var engine = ApplicationEngine.Create(TriggerType.Application,
new Nep17NativeContractExtensions.ManualWitness(signAccount ? new UInt160(account) : UInt160.Zero), snapshot, persistingBlock);

engine.LoadScript(NativeContract.NEO.Script, pcount: 2, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);
engine.LoadScript(NativeContract.NEO.Script, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);

using var script = new ScriptBuilder();

Expand Down Expand Up @@ -941,7 +941,7 @@ internal static (bool State, bool Result) Check_RegisterValidator(StoreView snap
using var engine = ApplicationEngine.Create(TriggerType.Application,
new Nep17NativeContractExtensions.ManualWitness(Contract.CreateSignatureRedeemScript(ECPoint.DecodePoint(pubkey, ECCurve.Secp256r1)).ToScriptHash()), snapshot, persistingBlock);

engine.LoadScript(NativeContract.NEO.Script, pcount: 1, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);
engine.LoadScript(NativeContract.NEO.Script, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);

using var script = new ScriptBuilder();
script.EmitPush(pubkey);
Expand All @@ -963,7 +963,7 @@ internal static ECPoint[] Check_GetCommittee(StoreView snapshot, Block persistin
{
using var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, persistingBlock);

engine.LoadScript(NativeContract.NEO.Script, pcount: 0, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);
engine.LoadScript(NativeContract.NEO.Script, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);

using var script = new ScriptBuilder();
script.EmitPush("getCommittee");
Expand All @@ -981,7 +981,7 @@ internal static (BigInteger Value, bool State) Check_UnclaimedGas(StoreView snap
{
using var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, persistingBlock);

engine.LoadScript(NativeContract.NEO.Script, pcount: 2, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);
engine.LoadScript(NativeContract.NEO.Script, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);

using var script = new ScriptBuilder();
script.EmitPush(persistingBlock.Index);
Expand Down Expand Up @@ -1041,7 +1041,7 @@ internal static (bool State, bool Result) Check_UnregisterCandidate(StoreView sn
using var engine = ApplicationEngine.Create(TriggerType.Application,
new Nep17NativeContractExtensions.ManualWitness(Contract.CreateSignatureRedeemScript(ECPoint.DecodePoint(pubkey, ECCurve.Secp256r1)).ToScriptHash()), snapshot, persistingBlock);

engine.LoadScript(NativeContract.NEO.Script, pcount: 1, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);
engine.LoadScript(NativeContract.NEO.Script, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);

using var script = new ScriptBuilder();
script.EmitPush(pubkey);
Expand Down

0 comments on commit 56bc2b1

Please sign in to comment.