From d9255a439909bfdf2934349c58bcb4f984c52851 Mon Sep 17 00:00:00 2001 From: Sam Wilson Date: Tue, 12 Nov 2024 10:36:02 -0500 Subject: [PATCH] Switch to ethereum rlp --- setup.cfg | 1 + src/ethereum/arrow_glacier/fork.py | 15 +- src/ethereum/arrow_glacier/fork_types.py | 2 +- src/ethereum/arrow_glacier/transactions.py | 2 +- src/ethereum/arrow_glacier/trie.py | 2 +- src/ethereum/arrow_glacier/utils/address.py | 2 +- src/ethereum/berlin/fork.py | 15 +- src/ethereum/berlin/fork_types.py | 2 +- src/ethereum/berlin/transactions.py | 2 +- src/ethereum/berlin/trie.py | 2 +- src/ethereum/berlin/utils/address.py | 2 +- src/ethereum/byzantium/fork.py | 15 +- src/ethereum/byzantium/fork_types.py | 2 +- src/ethereum/byzantium/transactions.py | 2 +- src/ethereum/byzantium/trie.py | 2 +- src/ethereum/byzantium/utils/address.py | 2 +- src/ethereum/cancun/fork.py | 2 +- src/ethereum/cancun/fork_types.py | 2 +- src/ethereum/cancun/transactions.py | 2 +- src/ethereum/cancun/trie.py | 2 +- src/ethereum/cancun/utils/address.py | 2 +- src/ethereum/constantinople/fork.py | 15 +- src/ethereum/constantinople/fork_types.py | 2 +- src/ethereum/constantinople/transactions.py | 2 +- src/ethereum/constantinople/trie.py | 2 +- src/ethereum/constantinople/utils/address.py | 2 +- src/ethereum/dao_fork/fork.py | 15 +- src/ethereum/dao_fork/fork_types.py | 2 +- src/ethereum/dao_fork/transactions.py | 2 +- src/ethereum/dao_fork/trie.py | 2 +- src/ethereum/dao_fork/utils/address.py | 2 +- src/ethereum/ethash.py | 6 +- src/ethereum/exceptions.py | 12 - src/ethereum/frontier/fork.py | 15 +- src/ethereum/frontier/fork_types.py | 2 +- src/ethereum/frontier/transactions.py | 2 +- src/ethereum/frontier/trie.py | 2 +- src/ethereum/frontier/utils/address.py | 2 +- src/ethereum/genesis.py | 6 +- src/ethereum/gray_glacier/fork.py | 15 +- src/ethereum/gray_glacier/fork_types.py | 2 +- src/ethereum/gray_glacier/transactions.py | 2 +- src/ethereum/gray_glacier/trie.py | 2 +- src/ethereum/gray_glacier/utils/address.py | 2 +- src/ethereum/homestead/fork.py | 15 +- src/ethereum/homestead/fork_types.py | 2 +- src/ethereum/homestead/transactions.py | 2 +- src/ethereum/homestead/trie.py | 2 +- src/ethereum/homestead/utils/address.py | 2 +- src/ethereum/istanbul/fork.py | 15 +- src/ethereum/istanbul/fork_types.py | 2 +- src/ethereum/istanbul/transactions.py | 2 +- src/ethereum/istanbul/trie.py | 2 +- src/ethereum/istanbul/utils/address.py | 2 +- src/ethereum/london/fork.py | 15 +- src/ethereum/london/fork_types.py | 2 +- src/ethereum/london/transactions.py | 2 +- src/ethereum/london/trie.py | 2 +- src/ethereum/london/utils/address.py | 2 +- src/ethereum/muir_glacier/fork.py | 15 +- src/ethereum/muir_glacier/fork_types.py | 2 +- src/ethereum/muir_glacier/transactions.py | 2 +- src/ethereum/muir_glacier/trie.py | 2 +- src/ethereum/muir_glacier/utils/address.py | 2 +- src/ethereum/paris/fork.py | 2 +- src/ethereum/paris/fork_types.py | 2 +- src/ethereum/paris/transactions.py | 2 +- src/ethereum/paris/trie.py | 2 +- src/ethereum/paris/utils/address.py | 2 +- src/ethereum/rlp.py | 573 ------------------ src/ethereum/shanghai/fork.py | 2 +- src/ethereum/shanghai/fork_types.py | 2 +- src/ethereum/shanghai/transactions.py | 2 +- src/ethereum/shanghai/trie.py | 2 +- src/ethereum/shanghai/utils/address.py | 2 +- src/ethereum/spurious_dragon/fork.py | 15 +- src/ethereum/spurious_dragon/fork_types.py | 2 +- src/ethereum/spurious_dragon/transactions.py | 2 +- src/ethereum/spurious_dragon/trie.py | 2 +- src/ethereum/spurious_dragon/utils/address.py | 2 +- src/ethereum/tangerine_whistle/fork.py | 15 +- src/ethereum/tangerine_whistle/fork_types.py | 2 +- .../tangerine_whistle/transactions.py | 2 +- src/ethereum/tangerine_whistle/trie.py | 2 +- .../tangerine_whistle/utils/address.py | 2 +- .../evm_tools/b11r/__init__.py | 5 +- .../evm_tools/b11r/b11r_types.py | 2 +- .../evm_tools/loaders/fixture_loader.py | 6 +- .../evm_tools/loaders/transaction_loader.py | 2 +- .../evm_tools/t8n/__init__.py | 3 +- src/ethereum_spec_tools/evm_tools/t8n/env.py | 2 +- .../evm_tools/t8n/t8n_types.py | 2 +- tests/berlin/test_ethash.py | 2 +- tests/berlin/test_rlp.py | 2 +- tests/berlin/test_state_transition.py | 6 +- tests/berlin/test_transaction.py | 2 +- tests/byzantium/test_ethash.py | 2 +- tests/byzantium/test_rlp.py | 2 +- tests/byzantium/test_state_transition.py | 6 +- tests/byzantium/test_transaction.py | 2 +- tests/cancun/test_rlp.py | 2 +- tests/constantinople/test_ethash.py | 2 +- tests/constantinople/test_rlp.py | 2 +- tests/constantinople/test_state_transition.py | 6 +- tests/constantinople/test_transaction.py | 2 +- tests/frontier/test_ethash.py | 2 +- tests/frontier/test_rlp.py | 2 +- tests/frontier/test_state_transition.py | 6 +- tests/frontier/test_transaction.py | 2 +- tests/helpers/load_state_tests.py | 12 +- tests/helpers/load_vm_tests.py | 2 +- tests/homestead/test_ethash.py | 2 +- tests/homestead/test_rlp.py | 2 +- tests/homestead/test_state_transition.py | 6 +- tests/homestead/test_transaction.py | 2 +- tests/istanbul/test_ethash.py | 2 +- tests/istanbul/test_rlp.py | 2 +- tests/istanbul/test_state_transition.py | 6 +- tests/istanbul/test_transaction.py | 2 +- tests/london/test_rlp.py | 2 +- tests/london/test_state_transition.py | 6 +- tests/london/test_transaction.py | 2 +- tests/paris/test_rlp.py | 2 +- tests/paris/test_state_transition.py | 6 +- tests/shanghai/test_rlp.py | 2 +- tests/spurious_dragon/test_ethash.py | 2 +- tests/spurious_dragon/test_rlp.py | 2 +- .../spurious_dragon/test_state_transition.py | 6 +- tests/spurious_dragon/test_transaction.py | 2 +- tests/tangerine_whistle/test_ethash.py | 2 +- tests/tangerine_whistle/test_rlp.py | 2 +- .../test_state_transition.py | 6 +- tests/tangerine_whistle/test_transaction.py | 2 +- tests/test_genesis.py | 2 +- tests/test_rlp.py | 337 +--------- 135 files changed, 261 insertions(+), 1159 deletions(-) delete mode 100644 src/ethereum/rlp.py diff --git a/setup.cfg b/setup.cfg index 730939c928..bee1da28ac 100644 --- a/setup.cfg +++ b/setup.cfg @@ -116,6 +116,7 @@ install_requires = typing_extensions>=4 py_ecc @ git+https://github.com/petertdavies/py_ecc.git@127184f4c57b1812da959586d0fe8f43bb1a2389 ethereum-types>=0.2.1,<0.3 + ethereum-rlp>=0.1.1,<0.2 [options.package_data] ethereum = diff --git a/src/ethereum/arrow_glacier/fork.py b/src/ethereum/arrow_glacier/fork.py index 8b24973a19..2108a5fba7 100644 --- a/src/ethereum/arrow_glacier/fork.py +++ b/src/ethereum/arrow_glacier/fork.py @@ -15,6 +15,7 @@ from dataclasses import dataclass from typing import List, Optional, Set, Tuple, Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.numeric import U64, U256, Uint @@ -22,7 +23,6 @@ from ethereum.ethash import dataset_size, generate_cache, hashimoto_light from ethereum.exceptions import InvalidBlock, InvalidSenderError -from .. import rlp from . import vm from .blocks import Block, Header, Log, Receipt from .bloom import logs_bloom @@ -351,7 +351,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32: header.base_fee_per_gas, ) - return rlp.rlp_hash(header_data_without_pow_artefacts) + return keccak256(rlp.encode(header_data_without_pow_artefacts)) def validate_proof_of_work(header: Header) -> None: @@ -650,8 +650,8 @@ def validate_ommers( chain : History and current state. """ - block_hash = rlp.rlp_hash(block_header) - if rlp.rlp_hash(ommers) != block_header.ommers_hash: + block_hash = keccak256(rlp.encode(block_header)) + if keccak256(rlp.encode(ommers)) != block_header.ommers_hash: raise InvalidBlock if len(ommers) == 0: @@ -669,18 +669,19 @@ def validate_ommers( if len(ommers) > 2: raise InvalidBlock - ommers_hashes = [rlp.rlp_hash(ommer) for ommer in ommers] + ommers_hashes = [keccak256(rlp.encode(ommer)) for ommer in ommers] if len(ommers_hashes) != len(set(ommers_hashes)): raise InvalidBlock recent_canonical_blocks = chain.blocks[-(MAX_OMMER_DEPTH + Uint(1)) :] recent_canonical_block_hashes = { - rlp.rlp_hash(block.header) for block in recent_canonical_blocks + keccak256(rlp.encode(block.header)) + for block in recent_canonical_blocks } recent_ommers_hashes: Set[Hash32] = set() for block in recent_canonical_blocks: recent_ommers_hashes = recent_ommers_hashes.union( - {rlp.rlp_hash(ommer) for ommer in block.ommers} + {keccak256(rlp.encode(ommer)) for ommer in block.ommers} ) for ommer_index, ommer in enumerate(ommers): diff --git a/src/ethereum/arrow_glacier/fork_types.py b/src/ethereum/arrow_glacier/fork_types.py index 05f34f691b..00817ba506 100644 --- a/src/ethereum/arrow_glacier/fork_types.py +++ b/src/ethereum/arrow_glacier/fork_types.py @@ -14,11 +14,11 @@ from dataclasses import dataclass +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes20, Bytes256 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint -from .. import rlp from ..crypto.hash import Hash32, keccak256 Address = Bytes20 diff --git a/src/ethereum/arrow_glacier/transactions.py b/src/ethereum/arrow_glacier/transactions.py index 10f52ad908..9d9bb6bd17 100644 --- a/src/ethereum/arrow_glacier/transactions.py +++ b/src/ethereum/arrow_glacier/transactions.py @@ -6,6 +6,7 @@ from dataclasses import dataclass from typing import Tuple, Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes32 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U64, U256, Uint @@ -14,7 +15,6 @@ from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidSignatureError -from .. import rlp from .exceptions import TransactionTypeError from .fork_types import Address diff --git a/src/ethereum/arrow_glacier/trie.py b/src/ethereum/arrow_glacier/trie.py index 069b8127e2..476a61ab8f 100644 --- a/src/ethereum/arrow_glacier/trie.py +++ b/src/ethereum/arrow_glacier/trie.py @@ -28,6 +28,7 @@ Union, ) +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint @@ -36,7 +37,6 @@ from ethereum.london import trie as previous_trie from ethereum.utils.hexadecimal import hex_to_bytes -from .. import rlp from .blocks import Receipt from .fork_types import Account, Address, Root, encode_account from .transactions import LegacyTransaction diff --git a/src/ethereum/arrow_glacier/utils/address.py b/src/ethereum/arrow_glacier/utils/address.py index 7bd7f1e1bf..a19077d4ea 100644 --- a/src/ethereum/arrow_glacier/utils/address.py +++ b/src/ethereum/arrow_glacier/utils/address.py @@ -14,13 +14,13 @@ """ from typing import Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes32 from ethereum_types.numeric import U256, Uint from ethereum.crypto.hash import keccak256 from ethereum.utils.byte import left_pad_zero_bytes -from ... import rlp from ..fork_types import Address diff --git a/src/ethereum/berlin/fork.py b/src/ethereum/berlin/fork.py index a3bdea1a00..8ec009f424 100644 --- a/src/ethereum/berlin/fork.py +++ b/src/ethereum/berlin/fork.py @@ -15,6 +15,7 @@ from dataclasses import dataclass from typing import List, Optional, Set, Tuple, Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.numeric import U64, U256, Uint @@ -22,7 +23,6 @@ from ethereum.ethash import dataset_size, generate_cache, hashimoto_light from ethereum.exceptions import InvalidBlock, InvalidSenderError -from .. import rlp from . import vm from .blocks import Block, Header, Log, Receipt from .bloom import logs_bloom @@ -274,7 +274,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32: header.extra_data, ) - return rlp.rlp_hash(header_data_without_pow_artefacts) + return keccak256(rlp.encode(header_data_without_pow_artefacts)) def validate_proof_of_work(header: Header) -> None: @@ -544,8 +544,8 @@ def validate_ommers( chain : History and current state. """ - block_hash = rlp.rlp_hash(block_header) - if rlp.rlp_hash(ommers) != block_header.ommers_hash: + block_hash = keccak256(rlp.encode(block_header)) + if keccak256(rlp.encode(ommers)) != block_header.ommers_hash: raise InvalidBlock if len(ommers) == 0: @@ -563,18 +563,19 @@ def validate_ommers( if len(ommers) > 2: raise InvalidBlock - ommers_hashes = [rlp.rlp_hash(ommer) for ommer in ommers] + ommers_hashes = [keccak256(rlp.encode(ommer)) for ommer in ommers] if len(ommers_hashes) != len(set(ommers_hashes)): raise InvalidBlock recent_canonical_blocks = chain.blocks[-(MAX_OMMER_DEPTH + Uint(1)) :] recent_canonical_block_hashes = { - rlp.rlp_hash(block.header) for block in recent_canonical_blocks + keccak256(rlp.encode(block.header)) + for block in recent_canonical_blocks } recent_ommers_hashes: Set[Hash32] = set() for block in recent_canonical_blocks: recent_ommers_hashes = recent_ommers_hashes.union( - {rlp.rlp_hash(ommer) for ommer in block.ommers} + {keccak256(rlp.encode(ommer)) for ommer in block.ommers} ) for ommer_index, ommer in enumerate(ommers): diff --git a/src/ethereum/berlin/fork_types.py b/src/ethereum/berlin/fork_types.py index 05f34f691b..00817ba506 100644 --- a/src/ethereum/berlin/fork_types.py +++ b/src/ethereum/berlin/fork_types.py @@ -14,11 +14,11 @@ from dataclasses import dataclass +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes20, Bytes256 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint -from .. import rlp from ..crypto.hash import Hash32, keccak256 Address = Bytes20 diff --git a/src/ethereum/berlin/transactions.py b/src/ethereum/berlin/transactions.py index 17c941886c..fcc54d0485 100644 --- a/src/ethereum/berlin/transactions.py +++ b/src/ethereum/berlin/transactions.py @@ -6,6 +6,7 @@ from dataclasses import dataclass from typing import Tuple, Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes32 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U64, U256, Uint @@ -14,7 +15,6 @@ from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidSignatureError -from .. import rlp from .exceptions import TransactionTypeError from .fork_types import Address diff --git a/src/ethereum/berlin/trie.py b/src/ethereum/berlin/trie.py index 5ab348f521..74068d6331 100644 --- a/src/ethereum/berlin/trie.py +++ b/src/ethereum/berlin/trie.py @@ -28,6 +28,7 @@ Union, ) +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint @@ -36,7 +37,6 @@ from ethereum.muir_glacier import trie as previous_trie from ethereum.utils.hexadecimal import hex_to_bytes -from .. import rlp from .blocks import Receipt from .fork_types import Account, Address, Root, encode_account from .transactions import LegacyTransaction diff --git a/src/ethereum/berlin/utils/address.py b/src/ethereum/berlin/utils/address.py index e1c01739ca..c77bd34d99 100644 --- a/src/ethereum/berlin/utils/address.py +++ b/src/ethereum/berlin/utils/address.py @@ -14,13 +14,13 @@ """ from typing import Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes32 from ethereum_types.numeric import U256, Uint from ethereum.crypto.hash import keccak256 from ethereum.utils.byte import left_pad_zero_bytes -from ... import rlp from ..fork_types import Address diff --git a/src/ethereum/byzantium/fork.py b/src/ethereum/byzantium/fork.py index fad24adba7..68001e3ee1 100644 --- a/src/ethereum/byzantium/fork.py +++ b/src/ethereum/byzantium/fork.py @@ -15,6 +15,7 @@ from dataclasses import dataclass from typing import List, Optional, Set, Tuple +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.numeric import U64, U256, Uint @@ -22,7 +23,6 @@ from ethereum.ethash import dataset_size, generate_cache, hashimoto_light from ethereum.exceptions import InvalidBlock, InvalidSenderError -from .. import rlp from . import vm from .blocks import Block, Header, Log, Receipt from .bloom import logs_bloom @@ -270,7 +270,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32: header.extra_data, ) - return rlp.rlp_hash(header_data_without_pow_artefacts) + return keccak256(rlp.encode(header_data_without_pow_artefacts)) def validate_proof_of_work(header: Header) -> None: @@ -534,8 +534,8 @@ def validate_ommers( chain : History and current state. """ - block_hash = rlp.rlp_hash(block_header) - if rlp.rlp_hash(ommers) != block_header.ommers_hash: + block_hash = keccak256(rlp.encode(block_header)) + if keccak256(rlp.encode(ommers)) != block_header.ommers_hash: raise InvalidBlock if len(ommers) == 0: @@ -553,18 +553,19 @@ def validate_ommers( if len(ommers) > 2: raise InvalidBlock - ommers_hashes = [rlp.rlp_hash(ommer) for ommer in ommers] + ommers_hashes = [keccak256(rlp.encode(ommer)) for ommer in ommers] if len(ommers_hashes) != len(set(ommers_hashes)): raise InvalidBlock recent_canonical_blocks = chain.blocks[-(MAX_OMMER_DEPTH + Uint(1)) :] recent_canonical_block_hashes = { - rlp.rlp_hash(block.header) for block in recent_canonical_blocks + keccak256(rlp.encode(block.header)) + for block in recent_canonical_blocks } recent_ommers_hashes: Set[Hash32] = set() for block in recent_canonical_blocks: recent_ommers_hashes = recent_ommers_hashes.union( - {rlp.rlp_hash(ommer) for ommer in block.ommers} + {keccak256(rlp.encode(ommer)) for ommer in block.ommers} ) for ommer_index, ommer in enumerate(ommers): diff --git a/src/ethereum/byzantium/fork_types.py b/src/ethereum/byzantium/fork_types.py index fbf239a3f6..328a2779ee 100644 --- a/src/ethereum/byzantium/fork_types.py +++ b/src/ethereum/byzantium/fork_types.py @@ -14,11 +14,11 @@ from dataclasses import dataclass +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes20, Bytes256 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint -from .. import rlp from ..crypto.hash import Hash32, keccak256 Address = Bytes20 diff --git a/src/ethereum/byzantium/transactions.py b/src/ethereum/byzantium/transactions.py index da6aff2c1b..142929587d 100644 --- a/src/ethereum/byzantium/transactions.py +++ b/src/ethereum/byzantium/transactions.py @@ -6,6 +6,7 @@ from dataclasses import dataclass from typing import Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U64, U256, Uint @@ -14,7 +15,6 @@ from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidSignatureError -from .. import rlp from .fork_types import Address TX_BASE_COST = 21000 diff --git a/src/ethereum/byzantium/trie.py b/src/ethereum/byzantium/trie.py index 9d2a8f256e..a8e2890907 100644 --- a/src/ethereum/byzantium/trie.py +++ b/src/ethereum/byzantium/trie.py @@ -28,6 +28,7 @@ Union, ) +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint @@ -36,7 +37,6 @@ from ethereum.spurious_dragon import trie as previous_trie from ethereum.utils.hexadecimal import hex_to_bytes -from .. import rlp from .blocks import Receipt from .fork_types import Account, Address, Root, encode_account from .transactions import Transaction diff --git a/src/ethereum/byzantium/utils/address.py b/src/ethereum/byzantium/utils/address.py index f175816214..386c13bff2 100644 --- a/src/ethereum/byzantium/utils/address.py +++ b/src/ethereum/byzantium/utils/address.py @@ -14,12 +14,12 @@ """ from typing import Union +from ethereum_rlp import rlp from ethereum_types.numeric import U256, Uint from ethereum.crypto.hash import keccak256 from ethereum.utils.byte import left_pad_zero_bytes -from ... import rlp from ..fork_types import Address diff --git a/src/ethereum/cancun/fork.py b/src/ethereum/cancun/fork.py index ada6431cf3..531de2ed5a 100644 --- a/src/ethereum/cancun/fork.py +++ b/src/ethereum/cancun/fork.py @@ -15,13 +15,13 @@ from dataclasses import dataclass from typing import List, Optional, Tuple, Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes32 from ethereum_types.numeric import U64, U256, Uint from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidBlock, InvalidSenderError -from .. import rlp from . import vm from .blocks import Block, Header, Log, Receipt, Withdrawal from .bloom import logs_bloom diff --git a/src/ethereum/cancun/fork_types.py b/src/ethereum/cancun/fork_types.py index c53dcb62d3..dc287c84f1 100644 --- a/src/ethereum/cancun/fork_types.py +++ b/src/ethereum/cancun/fork_types.py @@ -14,11 +14,11 @@ from dataclasses import dataclass +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes20, Bytes256 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint -from .. import rlp from ..crypto.hash import Hash32, keccak256 Address = Bytes20 diff --git a/src/ethereum/cancun/transactions.py b/src/ethereum/cancun/transactions.py index d1f7df04f7..3fe7a584f5 100644 --- a/src/ethereum/cancun/transactions.py +++ b/src/ethereum/cancun/transactions.py @@ -6,6 +6,7 @@ from dataclasses import dataclass from typing import Tuple, Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes32 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U64, U256, Uint @@ -14,7 +15,6 @@ from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidSignatureError -from .. import rlp from .exceptions import TransactionTypeError from .fork_types import Address, VersionedHash diff --git a/src/ethereum/cancun/trie.py b/src/ethereum/cancun/trie.py index db0394cdb2..082cbb5495 100644 --- a/src/ethereum/cancun/trie.py +++ b/src/ethereum/cancun/trie.py @@ -28,6 +28,7 @@ Union, ) +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint @@ -36,7 +37,6 @@ from ethereum.shanghai import trie as previous_trie from ethereum.utils.hexadecimal import hex_to_bytes -from .. import rlp from .blocks import Receipt, Withdrawal from .fork_types import Account, Address, Root, encode_account from .transactions import LegacyTransaction diff --git a/src/ethereum/cancun/utils/address.py b/src/ethereum/cancun/utils/address.py index 2ff9ea38cf..1bfbd2326c 100644 --- a/src/ethereum/cancun/utils/address.py +++ b/src/ethereum/cancun/utils/address.py @@ -14,13 +14,13 @@ """ from typing import Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes32 from ethereum_types.numeric import U256, Uint from ethereum.crypto.hash import keccak256 from ethereum.utils.byte import left_pad_zero_bytes -from ... import rlp from ..fork_types import Address diff --git a/src/ethereum/constantinople/fork.py b/src/ethereum/constantinople/fork.py index 8f46f92056..e195589a13 100644 --- a/src/ethereum/constantinople/fork.py +++ b/src/ethereum/constantinople/fork.py @@ -15,6 +15,7 @@ from dataclasses import dataclass from typing import List, Optional, Set, Tuple +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.numeric import U64, U256, Uint @@ -22,7 +23,6 @@ from ethereum.ethash import dataset_size, generate_cache, hashimoto_light from ethereum.exceptions import InvalidBlock, InvalidSenderError -from .. import rlp from . import vm from .blocks import Block, Header, Log, Receipt from .bloom import logs_bloom @@ -270,7 +270,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32: header.extra_data, ) - return rlp.rlp_hash(header_data_without_pow_artefacts) + return keccak256(rlp.encode(header_data_without_pow_artefacts)) def validate_proof_of_work(header: Header) -> None: @@ -534,8 +534,8 @@ def validate_ommers( chain : History and current state. """ - block_hash = rlp.rlp_hash(block_header) - if rlp.rlp_hash(ommers) != block_header.ommers_hash: + block_hash = keccak256(rlp.encode(block_header)) + if keccak256(rlp.encode(ommers)) != block_header.ommers_hash: raise InvalidBlock if len(ommers) == 0: @@ -553,18 +553,19 @@ def validate_ommers( if len(ommers) > 2: raise InvalidBlock - ommers_hashes = [rlp.rlp_hash(ommer) for ommer in ommers] + ommers_hashes = [keccak256(rlp.encode(ommer)) for ommer in ommers] if len(ommers_hashes) != len(set(ommers_hashes)): raise InvalidBlock recent_canonical_blocks = chain.blocks[-(MAX_OMMER_DEPTH + Uint(1)) :] recent_canonical_block_hashes = { - rlp.rlp_hash(block.header) for block in recent_canonical_blocks + keccak256(rlp.encode(block.header)) + for block in recent_canonical_blocks } recent_ommers_hashes: Set[Hash32] = set() for block in recent_canonical_blocks: recent_ommers_hashes = recent_ommers_hashes.union( - {rlp.rlp_hash(ommer) for ommer in block.ommers} + {keccak256(rlp.encode(ommer)) for ommer in block.ommers} ) for ommer_index, ommer in enumerate(ommers): diff --git a/src/ethereum/constantinople/fork_types.py b/src/ethereum/constantinople/fork_types.py index 05f34f691b..00817ba506 100644 --- a/src/ethereum/constantinople/fork_types.py +++ b/src/ethereum/constantinople/fork_types.py @@ -14,11 +14,11 @@ from dataclasses import dataclass +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes20, Bytes256 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint -from .. import rlp from ..crypto.hash import Hash32, keccak256 Address = Bytes20 diff --git a/src/ethereum/constantinople/transactions.py b/src/ethereum/constantinople/transactions.py index da6aff2c1b..142929587d 100644 --- a/src/ethereum/constantinople/transactions.py +++ b/src/ethereum/constantinople/transactions.py @@ -6,6 +6,7 @@ from dataclasses import dataclass from typing import Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U64, U256, Uint @@ -14,7 +15,6 @@ from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidSignatureError -from .. import rlp from .fork_types import Address TX_BASE_COST = 21000 diff --git a/src/ethereum/constantinople/trie.py b/src/ethereum/constantinople/trie.py index 6d61f3aaa9..9e9d3052e9 100644 --- a/src/ethereum/constantinople/trie.py +++ b/src/ethereum/constantinople/trie.py @@ -28,6 +28,7 @@ Union, ) +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint @@ -36,7 +37,6 @@ from ethereum.crypto.hash import keccak256 from ethereum.utils.hexadecimal import hex_to_bytes -from .. import rlp from .blocks import Receipt from .fork_types import Account, Address, Root, encode_account from .transactions import Transaction diff --git a/src/ethereum/constantinople/utils/address.py b/src/ethereum/constantinople/utils/address.py index 709f8b6ab8..96c72cbc85 100644 --- a/src/ethereum/constantinople/utils/address.py +++ b/src/ethereum/constantinople/utils/address.py @@ -14,13 +14,13 @@ """ from typing import Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes32 from ethereum_types.numeric import U256, Uint from ethereum.crypto.hash import keccak256 from ethereum.utils.byte import left_pad_zero_bytes -from ... import rlp from ..fork_types import Address diff --git a/src/ethereum/dao_fork/fork.py b/src/ethereum/dao_fork/fork.py index 8679efabe2..1835a2525f 100644 --- a/src/ethereum/dao_fork/fork.py +++ b/src/ethereum/dao_fork/fork.py @@ -17,6 +17,7 @@ from dataclasses import dataclass from typing import List, Optional, Set, Tuple +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes32 from ethereum_types.numeric import U64, U256, Uint @@ -24,7 +25,6 @@ from ethereum.ethash import dataset_size, generate_cache, hashimoto_light from ethereum.exceptions import InvalidBlock, InvalidSenderError -from .. import rlp from . import FORK_CRITERIA, vm from .blocks import Block, Header, Log, Receipt from .bloom import logs_bloom @@ -282,7 +282,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32: header.extra_data, ) - return rlp.rlp_hash(header_data_without_pow_artefacts) + return keccak256(rlp.encode(header_data_without_pow_artefacts)) def validate_proof_of_work(header: Header) -> None: @@ -540,8 +540,8 @@ def validate_ommers( chain : History and current state. """ - block_hash = rlp.rlp_hash(block_header) - if rlp.rlp_hash(ommers) != block_header.ommers_hash: + block_hash = keccak256(rlp.encode(block_header)) + if keccak256(rlp.encode(ommers)) != block_header.ommers_hash: raise InvalidBlock if len(ommers) == 0: @@ -559,18 +559,19 @@ def validate_ommers( if len(ommers) > 2: raise InvalidBlock - ommers_hashes = [rlp.rlp_hash(ommer) for ommer in ommers] + ommers_hashes = [keccak256(rlp.encode(ommer)) for ommer in ommers] if len(ommers_hashes) != len(set(ommers_hashes)): raise InvalidBlock recent_canonical_blocks = chain.blocks[-(MAX_OMMER_DEPTH + Uint(1)) :] recent_canonical_block_hashes = { - rlp.rlp_hash(block.header) for block in recent_canonical_blocks + keccak256(rlp.encode(block.header)) + for block in recent_canonical_blocks } recent_ommers_hashes: Set[Hash32] = set() for block in recent_canonical_blocks: recent_ommers_hashes = recent_ommers_hashes.union( - {rlp.rlp_hash(ommer) for ommer in block.ommers} + {keccak256(rlp.encode(ommer)) for ommer in block.ommers} ) for ommer_index, ommer in enumerate(ommers): diff --git a/src/ethereum/dao_fork/fork_types.py b/src/ethereum/dao_fork/fork_types.py index 05f34f691b..00817ba506 100644 --- a/src/ethereum/dao_fork/fork_types.py +++ b/src/ethereum/dao_fork/fork_types.py @@ -14,11 +14,11 @@ from dataclasses import dataclass +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes20, Bytes256 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint -from .. import rlp from ..crypto.hash import Hash32, keccak256 Address = Bytes20 diff --git a/src/ethereum/dao_fork/transactions.py b/src/ethereum/dao_fork/transactions.py index a9caf4990f..57d697d615 100644 --- a/src/ethereum/dao_fork/transactions.py +++ b/src/ethereum/dao_fork/transactions.py @@ -6,6 +6,7 @@ from dataclasses import dataclass from typing import Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U64, U256, Uint @@ -14,7 +15,6 @@ from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidSignatureError -from .. import rlp from .fork_types import Address TX_BASE_COST = 21000 diff --git a/src/ethereum/dao_fork/trie.py b/src/ethereum/dao_fork/trie.py index 0b1bd95d96..f5577adffd 100644 --- a/src/ethereum/dao_fork/trie.py +++ b/src/ethereum/dao_fork/trie.py @@ -28,6 +28,7 @@ Union, ) +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint @@ -36,7 +37,6 @@ from ethereum.homestead import trie as previous_trie from ethereum.utils.hexadecimal import hex_to_bytes -from .. import rlp from .blocks import Receipt from .fork_types import Account, Address, Root, encode_account from .transactions import Transaction diff --git a/src/ethereum/dao_fork/utils/address.py b/src/ethereum/dao_fork/utils/address.py index 83fea9e2b4..f9434e1dd0 100644 --- a/src/ethereum/dao_fork/utils/address.py +++ b/src/ethereum/dao_fork/utils/address.py @@ -13,12 +13,12 @@ """ from typing import Union +from ethereum_rlp import rlp from ethereum_types.numeric import U256, Uint from ethereum.crypto.hash import keccak256 from ethereum.utils.byte import left_pad_zero_bytes -from ... import rlp from ..fork_types import Address diff --git a/src/ethereum/ethash.py b/src/ethereum/ethash.py index 5dcd2f766b..6cb9aca590 100644 --- a/src/ethereum/ethash.py +++ b/src/ethereum/ethash.py @@ -349,7 +349,7 @@ def hashimoto( #### Parameters - - `header_hash` is a valid [RLP hash] of a block header. + - `header_hash` is a valid RLP hash of a block header. - `nonce` is the propagated nonce for the given block. - `dataset_size` is the size of the dataset. See [`dataset_size`]. - `fetch_dataset_item` is a function that retrieves a specific dataset item @@ -361,7 +361,6 @@ def hashimoto( - The final result obtained which will be checked for leading zeros (in byte representation) in correspondence with the block difficulty. - [RLP hash]: ref:ethereum.rlp.rlp_hash [`dataset_size`]: ref:ethereum.ethash.dataset_size """ nonce_le = bytes(reversed(nonce)) @@ -405,7 +404,7 @@ def hashimoto_light( #### Parameters - - `header_hash` is a valid [RLP hash] of a block header. + - `header_hash` is a valid RLP hash of a block header. - `nonce` is the propagated nonce for the given block. - `cache` is the cache generated by [`generate_cache`]. - `dataset_size` is the size of the dataset. See [`dataset_size`]. @@ -416,7 +415,6 @@ def hashimoto_light( - The final result obtained which will be checked for leading zeros (in byte representation) in correspondence with the block difficulty. - [RLP hash]: ref:ethereum.rlp.rlp_hash [`dataset_size`]: ref:ethereum.ethash.dataset_size [`generate_cache`]: ref:ethereum.ethash.generate_cache [`hashimoto`]: ref:ethereum.ethash.hashimoto diff --git a/src/ethereum/exceptions.py b/src/ethereum/exceptions.py index 8ee32d7b78..7fd959d9d3 100644 --- a/src/ethereum/exceptions.py +++ b/src/ethereum/exceptions.py @@ -22,18 +22,6 @@ class InvalidTransaction(EthereumException): """ -class RLPDecodingError(EthereumException): - """ - Indicates that RLP decoding failed. - """ - - -class RLPEncodingError(EthereumException): - """ - Indicates that RLP encoding failed. - """ - - class InvalidSenderError(InvalidTransaction): """ Thrown when a transaction originates from an account that cannot send diff --git a/src/ethereum/frontier/fork.py b/src/ethereum/frontier/fork.py index ea04ce5719..46749732f4 100644 --- a/src/ethereum/frontier/fork.py +++ b/src/ethereum/frontier/fork.py @@ -15,6 +15,7 @@ from dataclasses import dataclass from typing import List, Optional, Set, Tuple +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes32 from ethereum_types.numeric import U64, U256, Uint @@ -22,7 +23,6 @@ from ethereum.ethash import dataset_size, generate_cache, hashimoto_light from ethereum.exceptions import InvalidBlock, InvalidSenderError -from .. import rlp from . import vm from .blocks import Block, Header, Log, Receipt from .bloom import logs_bloom @@ -264,7 +264,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32: header.extra_data, ) - return rlp.rlp_hash(header_data_without_pow_artefacts) + return keccak256(rlp.encode(header_data_without_pow_artefacts)) def validate_proof_of_work(header: Header) -> None: @@ -522,8 +522,8 @@ def validate_ommers( chain : History and current state. """ - block_hash = rlp.rlp_hash(block_header) - if rlp.rlp_hash(ommers) != block_header.ommers_hash: + block_hash = keccak256(rlp.encode(block_header)) + if keccak256(rlp.encode(ommers)) != block_header.ommers_hash: raise InvalidBlock if len(ommers) == 0: @@ -541,18 +541,19 @@ def validate_ommers( if len(ommers) > 2: raise InvalidBlock - ommers_hashes = [rlp.rlp_hash(ommer) for ommer in ommers] + ommers_hashes = [keccak256(rlp.encode(ommer)) for ommer in ommers] if len(ommers_hashes) != len(set(ommers_hashes)): raise InvalidBlock recent_canonical_blocks = chain.blocks[-(MAX_OMMER_DEPTH + Uint(1)) :] recent_canonical_block_hashes = { - rlp.rlp_hash(block.header) for block in recent_canonical_blocks + keccak256(rlp.encode(block.header)) + for block in recent_canonical_blocks } recent_ommers_hashes: Set[Hash32] = set() for block in recent_canonical_blocks: recent_ommers_hashes = recent_ommers_hashes.union( - {rlp.rlp_hash(ommer) for ommer in block.ommers} + {keccak256(rlp.encode(ommer)) for ommer in block.ommers} ) for ommer_index, ommer in enumerate(ommers): diff --git a/src/ethereum/frontier/fork_types.py b/src/ethereum/frontier/fork_types.py index 05f34f691b..00817ba506 100644 --- a/src/ethereum/frontier/fork_types.py +++ b/src/ethereum/frontier/fork_types.py @@ -14,11 +14,11 @@ from dataclasses import dataclass +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes20, Bytes256 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint -from .. import rlp from ..crypto.hash import Hash32, keccak256 Address = Bytes20 diff --git a/src/ethereum/frontier/transactions.py b/src/ethereum/frontier/transactions.py index e208b4cfb7..a09bba38ba 100644 --- a/src/ethereum/frontier/transactions.py +++ b/src/ethereum/frontier/transactions.py @@ -6,6 +6,7 @@ from dataclasses import dataclass from typing import Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U64, U256, Uint @@ -14,7 +15,6 @@ from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidSignatureError -from .. import rlp from .fork_types import Address TX_BASE_COST = 21000 diff --git a/src/ethereum/frontier/trie.py b/src/ethereum/frontier/trie.py index 587e811172..1f20ad5af1 100644 --- a/src/ethereum/frontier/trie.py +++ b/src/ethereum/frontier/trie.py @@ -28,6 +28,7 @@ Union, ) +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint @@ -35,7 +36,6 @@ from ethereum.crypto.hash import keccak256 from ethereum.utils.hexadecimal import hex_to_bytes -from .. import rlp from .blocks import Receipt from .fork_types import Account, Address, Root, encode_account from .transactions import Transaction diff --git a/src/ethereum/frontier/utils/address.py b/src/ethereum/frontier/utils/address.py index ed1f792cd6..aee29af13d 100644 --- a/src/ethereum/frontier/utils/address.py +++ b/src/ethereum/frontier/utils/address.py @@ -13,12 +13,12 @@ """ from typing import Union +from ethereum_rlp import rlp from ethereum_types.numeric import U256, Uint from ethereum.crypto.hash import keccak256 from ethereum.utils.byte import left_pad_zero_bytes -from ... import rlp from ..fork_types import Address diff --git a/src/ethereum/genesis.py b/src/ethereum/genesis.py index a7b515b309..330022bd40 100644 --- a/src/ethereum/genesis.py +++ b/src/ethereum/genesis.py @@ -17,12 +17,12 @@ from dataclasses import dataclass from typing import Any, Callable, Dict, Generic, Type, TypeVar +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes8, Bytes32, FixedBytes from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U64, U256, Uint -from ethereum import rlp -from ethereum.crypto.hash import Hash32 +from ethereum.crypto.hash import Hash32, keccak256 from ethereum.utils import has_field from ethereum.utils.hexadecimal import ( hex_to_bytes, @@ -220,7 +220,7 @@ def add_genesis_block( fields = { "parent_hash": Hash32(b"\0" * 32), - "ommers_hash": rlp.rlp_hash(()), + "ommers_hash": keccak256(rlp.encode(())), "coinbase": Address(b"\0" * Address.LENGTH), "state_root": hardfork.state_root(chain.state), "transactions_root": hardfork.root(hardfork.Trie(False, None)), diff --git a/src/ethereum/gray_glacier/fork.py b/src/ethereum/gray_glacier/fork.py index 09bbc3d839..ce070e7c08 100644 --- a/src/ethereum/gray_glacier/fork.py +++ b/src/ethereum/gray_glacier/fork.py @@ -15,6 +15,7 @@ from dataclasses import dataclass from typing import List, Optional, Set, Tuple, Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.numeric import U64, U256, Uint @@ -22,7 +23,6 @@ from ethereum.ethash import dataset_size, generate_cache, hashimoto_light from ethereum.exceptions import InvalidBlock, InvalidSenderError -from .. import rlp from . import vm from .blocks import Block, Header, Log, Receipt from .bloom import logs_bloom @@ -351,7 +351,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32: header.base_fee_per_gas, ) - return rlp.rlp_hash(header_data_without_pow_artefacts) + return keccak256(rlp.encode(header_data_without_pow_artefacts)) def validate_proof_of_work(header: Header) -> None: @@ -650,8 +650,8 @@ def validate_ommers( chain : History and current state. """ - block_hash = rlp.rlp_hash(block_header) - if rlp.rlp_hash(ommers) != block_header.ommers_hash: + block_hash = keccak256(rlp.encode(block_header)) + if keccak256(rlp.encode(ommers)) != block_header.ommers_hash: raise InvalidBlock if len(ommers) == 0: @@ -669,18 +669,19 @@ def validate_ommers( if len(ommers) > 2: raise InvalidBlock - ommers_hashes = [rlp.rlp_hash(ommer) for ommer in ommers] + ommers_hashes = [keccak256(rlp.encode(ommer)) for ommer in ommers] if len(ommers_hashes) != len(set(ommers_hashes)): raise InvalidBlock recent_canonical_blocks = chain.blocks[-(MAX_OMMER_DEPTH + Uint(1)) :] recent_canonical_block_hashes = { - rlp.rlp_hash(block.header) for block in recent_canonical_blocks + keccak256(rlp.encode(block.header)) + for block in recent_canonical_blocks } recent_ommers_hashes: Set[Hash32] = set() for block in recent_canonical_blocks: recent_ommers_hashes = recent_ommers_hashes.union( - {rlp.rlp_hash(ommer) for ommer in block.ommers} + {keccak256(rlp.encode(ommer)) for ommer in block.ommers} ) for ommer_index, ommer in enumerate(ommers): diff --git a/src/ethereum/gray_glacier/fork_types.py b/src/ethereum/gray_glacier/fork_types.py index 05f34f691b..00817ba506 100644 --- a/src/ethereum/gray_glacier/fork_types.py +++ b/src/ethereum/gray_glacier/fork_types.py @@ -14,11 +14,11 @@ from dataclasses import dataclass +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes20, Bytes256 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint -from .. import rlp from ..crypto.hash import Hash32, keccak256 Address = Bytes20 diff --git a/src/ethereum/gray_glacier/transactions.py b/src/ethereum/gray_glacier/transactions.py index 10f52ad908..9d9bb6bd17 100644 --- a/src/ethereum/gray_glacier/transactions.py +++ b/src/ethereum/gray_glacier/transactions.py @@ -6,6 +6,7 @@ from dataclasses import dataclass from typing import Tuple, Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes32 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U64, U256, Uint @@ -14,7 +15,6 @@ from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidSignatureError -from .. import rlp from .exceptions import TransactionTypeError from .fork_types import Address diff --git a/src/ethereum/gray_glacier/trie.py b/src/ethereum/gray_glacier/trie.py index e6ee3e9bef..45b96d4226 100644 --- a/src/ethereum/gray_glacier/trie.py +++ b/src/ethereum/gray_glacier/trie.py @@ -28,6 +28,7 @@ Union, ) +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint @@ -36,7 +37,6 @@ from ethereum.crypto.hash import keccak256 from ethereum.utils.hexadecimal import hex_to_bytes -from .. import rlp from .blocks import Receipt from .fork_types import Account, Address, Root, encode_account from .transactions import LegacyTransaction diff --git a/src/ethereum/gray_glacier/utils/address.py b/src/ethereum/gray_glacier/utils/address.py index 962bb0a1fa..35b219756c 100644 --- a/src/ethereum/gray_glacier/utils/address.py +++ b/src/ethereum/gray_glacier/utils/address.py @@ -14,13 +14,13 @@ """ from typing import Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes32 from ethereum_types.numeric import U256, Uint from ethereum.crypto.hash import keccak256 from ethereum.utils.byte import left_pad_zero_bytes -from ... import rlp from ..fork_types import Address diff --git a/src/ethereum/homestead/fork.py b/src/ethereum/homestead/fork.py index 8e957c7424..1419c87b6d 100644 --- a/src/ethereum/homestead/fork.py +++ b/src/ethereum/homestead/fork.py @@ -15,6 +15,7 @@ from dataclasses import dataclass from typing import List, Optional, Set, Tuple +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes32 from ethereum_types.numeric import U64, U256, Uint @@ -22,7 +23,6 @@ from ethereum.ethash import dataset_size, generate_cache, hashimoto_light from ethereum.exceptions import InvalidBlock, InvalidSenderError -from .. import rlp from . import vm from .blocks import Block, Header, Log, Receipt from .bloom import logs_bloom @@ -264,7 +264,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32: header.extra_data, ) - return rlp.rlp_hash(header_data_without_pow_artefacts) + return keccak256(rlp.encode(header_data_without_pow_artefacts)) def validate_proof_of_work(header: Header) -> None: @@ -522,8 +522,8 @@ def validate_ommers( chain : History and current state. """ - block_hash = rlp.rlp_hash(block_header) - if rlp.rlp_hash(ommers) != block_header.ommers_hash: + block_hash = keccak256(rlp.encode(block_header)) + if keccak256(rlp.encode(ommers)) != block_header.ommers_hash: raise InvalidBlock if len(ommers) == 0: @@ -541,18 +541,19 @@ def validate_ommers( if len(ommers) > 2: raise InvalidBlock - ommers_hashes = [rlp.rlp_hash(ommer) for ommer in ommers] + ommers_hashes = [keccak256(rlp.encode(ommer)) for ommer in ommers] if len(ommers_hashes) != len(set(ommers_hashes)): raise InvalidBlock recent_canonical_blocks = chain.blocks[-(MAX_OMMER_DEPTH + Uint(1)) :] recent_canonical_block_hashes = { - rlp.rlp_hash(block.header) for block in recent_canonical_blocks + keccak256(rlp.encode(block.header)) + for block in recent_canonical_blocks } recent_ommers_hashes: Set[Hash32] = set() for block in recent_canonical_blocks: recent_ommers_hashes = recent_ommers_hashes.union( - {rlp.rlp_hash(ommer) for ommer in block.ommers} + {keccak256(rlp.encode(ommer)) for ommer in block.ommers} ) for ommer_index, ommer in enumerate(ommers): diff --git a/src/ethereum/homestead/fork_types.py b/src/ethereum/homestead/fork_types.py index 05f34f691b..00817ba506 100644 --- a/src/ethereum/homestead/fork_types.py +++ b/src/ethereum/homestead/fork_types.py @@ -14,11 +14,11 @@ from dataclasses import dataclass +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes20, Bytes256 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint -from .. import rlp from ..crypto.hash import Hash32, keccak256 Address = Bytes20 diff --git a/src/ethereum/homestead/transactions.py b/src/ethereum/homestead/transactions.py index a9caf4990f..57d697d615 100644 --- a/src/ethereum/homestead/transactions.py +++ b/src/ethereum/homestead/transactions.py @@ -6,6 +6,7 @@ from dataclasses import dataclass from typing import Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U64, U256, Uint @@ -14,7 +15,6 @@ from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidSignatureError -from .. import rlp from .fork_types import Address TX_BASE_COST = 21000 diff --git a/src/ethereum/homestead/trie.py b/src/ethereum/homestead/trie.py index 5294a2ba0d..96efb4d8d4 100644 --- a/src/ethereum/homestead/trie.py +++ b/src/ethereum/homestead/trie.py @@ -28,6 +28,7 @@ Union, ) +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint @@ -36,7 +37,6 @@ from ethereum.frontier import trie as previous_trie from ethereum.utils.hexadecimal import hex_to_bytes -from .. import rlp from .blocks import Receipt from .fork_types import Account, Address, Root, encode_account from .transactions import Transaction diff --git a/src/ethereum/homestead/utils/address.py b/src/ethereum/homestead/utils/address.py index febe47f5c6..a545a94d91 100644 --- a/src/ethereum/homestead/utils/address.py +++ b/src/ethereum/homestead/utils/address.py @@ -13,12 +13,12 @@ """ from typing import Union +from ethereum_rlp import rlp from ethereum_types.numeric import U256, Uint from ethereum.crypto.hash import keccak256 from ethereum.utils.byte import left_pad_zero_bytes -from ... import rlp from ..fork_types import Address diff --git a/src/ethereum/istanbul/fork.py b/src/ethereum/istanbul/fork.py index b9c6ed815c..35eb37c9c5 100644 --- a/src/ethereum/istanbul/fork.py +++ b/src/ethereum/istanbul/fork.py @@ -15,6 +15,7 @@ from dataclasses import dataclass from typing import List, Optional, Set, Tuple +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.numeric import U64, U256, Uint @@ -22,7 +23,6 @@ from ethereum.ethash import dataset_size, generate_cache, hashimoto_light from ethereum.exceptions import InvalidBlock, InvalidSenderError -from .. import rlp from . import vm from .blocks import Block, Header, Log, Receipt from .bloom import logs_bloom @@ -270,7 +270,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32: header.extra_data, ) - return rlp.rlp_hash(header_data_without_pow_artefacts) + return keccak256(rlp.encode(header_data_without_pow_artefacts)) def validate_proof_of_work(header: Header) -> None: @@ -535,8 +535,8 @@ def validate_ommers( chain : History and current state. """ - block_hash = rlp.rlp_hash(block_header) - if rlp.rlp_hash(ommers) != block_header.ommers_hash: + block_hash = keccak256(rlp.encode(block_header)) + if keccak256(rlp.encode(ommers)) != block_header.ommers_hash: raise InvalidBlock if len(ommers) == 0: @@ -554,18 +554,19 @@ def validate_ommers( if len(ommers) > 2: raise InvalidBlock - ommers_hashes = [rlp.rlp_hash(ommer) for ommer in ommers] + ommers_hashes = [keccak256(rlp.encode(ommer)) for ommer in ommers] if len(ommers_hashes) != len(set(ommers_hashes)): raise InvalidBlock recent_canonical_blocks = chain.blocks[-(MAX_OMMER_DEPTH + Uint(1)) :] recent_canonical_block_hashes = { - rlp.rlp_hash(block.header) for block in recent_canonical_blocks + keccak256(rlp.encode(block.header)) + for block in recent_canonical_blocks } recent_ommers_hashes: Set[Hash32] = set() for block in recent_canonical_blocks: recent_ommers_hashes = recent_ommers_hashes.union( - {rlp.rlp_hash(ommer) for ommer in block.ommers} + {keccak256(rlp.encode(ommer)) for ommer in block.ommers} ) for ommer_index, ommer in enumerate(ommers): diff --git a/src/ethereum/istanbul/fork_types.py b/src/ethereum/istanbul/fork_types.py index 05f34f691b..00817ba506 100644 --- a/src/ethereum/istanbul/fork_types.py +++ b/src/ethereum/istanbul/fork_types.py @@ -14,11 +14,11 @@ from dataclasses import dataclass +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes20, Bytes256 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint -from .. import rlp from ..crypto.hash import Hash32, keccak256 Address = Bytes20 diff --git a/src/ethereum/istanbul/transactions.py b/src/ethereum/istanbul/transactions.py index b086f8c88b..2bdb3603b9 100644 --- a/src/ethereum/istanbul/transactions.py +++ b/src/ethereum/istanbul/transactions.py @@ -6,6 +6,7 @@ from dataclasses import dataclass from typing import Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U64, U256, Uint @@ -14,7 +15,6 @@ from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidSignatureError -from .. import rlp from .fork_types import Address TX_BASE_COST = 21000 diff --git a/src/ethereum/istanbul/trie.py b/src/ethereum/istanbul/trie.py index a1a4cfbf98..64bd03ecb6 100644 --- a/src/ethereum/istanbul/trie.py +++ b/src/ethereum/istanbul/trie.py @@ -28,6 +28,7 @@ Union, ) +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint @@ -36,7 +37,6 @@ from ethereum.crypto.hash import keccak256 from ethereum.utils.hexadecimal import hex_to_bytes -from .. import rlp from .blocks import Receipt from .fork_types import Account, Address, Root, encode_account from .transactions import Transaction diff --git a/src/ethereum/istanbul/utils/address.py b/src/ethereum/istanbul/utils/address.py index 7429e4a7e4..8501d2066c 100644 --- a/src/ethereum/istanbul/utils/address.py +++ b/src/ethereum/istanbul/utils/address.py @@ -14,13 +14,13 @@ """ from typing import Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes32 from ethereum_types.numeric import U256, Uint from ethereum.crypto.hash import keccak256 from ethereum.utils.byte import left_pad_zero_bytes -from ... import rlp from ..fork_types import Address diff --git a/src/ethereum/london/fork.py b/src/ethereum/london/fork.py index f476ef5275..a9599345c9 100644 --- a/src/ethereum/london/fork.py +++ b/src/ethereum/london/fork.py @@ -15,6 +15,7 @@ from dataclasses import dataclass from typing import List, Optional, Set, Tuple, Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.numeric import U64, U256, Uint @@ -22,7 +23,6 @@ from ethereum.ethash import dataset_size, generate_cache, hashimoto_light from ethereum.exceptions import InvalidBlock, InvalidSenderError -from .. import rlp from . import FORK_CRITERIA, vm from .blocks import Block, Header, Log, Receipt from .bloom import logs_bloom @@ -357,7 +357,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32: header.base_fee_per_gas, ) - return rlp.rlp_hash(header_data_without_pow_artefacts) + return keccak256(rlp.encode(header_data_without_pow_artefacts)) def validate_proof_of_work(header: Header) -> None: @@ -656,8 +656,8 @@ def validate_ommers( chain : History and current state. """ - block_hash = rlp.rlp_hash(block_header) - if rlp.rlp_hash(ommers) != block_header.ommers_hash: + block_hash = keccak256(rlp.encode(block_header)) + if keccak256(rlp.encode(ommers)) != block_header.ommers_hash: raise InvalidBlock if len(ommers) == 0: @@ -675,18 +675,19 @@ def validate_ommers( if len(ommers) > 2: raise InvalidBlock - ommers_hashes = [rlp.rlp_hash(ommer) for ommer in ommers] + ommers_hashes = [keccak256(rlp.encode(ommer)) for ommer in ommers] if len(ommers_hashes) != len(set(ommers_hashes)): raise InvalidBlock recent_canonical_blocks = chain.blocks[-(MAX_OMMER_DEPTH + Uint(1)) :] recent_canonical_block_hashes = { - rlp.rlp_hash(block.header) for block in recent_canonical_blocks + keccak256(rlp.encode(block.header)) + for block in recent_canonical_blocks } recent_ommers_hashes: Set[Hash32] = set() for block in recent_canonical_blocks: recent_ommers_hashes = recent_ommers_hashes.union( - {rlp.rlp_hash(ommer) for ommer in block.ommers} + {keccak256(rlp.encode(ommer)) for ommer in block.ommers} ) for ommer_index, ommer in enumerate(ommers): diff --git a/src/ethereum/london/fork_types.py b/src/ethereum/london/fork_types.py index 05f34f691b..00817ba506 100644 --- a/src/ethereum/london/fork_types.py +++ b/src/ethereum/london/fork_types.py @@ -14,11 +14,11 @@ from dataclasses import dataclass +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes20, Bytes256 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint -from .. import rlp from ..crypto.hash import Hash32, keccak256 Address = Bytes20 diff --git a/src/ethereum/london/transactions.py b/src/ethereum/london/transactions.py index 10f52ad908..9d9bb6bd17 100644 --- a/src/ethereum/london/transactions.py +++ b/src/ethereum/london/transactions.py @@ -6,6 +6,7 @@ from dataclasses import dataclass from typing import Tuple, Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes32 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U64, U256, Uint @@ -14,7 +15,6 @@ from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidSignatureError -from .. import rlp from .exceptions import TransactionTypeError from .fork_types import Address diff --git a/src/ethereum/london/trie.py b/src/ethereum/london/trie.py index 03e230531c..71b83f7921 100644 --- a/src/ethereum/london/trie.py +++ b/src/ethereum/london/trie.py @@ -28,6 +28,7 @@ Union, ) +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint @@ -36,7 +37,6 @@ from ethereum.crypto.hash import keccak256 from ethereum.utils.hexadecimal import hex_to_bytes -from .. import rlp from .blocks import Receipt from .fork_types import Account, Address, Root, encode_account from .transactions import LegacyTransaction diff --git a/src/ethereum/london/utils/address.py b/src/ethereum/london/utils/address.py index 0461f13c20..54c000069e 100644 --- a/src/ethereum/london/utils/address.py +++ b/src/ethereum/london/utils/address.py @@ -14,13 +14,13 @@ """ from typing import Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes32 from ethereum_types.numeric import U256, Uint from ethereum.crypto.hash import keccak256 from ethereum.utils.byte import left_pad_zero_bytes -from ... import rlp from ..fork_types import Address diff --git a/src/ethereum/muir_glacier/fork.py b/src/ethereum/muir_glacier/fork.py index eaf167a7fc..4060049c84 100644 --- a/src/ethereum/muir_glacier/fork.py +++ b/src/ethereum/muir_glacier/fork.py @@ -15,6 +15,7 @@ from dataclasses import dataclass from typing import List, Optional, Set, Tuple +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.numeric import U64, U256, Uint @@ -22,7 +23,6 @@ from ethereum.ethash import dataset_size, generate_cache, hashimoto_light from ethereum.exceptions import InvalidBlock, InvalidSenderError -from .. import rlp from . import vm from .blocks import Block, Header, Log, Receipt from .bloom import logs_bloom @@ -270,7 +270,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32: header.extra_data, ) - return rlp.rlp_hash(header_data_without_pow_artefacts) + return keccak256(rlp.encode(header_data_without_pow_artefacts)) def validate_proof_of_work(header: Header) -> None: @@ -535,8 +535,8 @@ def validate_ommers( chain : History and current state. """ - block_hash = rlp.rlp_hash(block_header) - if rlp.rlp_hash(ommers) != block_header.ommers_hash: + block_hash = keccak256(rlp.encode(block_header)) + if keccak256(rlp.encode(ommers)) != block_header.ommers_hash: raise InvalidBlock if len(ommers) == 0: @@ -554,18 +554,19 @@ def validate_ommers( if len(ommers) > 2: raise InvalidBlock - ommers_hashes = [rlp.rlp_hash(ommer) for ommer in ommers] + ommers_hashes = [keccak256(rlp.encode(ommer)) for ommer in ommers] if len(ommers_hashes) != len(set(ommers_hashes)): raise InvalidBlock recent_canonical_blocks = chain.blocks[-(MAX_OMMER_DEPTH + Uint(1)) :] recent_canonical_block_hashes = { - rlp.rlp_hash(block.header) for block in recent_canonical_blocks + keccak256(rlp.encode(block.header)) + for block in recent_canonical_blocks } recent_ommers_hashes: Set[Hash32] = set() for block in recent_canonical_blocks: recent_ommers_hashes = recent_ommers_hashes.union( - {rlp.rlp_hash(ommer) for ommer in block.ommers} + {keccak256(rlp.encode(ommer)) for ommer in block.ommers} ) for ommer_index, ommer in enumerate(ommers): diff --git a/src/ethereum/muir_glacier/fork_types.py b/src/ethereum/muir_glacier/fork_types.py index 05f34f691b..00817ba506 100644 --- a/src/ethereum/muir_glacier/fork_types.py +++ b/src/ethereum/muir_glacier/fork_types.py @@ -14,11 +14,11 @@ from dataclasses import dataclass +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes20, Bytes256 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint -from .. import rlp from ..crypto.hash import Hash32, keccak256 Address = Bytes20 diff --git a/src/ethereum/muir_glacier/transactions.py b/src/ethereum/muir_glacier/transactions.py index b086f8c88b..2bdb3603b9 100644 --- a/src/ethereum/muir_glacier/transactions.py +++ b/src/ethereum/muir_glacier/transactions.py @@ -6,6 +6,7 @@ from dataclasses import dataclass from typing import Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U64, U256, Uint @@ -14,7 +15,6 @@ from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidSignatureError -from .. import rlp from .fork_types import Address TX_BASE_COST = 21000 diff --git a/src/ethereum/muir_glacier/trie.py b/src/ethereum/muir_glacier/trie.py index f2e4f29460..ab1a5fd3fe 100644 --- a/src/ethereum/muir_glacier/trie.py +++ b/src/ethereum/muir_glacier/trie.py @@ -28,6 +28,7 @@ Union, ) +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint @@ -36,7 +37,6 @@ from ethereum.istanbul import trie as previous_trie from ethereum.utils.hexadecimal import hex_to_bytes -from .. import rlp from .blocks import Receipt from .fork_types import Account, Address, Root, encode_account from .transactions import Transaction diff --git a/src/ethereum/muir_glacier/utils/address.py b/src/ethereum/muir_glacier/utils/address.py index 72b5c4cd06..5e6515f8b1 100644 --- a/src/ethereum/muir_glacier/utils/address.py +++ b/src/ethereum/muir_glacier/utils/address.py @@ -14,13 +14,13 @@ """ from typing import Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes32 from ethereum_types.numeric import U256, Uint from ethereum.crypto.hash import keccak256 from ethereum.utils.byte import left_pad_zero_bytes -from ... import rlp from ..fork_types import Address diff --git a/src/ethereum/paris/fork.py b/src/ethereum/paris/fork.py index e6ad598a54..1e34b33159 100644 --- a/src/ethereum/paris/fork.py +++ b/src/ethereum/paris/fork.py @@ -15,13 +15,13 @@ from dataclasses import dataclass from typing import List, Optional, Tuple, Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes32 from ethereum_types.numeric import U64, U256, Uint from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidBlock, InvalidSenderError -from .. import rlp from . import vm from .blocks import Block, Header, Log, Receipt from .bloom import logs_bloom diff --git a/src/ethereum/paris/fork_types.py b/src/ethereum/paris/fork_types.py index 05f34f691b..00817ba506 100644 --- a/src/ethereum/paris/fork_types.py +++ b/src/ethereum/paris/fork_types.py @@ -14,11 +14,11 @@ from dataclasses import dataclass +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes20, Bytes256 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint -from .. import rlp from ..crypto.hash import Hash32, keccak256 Address = Bytes20 diff --git a/src/ethereum/paris/transactions.py b/src/ethereum/paris/transactions.py index 70df38afe2..5944d96b6e 100644 --- a/src/ethereum/paris/transactions.py +++ b/src/ethereum/paris/transactions.py @@ -6,6 +6,7 @@ from dataclasses import dataclass from typing import Tuple, Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes32 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U64, U256, Uint @@ -14,7 +15,6 @@ from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidSignatureError -from .. import rlp from .exceptions import TransactionTypeError from .fork_types import Address diff --git a/src/ethereum/paris/trie.py b/src/ethereum/paris/trie.py index ccd99a17af..f51dba170d 100644 --- a/src/ethereum/paris/trie.py +++ b/src/ethereum/paris/trie.py @@ -28,6 +28,7 @@ Union, ) +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint @@ -36,7 +37,6 @@ from ethereum.gray_glacier import trie as previous_trie from ethereum.utils.hexadecimal import hex_to_bytes -from .. import rlp from .blocks import Receipt from .fork_types import Account, Address, Root, encode_account from .transactions import LegacyTransaction diff --git a/src/ethereum/paris/utils/address.py b/src/ethereum/paris/utils/address.py index 7d5933207f..1f26a8f723 100644 --- a/src/ethereum/paris/utils/address.py +++ b/src/ethereum/paris/utils/address.py @@ -14,13 +14,13 @@ """ from typing import Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes32 from ethereum_types.numeric import U256, Uint from ethereum.crypto.hash import keccak256 from ethereum.utils.byte import left_pad_zero_bytes -from ... import rlp from ..fork_types import Address diff --git a/src/ethereum/rlp.py b/src/ethereum/rlp.py deleted file mode 100644 index e1fd7165ad..0000000000 --- a/src/ethereum/rlp.py +++ /dev/null @@ -1,573 +0,0 @@ -""" -.. _rlp: - -Recursive Length Prefix (RLP) Encoding -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -.. contents:: Table of Contents - :backlinks: none - :local: - -Introduction ------------- - -Defines the serialization and deserialization format used throughout Ethereum. -""" - -from dataclasses import astuple, fields, is_dataclass -from typing import ( - Any, - ClassVar, - Dict, - Protocol, - Sequence, - Tuple, - Type, - TypeAlias, - TypeVar, - Union, - cast, - get_args, - get_origin, - get_type_hints, - overload, -) - -from ethereum_types.bytes import Bytes, FixedBytes -from ethereum_types.numeric import FixedUnsigned, Uint - -from ethereum.crypto.hash import Hash32, keccak256 -from ethereum.exceptions import RLPDecodingError, RLPEncodingError - - -class RLP(Protocol): - """ - [`Protocol`] that describes the requirements to be RLP-encodable. - - [`Protocol`]: https://docs.python.org/3/library/typing.html#typing.Protocol - """ - - __dataclass_fields__: ClassVar[Dict] - - -Simple: TypeAlias = Union[Sequence["Simple"], bytes] - -Extended: TypeAlias = Union[ - Sequence["Extended"], bytearray, bytes, Uint, FixedUnsigned, str, bool, RLP -] - - -# -# RLP Encode -# - - -def encode(raw_data: Extended) -> Bytes: - """ - Encodes `raw_data` into a sequence of bytes using RLP. - - Parameters - ---------- - raw_data : - A `Bytes`, `Uint`, `Uint256` or sequence of `RLP` encodable - objects. - - Returns - ------- - encoded : `ethereum.base_types.Bytes` - The RLP encoded bytes representing `raw_data`. - """ - if isinstance(raw_data, Sequence): - if isinstance(raw_data, (bytearray, bytes)): - return encode_bytes(raw_data) - elif isinstance(raw_data, str): - return encode_bytes(raw_data.encode()) - else: - return encode_sequence(raw_data) - elif isinstance(raw_data, (Uint, FixedUnsigned)): - return encode(raw_data.to_be_bytes()) - elif isinstance(raw_data, bool): - if raw_data: - return encode_bytes(b"\x01") - else: - return encode_bytes(b"") - elif is_dataclass(raw_data): - return encode(astuple(raw_data)) - else: - raise RLPEncodingError( - "RLP Encoding of type {} is not supported".format(type(raw_data)) - ) - - -def encode_bytes(raw_bytes: Bytes) -> Bytes: - """ - Encodes `raw_bytes`, a sequence of bytes, using RLP. - - Parameters - ---------- - raw_bytes : - Bytes to encode with RLP. - - Returns - ------- - encoded : `ethereum.base_types.Bytes` - The RLP encoded bytes representing `raw_bytes`. - """ - len_raw_data = len(raw_bytes) - - if len_raw_data == 1 and raw_bytes[0] < 0x80: - return raw_bytes - elif len_raw_data < 0x38: - return bytes([0x80 + len_raw_data]) + raw_bytes - else: - # length of raw data represented as big endian bytes - len_raw_data_as_be = Uint(len_raw_data).to_be_bytes() - return ( - bytes([0xB7 + len(len_raw_data_as_be)]) - + len_raw_data_as_be - + raw_bytes - ) - - -def encode_sequence(raw_sequence: Sequence[Extended]) -> Bytes: - """ - Encodes a list of RLP encodable objects (`raw_sequence`) using RLP. - - Parameters - ---------- - raw_sequence : - Sequence of RLP encodable objects. - - Returns - ------- - encoded : `ethereum.base_types.Bytes` - The RLP encoded bytes representing `raw_sequence`. - """ - joined_encodings = get_joined_encodings(raw_sequence) - len_joined_encodings = len(joined_encodings) - - if len_joined_encodings < 0x38: - return Bytes([0xC0 + len_joined_encodings]) + joined_encodings - else: - len_joined_encodings_as_be = Uint(len_joined_encodings).to_be_bytes() - return ( - Bytes([0xF7 + len(len_joined_encodings_as_be)]) - + len_joined_encodings_as_be - + joined_encodings - ) - - -def get_joined_encodings(raw_sequence: Sequence[Extended]) -> Bytes: - """ - Obtain concatenation of rlp encoding for each item in the sequence - raw_sequence. - - Parameters - ---------- - raw_sequence : - Sequence to encode with RLP. - - Returns - ------- - joined_encodings : `ethereum.base_types.Bytes` - The concatenated RLP encoded bytes for each item in sequence - raw_sequence. - """ - return b"".join(encode(item) for item in raw_sequence) - - -# -# RLP Decode -# - - -def decode(encoded_data: Bytes) -> Simple: - """ - Decodes an integer, byte sequence, or list of RLP encodable objects - from the byte sequence `encoded_data`, using RLP. - - Parameters - ---------- - encoded_data : - A sequence of bytes, in RLP form. - - Returns - ------- - decoded_data : `RLP` - Object decoded from `encoded_data`. - """ - if len(encoded_data) <= 0: - raise RLPDecodingError("Cannot decode empty bytestring") - - if encoded_data[0] <= 0xBF: - # This means that the raw data is of type bytes - return decode_to_bytes(encoded_data) - else: - # This means that the raw data is of type sequence - return decode_to_sequence(encoded_data) - - -U = TypeVar("U", bound=Extended) - - -def decode_to(cls: Type[U], encoded_data: Bytes) -> U: - """ - Decode the bytes in `encoded_data` to an object of type `cls`. `cls` can be - a `Bytes` subclass, a dataclass, `Uint`, `U256` or `Tuple[cls]`. - - Parameters - ---------- - cls: `Type[U]` - The type to decode to. - encoded_data : - A sequence of bytes, in RLP form. - - Returns - ------- - decoded_data : `U` - Object decoded from `encoded_data`. - """ - decoded = decode(encoded_data) - return _deserialize_to(cls, decoded) - - -@overload -def _deserialize_to(class_: Type[U], value: Simple) -> U: - pass - - -@overload -def _deserialize_to(class_: object, value: Simple) -> Extended: - pass - - -def _deserialize_to(class_: object, value: Simple) -> Extended: - if not isinstance(class_, type): - return _deserialize_to_annotation(class_, value) - elif is_dataclass(class_): - return _deserialize_to_dataclass(class_, value) - elif issubclass(class_, (Uint, FixedUnsigned)): - return _deserialize_to_uint(class_, value) - elif issubclass(class_, (Bytes, FixedBytes)): - return _deserialize_to_bytes(class_, value) - elif class_ is bool: - return _deserialize_to_bool(value) - else: - raise NotImplementedError(class_) - - -def _deserialize_to_dataclass(cls: Type[U], decoded: Simple) -> U: - assert is_dataclass(cls) - hints = get_type_hints(cls) - target_fields = fields(cls) - - if isinstance(decoded, bytes): - raise RLPDecodingError(f"got `bytes` while decoding `{cls.__name__}`") - - if len(target_fields) != len(decoded): - name = cls.__name__ - actual = len(decoded) - expected = len(target_fields) - raise RLPDecodingError( - f"`{name}` needs {expected} field(s), but got {actual} instead" - ) - - values: Dict[str, Any] = {} - - for value, target_field in zip(decoded, target_fields): - resolved_type = hints[target_field.name] - values[target_field.name] = _deserialize_to(resolved_type, value) - - result = cls(**values) - assert isinstance(result, cls) - return cast(U, result) - - -def _deserialize_to_bool(value: Simple) -> bool: - if value == b"": - return False - elif value == b"\x01": - return True - else: - raise RLPDecodingError - - -def _deserialize_to_bytes( - class_: Union[Type[Bytes], Type[FixedBytes]], value: Simple -) -> Union[Bytes, FixedBytes]: - if not isinstance(value, bytes): - raise RLPDecodingError - try: - return class_(value) - except ValueError as e: - raise RLPDecodingError from e - - -def _deserialize_to_uint( - class_: Union[Type[Uint], Type[FixedUnsigned]], decoded: Simple -) -> Union[Uint, FixedUnsigned]: - if not isinstance(decoded, bytes): - raise RLPDecodingError - try: - return class_.from_be_bytes(decoded) - except ValueError as e: - raise RLPDecodingError from e - - -def _deserialize_to_annotation(annotation: object, value: Simple) -> Extended: - origin = get_origin(annotation) - if origin is Union: - return _deserialize_to_union(annotation, value) - elif origin in (Tuple, tuple): - return _deserialize_to_tuple(annotation, value) - elif origin is None: - raise Exception(annotation) - else: - raise NotImplementedError(f"RLP non-type {origin!r}") - - -def _deserialize_to_union(annotation: object, value: Simple) -> Extended: - arguments = get_args(annotation) - successes = [] - failures = [] - for argument in arguments: - try: - success = _deserialize_to(argument, value) - except Exception as e: - failures.append(e) - continue - - successes.append(success) - - if len(successes) == 1: - return successes[0] - elif not successes: - raise RLPDecodingError(f"no matching union variant\n{failures!r}") - else: - raise RLPDecodingError("multiple matching union variants") - - -def _deserialize_to_tuple( - annotation: object, values: Simple -) -> Sequence[Extended]: - if isinstance(values, bytes): - raise RLPDecodingError - arguments = list(get_args(annotation)) - - if arguments[-1] is Ellipsis: - arguments.pop() - fill_count = len(values) - len(arguments) - arguments = list(arguments) + [arguments[-1]] * fill_count - - decoded = [] - for argument, value in zip(arguments, values): - decoded.append(_deserialize_to(argument, value)) - - return tuple(decoded) - - -def decode_to_bytes(encoded_bytes: Bytes) -> Bytes: - """ - Decodes a rlp encoded byte stream assuming that the decoded data - should be of type `bytes`. - - Parameters - ---------- - encoded_bytes : - RLP encoded byte stream. - - Returns - ------- - decoded : `ethereum.base_types.Bytes` - RLP decoded Bytes data - """ - if len(encoded_bytes) == 1 and encoded_bytes[0] < 0x80: - return encoded_bytes - elif encoded_bytes[0] <= 0xB7: - len_raw_data = encoded_bytes[0] - 0x80 - if len_raw_data >= len(encoded_bytes): - raise RLPDecodingError - raw_data = encoded_bytes[1 : 1 + len_raw_data] - if len_raw_data == 1 and raw_data[0] < 0x80: - raise RLPDecodingError - return raw_data - else: - # This is the index in the encoded data at which decoded data - # starts from. - decoded_data_start_idx = 1 + encoded_bytes[0] - 0xB7 - if decoded_data_start_idx - 1 >= len(encoded_bytes): - raise RLPDecodingError - if encoded_bytes[1] == 0: - raise RLPDecodingError - len_decoded_data = int( - Uint.from_be_bytes(encoded_bytes[1:decoded_data_start_idx]) - ) - if len_decoded_data < 0x38: - raise RLPDecodingError - decoded_data_end_idx = decoded_data_start_idx + int(len_decoded_data) - if decoded_data_end_idx - 1 >= len(encoded_bytes): - raise RLPDecodingError - return encoded_bytes[decoded_data_start_idx:decoded_data_end_idx] - - -def decode_to_sequence(encoded_sequence: Bytes) -> Sequence[Simple]: - """ - Decodes a rlp encoded byte stream assuming that the decoded data - should be of type `Sequence` of objects. - - Parameters - ---------- - encoded_sequence : - An RLP encoded Sequence. - - Returns - ------- - decoded : `Sequence[RLP]` - Sequence of objects decoded from `encoded_sequence`. - """ - if encoded_sequence[0] <= 0xF7: - len_joined_encodings = encoded_sequence[0] - 0xC0 - if len_joined_encodings >= len(encoded_sequence): - raise RLPDecodingError - joined_encodings = encoded_sequence[1 : 1 + len_joined_encodings] - else: - joined_encodings_start_idx = 1 + encoded_sequence[0] - 0xF7 - if joined_encodings_start_idx - 1 >= len(encoded_sequence): - raise RLPDecodingError - if encoded_sequence[1] == 0: - raise RLPDecodingError - len_joined_encodings = int( - Uint.from_be_bytes(encoded_sequence[1:joined_encodings_start_idx]) - ) - if len_joined_encodings < 0x38: - raise RLPDecodingError - joined_encodings_end_idx = ( - joined_encodings_start_idx + len_joined_encodings - ) - if joined_encodings_end_idx - 1 >= len(encoded_sequence): - raise RLPDecodingError - joined_encodings = encoded_sequence[ - joined_encodings_start_idx:joined_encodings_end_idx - ] - - return decode_joined_encodings(joined_encodings) - - -def decode_joined_encodings(joined_encodings: Bytes) -> Sequence[Simple]: - """ - Decodes `joined_encodings`, which is a concatenation of RLP encoded - objects. - - Parameters - ---------- - joined_encodings : - concatenation of RLP encoded objects - - Returns - ------- - decoded : `List[RLP]` - A list of objects decoded from `joined_encodings`. - """ - decoded_sequence = [] - - item_start_idx = 0 - while item_start_idx < len(joined_encodings): - encoded_item_length = decode_item_length( - joined_encodings[item_start_idx:] - ) - if item_start_idx + encoded_item_length - 1 >= len(joined_encodings): - raise RLPDecodingError - encoded_item = joined_encodings[ - item_start_idx : item_start_idx + encoded_item_length - ] - decoded_sequence.append(decode(encoded_item)) - item_start_idx += encoded_item_length - - return decoded_sequence - - -def decode_item_length(encoded_data: Bytes) -> int: - """ - Find the length of the rlp encoding for the first object in the - encoded sequence. - Here `encoded_data` refers to concatenation of rlp encoding for each - item in a sequence. - - NOTE - This is a helper function not described in the spec. It was - introduced as the spec doesn't discuss about decoding the RLP encoded - data. - - Parameters - ---------- - encoded_data : - RLP encoded data for a sequence of objects. - - Returns - ------- - rlp_length : `int` - """ - if len(encoded_data) <= 0: - raise RLPDecodingError - - first_rlp_byte = encoded_data[0] - - # This is the length of the big endian representation of the length of - # rlp encoded object byte stream. - length_length = 0 - decoded_data_length = 0 - - # This occurs only when the raw_data is a single byte whose value < 128 - if first_rlp_byte < 0x80: - # We return 1 here, as the end formula - # 1 + length_length + decoded_data_length would be invalid for - # this case. - return 1 - # This occurs only when the raw_data is a byte stream with length < 56 - # and doesn't fall into the above cases - elif first_rlp_byte <= 0xB7: - decoded_data_length = first_rlp_byte - 0x80 - # This occurs only when the raw_data is a byte stream and doesn't fall - # into the above cases - elif first_rlp_byte <= 0xBF: - length_length = first_rlp_byte - 0xB7 - if length_length >= len(encoded_data): - raise RLPDecodingError - if encoded_data[1] == 0: - raise RLPDecodingError - decoded_data_length = int( - Uint.from_be_bytes(encoded_data[1 : 1 + length_length]) - ) - # This occurs only when the raw_data is a sequence of objects with - # length(concatenation of encoding of each object) < 56 - elif first_rlp_byte <= 0xF7: - decoded_data_length = first_rlp_byte - 0xC0 - # This occurs only when the raw_data is a sequence of objects and - # doesn't fall into the above cases. - elif first_rlp_byte <= 0xFF: - length_length = first_rlp_byte - 0xF7 - if length_length >= len(encoded_data): - raise RLPDecodingError - if encoded_data[1] == 0: - raise RLPDecodingError - decoded_data_length = int( - Uint.from_be_bytes(encoded_data[1 : 1 + length_length]) - ) - - return 1 + length_length + decoded_data_length - - -def rlp_hash(data: Extended) -> Hash32: - """ - Obtain the keccak-256 hash of the rlp encoding of the passed in data. - - Parameters - ---------- - data : - The data for which we need the rlp hash. - - Returns - ------- - hash : `Hash32` - The rlp hash of the passed in data. - """ - return keccak256(encode(data)) diff --git a/src/ethereum/shanghai/fork.py b/src/ethereum/shanghai/fork.py index 0133b64c87..d0347b7d1e 100644 --- a/src/ethereum/shanghai/fork.py +++ b/src/ethereum/shanghai/fork.py @@ -15,13 +15,13 @@ from dataclasses import dataclass from typing import List, Optional, Tuple, Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes32 from ethereum_types.numeric import U64, U256, Uint from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidBlock, InvalidSenderError -from .. import rlp from . import vm from .blocks import Block, Header, Log, Receipt, Withdrawal from .bloom import logs_bloom diff --git a/src/ethereum/shanghai/fork_types.py b/src/ethereum/shanghai/fork_types.py index 05f34f691b..00817ba506 100644 --- a/src/ethereum/shanghai/fork_types.py +++ b/src/ethereum/shanghai/fork_types.py @@ -14,11 +14,11 @@ from dataclasses import dataclass +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes20, Bytes256 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint -from .. import rlp from ..crypto.hash import Hash32, keccak256 Address = Bytes20 diff --git a/src/ethereum/shanghai/transactions.py b/src/ethereum/shanghai/transactions.py index dad2bbd956..efd3b2fcda 100644 --- a/src/ethereum/shanghai/transactions.py +++ b/src/ethereum/shanghai/transactions.py @@ -6,6 +6,7 @@ from dataclasses import dataclass from typing import Tuple, Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes32 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U64, U256, Uint @@ -14,7 +15,6 @@ from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidSignatureError -from .. import rlp from .exceptions import TransactionTypeError from .fork_types import Address diff --git a/src/ethereum/shanghai/trie.py b/src/ethereum/shanghai/trie.py index 020e9900c4..6a1d563593 100644 --- a/src/ethereum/shanghai/trie.py +++ b/src/ethereum/shanghai/trie.py @@ -28,6 +28,7 @@ Union, ) +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint @@ -36,7 +37,6 @@ from ethereum.paris import trie as previous_trie from ethereum.utils.hexadecimal import hex_to_bytes -from .. import rlp from .blocks import Receipt, Withdrawal from .fork_types import Account, Address, Root, encode_account from .transactions import LegacyTransaction diff --git a/src/ethereum/shanghai/utils/address.py b/src/ethereum/shanghai/utils/address.py index 96f1949a05..57d049acc2 100644 --- a/src/ethereum/shanghai/utils/address.py +++ b/src/ethereum/shanghai/utils/address.py @@ -14,13 +14,13 @@ """ from typing import Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes32 from ethereum_types.numeric import U256, Uint from ethereum.crypto.hash import keccak256 from ethereum.utils.byte import left_pad_zero_bytes -from ... import rlp from ..fork_types import Address diff --git a/src/ethereum/spurious_dragon/fork.py b/src/ethereum/spurious_dragon/fork.py index 3e358272fc..cb4d0b0702 100644 --- a/src/ethereum/spurious_dragon/fork.py +++ b/src/ethereum/spurious_dragon/fork.py @@ -15,6 +15,7 @@ from dataclasses import dataclass from typing import List, Optional, Set, Tuple +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes32 from ethereum_types.numeric import U64, U256, Uint @@ -22,7 +23,6 @@ from ethereum.ethash import dataset_size, generate_cache, hashimoto_light from ethereum.exceptions import InvalidBlock, InvalidSenderError -from .. import rlp from . import vm from .blocks import Block, Header, Log, Receipt from .bloom import logs_bloom @@ -266,7 +266,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32: header.extra_data, ) - return rlp.rlp_hash(header_data_without_pow_artefacts) + return keccak256(rlp.encode(header_data_without_pow_artefacts)) def validate_proof_of_work(header: Header) -> None: @@ -530,8 +530,8 @@ def validate_ommers( chain : History and current state. """ - block_hash = rlp.rlp_hash(block_header) - if rlp.rlp_hash(ommers) != block_header.ommers_hash: + block_hash = keccak256(rlp.encode(block_header)) + if keccak256(rlp.encode(ommers)) != block_header.ommers_hash: raise InvalidBlock if len(ommers) == 0: @@ -549,18 +549,19 @@ def validate_ommers( if len(ommers) > 2: raise InvalidBlock - ommers_hashes = [rlp.rlp_hash(ommer) for ommer in ommers] + ommers_hashes = [keccak256(rlp.encode(ommer)) for ommer in ommers] if len(ommers_hashes) != len(set(ommers_hashes)): raise InvalidBlock recent_canonical_blocks = chain.blocks[-(MAX_OMMER_DEPTH + Uint(1)) :] recent_canonical_block_hashes = { - rlp.rlp_hash(block.header) for block in recent_canonical_blocks + keccak256(rlp.encode(block.header)) + for block in recent_canonical_blocks } recent_ommers_hashes: Set[Hash32] = set() for block in recent_canonical_blocks: recent_ommers_hashes = recent_ommers_hashes.union( - {rlp.rlp_hash(ommer) for ommer in block.ommers} + {keccak256(rlp.encode(ommer)) for ommer in block.ommers} ) for ommer_index, ommer in enumerate(ommers): diff --git a/src/ethereum/spurious_dragon/fork_types.py b/src/ethereum/spurious_dragon/fork_types.py index 05f34f691b..00817ba506 100644 --- a/src/ethereum/spurious_dragon/fork_types.py +++ b/src/ethereum/spurious_dragon/fork_types.py @@ -14,11 +14,11 @@ from dataclasses import dataclass +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes20, Bytes256 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint -from .. import rlp from ..crypto.hash import Hash32, keccak256 Address = Bytes20 diff --git a/src/ethereum/spurious_dragon/transactions.py b/src/ethereum/spurious_dragon/transactions.py index da6aff2c1b..142929587d 100644 --- a/src/ethereum/spurious_dragon/transactions.py +++ b/src/ethereum/spurious_dragon/transactions.py @@ -6,6 +6,7 @@ from dataclasses import dataclass from typing import Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U64, U256, Uint @@ -14,7 +15,6 @@ from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidSignatureError -from .. import rlp from .fork_types import Address TX_BASE_COST = 21000 diff --git a/src/ethereum/spurious_dragon/trie.py b/src/ethereum/spurious_dragon/trie.py index 1b13975d22..50e3e69c1f 100644 --- a/src/ethereum/spurious_dragon/trie.py +++ b/src/ethereum/spurious_dragon/trie.py @@ -28,6 +28,7 @@ Union, ) +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint @@ -36,7 +37,6 @@ from ethereum.tangerine_whistle import trie as previous_trie from ethereum.utils.hexadecimal import hex_to_bytes -from .. import rlp from .blocks import Receipt from .fork_types import Account, Address, Root, encode_account from .transactions import Transaction diff --git a/src/ethereum/spurious_dragon/utils/address.py b/src/ethereum/spurious_dragon/utils/address.py index 56f2108cda..8f8c9ea503 100644 --- a/src/ethereum/spurious_dragon/utils/address.py +++ b/src/ethereum/spurious_dragon/utils/address.py @@ -14,12 +14,12 @@ """ from typing import Union +from ethereum_rlp import rlp from ethereum_types.numeric import U256, Uint from ethereum.crypto.hash import keccak256 from ethereum.utils.byte import left_pad_zero_bytes -from ... import rlp from ..fork_types import Address diff --git a/src/ethereum/tangerine_whistle/fork.py b/src/ethereum/tangerine_whistle/fork.py index 8e957c7424..1419c87b6d 100644 --- a/src/ethereum/tangerine_whistle/fork.py +++ b/src/ethereum/tangerine_whistle/fork.py @@ -15,6 +15,7 @@ from dataclasses import dataclass from typing import List, Optional, Set, Tuple +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes32 from ethereum_types.numeric import U64, U256, Uint @@ -22,7 +23,6 @@ from ethereum.ethash import dataset_size, generate_cache, hashimoto_light from ethereum.exceptions import InvalidBlock, InvalidSenderError -from .. import rlp from . import vm from .blocks import Block, Header, Log, Receipt from .bloom import logs_bloom @@ -264,7 +264,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32: header.extra_data, ) - return rlp.rlp_hash(header_data_without_pow_artefacts) + return keccak256(rlp.encode(header_data_without_pow_artefacts)) def validate_proof_of_work(header: Header) -> None: @@ -522,8 +522,8 @@ def validate_ommers( chain : History and current state. """ - block_hash = rlp.rlp_hash(block_header) - if rlp.rlp_hash(ommers) != block_header.ommers_hash: + block_hash = keccak256(rlp.encode(block_header)) + if keccak256(rlp.encode(ommers)) != block_header.ommers_hash: raise InvalidBlock if len(ommers) == 0: @@ -541,18 +541,19 @@ def validate_ommers( if len(ommers) > 2: raise InvalidBlock - ommers_hashes = [rlp.rlp_hash(ommer) for ommer in ommers] + ommers_hashes = [keccak256(rlp.encode(ommer)) for ommer in ommers] if len(ommers_hashes) != len(set(ommers_hashes)): raise InvalidBlock recent_canonical_blocks = chain.blocks[-(MAX_OMMER_DEPTH + Uint(1)) :] recent_canonical_block_hashes = { - rlp.rlp_hash(block.header) for block in recent_canonical_blocks + keccak256(rlp.encode(block.header)) + for block in recent_canonical_blocks } recent_ommers_hashes: Set[Hash32] = set() for block in recent_canonical_blocks: recent_ommers_hashes = recent_ommers_hashes.union( - {rlp.rlp_hash(ommer) for ommer in block.ommers} + {keccak256(rlp.encode(ommer)) for ommer in block.ommers} ) for ommer_index, ommer in enumerate(ommers): diff --git a/src/ethereum/tangerine_whistle/fork_types.py b/src/ethereum/tangerine_whistle/fork_types.py index 05f34f691b..00817ba506 100644 --- a/src/ethereum/tangerine_whistle/fork_types.py +++ b/src/ethereum/tangerine_whistle/fork_types.py @@ -14,11 +14,11 @@ from dataclasses import dataclass +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes20, Bytes256 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint -from .. import rlp from ..crypto.hash import Hash32, keccak256 Address = Bytes20 diff --git a/src/ethereum/tangerine_whistle/transactions.py b/src/ethereum/tangerine_whistle/transactions.py index a9caf4990f..57d697d615 100644 --- a/src/ethereum/tangerine_whistle/transactions.py +++ b/src/ethereum/tangerine_whistle/transactions.py @@ -6,6 +6,7 @@ from dataclasses import dataclass from typing import Union +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0 from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U64, U256, Uint @@ -14,7 +15,6 @@ from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidSignatureError -from .. import rlp from .fork_types import Address TX_BASE_COST = 21000 diff --git a/src/ethereum/tangerine_whistle/trie.py b/src/ethereum/tangerine_whistle/trie.py index 884faff508..f9d3d6ed97 100644 --- a/src/ethereum/tangerine_whistle/trie.py +++ b/src/ethereum/tangerine_whistle/trie.py @@ -28,6 +28,7 @@ Union, ) +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.frozen import slotted_freezable from ethereum_types.numeric import U256, Uint @@ -36,7 +37,6 @@ from ethereum.dao_fork import trie as previous_trie from ethereum.utils.hexadecimal import hex_to_bytes -from .. import rlp from .blocks import Receipt from .fork_types import Account, Address, Root, encode_account from .transactions import Transaction diff --git a/src/ethereum/tangerine_whistle/utils/address.py b/src/ethereum/tangerine_whistle/utils/address.py index 1bbcb3ea70..8853e91c9b 100644 --- a/src/ethereum/tangerine_whistle/utils/address.py +++ b/src/ethereum/tangerine_whistle/utils/address.py @@ -14,12 +14,12 @@ """ from typing import Union +from ethereum_rlp import rlp from ethereum_types.numeric import U256, Uint from ethereum.crypto.hash import keccak256 from ethereum.utils.byte import left_pad_zero_bytes -from ... import rlp from ..fork_types import Address diff --git a/src/ethereum_spec_tools/evm_tools/b11r/__init__.py b/src/ethereum_spec_tools/evm_tools/b11r/__init__.py index f86c533524..e106af5cd7 100644 --- a/src/ethereum_spec_tools/evm_tools/b11r/__init__.py +++ b/src/ethereum_spec_tools/evm_tools/b11r/__init__.py @@ -6,9 +6,10 @@ import json from typing import Optional, TextIO +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes32 -from ethereum import rlp +from ethereum.crypto.hash import keccak256 from ..utils import get_stream_logger from .b11r_types import Body, Header @@ -111,7 +112,7 @@ def build_block(self) -> None: block.append(self.body.withdrawals) self.block_rlp = rlp.encode(block) - self.block_hash = rlp.rlp_hash(header_to_list) + self.block_hash = keccak256(rlp.encode(header_to_list)) def run(self) -> int: """ diff --git a/src/ethereum_spec_tools/evm_tools/b11r/b11r_types.py b/src/ethereum_spec_tools/evm_tools/b11r/b11r_types.py index 3c34883012..b8b99b4fb7 100644 --- a/src/ethereum_spec_tools/evm_tools/b11r/b11r_types.py +++ b/src/ethereum_spec_tools/evm_tools/b11r/b11r_types.py @@ -4,10 +4,10 @@ import json from typing import Any, List, Optional, Tuple +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes8, Bytes20, Bytes32, Bytes256 from ethereum_types.numeric import U64, U256, Uint -from ethereum import rlp from ethereum.crypto.hash import Hash32, keccak256 from ethereum.utils.hexadecimal import hex_to_bytes, hex_to_bytes8 diff --git a/src/ethereum_spec_tools/evm_tools/loaders/fixture_loader.py b/src/ethereum_spec_tools/evm_tools/loaders/fixture_loader.py index 8aa936c7ab..db6c5ade55 100644 --- a/src/ethereum_spec_tools/evm_tools/loaders/fixture_loader.py +++ b/src/ethereum_spec_tools/evm_tools/loaders/fixture_loader.py @@ -7,10 +7,10 @@ from abc import ABC, abstractmethod from typing import Any, Tuple +from ethereum_rlp import rlp from ethereum_types.numeric import U256 -from ethereum import rlp -from ethereum.crypto.hash import Hash32 +from ethereum.crypto.hash import Hash32, keccak256 from ethereum.utils.hexadecimal import ( hex_to_bytes, hex_to_bytes8, @@ -99,7 +99,7 @@ def json_to_block( # Always decode from rlp block_rlp = hex_to_bytes(json_block["rlp"]) block = rlp.decode_to(self.fork.Block, block_rlp) - block_header_hash = rlp.rlp_hash(block.header) + block_header_hash = keccak256(rlp.encode(block.header)) return block, block_header_hash, block_rlp header = self.json_to_header(json_block["blockHeader"]) diff --git a/src/ethereum_spec_tools/evm_tools/loaders/transaction_loader.py b/src/ethereum_spec_tools/evm_tools/loaders/transaction_loader.py index e8cb7baade..76b0c25b8c 100644 --- a/src/ethereum_spec_tools/evm_tools/loaders/transaction_loader.py +++ b/src/ethereum_spec_tools/evm_tools/loaders/transaction_loader.py @@ -6,10 +6,10 @@ from dataclasses import fields from typing import Any, List +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes32 from ethereum_types.numeric import U64, U256, Uint -from ethereum import rlp from ethereum.utils.hexadecimal import ( hex_to_bytes, hex_to_bytes32, diff --git a/src/ethereum_spec_tools/evm_tools/t8n/__init__.py b/src/ethereum_spec_tools/evm_tools/t8n/__init__.py index 0f4ed931d2..a5c2be531f 100644 --- a/src/ethereum_spec_tools/evm_tools/t8n/__init__.py +++ b/src/ethereum_spec_tools/evm_tools/t8n/__init__.py @@ -8,9 +8,10 @@ from functools import partial from typing import Any, TextIO +from ethereum_rlp import rlp from ethereum_types.numeric import U64, U256, Uint -from ethereum import rlp, trace +from ethereum import trace from ethereum.crypto.hash import keccak256 from ethereum.exceptions import EthereumException, InvalidBlock from ethereum_spec_tools.forks import Hardfork diff --git a/src/ethereum_spec_tools/evm_tools/t8n/env.py b/src/ethereum_spec_tools/evm_tools/t8n/env.py index a52dd43c0f..802c524304 100644 --- a/src/ethereum_spec_tools/evm_tools/t8n/env.py +++ b/src/ethereum_spec_tools/evm_tools/t8n/env.py @@ -5,10 +5,10 @@ from dataclasses import dataclass from typing import TYPE_CHECKING, Any, Dict, List, Optional +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes32 from ethereum_types.numeric import U64, U256, Uint -from ethereum import rlp from ethereum.crypto.hash import Hash32, keccak256 from ethereum.utils.byte import left_pad_zero_bytes from ethereum.utils.hexadecimal import hex_to_bytes diff --git a/src/ethereum_spec_tools/evm_tools/t8n/t8n_types.py b/src/ethereum_spec_tools/evm_tools/t8n/t8n_types.py index 6a0aa1ab8b..404f96bfe9 100644 --- a/src/ethereum_spec_tools/evm_tools/t8n/t8n_types.py +++ b/src/ethereum_spec_tools/evm_tools/t8n/t8n_types.py @@ -5,10 +5,10 @@ from dataclasses import dataclass from typing import TYPE_CHECKING, Any, Dict, Iterator, List, Optional, Tuple +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes from ethereum_types.numeric import U64, U256, Uint -from ethereum import rlp from ethereum.crypto.hash import keccak256 from ethereum.utils.hexadecimal import hex_to_bytes, hex_to_u256, hex_to_uint diff --git a/tests/berlin/test_ethash.py b/tests/berlin/test_ethash.py index 503de96b4f..00f3cbfa57 100644 --- a/tests/berlin/test_ethash.py +++ b/tests/berlin/test_ethash.py @@ -3,9 +3,9 @@ from typing import Any, Dict, List, cast import pytest +from ethereum_rlp import rlp from ethereum_types.numeric import Uint -from ethereum import rlp from ethereum.berlin.blocks import Header from ethereum.berlin.fork import ( generate_header_hash_for_pow, diff --git a/tests/berlin/test_rlp.py b/tests/berlin/test_rlp.py index dbc5a8873b..e853f4e577 100644 --- a/tests/berlin/test_rlp.py +++ b/tests/berlin/test_rlp.py @@ -1,8 +1,8 @@ import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes8 from ethereum_types.numeric import U64, U256, Uint -import ethereum.rlp as rlp from ethereum.berlin.blocks import Block, Header, Log, Receipt from ethereum.berlin.transactions import ( AccessListTransaction, diff --git a/tests/berlin/test_state_transition.py b/tests/berlin/test_state_transition.py index fe3487f045..aa8e03e06f 100644 --- a/tests/berlin/test_state_transition.py +++ b/tests/berlin/test_state_transition.py @@ -2,11 +2,11 @@ from typing import Dict import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes8, Bytes32 from ethereum_types.numeric import U256, Uint -from ethereum import rlp -from ethereum.crypto.hash import Hash32 +from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidBlock from tests.helpers import TEST_FIXTURES from tests.helpers.load_state_tests import ( @@ -131,7 +131,7 @@ def test_transaction_with_insufficient_balance_for_value() -> None: "0b22b0d49035cb4f8a969d584f36126e0ac6996b9db7264ac5a192b8698177eb" ) - assert rlp.rlp_hash(genesis_header) == genesis_header_hash + assert keccak256(rlp.encode(genesis_header)) == genesis_header_hash genesis_block = FIXTURES_LOADER.fork.Block( genesis_header, diff --git a/tests/berlin/test_transaction.py b/tests/berlin/test_transaction.py index ab49e80f0f..9c86d1643b 100644 --- a/tests/berlin/test_transaction.py +++ b/tests/berlin/test_transaction.py @@ -1,8 +1,8 @@ from functools import partial import pytest +from ethereum_rlp import rlp -from ethereum import rlp from ethereum.berlin.fork import calculate_intrinsic_cost, validate_transaction from ethereum.berlin.transactions import LegacyTransaction from ethereum.utils.hexadecimal import hex_to_uint diff --git a/tests/byzantium/test_ethash.py b/tests/byzantium/test_ethash.py index 31f5e39641..409ed6b986 100644 --- a/tests/byzantium/test_ethash.py +++ b/tests/byzantium/test_ethash.py @@ -3,9 +3,9 @@ from typing import Any, Dict, List, cast import pytest +from ethereum_rlp import rlp from ethereum_types.numeric import Uint -from ethereum import rlp from ethereum.byzantium.blocks import Header from ethereum.byzantium.fork import ( generate_header_hash_for_pow, diff --git a/tests/byzantium/test_rlp.py b/tests/byzantium/test_rlp.py index 0a1cae29c4..fa0985ac9c 100644 --- a/tests/byzantium/test_rlp.py +++ b/tests/byzantium/test_rlp.py @@ -1,8 +1,8 @@ import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes8 from ethereum_types.numeric import U256, Uint -import ethereum.rlp as rlp from ethereum.byzantium.blocks import Block, Header, Log, Receipt from ethereum.byzantium.transactions import Transaction from ethereum.byzantium.utils.hexadecimal import hex_to_address diff --git a/tests/byzantium/test_state_transition.py b/tests/byzantium/test_state_transition.py index 6d9e7317c8..c392873f78 100644 --- a/tests/byzantium/test_state_transition.py +++ b/tests/byzantium/test_state_transition.py @@ -2,11 +2,11 @@ from typing import Dict import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes8, Bytes32 from ethereum_types.numeric import U256, Uint -from ethereum import rlp -from ethereum.crypto.hash import Hash32 +from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidBlock from tests.helpers import TEST_FIXTURES from tests.helpers.load_state_tests import ( @@ -136,7 +136,7 @@ def test_transaction_with_insufficient_balance_for_value() -> None: "0b22b0d49035cb4f8a969d584f36126e0ac6996b9db7264ac5a192b8698177eb" ) - assert rlp.rlp_hash(genesis_header) == genesis_header_hash + assert keccak256(rlp.encode(genesis_header)) == genesis_header_hash genesis_block = FIXTURES_LOADER.fork.Block( genesis_header, diff --git a/tests/byzantium/test_transaction.py b/tests/byzantium/test_transaction.py index 8d8f8b5ea6..a133df527d 100644 --- a/tests/byzantium/test_transaction.py +++ b/tests/byzantium/test_transaction.py @@ -1,8 +1,8 @@ from functools import partial import pytest +from ethereum_rlp import rlp -from ethereum import rlp from ethereum.byzantium.fork import ( calculate_intrinsic_cost, validate_transaction, diff --git a/tests/cancun/test_rlp.py b/tests/cancun/test_rlp.py index e070ca566a..6478fe25d9 100644 --- a/tests/cancun/test_rlp.py +++ b/tests/cancun/test_rlp.py @@ -1,8 +1,8 @@ import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes8, Bytes32 from ethereum_types.numeric import U64, U256, Uint -import ethereum.rlp as rlp from ethereum.cancun.blocks import Block, Header, Log, Receipt, Withdrawal from ethereum.cancun.transactions import ( AccessListTransaction, diff --git a/tests/constantinople/test_ethash.py b/tests/constantinople/test_ethash.py index 7712029398..f608de94f9 100644 --- a/tests/constantinople/test_ethash.py +++ b/tests/constantinople/test_ethash.py @@ -3,9 +3,9 @@ from typing import Any, Dict, List, cast import pytest +from ethereum_rlp import rlp from ethereum_types.numeric import Uint -from ethereum import rlp from ethereum.constantinople.blocks import Header from ethereum.constantinople.fork import ( generate_header_hash_for_pow, diff --git a/tests/constantinople/test_rlp.py b/tests/constantinople/test_rlp.py index 4a6b5d2df9..bdb13cc502 100644 --- a/tests/constantinople/test_rlp.py +++ b/tests/constantinople/test_rlp.py @@ -1,8 +1,8 @@ import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes8 from ethereum_types.numeric import U256, Uint -import ethereum.rlp as rlp from ethereum.constantinople.blocks import Block, Header, Log, Receipt from ethereum.constantinople.transactions import Transaction from ethereum.constantinople.utils.hexadecimal import hex_to_address diff --git a/tests/constantinople/test_state_transition.py b/tests/constantinople/test_state_transition.py index 67b29460ac..1eda69979c 100644 --- a/tests/constantinople/test_state_transition.py +++ b/tests/constantinople/test_state_transition.py @@ -2,11 +2,11 @@ from typing import Dict import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes8, Bytes32 from ethereum_types.numeric import U256, Uint -from ethereum import rlp -from ethereum.crypto.hash import Hash32 +from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidBlock from tests.helpers import TEST_FIXTURES from tests.helpers.load_state_tests import ( @@ -139,7 +139,7 @@ def test_transaction_with_insufficient_balance_for_value() -> None: "0b22b0d49035cb4f8a969d584f36126e0ac6996b9db7264ac5a192b8698177eb" ) - assert rlp.rlp_hash(genesis_header) == genesis_header_hash + assert keccak256(rlp.encode(genesis_header)) == genesis_header_hash genesis_block = FIXTURES_LOADER.fork.Block( genesis_header, diff --git a/tests/constantinople/test_transaction.py b/tests/constantinople/test_transaction.py index 1f03190047..0916fedcbd 100644 --- a/tests/constantinople/test_transaction.py +++ b/tests/constantinople/test_transaction.py @@ -1,8 +1,8 @@ from functools import partial import pytest +from ethereum_rlp import rlp -from ethereum import rlp from ethereum.constantinople.fork import ( calculate_intrinsic_cost, validate_transaction, diff --git a/tests/frontier/test_ethash.py b/tests/frontier/test_ethash.py index 124708982c..64fc675866 100644 --- a/tests/frontier/test_ethash.py +++ b/tests/frontier/test_ethash.py @@ -3,9 +3,9 @@ from typing import Any, Dict, List, cast import pytest +from ethereum_rlp import rlp from ethereum_types.numeric import Uint -from ethereum import rlp from ethereum.crypto.hash import keccak256 from ethereum.ethash import ( cache_size, diff --git a/tests/frontier/test_rlp.py b/tests/frontier/test_rlp.py index f25223e9ef..c39b6ae15e 100644 --- a/tests/frontier/test_rlp.py +++ b/tests/frontier/test_rlp.py @@ -1,8 +1,8 @@ import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes8 from ethereum_types.numeric import U256, Uint -import ethereum.rlp as rlp from ethereum.crypto.hash import keccak256 from ethereum.frontier.blocks import Block, Header, Log, Receipt from ethereum.frontier.transactions import Transaction diff --git a/tests/frontier/test_state_transition.py b/tests/frontier/test_state_transition.py index 048c90166e..840fe3a9ca 100644 --- a/tests/frontier/test_state_transition.py +++ b/tests/frontier/test_state_transition.py @@ -2,11 +2,11 @@ from typing import Dict import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes8, Bytes32 from ethereum_types.numeric import U256, Uint -from ethereum import rlp -from ethereum.crypto.hash import Hash32 +from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidBlock from tests.helpers import TEST_FIXTURES from tests.helpers.load_state_tests import ( @@ -130,7 +130,7 @@ def test_transaction_with_insufficient_balance_for_value() -> None: "0b22b0d49035cb4f8a969d584f36126e0ac6996b9db7264ac5a192b8698177eb" ) - assert rlp.rlp_hash(genesis_header) == genesis_header_hash + assert keccak256(rlp.encode(genesis_header)) == genesis_header_hash genesis_block = FIXTURES_LOADER.fork.Block( genesis_header, diff --git a/tests/frontier/test_transaction.py b/tests/frontier/test_transaction.py index 59f524565b..6b0c12f2e8 100644 --- a/tests/frontier/test_transaction.py +++ b/tests/frontier/test_transaction.py @@ -1,8 +1,8 @@ from functools import partial import pytest +from ethereum_rlp import rlp -from ethereum import rlp from ethereum.frontier.fork import ( calculate_intrinsic_cost, validate_transaction, diff --git a/tests/helpers/load_state_tests.py b/tests/helpers/load_state_tests.py index e8ffe5b1b2..eca1953ae3 100644 --- a/tests/helpers/load_state_tests.py +++ b/tests/helpers/load_state_tests.py @@ -8,9 +8,11 @@ import pytest from _pytest.mark.structures import ParameterSet +from ethereum_rlp import rlp +from ethereum_rlp.exceptions import RLPException from ethereum_types.numeric import U64 -from ethereum import rlp +from ethereum.crypto.hash import keccak256 from ethereum.exceptions import EthereumException from ethereum.utils.hexadecimal import hex_to_bytes from ethereum_spec_tools.evm_tools.loaders.fixture_loader import Load @@ -47,7 +49,7 @@ def run_blockchain_st_test(test_case: Dict, load: Load) -> None: genesis_block = load.fork.Block(*parameters) genesis_header_hash = hex_to_bytes(json_data["genesisBlockHeader"]["hash"]) - assert rlp.rlp_hash(genesis_header) == genesis_header_hash + assert keccak256(rlp.encode(genesis_header)) == genesis_header_hash genesis_rlp = hex_to_bytes(json_data["genesisRLP"]) assert rlp.encode(genesis_block) == genesis_rlp @@ -72,14 +74,14 @@ def run_blockchain_st_test(test_case: Dict, load: Load) -> None: # TODO: Once all the specific exception types are thrown, # only `pytest.raises` the correct exception type instead of # all of them. - with pytest.raises(EthereumException): + with pytest.raises((EthereumException, RLPException)): add_block_to_chain(chain, json_block, load, mock_pow) return else: add_block_to_chain(chain, json_block, load, mock_pow) last_block_hash = hex_to_bytes(json_data["lastblockhash"]) - assert rlp.rlp_hash(chain.blocks[-1].header) == last_block_hash + assert keccak256(rlp.encode(chain.blocks[-1].header)) == last_block_hash expected_post_state = load.json_to_state(json_data["postState"]) assert chain.state == expected_post_state @@ -96,7 +98,7 @@ def add_block_to_chain( block_rlp, ) = load.json_to_block(json_block) - assert rlp.rlp_hash(block.header) == block_header_hash + assert keccak256(rlp.encode(block.header)) == block_header_hash assert rlp.encode(block) == block_rlp if not mock_pow: diff --git a/tests/helpers/load_vm_tests.py b/tests/helpers/load_vm_tests.py index ecfd081fe1..6469f58e3d 100644 --- a/tests/helpers/load_vm_tests.py +++ b/tests/helpers/load_vm_tests.py @@ -3,9 +3,9 @@ from importlib import import_module from typing import Any, List +from ethereum_rlp import rlp from ethereum_types.numeric import U64, U256, Uint -from ethereum import rlp from ethereum.crypto.hash import keccak256 from ethereum.utils.hexadecimal import ( hex_to_bytes, diff --git a/tests/homestead/test_ethash.py b/tests/homestead/test_ethash.py index e4b9164e2f..0d651d7bb5 100644 --- a/tests/homestead/test_ethash.py +++ b/tests/homestead/test_ethash.py @@ -3,9 +3,9 @@ from typing import Any, Dict, List, cast import pytest +from ethereum_rlp import rlp from ethereum_types.numeric import Uint -from ethereum import rlp from ethereum.crypto.hash import keccak256 from ethereum.ethash import ( cache_size, diff --git a/tests/homestead/test_rlp.py b/tests/homestead/test_rlp.py index 1f02865bde..647ddcafc5 100644 --- a/tests/homestead/test_rlp.py +++ b/tests/homestead/test_rlp.py @@ -1,8 +1,8 @@ import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes8 from ethereum_types.numeric import U256, Uint -import ethereum.rlp as rlp from ethereum.crypto.hash import keccak256 from ethereum.homestead.blocks import Block, Header, Log, Receipt from ethereum.homestead.transactions import Transaction diff --git a/tests/homestead/test_state_transition.py b/tests/homestead/test_state_transition.py index 51bee07ff1..978ee95512 100644 --- a/tests/homestead/test_state_transition.py +++ b/tests/homestead/test_state_transition.py @@ -2,11 +2,11 @@ from typing import Dict import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes8, Bytes32 from ethereum_types.numeric import U256, Uint -from ethereum import rlp -from ethereum.crypto.hash import Hash32 +from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidBlock from tests.helpers import TEST_FIXTURES from tests.helpers.load_state_tests import ( @@ -212,7 +212,7 @@ def test_transaction_with_insufficient_balance_for_value() -> None: "0b22b0d49035cb4f8a969d584f36126e0ac6996b9db7264ac5a192b8698177eb" ) - assert rlp.rlp_hash(genesis_header) == genesis_header_hash + assert keccak256(rlp.encode(genesis_header)) == genesis_header_hash genesis_block = FIXTURES_LOADER.fork.Block( genesis_header, diff --git a/tests/homestead/test_transaction.py b/tests/homestead/test_transaction.py index 03df4018cf..5fff1c1bbc 100644 --- a/tests/homestead/test_transaction.py +++ b/tests/homestead/test_transaction.py @@ -1,8 +1,8 @@ from functools import partial import pytest +from ethereum_rlp import rlp -from ethereum import rlp from ethereum.homestead.fork import ( calculate_intrinsic_cost, validate_transaction, diff --git a/tests/istanbul/test_ethash.py b/tests/istanbul/test_ethash.py index de1b421240..ea72cdc935 100644 --- a/tests/istanbul/test_ethash.py +++ b/tests/istanbul/test_ethash.py @@ -3,9 +3,9 @@ from typing import Any, Dict, List, cast import pytest +from ethereum_rlp import rlp from ethereum_types.numeric import Uint -from ethereum import rlp from ethereum.crypto.hash import keccak256 from ethereum.ethash import ( cache_size, diff --git a/tests/istanbul/test_rlp.py b/tests/istanbul/test_rlp.py index 84c21b03e7..3eb39041ac 100644 --- a/tests/istanbul/test_rlp.py +++ b/tests/istanbul/test_rlp.py @@ -1,8 +1,8 @@ import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes8 from ethereum_types.numeric import U256, Uint -import ethereum.rlp as rlp from ethereum.crypto.hash import keccak256 from ethereum.istanbul.blocks import Block, Header, Log, Receipt from ethereum.istanbul.transactions import Transaction diff --git a/tests/istanbul/test_state_transition.py b/tests/istanbul/test_state_transition.py index d08f60128d..c73b4525f9 100644 --- a/tests/istanbul/test_state_transition.py +++ b/tests/istanbul/test_state_transition.py @@ -2,11 +2,11 @@ from typing import Dict import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes8, Bytes32 from ethereum_types.numeric import U256, Uint -from ethereum import rlp -from ethereum.crypto.hash import Hash32 +from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidBlock from tests.helpers import TEST_FIXTURES from tests.helpers.load_state_tests import ( @@ -132,7 +132,7 @@ def test_transaction_with_insufficient_balance_for_value() -> None: "0b22b0d49035cb4f8a969d584f36126e0ac6996b9db7264ac5a192b8698177eb" ) - assert rlp.rlp_hash(genesis_header) == genesis_header_hash + assert keccak256(rlp.encode(genesis_header)) == genesis_header_hash genesis_block = FIXTURES_LOADER.fork.Block( genesis_header, diff --git a/tests/istanbul/test_transaction.py b/tests/istanbul/test_transaction.py index c2bf2e2e6d..6d0719fc3c 100644 --- a/tests/istanbul/test_transaction.py +++ b/tests/istanbul/test_transaction.py @@ -1,8 +1,8 @@ from functools import partial import pytest +from ethereum_rlp import rlp -from ethereum import rlp from ethereum.istanbul.fork import ( calculate_intrinsic_cost, validate_transaction, diff --git a/tests/london/test_rlp.py b/tests/london/test_rlp.py index 19ed3d4dd4..a18856e6e7 100644 --- a/tests/london/test_rlp.py +++ b/tests/london/test_rlp.py @@ -1,8 +1,8 @@ import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes8 from ethereum_types.numeric import U64, U256, Uint -import ethereum.rlp as rlp from ethereum.crypto.hash import keccak256 from ethereum.london.blocks import Block, Header, Log, Receipt from ethereum.london.transactions import ( diff --git a/tests/london/test_state_transition.py b/tests/london/test_state_transition.py index 75b2d14b3f..502c1fb151 100644 --- a/tests/london/test_state_transition.py +++ b/tests/london/test_state_transition.py @@ -2,11 +2,11 @@ from typing import Dict import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes8, Bytes32 from ethereum_types.numeric import U256, Uint -from ethereum import rlp -from ethereum.crypto.hash import Hash32 +from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidBlock from tests.helpers import TEST_FIXTURES from tests.helpers.load_state_tests import ( @@ -134,7 +134,7 @@ def test_transaction_with_insufficient_balance_for_value() -> None: "4a62c29ca7f3a61e5519eabbf57a40bb28ee1f164839b3160281c30d2443a69e" ) - assert rlp.rlp_hash(genesis_header) == genesis_header_hash + assert keccak256(rlp.encode(genesis_header)) == genesis_header_hash genesis_block = FIXTURES_LOADER.fork.Block( genesis_header, diff --git a/tests/london/test_transaction.py b/tests/london/test_transaction.py index c519d7aa83..21b418d62c 100644 --- a/tests/london/test_transaction.py +++ b/tests/london/test_transaction.py @@ -1,8 +1,8 @@ from functools import partial import pytest +from ethereum_rlp import rlp -from ethereum import rlp from ethereum.london.fork import calculate_intrinsic_cost, validate_transaction from ethereum.london.transactions import LegacyTransaction from ethereum.utils.hexadecimal import hex_to_uint diff --git a/tests/paris/test_rlp.py b/tests/paris/test_rlp.py index aa9c582f32..33497fde81 100644 --- a/tests/paris/test_rlp.py +++ b/tests/paris/test_rlp.py @@ -1,8 +1,8 @@ import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes8, Bytes32 from ethereum_types.numeric import U64, U256, Uint -import ethereum.rlp as rlp from ethereum.crypto.hash import keccak256 from ethereum.paris.blocks import Block, Header, Log, Receipt from ethereum.paris.transactions import ( diff --git a/tests/paris/test_state_transition.py b/tests/paris/test_state_transition.py index e3e7ada840..7c5df593c1 100644 --- a/tests/paris/test_state_transition.py +++ b/tests/paris/test_state_transition.py @@ -2,11 +2,11 @@ from typing import Dict import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes8, Bytes32 from ethereum_types.numeric import U256, Uint -from ethereum import rlp -from ethereum.crypto.hash import Hash32 +from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidBlock from tests.helpers import TEST_FIXTURES from tests.helpers.load_state_tests import ( @@ -134,7 +134,7 @@ def test_transaction_with_insufficient_balance_for_value() -> None: "4a62c29ca7f3a61e5519eabbf57a40bb28ee1f164839b3160281c30d2443a69e" ) - assert rlp.rlp_hash(genesis_header) == genesis_header_hash + assert keccak256(rlp.encode(genesis_header)) == genesis_header_hash genesis_block = FIXTURES_LOADER.fork.Block( genesis_header, diff --git a/tests/shanghai/test_rlp.py b/tests/shanghai/test_rlp.py index 34c19612a9..7a7d689ef6 100644 --- a/tests/shanghai/test_rlp.py +++ b/tests/shanghai/test_rlp.py @@ -1,8 +1,8 @@ import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes8, Bytes32 from ethereum_types.numeric import U64, U256, Uint -import ethereum.rlp as rlp from ethereum.crypto.hash import keccak256 from ethereum.shanghai.blocks import Block, Header, Log, Receipt, Withdrawal from ethereum.shanghai.transactions import ( diff --git a/tests/spurious_dragon/test_ethash.py b/tests/spurious_dragon/test_ethash.py index 3605bc2c94..7a50cf3fe9 100644 --- a/tests/spurious_dragon/test_ethash.py +++ b/tests/spurious_dragon/test_ethash.py @@ -3,9 +3,9 @@ from typing import Any, Dict, List, cast import pytest +from ethereum_rlp import rlp from ethereum_types.numeric import Uint -from ethereum import rlp from ethereum.crypto.hash import keccak256 from ethereum.ethash import ( cache_size, diff --git a/tests/spurious_dragon/test_rlp.py b/tests/spurious_dragon/test_rlp.py index 63d1bd4696..05506ef6f1 100644 --- a/tests/spurious_dragon/test_rlp.py +++ b/tests/spurious_dragon/test_rlp.py @@ -1,8 +1,8 @@ import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes8 from ethereum_types.numeric import U256, Uint -import ethereum.rlp as rlp from ethereum.crypto.hash import keccak256 from ethereum.spurious_dragon.blocks import Block, Header, Log, Receipt from ethereum.spurious_dragon.transactions import Transaction diff --git a/tests/spurious_dragon/test_state_transition.py b/tests/spurious_dragon/test_state_transition.py index 278e6970c0..d3af5e4788 100644 --- a/tests/spurious_dragon/test_state_transition.py +++ b/tests/spurious_dragon/test_state_transition.py @@ -2,11 +2,11 @@ from typing import Dict import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes8, Bytes32 from ethereum_types.numeric import U256, Uint -from ethereum import rlp -from ethereum.crypto.hash import Hash32 +from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidBlock from tests.helpers import TEST_FIXTURES from tests.helpers.load_state_tests import ( @@ -130,7 +130,7 @@ def test_transaction_with_insufficient_balance_for_value() -> None: "0b22b0d49035cb4f8a969d584f36126e0ac6996b9db7264ac5a192b8698177eb" ) - assert rlp.rlp_hash(genesis_header) == genesis_header_hash + assert keccak256(rlp.encode(genesis_header)) == genesis_header_hash genesis_block = FIXTURES_LOADER.fork.Block( genesis_header, diff --git a/tests/spurious_dragon/test_transaction.py b/tests/spurious_dragon/test_transaction.py index 2a53546074..fc70c08d14 100644 --- a/tests/spurious_dragon/test_transaction.py +++ b/tests/spurious_dragon/test_transaction.py @@ -1,8 +1,8 @@ from functools import partial import pytest +from ethereum_rlp import rlp -from ethereum import rlp from ethereum.spurious_dragon.fork import ( calculate_intrinsic_cost, validate_transaction, diff --git a/tests/tangerine_whistle/test_ethash.py b/tests/tangerine_whistle/test_ethash.py index a74ea86fea..fe82d033d8 100644 --- a/tests/tangerine_whistle/test_ethash.py +++ b/tests/tangerine_whistle/test_ethash.py @@ -3,9 +3,9 @@ from typing import Any, Dict, List, cast import pytest +from ethereum_rlp import rlp from ethereum_types.numeric import Uint -from ethereum import rlp from ethereum.crypto.hash import keccak256 from ethereum.ethash import ( cache_size, diff --git a/tests/tangerine_whistle/test_rlp.py b/tests/tangerine_whistle/test_rlp.py index cee5ddaba7..3a4a30074a 100644 --- a/tests/tangerine_whistle/test_rlp.py +++ b/tests/tangerine_whistle/test_rlp.py @@ -1,8 +1,8 @@ import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes0, Bytes8 from ethereum_types.numeric import U256, Uint -import ethereum.rlp as rlp from ethereum.crypto.hash import keccak256 from ethereum.tangerine_whistle.blocks import Block, Header, Log, Receipt from ethereum.tangerine_whistle.transactions import Transaction diff --git a/tests/tangerine_whistle/test_state_transition.py b/tests/tangerine_whistle/test_state_transition.py index 55768954d9..c56202145b 100644 --- a/tests/tangerine_whistle/test_state_transition.py +++ b/tests/tangerine_whistle/test_state_transition.py @@ -2,11 +2,11 @@ from typing import Dict import pytest +from ethereum_rlp import rlp from ethereum_types.bytes import Bytes, Bytes8, Bytes32 from ethereum_types.numeric import U256, Uint -from ethereum import rlp -from ethereum.crypto.hash import Hash32 +from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import InvalidBlock from tests.helpers import TEST_FIXTURES from tests.helpers.load_state_tests import ( @@ -132,7 +132,7 @@ def test_transaction_with_insufficient_balance_for_value() -> None: "0b22b0d49035cb4f8a969d584f36126e0ac6996b9db7264ac5a192b8698177eb" ) - assert rlp.rlp_hash(genesis_header) == genesis_header_hash + assert keccak256(rlp.encode(genesis_header)) == genesis_header_hash genesis_block = FIXTURES_LOADER.fork.Block( genesis_header, diff --git a/tests/tangerine_whistle/test_transaction.py b/tests/tangerine_whistle/test_transaction.py index b99a80f469..7e3a253fdb 100644 --- a/tests/tangerine_whistle/test_transaction.py +++ b/tests/tangerine_whistle/test_transaction.py @@ -1,8 +1,8 @@ from functools import partial import pytest +from ethereum_rlp import rlp -from ethereum import rlp from ethereum.tangerine_whistle.fork import ( calculate_intrinsic_cost, validate_transaction, diff --git a/tests/test_genesis.py b/tests/test_genesis.py index a75d73adfe..3dc725c151 100644 --- a/tests/test_genesis.py +++ b/tests/test_genesis.py @@ -1,7 +1,7 @@ import pytest +from ethereum_rlp import rlp from ethereum_types.numeric import U64 -from ethereum import rlp from ethereum.crypto.hash import keccak256 from ethereum.frontier.blocks import Block, Header from ethereum.frontier.fork import BlockChain diff --git a/tests/test_rlp.py b/tests/test_rlp.py index 2c1a0f2d19..1a845d43a2 100644 --- a/tests/test_rlp.py +++ b/tests/test_rlp.py @@ -1,346 +1,17 @@ import json import os -from typing import List, Sequence, Tuple, Union, cast +from typing import List, Sequence, Tuple, Union import pytest +from ethereum_rlp import Extended, rlp -from ethereum import rlp -from ethereum.exceptions import RLPDecodingError, RLPEncodingError -from ethereum.frontier.fork_types import U256, Bytes, Uint -from ethereum.rlp import Extended +from ethereum.frontier.fork_types import Bytes, Uint from ethereum.utils.hexadecimal import hex_to_bytes from tests.helpers import TEST_FIXTURES ETHEREUM_TESTS_PATH = TEST_FIXTURES["ethereum_tests"]["fixture_path"] -# -# Tests for RLP encode -# - -# -# Testing bytes encoding -# - - -def test_rlp_encode_empty_bytes() -> None: - assert rlp.encode_bytes(b"") == bytearray([0x80]) - assert rlp.encode_bytes(bytearray()) == bytearray([0x80]) - - -def test_rlp_encode_single_byte_val_less_than_128() -> None: - assert rlp.encode_bytes(b"x") == bytearray([0x78]) - assert rlp.encode_bytes(bytearray(b"x")) == bytearray([0x78]) - - -def test_rlp_encode_single_byte_val_equal_128() -> None: - assert rlp.encode_bytes(b"\x80") == b"\x81\x80" - assert rlp.encode_bytes(bytearray(b"\x80")) == b"\x81\x80" - - -def test_rlp_encode_single_byte_val_greater_than_128() -> None: - assert rlp.encode_bytes(b"\x83") == bytearray([0x81, 0x83]) - assert rlp.encode_bytes(bytearray(b"\x83")) == bytearray([0x81, 0x83]) - - -def test_rlp_encode_55_bytes() -> None: - assert rlp.encode_bytes(b"\x83" * 55) == bytearray([0xB7]) + bytearray( - b"\x83" * 55 - ) - assert rlp.encode_bytes(bytearray(b"\x83") * 55) == bytearray( - [0xB7] - ) + bytearray(b"\x83" * 55) - - -def test_rlp_encode_large_bytes() -> None: - assert rlp.encode_bytes(b"\x83" * 2**20) == ( - bytearray([0xBA]) - + bytearray(b"\x10\x00\x00") - + bytearray(b"\x83" * 2**20) - ) - assert rlp.encode_bytes(bytearray(b"\x83") * 2**20) == ( - bytearray([0xBA]) - + bytearray(b"\x10\x00\x00") - + bytearray(b"\x83" * 2**20) - ) - - -# -# Testing uint and u256 encoding -# - - -def test_rlp_encode_uint_0() -> None: - assert rlp.encode(Uint(0)) == b"\x80" - - -def test_rlp_encode_uint_byte_max() -> None: - assert rlp.encode(Uint(255)) == b"\x81\xff" - - -def test_rlp_encode_uint256_0() -> None: - assert rlp.encode(U256(0)) == b"\x80" - - -def test_rlp_encode_uint256_byte_max() -> None: - assert rlp.encode(U256(255)) == b"\x81\xff" - - -# -# Testing str encoding -# - - -def test_rlp_encode_empty_str() -> None: - assert rlp.encode("") == b"\x80" - - -def test_rlp_encode_one_char_str() -> None: - assert rlp.encode("h") == b"h" - - -def test_rlp_encode_multi_char_str() -> None: - assert rlp.encode("hello") == b"\x85hello" - - -# -# Testing sequence encoding -# - - -def test_rlp_encode_empty_sequence() -> None: - assert rlp.encode_sequence([]) == bytearray([0xC0]) - - -def test_rlp_encode_single_elem_list_byte() -> None: - assert rlp.encode_sequence([b"hello"]) == bytearray([0xC6]) + b"\x85hello" - - -def test_rlp_encode_single_elem_list_uint() -> None: - assert rlp.encode_sequence([Uint(255)]) == bytearray([0xC2]) + b"\x81\xff" - - -def test_rlp_encode_10_elem_byte_uint_combo() -> None: - raw_data = [b"hello"] * 5 + [Uint(35)] * 5 - expected = ( - bytearray([0xE3]) - + b"\x85hello\x85hello\x85hello\x85hello\x85hello#####" - ) - assert rlp.encode_sequence(raw_data) == expected - - -def test_rlp_encode_20_elem_byte_uint_combo() -> None: - raw_data = [Uint(35)] * 10 + [b"hello"] * 10 - expected = ( - bytearray([0xF8]) - + b"F" - + b"##########\x85hello\x85hello\x85hello\x85hello\x85hello\x85hello\x85hello\x85hello\x85hello\x85hello" - ) - assert rlp.encode_sequence(raw_data) == expected - - -def test_rlp_encode_nested_sequence() -> None: - nested_sequence: Sequence["Extended"] = [ - b"hello", - Uint(255), - [b"how", [b"are", b"you", [b"doing"]]], - ] - expected: Bytes = ( - b"\xdd\x85hello\x81\xff\xd4\x83how\xcf\x83are\x83you\xc6\x85doing" - ) - assert rlp.encode_sequence(nested_sequence) == expected - - -def test_rlp_encode_successfully() -> None: - test_cases: List[Tuple[rlp.Extended, Union[bytes, bytearray]]] = [ - (b"", bytearray([0x80])), - (b"\x83" * 55, bytearray([0xB7]) + bytearray(b"\x83" * 55)), - (Uint(0), b"\x80"), - (Uint(255), b"\x81\xff"), - ([], bytearray([0xC0])), - ( - [b"hello"] * 5 + [Uint(35)] * 5, - bytearray([0xE3]) - + bytearray(b"\x85hello\x85hello\x85hello\x85hello\x85hello#####"), - ), - ( - [b"hello", Uint(255), [b"how", [b"are", b"you", [b"doing"]]]], - bytearray( - b"\xdd\x85hello\x81\xff\xd4\x83how\xcf\x83are\x83you\xc6\x85doing" - ), - ), - ] - for raw_data, expected_encoding in test_cases: - assert rlp.encode(raw_data) == expected_encoding - - -def test_rlp_encode_fails() -> None: - test_cases = [ - 123, - [b"hello", Uint(255), [b"how", [b"are", [b"you", [123]]]]], - ] - for raw_data in test_cases: - with pytest.raises(RLPEncodingError): - rlp.encode(cast(Extended, raw_data)) - - -# -# Tests for RLP decode -# - -# -# Testing bytes decoding -# - - -def test_rlp_decode_to_empty_bytes() -> None: - assert rlp.decode_to_bytes(bytearray([0x80])) == b"" - - -def test_rlp_decode_to_single_byte_less_than_128() -> None: - assert rlp.decode_to_bytes(bytearray([0])) == bytearray([0]) - assert rlp.decode_to_bytes(bytearray([0x78])) == bytearray([0x78]) - - -def test_rlp_decode_to_single_byte_gte_128() -> None: - assert rlp.decode_to_bytes(bytearray([0x81, 0x83])) == b"\x83" - assert rlp.decode_to_bytes(b"\x81\x80") == b"\x80" - - -def test_rlp_decode_to_55_bytes() -> None: - encoding = bytearray([0xB7]) + bytearray(b"\x83" * 55) - expected_raw_data = bytearray(b"\x83") * 55 - assert rlp.decode_to_bytes(encoding) == expected_raw_data - - -def test_rlp_decode_to_large_bytes() -> None: - encoding = bytearray([0xBA]) + b"\x10\x00\x00" + b"\x83" * (2**20) - expected_raw_data = b"\x83" * (2**20) - assert rlp.decode_to_bytes(encoding) == expected_raw_data - - -# -# Testing uint decoding -# - - -def test_rlp_decode_to_zero_uint() -> None: - assert rlp.decode(b"\x80") == Uint(0).to_be_bytes() - - -def test_rlp_decode_to_255_uint() -> None: - assert rlp.decode(b"\x81\xff") == Uint(255).to_be_bytes() - - -# -# Testing string decoding -# - - -def test_rlp_decode_empty_str() -> None: - assert rlp.decode(b"\x80") == "".encode() - - -def test_rlp_decode_one_char_str() -> None: - assert rlp.decode(b"h") == "h".encode() - - -def test_rlp_decode_multi_char_str() -> None: - assert rlp.decode(b"\x85hello") == "hello".encode() - - -# -# Testing sequence decoding -# - - -def test_rlp_decode_to_empty_sequence() -> None: - assert rlp.decode_to_sequence(bytearray([0xC0])) == [] - - -def test_rlp_decode_to_1_elem_sequence_of_byte() -> None: - assert rlp.decode_to_sequence(bytearray([0xC6]) + b"\x85hello") == [ - b"hello" - ] - - -def test_rlp_decode_to_1_elem_sequence_of_uint() -> None: - assert rlp.decode_to_sequence(bytearray([0xC2]) + b"\x81\xff") == [ - Uint(255).to_be_bytes() - ] - - -def test_rlp_decode_to_10_elem_sequence_of_bytes_and_uints() -> None: - encoded_data = ( - bytearray([0xE3]) - + b"\x85hello\x85hello\x85hello\x85hello\x85hello#####" - ) - expected_raw_data = [b"hello"] * 5 + [Uint(35).to_be_bytes()] * 5 - assert rlp.decode_to_sequence(encoded_data) == expected_raw_data - - -def test_rlp_decode_to_20_elem_sequence_of_bytes_and_uints() -> None: - encoded_data = ( - bytearray([0xF8]) - + b"F" - + b"\x85hello\x85hello\x85hello\x85hello\x85hello\x85hello\x85hello\x85hello\x85hello\x85hello##########" - ) - expected_raw_data = [b"hello"] * 10 + [Uint(35).to_be_bytes()] * 10 - assert rlp.decode_to_sequence(encoded_data) == expected_raw_data - - -def test_rlp_decode_to_nested_sequence() -> None: - encoded_data = ( - b"\xdf\x85hello\x81\xff\xd6\x83how\xd1\x83are\x83you\xc8\x85doing\xc1#" - ) - expected_raw_data = [ - b"hello", - Uint(255).to_be_bytes(), - [ - b"how", - [b"are", b"you", [b"doing", [Uint(35).to_be_bytes()]]], - ], - ] - assert rlp.decode_to_sequence(encoded_data) == expected_raw_data - - -def test_rlp_decode_successfully() -> None: - test_cases = [ - (bytearray([0x80]), bytearray()), - (bytearray([0xB7]) + bytearray(b"\x83" * 55), bytearray(b"\x83") * 55), - (bytearray([0xC0]), []), - ( - b"\xdb\x85hello\xd4\x83how\xcf\x83are\x83you\xc6\x85doing", - [b"hello", [b"how", [b"are", b"you", [b"doing"]]]], - ), - ] - for encoding, expected_raw_data in test_cases: - assert rlp.decode(encoding) == expected_raw_data - - -def test_rlp_decode_failure_empty_bytes() -> None: - with pytest.raises(RLPDecodingError): - rlp.decode(b"") - - -def test_roundtrip_encoding_and_decoding() -> None: - test_cases: List[Extended] = [ - b"", - b"h", - b"hello how are you doing today?", - Uint(35).to_be_bytes(), - Uint(255).to_be_bytes(), - [], - [ - b"hello", - [b"how", [b"are", b"you", [b"doing", [Uint(255).to_be_bytes()]]]], - ], - [[b"hello", b"world"], [b"how", b"are"], [b"you", b"doing"]], - ] - for raw_data in test_cases: - assert rlp.decode(rlp.encode(raw_data)) == raw_data - - # # Running ethereum/tests for rlp # @@ -413,5 +84,5 @@ def test_ethtest_fixtures_for_successfully_rlp_decoding( def test_ethtest_fixtures_for_fails_in_rlp_decoding( raw_data: Bytes, encoded_data: Bytes ) -> None: - with pytest.raises(RLPDecodingError): + with pytest.raises(rlp.DecodingError): rlp.decode(encoded_data)