-
Notifications
You must be signed in to change notification settings - Fork 473
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
Better control of gas accounting #1823
Open
feliam
wants to merge
11
commits into
master
Choose a base branch
from
dev-ignore-gas-better
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
01e4697
Better control of gas accounting
feliam 154ec62
Lint 1/100
feliam 5b17718
Lint 3/100
feliam c4de8a0
New black
feliam 423f5d5
FIx tests config + bugfix
feliam 5e324af
new black
feliam 200b9c5
Monster black
feliam 2bb05ee
blkn
feliam c741d20
merge
feliam b5e5e36
blkn
feliam f1f9f92
bad merge
feliam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,18 @@ def globalfakesha3(data): | |
return None | ||
|
||
|
||
consts.add( | ||
"gas", | ||
default="static", | ||
description=( | ||
"Control how to keep the gas count" | ||
"insane: Keep static and dynamic gas including transaction fee VERY EXPENSIVE (INSANE)." | ||
"full: Keep static and dynamic gas excluding the dynamic aspect of transaction related fee." | ||
"static: Use a static fee for each instruction. Dynamic gas is ignored. For example, memory, storage, refunds, tx dynamic fees are ignored" | ||
"ignore: Ignore gas completely. Instructions won't consume gas" | ||
), | ||
) | ||
|
||
consts.add( | ||
"oog", | ||
default="ignore", | ||
|
@@ -102,11 +114,7 @@ def globalfakesha3(data): | |
default=-1, | ||
description="Max calldata size to explore in each CALLDATACOPY. Iff size in a calldata related instruction are symbolic it will be constrained to be less than this constant. -1 means free(only use when gas is being tracked)", | ||
) | ||
consts.add( | ||
"ignore_balance", | ||
default=False, | ||
description="Do not try to solve symbolic balances", | ||
) | ||
consts.add("ignore_balance", default=False, description="Do not try to solve symbolic balances") | ||
|
||
|
||
# Auxiliary constants and functions | ||
|
@@ -1061,6 +1069,11 @@ def _pop(self): | |
return self.stack.pop() | ||
|
||
def _consume(self, fee): | ||
if consts.gas == "ignore": | ||
return 0 | ||
if consts.gas == "static": | ||
assert not issymbolic(fee) | ||
|
||
# Check type and bitvec size | ||
if isinstance(fee, int): | ||
if fee > (1 << 512) - 1: | ||
|
@@ -1167,11 +1180,14 @@ def _push_results(self, instruction, result): | |
assert result is None | ||
|
||
def _calculate_gas(self, *arguments): | ||
if consts.gas == "ignore": | ||
return 0 | ||
current = self.instruction | ||
implementation = getattr(self, f"{current.semantics}_gas", None) | ||
if implementation is None: | ||
return current.fee | ||
return current.fee + implementation(*arguments) | ||
fee = current.fee | ||
if implementation is not None and consts.gas != "static": | ||
fee += implementation(*arguments) | ||
return fee | ||
|
||
def _handler(self, *arguments): | ||
current = self.instruction | ||
|
@@ -2124,12 +2140,13 @@ def SWAP(self, *operands): | |
############################################################################ | ||
# Logging Operations | ||
def LOG_gas(self, address, size, *topics): | ||
return self._get_memfee(address, size) | ||
GLOGBYTE = 8 | ||
fee = self.safe_mul(size, GLOGBYTE) | ||
fee += self._get_memfee(address, size) | ||
return fee | ||
|
||
@concretized_args(size="ONE") | ||
def LOG(self, address, size, *topics): | ||
GLOGBYTE = 8 | ||
self._consume(self.safe_mul(size, GLOGBYTE)) | ||
memlog = self.read_buffer(address, size) | ||
self.world.log(self.address, topics, memlog) | ||
|
||
|
@@ -2164,14 +2181,15 @@ def CALL_gas(self, wanted_gas, address, value, in_offset, in_size, out_offset, o | |
) | ||
fee += self._get_memfee(in_offset, in_size) | ||
|
||
exception = False | ||
available_gas = self._gas | ||
available_gas -= fee | ||
|
||
exception = Operators.OR( | ||
Operators.UGT(fee, self._gas), | ||
Operators.ULT(self.safe_mul(available_gas, 63), available_gas), | ||
) | ||
self.fail_if(exception) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this line need to check the value of |
||
|
||
available_gas *= 63 | ||
available_gas //= 64 | ||
|
||
|
@@ -2185,13 +2203,23 @@ def CALL_gas(self, wanted_gas, address, value, in_offset, in_size, out_offset, o | |
@concretized_args(address="ACCOUNTS", in_offset="SAMPLED", in_size="SAMPLED") | ||
def CALL(self, gas, address, value, in_offset, in_size, out_offset, out_size): | ||
"""Message-call into an account""" | ||
if consts.gas in ("dynamic", "insane"): | ||
tx_gas = self._temp_call_gas + Operators.ITEBV(512, value != 0, 2300, 0) | ||
else: | ||
|
||
available_gas = self._gas | ||
available_gas *= 63 | ||
available_gas //= 64 | ||
wanted_gas = gas | ||
tx_gas = Operators.ITEBV(256, available_gas < wanted_gas, available_gas, wanted_gas) | ||
|
||
self.world.start_transaction( | ||
"CALL", | ||
address, | ||
data=self.read_buffer(in_offset, in_size), | ||
caller=self.address, | ||
value=value, | ||
gas=self._temp_call_gas + Operators.ITEBV(512, value != 0, 2300, 0), | ||
gas=tx_gas, | ||
) | ||
raise StartTx() | ||
|
||
|
@@ -2555,19 +2583,22 @@ def _transaction_fee(self, sort, address, price, bytecode_or_data, caller, value | |
|
||
zerocount = 0 | ||
nonzerocount = 0 | ||
if isinstance(bytecode_or_data, (Array, ArrayProxy)): | ||
# if nothing was written we can assume all elements are default to zero | ||
if len(bytecode_or_data.written) == 0: | ||
zerocount = len(bytecode_or_data) | ||
else: | ||
for index in range(len(bytecode_or_data)): | ||
try: | ||
c = bytecode_or_data.get(index, 0) | ||
except AttributeError: | ||
c = bytecode_or_data[index] | ||
|
||
zerocount += Operators.ITEBV(256, c == 0, 1, 0) | ||
nonzerocount += Operators.ITEBV(256, c == 0, 0, 1) | ||
if consts.gas == "insane": | ||
if isinstance(bytecode_or_data, (Array, ArrayProxy)): | ||
# if nothing was written we can assume all elements are default to zero | ||
if len(bytecode_or_data.written) == 0: | ||
zerocount = len(bytecode_or_data) | ||
else: | ||
for index in range(len(bytecode_or_data)): | ||
try: | ||
c = bytecode_or_data.get(index, 0) | ||
except AttributeError: | ||
c = bytecode_or_data[index] | ||
|
||
zerocount += Operators.ITEBV(256, c == 0, 1, 0) | ||
nonzerocount += Operators.ITEBV(256, c == 0, 0, 1) | ||
elif consts.gas == "full": | ||
nonzerocount = len(bytecode_or_data) | ||
|
||
tx_fee += zerocount * GTXDATAZERO | ||
tx_fee += nonzerocount * GTXDATANONZERO | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, but can we make this "exhaustive"? Or, if you want to preserve the suggestion that it's over-the-top, perhaps "ludicrous"?