diff --git a/packages/contract_bootloader/dryrun_syscall_handler.py b/packages/contract_bootloader/dryrun_syscall_handler.py index eca2d235..f11a70dc 100644 --- a/packages/contract_bootloader/dryrun_syscall_handler.py +++ b/packages/contract_bootloader/dryrun_syscall_handler.py @@ -17,10 +17,10 @@ MemorizerFunctionId as HeaderMemorizerFunctionId, MemorizerKey as HeaderMemorizerKey, ) -from contract_bootloader.dryrun_syscall_handler.header_memorizer_handler import ( +from contract_bootloader.dryrun_syscall_handler_memorizer.header_memorizer_handler import ( DryRunHeaderMemorizerHandler, ) -from contract_bootloader.dryrun_syscall_handler.account_memorizer_handler import ( +from contract_bootloader.dryrun_syscall_handler_memorizer.account_memorizer_handler import ( DryRunAccountMemorizerHandler, ) diff --git a/packages/contract_bootloader/dryrun_syscall_handler/__init__.py b/packages/contract_bootloader/dryrun_syscall_handler_memorizer/__init__.py similarity index 100% rename from packages/contract_bootloader/dryrun_syscall_handler/__init__.py rename to packages/contract_bootloader/dryrun_syscall_handler_memorizer/__init__.py diff --git a/packages/contract_bootloader/dryrun_syscall_handler/account_memorizer_handler.py b/packages/contract_bootloader/dryrun_syscall_handler_memorizer/account_memorizer_handler.py similarity index 100% rename from packages/contract_bootloader/dryrun_syscall_handler/account_memorizer_handler.py rename to packages/contract_bootloader/dryrun_syscall_handler_memorizer/account_memorizer_handler.py diff --git a/packages/contract_bootloader/dryrun_syscall_handler/header_memorizer_handler.py b/packages/contract_bootloader/dryrun_syscall_handler_memorizer/header_memorizer_handler.py similarity index 100% rename from packages/contract_bootloader/dryrun_syscall_handler/header_memorizer_handler.py rename to packages/contract_bootloader/dryrun_syscall_handler_memorizer/header_memorizer_handler.py diff --git a/packages/contract_bootloader/syscall_handler.py b/packages/contract_bootloader/syscall_handler.py index 8e7d4d06..ea8afbfb 100644 --- a/packages/contract_bootloader/syscall_handler.py +++ b/packages/contract_bootloader/syscall_handler.py @@ -17,10 +17,10 @@ MemorizerFunctionId as AccountMemorizerFunctionId, MemorizerKey as AccountMemorizerKey, ) -from contract_bootloader.syscall_handler.account_memorizer_handler import ( +from contract_bootloader.syscall_handler_memorizer.account_memorizer_handler import ( AccountMemorizerHandler, ) -from contract_bootloader.syscall_handler.header_memorizer_handler import ( +from contract_bootloader.syscall_handler_memorizer.header_memorizer_handler import ( HeaderMemorizerHandler, ) diff --git a/packages/contract_bootloader/syscall_handler/account_memorizer_handler.py b/packages/contract_bootloader/syscall_handler/account_memorizer_handler.py deleted file mode 100644 index 9a1de520..00000000 --- a/packages/contract_bootloader/syscall_handler/account_memorizer_handler.py +++ /dev/null @@ -1,46 +0,0 @@ -from rlp import decode -from typing import Tuple -from contract_bootloader.memorizer.memorizer import Memorizer -from contract_bootloader.memorizer.account_memorizer import ( - AbstractAccountMemorizerBase, - MemorizerKey, -) -from starkware.cairo.lang.vm.memory_segments import MemorySegmentManager -from tools.py.account import Account -from tools.py.rlp import get_rlp_len -from tools.py.utils import little_8_bytes_chunks_to_bytes - - -class AccountMemorizerHandler(AbstractAccountMemorizerBase): - def __init__(self, segments: MemorySegmentManager, memorizer: Memorizer): - super().__init__(memorizer=memorizer) - self.segments = segments - - def get_balance(self, key: MemorizerKey) -> Tuple[int, int]: - memorizer_value_ptr = self.memorizer.read(key=key.derive()) - - rlp_len = get_rlp_len( - rlp=self.segments.memory[memorizer_value_ptr], item_start_offset=0 - ) - rlp = self._get_felt_range( - start_addr=memorizer_value_ptr, - end_addr=memorizer_value_ptr + (rlp_len + 7) // 8, - ) - - value = decode(little_8_bytes_chunks_to_bytes(rlp, rlp_len), Account).as_dict()[ - "balance" - ] - - return ( - value % 0x100000000000000000000000000000000, - value // 0x100000000000000000000000000000000, - ) - - def get_balance(self, key: MemorizerKey) -> Tuple[int, int]: - pass - - def get_state_root(self, key: MemorizerKey) -> Tuple[int, int]: - pass - - def get_code_hash(self, key: MemorizerKey) -> Tuple[int, int]: - pass diff --git a/packages/contract_bootloader/syscall_handler/header_memorizer_handler.py b/packages/contract_bootloader/syscall_handler/header_memorizer_handler.py deleted file mode 100644 index 9d9ab270..00000000 --- a/packages/contract_bootloader/syscall_handler/header_memorizer_handler.py +++ /dev/null @@ -1,94 +0,0 @@ -from rlp import decode -from typing import Tuple -from contract_bootloader.memorizer.memorizer import Memorizer -from contract_bootloader.memorizer.header_memorizer import ( - AbstractHeaderMemorizerBase, - MemorizerKey, -) -from starkware.cairo.lang.vm.memory_segments import MemorySegmentManager -from tools.py.block_header import BlockHeaderDencun -from tools.py.rlp import get_rlp_len -from tools.py.utils import little_8_bytes_chunks_to_bytes - - -class HeaderMemorizerHandler(AbstractHeaderMemorizerBase): - def __init__(self, segments: MemorySegmentManager, memorizer: Memorizer): - super().__init__(memorizer=memorizer) - self.segments = segments - - def get_parent(self, key: MemorizerKey) -> Tuple[int, int]: - memorizer_value_ptr = self.memorizer.read(key=key.derive()) - - rlp_len = get_rlp_len( - rlp=self.segments.memory[memorizer_value_ptr], item_start_offset=0 - ) - rlp = self._get_felt_range( - start_addr=memorizer_value_ptr, - end_addr=memorizer_value_ptr + (rlp_len + 7) // 8, - ) - - value = decode( - little_8_bytes_chunks_to_bytes(rlp, rlp_len), BlockHeaderDencun - ).as_dict()["parentHash"] - - return ( - value % 0x100000000000000000000000000000000, - value // 0x100000000000000000000000000000000, - ) - - def get_uncle(self, key: MemorizerKey) -> Tuple[int, int]: - pass - - def get_coinbase(self, key: MemorizerKey) -> Tuple[int, int]: - pass - - def get_state_root(self, key: MemorizerKey) -> Tuple[int, int]: - pass - - def get_transaction_root(self, key: MemorizerKey) -> Tuple[int, int]: - pass - - def get_receipt_root(self, key: MemorizerKey) -> Tuple[int, int]: - pass - - def get_bloom(self, key: MemorizerKey) -> Tuple[int, int]: - pass - - def get_difficulty(self, key: MemorizerKey) -> Tuple[int, int]: - pass - - def get_number(self, key: MemorizerKey) -> Tuple[int, int]: - pass - - def get_gas_limit(self, key: MemorizerKey) -> Tuple[int, int]: - pass - - def get_gas_used(self, key: MemorizerKey) -> Tuple[int, int]: - pass - - def get_timestamp(self, key: MemorizerKey) -> Tuple[int, int]: - pass - - def get_extra_data(self, key: MemorizerKey) -> Tuple[int, int]: - pass - - def get_mix_hash(self, key: MemorizerKey) -> Tuple[int, int]: - pass - - def get_nonce(self, key: MemorizerKey) -> Tuple[int, int]: - pass - - def get_base_fee_per_gas(self, key: MemorizerKey) -> Tuple[int, int]: - pass - - def get_withdrawals_root(self, key: MemorizerKey) -> Tuple[int, int]: - pass - - def get_blob_gas_used(self, key: MemorizerKey) -> Tuple[int, int]: - pass - - def get_excess_blob_gas(self, key: MemorizerKey) -> Tuple[int, int]: - pass - - def get_parent_beacon_block_root(self, key: MemorizerKey) -> Tuple[int, int]: - pass diff --git a/packages/contract_bootloader/syscall_handler/__init__.py b/packages/contract_bootloader/syscall_handler_memorizer/__init__.py similarity index 100% rename from packages/contract_bootloader/syscall_handler/__init__.py rename to packages/contract_bootloader/syscall_handler_memorizer/__init__.py diff --git a/packages/contract_bootloader/syscall_handler_memorizer/account_memorizer_handler.py b/packages/contract_bootloader/syscall_handler_memorizer/account_memorizer_handler.py new file mode 100644 index 00000000..760c584c --- /dev/null +++ b/packages/contract_bootloader/syscall_handler_memorizer/account_memorizer_handler.py @@ -0,0 +1,97 @@ +from rlp import decode +from typing import Tuple +from contract_bootloader.memorizer.memorizer import Memorizer +from contract_bootloader.memorizer.account_memorizer import ( + AbstractAccountMemorizerBase, + MemorizerKey, +) +from starkware.cairo.lang.vm.memory_segments import MemorySegmentManager +from tools.py.account import Account +from tools.py.rlp import get_rlp_len +from tools.py.utils import little_8_bytes_chunks_to_bytes + + +class AccountMemorizerHandler(AbstractAccountMemorizerBase): + def __init__(self, segments: MemorySegmentManager, memorizer: Memorizer): + super().__init__(memorizer=memorizer) + self.segments = segments + + def get_nonce(self, key: MemorizerKey) -> Tuple[int, int]: + memorizer_value_ptr = self.memorizer.read(key=key.derive()) + + rlp_len = get_rlp_len( + rlp=self.segments.memory[memorizer_value_ptr], item_start_offset=0 + ) + rlp = self._get_felt_range( + start_addr=memorizer_value_ptr, + end_addr=memorizer_value_ptr + (rlp_len + 7) // 8, + ) + + value = decode(little_8_bytes_chunks_to_bytes(rlp, rlp_len), Account).as_dict()[ + "nonce" + ] + + return ( + value % 0x100000000000000000000000000000000, + value // 0x100000000000000000000000000000000, + ) + + def get_balance(self, key: MemorizerKey) -> Tuple[int, int]: + memorizer_value_ptr = self.memorizer.read(key=key.derive()) + + rlp_len = get_rlp_len( + rlp=self.segments.memory[memorizer_value_ptr], item_start_offset=0 + ) + rlp = self._get_felt_range( + start_addr=memorizer_value_ptr, + end_addr=memorizer_value_ptr + (rlp_len + 7) // 8, + ) + + value = decode(little_8_bytes_chunks_to_bytes(rlp, rlp_len), Account).as_dict()[ + "balance" + ] + + return ( + value % 0x100000000000000000000000000000000, + value // 0x100000000000000000000000000000000, + ) + + def get_state_root(self, key: MemorizerKey) -> Tuple[int, int]: + memorizer_value_ptr = self.memorizer.read(key=key.derive()) + + rlp_len = get_rlp_len( + rlp=self.segments.memory[memorizer_value_ptr], item_start_offset=0 + ) + rlp = self._get_felt_range( + start_addr=memorizer_value_ptr, + end_addr=memorizer_value_ptr + (rlp_len + 7) // 8, + ) + + value = decode(little_8_bytes_chunks_to_bytes(rlp, rlp_len), Account).as_dict()[ + "storageRoot" + ] + + return ( + value % 0x100000000000000000000000000000000, + value // 0x100000000000000000000000000000000, + ) + + def get_code_hash(self, key: MemorizerKey) -> Tuple[int, int]: + memorizer_value_ptr = self.memorizer.read(key=key.derive()) + + rlp_len = get_rlp_len( + rlp=self.segments.memory[memorizer_value_ptr], item_start_offset=0 + ) + rlp = self._get_felt_range( + start_addr=memorizer_value_ptr, + end_addr=memorizer_value_ptr + (rlp_len + 7) // 8, + ) + + value = decode(little_8_bytes_chunks_to_bytes(rlp, rlp_len), Account).as_dict()[ + "codeHash" + ] + + return ( + value % 0x100000000000000000000000000000000, + value // 0x100000000000000000000000000000000, + ) diff --git a/packages/contract_bootloader/syscall_handler_memorizer/header_memorizer_handler.py b/packages/contract_bootloader/syscall_handler_memorizer/header_memorizer_handler.py new file mode 100644 index 00000000..eb1cd381 --- /dev/null +++ b/packages/contract_bootloader/syscall_handler_memorizer/header_memorizer_handler.py @@ -0,0 +1,315 @@ +from rlp import decode +from typing import Tuple +from contract_bootloader.memorizer.memorizer import Memorizer +from contract_bootloader.memorizer.header_memorizer import ( + AbstractHeaderMemorizerBase, + MemorizerKey, +) +from starkware.cairo.lang.vm.memory_segments import MemorySegmentManager +from tools.py.block_header import BlockHeaderDencun +from tools.py.rlp import get_rlp_len +from tools.py.utils import little_8_bytes_chunks_to_bytes + + +class HeaderMemorizerHandler(AbstractHeaderMemorizerBase): + def __init__(self, segments: MemorySegmentManager, memorizer: Memorizer): + super().__init__(memorizer=memorizer) + self.segments = segments + + def get_parent(self, key: MemorizerKey) -> Tuple[int, int]: + memorizer_value_ptr = self.memorizer.read(key=key.derive()) + + rlp_len = get_rlp_len( + rlp=self.segments.memory[memorizer_value_ptr], item_start_offset=0 + ) + rlp = self._get_felt_range( + start_addr=memorizer_value_ptr, + end_addr=memorizer_value_ptr + (rlp_len + 7) // 8, + ) + + value = decode( + little_8_bytes_chunks_to_bytes(rlp, rlp_len), BlockHeaderDencun + ).as_dict()["parentHash"] + + return ( + value % 0x100000000000000000000000000000000, + value // 0x100000000000000000000000000000000, + ) + + def get_uncle(self, key: MemorizerKey) -> Tuple[int, int]: + memorizer_value_ptr = self.memorizer.read(key=key.derive()) + + rlp_len = get_rlp_len( + rlp=self.segments.memory[memorizer_value_ptr], item_start_offset=0 + ) + rlp = self._get_felt_range( + start_addr=memorizer_value_ptr, + end_addr=memorizer_value_ptr + (rlp_len + 7) // 8, + ) + + value = decode( + little_8_bytes_chunks_to_bytes(rlp, rlp_len), BlockHeaderDencun + ).as_dict()["unclesHash"] + + return ( + value % 0x100000000000000000000000000000000, + value // 0x100000000000000000000000000000000, + ) + + def get_coinbase(self, key: MemorizerKey) -> Tuple[int, int]: + memorizer_value_ptr = self.memorizer.read(key=key.derive()) + + rlp_len = get_rlp_len( + rlp=self.segments.memory[memorizer_value_ptr], item_start_offset=0 + ) + rlp = self._get_felt_range( + start_addr=memorizer_value_ptr, + end_addr=memorizer_value_ptr + (rlp_len + 7) // 8, + ) + + value = decode( + little_8_bytes_chunks_to_bytes(rlp, rlp_len), BlockHeaderDencun + ).as_dict()["coinbase"] + + return ( + value % 0x100000000000000000000000000000000, + value // 0x100000000000000000000000000000000, + ) + + def get_state_root(self, key: MemorizerKey) -> Tuple[int, int]: + memorizer_value_ptr = self.memorizer.read(key=key.derive()) + + rlp_len = get_rlp_len( + rlp=self.segments.memory[memorizer_value_ptr], item_start_offset=0 + ) + rlp = self._get_felt_range( + start_addr=memorizer_value_ptr, + end_addr=memorizer_value_ptr + (rlp_len + 7) // 8, + ) + + value = decode( + little_8_bytes_chunks_to_bytes(rlp, rlp_len), BlockHeaderDencun + ).as_dict()["stateRoot"] + + return ( + value % 0x100000000000000000000000000000000, + value // 0x100000000000000000000000000000000, + ) + + def get_transaction_root(self, key: MemorizerKey) -> Tuple[int, int]: + memorizer_value_ptr = self.memorizer.read(key=key.derive()) + + rlp_len = get_rlp_len( + rlp=self.segments.memory[memorizer_value_ptr], item_start_offset=0 + ) + rlp = self._get_felt_range( + start_addr=memorizer_value_ptr, + end_addr=memorizer_value_ptr + (rlp_len + 7) // 8, + ) + + value = decode( + little_8_bytes_chunks_to_bytes(rlp, rlp_len), BlockHeaderDencun + ).as_dict()["transactionsRoot"] + + return ( + value % 0x100000000000000000000000000000000, + value // 0x100000000000000000000000000000000, + ) + + def get_receipt_root(self, key: MemorizerKey) -> Tuple[int, int]: + memorizer_value_ptr = self.memorizer.read(key=key.derive()) + + rlp_len = get_rlp_len( + rlp=self.segments.memory[memorizer_value_ptr], item_start_offset=0 + ) + rlp = self._get_felt_range( + start_addr=memorizer_value_ptr, + end_addr=memorizer_value_ptr + (rlp_len + 7) // 8, + ) + + value = decode( + little_8_bytes_chunks_to_bytes(rlp, rlp_len), BlockHeaderDencun + ).as_dict()["receiptsRoot"] + + return ( + value % 0x100000000000000000000000000000000, + value // 0x100000000000000000000000000000000, + ) + + def get_bloom(self, key: MemorizerKey) -> Tuple[int, int]: + pass + + def get_difficulty(self, key: MemorizerKey) -> Tuple[int, int]: + memorizer_value_ptr = self.memorizer.read(key=key.derive()) + + rlp_len = get_rlp_len( + rlp=self.segments.memory[memorizer_value_ptr], item_start_offset=0 + ) + rlp = self._get_felt_range( + start_addr=memorizer_value_ptr, + end_addr=memorizer_value_ptr + (rlp_len + 7) // 8, + ) + + value = decode( + little_8_bytes_chunks_to_bytes(rlp, rlp_len), BlockHeaderDencun + ).as_dict()["difficulty"] + + return ( + value % 0x100000000000000000000000000000000, + value // 0x100000000000000000000000000000000, + ) + + def get_number(self, key: MemorizerKey) -> Tuple[int, int]: + memorizer_value_ptr = self.memorizer.read(key=key.derive()) + + rlp_len = get_rlp_len( + rlp=self.segments.memory[memorizer_value_ptr], item_start_offset=0 + ) + rlp = self._get_felt_range( + start_addr=memorizer_value_ptr, + end_addr=memorizer_value_ptr + (rlp_len + 7) // 8, + ) + + value = decode( + little_8_bytes_chunks_to_bytes(rlp, rlp_len), BlockHeaderDencun + ).as_dict()["number"] + + return ( + value % 0x100000000000000000000000000000000, + value // 0x100000000000000000000000000000000, + ) + + def get_gas_limit(self, key: MemorizerKey) -> Tuple[int, int]: + memorizer_value_ptr = self.memorizer.read(key=key.derive()) + + rlp_len = get_rlp_len( + rlp=self.segments.memory[memorizer_value_ptr], item_start_offset=0 + ) + rlp = self._get_felt_range( + start_addr=memorizer_value_ptr, + end_addr=memorizer_value_ptr + (rlp_len + 7) // 8, + ) + + value = decode( + little_8_bytes_chunks_to_bytes(rlp, rlp_len), BlockHeaderDencun + ).as_dict()["gasLimit"] + + return ( + value % 0x100000000000000000000000000000000, + value // 0x100000000000000000000000000000000, + ) + + def get_gas_used(self, key: MemorizerKey) -> Tuple[int, int]: + memorizer_value_ptr = self.memorizer.read(key=key.derive()) + + rlp_len = get_rlp_len( + rlp=self.segments.memory[memorizer_value_ptr], item_start_offset=0 + ) + rlp = self._get_felt_range( + start_addr=memorizer_value_ptr, + end_addr=memorizer_value_ptr + (rlp_len + 7) // 8, + ) + + value = decode( + little_8_bytes_chunks_to_bytes(rlp, rlp_len), BlockHeaderDencun + ).as_dict()["gasUsed"] + + return ( + value % 0x100000000000000000000000000000000, + value // 0x100000000000000000000000000000000, + ) + + def get_timestamp(self, key: MemorizerKey) -> Tuple[int, int]: + memorizer_value_ptr = self.memorizer.read(key=key.derive()) + + rlp_len = get_rlp_len( + rlp=self.segments.memory[memorizer_value_ptr], item_start_offset=0 + ) + rlp = self._get_felt_range( + start_addr=memorizer_value_ptr, + end_addr=memorizer_value_ptr + (rlp_len + 7) // 8, + ) + + value = decode( + little_8_bytes_chunks_to_bytes(rlp, rlp_len), BlockHeaderDencun + ).as_dict()["timestamp"] + + return ( + value % 0x100000000000000000000000000000000, + value // 0x100000000000000000000000000000000, + ) + + def get_extra_data(self, key: MemorizerKey) -> Tuple[int, int]: + pass + + def get_mix_hash(self, key: MemorizerKey) -> Tuple[int, int]: + memorizer_value_ptr = self.memorizer.read(key=key.derive()) + + rlp_len = get_rlp_len( + rlp=self.segments.memory[memorizer_value_ptr], item_start_offset=0 + ) + rlp = self._get_felt_range( + start_addr=memorizer_value_ptr, + end_addr=memorizer_value_ptr + (rlp_len + 7) // 8, + ) + + value = decode( + little_8_bytes_chunks_to_bytes(rlp, rlp_len), BlockHeaderDencun + ).as_dict()["mixHash"] + + return ( + value % 0x100000000000000000000000000000000, + value // 0x100000000000000000000000000000000, + ) + + def get_nonce(self, key: MemorizerKey) -> Tuple[int, int]: + memorizer_value_ptr = self.memorizer.read(key=key.derive()) + + rlp_len = get_rlp_len( + rlp=self.segments.memory[memorizer_value_ptr], item_start_offset=0 + ) + rlp = self._get_felt_range( + start_addr=memorizer_value_ptr, + end_addr=memorizer_value_ptr + (rlp_len + 7) // 8, + ) + + value = decode( + little_8_bytes_chunks_to_bytes(rlp, rlp_len), BlockHeaderDencun + ).as_dict()["nonce"] + + return ( + value % 0x100000000000000000000000000000000, + value // 0x100000000000000000000000000000000, + ) + + def get_base_fee_per_gas(self, key: MemorizerKey) -> Tuple[int, int]: + memorizer_value_ptr = self.memorizer.read(key=key.derive()) + + rlp_len = get_rlp_len( + rlp=self.segments.memory[memorizer_value_ptr], item_start_offset=0 + ) + rlp = self._get_felt_range( + start_addr=memorizer_value_ptr, + end_addr=memorizer_value_ptr + (rlp_len + 7) // 8, + ) + + value = decode( + little_8_bytes_chunks_to_bytes(rlp, rlp_len), BlockHeaderDencun + ).as_dict()["baseFeePerGas"] + + return ( + value % 0x100000000000000000000000000000000, + value // 0x100000000000000000000000000000000, + ) + + def get_withdrawals_root(self, key: MemorizerKey) -> Tuple[int, int]: + pass + + def get_blob_gas_used(self, key: MemorizerKey) -> Tuple[int, int]: + pass + + def get_excess_blob_gas(self, key: MemorizerKey) -> Tuple[int, int]: + pass + + def get_parent_beacon_block_root(self, key: MemorizerKey) -> Tuple[int, int]: + pass