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

Update gas metering for calls to empty accounts #1774

Merged
merged 4 commits into from
Aug 25, 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
2 changes: 1 addition & 1 deletion manticore/platforms/evm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2144,7 +2144,7 @@ def CALL_gas(self, wanted_gas, address, value, in_offset, in_size, out_offset, o
known_address = False
for address_i in self.world.accounts:
known_address = Operators.OR(known_address, address == address_i)
fee += Operators.ITEBV(512, Operators.AND(known_address, value == 0), 0, GCALLNEW)
fee += Operators.ITEBV(512, Operators.OR(known_address, value == 0), 0, GCALLNEW)
fee += self._get_memfee(in_offset, in_size)

exception = False
Expand Down
119 changes: 119 additions & 0 deletions tests/ethereum/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,125 @@ def test_selfdestruct(self):
)
self.assertEqual(m.count_ready_states(), 1)

def test_call_gas(self):
GCALLSTATIC = 21721 # 21000 + (3 * 7 push ops) + 700 static cost for call
GCALLVALUE = 9000 # cost added for nonzero callvalue
GCALLNEW = 25000 # cost added for forcing new acct creation
GCALLSTIPEND = 2300 # additional gas sent with a call if value > 0

with disposable_mevm() as m:
# nonempty call target
m.create_account(
address=0x111111111111111111111111111111111111111,
nonce=1 # nonempty account
)

# call(gas, target, value, in_offset, in_size, out_offset, out_size)
# call to empty acct with value = 0
asm_call_empty_no_val = """ PUSH1 0x0
PUSH1 0X0
PUSH1 0x0
PUSH1 0X0
PUSH1 0x0
PUSH20 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
PUSH1 0x0
CALL
STOP
"""
# call to existing acct with value > 0
asm_call_nonempty_w_val = """ PUSH1 0x0
PUSH1 0X0
PUSH1 0x0
PUSH1 0X0
PUSH1 0x1
PUSH20 0x111111111111111111111111111111111111111
PUSH1 0x0
CALL
STOP
"""
# call to empty acct with value > 0, forcing addition to state trie
asm_call_empty_w_val = """ PUSH1 0x0
PUSH1 0X0
PUSH1 0x0
PUSH1 0X0
PUSH1 0x1
PUSH20 0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
PUSH1 0x0
CALL
STOP
"""

call_empty_no_val = m.create_account(
code=EVMAsm.assemble(asm_call_empty_no_val)
)
call_nonempty_w_val = m.create_account(
balance=100,
code=EVMAsm.assemble(asm_call_nonempty_w_val)
)
call_empty_w_val = m.create_account(
balance=100,
code=EVMAsm.assemble(asm_call_empty_w_val)
)

caller = m.create_account(
address=0x222222222222222222222222222222222222222,
balance=1000000000000000000
)

# call to empty acct with value = 0
m.transaction(
caller=caller,
address=call_empty_no_val,
data=b'',
value=0,
gas=50000000
)
self.assertEqual(m.count_ready_states(), 1)
state = next(m.ready_states)
txs = state.platform.transactions
# no value, so no call stipend should be sent
self.assertEqual(txs[-2].gas, 0)
# no value, so only static call cost should be charged
self.assertEqual(txs[-1].used_gas, GCALLSTATIC)

# call to existing acct with value > 0
m.transaction(
caller=caller,
address=call_nonempty_w_val,
data=b'',
value=0,
gas=50000000
)
self.assertEqual(m.count_ready_states(), 1)
state = next(m.ready_states)
txs = state.platform.transactions
# call stipend should be sent with call
self.assertEqual(txs[-2].gas, GCALLSTIPEND)
# cost of call should include value cost, but not new acct cost
self.assertEqual(
txs[-1].used_gas,
GCALLSTATIC + GCALLVALUE - GCALLSTIPEND
)

# call to empty acct with value > 0, forcing addition to state trie
m.transaction(
caller=caller,
address=call_empty_w_val,
data=b'',
value=0,
gas=50000000
)
self.assertEqual(m.count_ready_states(), 1)
state = next(m.ready_states)
txs = state.platform.transactions
# call stipend should be sent with call
self.assertEqual(txs[-2].gas, GCALLSTIPEND)
# cost of call should include value cost and new acct cost
self.assertEqual(
txs[-1].used_gas,
GCALLSTATIC + GCALLVALUE + GCALLNEW - GCALLSTIPEND
)


class EthPluginTests(unittest.TestCase):
def test_FilterFunctions_fallback_function_matching(self):
Expand Down