Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove testmode from ApplicationEngine #1819

Merged
merged 13 commits into from
Aug 6, 2020
21 changes: 14 additions & 7 deletions src/neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.Plugins;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.VM.Types;
using System;
Expand Down Expand Up @@ -31,7 +32,6 @@ private class InvocationState
private static IApplicationEngineProvider applicationEngineProvider;
private static Dictionary<uint, InteropDescriptor> services;
private readonly long gas_amount;
private readonly bool testMode;
private List<NotifyEventArgs> notifications;
private List<IDisposable> disposables;
private readonly Dictionary<UInt160, int> invocationCounter = new Dictionary<UInt160, int>();
Expand All @@ -42,26 +42,25 @@ private class InvocationState
public IVerifiable ScriptContainer { get; }
public StoreView Snapshot { get; }
public long GasConsumed { get; private set; } = 0;
public long GasLeft => testMode ? -1 : gas_amount - GasConsumed;
public long GasLeft => gas_amount - GasConsumed;
public Exception FaultException { get; private set; }
public UInt160 CurrentScriptHash => CurrentContext?.GetState<ExecutionContextState>().ScriptHash;
public UInt160 CallingScriptHash => CurrentContext?.GetState<ExecutionContextState>().CallingScriptHash;
public UInt160 EntryScriptHash => EntryContext?.GetState<ExecutionContextState>().ScriptHash;
public IReadOnlyList<NotifyEventArgs> Notifications => notifications ?? (IReadOnlyList<NotifyEventArgs>)Array.Empty<NotifyEventArgs>();

protected ApplicationEngine(TriggerType trigger, IVerifiable container, StoreView snapshot, long gas, bool testMode = false)
protected ApplicationEngine(TriggerType trigger, IVerifiable container, StoreView snapshot, long gas)
{
this.Trigger = trigger;
this.ScriptContainer = container;
this.Snapshot = snapshot;
this.gas_amount = gas;
this.testMode = testMode;
}

internal void AddGas(long gas)
{
GasConsumed = checked(GasConsumed + gas);
if (!testMode && GasConsumed > gas_amount)
if (GasConsumed > gas_amount)
throw new InvalidOperationException("Insufficient GAS.");
}

Expand Down Expand Up @@ -112,8 +111,16 @@ 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);
{
if (testMode)
{
if (snapshot == null) gas = 9000 * (long)NativeContract.GAS.Factor;
else gas = NativeContract.Policy.GetMaxBlockSystemFee(snapshot);
}

return applicationEngineProvider?.Create(trigger, container, snapshot, gas, testMode)
?? new ApplicationEngine(trigger, container, snapshot, gas);
}

private InvocationState GetInvocationState(ExecutionContext context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ 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);
return new TestEngine(trigger, container, snapshot, gas);
}
}

class TestEngine : ApplicationEngine
{
public TestEngine(TriggerType trigger, IVerifiable container, StoreView snapshot, long gas, bool testMode = false)
: base(trigger, container, snapshot, gas, testMode)
public TestEngine(TriggerType trigger, IVerifiable container, StoreView snapshot, long gas)
: base(trigger, container, snapshot, gas)
{
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/neo.UnitTests/SmartContract/UT_Syscalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public void System_Runtime_GasLeft()
Assert.AreEqual(engine.Execute(), VMState.HALT);
Assert.AreEqual(1, engine.ResultStack.Count);
Assert.IsInstanceOfType(engine.ResultStack.Peek(), typeof(Integer));
Assert.AreEqual(-1, engine.ResultStack.Pop().GetInteger());
Assert.AreEqual(899999999600, engine.ResultStack.Pop().GetInteger());
}
}

Expand Down