Skip to content

Commit

Permalink
Better clarify some EIP-161 nuances.
Browse files Browse the repository at this point in the history
  • Loading branch information
fselmo committed Oct 20, 2022
1 parent e20500d commit 079881d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 36 deletions.
16 changes: 5 additions & 11 deletions eth/_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,9 @@ def get_block_header_by_hash(block_hash: Hash32, db: ChainDatabaseAPI) -> BlockH

def apply_state_dict(state: StateAPI, state_dict: AccountState) -> None:
for account, account_data in state_dict.items():
balance, nonce, code, storage = (
account_data["balance"],
account_data["nonce"],
account_data["code"],
account_data["storage"],
)
state.set_balance(account, balance)
state.set_nonce(account, nonce)
state.set_code(account, code)

for slot, value in storage.items():
state.set_balance(account, account_data["balance"])
state.set_nonce(account, account_data["nonce"])
state.set_code(account, account_data["code"])

for slot, value in account_data["storage"].items():
state.set_storage(account, slot, value)
36 changes: 17 additions & 19 deletions eth/vm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,28 +372,26 @@ def _assign_block_rewards(self, block: BlockAPI) -> None:
len(block.uncles) * self.get_nephew_reward()
)

if block_reward != 0:
self.state.delta_balance(block.header.coinbase, block_reward)
self.logger.debug(
"BLOCK REWARD: %s -> %s",
block_reward,
encode_hex(block.header.coinbase),
)
else:
self.logger.debug("No block reward given to %s", block.header.coinbase)
# EIP-161:
# Even if block reward is zero, the coinbase is still touched here. This was
# not likely to ever happen in PoW, except maybe in some very niche cases, but
# happens now in PoS. In these cases, the coinbase may end up zeroed after the
# computation and thus should be marked for deletion since it was touched.
self.state.delta_balance(block.header.coinbase, block_reward)
self.logger.debug(
"BLOCK REWARD: %s -> %s",
block_reward,
encode_hex(block.header.coinbase),
)

for uncle in block.uncles:
uncle_reward = self.get_uncle_reward(block.number, uncle)

if uncle_reward != 0:
self.state.delta_balance(uncle.coinbase, uncle_reward)
self.logger.debug(
"UNCLE REWARD REWARD: %s -> %s",
uncle_reward,
encode_hex(uncle.coinbase),
)
else:
self.logger.debug("No uncle reward given to %s", uncle.coinbase)
self.logger.debug(
"UNCLE REWARD REWARD: %s -> %s",
uncle_reward,
encode_hex(uncle.coinbase),
)
self.state.delta_balance(uncle.coinbase, uncle_reward)

def finalize_block(self, block: BlockAPI) -> BlockAndMetaWitness:
if block.number > 0:
Expand Down
6 changes: 6 additions & 0 deletions eth/vm/forks/frontier/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ def finalize_computation(
# Beneficiary Fees
gas_used = transaction.gas - gas_remaining - gas_refund
transaction_fee = gas_used * self.vm_state.get_tip(transaction)

# EIP-161:
# Even if the txn fee is zero, the coinbase is still touched here. Post-merge,
# with no block reward, in the cases where the txn fee is also zero, the
# coinbase may end up zeroed after the computation and thus should be marked
# for deletion since it was touched.
self.vm_state.logger.debug2(
'TRANSACTION FEE: %s -> %s',
transaction_fee,
Expand Down
9 changes: 4 additions & 5 deletions eth/vm/forks/spurious_dragon/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ def collect_touched_accounts(
See also: https://github.com/ethereum/EIPs/issues/716
"""
# collect the coinbase account if it is in a zero-state. The coinbase is always
# touched via block transaction fee.
coinbase = computation.state.coinbase
if computation.state.account_is_empty(coinbase):
yield coinbase
# EIP-161:
# The coinbase is always touched via block transaction fee and block rewards
# (pre-merge).
yield computation.state.coinbase

# collect those explicitly marked for deletion ("beneficiary" is of SELFDESTRUCT)
for beneficiary in sorted(set(computation.accounts_to_delete.values())):
Expand Down
2 changes: 1 addition & 1 deletion tests/core/consensus/test_clique_consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def test_import_block(paragon_chain):

signed_header = sign_block_header(vm.get_block().header.copy(
extra_data=VANITY_LENGTH * b'0' + SIGNATURE_LENGTH * b'0',
state_root=b'\x99\xaa\xf5CF^\x95_\xce~\xe4)\x00\xb1zr\x1dr\xd6\x00N^\xa6\xdc\xc41\x90~\xb7te\x00', # noqa: E501
state_root=b'\x02g\xd5{\xf9\x9f\x9e\xab)\x06\x1eY\x9a\xb7W\x95\xfd\xae\x9a:\x83m%\xbb\xcc\xe1\xca\xe3\x85d\xa7\x05', # noqa: E501
transaction_root=b'\xd1\t\xc4\x150\x9f\xb0\xb4H{\xfd$?Q\x16\x90\xac\xb2L[f\x98\xdd\xc6*\xf7\n\x84f\xafg\xb3', # noqa: E501
nonce=NONCE_DROP,
gas_used=21000,
Expand Down

0 comments on commit 079881d

Please sign in to comment.