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 contract.gasLeft() #255

Draft
wants to merge 6 commits into
base: topic/hardfork-v4
Choose a base branch
from
Draft

add contract.gasLeft() #255

wants to merge 6 commits into from

Conversation

kroggen
Copy link
Member

@kroggen kroggen commented Sep 23, 2023

This PR adds the contract.gasLeft() function to retrieve the remaining gas in the contract execution

Closes #239

There is a problem though:

The lua_pushinteger() function accepts a value of type lua_Integer, that is signed int64, and the lua_gasget() function returns an unsigned int64

So if the value of gas is too big, it may appear for the Lua code as a negative value (to be tested)

@kroggen kroggen added the V4 To be released on hardfork V4 label Sep 25, 2023
@kroggen
Copy link
Member Author

kroggen commented Sep 26, 2023

One solution is to use bignum for gasLeft()

But then other gas related functions should use bignum as well, for consistency

system.getAmount() and contract.balance() do not use bignum 😕

@kroggen kroggen added the feature New feature or request label Sep 28, 2023
@kroggen kroggen changed the base branch from develop to topic/hardfork-v4 October 26, 2023 15:38
@kroggen
Copy link
Member Author

kroggen commented Nov 16, 2023

When the gaslimit is not set on the transaction, or when it is set to unlimited, the gasLeft is set to UINT64_MAX

So the gaslimit may not be so useful to detect if the txn is about to run out of gas due to the txn creator's balance being low

It may be useful only to limit gas usage on a called contract. But the method used for that would make the transaction to fail (and the changes be reverted). It would be useful to have a way to set a limit and do not revert the txn, like a protected call that just returns false if it runs out of gas

@kroggen
Copy link
Member Author

kroggen commented Nov 16, 2023

Maybe this PR do not need to be applied on V4, and can be left to the next hardfork

@hayarobi
Copy link
Member

In the current version, gas usage is increased on garbage collection. This means that it is virtually impossible for the user to determine the actual remaining gas since it is executed after the contract function call is returned. We need to change the way gas is calculated first, for this function to be meaningful.

@kroggen
Copy link
Member Author

kroggen commented Dec 22, 2023

This is changed on V4, this commit disables gas consumption for garbage collection

Although not tested

@kroggen
Copy link
Member Author

kroggen commented Dec 22, 2023

But the "remaining gas" is computed based on the transaction "gas limit", and if it is not specified, it is set to UINT64_MAX, even if it is higher than what the account's balance can pay.

The reason may be because the account can receive tokens while the transaction is executed, on one of the called contracts, so the execution is not limited on the initial balance of the account

It turns out that the number is huge and makes no much sense when used on contract.gasLeft()

Maybe this can be implemented later when the gas limit per block is implemented

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request V4 To be released on hardfork V4
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add GasLeft() function
2 participants