Skip to content

Commit 6604ee8

Browse files
committed
oracle like approach
1 parent 72df534 commit 6604ee8

File tree

10 files changed

+115
-66
lines changed

10 files changed

+115
-66
lines changed

src/ethereum/osaka/fork.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"""
1414

1515
from dataclasses import dataclass
16-
from typing import Any, List, Optional, Tuple
16+
from typing import List, Optional, Tuple
1717

1818
from ethereum_rlp import rlp
1919
from ethereum_types.bytes import Bytes
@@ -179,9 +179,7 @@ def get_last_256_block_hashes(chain: BlockChain) -> List[Hash32]:
179179
return recent_block_hashes
180180

181181

182-
def state_transition(
183-
chain: BlockChain, block: Block, oracle: Optional[Any] = None
184-
) -> None:
182+
def state_transition(chain: BlockChain, block: Block) -> None:
185183
"""
186184
Attempts to apply a block to an existing block chain.
187185
@@ -212,13 +210,9 @@ def state_transition(
212210
if block.ommers != ():
213211
raise InvalidBlock
214212

215-
# Oracle must be provided
216-
if oracle is None:
217-
raise ValueError("Oracle parameter is required for state_transition")
218-
219213
block_env = vm.BlockEnvironment(
220214
chain_id=chain.chain_id,
221-
oracle=oracle,
215+
state=chain.state,
222216
block_gas_limit=block.header.gas_limit,
223217
block_hashes=get_last_256_block_hashes(chain),
224218
coinbase=block.header.coinbase,
@@ -235,7 +229,7 @@ def state_transition(
235229
transactions=block.transactions,
236230
withdrawals=block.withdrawals,
237231
)
238-
block_state_root = block_env.oracle.state_root()
232+
block_state_root = block_env.get_oracle().state_root()
239233
transactions_root = root(block_output.transactions_trie)
240234
receipt_root = root(block_output.receipts_trie)
241235
block_logs_bloom = logs_bloom(block_output.block_logs)
@@ -459,7 +453,7 @@ def check_transaction(
459453
raise BlobGasLimitExceededError("blob gas limit exceeded")
460454

461455
sender_address = recover_sender(block_env.chain_id, tx)
462-
sender_account = block_env.oracle.get_account(sender_address)
456+
sender_account = block_env.get_oracle().get_account(sender_address)
463457

464458
if isinstance(
465459
tx, (FeeMarketTransaction, BlobTransaction, SetCodeTransaction)
@@ -664,7 +658,9 @@ def process_checked_system_transaction(
664658
system_tx_output : `MessageCallOutput`
665659
Output of processing the system transaction.
666660
"""
667-
system_contract_code = block_env.oracle.get_account(target_address).code
661+
system_contract_code = (
662+
block_env.get_oracle().get_account(target_address).code
663+
)
668664

669665
if len(system_contract_code) == 0:
670666
raise InvalidBlock(
@@ -711,7 +707,9 @@ def process_unchecked_system_transaction(
711707
system_tx_output : `MessageCallOutput`
712708
Output of processing the system transaction.
713709
"""
714-
system_contract_code = block_env.oracle.get_account(target_address).code
710+
system_contract_code = (
711+
block_env.get_oracle().get_account(target_address).code
712+
)
715713
return process_system_transaction(
716714
block_env,
717715
target_address,
@@ -868,7 +866,7 @@ def process_transaction(
868866
tx=tx,
869867
)
870868

871-
sender_account = block_env.oracle.get_account(sender)
869+
sender_account = block_env.get_oracle().get_account(sender)
872870

873871
if isinstance(tx, BlobTransaction):
874872
blob_gas_fee = calculate_data_fee(block_env.excess_blob_gas, tx)
@@ -878,12 +876,12 @@ def process_transaction(
878876
effective_gas_fee = tx.gas * effective_gas_price
879877

880878
gas = tx.gas - intrinsic_gas
881-
block_env.oracle.increment_nonce(sender)
879+
block_env.get_oracle().increment_nonce(sender)
882880

883881
sender_balance_after_gas_fee = (
884882
Uint(sender_account.balance) - effective_gas_fee - blob_gas_fee
885883
)
886-
block_env.oracle.set_account_balance(
884+
block_env.get_oracle().set_account_balance(
887885
sender, U256(sender_balance_after_gas_fee)
888886
)
889887

@@ -947,29 +945,33 @@ def process_transaction(
947945
transaction_fee = tx_gas_used_after_refund * priority_fee_per_gas
948946

949947
# refund gas
950-
current_sender_balance = block_env.oracle.get_account(sender).balance
948+
current_sender_balance = block_env.get_oracle().get_account(sender).balance
951949
sender_balance_after_refund = current_sender_balance + U256(
952950
gas_refund_amount
953951
)
954-
block_env.oracle.set_account_balance(sender, sender_balance_after_refund)
952+
block_env.get_oracle().set_account_balance(
953+
sender, sender_balance_after_refund
954+
)
955955

956956
# transfer miner fees
957-
current_coinbase_balance = block_env.oracle.get_account(
958-
block_env.coinbase
959-
).balance
957+
current_coinbase_balance = (
958+
block_env.get_oracle().get_account(block_env.coinbase).balance
959+
)
960960
coinbase_balance_after_mining_fee = current_coinbase_balance + U256(
961961
transaction_fee
962962
)
963963
if coinbase_balance_after_mining_fee != 0:
964-
block_env.oracle.set_account_balance(
964+
block_env.get_oracle().set_account_balance(
965965
block_env.coinbase,
966966
coinbase_balance_after_mining_fee,
967967
)
968-
elif block_env.oracle.account_exists_and_is_empty(block_env.coinbase):
969-
block_env.oracle.destroy_account(block_env.coinbase)
968+
elif block_env.get_oracle().account_exists_and_is_empty(
969+
block_env.coinbase
970+
):
971+
block_env.get_oracle().destroy_account(block_env.coinbase)
970972

971973
for address in tx_output.accounts_to_delete:
972-
block_env.oracle.destroy_account(address)
974+
block_env.get_oracle().destroy_account(address)
973975

974976
block_output.block_gas_used += tx_gas_used_after_refund
975977
block_output.blob_gas_used += tx_blob_gas_used
@@ -1009,10 +1011,12 @@ def increase_recipient_balance(recipient: Account) -> None:
10091011
rlp.encode(wd),
10101012
)
10111013

1012-
block_env.oracle.modify_state(wd.address, increase_recipient_balance)
1014+
block_env.get_oracle().modify_state(
1015+
wd.address, increase_recipient_balance
1016+
)
10131017

1014-
if block_env.oracle.account_exists_and_is_empty(wd.address):
1015-
block_env.oracle.destroy_account(wd.address)
1018+
if block_env.get_oracle().account_exists_and_is_empty(wd.address):
1019+
block_env.get_oracle().destroy_account(wd.address)
10161020

10171021

10181022
def check_gas_limit(gas_limit: Uint, parent_gas_limit: Uint) -> bool:

src/ethereum/osaka/utils/message.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ def prepare_message(
5353
if isinstance(tx.to, Bytes0):
5454
current_target = compute_contract_address(
5555
tx_env.origin,
56-
block_env.oracle.get_account(tx_env.origin).nonce - Uint(1),
56+
block_env.get_oracle().get_account(tx_env.origin).nonce - Uint(1),
5757
)
5858
msg_data = Bytes(b"")
5959
code = tx.data
6060
code_address = None
6161
elif isinstance(tx.to, Address):
6262
current_target = tx.to
6363
msg_data = tx.data
64-
code = block_env.oracle.get_account(tx.to).code
64+
code = block_env.get_oracle().get_account(tx.to).code
6565
code_address = tx.to
6666
else:
6767
raise AssertionError("Target must be address or empty bytes")

src/ethereum/osaka/vm/__init__.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@
1414
"""
1515

1616
from dataclasses import dataclass, field
17-
from typing import List, Optional, Set, Tuple
17+
from typing import Any, List, Optional, Set, Tuple
1818

1919
from ethereum_types.bytes import Bytes, Bytes0, Bytes32
2020
from ethereum_types.numeric import U64, U256, Uint
2121

2222
from ethereum.crypto.hash import Hash32
2323
from ethereum.exceptions import EthereumException
24-
from ethereum.state_oracle import MerkleOracle
2524

2625
from ..blocks import Log, Receipt, Withdrawal
2726
from ..fork_types import Address, Authorization, VersionedHash
28-
from ..state import TransientStorage
27+
from ..state import State, TransientStorage
2928
from ..transactions import LegacyTransaction
3029
from ..trie import Trie
3130

@@ -39,7 +38,8 @@ class BlockEnvironment:
3938
"""
4039

4140
chain_id: U64
42-
oracle: MerkleOracle
41+
# TODO: Remove, this is no longer being used. Kept so all tests don't break for now.
42+
state: State
4343
block_gas_limit: Uint
4444
block_hashes: List[Hash32]
4545
coinbase: Address
@@ -50,6 +50,16 @@ class BlockEnvironment:
5050
excess_blob_gas: U64
5151
parent_beacon_block_root: Hash32
5252

53+
def get_oracle(self) -> Any:
54+
"""
55+
Get the state oracle.
56+
57+
Returns the global oracle (raises error if not set).
58+
"""
59+
from ethereum.state_oracle import get_state_oracle
60+
61+
return get_state_oracle()
62+
5363

5464
@dataclass
5565
class BlockOutput:

src/ethereum/osaka/vm/eoa_delegation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def access_delegation(
128128
delegation : `Tuple[bool, Address, Bytes, Uint]`
129129
The delegation address, code, and access gas cost.
130130
"""
131-
oracle = evm.message.block_env.oracle
131+
oracle = evm.message.block_env.get_oracle()
132132
code = oracle.get_account(address).code
133133
if not is_valid_delegation(code):
134134
return False, address, code, Uint(0)
@@ -160,7 +160,7 @@ def set_delegation(message: Message) -> U256:
160160
refund_counter: `U256`
161161
Refund from authority which already exists in state.
162162
"""
163-
oracle = message.block_env.oracle
163+
oracle = message.block_env.get_oracle()
164164
refund_counter = U256(0)
165165
for auth in message.tx_env.authorizations:
166166
if auth.chain_id not in (message.block_env.chain_id, U256(0)):

src/ethereum/osaka/vm/instructions/environment.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def balance(evm: Evm) -> None:
8383
charge_gas(evm, GAS_COLD_ACCOUNT_ACCESS)
8484

8585
# OPERATION
86-
oracle = evm.message.block_env.oracle
86+
oracle = evm.message.block_env.get_oracle()
8787
# Non-existent accounts default to EMPTY_ACCOUNT, which has balance 0.
8888
balance = oracle.get_account(address).balance
8989

@@ -351,7 +351,7 @@ def extcodesize(evm: Evm) -> None:
351351
charge_gas(evm, access_gas_cost)
352352

353353
# OPERATION
354-
oracle = evm.message.block_env.oracle
354+
oracle = evm.message.block_env.get_oracle()
355355
code = oracle.get_account(address).code
356356

357357
codesize = U256(len(code))
@@ -394,7 +394,7 @@ def extcodecopy(evm: Evm) -> None:
394394

395395
# OPERATION
396396
evm.memory += b"\x00" * extend_memory.expand_by
397-
oracle = evm.message.block_env.oracle
397+
oracle = evm.message.block_env.get_oracle()
398398
code = oracle.get_account(address).code
399399

400400
value = buffer_read(code, code_start_index, size)
@@ -481,7 +481,7 @@ def extcodehash(evm: Evm) -> None:
481481
charge_gas(evm, access_gas_cost)
482482

483483
# OPERATION
484-
oracle = evm.message.block_env.oracle
484+
oracle = evm.message.block_env.get_oracle()
485485
account = oracle.get_account(address)
486486

487487
if account == EMPTY_ACCOUNT:
@@ -513,7 +513,7 @@ def self_balance(evm: Evm) -> None:
513513
charge_gas(evm, GAS_FAST_STEP)
514514

515515
# OPERATION
516-
oracle = evm.message.block_env.oracle
516+
oracle = evm.message.block_env.get_oracle()
517517
# Non-existent accounts default to EMPTY_ACCOUNT, which has balance 0.
518518
balance = oracle.get_account(evm.message.current_target).balance
519519

src/ethereum/osaka/vm/instructions/storage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def sload(evm: Evm) -> None:
5151
charge_gas(evm, GAS_COLD_SLOAD)
5252

5353
# OPERATION
54-
oracle = evm.message.block_env.oracle
54+
oracle = evm.message.block_env.get_oracle()
5555
value_bytes = oracle.get_storage(evm.message.current_target, key)
5656
value = U256.from_be_bytes(value_bytes)
5757

@@ -77,7 +77,7 @@ def sstore(evm: Evm) -> None:
7777
if evm.gas_left <= GAS_CALL_STIPEND:
7878
raise OutOfGasError
7979

80-
oracle = evm.message.block_env.oracle
80+
oracle = evm.message.block_env.get_oracle()
8181
original_value_bytes = oracle.get_storage_original(
8282
evm.message.current_target, key
8383
)

src/ethereum/osaka/vm/instructions/system.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def generic_create(
8282
evm.return_data = b""
8383

8484
sender_address = evm.message.current_target
85-
oracle = evm.message.block_env.oracle
85+
oracle = evm.message.block_env.get_oracle()
8686
sender = oracle.get_account(sender_address)
8787

8888
if (
@@ -159,7 +159,7 @@ def create(evm: Evm) -> None:
159159
charge_gas(evm, GAS_CREATE + extend_memory.cost + init_code_gas)
160160

161161
# OPERATION
162-
oracle = evm.message.block_env.oracle
162+
oracle = evm.message.block_env.get_oracle()
163163
evm.memory += b"\x00" * extend_memory.expand_by
164164
contract_address = compute_contract_address(
165165
evm.message.current_target,
@@ -374,7 +374,7 @@ def call(evm: Evm) -> None:
374374
) = access_delegation(evm, code_address)
375375
access_gas_cost += delegated_access_gas_cost
376376

377-
oracle = evm.message.block_env.oracle
377+
oracle = evm.message.block_env.get_oracle()
378378
create_gas_cost = GAS_NEW_ACCOUNT
379379
if value == 0 or oracle.is_account_alive(to):
380380
create_gas_cost = Uint(0)
@@ -471,7 +471,7 @@ def callcode(evm: Evm) -> None:
471471
charge_gas(evm, message_call_gas.cost + extend_memory.cost)
472472

473473
# OPERATION
474-
oracle = evm.message.block_env.oracle
474+
oracle = evm.message.block_env.get_oracle()
475475
evm.memory += b"\x00" * extend_memory.expand_by
476476
sender_balance = oracle.get_account(evm.message.current_target).balance
477477
if sender_balance < value:
@@ -518,7 +518,7 @@ def selfdestruct(evm: Evm) -> None:
518518
evm.accessed_addresses.add(beneficiary)
519519
gas_cost += GAS_COLD_ACCOUNT_ACCESS
520520

521-
oracle = evm.message.block_env.oracle
521+
oracle = evm.message.block_env.get_oracle()
522522
current_balance = oracle.get_account(evm.message.current_target).balance
523523

524524
if not oracle.is_account_alive(beneficiary) and current_balance != 0:

0 commit comments

Comments
 (0)