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

Limit Refuel to 1 GAS #2561

Closed
wants to merge 10 commits into from
Closed
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
5 changes: 5 additions & 0 deletions src/neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public partial class ApplicationEngine : ExecutionEngine
private static IApplicationEngineProvider applicationEngineProvider;
private static Dictionary<uint, InteropDescriptor> services;
private long gas_amount;
private long gas_refuel = 0;
private List<NotifyEventArgs> notifications;
private List<IDisposable> disposables;
private readonly Dictionary<UInt160, int> invocationCounter = new();
Expand Down Expand Up @@ -158,8 +159,12 @@ protected internal void AddGas(long gas)

internal void Refuel(long gas)
{
if (ScriptContainer is not Transaction tx) throw new InvalidOperationException();
checked
{
gas_refuel += gas;
if (gas_refuel > tx.SystemFee + tx.NetworkFee)
throw new InvalidOperationException("Too much GAS is refuelled.");
gas_amount += gas;
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/neo/SmartContract/Native/GasToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ internal override async ContractTask OnPersist(ApplicationEngine engine)
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States | CallFlags.AllowNotify)]
private async ContractTask Refuel(ApplicationEngine engine, UInt160 account, long amount)
{
if (amount < 0) throw new ArgumentOutOfRangeException(nameof(amount));
if (amount <= 0) throw new ArgumentOutOfRangeException(nameof(amount));
if (ContractManagement.GetContract(engine.Snapshot, engine.CallingScriptHash) is null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Account?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?

Copy link
Member

@shargon shargon Aug 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who pay te gas it's the account argument

throw new InvalidOperationException();
if (!engine.CheckWitnessInternal(account)) throw new InvalidOperationException();
await Burn(engine, account, amount);
engine.Refuel(amount);
await Burn(engine, account, amount);
}
}
}
6 changes: 3 additions & 3 deletions tests/neo.UnitTests/SmartContract/Native/UT_GasToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void Refuel()
byte[] script;
using (ScriptBuilder sb = new())
{
sb.EmitDynamicCall(NativeContract.GAS.Hash, "refuel", accBalance.ScriptHash, 100 * NativeContract.GAS.Factor);
sb.EmitDynamicCall(NativeContract.GAS.Hash, "refuel", accBalance.ScriptHash, 1 * NativeContract.GAS.Factor);
sb.Emit(OpCode.DROP);
sb.EmitSysCall(ApplicationEngine.System_Runtime_GasLeft);
script = sb.ToArray();
Expand All @@ -91,10 +91,10 @@ public void Refuel()
engine.LoadScript(tx.Script);
Assert.AreEqual(VMState.HALT, engine.Execute());
Assert.AreEqual(1, engine.ResultStack.Count);
Assert.AreEqual(100_00300140, engine.ResultStack.Pop().GetInteger());
Assert.AreEqual(1_00296140, engine.ResultStack.Pop().GetInteger());

entry = snapshot.GetAndChange(key, () => new StorageItem(new AccountState()));
Assert.AreEqual(0, entry.GetInteroperable<AccountState>().Balance);
Assert.AreEqual(99_00000000, entry.GetInteroperable<AccountState>().Balance);
}

[TestMethod]
Expand Down