From 9d996e064ebb27e9ae64b4983c3b1f730fa4c915 Mon Sep 17 00:00:00 2001 From: Harry Pierson Date: Sat, 11 Jul 2020 09:25:03 -0700 Subject: [PATCH] IApplicationEngineProvider (#1758) --- src/neo/Ledger/Blockchain.cs | 4 +- src/neo/Plugins/IApplicationEngineProvider.cs | 11 +++ ...{IStoragePlugin.cs => IStorageProvider.cs} | 2 +- src/neo/Plugins/Plugin.cs | 6 +- src/neo/SmartContract/ApplicationEngine.cs | 21 ++++- src/neo/SmartContract/Helper.cs | 2 +- .../Extensions/NativeContractExtensions.cs | 2 +- .../Nep5NativeContractExtensions.cs | 12 +-- tests/neo.UnitTests/Ledger/UT_MemoryPool.cs | 2 +- .../Network/P2P/Payloads/UT_Transaction.cs | 14 ++-- .../Native/Tokens/UT_GasToken.cs | 4 +- .../Native/Tokens/UT_NeoToken.cs | 12 +-- .../SmartContract/Native/UT_NativeContract.cs | 8 +- .../SmartContract/Native/UT_PolicyContract.cs | 12 +-- .../SmartContract/UT_ApplicationEngine.cs | 2 +- .../UT_ApplicationEngineProvider.cs | 80 +++++++++++++++++++ .../SmartContract/UT_InteropPrices.cs | 14 ++-- .../SmartContract/UT_InteropService.NEO.cs | 10 +-- .../SmartContract/UT_InteropService.cs | 32 ++++---- .../SmartContract/UT_Syscalls.Callback.cs | 6 +- .../SmartContract/UT_Syscalls.cs | 26 +++--- 21 files changed, 196 insertions(+), 86 deletions(-) create mode 100644 src/neo/Plugins/IApplicationEngineProvider.cs rename src/neo/Plugins/{IStoragePlugin.cs => IStorageProvider.cs} (70%) create mode 100644 tests/neo.UnitTests/SmartContract/UT_ApplicationEngineProvider.cs diff --git a/src/neo/Ledger/Blockchain.cs b/src/neo/Ledger/Blockchain.cs index 092309a5a7..142180932a 100644 --- a/src/neo/Ledger/Blockchain.cs +++ b/src/neo/Ledger/Blockchain.cs @@ -411,7 +411,7 @@ private void Persist(Block block) snapshot.PersistingBlock = block; if (block.Index > 0) { - using (ApplicationEngine engine = new ApplicationEngine(TriggerType.System, null, snapshot, 0, true)) + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.System, null, snapshot, 0, true)) { engine.LoadScript(onPersistNativeContractScript); if (engine.Execute() != VMState.HALT) throw new InvalidOperationException(); @@ -434,7 +434,7 @@ private void Persist(Block block) clonedSnapshot.Transactions.Add(tx.Hash, state); clonedSnapshot.Transactions.Commit(); - using (ApplicationEngine engine = new ApplicationEngine(TriggerType.Application, tx, clonedSnapshot, tx.SystemFee)) + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Application, tx, clonedSnapshot, tx.SystemFee)) { engine.LoadScript(tx.Script); state.VMState = engine.Execute(); diff --git a/src/neo/Plugins/IApplicationEngineProvider.cs b/src/neo/Plugins/IApplicationEngineProvider.cs new file mode 100644 index 0000000000..ed74147fa0 --- /dev/null +++ b/src/neo/Plugins/IApplicationEngineProvider.cs @@ -0,0 +1,11 @@ +using Neo.Network.P2P.Payloads; +using Neo.Persistence; +using Neo.SmartContract; + +namespace Neo.Plugins +{ + public interface IApplicationEngineProvider + { + ApplicationEngine Create(TriggerType trigger, IVerifiable container, StoreView snapshot, long gas, bool testMode = false); + } +} diff --git a/src/neo/Plugins/IStoragePlugin.cs b/src/neo/Plugins/IStorageProvider.cs similarity index 70% rename from src/neo/Plugins/IStoragePlugin.cs rename to src/neo/Plugins/IStorageProvider.cs index 68f75dd2bb..5ef2d9f098 100644 --- a/src/neo/Plugins/IStoragePlugin.cs +++ b/src/neo/Plugins/IStorageProvider.cs @@ -2,7 +2,7 @@ namespace Neo.Plugins { - public interface IStoragePlugin + public interface IStorageProvider { IStore GetStore(); } diff --git a/src/neo/Plugins/Plugin.cs b/src/neo/Plugins/Plugin.cs index d98d1703a6..690c202d53 100644 --- a/src/neo/Plugins/Plugin.cs +++ b/src/neo/Plugins/Plugin.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Configuration; +using Neo.SmartContract; using System; using System.Collections.Generic; using System.IO; @@ -13,7 +14,7 @@ public abstract class Plugin : IDisposable { public static readonly List Plugins = new List(); internal static readonly List Loggers = new List(); - internal static readonly Dictionary Storages = new Dictionary(); + internal static readonly Dictionary Storages = new Dictionary(); internal static readonly List PersistencePlugins = new List(); internal static readonly List P2PPlugins = new List(); internal static readonly List TxObserverPlugins = new List(); @@ -50,10 +51,11 @@ protected Plugin() Plugins.Add(this); if (this is ILogPlugin logger) Loggers.Add(logger); - if (this is IStoragePlugin storage) Storages.Add(Name, storage); + if (this is IStorageProvider storage) Storages.Add(Name, storage); if (this is IP2PPlugin p2p) P2PPlugins.Add(p2p); if (this is IPersistencePlugin persistence) PersistencePlugins.Add(persistence); if (this is IMemoryPoolTxObserverPlugin txObserver) TxObserverPlugins.Add(txObserver); + if (this is IApplicationEngineProvider provider) ApplicationEngine.SetApplicationEngineProvider(provider); Configure(); } diff --git a/src/neo/SmartContract/ApplicationEngine.cs b/src/neo/SmartContract/ApplicationEngine.cs index 97646236bb..a3a1078a37 100644 --- a/src/neo/SmartContract/ApplicationEngine.cs +++ b/src/neo/SmartContract/ApplicationEngine.cs @@ -2,6 +2,7 @@ using Neo.Ledger; using Neo.Network.P2P.Payloads; using Neo.Persistence; +using Neo.Plugins; using Neo.VM; using Neo.VM.Types; using System; @@ -9,6 +10,7 @@ using System.Linq; using System.Numerics; using System.Reflection; +using static System.Threading.Interlocked; using Array = System.Array; using VMArray = Neo.VM.Types.Array; @@ -26,6 +28,7 @@ private class InvocationState public static event EventHandler Notify; public static event EventHandler Log; + private static IApplicationEngineProvider applicationEngineProvider; private static Dictionary services; private readonly long gas_amount; private readonly bool testMode; @@ -46,7 +49,7 @@ private class InvocationState public UInt160 EntryScriptHash => EntryContext?.GetState().ScriptHash; public IReadOnlyList Notifications => notifications ?? (IReadOnlyList)Array.Empty(); - public ApplicationEngine(TriggerType trigger, IVerifiable container, StoreView snapshot, long gas, bool testMode = false) + protected ApplicationEngine(TriggerType trigger, IVerifiable container, StoreView snapshot, long gas, bool testMode = false) { this.Trigger = trigger; this.ScriptContainer = container; @@ -108,6 +111,10 @@ protected override void ContextUnloaded(ExecutionContext context) } } + public static ApplicationEngine Create(TriggerType trigger, IVerifiable container, StoreView snapshot, long gas, bool testMode = false) + => applicationEngineProvider?.Create(trigger, container, snapshot, gas, testMode) + ?? new ApplicationEngine(trigger, container, snapshot, gas, testMode); + private InvocationState GetInvocationState(ExecutionContext context) { if (!invocationStates.TryGetValue(context, out InvocationState state)) @@ -266,11 +273,16 @@ private static InteropDescriptor Register(string name, string handler, long fixe return descriptor; } + internal static void ResetApplicationEngineProvider() + { + Exchange(ref applicationEngineProvider, null); + } + public static ApplicationEngine Run(byte[] script, StoreView snapshot, IVerifiable container = null, Block persistingBlock = null, int offset = 0, bool testMode = false, long gas = default) { snapshot.PersistingBlock = persistingBlock ?? snapshot.PersistingBlock ?? CreateDummyBlock(snapshot); - ApplicationEngine engine = new ApplicationEngine(TriggerType.Application, container, snapshot, gas, testMode); + ApplicationEngine engine = Create(TriggerType.Application, container, snapshot, gas, testMode); engine.LoadScript(script).InstructionPointer = offset; engine.Execute(); return engine; @@ -283,5 +295,10 @@ public static ApplicationEngine Run(byte[] script, IVerifiable container = null, return Run(script, snapshot, container, persistingBlock, offset, testMode, gas); } } + + internal static bool SetApplicationEngineProvider(IApplicationEngineProvider provider) + { + return CompareExchange(ref applicationEngineProvider, provider, null) is null; + } } } diff --git a/src/neo/SmartContract/Helper.cs b/src/neo/SmartContract/Helper.cs index 6fedd8b9f9..54f3c9eb2e 100644 --- a/src/neo/SmartContract/Helper.cs +++ b/src/neo/SmartContract/Helper.cs @@ -162,7 +162,7 @@ internal static bool VerifyWitnesses(this IVerifiable verifiable, StoreView snap if (hashes[i] != verifiable.Witnesses[i].ScriptHash) return false; offset = 0; } - using (ApplicationEngine engine = new ApplicationEngine(TriggerType.Verification, verifiable, snapshot, gas)) + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, verifiable, snapshot, gas)) { engine.LoadScript(verification, CallFlags.ReadOnly).InstructionPointer = offset; engine.LoadScript(verifiable.Witnesses[i].InvocationScript, CallFlags.None); diff --git a/tests/neo.UnitTests/Extensions/NativeContractExtensions.cs b/tests/neo.UnitTests/Extensions/NativeContractExtensions.cs index cbf4a109d9..dfd2cb7005 100644 --- a/tests/neo.UnitTests/Extensions/NativeContractExtensions.cs +++ b/tests/neo.UnitTests/Extensions/NativeContractExtensions.cs @@ -17,7 +17,7 @@ public static StackItem Call(this NativeContract contract, StoreView snapshot, s public static StackItem Call(this NativeContract contract, StoreView snapshot, IVerifiable container, string method, params ContractParameter[] args) { - var engine = new ApplicationEngine(TriggerType.Application, container, snapshot, 0, true); + var engine = ApplicationEngine.Create(TriggerType.Application, container, snapshot, 0, true); engine.LoadScript(contract.Script); diff --git a/tests/neo.UnitTests/Extensions/Nep5NativeContractExtensions.cs b/tests/neo.UnitTests/Extensions/Nep5NativeContractExtensions.cs index 0053b4e929..b0cd686022 100644 --- a/tests/neo.UnitTests/Extensions/Nep5NativeContractExtensions.cs +++ b/tests/neo.UnitTests/Extensions/Nep5NativeContractExtensions.cs @@ -42,7 +42,7 @@ public void SerializeUnsigned(BinaryWriter writer) { } public static bool Transfer(this NativeContract contract, StoreView snapshot, byte[] from, byte[] to, BigInteger amount, bool signFrom) { - var engine = new ApplicationEngine(TriggerType.Application, + var engine = ApplicationEngine.Create(TriggerType.Application, new ManualWitness(signFrom ? new UInt160(from) : null), snapshot, 0, true); engine.LoadScript(contract.Script); @@ -69,7 +69,7 @@ public static bool Transfer(this NativeContract contract, StoreView snapshot, by public static BigInteger TotalSupply(this NativeContract contract, StoreView snapshot) { - var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(contract.Script); @@ -89,7 +89,7 @@ public static BigInteger TotalSupply(this NativeContract contract, StoreView sna public static BigInteger BalanceOf(this NativeContract contract, StoreView snapshot, byte[] account) { - var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(contract.Script); @@ -110,7 +110,7 @@ public static BigInteger BalanceOf(this NativeContract contract, StoreView snaps public static BigInteger Decimals(this NativeContract contract) { - var engine = new ApplicationEngine(TriggerType.Application, null, null, 0, testMode: true); + var engine = ApplicationEngine.Create(TriggerType.Application, null, null, 0, testMode: true); engine.LoadScript(contract.Script); @@ -130,7 +130,7 @@ public static BigInteger Decimals(this NativeContract contract) public static string Symbol(this NativeContract contract) { - var engine = new ApplicationEngine(TriggerType.Application, null, null, 0, testMode: true); + var engine = ApplicationEngine.Create(TriggerType.Application, null, null, 0, testMode: true); engine.LoadScript(contract.Script); @@ -150,7 +150,7 @@ public static string Symbol(this NativeContract contract) public static string Name(this NativeContract contract) { - var engine = new ApplicationEngine(TriggerType.Application, null, null, 0, testMode: true); + var engine = ApplicationEngine.Create(TriggerType.Application, null, null, 0, testMode: true); engine.LoadScript(contract.Script); diff --git a/tests/neo.UnitTests/Ledger/UT_MemoryPool.cs b/tests/neo.UnitTests/Ledger/UT_MemoryPool.cs index 4fd9617e63..e2bb92836e 100644 --- a/tests/neo.UnitTests/Ledger/UT_MemoryPool.cs +++ b/tests/neo.UnitTests/Ledger/UT_MemoryPool.cs @@ -223,7 +223,7 @@ public void BlockPersistAndReverificationWillAbandonTxAsBalanceTransfered() SnapshotView snapshot = Blockchain.Singleton.GetSnapshot(); BigInteger balance = NativeContract.GAS.BalanceOf(snapshot, sender); - ApplicationEngine applicationEngine = new ApplicationEngine(TriggerType.All, block, snapshot, (long)balance); + ApplicationEngine applicationEngine = ApplicationEngine.Create(TriggerType.All, block, snapshot, (long)balance); NativeContract.GAS.Burn(applicationEngine, sender, balance); NativeContract.GAS.Mint(applicationEngine, sender, txFee * 30); // Set the balance to meet 30 txs only diff --git a/tests/neo.UnitTests/Network/P2P/Payloads/UT_Transaction.cs b/tests/neo.UnitTests/Network/P2P/Payloads/UT_Transaction.cs index bf11da269f..de5079a46d 100644 --- a/tests/neo.UnitTests/Network/P2P/Payloads/UT_Transaction.cs +++ b/tests/neo.UnitTests/Network/P2P/Payloads/UT_Transaction.cs @@ -145,7 +145,7 @@ public void FeeIsMultiSigContract() long verificationGas = 0; foreach (var witness in tx.Witnesses) { - using (ApplicationEngine engine = new ApplicationEngine(TriggerType.Verification, tx, snapshot, tx.NetworkFee, false)) + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, tx, snapshot, tx.NetworkFee, false)) { engine.LoadScript(witness.VerificationScript); engine.LoadScript(witness.InvocationScript); @@ -227,7 +227,7 @@ public void FeeIsSignatureContractDetailed() long verificationGas = 0; foreach (var witness in tx.Witnesses) { - using (ApplicationEngine engine = new ApplicationEngine(TriggerType.Verification, tx, snapshot, tx.NetworkFee, false)) + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, tx, snapshot, tx.NetworkFee, false)) { engine.LoadScript(witness.VerificationScript); engine.LoadScript(witness.InvocationScript); @@ -340,7 +340,7 @@ public void FeeIsSignatureContract_TestScope_Global() long verificationGas = 0; foreach (var witness in tx.Witnesses) { - using (ApplicationEngine engine = new ApplicationEngine(TriggerType.Verification, tx, snapshot, tx.NetworkFee, false)) + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, tx, snapshot, tx.NetworkFee, false)) { engine.LoadScript(witness.VerificationScript); engine.LoadScript(witness.InvocationScript); @@ -427,7 +427,7 @@ public void FeeIsSignatureContract_TestScope_CurrentHash_GAS() long verificationGas = 0; foreach (var witness in tx.Witnesses) { - using (ApplicationEngine engine = new ApplicationEngine(TriggerType.Verification, tx, snapshot, tx.NetworkFee, false)) + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, tx, snapshot, tx.NetworkFee, false)) { engine.LoadScript(witness.VerificationScript); engine.LoadScript(witness.InvocationScript); @@ -517,7 +517,7 @@ public void FeeIsSignatureContract_TestScope_CalledByEntry_Plus_GAS() long verificationGas = 0; foreach (var witness in tx.Witnesses) { - using (ApplicationEngine engine = new ApplicationEngine(TriggerType.Verification, tx, snapshot, tx.NetworkFee, false)) + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, tx, snapshot, tx.NetworkFee, false)) { engine.LoadScript(witness.VerificationScript); engine.LoadScript(witness.InvocationScript); @@ -659,7 +659,7 @@ public void FeeIsSignatureContract_TestScope_CurrentHash_NEO_GAS() long verificationGas = 0; foreach (var witness in tx.Witnesses) { - using (ApplicationEngine engine = new ApplicationEngine(TriggerType.Verification, tx, snapshot, tx.NetworkFee, false)) + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, tx, snapshot, tx.NetworkFee, false)) { engine.LoadScript(witness.VerificationScript); engine.LoadScript(witness.InvocationScript); @@ -1009,7 +1009,7 @@ public void FeeIsSignatureContract_TestScope_FeeOnly_Default() long verificationGas = 0; foreach (var witness in tx.Witnesses) { - using (ApplicationEngine engine = new ApplicationEngine(TriggerType.Verification, tx, snapshot, tx.NetworkFee, false)) + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, tx, snapshot, tx.NetworkFee, false)) { engine.LoadScript(witness.VerificationScript); engine.LoadScript(witness.InvocationScript); diff --git a/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_GasToken.cs b/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_GasToken.cs index f1d10853b0..17e45ed08c 100644 --- a/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_GasToken.cs +++ b/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_GasToken.cs @@ -89,7 +89,7 @@ public void Check_BalanceOfTransferAndBurn() // Burn - var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0); + var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0); keyCount = snapshot.Storages.GetChangeSet().Count(); Assert.ThrowsException(() => @@ -124,7 +124,7 @@ public void Check_BalanceOfTransferAndBurn() [TestMethod] public void Check_BadScript() { - var engine = new ApplicationEngine(TriggerType.Application, null, Blockchain.Singleton.GetSnapshot(), 0); + var engine = ApplicationEngine.Create(TriggerType.Application, null, Blockchain.Singleton.GetSnapshot(), 0); var script = new ScriptBuilder(); script.Emit(OpCode.NOP); diff --git a/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_NeoToken.cs b/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_NeoToken.cs index 02e9cba21a..1cce77536e 100644 --- a/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_NeoToken.cs +++ b/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_NeoToken.cs @@ -206,7 +206,7 @@ public void Check_Initialize() [TestMethod] public void Check_BadScript() { - var engine = new ApplicationEngine(TriggerType.Application, null, Blockchain.Singleton.GetSnapshot(), 0); + var engine = ApplicationEngine.Create(TriggerType.Application, null, Blockchain.Singleton.GetSnapshot(), 0); var script = new ScriptBuilder(); script.Emit(OpCode.NOP); @@ -424,7 +424,7 @@ public void TestVote() { var snapshot = Blockchain.Singleton.GetSnapshot(); snapshot.PersistingBlock = Blockchain.GenesisBlock; - var engine = new ApplicationEngine(TriggerType.Application, Blockchain.GenesisBlock, snapshot, 0, true); + var engine = ApplicationEngine.Create(TriggerType.Application, Blockchain.GenesisBlock, snapshot, 0, true); ScriptBuilder sb = new ScriptBuilder(); var tmp = engine.ScriptContainer.GetScriptHashesForVerifying(engine.Snapshot); UInt160 from = engine.ScriptContainer.GetScriptHashesForVerifying(engine.Snapshot)[0]; @@ -455,7 +455,7 @@ public void TestVote() internal static (bool State, bool Result) Check_Vote(StoreView snapshot, byte[] account, byte[] pubkey, bool signAccount) { - var engine = new ApplicationEngine(TriggerType.Application, + var engine = ApplicationEngine.Create(TriggerType.Application, new Nep5NativeContractExtensions.ManualWitness(signAccount ? new UInt160(account) : UInt160.Zero), snapshot, 0, true); engine.LoadScript(NativeContract.NEO.Script); @@ -485,7 +485,7 @@ internal static (bool State, bool Result) Check_Vote(StoreView snapshot, byte[] internal static (bool State, bool Result) Check_RegisterValidator(StoreView snapshot, byte[] pubkey) { - var engine = new ApplicationEngine(TriggerType.Application, + var engine = ApplicationEngine.Create(TriggerType.Application, new Nep5NativeContractExtensions.ManualWitness(Contract.CreateSignatureRedeemScript(ECPoint.DecodePoint(pubkey, ECCurve.Secp256r1)).ToScriptHash()), snapshot, 0, true); engine.LoadScript(NativeContract.NEO.Script); @@ -510,7 +510,7 @@ internal static (bool State, bool Result) Check_RegisterValidator(StoreView snap internal static ECPoint[] Check_GetValidators(StoreView snapshot) { - var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(NativeContract.NEO.Script); @@ -530,7 +530,7 @@ internal static ECPoint[] Check_GetValidators(StoreView snapshot) internal static (BigInteger Value, bool State) Check_UnclaimedGas(StoreView snapshot, byte[] address) { - var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(NativeContract.NEO.Script); diff --git a/tests/neo.UnitTests/SmartContract/Native/UT_NativeContract.cs b/tests/neo.UnitTests/SmartContract/Native/UT_NativeContract.cs index 90970a506b..3188bddb4c 100644 --- a/tests/neo.UnitTests/SmartContract/Native/UT_NativeContract.cs +++ b/tests/neo.UnitTests/SmartContract/Native/UT_NativeContract.cs @@ -24,7 +24,7 @@ public void TestSetup() [TestMethod] public void TestInitialize() { - ApplicationEngine ae = new ApplicationEngine(TriggerType.Application, null, null, 0); + ApplicationEngine ae = ApplicationEngine.Create(TriggerType.Application, null, null, 0); testNativeContract.Initialize(ae); } @@ -32,7 +32,7 @@ public void TestInitialize() public void TestInvoke() { var snapshot = Blockchain.Singleton.GetSnapshot(); - ApplicationEngine engine = new ApplicationEngine(TriggerType.System, null, snapshot, 0); + ApplicationEngine engine = ApplicationEngine.Create(TriggerType.System, null, snapshot, 0); engine.LoadScript(testNativeContract.Script); ByteString method1 = new ByteString(System.Text.Encoding.Default.GetBytes("wrongMethod")); @@ -53,10 +53,10 @@ public void TestOnPersistWithArgs() { var snapshot = Blockchain.Singleton.GetSnapshot(); - ApplicationEngine engine1 = new ApplicationEngine(TriggerType.Application, null, snapshot, 0); + ApplicationEngine engine1 = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0); Assert.ThrowsException(() => testNativeContract.TestOnPersist(engine1)); - ApplicationEngine engine2 = new ApplicationEngine(TriggerType.System, null, snapshot, 0); + ApplicationEngine engine2 = ApplicationEngine.Create(TriggerType.System, null, snapshot, 0); testNativeContract.TestOnPersist(engine2); } diff --git a/tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs b/tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs index ac3b1dd2e9..d464f45ea8 100644 --- a/tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs +++ b/tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs @@ -25,7 +25,7 @@ public void Check_Initialize() var snapshot = Blockchain.Singleton.GetSnapshot(); var keyCount = snapshot.Storages.GetChangeSet().Count(); - NativeContract.Policy.Initialize(new ApplicationEngine(TriggerType.Application, null, snapshot, 0)); + NativeContract.Policy.Initialize(ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0)); (keyCount + 5).Should().Be(snapshot.Storages.GetChangeSet().Count()); @@ -62,7 +62,7 @@ public void Check_SetMaxBlockSize() UInt160 committeeMultiSigAddr = NativeContract.NEO.GetCommitteeAddress(snapshot); - NativeContract.Policy.Initialize(new ApplicationEngine(TriggerType.Application, null, snapshot, 0)); + NativeContract.Policy.Initialize(ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0)); // Without signature @@ -110,7 +110,7 @@ public void Check_SetMaxBlockSystemFee() UInt160 committeeMultiSigAddr = NativeContract.NEO.GetCommitteeAddress(snapshot); - NativeContract.Policy.Initialize(new ApplicationEngine(TriggerType.Application, null, snapshot, 0)); + NativeContract.Policy.Initialize(ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0)); // Without signature @@ -156,7 +156,7 @@ public void Check_SetMaxTransactionsPerBlock() snapshot.PersistingBlock = new Block() { Index = 1000, PrevHash = UInt256.Zero }; snapshot.Blocks.Add(UInt256.Zero, new Neo.Ledger.TrimmedBlock() { NextConsensus = UInt160.Zero }); - NativeContract.Policy.Initialize(new ApplicationEngine(TriggerType.Application, null, snapshot, 0)); + NativeContract.Policy.Initialize(ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0)); // Without signature @@ -191,7 +191,7 @@ public void Check_SetFeePerByte() snapshot.PersistingBlock = new Block() { Index = 1000, PrevHash = UInt256.Zero }; snapshot.Blocks.Add(UInt256.Zero, new Neo.Ledger.TrimmedBlock() { NextConsensus = UInt160.Zero }); - NativeContract.Policy.Initialize(new ApplicationEngine(TriggerType.Application, null, snapshot, 0)); + NativeContract.Policy.Initialize(ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0)); // Without signature @@ -228,7 +228,7 @@ public void Check_Block_UnblockAccount() UInt160 committeeMultiSigAddr = NativeContract.NEO.GetCommitteeAddress(snapshot); - NativeContract.Policy.Initialize(new ApplicationEngine(TriggerType.Application, null, snapshot, 0)); + NativeContract.Policy.Initialize(ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0)); // Block without signature diff --git a/tests/neo.UnitTests/SmartContract/UT_ApplicationEngine.cs b/tests/neo.UnitTests/SmartContract/UT_ApplicationEngine.cs index 6bccb8f179..3d8c435a06 100644 --- a/tests/neo.UnitTests/SmartContract/UT_ApplicationEngine.cs +++ b/tests/neo.UnitTests/SmartContract/UT_ApplicationEngine.cs @@ -21,7 +21,7 @@ public void TestSetup() public void TestNotify() { var snapshot = Blockchain.Singleton.GetSnapshot(); - var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); ApplicationEngine.Notify += Test_Notify1; const string notifyEvent = "TestEvent"; diff --git a/tests/neo.UnitTests/SmartContract/UT_ApplicationEngineProvider.cs b/tests/neo.UnitTests/SmartContract/UT_ApplicationEngineProvider.cs new file mode 100644 index 0000000000..81c32cd8b5 --- /dev/null +++ b/tests/neo.UnitTests/SmartContract/UT_ApplicationEngineProvider.cs @@ -0,0 +1,80 @@ +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Network.P2P.Payloads; +using Neo.Persistence; +using Neo.Plugins; +using Neo.SmartContract; + +namespace Neo.UnitTests.SmartContract +{ + [TestClass] + public class UT_ApplicationEngineProvider + { + [TestInitialize] + public void TestInitialize() + { + ApplicationEngine.ResetApplicationEngineProvider(); + } + + [TestCleanup] + public void TestCleanup() + { + ApplicationEngine.ResetApplicationEngineProvider(); + } + + [TestMethod] + public void TestSetAppEngineProvider() + { + var provider = new TestProvider(); + ApplicationEngine.SetApplicationEngineProvider(provider).Should().BeTrue(); + + using var appEngine = ApplicationEngine.Create(TriggerType.Application, null, null, 0); + (appEngine is TestEngine).Should().BeTrue(); + } + + [TestMethod] + public void TestDefaultAppEngineProvider() + { + using var appEngine = ApplicationEngine.Create(TriggerType.Application, null, null, 0); + (appEngine is ApplicationEngine).Should().BeTrue(); + } + + [TestMethod] + public void TestCantSetAppEngineProviderTwice() + { + var provider = new TestProvider(); + ApplicationEngine.SetApplicationEngineProvider(provider).Should().BeTrue(); + + var provider2 = new TestProvider(); + ApplicationEngine.SetApplicationEngineProvider(provider2).Should().BeFalse(); + } + + [TestMethod] + public void TestCanResetAppEngineProviderTwice() + { + var provider = new TestProvider(); + ApplicationEngine.SetApplicationEngineProvider(provider).Should().BeTrue(); + + ApplicationEngine.ResetApplicationEngineProvider(); + + var provider2 = new TestProvider(); + ApplicationEngine.SetApplicationEngineProvider(provider2).Should().BeTrue(); + } + + class TestProvider : IApplicationEngineProvider + { + public ApplicationEngine Create(TriggerType trigger, IVerifiable container, StoreView snapshot, long gas, bool testMode = false) + { + return new TestEngine(trigger, container, snapshot, gas, testMode); + } + } + + class TestEngine : ApplicationEngine + { + public TestEngine(TriggerType trigger, IVerifiable container, StoreView snapshot, long gas, bool testMode = false) + : base(trigger, container, snapshot, gas, testMode) + { + } + } + } +} diff --git a/tests/neo.UnitTests/SmartContract/UT_InteropPrices.cs b/tests/neo.UnitTests/SmartContract/UT_InteropPrices.cs index cf93e5abb3..1635e431e8 100644 --- a/tests/neo.UnitTests/SmartContract/UT_InteropPrices.cs +++ b/tests/neo.UnitTests/SmartContract/UT_InteropPrices.cs @@ -21,7 +21,7 @@ public void ApplicationEngineFixedPrices() { // System.Runtime.CheckWitness: f827ec8c (price is 200) byte[] SyscallSystemRuntimeCheckWitnessHash = new byte[] { 0x68, 0xf8, 0x27, 0xec, 0x8c }; - using (ApplicationEngine ae = new ApplicationEngine(TriggerType.Application, null, null, 0)) + using (ApplicationEngine ae = ApplicationEngine.Create(TriggerType.Application, null, null, 0)) { ae.LoadScript(SyscallSystemRuntimeCheckWitnessHash); ApplicationEngine.System_Runtime_CheckWitness.FixedPrice.Should().Be(0_00030000L); @@ -29,7 +29,7 @@ public void ApplicationEngineFixedPrices() // System.Storage.GetContext: 9bf667ce (price is 1) byte[] SyscallSystemStorageGetContextHash = new byte[] { 0x68, 0x9b, 0xf6, 0x67, 0xce }; - using (ApplicationEngine ae = new ApplicationEngine(TriggerType.Application, null, null, 0)) + using (ApplicationEngine ae = ApplicationEngine.Create(TriggerType.Application, null, null, 0)) { ae.LoadScript(SyscallSystemStorageGetContextHash); ApplicationEngine.System_Storage_GetContext.FixedPrice.Should().Be(0_00000400L); @@ -37,7 +37,7 @@ public void ApplicationEngineFixedPrices() // System.Storage.Get: 925de831 (price is 100) byte[] SyscallSystemStorageGetHash = new byte[] { 0x68, 0x92, 0x5d, 0xe8, 0x31 }; - using (ApplicationEngine ae = new ApplicationEngine(TriggerType.Application, null, null, 0)) + using (ApplicationEngine ae = ApplicationEngine.Create(TriggerType.Application, null, null, 0)) { ae.LoadScript(SyscallSystemStorageGetHash); ApplicationEngine.System_Storage_Get.FixedPrice.Should().Be(0_01000000L); @@ -65,7 +65,7 @@ public void ApplicationEngineRegularPut() snapshot.Storages.Add(skey, sItem); snapshot.Contracts.Add(script.ToScriptHash(), contractState); - using (ApplicationEngine ae = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, testMode: true)) + using (ApplicationEngine ae = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, testMode: true)) { Debugger debugger = new Debugger(ae); ae.LoadScript(script); @@ -99,7 +99,7 @@ public void ApplicationEngineReusedStorage_FullReuse() snapshot.Storages.Add(skey, sItem); snapshot.Contracts.Add(script.ToScriptHash(), contractState); - using (ApplicationEngine applicationEngine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, testMode: true)) + using (ApplicationEngine applicationEngine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, testMode: true)) { Debugger debugger = new Debugger(applicationEngine); applicationEngine.LoadScript(script); @@ -135,7 +135,7 @@ public void ApplicationEngineReusedStorage_PartialReuse() snapshot.Storages.Add(skey, sItem); snapshot.Contracts.Add(script.ToScriptHash(), contractState); - using (ApplicationEngine ae = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, testMode: true)) + using (ApplicationEngine ae = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, testMode: true)) { Debugger debugger = new Debugger(ae); ae.LoadScript(script); @@ -172,7 +172,7 @@ public void ApplicationEngineReusedStorage_PartialReuseTwice() snapshot.Storages.Add(skey, sItem); snapshot.Contracts.Add(script.ToScriptHash(), contractState); - using (ApplicationEngine ae = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, testMode: true)) + using (ApplicationEngine ae = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, testMode: true)) { Debugger debugger = new Debugger(ae); ae.LoadScript(script); diff --git a/tests/neo.UnitTests/SmartContract/UT_InteropService.NEO.cs b/tests/neo.UnitTests/SmartContract/UT_InteropService.NEO.cs index 5d78c4ebd5..4d4eaf4b07 100644 --- a/tests/neo.UnitTests/SmartContract/UT_InteropService.NEO.cs +++ b/tests/neo.UnitTests/SmartContract/UT_InteropService.NEO.cs @@ -115,12 +115,12 @@ public void TestAccount_IsStandard() var snapshot = Blockchain.Singleton.GetSnapshot(); var state = TestUtils.GetContract(); snapshot.Contracts.Add(state.ScriptHash, state); - engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(new byte[] { 0x01 }); engine.IsStandardContract(state.ScriptHash).Should().BeFalse(); state.Script = Contract.CreateSignatureRedeemScript(Blockchain.StandbyValidators[0]); - engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(new byte[] { 0x01 }); engine.IsStandardContract(state.ScriptHash).Should().BeTrue(); } @@ -141,7 +141,7 @@ public void TestContract_Create() var snapshot = Blockchain.Singleton.GetSnapshot(); var state = TestUtils.GetContract(); snapshot.Contracts.Add(state.ScriptHash, state); - engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0); + engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0); engine.LoadScript(new byte[] { 0x01 }); Assert.ThrowsException(() => engine.CreateContract(state.Script, manifest.ToJson().ToByteArray(false))); } @@ -184,7 +184,7 @@ public void TestContract_Update() }; snapshot.Contracts.Add(state.ScriptHash, state); snapshot.Storages.Add(storageKey, storageItem); - engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(state.Script); engine.UpdateContract(script, manifest.ToJson().ToByteArray(false)); engine.Snapshot.Storages.Find(BitConverter.GetBytes(state.Id)).ToList().Count().Should().Be(1); @@ -209,7 +209,7 @@ public void TestStorage_Find() }; snapshot.Contracts.Add(state.ScriptHash, state); snapshot.Storages.Add(storageKey, storageItem); - var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(new byte[] { 0x01 }); var iterator = engine.Find(new StorageContext diff --git a/tests/neo.UnitTests/SmartContract/UT_InteropService.cs b/tests/neo.UnitTests/SmartContract/UT_InteropService.cs index 174aace8f1..3a0062275f 100644 --- a/tests/neo.UnitTests/SmartContract/UT_InteropService.cs +++ b/tests/neo.UnitTests/SmartContract/UT_InteropService.cs @@ -58,7 +58,7 @@ public void Runtime_GetNotifications_Test() // Wrong length - using (var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true)) + using (var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true)) using (var script = new ScriptBuilder()) { // Retrive @@ -75,7 +75,7 @@ public void Runtime_GetNotifications_Test() // All test - using (var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true)) + using (var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true)) using (var script = new ScriptBuilder()) { // Notification @@ -127,7 +127,7 @@ public void Runtime_GetNotifications_Test() // Script notifications - using (var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true)) + using (var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true)) using (var script = new ScriptBuilder()) { // Notification @@ -386,7 +386,7 @@ public void TestBlockchain_GetContract() var snapshot = Blockchain.Singleton.GetSnapshot(); var state = TestUtils.GetContract(); snapshot.Contracts.Add(state.ScriptHash, state); - engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(new byte[] { 0x01 }); engine.GetContract(state.ScriptHash).Should().BeSameAs(state); } @@ -433,7 +433,7 @@ public void TestStorage_Get() }; snapshot.Contracts.Add(state.ScriptHash, state); snapshot.Storages.Add(storageKey, storageItem); - var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(new byte[] { 0x01 }); engine.Get(new StorageContext @@ -491,7 +491,7 @@ public void TestStorage_Put() }; snapshot.Contracts.Add(state.ScriptHash, state); snapshot.Storages.Add(storageKey, storageItem); - engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(new byte[] { 0x01 }); key = new byte[] { 0x01 }; value = new byte[] { 0x02 }; @@ -527,7 +527,7 @@ public void TestStorage_PutEx() }; snapshot.Contracts.Add(state.ScriptHash, state); snapshot.Storages.Add(storageKey, storageItem); - engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(new byte[] { 0x01 }); var key = new byte[] { 0x01 }; var value = new byte[] { 0x02 }; @@ -558,7 +558,7 @@ public void TestStorage_Delete() }; snapshot.Contracts.Add(state.ScriptHash, state); snapshot.Storages.Add(storageKey, storageItem); - engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(new byte[] { 0x01 }); state.Manifest.Features = ContractFeatures.HasStorage; var key = new byte[] { 0x01 }; @@ -597,7 +597,7 @@ public void TestContract_Call() state.Manifest.Features = ContractFeatures.HasStorage; snapshot.Contracts.Add(state.ScriptHash, state); - var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(new byte[] { 0x01 }); engine.CallContract(state.ScriptHash, method, args); @@ -627,7 +627,7 @@ public void TestContract_CallEx() foreach (var flags in new CallFlags[] { CallFlags.None, CallFlags.AllowCall, CallFlags.AllowModifyStates, CallFlags.All }) { - var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(new byte[] { 0x01 }); engine.CallContractEx(state.ScriptHash, method, args, CallFlags.All); @@ -667,7 +667,7 @@ public void TestContract_Destroy() }; snapshot.Contracts.Add(scriptHash, state); snapshot.Storages.Add(storageKey, storageItem); - engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(new byte[0]); engine.DestroyContract(); engine.Snapshot.Storages.Find(BitConverter.GetBytes(0x43000000)).Any().Should().BeFalse(); @@ -676,7 +676,7 @@ public void TestContract_Destroy() snapshot = Blockchain.Singleton.GetSnapshot(); state = TestUtils.GetContract(); snapshot.Contracts.Add(scriptHash, state); - engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(new byte[0]); engine.DestroyContract(); engine.Snapshot.Storages.Find(BitConverter.GetBytes(0x43000000)).Any().Should().BeFalse(); @@ -703,19 +703,19 @@ private static ApplicationEngine GetEngine(bool hasContainer = false, bool hasSn ApplicationEngine engine; if (hasContainer && hasSnapshot) { - engine = new ApplicationEngine(TriggerType.Application, tx, snapshot, 0, true); + engine = ApplicationEngine.Create(TriggerType.Application, tx, snapshot, 0, true); } else if (hasContainer && !hasSnapshot) { - engine = new ApplicationEngine(TriggerType.Application, tx, null, 0, true); + engine = ApplicationEngine.Create(TriggerType.Application, tx, null, 0, true); } else if (!hasContainer && hasSnapshot) { - engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); } else { - engine = new ApplicationEngine(TriggerType.Application, null, null, 0, true); + engine = ApplicationEngine.Create(TriggerType.Application, null, null, 0, true); } if (addScript) { diff --git a/tests/neo.UnitTests/SmartContract/UT_Syscalls.Callback.cs b/tests/neo.UnitTests/SmartContract/UT_Syscalls.Callback.cs index e97e144637..92444fa92c 100644 --- a/tests/neo.UnitTests/SmartContract/UT_Syscalls.Callback.cs +++ b/tests/neo.UnitTests/SmartContract/UT_Syscalls.Callback.cs @@ -21,7 +21,7 @@ public void CreateCallbackTest() // Execute - var engine = new ApplicationEngine(TriggerType.Application, null, null, 100_000_000, false); + var engine = ApplicationEngine.Create(TriggerType.Application, null, null, 100_000_000, false); engine.LoadScript(script.ToArray()); Assert.AreEqual(engine.Execute(), VMState.HALT); @@ -54,7 +54,7 @@ public void InvokeCallbackTest() // Execute - var engine = new ApplicationEngine(TriggerType.Application, null, null, 100_000_000, false); + var engine = ApplicationEngine.Create(TriggerType.Application, null, null, 100_000_000, false); engine.LoadScript(script.ToArray()); Assert.AreEqual(engine.Execute(), VMState.HALT); @@ -79,7 +79,7 @@ public void CreateSyscallCallbackTest() // Execute - var engine = new ApplicationEngine(TriggerType.Application, null, null, 100_000_000, false); + var engine = ApplicationEngine.Create(TriggerType.Application, null, null, 100_000_000, false); engine.LoadScript(script.ToArray()); Assert.AreEqual(engine.Execute(), VMState.HALT); diff --git a/tests/neo.UnitTests/SmartContract/UT_Syscalls.cs b/tests/neo.UnitTests/SmartContract/UT_Syscalls.cs index b32e808f4d..72b33f41b5 100644 --- a/tests/neo.UnitTests/SmartContract/UT_Syscalls.cs +++ b/tests/neo.UnitTests/SmartContract/UT_Syscalls.cs @@ -61,7 +61,7 @@ public void System_Blockchain_GetBlock() // Without block - var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(script.ToArray()); Assert.AreEqual(engine.Execute(), VMState.HALT); @@ -78,7 +78,7 @@ public void System_Blockchain_GetBlock() blocks.Add(block.Hash, block.Trim()); txs.Add(tx.Hash, new TransactionState() { Transaction = tx, BlockIndex = block.Index, VMState = VMState.HALT }); - engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(script.ToArray()); Assert.AreEqual(engine.Execute(), VMState.HALT); @@ -89,7 +89,7 @@ public void System_Blockchain_GetBlock() height.Index = block.Index; - engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(script.ToArray()); Assert.AreEqual(engine.Execute(), VMState.HALT); @@ -117,7 +117,7 @@ public void Json_Deserialize() script.EmitPush("null"); script.EmitSysCall(ApplicationEngine.System_Json_Deserialize); - using (var engine = new ApplicationEngine(TriggerType.Application, null, null, 0, true)) + using (var engine = ApplicationEngine.Create(TriggerType.Application, null, null, 0, true)) { engine.LoadScript(script.ToArray()); @@ -136,7 +136,7 @@ public void Json_Deserialize() script.EmitPush("***"); script.EmitSysCall(ApplicationEngine.System_Json_Deserialize); - using (var engine = new ApplicationEngine(TriggerType.Application, null, null, 0, true)) + using (var engine = ApplicationEngine.Create(TriggerType.Application, null, null, 0, true)) { engine.LoadScript(script.ToArray()); @@ -152,7 +152,7 @@ public void Json_Deserialize() script.EmitPush("123.45"); script.EmitSysCall(ApplicationEngine.System_Json_Deserialize); - using (var engine = new ApplicationEngine(TriggerType.Application, null, null, 0, true)) + using (var engine = ApplicationEngine.Create(TriggerType.Application, null, null, 0, true)) { engine.LoadScript(script.ToArray()); @@ -185,7 +185,7 @@ public void Json_Serialize() script.Emit(OpCode.SETITEM); script.EmitSysCall(ApplicationEngine.System_Json_Serialize); - using (var engine = new ApplicationEngine(TriggerType.Application, null, null, 0, true)) + using (var engine = ApplicationEngine.Create(TriggerType.Application, null, null, 0, true)) { engine.LoadScript(script.ToArray()); @@ -207,7 +207,7 @@ public void Json_Serialize() script.EmitSysCall(ApplicationEngine.System_Storage_GetContext); script.EmitSysCall(ApplicationEngine.System_Json_Serialize); - using (var engine = new ApplicationEngine(TriggerType.Application, null, null, 0, true)) + using (var engine = ApplicationEngine.Create(TriggerType.Application, null, null, 0, true)) { engine.LoadScript(script.ToArray()); @@ -227,7 +227,7 @@ public void System_ExecutionEngine_GetScriptContainer() // Without tx - var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(script.ToArray()); Assert.AreEqual(engine.Execute(), VMState.HALT); @@ -249,7 +249,7 @@ public void System_ExecutionEngine_GetScriptContainer() Witnesses = new Witness[] { new Witness() { VerificationScript = new byte[] { 0x07 } } }, }; - engine = new ApplicationEngine(TriggerType.Application, tx, snapshot, 0, true); + engine = ApplicationEngine.Create(TriggerType.Application, tx, snapshot, 0, true); engine.LoadScript(script.ToArray()); Assert.AreEqual(engine.Execute(), VMState.HALT); @@ -278,7 +278,7 @@ public void System_Runtime_GasLeft() // Execute - var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 100_000_000, false); + var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 100_000_000, false); engine.LoadScript(script.ToArray()); Assert.AreEqual(engine.Execute(), VMState.HALT); @@ -299,7 +299,7 @@ public void System_Runtime_GasLeft() // Execute - var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(script.ToArray()); // Check the results @@ -353,7 +353,7 @@ public void System_Runtime_GetInvocationCounter() // Execute - var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); + var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(script.ToArray()); Assert.AreEqual(VMState.HALT, engine.Execute());