Skip to content

Commit

Permalink
Add GasLeft syscall (#1509)
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon authored Mar 26, 2020
1 parent 4755824 commit fe66c0f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
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);

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

0 comments on commit fe66c0f

Please sign in to comment.