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

Add GasLeft syscall #1509

Merged
merged 5 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public partial class ApplicationEngine : ExecutionEngine
public IVerifiable ScriptContainer { get; }
public StoreView Snapshot { get; }
public long GasConsumed { get; private set; } = 0;
public long GasLeft => testMode ? -1 : gas_amount - GasConsumed;

public UInt160 CurrentScriptHash => CurrentContext?.GetState<ExecutionContextState>().ScriptHash;
public UInt160 CallingScriptHash => CurrentContext?.GetState<ExecutionContextState>().CallingScriptHash;
Expand Down
7 changes: 7 additions & 0 deletions src/neo/SmartContract/InteropService.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static class Runtime
public static readonly InteropDescriptor Log = Register("System.Runtime.Log", Runtime_Log, 0_01000000, TriggerType.All, CallFlags.AllowNotify);
public static readonly InteropDescriptor Notify = Register("System.Runtime.Notify", Runtime_Notify, 0_01000000, TriggerType.All, CallFlags.AllowNotify);
public static readonly InteropDescriptor GetNotifications = Register("System.Runtime.GetNotifications", Runtime_GetNotifications, 0_00010000, TriggerType.All, CallFlags.None);
public static readonly InteropDescriptor GasLeft = Register("System.Runtime.GasLeft", Runtime_GasLeft, 0_00000400, TriggerType.All, CallFlags.None);
shargon marked this conversation as resolved.
Show resolved Hide resolved

private static bool CheckItemForNotification(StackItem state)
{
Expand Down Expand Up @@ -167,6 +168,12 @@ private static bool Runtime_CheckWitness(ApplicationEngine engine)
return true;
}

private static bool Runtime_GasLeft(ApplicationEngine engine)
{
engine.Push(engine.GasLeft);
return true;
}

private static bool Runtime_GetInvocationCounter(ApplicationEngine engine)
{
if (!engine.InvocationCounter.TryGetValue(engine.CurrentScriptHash, out var counter))
Expand Down
51 changes: 51 additions & 0 deletions tests/neo.UnitTests/SmartContract/UT_Syscalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,57 @@ public void System_ExecutionEngine_GetScriptContainer()
}
}

[TestMethod]
public void System_Runtime_GasLeft()
{
var snapshot = Blockchain.Singleton.GetSnapshot();

using (var script = new ScriptBuilder())
{
script.Emit(OpCode.NOP);
script.EmitSysCall(InteropService.Runtime.GasLeft);
script.Emit(OpCode.NOP);
script.EmitSysCall(InteropService.Runtime.GasLeft);
script.Emit(OpCode.NOP);
script.Emit(OpCode.NOP);
script.Emit(OpCode.NOP);
script.EmitSysCall(InteropService.Runtime.GasLeft);

// Execute

var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 100_000_000, false);
engine.LoadScript(script.ToArray());
Assert.AreEqual(engine.Execute(), VMState.HALT);

// Check the results

CollectionAssert.AreEqual
(
engine.ResultStack.Select(u => (int)((VM.Types.Integer)u).GetBigInteger()).ToArray(),
new int[] { 99_999_570, 99_999_140, 99_998_650 }
);
}

// Check test mode

using (var script = new ScriptBuilder())
{
script.EmitSysCall(InteropService.Runtime.GasLeft);

// Execute

var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true);
engine.LoadScript(script.ToArray());

// Check the results

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().GetBigInteger());
}
}

[TestMethod]
public void System_Runtime_GetInvocationCounter()
{
Expand Down